Bug 1404584 Part 3: Convert ExtensionPreferencesManager to use update and uninstall events, r=aswan,jkt

This includes removing the "web-extension-preferences-replacing" and "web-extension-preferences-replaced"
notifications as they are no longer needed.

MozReview-Commit-ID: IjNf4BImgas

--HG--
extra : rebase_source : a48e72c291ebd509d19e9e94196d3600eb7328c4
This commit is contained in:
Bob Silverberg 2017-11-01 09:51:41 -04:00
Родитель d5a84e4c0a
Коммит ea5edb4f6a
5 изменённых файлов: 44 добавлений и 69 удалений

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

@ -200,9 +200,8 @@ add_task(async function test_extension_setting_home_page_back() {
});
add_task(async function test_disable() {
let defaultHomePage = getHomePageURL();
const ID = "id@tests.mozilla.org";
let defaultHomePage = getHomePageURL();
let ext1 = ExtensionTestUtils.loadExtension({
manifest: {
@ -222,24 +221,26 @@ add_task(async function test_disable() {
await ext1.startup();
await prefPromise;
ok(getHomePageURL().endsWith(HOME_URI_1),
is(getHomePageURL(), HOME_URI_1,
"Home url should be overridden by the extension.");
let addon = await AddonManager.getAddonByID(ID);
is(addon.id, ID);
is(addon.id, ID, "Found the correct add-on.");
let disabledPromise = awaitEvent("shutdown", ID);
prefPromise = promisePrefChangeObserved(HOMEPAGE_URL_PREF);
addon.userDisabled = true;
await prefPromise;
await Promise.all([disabledPromise, prefPromise]);
is(getHomePageURL(), defaultHomePage,
"Home url should be the default");
let enabledPromise = awaitEvent("ready", ID);
prefPromise = promisePrefChangeObserved(HOMEPAGE_URL_PREF);
addon.userDisabled = false;
await prefPromise;
await Promise.all([enabledPromise, prefPromise]);
ok(getHomePageURL().endsWith(HOME_URI_1),
is(getHomePageURL(), HOME_URI_1,
"Home url should be overridden by the extension.");
prefPromise = promisePrefChangeObserved(HOMEPAGE_URL_PREF);

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

@ -3,32 +3,12 @@
"use strict";
XPCOMUtils.defineLazyGetter(this, "Management", () => {
const {Management} = Cu.import("resource://gre/modules/Extension.jsm", {});
return Management;
});
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");
const EXTENSION1_ID = "extension1@mozilla.com";
const EXTENSION2_ID = "extension2@mozilla.com";
function awaitEvent(eventName, id) {
return new Promise(resolve => {
let listener = (_eventName, ...args) => {
let extension = args[0];
if (_eventName === eventName &&
extension.id == id) {
Management.off(eventName, listener);
resolve(...args);
}
};
Management.on(eventName, listener);
});
}
let defaultEngineName = Services.search.currentEngine.name;
function restoreDefaultEngine() {

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

@ -20,6 +20,7 @@
* promiseContentDimensions alterContent
* promisePrefChangeObserved openContextMenuInFrame
* promiseAnimationFrame getCustomizableUIPanelID
* awaitEvent
*/
// There are shutdown issues for which multiple rejections are left uncaught.
@ -36,6 +37,11 @@ const {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm", {});
const {CustomizableUI} = Cu.import("resource:///modules/CustomizableUI.jsm", {});
const {Preferences} = Cu.import("resource://gre/modules/Preferences.jsm", {});
XPCOMUtils.defineLazyGetter(this, "Management", () => {
const {Management} = Cu.import("resource://gre/modules/Extension.jsm", {});
return Management;
});
// We run tests under two different configurations, from browser.ini and
// browser-remote.ini. When running from browser-remote.ini, the tests are
// copied to the sub-directory "test-oop-extensions", which we detect here, and
@ -460,3 +466,18 @@ function promisePrefChangeObserved(pref) {
resolve();
}));
}
function awaitEvent(eventName, id) {
return new Promise(resolve => {
let listener = (_eventName, ...args) => {
let extension = args[0];
if (_eventName === eventName &&
extension.id == id) {
Management.off(eventName, listener);
resolve();
}
};
Management.on(eventName, listener);
});
}

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

@ -117,29 +117,18 @@ _ContextualIdentityService.prototype = {
init(path) {
this._path = path;
this._webExtensionUpdating = false;
Services.prefs.addObserver(CONTEXTUAL_IDENTITY_ENABLED_PREF, this);
Services.obs.addObserver(this, "web-extension-preferences-replacing");
Services.obs.addObserver(this, "web-extension-preferences-replaced");
},
async observe(aSubject, aTopic) {
switch (aTopic) {
case "web-extension-preferences-replacing":
this._webExtensionUpdating = true;
break;
case "web-extension-preferences-replaced":
this._webExtensionUpdating = false;
// We want to check the pref when the extension has been replaced too
case "nsPref:changed":
const contextualIdentitiesEnabled = Services.prefs.getBoolPref(CONTEXTUAL_IDENTITY_ENABLED_PREF);
if (!contextualIdentitiesEnabled && !this._webExtensionUpdating) {
await this.closeContainerTabs();
this.notifyAllContainersCleared();
this.resetDefault();
}
break;
if (aTopic === "nsPref:changed") {
const contextualIdentitiesEnabled = Services.prefs.getBoolPref(CONTEXTUAL_IDENTITY_ENABLED_PREF);
if (!contextualIdentitiesEnabled) {
await this.closeContainerTabs();
this.notifyAllContainersCleared();
this.resetDefault();
}
}
},

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

@ -36,36 +36,20 @@ XPCOMUtils.defineLazyGetter(this, "defaultPreferences", function() {
return new Preferences({defaultBranch: true});
});
const ADDON_REPLACE_REASONS = new Set([
"ADDON_DOWNGRADE",
"ADDON_UPGRADE",
]);
/* eslint-disable mozilla/balanced-listeners */
Management.on("shutdown", (type, extension) => {
switch (extension.shutdownReason) {
case "ADDON_DISABLE":
case "ADDON_DOWNGRADE":
case "ADDON_UPGRADE":
if (ADDON_REPLACE_REASONS.has(extension.shutdownReason)) {
Services.obs.notifyObservers(null, "web-extension-preferences-replacing");
}
this.ExtensionPreferencesManager.disableAll(extension.id);
break;
Management.on("uninstall", (type, {id}) => {
ExtensionPreferencesManager.removeAll(id);
});
case "ADDON_UNINSTALL":
this.ExtensionPreferencesManager.removeAll(extension.id);
break;
Management.on("shutdown", (type, extension) => {
if (extension.shutdownReason == "ADDON_DISABLE") {
this.ExtensionPreferencesManager.disableAll(extension.id);
}
});
Management.on("startup", async (type, extension) => {
if (["ADDON_ENABLE", "ADDON_UPGRADE", "ADDON_DOWNGRADE"].includes(extension.startupReason)) {
const enablePromise = this.ExtensionPreferencesManager.enableAll(extension.id);
if (ADDON_REPLACE_REASONS.has(extension.startupReason)) {
await enablePromise;
Services.obs.notifyObservers(null, "web-extension-preferences-replaced");
}
if (extension.startupReason == "ADDON_ENABLE") {
this.ExtensionPreferencesManager.enableAll(extension.id);
}
});
/* eslint-enable mozilla/balanced-listeners */