Bug 1642391 - Split SearchService._loadEngines into two to clearly separate out modern and legacy configuration code. r=daleharvey
authorMark Banner <standard8@mozilla.com>
Tue, 16 Jun 2020 18:58:52 +0000
changeset 535945 b3ac40fd7709b6072c9a76c31a1a0a85b83fe705
parent 535944 745ca9baa9c8891decb1394aca0b178299fd6d29
child 535946 b9f5e13996897102e9cba4b12f4e6368992f109f
push id37513
push userrmaries@mozilla.com
push dateWed, 17 Jun 2020 03:41:56 +0000
treeherdermozilla-central@0e023da23571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdaleharvey
bugs1642391
milestone79.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 1642391 - Split SearchService._loadEngines into two to clearly separate out modern and legacy configuration code. r=daleharvey Differential Revision: https://phabricator.services.mozilla.com/D78932
toolkit/components/search/SearchService.jsm
--- a/toolkit/components/search/SearchService.jsm
+++ b/toolkit/components/search/SearchService.jsm
@@ -1035,122 +1035,88 @@ SearchService.prototype = {
    * Loads engines asynchronously.
    *
    * @param {object} cache
    *   An object representing the search engine cache.
    * @param {boolean} isReload
    *   Set to true if this load is happening during a reload.
    */
   async _loadEngines(cache, isReload) {
+    if (!gModernConfig) {
+      await this._loadEnginesLegacy(cache, isReload);
+      return;
+    }
+
     logConsole.debug("_loadEngines: start");
-    let engines = await this._findEngines();
+    let engines = await this._findEngineSelectorEngines();
 
     let buildID = Services.appinfo.platformBuildID;
     let rebuildCache =
       gEnvironment.get("RELOAD_ENGINES") ||
       !cache.engines ||
       cache.version != SearchUtils.CACHE_VERSION ||
       cache.locale != Services.locale.requestedLocale ||
       cache.buildID != buildID;
 
     let enginesCorrupted = false;
     if (!rebuildCache) {
-      if (gModernConfig) {
-        const notInCacheEngines = engine => {
-          return !cache.builtInEngineList.find(details => {
-            return (
-              engine.webExtension.id == details.id &&
-              engine.webExtension.locale == details.locale
-            );
-          });
-        };
-
-        rebuildCache =
-          !cache.builtInEngineList ||
-          cache.builtInEngineList.length != engines.length ||
-          engines.some(notInCacheEngines);
-
-        if (
-          !rebuildCache &&
-          cache.engines.filter(e => e._isBuiltin).length !=
-            cache.builtInEngineList.length
-        ) {
-          rebuildCache = true;
-          enginesCorrupted = true;
-        }
-      } else {
-        function notInCacheVisibleEngines(engineName) {
-          return !cache.visibleDefaultEngines.includes(engineName);
-        }
-        // Legacy config.
-        rebuildCache =
-          cache.visibleDefaultEngines.length !=
-            this._visibleDefaultEngines.length ||
-          this._visibleDefaultEngines.some(notInCacheVisibleEngines);
-
-        // We don't do a built-in list comparison with distributions because they
-        // have a different set of built-ins to that given from the configuration.
-        if (
-          !rebuildCache &&
-          SearchUtils.distroID == "" &&
-          cache.engines.filter(e => e._isBuiltin).length !=
-            cache.visibleDefaultEngines.length
-        ) {
-          rebuildCache = true;
-          enginesCorrupted = true;
-        }
+      const notInCacheEngines = engine => {
+        return !cache.builtInEngineList.find(details => {
+          return (
+            engine.webExtension.id == details.id &&
+            engine.webExtension.locale == details.locale
+          );
+        });
+      };
+
+      rebuildCache =
+        !cache.builtInEngineList ||
+        cache.builtInEngineList.length != engines.length ||
+        engines.some(notInCacheEngines);
+
+      if (
+        !rebuildCache &&
+        cache.engines.filter(e => e._isBuiltin).length !=
+          cache.builtInEngineList.length
+      ) {
+        rebuildCache = true;
+        enginesCorrupted = true;
       }
     }
 
     Services.telemetry.scalarSet(
       "browser.searchinit.engines_cache_corrupted",
       enginesCorrupted
     );
 
     if (!rebuildCache) {
       logConsole.debug("_loadEngines: loading from cache directories");
-      if (gModernConfig) {
-        const newEngines = await this._loadEnginesFromConfig(engines, isReload);
-        for (let engine of newEngines) {
-          this._addEngineToStore(engine);
-        }
-        // TODO: Remove the second argument when we remove the legacy config
-        // (the function should then always skip-builtin engines).
-        this._loadEnginesFromCache(cache, true);
-        this._loadEnginesMetadataFromCache(cache);
-      } else {
-        this._loadEnginesFromCache(cache);
+      const newEngines = await this._loadEnginesFromConfig(engines, isReload);
+      for (let engine of newEngines) {
+        this._addEngineToStore(engine);
       }
+      // TODO: Remove the second argument when we remove the legacy config
+      // (the function should then always skip-builtin engines).
+      this._loadEnginesFromCache(cache, true);
+      this._loadEnginesMetadataFromCache(cache);
       if (this._engines.size) {
         logConsole.debug("_loadEngines: done using existing cache");
         return;
       }
       logConsole.debug(
         "_loadEngines: No valid engines found in cache. Loading engines from disk."
       );
     }
 
     logConsole.debug(
       "_loadEngines: Absent or outdated cache. Loading engines from disk."
     );
-    if (gModernConfig) {
-      let newEngines = await this._loadEnginesFromConfig(engines, isReload);
-      newEngines.forEach(this._addEngineToStore, this);
-    } else {
-      let distDirs = await this._getDistibutionEngineDirectories();
-      for (let loadDir of distDirs) {
-        let enginesFromDir = await this._loadEnginesFromDir(loadDir);
-        enginesFromDir.forEach(this._addEngineToStore, this);
-      }
-
-      let engineList = this._enginesToLocales(engines);
-      for (let [id, locales] of engineList) {
-        await this.ensureBuiltinExtension(id, locales, isReload);
-      }
-    }
+    let newEngines = await this._loadEnginesFromConfig(engines, isReload);
+    newEngines.forEach(this._addEngineToStore, this);
+
     logConsole.debug(
       "_loadEngines: loading",
       this._startupExtensions.size,
       "engines reported by AddonManager startup"
     );
     for (let extension of this._startupExtensions) {
       await this._installExtensionEngine(
         extension,
@@ -1165,16 +1131,114 @@ SearchService.prototype = {
     this._loadEnginesFromCache(cache, true);
 
     this._loadEnginesMetadataFromCache(cache);
 
     logConsole.debug("_loadEngines: done using rebuilt cache");
   },
 
   /**
+   * Loads engines asynchronously (legacy configuration).
+   *
+   * @param {object} cache
+   *   An object representing the search engine cache.
+   * @param {boolean} isReload
+   *   Set to true if this load is happening during a reload.
+   */
+  async _loadEnginesLegacy(cache, isReload) {
+    logConsole.debug("_loadEnginesLegacy: start");
+    let engines = await this._findEnginesLegacy();
+
+    let buildID = Services.appinfo.platformBuildID;
+    let rebuildCache =
+      gEnvironment.get("RELOAD_ENGINES") ||
+      !cache.engines ||
+      cache.version != SearchUtils.CACHE_VERSION ||
+      cache.locale != Services.locale.requestedLocale ||
+      cache.buildID != buildID;
+
+    let enginesCorrupted = false;
+    if (!rebuildCache) {
+      function notInCacheVisibleEngines(engineName) {
+        return !cache.visibleDefaultEngines.includes(engineName);
+      }
+      // Legacy config.
+      rebuildCache =
+        cache.visibleDefaultEngines.length !=
+          this._visibleDefaultEngines.length ||
+        this._visibleDefaultEngines.some(notInCacheVisibleEngines);
+
+      // We don't do a built-in list comparison with distributions because they
+      // have a different set of built-ins to that given from the configuration.
+      if (
+        !rebuildCache &&
+        SearchUtils.distroID == "" &&
+        cache.engines.filter(e => e._isBuiltin).length !=
+          cache.visibleDefaultEngines.length
+      ) {
+        rebuildCache = true;
+        enginesCorrupted = true;
+      }
+    }
+
+    Services.telemetry.scalarSet(
+      "browser.searchinit.engines_cache_corrupted",
+      enginesCorrupted
+    );
+
+    if (!rebuildCache) {
+      logConsole.debug("_loadEnginesLegacy: loading from cache directories");
+      this._loadEnginesFromCache(cache);
+      if (this._engines.size) {
+        logConsole.debug("_loadEnginesLegacy: done using existing cache");
+        return;
+      }
+      logConsole.debug(
+        "_loadEnginesLegacy: No valid engines found in cache. Loading engines from disk."
+      );
+    }
+
+    logConsole.debug(
+      "_loadEnginesLegacy: Absent or outdated cache. Loading engines from disk."
+    );
+    let distDirs = await this._getDistibutionEngineDirectories();
+    for (let loadDir of distDirs) {
+      let enginesFromDir = await this._loadEnginesFromDir(loadDir);
+      enginesFromDir.forEach(this._addEngineToStore, this);
+    }
+
+    let engineList = this._enginesToLocales(engines);
+    for (let [id, locales] of engineList) {
+      await this.ensureBuiltinExtension(id, locales, isReload);
+    }
+
+    logConsole.debug(
+      "_loadEnginesLegacy: loading",
+      this._startupExtensions.size,
+      "engines reported by AddonManager startup"
+    );
+    for (let extension of this._startupExtensions) {
+      await this._installExtensionEngine(
+        extension,
+        [SearchUtils.DEFAULT_TAG],
+        true
+      );
+    }
+
+    logConsole.debug(
+      "_loadEnginesLegacy: loading user-installed engines from the obsolete cache"
+    );
+    this._loadEnginesFromCache(cache, true);
+
+    this._loadEnginesMetadataFromCache(cache);
+
+    logConsole.debug("_loadEnginesLegacy: done using rebuilt cache");
+  },
+
+  /**
    * Loads engines as specified by the configuration. We only expect
    * configured engines here, user engines should not be listed.
    *
    * @param {array} engineConfigs
    *   An array of engines configurations based on the schema.
    * @param {boolean} [isReload]
    *   Set to true to indicate a reload is happening.
    * @returns {array.<nsISearchEngine>}
@@ -1202,17 +1266,19 @@ SearchService.prototype = {
   /**
    * Get the directories that contain distribution engines.
    *
    * @returns {array}
    *   Returns an array of directories that contain distribution engines.
    */
   async _getDistibutionEngineDirectories() {
     if (gModernConfig) {
-      return [];
+      throw new Error(
+        "_getDistibutionEngineDirectories is obsolete for modern config."
+      );
     }
     // Get the non-empty distribution directories into distDirs...
     let distDirs = [];
     let locations;
     try {
       locations = Services.dirsvc.get(
         NS_APP_DISTRIBUTION_SEARCH_DIR_LIST,
         Ci.nsISimpleEnumerator
@@ -1830,37 +1896,41 @@ SearchService.prototype = {
   },
 
   /**
    * Loads the list of engines from list.json
    *
    * @returns {Array<string>}
    *   Returns an array of engine names.
    */
-  async _findEngines() {
-    if (gModernConfig) {
-      return this._findEngineSelectorEngines();
-    }
-    logConsole.debug("_findEngines: looking for engines in list.json");
+  async _findEnginesLegacy() {
+    logConsole.debug("_findEnginesLegacy: looking for engines in list.json");
 
     let chan = SearchUtils.makeChannel(this._listJSONURL);
     if (!chan) {
-      logConsole.debug("_findEngines:", this._listJSONURL, "isn't registered");
+      logConsole.debug(
+        "_findEnginesLegacy:",
+        this._listJSONURL,
+        "isn't registered"
+      );
       return [];
     }
 
     // Read list.json to find the engines we need to load.
     let request = new XMLHttpRequest();
     request.overrideMimeType("text/plain");
     let list = await new Promise(resolve => {
       request.onload = function(event) {
         resolve(event.target.responseText);
       };
       request.onerror = function(event) {
-        logConsole.debug("_findEngines: failed to read", this._listJSONURL);
+        logConsole.debug(
+          "_findEnginesLegacy: failed to read",
+          this._listJSONURL
+        );
         resolve();
       };
       request.open("GET", Services.io.newURI(this._listJSONURL).spec, true);
       request.send();
     });
 
     return this._parseListJSON(list);
   },