Verified Commit 80c0f80a authored by henry's avatar henry Committed by boklm
Browse files

fixup! BB 43525: Skip Remote Settings for search engine customization.

TB 44647: Restore upstream's logic for getConfiguration* methods.
parent b50d9e58
Loading
Loading
Loading
Loading
+49 −9
Original line number Diff line number Diff line
@@ -92,19 +92,42 @@ export class SearchEngineSelector {
      return this.#getConfigurationPromise;
    }

    let { promise, resolve } = Promise.withResolvers();
    this.#getConfigurationPromise = promise;
    this.#configuration = await (
      await fetch(
        "chrome://global/content/search/base-browser-search-engines.json"
      )
    ).json();
    resolve(this.#configuration);
    this.#getConfigurationPromise = Promise.all([
      this.#getConfiguration(),
      this.#getConfigurationOverrides(),
    ]);
    let remoteSettingsData = await this.#getConfigurationPromise;
    this.#configuration = remoteSettingsData[0];
    this.#getConfigurationPromise = null;

    if (!this.#configuration?.length) {
      throw Components.Exception(
        "Failed to get engine data from Remote Settings",
        Cr.NS_ERROR_UNEXPECTED
      );
    }

    /**
     * Records whether the listeners have been added or not.
     */
    if (!this.#listenerAdded) {
      this.#remoteConfig.on("sync", this.#boundOnConfigurationUpdated);
      this.#remoteConfigOverrides.on(
        "sync",
        this.#boundOnConfigurationOverridesUpdated
      );
      /**
       * Records whether the listeners have been added or not.
       */
      this.#listenerAdded = true;
    }

    this.#selector.setSearchConfig(
      JSON.stringify({ data: this.#configuration })
    );
    this.#selector.setConfigOverrides(JSON.stringify({ data: [] }));
    this.#selector.setConfigOverrides(
      JSON.stringify({ data: remoteSettingsData[1] })
    );

    return this.#configuration;
  }
@@ -394,6 +417,23 @@ export class SearchEngineSelector {
    }
  }

  /**
   * Obtains the configuration overrides from remote settings.
   *
   * @returns {Promise<object[]>}
   *   An array of objects in the database, or an empty array if none
   *   could be obtained.
   */
  async #getConfigurationOverrides() {
    let result = [];
    try {
      result = await this.#remoteConfigOverrides.get();
    } catch (ex) {
      // This data is remote only, so we just return an empty array if it fails.
    }
    return result;
  }

  /**
   * @type {InstanceType<typeof lazy.SearchEngineSelector>?}
   */