Bug 1541317 fix extension failures when setting homepage on upgrade r=aswan a=pascalc
authorShane Caraveo <scaraveo@mozilla.com>
Wed, 08 May 2019 01:30:25 +0000
changeset 526521 6124d4a75c1fc532f9240cfc1c70f83d293ce9e1
parent 526520 b620b8ee82853fa4ec7d25d27d7f347f42e87021
child 526522 432e429f9663a23a3be48459cc0f0fe8324d4722
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan, pascalc
bugs1541317
milestone67.0
Bug 1541317 fix extension failures when setting homepage on upgrade r=aswan a=pascalc Differential Revision: https://phabricator.services.mozilla.com/D29912
browser/components/extensions/parent/ext-chrome-settings-overrides.js
browser/components/extensions/test/xpcshell/test_ext_chrome_settings_overrides_update.js
--- a/browser/components/extensions/parent/ext-chrome-settings-overrides.js
+++ b/browser/components/extensions/parent/ext-chrome-settings-overrides.js
@@ -179,17 +179,17 @@ this.chrome_settings_overrides = class e
     if (homepageUrl) {
       let inControl;
       if (extension.startupReason == "ADDON_INSTALL" ||
           extension.startupReason == "ADDON_ENABLE") {
         inControl = await ExtensionPreferencesManager.setSetting(
           extension.id, "homepage_override", homepageUrl);
       } else {
         let item = await ExtensionPreferencesManager.getSetting("homepage_override");
-        inControl = item.id == extension.id;
+        inControl = item && item.id == extension.id;
       }
       // We need to add the listener here too since onPrefsChanged won't trigger on a
       // restart (the prefs are already set).
       if (inControl) {
         Services.prefs.setBoolPref(HOMEPAGE_PRIVATE_ALLOWED, extension.privateBrowsingAllowed);
         // Also set this now as an upgraded browser will need this.
         Services.prefs.setBoolPref(HOMEPAGE_EXTENSION_CONTROLLED, true);
         if (extension.startupReason == "APP_STARTUP") {
@@ -199,26 +199,26 @@ this.chrome_settings_overrides = class e
         }
       }
 
       // We need to monitor permission change and update the preferences.
       // eslint-disable-next-line mozilla/balanced-listeners
       extension.on("add-permissions", async (ignoreEvent, permissions) => {
         if (permissions.permissions.includes("internal:privateBrowsingAllowed")) {
           let item = await ExtensionPreferencesManager.getSetting("homepage_override");
-          if (item.id == extension.id) {
+          if (item && item.id == extension.id) {
             Services.prefs.setBoolPref(HOMEPAGE_PRIVATE_ALLOWED, true);
           }
         }
       });
       // eslint-disable-next-line mozilla/balanced-listeners
       extension.on("remove-permissions", async (ignoreEvent, permissions) => {
         if (permissions.permissions.includes("internal:privateBrowsingAllowed")) {
           let item = await ExtensionPreferencesManager.getSetting("homepage_override");
-          if (item.id == extension.id) {
+          if (item && item.id == extension.id) {
             Services.prefs.setBoolPref(HOMEPAGE_PRIVATE_ALLOWED, false);
           }
         }
       });
 
       extension.callOnClose({
         close: () => {
           if (extension.shutdownReason == "ADDON_DISABLE") {
--- a/browser/components/extensions/test/xpcshell/test_ext_chrome_settings_overrides_update.js
+++ b/browser/components/extensions/test/xpcshell/test_ext_chrome_settings_overrides_update.js
@@ -1,30 +1,30 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
 const {AddonTestUtils} = ChromeUtils.import("resource://testing-common/AddonTestUtils.jsm");
 const {HomePage} = ChromeUtils.import("resource:///modules/HomePage.jsm");
 
-const {
-  createAppInfo,
-  promiseShutdownManager,
-  promiseStartupManager,
-} = AddonTestUtils;
-
 AddonTestUtils.init(this);
 AddonTestUtils.overrideCertDB();
 
-createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "42");
+AddonTestUtils.createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "42");
+
+add_task(async function setup() {
+  await AddonTestUtils.promiseStartupManager();
+});
 
 add_task(async function test_overrides_update_removal() {
   /* This tests the scenario where the manifest key for homepage and/or
    * search_provider are removed between updates and therefore the
-   * settings are expected to revert. */
+   * settings are expected to revert.  It also tests that an extension
+   * can make a builtin extension the default extension without user
+   * interaction.  */
 
   const EXTENSION_ID = "test_overrides_update@tests.mozilla.org";
   const HOMEPAGE_URI = "webext-homepage-1.html";
 
   const HOMEPAGE_URL_PREF = "browser.startup.homepage";
 
   function promisePrefChanged(value) {
     return new Promise((resolve, reject) => {
@@ -32,17 +32,16 @@ add_task(async function test_overrides_u
         if (HomePage.get().endsWith(value)) {
           Services.prefs.removeObserver(HOMEPAGE_URL_PREF, observer);
           resolve();
         }
       });
     });
   }
 
-  await promiseStartupManager();
 
   let extensionInfo = {
     useAddonManager: "permanent",
     manifest: {
       "version": "1.0",
       "applications": {
         "gecko": {
           "id": EXTENSION_ID,
@@ -57,28 +56,30 @@ add_task(async function test_overrides_u
         },
       },
     },
   };
   let extension = ExtensionTestUtils.loadExtension(extensionInfo);
 
   let defaultHomepageURL = HomePage.get();
   let defaultEngineName = (await Services.search.getDefault()).name;
+  ok(defaultEngineName !== "DuckDuckGo",
+     "Default engine is not DuckDuckGo.");
 
   let prefPromise = promisePrefChanged(HOMEPAGE_URI);
   await extension.startup();
   await AddonTestUtils.waitForSearchProviderStartup(extension);
   await prefPromise;
 
   equal(extension.version, "1.0", "The installed addon has the expected version.");
   ok(HomePage.get().endsWith(HOMEPAGE_URI),
      "Home page url is overridden by the extension.");
   equal((await Services.search.getDefault()).name,
         "DuckDuckGo",
-        "Default engine is overridden by the extension");
+        "Builtin default engine was set default by extension");
 
   extensionInfo.manifest = {
     "version": "2.0",
     "applications": {
       "gecko": {
         "id": EXTENSION_ID,
       },
     },
@@ -92,11 +93,80 @@ add_task(async function test_overrides_u
   equal(HomePage.get(),
         defaultHomepageURL,
         "Home page url reverted to the default after update.");
   equal((await Services.search.getDefault()).name,
         defaultEngineName,
         "Default engine reverted to the default after update.");
 
   await extension.unload();
+});
 
-  await promiseShutdownManager();
+add_task(async function test_overrides_update_adding() {
+  /* This tests the scenario where an addon adds support for
+   * a homepage or search service when upgrading. Neither
+   * should override existing entries for those when added
+   * in an upgrade. Also, a search_provider being added
+   * with is_default should not prompt the user or override
+   * the current default engine. */
+
+  const EXTENSION_ID = "test_overrides_update@tests.mozilla.org";
+  const HOMEPAGE_URI = "webext-homepage-1.html";
+
+  let extensionInfo = {
+    useAddonManager: "permanent",
+    manifest: {
+      "version": "1.0",
+      "applications": {
+        "gecko": {
+          "id": EXTENSION_ID,
+        },
+      },
+    },
+  };
+  let extension = ExtensionTestUtils.loadExtension(extensionInfo);
+
+  let defaultHomepageURL = HomePage.get();
+  let defaultEngineName = (await Services.search.getDefault()).name;
+  ok(defaultEngineName !== "DuckDuckGo",
+     "Home page url is not DuckDuckGo.");
+
+  await extension.startup();
+
+  equal(extension.version, "1.0", "The installed addon has the expected version.");
+  equal(HomePage.get(),
+        defaultHomepageURL,
+        "Home page url is the default after startup.");
+  equal((await Services.search.getDefault()).name,
+        defaultEngineName,
+        "Default engine is the default after startup.");
+
+  extensionInfo.manifest = {
+    "version": "2.0",
+    "applications": {
+      "gecko": {
+        "id": EXTENSION_ID,
+      },
+    },
+    "chrome_settings_overrides": {
+      "homepage": HOMEPAGE_URI,
+      "search_provider": {
+        "name": "DuckDuckGo",
+        "search_url": "https://example.com/?q={searchTerms}",
+        "is_default": true,
+      },
+    },
+  };
+
+  await extension.upgrade(extensionInfo);
+  await AddonTestUtils.waitForSearchProviderStartup(extension);
+
+  equal(extension.version, "2.0", "The updated addon has the expected version.");
+  equal(HomePage.get(), defaultHomepageURL,
+        "Home page url is not overridden by the extension during upgrade.");
+  // An upgraded extension adding a search engine cannot override
+  // the default engine.
+  equal((await Services.search.getDefault()).name,
+        defaultEngineName,
+        "Default engine is still the default after startup.");
+
+  await extension.unload();
 });