Bug 1407209 - Add observer for preference changes whilst extensions are being updated. r=aswan,baku,bsilverberg

MozReview-Commit-ID: 5CqpYDc4tCg

--HG--
extra : rebase_source : 0170a0685d930a8fb1f7ca6b2e7206c0a655abe2
This commit is contained in:
Jonathan Kingston 2017-10-16 18:44:50 +01:00
Родитель 09e9d30178
Коммит 79802d262c
3 изменённых файлов: 87 добавлений и 11 удалений

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

@ -115,17 +115,29 @@ _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");
},
// observe() is only used to listen to container enabling pref
async observe() {
const contextualIdentitiesEnabled = Services.prefs.getBoolPref(CONTEXTUAL_IDENTITY_ENABLED_PREF);
if (!contextualIdentitiesEnabled) {
await this.closeContainerTabs();
this.notifyAllContainersCleared();
this.resetDefault();
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;
}
},

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

@ -25,6 +25,7 @@ const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
const {Management} = Cu.import("resource://gre/modules/Extension.jsm", {});
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionSettingsStore",
"resource://gre/modules/ExtensionSettingsStore.jsm");
@ -35,12 +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);
break;
@ -50,9 +59,13 @@ Management.on("shutdown", (type, extension) => {
}
});
Management.on("startup", (type, extension) => {
Management.on("startup", async (type, extension) => {
if (["ADDON_ENABLE", "ADDON_UPGRADE", "ADDON_DOWNGRADE"].includes(extension.startupReason)) {
this.ExtensionPreferencesManager.enableAll(extension);
const enablePromise = this.ExtensionPreferencesManager.enableAll(extension);
if (ADDON_REPLACE_REASONS.has(extension.startupReason)) {
await enablePromise;
Services.obs.notifyObservers(null, "web-extension-preferences-replaced");
}
}
});
/* eslint-enable mozilla/balanced-listeners */

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

@ -125,7 +125,7 @@ add_task(async function test_contextualIdentity_with_permissions() {
const CONTAINERS_PREF = "privacy.userContext.enabled";
const initial = Services.prefs.getBoolPref(CONTAINERS_PREF);
async function background(ver) {
async function background() {
let ci;
await browser.test.assertRejects(browser.contextualIdentities.get("foobar"), "Invalid contextual identity: foobar", "API should reject here");
await browser.test.assertRejects(browser.contextualIdentities.update("foobar", {name: "testing"}), "Invalid contextual identity: foobar", "API should reject for unknown updates");
@ -265,7 +265,7 @@ add_task(async function test_contextualIdentity_with_permissions() {
add_task(async function test_contextualIdentity_extensions_enable_containers() {
const CONTAINERS_PREF = "privacy.userContext.enabled";
const initial = Services.prefs.getBoolPref(CONTAINERS_PREF);
async function background(ver) {
async function background() {
let ci = await browser.contextualIdentities.get("firefox-container-1");
browser.test.assertTrue(!!ci, "We have an identity");
@ -328,3 +328,54 @@ add_task(async function test_contextualIdentity_extensions_enable_containers() {
Services.prefs.clearUserPref(CONTAINERS_PREF);
});
add_task(async function test_contextualIdentity_preference_change() {
const CONTAINERS_PREF = "privacy.userContext.enabled";
async function background() {
let extensionInfo = await browser.management.getSelf();
if (extensionInfo.version == "1.0.0") {
const containers = await browser.contextualIdentities.query({});
browser.test.assertEq(containers.length, 4, "We still have the original containers");
await browser.contextualIdentities.create({
name: "foobar",
color: "red",
icon: "circle",
});
}
const containers = await browser.contextualIdentities.query({});
browser.test.assertEq(containers.length, 5, "We have a new container");
if (extensionInfo.version == "1.1.0") {
await browser.contextualIdentities.remove(containers[4].cookieStoreId);
}
browser.test.notifyPass("contextualIdentities");
}
function makeExtension(id, version) {
return ExtensionTestUtils.loadExtension({
useAddonManager: "temporary",
background,
manifest: {
version,
applications: {
gecko: {id},
},
permissions: ["contextualIdentities"],
},
});
}
Services.prefs.setBoolPref(CONTAINERS_PREF, false);
let extension = makeExtension("containers-pref-test@mozilla.org", "1.0.0");
await extension.startup();
await extension.awaitFinish("contextualIdentities");
equal(Services.prefs.getBoolPref(CONTAINERS_PREF), true, "Pref should now be enabled, whatever it's initial state");
let extension2 = makeExtension("containers-pref-test@mozilla.org", "1.1.0");
await extension2.startup();
await extension2.awaitFinish("contextualIdentities");
const prefChange = waitForPrefChange(CONTAINERS_PREF);
await extension.unload();
await prefChange;
Services.prefs.clearUserPref(CONTAINERS_PREF);
});