Bug 1486819 move search engine specific logic into search service, r=mkaply
authorShane Caraveo <scaraveo@mozilla.com>
Wed, 03 Oct 2018 21:35:22 +0000
changeset 495245 47f1126313c624f27bf6b6622162727fa596f9d9
parent 495244 1f268b2058c9e498547c2a902127fa45a93673f8
child 495246 e682824ac09e11bc6e3778f2e1aeb89dea4043bd
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkaply
bugs1486819
milestone64.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 1486819 move search engine specific logic into search service, r=mkaply refactor some code into the search service. This is necessary to allow the searchservice to pull multiple locales or regions from a single extension, based on data the searchservice maintains. Differential Revision: https://phabricator.services.mozilla.com/D7632
browser/components/extensions/parent/ext-chrome-settings-overrides.js
netwerk/base/nsIBrowserSearchService.idl
toolkit/components/search/nsSearchService.js
--- a/browser/components/extensions/parent/ext-chrome-settings-overrides.js
+++ b/browser/components/extensions/parent/ext-chrome-settings-overrides.js
@@ -1,26 +1,21 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
-ChromeUtils.import("resource://gre/modules/ExtensionParent.jsm");
 ChromeUtils.defineModuleGetter(this, "ExtensionPreferencesManager",
                                "resource://gre/modules/ExtensionPreferencesManager.jsm");
 ChromeUtils.defineModuleGetter(this, "ExtensionSettingsStore",
                                "resource://gre/modules/ExtensionSettingsStore.jsm");
 ChromeUtils.defineModuleGetter(this, "ExtensionControlledPopup",
                                "resource:///modules/ExtensionControlledPopup.jsm");
 
-var {
-  IconDetails,
-} = ExtensionParent;
-
 const DEFAULT_SEARCH_STORE_TYPE = "default_search";
 const DEFAULT_SEARCH_SETTING_NAME = "defaultSearch";
 const ENGINE_ADDED_SETTING_NAME = "engineAdded";
 
 const HOMEPAGE_PREF = "browser.startup.homepage";
 const HOMEPAGE_CONFIRMED_TYPE = "homepageNotification";
 const HOMEPAGE_SETTING_TYPE = "prefs";
 const HOMEPAGE_SETTING_NAME = "homepage_override";
@@ -212,17 +207,17 @@ this.chrome_settings_overrides = class e
         if (engine && Services.search.getDefaultEngines().includes(engine)) {
           // 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(searchProvider);
+      await this.addSearchEngine();
       if (searchProvider.is_default) {
         if (extension.startupReason === "ADDON_INSTALL") {
           // Don't ask if it already the current engine
           let engine = Services.search.getEngineByName(engineName);
           if (Services.search.currentEngine != engine) {
             let allow = await new Promise(resolve => {
               let subject = {
                 wrappedJSObject: {
@@ -264,61 +259,40 @@ this.chrome_settings_overrides = class e
           return Services.search.currentEngine.name;
         });
       Services.search.currentEngine = Services.search.getEngineByName(item.value);
     } else if (extension.startupReason === "ADDON_ENABLE") {
       chrome_settings_overrides.processDefaultSearchSetting("enable", extension.id);
     }
   }
 
-  async addSearchEngine(searchProvider) {
+  async addSearchEngine() {
     let {extension} = this;
     let isCurrent = false;
     let index = -1;
     if (extension.startupReason === "ADDON_UPGRADE") {
       let engines = Services.search.getEnginesByExtensionID(extension.id);
       if (engines.length > 0) {
         // There can be only one engine right now
         isCurrent = Services.search.currentEngine == engines[0];
         // Get position of engine and store it
         index = Services.search.getEngines().indexOf(engines[0]);
         Services.search.removeEngine(engines[0]);
       }
     }
-
-    let icons = extension.manifest.icons;
-    let iconURL = searchProvider.favicon_url ||
-                  (icons && extension.baseURI.resolve(IconDetails.getPreferredIcon(icons).icon));
-    let iconList = [];
-    if (icons) {
-      iconList = Object.entries(icons).map(icon => {
-        return {width: icon[0], height: icon[0],
-                url: extension.baseURI.resolve(icon[1])};
-      });
-    }
     try {
-      let params = {
-        template: searchProvider.search_url,
-        searchPostParams: searchProvider.search_url_post_params,
-        iconURL,
-        icons: iconList,
-        alias: searchProvider.keyword,
-        extensionID: extension.id,
-        isBuiltIn: extension.isPrivileged,
-        suggestURL: searchProvider.suggest_url,
-        suggestPostParams: searchProvider.suggest_url_post_params,
-        queryCharset: "UTF-8",
-        mozParams: searchProvider.params,
-      };
-      Services.search.addEngineWithDetails(searchProvider.name.trim(), params);
+      Services.search.addEnginesFromExtension(extension);
+      // Bug 1488516.  Preparing to support multiple engines per extension so
+      // multiple locales can be loaded.
+      let engines = Services.search.getEnginesByExtensionID(extension.id);
       await ExtensionSettingsStore.addSetting(
         extension.id, DEFAULT_SEARCH_STORE_TYPE, ENGINE_ADDED_SETTING_NAME,
-        searchProvider.name.trim());
+        engines[0].name);
       if (extension.startupReason === "ADDON_UPGRADE") {
-        let engine = Services.search.getEngineByName(searchProvider.name.trim());
+        let engine = Services.search.getEngineByName(engines[0].name);
         if (isCurrent) {
           Services.search.currentEngine = engine;
         }
         if (index != -1) {
           Services.search.moveEngine(engine, index);
         }
       }
     } catch (e) {
--- a/netwerk/base/nsIBrowserSearchService.idl
+++ b/netwerk/base/nsIBrowserSearchService.idl
@@ -382,16 +382,18 @@ interface nsIBrowserSearchService : nsIS
   void addEngineWithDetails(in AString name,
                             in jsval iconURL,
                             [optional] in AString alias,
                             [optional] in AString description,
                             [optional] in AString method,
                             [optional] in AString url,
                             [optional] in AString extensionID);
 
+  void addEnginesFromExtension(in jsval extension);
+
   /**
    * Un-hides all engines in the set of engines returned by getDefaultEngines.
    */
   void restoreDefaultEngines();
 
   /**
    * Returns an engine with the specified alias.
    *
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -13,16 +13,17 @@ XPCOMUtils.defineLazyModuleGetters(this,
   OS: "resource://gre/modules/osfile.jsm",
   TelemetryStopwatch: "resource://gre/modules/TelemetryStopwatch.jsm",
   Deprecated: "resource://gre/modules/Deprecated.jsm",
   SearchStaticData: "resource://gre/modules/SearchStaticData.jsm",
   setTimeout: "resource://gre/modules/Timer.jsm",
   clearTimeout: "resource://gre/modules/Timer.jsm",
   Lz4: "resource://gre/modules/lz4.js",
   NetUtil: "resource://gre/modules/NetUtil.jsm",
+  ExtensionParent: "resource://gre/modules/ExtensionParent.jsm",
 });
 
 XPCOMUtils.defineLazyServiceGetters(this, {
   gEnvironment: ["@mozilla.org/process/environment;1", "nsIEnvironment"],
   gChromeReg: ["@mozilla.org/chrome/chrome-registry;1", "nsIChromeRegistry"],
 });
 
 const BROWSER_SEARCH_PREF = "browser.search.";
@@ -3965,16 +3966,48 @@ SearchService.prototype = {
       newEngine._loadPath += ":" + params.extensionID;
     }
     this._addEngineToStore(newEngine);
     if (isCurrent) {
       this.currentEngine = newEngine;
     }
   },
 
+  addEnginesFromExtension(extension) {
+    let {IconDetails} = ExtensionParent;
+    let {manifest} = extension;
+
+    // General set of icons for an engine.
+    let icons = extension.manifest.icons;
+    let iconList = [];
+    if (icons) {
+      iconList = Object.entries(icons).map(icon => {
+        return {width: icon[0], height: icon[0],
+                url: extension.baseURI.resolve(icon[1])};
+      });
+    }
+    let preferredIconUrl = icons && extension.baseURI.resolve(IconDetails.getPreferredIcon(icons).icon);
+
+    let searchProvider = manifest.chrome_settings_overrides.search_provider;
+    let params = {
+      template: searchProvider.search_url,
+      searchPostParams: searchProvider.search_url_post_params,
+      iconURL: searchProvider.favicon_url || preferredIconUrl,
+      icons: iconList,
+      alias: searchProvider.keyword,
+      extensionID: extension.id,
+      isBuiltIn: extension.isPrivileged,
+      suggestURL: searchProvider.suggest_url,
+      suggestPostParams: searchProvider.suggest_url_post_params,
+      queryCharset: "UTF-8",
+      mozParams: searchProvider.params,
+    };
+    this.addEngineWithDetails(searchProvider.name.trim(), params);
+  },
+
   addEngine: function SRCH_SVC_addEngine(aEngineURL, aDataType, aIconURL,
                                          aConfirm, aCallback, aExtensionID) {
     LOG("addEngine: Adding \"" + aEngineURL + "\".");
     this._ensureInitialized();
     try {
       var uri = makeURI(aEngineURL);
       var engine = new Engine(uri, false);
       if (aCallback) {