Skip to content

Commit

Permalink
Bug 1872940 - Implement support for prior settings when using the new…
Browse files Browse the repository at this point in the history
… search configuration. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D197628
  • Loading branch information
mandysGit committed Jan 25, 2024
1 parent 76d9ef1 commit 386c527
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 42 deletions.
12 changes: 8 additions & 4 deletions toolkit/components/search/AppProvidedSearchEngine.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ export class AppProvidedSearchEngine extends SearchEngine {
* The engine config from Remote Settings.
*/
constructor(config) {
let extensionId = config.identifier;
let id = config.identifier;
// TODO Bug 1875912 - Remove the webextension.id and webextension.locale when
// we're ready to remove old search-config and use search-config-v2 for all
// clients. The id in appProvidedSearchEngine should be changed to
// engine.identifier.
let extensionId = config.webExtension.id;
let id = config.webExtension.id + config.webExtension.locale;

super({
loadPath: "[app]" + extensionId,
Expand All @@ -41,7 +45,7 @@ export class AppProvidedSearchEngine extends SearchEngine {
});

this._extensionID = extensionId;
this._locale = "default";
this._locale = config.webExtension.locale;

this.#init(config);
}
Expand All @@ -61,7 +65,7 @@ export class AppProvidedSearchEngine extends SearchEngine {
update({ locale, configuration } = {}) {
this._urls = [];
this._iconMapObj = null;
this.#init(locale, configuration);
this.#init(configuration);
lazy.SearchUtils.notifyAction(this, lazy.SearchUtils.MODIFIED_TYPE.CHANGED);
}

Expand Down
54 changes: 36 additions & 18 deletions toolkit/components/search/SearchService.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ XPCOMUtils.defineLazyServiceGetter(
"nsIUpdateTimerManager"
);

// Exported to tests for not splitting ids when building webextension ids.
export const NON_SPLIT_ENGINE_IDS = [
"allegro-pl",
"bok-NO",
"daum-kr",
"faclair-beag",
"gulesider-NO",
"mapy-cz",
"naver-kr",
"prisjakt-sv-SE",
"seznam-cz",
"tyda-sv-SE",
"wolnelektury-pl",
"yahoo-jp",
"yahoo-jp-auctions",
];

const TOPIC_LOCALES_CHANGE = "intl:app-locales-changed";
const QUIT_APPLICATION_TOPIC = "quit-application";

Expand Down Expand Up @@ -2309,35 +2326,36 @@ export class SearchService {
}
e.webExtension.locale =
e.webExtension?.locale ?? lazy.SearchUtils.DEFAULT_TAG;

// TODO Bug 1875912 - Remove the webextension.id and webextension.locale when
// we're ready to remove old search-config and use search-config-v2 for all
// clients. The id in appProvidedSearchEngine should be changed to
// engine.identifier.
if (lazy.SearchUtils.newSearchConfigEnabled) {
let identifierComponents = NON_SPLIT_ENGINE_IDS.includes(e.identifier)
? [e.identifier]
: e.identifier.split("-");

let locale = identifierComponents.slice(1).join("-") || "default";

e.webExtension.id = identifierComponents[0] + "@search.mozilla.org";
e.webExtension.locale = locale;
}
}

return { engines, privateDefault };
}

#setDefaultAndOrdersFromSelector(engines, privateDefault) {
const defaultEngine = engines[0];
if (!lazy.SearchUtils.newSearchConfigEnabled) {
this._searchDefault = {
id: defaultEngine.webExtension.id,
locale: defaultEngine.webExtension.locale,
};
if (privateDefault) {
this.#searchPrivateDefault = {
id: privateDefault.webExtension.id,
locale: privateDefault.webExtension.locale,
};
}
return;
}

this._searchDefault = {
id: defaultEngine.identifier,
locale: "default",
id: defaultEngine.webExtension.id,
locale: defaultEngine.webExtension.locale,
};
if (privateDefault) {
this.#searchPrivateDefault = {
id: privateDefault.identifier,
locale: "default",
id: privateDefault.webExtension.id,
locale: privateDefault.webExtension.locale,
};
}
}
Expand Down
24 changes: 24 additions & 0 deletions toolkit/components/search/tests/SearchTestUtils.sys.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MockRegistrar } from "resource://testing-common/MockRegistrar.sys.mjs";
import { NON_SPLIT_ENGINE_IDS } from "resource://gre/modules/SearchService.sys.mjs";

const lazy = {};

Expand Down Expand Up @@ -203,6 +204,29 @@ export var SearchTestUtils = {
*/
async searchConfigToEngines(engineConfigurations) {
let engines = [];

for (let e of engineConfigurations) {
if (!e.webExtension) {
e.webExtension = {};
}
e.webExtension.locale =
e.webExtension.locale ?? lazy.SearchUtils.DEFAULT_TAG;

// TODO Bug 1875912 - Remove the webextension.id and webextension.locale when
// we're ready to remove old search-config and use search-config-v2 for all
// clients. The id in appProvidedSearchEngine should be changed to
// engine.identifier.
if (lazy.SearchUtils.newSearchConfigEnabled) {
let identifierComponents = NON_SPLIT_ENGINE_IDS.includes(e.identifier)
? [e.identifier]
: e.identifier.split("-");

e.webExtension.locale =
identifierComponents.slice(1).join("-") || "default";
e.webExtension.id = identifierComponents[0] + "@search.mozilla.org";
}
}

for (let config of engineConfigurations) {
let engine = await Services.search.wrappedJSObject._makeEngineFromConfig(
config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,13 @@ class SearchConfigTest {
}

async _getEngines(region, locale) {
let engines = [];
let configs = await engineSelector.fetchEngineConfiguration({
locale,
region: region || "default",
channel: SearchUtils.MODIFIED_APP_CHANNEL,
});
for (let config of configs.engines) {
let engine = await Services.search.wrappedJSObject._makeEngineFromConfig(
config
);
engines.push(engine);
}
return engines;

return SearchTestUtils.searchConfigToEngines(configs.engines);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,11 @@ add_task(async function test_expected_distribution_engines() {
});
let engines = await SearchTestUtils.searchConfigToEngines(config.engines);
searchService._engines = engines;
if (SearchUtils.newSearchConfigEnabled) {
searchService._searchDefault = {
id: config.engines[0].identifier,
locale: "default",
};
} else {
searchService._searchDefault = {
id: config.engines[0].webExtension.id,
locale:
config.engines[0]?.webExtension?.locale ?? SearchUtils.DEFAULT_TAG,
};
}
searchService._searchDefault = {
id: config.engines[0].webExtension.id,
locale:
config.engines[0]?.webExtension?.locale ?? SearchUtils.DEFAULT_TAG,
};
engines = searchService._sortEnginesByDefaults(engines);
test(engines);
}
Expand Down

0 comments on commit 386c527

Please sign in to comment.