Bug 1437869 - [2.2] Add desktop mode session setting flag. r=snorp,jchen
authorEugen Sawin <esawin@mozilla.com>
Wed, 07 Mar 2018 22:47:12 +0100
changeset 462422 a2a51018aa5f04013f6772306d8154389082c0ff
parent 462421 c59f42a79b5a3c74d7d5d2279b5dc52ba3564034
child 462423 80169b0256617adc6be618a203bd01d6f2dd9499
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, jchen
bugs1437869
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1437869 - [2.2] Add desktop mode session setting flag. r=snorp,jchen
mobile/android/chrome/geckoview/GeckoViewContentSettings.js
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSessionSettings.java
mobile/android/modules/geckoview/GeckoViewSettings.jsm
--- a/mobile/android/chrome/geckoview/GeckoViewContentSettings.js
+++ b/mobile/android/chrome/geckoview/GeckoViewContentSettings.js
@@ -16,28 +16,45 @@ function debug(aMsg) {
 
 // Handles GeckoView content settings including:
 // * tracking protection
 // * desktop mode
 class GeckoViewContentSettings extends GeckoViewContentModule {
   init() {
     debug("init");
     this._useTrackingProtection = false;
+    this._useDesktopMode = false;
   }
 
   onSettingsUpdate() {
     debug("onSettingsUpdate");
+
     this.useTrackingProtection = !!this.settings.useTrackingProtection;
+    this.useDesktopMode = !!this.settings.useDesktopMode;
   }
 
   get useTrackingProtection() {
     return this._useTrackingProtection;
   }
 
   set useTrackingProtection(aUse) {
     if (aUse != this._useTrackingProtection) {
       docShell.useTrackingProtection = aUse;
       this._useTrackingProtection = aUse;
     }
   }
+
+  get useDesktopMode() {
+    return this._useDesktopMode;
+  }
+
+  set useDesktopMode(aUse) {
+    if (this.useDesktopMode === aUse) {
+      return;
+    }
+    let utils = content.QueryInterface(Ci.nsIInterfaceRequestor)
+                .getInterface(Ci.nsIDOMWindowUtils);
+    utils.setDesktopModeViewport(aUse);
+    this._useDesktopMode = aUse;
+  }
 }
 
 var settings = new GeckoViewContentSettings("GeckoViewSettings", this);
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSessionSettings.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSessionSettings.java
@@ -72,16 +72,22 @@ public final class GeckoSessionSettings 
     /*
      * Key to enable and disable multiprocess browsing (e10s).
      * Read-only once session is open.
      */
     public static final Key<Boolean> USE_MULTIPROCESS =
         new Key<Boolean>("useMultiprocess", /* initOnly */ true, /* values */ null);
 
     /*
+     * Key to enable and disable desktop mode browsing.
+     */
+    public static final Key<Boolean> USE_DESKTOP_MODE =
+        new Key<Boolean>("useDesktopMode");
+
+    /*
      * Key to specify which display-mode we should use
      */
     public static final Key<Integer> DISPLAY_MODE =
         new Key<Integer>("displayMode", /* initOnly */ false,
                          Arrays.asList(DISPLAY_MODE_BROWSER, DISPLAY_MODE_MINIMAL_UI,
                                        DISPLAY_MODE_STANDALONE, DISPLAY_MODE_FULLSCREEN));
 
     public static final Key<Boolean> USE_REMOTE_DEBUGGER =
@@ -108,16 +114,17 @@ public final class GeckoSessionSettings 
         }
 
         mBundle = new GeckoBundle();
         mBundle.putString(CHROME_URI.name, null);
         mBundle.putInt(SCREEN_ID.name, 0);
         mBundle.putBoolean(USE_TRACKING_PROTECTION.name, false);
         mBundle.putBoolean(USE_PRIVATE_MODE.name, false);
         mBundle.putBoolean(USE_MULTIPROCESS.name, true);
+        mBundle.putBoolean(USE_DESKTOP_MODE.name, false);
         mBundle.putInt(DISPLAY_MODE.name, DISPLAY_MODE_BROWSER);
         mBundle.putBoolean(USE_REMOTE_DEBUGGER.name, false);
     }
 
     public void setBoolean(final Key<Boolean> key, final boolean value) {
         synchronized (mBundle) {
             if (valueChangedLocked(key, value)) {
                 mBundle.putBoolean(key.name, value);
--- a/mobile/android/modules/geckoview/GeckoViewSettings.jsm
+++ b/mobile/android/modules/geckoview/GeckoViewSettings.jsm
@@ -6,48 +6,59 @@
 
 var EXPORTED_SYMBOLS = ["GeckoViewSettings"];
 
 ChromeUtils.import("resource://gre/modules/GeckoViewModule.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   SafeBrowsing: "resource://gre/modules/SafeBrowsing.jsm",
-  UserAgentOverrides: "resource://gre/modules/UserAgentOverrides.jsm",
+  Services: "resource://gre/modules/Services.jsm",
 });
 
+XPCOMUtils.defineLazyGetter(
+  this, "DESKTOP_USER_AGENT",
+  function() {
+    return Cc["@mozilla.org/network/protocol;1?name=http"]
+           .getService(Ci.nsIHttpProtocolHandler).userAgent
+           .replace(/Android \d.+?; [a-zA-Z]+/, "X11; Linux x86_64")
+           .replace(/Gecko\/[0-9\.]+/, "Gecko/20100101");
+  });
+
 XPCOMUtils.defineLazyGetter(this, "dump", () =>
     ChromeUtils.import("resource://gre/modules/AndroidLog.jsm",
                        {}).AndroidLog.d.bind(null, "ViewSettings"));
 
 function debug(aMsg) {
   // dump(aMsg);
 }
 
 // Handles GeckoView settings including:
 // * multiprocess
 // * user agent override
 class GeckoViewSettings extends GeckoViewModule {
   init() {
     this._isSafeBrowsingInit = false;
+    this._useDesktopMode = false;
 
     // We only allow to set this setting during initialization, further updates
     // will be ignored.
     this.useMultiprocess = !!this.settings.useMultiprocess;
     this._displayMode = Ci.nsIDocShell.DISPLAY_MODE_BROWSER;
 
     this.messageManager.loadFrameScript(
       "chrome://geckoview/content/GeckoViewContentSettings.js", true);
   }
 
   onSettingsUpdate() {
     debug("onSettingsUpdate: " + JSON.stringify(this.settings));
 
     this.displayMode = this.settings.displayMode;
     this.useTrackingProtection = !!this.settings.useTrackingProtection;
+    this.useDesktopMode = !!this.settings.useDesktopMode;
   }
 
   get useMultiprocess() {
     return this.browser.getAttribute("remote") == "true";
   }
 
   set useMultiprocess(aUse) {
     if (aUse == this.useMultiprocess) {
@@ -66,16 +77,48 @@ class GeckoViewSettings extends GeckoVie
 
   set useTrackingProtection(aUse) {
     if (aUse && !this._isSafeBrowsingInit) {
       SafeBrowsing.init();
       this._isSafeBrowsingInit = true;
     }
   }
 
+  onUserAgentRequest(aSubject, aTopic, aData) {
+    debug("onUserAgentRequest");
+
+    let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
+
+    if (this.browser.outerWindowID !== channel.topLevelOuterContentWindowId) {
+      return;
+    }
+
+    if (this.useDesktopMode) {
+      channel.setRequestHeader("User-Agent", DESKTOP_USER_AGENT, false);
+    }
+  }
+
+  get useDesktopMode() {
+    return this._useDesktopMode;
+  }
+
+  set useDesktopMode(aUse) {
+    if (this.useDesktopMode === aUse) {
+      return;
+    }
+    if (aUse) {
+      Services.obs.addObserver(this.onUserAgentRequest.bind(this),
+                               "http-on-useragent-request");
+    } else {
+      Services.obs.removeObserver(this.onUserAgentRequest.bind(this),
+                                  "http-on-useragent-request");
+    }
+    this._useDesktopMode = aUse;
+  }
+
   get displayMode() {
     return this._displayMode;
   }
 
   set displayMode(aMode) {
     if (!this.useMultiprocess) {
       this.window.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDocShell)