Bug 1386807 - Part 3: Make the UA pref actually disable UA overrides r=Felipe
☠☠ backed out by 411dfa0e714a ☠ ☠
authorDennis Schubert <dschubert@mozilla.com>
Fri, 09 Feb 2018 18:53:33 +0100
changeset 471799 8da96d4d931a55739e0514a3276ee854cf361ea7
parent 471798 6cd45f41ee018ce340618fdd337460dc72db2a2d
child 471800 669b85a6cab8c99a0bd0afe53a02149a2da77577
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersFelipe
bugs1386807
milestone61.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 1386807 - Part 3: Make the UA pref actually disable UA overrides r=Felipe MozReview-Commit-ID: 25YsSg7P8DP
browser/extensions/webcompat/bootstrap.js
browser/extensions/webcompat/content/lib/ua_overrider.jsm
--- a/browser/extensions/webcompat/bootstrap.js
+++ b/browser/extensions/webcompat/bootstrap.js
@@ -29,22 +29,17 @@ function InjectionsEnablePrefObserver() 
   webextensionPort.postMessage({
     type: "injection-pref-changed",
     prefState: isEnabled
   });
 }
 
 function UAEnablePrefObserver() {
   let isEnabled = Services.prefs.getBoolPref(UA_ENABLE_PREF_NAME);
-  if (isEnabled && !overrider) {
-    overrider = new UAOverrider(UAOverrides);
-    overrider.init();
-  } else if (!isEnabled && overrider) {
-    overrider = null;
-  }
+  overrider.setShouldOverride(isEnabled);
 }
 
 function setDefaultPrefs() {
   const branch = Services.prefs.getDefaultBranch(PREF_BRANCH);
   for (const [key, val] of Object.entries(PREF_DEFAULTS)) {
     // If someone beat us to setting a default, don't overwrite it.
     if (branch.getPrefType(key) !== branch.PREF_INVALID) {
       continue;
--- a/browser/extensions/webcompat/content/lib/ua_overrider.jsm
+++ b/browser/extensions/webcompat/content/lib/ua_overrider.jsm
@@ -5,16 +5,17 @@
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "UserAgentOverrides", "resource://gre/modules/UserAgentOverrides.jsm");
 XPCOMUtils.defineLazyServiceGetter(this, "eTLDService", "@mozilla.org/network/effective-tld-service;1", "nsIEffectiveTLDService");
 
 class UAOverrider {
   constructor(overrides) {
     this._overrides = {};
+    this._shouldOverride = true;
 
     this.initOverrides(overrides);
   }
 
   initOverrides(overrides) {
     // on xpcshell tests, there is no impleentation for nsIXULAppInfo, so this
     // might fail there. To have all of our test cases running at all times,
     // assume they are on Desktop for now.
@@ -42,21 +43,36 @@ class UAOverrider {
       if (!override.uriMatcher) {
         override.uriMatcher = () => true;
       }
 
       this._overrides[override.baseDomain].push(override);
     }
   }
 
+  /**
+   * Used for disabling overrides when the pref has been flipped to false.
+   *
+   * Since we no longer use our own event handlers, we check this bool in our
+   * override callback and simply return early if we are not supposed to do
+   * anything.
+   */
+  setShouldOverride(newState) {
+    this._shouldOverride = newState;
+  }
+
   init() {
     UserAgentOverrides.addComplexOverride(this.overrideCallback.bind(this));
   }
 
   overrideCallback(channel, defaultUA) {
+    if (!this._shouldOverride) {
+      return false;
+    }
+
     let uaOverride = this.lookupUAOverride(channel.URI, defaultUA);
     if (uaOverride) {
       console.log("The user agent has been overridden for compatibility reasons.");
       return uaOverride;
     }
 
     return false;
   }