Bug 1254386 - Move custom UA emulation to emulation actor. r=jryans
authorTim Nguyen <ntim.bugs@gmail.com>
Tue, 23 Aug 2016 01:51:16 -0700
changeset 336336 fec37519e65fe5d4b2e68a7f0c76bca60253a0ed
parent 336335 ed793a550dd602631e25febe9fc39f3bab41e80a
child 336337 6f5b69e1d1a9c8acafd059e8d221d667e86040d3
push id10033
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:50:26 +0000
treeherdermozilla-aurora@5dddbefdf759 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1254386
milestone51.0a1
Bug 1254386 - Move custom UA emulation to emulation actor. r=jryans MozReview-Commit-ID: 9005ktskEVk
devtools/server/actors/emulation.js
devtools/server/actors/webbrowser.js
devtools/shared/specs/emulation.js
--- a/devtools/server/actors/emulation.js
+++ b/devtools/server/actors/emulation.js
@@ -11,16 +11,18 @@ const { SimulatorCore } = require("devto
 
 let EmulationActor = protocol.ActorClassWithSpec(emulationSpec, {
   initialize(conn, tabActor) {
     protocol.Actor.prototype.initialize.call(this, conn);
     this.docShell = tabActor.docShell;
     this.simulatorCore = new SimulatorCore(tabActor.chromeEventHandler);
   },
 
+  /* Touch events override */
+
   _previousTouchEventsOverride: null,
 
   setTouchEventsOverride(flag) {
     if (this.docShell.touchEventsOverride == flag) {
       return false;
     }
     if (this._previousTouchEventsOverride === null) {
       this._previousTouchEventsOverride = this.docShell.touchEventsOverride;
@@ -38,25 +40,53 @@ let EmulationActor = protocol.ActorClass
   },
 
   getTouchEventsOverride() {
     return this.docShell.touchEventsOverride;
   },
 
   clearTouchEventsOverride() {
     if (this._previousTouchEventsOverride !== null) {
-      this.setTouchEventsOverride(this._previousTouchEventsOverride);
+      return this.setTouchEventsOverride(this._previousTouchEventsOverride);
+    }
+    return false;
+  },
+
+  /* User agent override */
+
+  _previousUserAgentOverride: null,
+
+  setUserAgentOverride(userAgent) {
+    if (this.docShell.customUserAgent == userAgent) {
+      return false;
     }
+    if (this._previousUserAgentOverride === null) {
+      this._previousUserAgentOverride = this.docShell.customUserAgent;
+    }
+    this.docShell.customUserAgent = userAgent;
+    return true;
+  },
+
+  getUserAgentOverride() {
+    return this.docShell.customUserAgent;
+  },
+
+  clearUserAgentOverride() {
+    if (this._previousUserAgentOverride !== null) {
+      return this.setUserAgentOverride(this._previousUserAgentOverride);
+    }
+    return false;
   },
 
   disconnect() {
     this.destroy();
   },
 
   destroy() {
     this.clearTouchEventsOverride();
+    this.clearUserAgentOverride();
     this.docShell = null;
     this.simulatorCore = null;
     protocol.Actor.prototype.destroy.call(this);
   },
 });
 
 exports.EmulationActor = EmulationActor;
--- a/devtools/server/actors/webbrowser.js
+++ b/devtools/server/actors/webbrowser.js
@@ -1672,22 +1672,16 @@ TabActor.prototype = {
     if ((typeof options.serviceWorkersTestingEnabled !== "undefined") &&
         (options.serviceWorkersTestingEnabled !==
          this._getServiceWorkersTestingEnabled())) {
       this._setServiceWorkersTestingEnabled(
         options.serviceWorkersTestingEnabled
       );
     }
 
-    if ((typeof options.customUserAgent !== "undefined") &&
-         options.customUserAgent !== this._getCustomUserAgent()) {
-      this._setCustomUserAgent(options.customUserAgent);
-      reload = true;
-    }
-
     // Reload if:
     //  - there's an explicit `performReload` flag and it's true
     //  - there's no `performReload` flag, but it makes sense to do so
     let hasExplicitReloadFlag = "performReload" in options;
     if ((hasExplicitReloadFlag && options.performReload) ||
        (!hasExplicitReloadFlag && reload)) {
       this.onReload();
     }
@@ -1696,17 +1690,16 @@ TabActor.prototype = {
   /**
    * Opposite of the _toggleDevToolsSettings method, that reset document state
    * when closing the toolbox.
    */
   _restoreDocumentSettings() {
     this._restoreJavascript();
     this._setCacheDisabled(false);
     this._setServiceWorkersTestingEnabled(false);
-    this._restoreUserAgent();
   },
 
   /**
    * Disable or enable the cache via docShell.
    */
   _setCacheDisabled(disabled) {
     let enable = Ci.nsIRequest.LOAD_NORMAL;
     let disable = Ci.nsIRequest.LOAD_BYPASS_CACHE |
@@ -1780,48 +1773,16 @@ TabActor.prototype = {
       return null;
     }
 
     let windowUtils = this.window.QueryInterface(Ci.nsIInterfaceRequestor)
                                  .getInterface(Ci.nsIDOMWindowUtils);
     return windowUtils.serviceWorkersTestingEnabled;
   },
 
-  _previousCustomUserAgent: null,
-
-  /**
-   * Return custom user agent.
-   */
-  _getCustomUserAgent() {
-    if (!this.docShell) {
-      // The tab is already closed.
-      return null;
-    }
-    return this.docShell.customUserAgent;
-  },
-
-  /**
-   * Sets custom user agent for the current tab
-   */
-  _setCustomUserAgent(userAgent) {
-    if (this._previousCustomUserAgent === null) {
-      this._previousCustomUserAgent = this.docShell.customUserAgent;
-    }
-    this.docShell.customUserAgent = userAgent;
-  },
-
-  /**
-   * Restore the user agent, before the actor modified it
-   */
-  _restoreUserAgent() {
-    if (this._previousCustomUserAgent !== null) {
-      this.docShell.customUserAgent = this._previousCustomUserAgent;
-    }
-  },
-
   /**
    * Prepare to enter a nested event loop by disabling debuggee events.
    */
   preNest() {
     if (!this.window) {
       // The tab is already closed.
       return;
     }
--- a/devtools/shared/specs/emulation.js
+++ b/devtools/shared/specs/emulation.js
@@ -9,27 +9,52 @@ const emulationSpec = generateActorSpec(
   typeName: "emulation",
 
   methods: {
     setTouchEventsOverride: {
       request: {
         flag: Arg(0, "number")
       },
       response: {
-        reload: RetVal("boolean")
+        valueChanged: RetVal("boolean")
       }
     },
 
     getTouchEventsOverride: {
       request: {},
       response: {
         flag: RetVal("number")
       }
     },
 
     clearTouchEventsOverride: {
       request: {},
-      response: {}
+      response: {
+        valueChanged: RetVal("boolean")
+      }
+    },
+
+    setUserAgentOverride: {
+      request: {
+        flag: Arg(0, "string")
+      },
+      response: {
+        valueChanged: RetVal("boolean")
+      }
+    },
+
+    getUserAgentOverride: {
+      request: {},
+      response: {
+        userAgent: RetVal("string")
+      }
+    },
+
+    clearUserAgentOverride: {
+      request: {},
+      response: {
+        valueChanged: RetVal("boolean")
+      }
     },
   }
 });
 
 exports.emulationSpec = emulationSpec;