Bug 1378882 - Support is_default for built-in engines only. r=mixedpuppy

MozReview-Commit-ID: C4iM2boQhK3

--HG--
extra : rebase_source : 1907777fa8ffcfe884ff2e22113f2f3e33698abc
This commit is contained in:
Michael Kaply 2017-07-18 11:28:33 -05:00
Родитель a66ad814f1
Коммит a8eb0ae196
4 изменённых файлов: 249 добавлений и 25 удалений

Просмотреть файл

@ -4,8 +4,30 @@
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
const {Management} = Cu.import("resource://gre/modules/Extension.jsm", {});
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionPreferencesManager",
"resource://gre/modules/ExtensionPreferencesManager.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionSettingsStore",
"resource://gre/modules/ExtensionSettingsStore.jsm");
const LOCAL_STORE_TYPE = "dynamic_values";
const DEFAULT_SEARCH_SETTING_NAME = "defaultSearch";
const processDefaultSearchSetting = (extension, action) => {
let item = ExtensionSettingsStore[action](extension, LOCAL_STORE_TYPE, DEFAULT_SEARCH_SETTING_NAME);
if (["disable", "removeSetting"].includes(action) && item) {
try {
let engine = Services.search.getEngineByName(item.value || item.initialValue);
if (engine && Services.search.getDefaultEngines().includes(engine)) {
Services.search.currentEngine = engine;
}
} catch (e) {
Components.utils.reportError(e);
}
}
};
const searchInitialized = () => {
return new Promise(resolve => {
@ -24,6 +46,31 @@ const searchInitialized = () => {
});
};
/* eslint-disable mozilla/balanced-listeners */
Management.on("shutdown", async (type, extension) => {
switch (extension.shutdownReason) {
case "ADDON_DISABLE":
case "ADDON_DOWNGRADE":
case "ADDON_UPGRADE":
await searchInitialized();
processDefaultSearchSetting(extension, "disable");
break;
case "ADDON_UNINSTALL":
await searchInitialized();
processDefaultSearchSetting(extension, "removeSetting");
break;
}
});
Management.on("startup", async (type, extension) => {
if (["ADDON_ENABLE", "ADDON_UPGRADE", "ADDON_DOWNGRADE"].includes(extension.startupReason)) {
await searchInitialized();
processDefaultSearchSetting(extension, "enable");
}
});
/* eslint-enable mozilla/balanced-listeners */
this.chrome_settings_overrides = class extends ExtensionAPI {
async onManifestEntry(entryName) {
let {extension} = this;
@ -34,39 +81,68 @@ this.chrome_settings_overrides = class extends ExtensionAPI {
manifest.chrome_settings_overrides.homepage);
}
if (manifest.chrome_settings_overrides.search_provider) {
let setDefault = false;
await searchInitialized();
let searchProvider = manifest.chrome_settings_overrides.search_provider;
let isCurrent = false;
let index = -1;
if (extension.startupReason === "ADDON_UPGRADE") {
let engines = Services.search.getEnginesByExtensionID(extension.id);
if (engines.length > 0) {
// There can be only one engine right now
isCurrent = Services.search.currentEngine == engines[0];
// Get position of engine and store it
index = Services.search.getEngines().indexOf(engines[0]);
Services.search.removeEngine(engines[0]);
if (searchProvider.is_default) {
let engineName = searchProvider.name.trim();
let engine = Services.search.getEngineByName(engineName);
if (engine && Services.search.getDefaultEngines().includes(engine)) {
await ExtensionSettingsStore.addSetting(
extension, LOCAL_STORE_TYPE, DEFAULT_SEARCH_SETTING_NAME, engineName, () => {
return Services.search.currentEngine.name;
});
setDefault = true;
} else {
Components.utils.reportError("is_default can only be used for built-in engines.")
}
// If the setting exists for the extension, but is missing from the manifest,
// remove it
} else if (ExtensionSettingsStore.hasSetting(
extension, LOCAL_STORE_TYPE, DEFAULT_SEARCH_SETTING_NAME)) {
await searchInitialized();
processDefaultSearchSetting(extension, "removeSetting");
}
try {
Services.search.addEngineWithDetails(searchProvider.name.trim(),
searchProvider.favicon_url,
searchProvider.keyword, null,
"GET", searchProvider.search_url,
extension.id);
// We only process the rest of search_provider if we did not set
// the default engine
if (!setDefault) {
let isCurrent = false;
let index = -1;
if (extension.startupReason === "ADDON_UPGRADE") {
let engine = Services.search.getEngineByName(searchProvider.name.trim());
if (isCurrent) {
Services.search.currentEngine = engine;
}
if (index != -1) {
Services.search.moveEngine(engine, index);
let engines = Services.search.getEnginesByExtensionID(extension.id);
if (engines.length > 0) {
// There can be only one engine right now
isCurrent = Services.search.currentEngine == engines[0];
// Get position of engine and store it
index = Services.search.getEngines().indexOf(engines[0]);
Services.search.removeEngine(engines[0]);
}
}
} catch (e) {
Components.utils.reportError(e);
try {
Services.search.addEngineWithDetails(searchProvider.name.trim(),
searchProvider.favicon_url,
searchProvider.keyword, null,
"GET", searchProvider.search_url,
extension.id);
if (extension.startupReason === "ADDON_UPGRADE") {
let engine = Services.search.getEngineByName(searchProvider.name.trim());
if (isCurrent) {
Services.search.currentEngine = engine;
}
if (index != -1) {
Services.search.moveEngine(engine, index);
}
}
} catch (e) {
Components.utils.reportError(e);
}
}
}
let item = ExtensionSettingsStore.getSetting(LOCAL_STORE_TYPE, DEFAULT_SEARCH_SETTING_NAME);
if (item) {
await searchInitialized();
Services.search.currentEngine = Services.search.getEngineByName(item.value || item.initialValue);
}
}
async onShutdown(reason) {
let {extension} = this;

Просмотреть файл

@ -99,7 +99,7 @@
"is_default": {
"type": "boolean",
"optional": true,
"deprecated": "Unsupported on Firefox at this time."
"description": "Sets the default engine to a built-in engine only."
}
}
}

Просмотреть файл

@ -101,6 +101,7 @@ skip-if = debug || asan # Bug 1354681
[browser_ext_sessions_getRecentlyClosed_private.js]
[browser_ext_sessions_getRecentlyClosed_tabs.js]
[browser_ext_sessions_restore.js]
[browser_ext_settings_overrides_default_search.js]
[browser_ext_settings_overrides_search.js]
[browser_ext_sidebarAction.js]
[browser_ext_sidebarAction_browser_style.js]

Просмотреть файл

@ -0,0 +1,147 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
add_task(async function test_extension_setting_default_engine() {
let defaultEngineName = Services.search.currentEngine.name;
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();
is(Services.search.currentEngine.name, "DuckDuckGo", "Default engine should be DuckDuckGo");
await ext1.unload();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
});
add_task(async function test_extension_setting_invalid_name_default_engine() {
let defaultEngineName = Services.search.currentEngine.name;
let ext1 = ExtensionTestUtils.loadExtension({
manifest: {
"chrome_settings_overrides": {
"search_provider": {
"name": "InvalidName",
"search_url": "https://example.com/?q={searchTerms}",
"is_default": true,
},
},
},
useAddonManager: "temporary",
});
await ext1.startup();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
await ext1.unload();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
});
add_task(async function test_extension_setting_multiple_default_engine() {
let defaultEngineName = Services.search.currentEngine.name;
let ext1 = ExtensionTestUtils.loadExtension({
manifest: {
"chrome_settings_overrides": {
"search_provider": {
"name": "DuckDuckGo",
"search_url": "https://example.com/?q={searchTerms}",
"is_default": true,
},
},
},
useAddonManager: "temporary",
});
let ext2 = ExtensionTestUtils.loadExtension({
manifest: {
"chrome_settings_overrides": {
"search_provider": {
"name": "Twitter",
"search_url": "https://example.com/?q={searchTerms}",
"is_default": true,
},
},
},
useAddonManager: "temporary",
});
await ext1.startup();
is(Services.search.currentEngine.name, "DuckDuckGo", "Default engine should be DuckDuckGo");
await ext2.startup();
is(Services.search.currentEngine.name, "Twitter", "Default engine should be Twitter");
await ext2.unload();
is(Services.search.currentEngine.name, "DuckDuckGo", "Default engine should be DuckDuckGo");
await ext1.unload();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
});
add_task(async function test_extension_setting_invalid_default_engine() {
let defaultEngineName = Services.search.currentEngine.name;
let ext1 = ExtensionTestUtils.loadExtension({
manifest: {
"chrome_settings_overrides": {
"search_provider": {
"name": "MozSearch",
"keyword": "MozSearch",
"search_url": "https://example.com/?q={searchTerms}",
},
},
},
useAddonManager: "temporary",
});
let ext2 = ExtensionTestUtils.loadExtension({
manifest: {
"chrome_settings_overrides": {
"search_provider": {
"name": "MozSearch",
"search_url": "https://example.com/?q={searchTerms}",
"is_default": true,
},
},
},
useAddonManager: "temporary",
});
await ext1.startup();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
let engine = Services.search.getEngineByName("MozSearch");
ok(engine, "Engine should exist.");
await ext2.startup();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
await ext2.unload();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
await ext1.unload();
is(Services.search.currentEngine.name, defaultEngineName, "Default engine should be " + defaultEngineName);
});