зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1879625 - Load settings into search engines at the time of their creation. r=search-reviewers,jteow
For some other patches, we need to be able to have the user's saved engine settings loaded into the search engine objects striaght away. Currently, they are generally not loaded until sometime later, and in the case of engines loaded in `loadEnginesFromSettings`, they are actually loaded twice. Additionally, I think it makes more sense to pass the data in when they are constructed. Differential Revision: https://phabricator.services.mozilla.com/D201270
This commit is contained in:
Родитель
c7b5a34096
Коммит
61963d1669
|
@ -84,14 +84,17 @@ export class AddonSearchEngine extends SearchEngine {
|
|||
* @param {object} [options.config]
|
||||
* The search engine configuration for application provided engines, that
|
||||
* may be overriding some of the WebExtension's settings.
|
||||
* @param {object[]} [options.enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
*/
|
||||
async init({ extension, locale, config }) {
|
||||
async init({ extension, locale, config, enginesSettings }) {
|
||||
let { baseURI, manifest } = await this.#getExtensionDetailsForLocale(
|
||||
extension,
|
||||
locale
|
||||
);
|
||||
|
||||
this.#initFromManifest(baseURI, manifest, locale, config);
|
||||
this._loadSettings(enginesSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,10 +27,14 @@ export class AppProvidedSearchEngine extends SearchEngine {
|
|||
]);
|
||||
|
||||
/**
|
||||
* @param {object} config
|
||||
* @param {object} options
|
||||
* The options for this search engine.
|
||||
* @param {object} options.config
|
||||
* The engine config from Remote Settings.
|
||||
* @param {object[]} [options.enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
*/
|
||||
constructor(config) {
|
||||
constructor({ config, enginesSettings }) {
|
||||
// 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
|
||||
|
@ -48,6 +52,8 @@ export class AppProvidedSearchEngine extends SearchEngine {
|
|||
this._locale = config.webExtension.locale;
|
||||
|
||||
this.#init(config);
|
||||
|
||||
this._loadSettings(enginesSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,8 @@ export class PolicySearchEngine extends SearchEngine {
|
|||
* The options for this search engine.
|
||||
* @param {object} options.details
|
||||
* An object that matches the `SearchEngines` policy schema.
|
||||
* @param {object[]} [options.enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
*
|
||||
* @see browser/components/enterprisepolicies/schemas/policies-schema.json
|
||||
*/
|
||||
|
@ -45,6 +47,8 @@ export class PolicySearchEngine extends SearchEngine {
|
|||
suggest_url: options.details.SuggestURLTemplate,
|
||||
};
|
||||
this._initWithDetails(details);
|
||||
|
||||
this._loadSettings(options.enginesSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1178,6 +1178,31 @@ export class SearchEngine {
|
|||
delete this._metaData[name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads engine settings (_metaData) from the list of settings, finding
|
||||
* the appropriate details for this engine.
|
||||
*
|
||||
* @param {object[]} enginesSettings
|
||||
* An array of search engine settings.
|
||||
*/
|
||||
_loadSettings(enginesSettings) {
|
||||
if (!enginesSettings) {
|
||||
return;
|
||||
}
|
||||
let engineSettings = enginesSettings.find(e => e.id == this.id);
|
||||
|
||||
// If we can't find it by id, try falling back to the name as this is
|
||||
// probably an upgrade from a version before we stored ids in the user's
|
||||
// settings file.
|
||||
if (!engineSettings) {
|
||||
engineSettings = enginesSettings.find(e => e._name == this.name);
|
||||
}
|
||||
|
||||
if (engineSettings?._metaData) {
|
||||
this._metaData = structuredClone(engineSettings._metaData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user-defined alias.
|
||||
*
|
||||
|
|
|
@ -584,10 +584,12 @@ export class SearchService {
|
|||
*
|
||||
* @param {object} details
|
||||
* An object that matches the `SearchEngines` policy schema.
|
||||
* @param {object[]} [enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
* @see browser/components/enterprisepolicies/schemas/policies-schema.json
|
||||
*/
|
||||
async #addPolicyEngine(details) {
|
||||
let newEngine = new lazy.PolicySearchEngine({ details });
|
||||
async #addPolicyEngine(details, enginesSettings) {
|
||||
let newEngine = new lazy.PolicySearchEngine({ details, enginesSettings });
|
||||
lazy.logConsole.debug("Adding Policy Engine:", newEngine.name);
|
||||
this.#addEngineToStore(newEngine);
|
||||
}
|
||||
|
@ -1614,20 +1616,19 @@ export class SearchService {
|
|||
await lazy.AddonManager.readyPromise;
|
||||
}
|
||||
|
||||
await this.#loadEnginesFromConfig(engines);
|
||||
await this.#loadEnginesFromConfig(engines, settings.engines);
|
||||
|
||||
await this.#loadStartupEngines();
|
||||
await this.#loadStartupEngines(settings.engines);
|
||||
|
||||
this.#loadEnginesFromPolicies();
|
||||
this.#loadEnginesFromPolicies(settings.engines);
|
||||
|
||||
// `loadEnginesFromSettings` loads the engines and their settings together.
|
||||
this.#loadEnginesFromSettings(settings.engines);
|
||||
|
||||
// Settings file version 6 and below will need a migration to store the
|
||||
// engine ids rather than engine names.
|
||||
this._settings.migrateEngineIds(settings);
|
||||
|
||||
this.#loadEnginesMetadataFromSettings(settings.engines);
|
||||
|
||||
lazy.logConsole.debug("#loadEngines: done");
|
||||
|
||||
let newCurrentEngine = this._getEngineDefault(false);
|
||||
|
@ -1748,12 +1749,14 @@ export class SearchService {
|
|||
*
|
||||
* @param {Array} engineConfigs
|
||||
* An array of engines configurations based on the schema.
|
||||
* @param {object[]} [enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
*/
|
||||
async #loadEnginesFromConfig(engineConfigs) {
|
||||
async #loadEnginesFromConfig(engineConfigs, enginesSettings) {
|
||||
lazy.logConsole.debug("#loadEnginesFromConfig");
|
||||
for (let config of engineConfigs) {
|
||||
try {
|
||||
let engine = await this._makeEngineFromConfig(config);
|
||||
let engine = await this._makeEngineFromConfig(config, enginesSettings);
|
||||
this.#addEngineToStore(engine);
|
||||
} catch (ex) {
|
||||
console.error(
|
||||
|
@ -1768,8 +1771,11 @@ export class SearchService {
|
|||
/**
|
||||
* Loads any engines that have been received from the AddonManager during
|
||||
* startup and before we have finished initialising.
|
||||
*
|
||||
* @param {object[]} [enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
*/
|
||||
async #loadStartupEngines() {
|
||||
async #loadStartupEngines(enginesSettings) {
|
||||
if (
|
||||
this.#startupExtensions.size &&
|
||||
lazy.SearchUtils.newSearchConfigEnabled
|
||||
|
@ -1787,6 +1793,7 @@ export class SearchService {
|
|||
await this.#installExtensionEngine(
|
||||
extension,
|
||||
[lazy.SearchUtils.DEFAULT_TAG],
|
||||
enginesSettings,
|
||||
true
|
||||
);
|
||||
} catch (ex) {
|
||||
|
@ -1944,7 +1951,10 @@ export class SearchService {
|
|||
// Any remaining configuration engines are ones that we need to add.
|
||||
for (let engine of configEngines) {
|
||||
try {
|
||||
let newEngine = await this._makeEngineFromConfig(engine);
|
||||
let newEngine = await this._makeEngineFromConfig(
|
||||
engine,
|
||||
settings.engines
|
||||
);
|
||||
this.#addEngineToStore(newEngine, true);
|
||||
} catch (ex) {
|
||||
lazy.logConsole.warn(
|
||||
|
@ -1954,7 +1964,6 @@ export class SearchService {
|
|||
);
|
||||
}
|
||||
}
|
||||
this.#loadEnginesMetadataFromSettings(settings.engines);
|
||||
|
||||
// Now set the sort out the default engines and notify as appropriate.
|
||||
|
||||
|
@ -2151,26 +2160,13 @@ export class SearchService {
|
|||
engine._engineAddedToStore = true;
|
||||
}
|
||||
|
||||
#loadEnginesMetadataFromSettings(engineSettings) {
|
||||
if (!engineSettings) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let engineSetting of engineSettings) {
|
||||
let eng = this.#getEngineByName(engineSetting._name);
|
||||
if (eng) {
|
||||
lazy.logConsole.debug(
|
||||
"#loadEnginesMetadataFromSettings, transfering metadata for",
|
||||
engineSetting._name,
|
||||
engineSetting._metaData
|
||||
);
|
||||
|
||||
eng._metaData = engineSetting._metaData || {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#loadEnginesFromPolicies() {
|
||||
/**
|
||||
* Loads any search engines specified by enterprise policies.
|
||||
*
|
||||
* @param {object[]} [enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
*/
|
||||
#loadEnginesFromPolicies(enginesSettings) {
|
||||
if (Services.policies?.status != Ci.nsIEnterprisePolicies.ACTIVE) {
|
||||
return;
|
||||
}
|
||||
|
@ -2180,7 +2176,7 @@ export class SearchService {
|
|||
return;
|
||||
}
|
||||
for (let engineDetails of activePolicies.SearchEngines.Add ?? []) {
|
||||
this.#addPolicyEngine(engineDetails);
|
||||
this.#addPolicyEngine(engineDetails, enginesSettings);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2607,12 +2603,15 @@ export class SearchService {
|
|||
* @param {string} [options.locale]
|
||||
* The locale to use within the WebExtension. Defaults to the WebExtension's
|
||||
* default locale.
|
||||
* @param {object[]} [options.enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
* @param {initEngine} [options.initEngine]
|
||||
* Set to true if this engine is being loaded during initialization.
|
||||
*/
|
||||
async _createAndAddEngine({
|
||||
extension,
|
||||
locale = lazy.SearchUtils.DEFAULT_TAG,
|
||||
enginesSettings,
|
||||
initEngine = false,
|
||||
}) {
|
||||
// If we're in the startup cycle, and we've already loaded this engine,
|
||||
|
@ -2661,6 +2660,7 @@ export class SearchService {
|
|||
},
|
||||
});
|
||||
await newEngine.init({
|
||||
enginesSettings,
|
||||
extension,
|
||||
locale,
|
||||
});
|
||||
|
@ -2719,11 +2719,21 @@ export class SearchService {
|
|||
return extensionEngines;
|
||||
}
|
||||
|
||||
async #installExtensionEngine(extension, locales, initEngine = false) {
|
||||
async #installExtensionEngine(
|
||||
extension,
|
||||
locales,
|
||||
enginesSettings,
|
||||
initEngine = false
|
||||
) {
|
||||
lazy.logConsole.debug("installExtensionEngine:", extension.id);
|
||||
|
||||
let installLocale = async locale => {
|
||||
return this._createAndAddEngine({ extension, locale, initEngine });
|
||||
return this._createAndAddEngine({
|
||||
extension,
|
||||
locale,
|
||||
enginesSettings,
|
||||
initEngine,
|
||||
});
|
||||
};
|
||||
|
||||
let engines = [];
|
||||
|
@ -3503,10 +3513,12 @@ export class SearchService {
|
|||
* @param {object} config
|
||||
* The configuration object that defines the details of the engine
|
||||
* webExtensionId etc.
|
||||
* @param {object[]} [enginesSettings]
|
||||
* The saved settings for the search engines.
|
||||
* @returns {nsISearchEngine}
|
||||
* Returns the search engine object.
|
||||
*/
|
||||
async _makeEngineFromConfig(config) {
|
||||
async _makeEngineFromConfig(config, enginesSettings) {
|
||||
lazy.logConsole.debug("_makeEngineFromConfig:", config);
|
||||
|
||||
if (!lazy.SearchUtils.newSearchConfigEnabled) {
|
||||
|
@ -3523,13 +3535,14 @@ export class SearchService {
|
|||
},
|
||||
});
|
||||
await engine.init({
|
||||
enginesSettings,
|
||||
locale,
|
||||
config,
|
||||
});
|
||||
return engine;
|
||||
}
|
||||
|
||||
return new lazy.AppProvidedSearchEngine(config);
|
||||
return new lazy.AppProvidedSearchEngine({ config, enginesSettings });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Загрузка…
Ссылка в новой задаче