Bug 1628155 - Bring ext-chrome-settings-overrides.js up-to-date with its Firefox equivalent. rs=bustage-fix
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 08 Apr 2020 12:14:19 +1200
changeset 38744 a027f9a2f39d8a9dde446c0fe5f39dca85b12c74
parent 38743 3f497398788daab69b15f64d49ea1d18cdafbccc
child 38745 14603c9794a4ae41ffc70ebeb21546389452741f
push id401
push userclokep@gmail.com
push dateMon, 01 Jun 2020 20:41:59 +0000
reviewersbustage-fix
bugs1628155
Bug 1628155 - Bring ext-chrome-settings-overrides.js up-to-date with its Firefox equivalent. rs=bustage-fix
mail/components/extensions/parent/ext-chrome-settings-overrides.js
--- a/mail/components/extensions/parent/ext-chrome-settings-overrides.js
+++ b/mail/components/extensions/parent/ext-chrome-settings-overrides.js
@@ -105,45 +105,64 @@ this.chrome_settings_overrides = class e
 
   static removeSearchSettings(id) {
     return Promise.all([
       this.processDefaultSearchSetting("removeSetting", id),
       this.removeEngine(id),
     ]);
   }
 
+  static async onEnabling(id) {
+    chrome_settings_overrides.processDefaultSearchSetting("enable", id);
+  }
+
   static async onUninstall(id) {
     let searchStartupPromise = pendingSearchSetupTasks.get(id);
     if (searchStartupPromise) {
       await searchStartupPromise;
     }
-    return Promise.all([this.removeSearchSettings(id)]);
+    // Note: We do not have to deal with homepage here as it is managed by
+    // the ExtensionPreferencesManager.
+    return this.removeSearchSettings(id);
   }
 
-  static onUpdate(id, manifest) {
+  static async onUpdate(id, manifest) {
     let haveSearchProvider =
       manifest &&
       manifest.chrome_settings_overrides &&
       manifest.chrome_settings_overrides.search_provider;
+
     if (!haveSearchProvider) {
       this.removeSearchSettings(id);
+    } else if (
+      !!haveSearchProvider.is_default &&
+      (await ExtensionSettingsStore.initialize()) &&
+      ExtensionSettingsStore.hasSetting(
+        id,
+        DEFAULT_SEARCH_STORE_TYPE,
+        DEFAULT_SEARCH_SETTING_NAME
+      )
+    ) {
+      // is_default has been removed, but we still have a setting. Remove it.
+      chrome_settings_overrides.processDefaultSearchSetting(
+        "removeSetting",
+        id
+      );
     }
   }
 
   static onDisable(id) {
     chrome_settings_overrides.processDefaultSearchSetting("disable", id);
     chrome_settings_overrides.removeEngine(id);
   }
 
   async onManifestEntry(entryName) {
     let { extension } = this;
     let { manifest } = extension;
 
-    await ExtensionSettingsStore.initialize();
-
     if (manifest.chrome_settings_overrides.search_provider) {
       // Registering a search engine can potentially take a long while,
       // or not complete at all (when searchInitialized is never resolved),
       // so we are deliberately not awaiting the returned promise here.
       let searchStartupPromise = this.processSearchProviderManifestEntry().finally(
         () => {
           if (
             pendingSearchSetupTasks.get(extension.id) === searchStartupPromise
@@ -170,21 +189,17 @@ this.chrome_settings_overrides = class e
         );
         return;
       }
     }
 
     let engineName = searchProvider.name.trim();
     if (searchProvider.is_default) {
       let engine = Services.search.getEngineByName(engineName);
-      let defaultEngines = await Services.search.getDefaultEngines();
-      if (
-        engine &&
-        defaultEngines.some(defaultEngine => defaultEngine.name == engineName)
-      ) {
+      if (engine && engine.isAppProvided) {
         // Needs to be called every time to handle reenabling, but
         // only sets default for install or enable.
         await this.setDefault(engineName);
         // For built in search engines, we don't do anything further
         return;
       }
     }
     await this.addSearchEngine();
@@ -199,18 +214,19 @@ this.chrome_settings_overrides = class e
               // This is a hack because we don't have the browser of
               // the actual install. This means the popup might show
               // in a different window. Will be addressed in a followup bug.
               browser: windowTracker.topWindow.gBrowser.selectedBrowser,
               name: this.extension.name,
               icon: this.extension.iconURL,
               currentEngine: defaultEngine.name,
               newEngine: engineName,
-              respond(allow) {
+              async respond(allow) {
                 if (allow) {
+                  await ExtensionSettingsStore.initialize();
                   ExtensionSettingsStore.addSetting(
                     extension.id,
                     DEFAULT_SEARCH_STORE_TYPE,
                     DEFAULT_SEARCH_SETTING_NAME,
                     engineName,
                     () => defaultEngine.name
                   );
                   Services.search.defaultEngine = Services.search.getEngineByName(
@@ -225,35 +241,24 @@ this.chrome_settings_overrides = class e
             "webextension-defaultsearch-prompt"
           );
         }
       } else {
         // Needs to be called every time to handle reenabling, but
         // only sets default for install or enable.
         this.setDefault(engineName);
       }
-    } else if (
-      ExtensionSettingsStore.hasSetting(
-        extension.id,
-        DEFAULT_SEARCH_STORE_TYPE,
-        DEFAULT_SEARCH_SETTING_NAME
-      )
-    ) {
-      // is_default has been removed, but we still have a setting. Remove it.
-      chrome_settings_overrides.processDefaultSearchSetting(
-        "removeSetting",
-        extension.id
-      );
     }
   }
 
   async setDefault(engineName) {
     let { extension } = this;
     if (extension.startupReason === "ADDON_INSTALL") {
       let defaultEngine = await Services.search.getDefault();
+      await ExtensionSettingsStore.initialize();
       let item = await ExtensionSettingsStore.addSetting(
         extension.id,
         DEFAULT_SEARCH_STORE_TYPE,
         DEFAULT_SEARCH_SETTING_NAME,
         engineName,
         () => defaultEngine.name
       );
       await Services.search.setDefault(
@@ -264,60 +269,26 @@ this.chrome_settings_overrides = class e
         "enable",
         extension.id
       );
     }
   }
 
   async addSearchEngine() {
     let { extension } = this;
-    let isCurrent = false;
-    let index = -1;
-    if (
-      extension.startupReason === "ADDON_UPGRADE" &&
-      !extension.addonData.builtIn
-    ) {
-      let engines = await Services.search.getEnginesByExtensionID(extension.id);
-      if (engines.length > 0) {
-        let firstEngine = engines[0];
-        let firstEngineName = firstEngine.name;
-        // There can be only one engine right now
-        isCurrent =
-          (await Services.search.getDefault()).name == firstEngineName;
-        // Get position of engine and store it
-        index = (await Services.search.getEngines())
-          .map(engine => engine.name)
-          .indexOf(firstEngineName);
-        await Services.search.removeEngine(firstEngine);
-      }
-    }
     try {
       let engines = await Services.search.addEnginesFromExtension(extension);
       if (engines.length > 0) {
+        await ExtensionSettingsStore.initialize();
         await ExtensionSettingsStore.addSetting(
           extension.id,
           DEFAULT_SEARCH_STORE_TYPE,
           ENGINE_ADDED_SETTING_NAME,
           engines[0].name
         );
       }
-      if (
-        extension.startupReason === "ADDON_UPGRADE" &&
-        !extension.addonData.builtIn
-      ) {
-        let engines = await Services.search.getEnginesByExtensionID(
-          extension.id
-        );
-        let engine = Services.search.getEngineByName(engines[0].name);
-        if (isCurrent) {
-          await Services.search.setDefault(engine);
-        }
-        if (index != -1) {
-          await Services.search.moveEngine(engine, index);
-        }
-      }
     } catch (e) {
       Cu.reportError(e);
       return false;
     }
     return true;
   }
 };