[epiphany] Set mobile user agent depending on chassis type



commit 3d52d36ac5f6b44185a967e21e07ce459958917c
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Sat Feb 29 19:52:40 2020 +0100

    Set mobile user agent depending on chassis type

 data/org.gnome.epiphany.gschema.xml |  5 ----
 embed/ephy-embed-prefs.c            |  5 ----
 lib/ephy-prefs.h                    |  2 --
 lib/ephy-user-agent.c               | 49 ++++++++++++++++++++++++++++++++++---
 lib/ephy-user-agent.h               |  1 +
 src/ephy-shell.c                    |  2 ++
 6 files changed, 48 insertions(+), 16 deletions(-)
---
diff --git a/data/org.gnome.epiphany.gschema.xml b/data/org.gnome.epiphany.gschema.xml
index 948e3c141..cb2227038 100644
--- a/data/org.gnome.epiphany.gschema.xml
+++ b/data/org.gnome.epiphany.gschema.xml
@@ -166,11 +166,6 @@
                        <summary>User agent</summary>
                        <description>String that will be used as user agent, to identify the browser to the 
web servers.</description>
                </key>
-               <key type="b" name="mobile-user-agent">
-                       <default>false</default>
-                       <summary>Mobile user agent</summary>
-                       <description>Whether to present a mobile user agent. If the user agent is overridden, 
this will have no effect.</description>
-               </key>
                <key type="b" name="enable-adblock">
                        <default>true</default>
                        <summary>Enable adblock</summary>
diff --git a/embed/ephy-embed-prefs.c b/embed/ephy-embed-prefs.c
index 3415f4141..311802eb5 100644
--- a/embed/ephy-embed-prefs.c
+++ b/embed/ephy-embed-prefs.c
@@ -474,11 +474,6 @@ static const PrefData webkit_pref_entries[] = {
     EPHY_PREFS_WEB_USER_AGENT,
     "user-agent",
     webkit_pref_callback_user_agent },
-  { EPHY_PREFS_WEB_SCHEMA,
-    EPHY_PREFS_WEB_MOBILE_USER_AGENT,
-    "mobile-user-agent",
-    /* No need for a specific callback, we just want to update the user agent. */
-    webkit_pref_callback_user_agent },
   { EPHY_PREFS_WEB_SCHEMA,
     EPHY_PREFS_WEB_COOKIES_POLICY,
     "accept-policy",
diff --git a/lib/ephy-prefs.h b/lib/ephy-prefs.h
index c0b651f22..714c2f78c 100644
--- a/lib/ephy-prefs.h
+++ b/lib/ephy-prefs.h
@@ -108,7 +108,6 @@ static const char * const ephy_prefs_state_schema[] = {
 #define EPHY_PREFS_WEB_ENABLE_SPELL_CHECKING        "enable-spell-checking"
 #define EPHY_PREFS_WEB_ENABLE_SMOOTH_SCROLLING      "enable-smooth-scrolling"
 #define EPHY_PREFS_WEB_USER_AGENT                   "user-agent"
-#define EPHY_PREFS_WEB_MOBILE_USER_AGENT            "mobile-user-agent"
 #define EPHY_PREFS_WEB_COOKIES_POLICY               "cookies-policy"
 #define EPHY_PREFS_WEB_DEFAULT_ENCODING             "default-encoding"
 #define EPHY_PREFS_WEB_ENABLE_ADBLOCK               "enable-adblock"
@@ -136,7 +135,6 @@ static const char * const ephy_prefs_web_schema[] = {
   EPHY_PREFS_WEB_ENABLE_SPELL_CHECKING,
   EPHY_PREFS_WEB_ENABLE_SMOOTH_SCROLLING,
   EPHY_PREFS_WEB_USER_AGENT,
-  EPHY_PREFS_WEB_MOBILE_USER_AGENT,
   EPHY_PREFS_WEB_COOKIES_POLICY,
   EPHY_PREFS_WEB_DEFAULT_ENCODING,
   EPHY_PREFS_WEB_ENABLE_ADBLOCK,
diff --git a/lib/ephy-user-agent.c b/lib/ephy-user-agent.c
index 56119b888..631005193 100644
--- a/lib/ephy-user-agent.c
+++ b/lib/ephy-user-agent.c
@@ -26,12 +26,54 @@
 
 #include <webkit2/webkit2.h>
 
+static gboolean mobile_user_agent = FALSE;
+static char *user_agent = NULL;
+
+void
+ephy_user_agent_init_sync (void)
+{
+  g_autoptr (GError) error = NULL;
+  g_autoptr (GDBusConnection) connection = NULL;
+  g_autoptr (GVariant) var = NULL;
+  g_autoptr (GVariant) v = NULL;
+  const char *chassis;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
+  if (!connection) {
+    g_debug ("Could not connect to system bus: %s", error->message);
+    return;
+  }
+
+  var = g_dbus_connection_call_sync (connection,
+                                     "org.freedesktop.hostname1",
+                                     "/org/freedesktop/hostname1",
+                                     "org.freedesktop.DBus.Properties",
+                                     "Get",
+                                     g_variant_new ("(ss)",
+                                                    "org.freedesktop.hostname1",
+                                                    "Chassis"),
+                                     NULL,
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     -1,
+                                     NULL,
+                                     &error);
+
+  if (!var) {
+    g_debug ("Could not access chassis property: %s", error->message);
+    return;
+  }
+
+  g_variant_get (var, "(v)", &v);
+  chassis = g_variant_get_string (v, NULL);
+  mobile_user_agent = g_strcmp0 (chassis, "handset") == 0;
+
+  g_clear_pointer (&user_agent, g_free);
+}
+
 const char *
 ephy_user_agent_get (void)
 {
-  static char *user_agent = NULL;
   WebKitSettings *settings;
-  gboolean mobile;
   gboolean web_app;
 
   if (user_agent)
@@ -44,13 +86,12 @@ ephy_user_agent_get (void)
     g_free (user_agent);
   }
 
-  mobile = g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_MOBILE_USER_AGENT);
   web_app = ephy_profile_dir_is_web_application ();
 
   settings = webkit_settings_new ();
   user_agent = g_strdup_printf ("%s%s%s",
                                 webkit_settings_get_user_agent (settings),
-                                mobile ? " Mobile" : "",
+                                mobile_user_agent ? " Mobile" : "",
                                 web_app ? " (Web App)" : "");
   g_object_unref (settings);
 
diff --git a/lib/ephy-user-agent.h b/lib/ephy-user-agent.h
index 353bbbb49..ce57b5315 100644
--- a/lib/ephy-user-agent.h
+++ b/lib/ephy-user-agent.h
@@ -25,5 +25,6 @@
 G_BEGIN_DECLS
 
 const char *ephy_user_agent_get (void);
+void ephy_user_agent_init_sync (void);
 
 G_END_DECLS
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index 95e4e5b94..fbba72b18 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -40,6 +40,7 @@
 #include "ephy-title-box.h"
 #include "ephy-title-widget.h"
 #include "ephy-type-builtins.h"
+#include "ephy-user-agent.h"
 #include "ephy-web-view.h"
 #include "ephy-window.h"
 #include "prefs-dialog.h"
@@ -400,6 +401,7 @@ ephy_shell_startup (GApplication *application)
   G_APPLICATION_CLASS (ephy_shell_parent_class)->startup (application);
 
   /* We're not remoting; start our services */
+  ephy_user_agent_init_sync ();
 
   mode = ephy_embed_shell_get_mode (embed_shell);
   if (mode != EPHY_EMBED_SHELL_MODE_APPLICATION) {


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