Bug 1552920 - Port bug 1549122 and bug 1549192: Copy changes to ext-chrome-settings-overrides.js from mozilla-central. r=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Fri, 31 May 2019 11:45:08 +1200
changeset 35728 9c160ed8bd8db02495a86609d6cd4b5f75310123
parent 35727 60cf979fc84d4c0745a5de2c6ed4036386cffb57
child 35729 0cc63242c9a4f3441112010907fb65c4a49d4f24
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewersjorgk
bugs1552920, 1549122, 1549192
Bug 1552920 - Port bug 1549122 and bug 1549192: Copy changes to ext-chrome-settings-overrides.js from mozilla-central. r=jorgk
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
@@ -61,17 +61,20 @@ this.chrome_settings_overrides = class e
     }
     // We can call removeEngine in nsSearchService startup, if so we dont
     // need to reforward the call, just disable the web extension.
     if (!Services.search.isInitialized) {
       return;
     }
 
     try {
-      await Services.search.removeWebExtensionEngine(id);
+      let engines = await Services.search.getEnginesByExtensionID(id);
+      if (engines.length > 0) {
+        await Services.search.removeWebExtensionEngine(id);
+      }
     } catch (e) {
       Cu.reportError(e);
     }
   }
 
   static removeSearchSettings(id) {
     return Promise.all([
       this.processDefaultSearchSetting("removeSetting", id),
@@ -92,16 +95,21 @@ this.chrome_settings_overrides = class e
   static onUpdate(id, manifest) {
     let haveSearchProvider = manifest && manifest.chrome_settings_overrides &&
                              manifest.chrome_settings_overrides.search_provider;
     if (!haveSearchProvider) {
       this.removeSearchSettings(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,
@@ -125,24 +133,16 @@ this.chrome_settings_overrides = class e
     let searchProvider = manifest.chrome_settings_overrides.search_provider;
     if (searchProvider.is_default) {
       await searchInitialized;
       if (!this.extension) {
         Cu.reportError(`Extension shut down before search provider was registered`);
         return;
       }
     }
-    extension.callOnClose({
-      close: () => {
-        if (extension.shutdownReason == "ADDON_DISABLE") {
-          chrome_settings_overrides.processDefaultSearchSetting("disable", extension.id);
-          chrome_settings_overrides.removeEngine(extension.id);
-        }
-      },
-    });
 
     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)) {
         // Needs to be called every time to handle reenabling, but
         // only sets default for install or enable.
@@ -203,17 +203,18 @@ this.chrome_settings_overrides = class e
       chrome_settings_overrides.processDefaultSearchSetting("enable", extension.id);
     }
   }
 
   async addSearchEngine() {
     let {extension} = this;
     let isCurrent = false;
     let index = -1;
-    if (extension.startupReason === "ADDON_UPGRADE") {
+    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);
@@ -222,17 +223,18 @@ this.chrome_settings_overrides = class e
     }
     try {
       let engines = await Services.search.addEnginesFromExtension(extension);
       if (engines.length > 0) {
         await ExtensionSettingsStore.addSetting(
           extension.id, DEFAULT_SEARCH_STORE_TYPE, ENGINE_ADDED_SETTING_NAME,
           engines[0].name);
       }
-      if (extension.startupReason === "ADDON_UPGRADE") {
+      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);
         }