Bug 1449548. r=mconley, r=aswan, a=RyanVM
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 28 Mar 2018 13:48:09 +0100
changeset 463057 ba71ad255604c1d2530e557c0d15dfd37ebb8f29
parent 463056 ac2e6a9da92c0afcda4e30a87ca2ad138a876b2f
child 463058 a0ba4a0eaba37467acde23fd1984b41e8f5e4cac
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)
reviewersmconley, aswan, RyanVM
bugs1449548
milestone60.0
Bug 1449548. r=mconley, r=aswan, a=RyanVM
browser/base/content/browser-addons.js
browser/base/content/content.js
--- a/browser/base/content/browser-addons.js
+++ b/browser/base/content/browser-addons.js
@@ -570,37 +570,39 @@ var gExtensionsNotifications = {
 };
 
 var LightWeightThemeWebInstaller = {
   init() {
     let mm = window.messageManager;
     mm.addMessageListener("LightWeightThemeWebInstaller:Install", this);
     mm.addMessageListener("LightWeightThemeWebInstaller:Preview", this);
     mm.addMessageListener("LightWeightThemeWebInstaller:ResetPreview", this);
+
+    XPCOMUtils.defineLazyPreferenceGetter(this, "_apiTesting", "extensions.webapi.testing", false);
   },
 
   receiveMessage(message) {
     // ignore requests from background tabs
     if (message.target != gBrowser.selectedBrowser) {
       return;
     }
 
     let data = message.data;
 
     switch (message.name) {
       case "LightWeightThemeWebInstaller:Install": {
-        this._installRequest(data.themeData, data.baseURI);
+        this._installRequest(data.themeData, data.principal, data.baseURI);
         break;
       }
       case "LightWeightThemeWebInstaller:Preview": {
-        this._preview(data.themeData, data.baseURI);
+        this._preview(data.themeData, data.principal, data.baseURI);
         break;
       }
       case "LightWeightThemeWebInstaller:ResetPreview": {
-        this._resetPreview(data && data.baseURI);
+        this._resetPreview(data && data.principal);
         break;
       }
     }
   },
 
   handleEvent(event) {
     switch (event.type) {
       case "TabSelect": {
@@ -612,52 +614,43 @@ var LightWeightThemeWebInstaller = {
 
   get _manager() {
     let temp = {};
     ChromeUtils.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
     delete this._manager;
     return this._manager = temp.LightweightThemeManager;
   },
 
-  _installRequest(dataString, baseURI) {
+  _installRequest(dataString, principal, baseURI) {
+    // Don't allow installing off null principals.
+    if (!principal.URI) {
+      return;
+    }
+
     let data = this._manager.parseTheme(dataString, baseURI);
 
     if (!data) {
       return;
     }
 
-    let uri = makeURI(baseURI);
-
     // A notification bar with the option to undo is normally shown after a
     // theme is installed.  But the discovery pane served from the url(s)
     // below has its own toggle switch for quick undos, so don't show the
     // notification in that case.
-    let notify = uri.prePath != "https://discovery.addons.mozilla.org";
-    if (notify) {
-      try {
-        if (Services.prefs.getBoolPref("extensions.webapi.testing")
-            && (uri.prePath == "https://discovery.addons.allizom.org"
-                || uri.prePath == "https://discovery.addons-dev.allizom.org")) {
-          notify = false;
-        }
-      } catch (e) {
-        // getBoolPref() throws if the testing pref isn't set.  ignore it.
-      }
-    }
-
-    if (this._isAllowed(baseURI)) {
+    let notify = this._shouldShowUndoPrompt(principal);
+    if (this._isAllowed(principal)) {
       this._install(data, notify);
       return;
     }
 
     let strings = {
       header: gNavigatorBundle.getFormattedString("webextPerms.header", ["<>"]),
       addonName: data.name,
       text: gNavigatorBundle.getFormattedString("lwthemeInstallRequest.message2",
-                                                [uri.host]),
+                                                [principal.URI.host]),
       acceptText: gNavigatorBundle.getString("lwthemeInstallRequest.allowButton2"),
       acceptKey: gNavigatorBundle.getString("lwthemeInstallRequest.allowButton.accesskey2"),
       cancelText: gNavigatorBundle.getString("webextPerms.cancel.label"),
       cancelKey: gNavigatorBundle.getString("webextPerms.cancel.accessKey"),
       msgs: []
     };
     ExtensionsUI.showPermissionsPrompt(gBrowser.selectedBrowser, strings, null,
       "installWeb").then(answer => {
@@ -701,45 +694,55 @@ var LightWeightThemeWebInstaller = {
       }
     };
 
     AddonManager.addAddonListener(listener);
     this._manager.currentTheme = newLWTheme;
     AddonManager.removeAddonListener(listener);
   },
 
-  _preview(dataString, baseURI) {
-    if (!this._isAllowed(baseURI))
+  _preview(dataString, principal, baseURI) {
+    if (!this._isAllowed(principal))
       return;
 
     let data = this._manager.parseTheme(dataString, baseURI);
     if (!data)
       return;
 
     this._resetPreview();
     gBrowser.tabContainer.addEventListener("TabSelect", this);
     this._manager.previewTheme(data);
   },
 
-  _resetPreview(baseURI) {
-    if (baseURI && !this._isAllowed(baseURI))
+  _resetPreview(principal) {
+    if (!this._isAllowed(principal))
       return;
     gBrowser.tabContainer.removeEventListener("TabSelect", this);
     this._manager.resetPreview();
   },
 
-  _isAllowed(srcURIString) {
-    let uri;
-    try {
-      uri = makeURI(srcURIString);
-    } catch (e) {
-      // makeURI fails if srcURIString is a nonsense URI
-      return false;
-    }
-
-    if (!uri.schemeIs("https")) {
+  _isAllowed(principal) {
+    if (!principal || !principal.URI || !principal.URI.schemeIs("https")) {
       return false;
     }
 
     let pm = Services.perms;
-    return pm.testPermission(uri, "install") == pm.ALLOW_ACTION;
-  }
+    return pm.testPermission(principal.URI, "install") == pm.ALLOW_ACTION;
+  },
+
+  _shouldShowUndoPrompt(principal) {
+    if (!principal || !principal.URI) {
+      return true;
+    }
+
+    let prePath = principal.URI.prePath;
+    if (prePath == "https://discovery.addons.mozilla.org") {
+      return false;
+    }
+
+    if (this._apiTesting && (prePath == "https://discovery.addons.allizom.org" ||
+                             prePath == "https://discovery.addons-dev.allizom.org")) {
+      return false;
+    }
+    return true;
+  },
+
 };
--- a/browser/base/content/content.js
+++ b/browser/base/content/content.js
@@ -744,38 +744,40 @@ var LightWeightThemeWebInstallListener =
     addEventListener("ResetBrowserThemePreview", this, false, true);
   },
 
   handleEvent(event) {
     switch (event.type) {
       case "InstallBrowserTheme": {
         sendAsyncMessage("LightWeightThemeWebInstaller:Install", {
           baseURI: event.target.baseURI,
+          principal: event.target.nodePrincipal,
           themeData: event.target.getAttribute("data-browsertheme"),
         });
         break;
       }
       case "PreviewBrowserTheme": {
         sendAsyncMessage("LightWeightThemeWebInstaller:Preview", {
           baseURI: event.target.baseURI,
+          principal: event.target.nodePrincipal,
           themeData: event.target.getAttribute("data-browsertheme"),
         });
         this._previewWindow = event.target.ownerGlobal;
         this._previewWindow.addEventListener("pagehide", this, true);
         break;
       }
       case "pagehide": {
         sendAsyncMessage("LightWeightThemeWebInstaller:ResetPreview");
         this._resetPreviewWindow();
         break;
       }
       case "ResetBrowserThemePreview": {
         if (this._previewWindow) {
           sendAsyncMessage("LightWeightThemeWebInstaller:ResetPreview",
-                           {baseURI: event.target.baseURI});
+                           {principal: event.target.nodePrincipal});
           this._resetPreviewWindow();
         }
         break;
       }
     }
   },
 
   _resetPreviewWindow() {