Commit c562e03d authored by Mark Banner's avatar Mark Banner
Browse files

Bug 1641261 - Handle the case when an app provided engine is hidden/removed,...

Bug 1641261 - Handle the case when an app provided engine is hidden/removed, and a WebExtension wants to select as default. r=daleharvey

Differential Revision: https://phabricator.services.mozilla.com/D77429
parent 2a241751
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -379,8 +379,11 @@ this.chrome_settings_overrides = class extends ExtensionAPI {

    let engineName = searchProvider.name.trim();
    if (searchProvider.is_default) {
      if (await Services.search.maybeSetAndOverrideDefault(extension)) {
      let result = await Services.search.maybeSetAndOverrideDefault(extension);
      if (result.canChangeToAppProvided) {
        await this.setDefault(engineName);
      }
      if (!result.canInstallEngine) {
        return;
      }
    }
+42 −0
Original line number Diff line number Diff line
@@ -63,6 +63,48 @@ add_task(async function test_extension_setting_default_engine() {
  );
});

/* This tests what happens when the engine you're setting it to is hidden. */
add_task(async function test_extension_setting_default_engine_hidden() {
  let engine = Services.search.getEngineByName("DuckDuckGo");
  engine.hidden = true;

  let ext1 = ExtensionTestUtils.loadExtension({
    manifest: {
      chrome_settings_overrides: {
        search_provider: {
          name: "DuckDuckGo",
          search_url: "https://example.com/?q={searchTerms}",
          is_default: true,
        },
      },
    },
    useAddonManager: "temporary",
  });

  await ext1.startup();
  await AddonTestUtils.waitForSearchProviderStartup(ext1);

  is(
    (await Services.search.getDefault()).name,
    defaultEngineName,
    "Default engine should have remained as the default"
  );
  is(
    ExtensionSettingsStore.getSetting("default_search", "defaultSearch"),
    null,
    "The extension should not have been recorded as having set the default search"
  );

  await ext1.unload();

  is(
    (await Services.search.getDefault()).name,
    defaultEngineName,
    `Default engine is ${defaultEngineName}`
  );
  engine.hidden = false;
});

// Test the popup displayed when trying to add a non-built-in default
// search engine.
add_task(async function test_extension_setting_default_engine_external() {
+29 −7
Original line number Diff line number Diff line
@@ -717,8 +717,15 @@ SearchService.prototype = {
    let searchProvider =
      extension.manifest.chrome_settings_overrides.search_provider;
    let engine = this._engines.get(searchProvider.name);
    if (!engine || !engine.isAppProvided) {
      return false;
    if (!engine || !engine.isAppProvided || engine.hidden) {
      // If the engine is not application provided, then we shouldn't simply
      // set default to it.
      // If the engine is application provided, but hidden, then we don't
      // switch to it, nor do we try to install it.
      return {
        canChangeToAppProvided: false,
        canInstallEngine: !engine?.hidden,
      };
    }
    let params = this.getEngineParams(
      extension,
@@ -736,7 +743,10 @@ SearchService.prototype = {
    ) {
      // Don't allow an extension to set the default if it is already the default.
      if (this.defaultEngine.name == searchProvider.name) {
        return false;
        return {
          canChangeToAppProvided: false,
          canInstallEngine: false,
        };
      }
      if (
        !(await this._defaultOverrideAllowlist.canOverride(
@@ -750,7 +760,10 @@ SearchService.prototype = {
        );
        // We don't allow overriding the engine in this case, but we can allow
        // the extension to change the default engine.
        return true;
        return {
          canChangeToAppProvided: true,
          canInstallEngine: false,
        };
      }
      // We're ok to override.
      engine.overrideWithExtension(params);
@@ -758,7 +771,10 @@ SearchService.prototype = {
        "Allowing default engine to be set to app-provided and overridden.",
        extension.id
      );
      return true;
      return {
        canChangeToAppProvided: true,
        canInstallEngine: false,
      };
    }

    if (
@@ -773,10 +789,16 @@ SearchService.prototype = {
        "Re-enabling overriding of core extension by",
        extension.id
      );
      return true;
      return {
        canChangeToAppProvided: true,
        canInstallEngine: false,
      };
    }

    return false;
    return {
      canChangeToAppProvided: false,
      canInstallEngine: false,
    };
  },

  /**
+5 −2
Original line number Diff line number Diff line
@@ -500,8 +500,11 @@ interface nsISearchService : nsISupports
   *
   *  @param extension
   *        The extension to load from.
   *  @returns A boolean that indicates if the WebExtension engine may set the
   *           named engine as default (because it is application provided).
   *  @returns An object with two booleans:
   *        - canChangeToAppProvided: indicates if the WebExtension engine may
   *            set the named engine as default e.g. it is application provided.
   *        - canInstallEngine: indicates if the WebExtension engine may be
   *            installed, e.g. it is not an app-provided engine.
   */
  Promise maybeSetAndOverrideDefault(in jsval extension);