[epiphany/wip/sync: 72/74] Move user agent functions from embed to lib



commit c19c101d212d7ba947af728c9993f1f8e445686d
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Wed Aug 2 00:43:42 2017 +0100

    Move user agent functions from embed to lib
    
    This way EphySyncService can use the WebKit user agent too.

 embed/ephy-embed-prefs.c     |   84 +--------------------------------
 lib/ephy-user-agent.c        |  104 ++++++++++++++++++++++++++++++++++++++++++
 lib/ephy-user-agent.h        |   29 ++++++++++++
 lib/meson.build              |    1 +
 lib/sync/ephy-sync-service.c |    8 ++--
 5 files changed, 141 insertions(+), 85 deletions(-)
---
diff --git a/embed/ephy-embed-prefs.c b/embed/ephy-embed-prefs.c
index ae3f728..04c3046 100644
--- a/embed/ephy-embed-prefs.c
+++ b/embed/ephy-embed-prefs.c
@@ -27,10 +27,9 @@
 #include "ephy-langs.h"
 #include "ephy-prefs.h"
 #include "ephy-settings.h"
+#include "ephy-user-agent.h"
 
-#include <glib.h>
 #include <math.h>
-#include <webkit2/webkit2.h>
 
 typedef struct {
   const char *schema;
@@ -111,99 +110,22 @@ webkit_pref_callback_user_stylesheet (GSettings  *settings,
   }
 }
 
-static char *
-webkit_pref_get_vendor_user_agent (void)
-{
-  char *vendor_user_agent = NULL;
-  GKeyFile *branding_keyfile;
-
-  branding_keyfile = g_key_file_new ();
-
-  if (g_key_file_load_from_file (branding_keyfile, PKGDATADIR "/branding.conf",
-                                 G_KEY_FILE_NONE, NULL)) {
-    char *vendor;
-    char *vendor_sub;
-    char *vendor_comment;
-
-    vendor = g_key_file_get_string (branding_keyfile,
-                                    "User Agent", "Vendor", NULL);
-    vendor_sub = g_key_file_get_string (branding_keyfile,
-                                        "User Agent", "VendorSub", NULL);
-    vendor_comment = g_key_file_get_string (branding_keyfile,
-                                            "User Agent", "VendorComment", NULL);
-
-    if (vendor) {
-      vendor_user_agent = g_strconcat (vendor,
-                                       vendor_sub ? "/" : "",
-                                       vendor_sub ? vendor_sub : "",
-                                       vendor_comment ? " (" : "",
-                                       vendor_comment ? vendor_comment : "",
-                                       vendor_comment ? ")" : "",
-                                       NULL);
-    }
-
-    g_free (vendor);
-    g_free (vendor_sub);
-    g_free (vendor_comment);
-  }
-
-  g_key_file_free (branding_keyfile);
-
-  return vendor_user_agent;
-}
-
-static const char *
-webkit_pref_get_internal_user_agent (void)
-{
-  static char *user_agent = NULL;
-  static gboolean initialized = FALSE;
-  char *vendor_user_agent;
-  const char *webkit_user_agent;
-
-  if (initialized)
-    return user_agent;
-
-  initialized = TRUE;
-
-  vendor_user_agent = webkit_pref_get_vendor_user_agent ();
-  webkit_user_agent = webkit_settings_get_user_agent (webkit_settings);
-
-  if (vendor_user_agent) {
-    user_agent = g_strdup_printf ("%s %s Epiphany/%s", webkit_user_agent,
-                                  vendor_user_agent, VERSION);
-    g_free (vendor_user_agent);
-  } else {
-    user_agent = g_strdup_printf ("%s Epiphany/%s", webkit_user_agent, VERSION);
-  }
-
-  return user_agent;
-}
-
 static void
 webkit_pref_callback_user_agent (GSettings  *settings,
                                  const char *key,
                                  gpointer    data)
 {
   EphyEmbedShell *shell = ephy_embed_shell_get_default ();
-  char *value;
   char *user_agent;
-  const char *base_user_agent;
-
-  value = g_settings_get_string (settings, key);
-  if (value != NULL && value[0] != '\0')
-    base_user_agent = value;
-  else
-    base_user_agent = webkit_pref_get_internal_user_agent ();
 
   if (ephy_embed_shell_get_mode (shell) == EPHY_EMBED_SHELL_MODE_APPLICATION)
-    user_agent = g_strdup_printf ("%s (Web App)", base_user_agent);
+    user_agent = g_strdup_printf ("%s (Web App)", ephy_user_agent_get_internal ());
   else
-    user_agent = g_strdup (base_user_agent);
+    user_agent = g_strdup (ephy_user_agent_get_internal ());
 
   webkit_settings_set_user_agent (webkit_settings, user_agent);
 
   g_free (user_agent);
-  g_free (value);
 }
 
 static gdouble
diff --git a/lib/ephy-user-agent.c b/lib/ephy-user-agent.c
new file mode 100644
index 0000000..30859f1
--- /dev/null
+++ b/lib/ephy-user-agent.c
@@ -0,0 +1,104 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ *  Copyright © 2017 Gabriel Ivascu <ivascu gabriel59 gmail com>
+ *
+ *  This file is part of Epiphany.
+ *
+ *  Epiphany is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Epiphany is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "ephy-user-agent.h"
+
+#include "ephy-settings.h"
+
+#include <webkit2/webkit2.h>
+
+static char *
+webkit_pref_get_vendor_user_agent (void)
+{
+  GKeyFile *branding_keyfile;
+  char *vendor_user_agent = NULL;
+
+  branding_keyfile = g_key_file_new ();
+
+  if (g_key_file_load_from_file (branding_keyfile, PKGDATADIR "/branding.conf",
+                                 G_KEY_FILE_NONE, NULL)) {
+    char *vendor;
+    char *vendor_sub;
+    char *vendor_comment;
+
+    vendor = g_key_file_get_string (branding_keyfile,
+                                    "User Agent", "Vendor", NULL);
+    vendor_sub = g_key_file_get_string (branding_keyfile,
+                                        "User Agent", "VendorSub", NULL);
+    vendor_comment = g_key_file_get_string (branding_keyfile,
+                                            "User Agent", "VendorComment", NULL);
+
+    if (vendor) {
+      vendor_user_agent = g_strconcat (vendor,
+                                       vendor_sub ? "/" : "",
+                                       vendor_sub ? vendor_sub : "",
+                                       vendor_comment ? " (" : "",
+                                       vendor_comment ? vendor_comment : "",
+                                       vendor_comment ? ")" : "",
+                                       NULL);
+    }
+
+    g_free (vendor);
+    g_free (vendor_sub);
+    g_free (vendor_comment);
+  }
+
+  g_key_file_free (branding_keyfile);
+
+  return vendor_user_agent;
+}
+
+const char *
+ephy_user_agent_get_internal (void)
+{
+  WebKitSettings *settings;
+  const char *webkit_user_agent;
+  char *vendor_user_agent;
+  static char *user_agent = NULL;
+
+  if (user_agent)
+    return user_agent;
+
+  user_agent = g_settings_get_string (EPHY_SETTINGS_WEB,
+                                      EPHY_PREFS_WEB_USER_AGENT);
+  if (user_agent && user_agent[0])
+    return user_agent;
+
+  settings = webkit_settings_new ();
+  webkit_user_agent = webkit_settings_get_user_agent (settings);
+  vendor_user_agent = webkit_pref_get_vendor_user_agent ();
+
+  if (vendor_user_agent) {
+    user_agent = g_strdup_printf ("%s %s Epiphany/%s",
+                                  webkit_user_agent,
+                                  vendor_user_agent,
+                                  VERSION);
+  } else {
+    user_agent = g_strdup_printf ("%s Epiphany/%s",
+                                  webkit_user_agent,
+                                  VERSION);
+  }
+
+  g_free (vendor_user_agent);
+  g_object_unref (settings);
+
+  return user_agent;
+}
diff --git a/lib/ephy-user-agent.h b/lib/ephy-user-agent.h
new file mode 100644
index 0000000..4294172
--- /dev/null
+++ b/lib/ephy-user-agent.h
@@ -0,0 +1,29 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ *  Copyright © 2017 Gabriel Ivascu <ivascu gabriel59 gmail com>
+ *
+ *  This file is part of Epiphany.
+ *
+ *  Epiphany is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Epiphany is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+const char *ephy_user_agent_get_internal (void);
+
+G_END_DECLS
diff --git a/lib/meson.build b/lib/meson.build
index 9b68094..d2ee3bf 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -37,6 +37,7 @@ libephymisc_sources = [
   'ephy-time-helpers.c',
   'ephy-uri-helpers.c',
   'ephy-uri-tester-shared.c',
+  'ephy-user-agent.c',
   'ephy-web-app-utils.c',
   'ephy-zoom.c',
   'history/ephy-history-service.c',
diff --git a/lib/sync/ephy-sync-service.c b/lib/sync/ephy-sync-service.c
index 677fcd0..5f3c230 100644
--- a/lib/sync/ephy-sync-service.c
+++ b/lib/sync/ephy-sync-service.c
@@ -26,6 +26,7 @@
 #include "ephy-settings.h"
 #include "ephy-sync-crypto.h"
 #include "ephy-sync-utils.h"
+#include "ephy-user-agent.h"
 
 #include <glib/gi18n.h>
 #include <json-glib/json-glib.h>
@@ -1615,10 +1616,9 @@ ephy_sync_service_constructed (GObject *object)
   G_OBJECT_CLASS (ephy_sync_service_parent_class)->constructed (object);
 
   if (self->sync_periodically) {
-    char *user_agent = g_settings_get_string (EPHY_SETTINGS_WEB,
-                                              EPHY_PREFS_WEB_USER_AGENT);
-    g_object_set (self->session, "user-agent", user_agent, NULL);
-    g_free (user_agent);
+    g_object_set (self->session,
+                  "user-agent", ephy_user_agent_get_internal (),
+                  NULL);
 
     g_signal_connect (EPHY_SETTINGS_SYNC, "changed::"EPHY_PREFS_SYNC_FREQUENCY,
                       G_CALLBACK (sync_frequency_changed_cb), self);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]