Commit 3ebe295e authored by Felipe Gomes's avatar Felipe Gomes
Browse files

Bug 1495749 - Allow add-ons to be updated via policy by uninstalling and...

Bug 1495749 - Allow add-ons to be updated via policy by uninstalling and reinstalling them. r=mkaply

--HG--
rename : browser/components/enterprisepolicies/tests/browser/policytest.xpi => browser/components/enterprisepolicies/tests/browser/policytest_v0.1.xpi
parent 066dfd8f
......@@ -491,8 +491,28 @@ var Policies = {
"Extensions": {
onBeforeUIStartup(manager, param) {
let uninstallingPromise = Promise.resolve();
if ("Uninstall" in param) {
uninstallingPromise = runOncePerModification("extensionsUninstall", JSON.stringify(param.Uninstall), async () => {
// If we're uninstalling add-ons, re-run the extensionsInstall runOnce even if it hasn't
// changed, which will allow add-ons to be updated.
Services.prefs.clearUserPref("browser.policies.runOncePerModification.extensionsInstall");
let addons = await AddonManager.getAddonsByIDs(param.Uninstall);
for (let addon of addons) {
if (addon) {
try {
await addon.uninstall();
} catch (e) {
// This can fail for add-ons that can't be uninstalled.
// Just ignore.
}
}
}
});
}
if ("Install" in param) {
runOncePerModification("extensionsInstall", JSON.stringify(param.Install), () => {
runOncePerModification("extensionsInstall", JSON.stringify(param.Install), async () => {
await uninstallingPromise;
for (let location of param.Install) {
let url;
if (location.includes("://")) {
......@@ -544,21 +564,6 @@ var Policies = {
}
});
}
if ("Uninstall" in param) {
runOncePerModification("extensionsUninstall", JSON.stringify(param.Uninstall), async () => {
let addons = await AddonManager.getAddonsByIDs(param.Uninstall);
for (let addon of addons) {
if (addon) {
try {
addon.uninstall();
} catch (e) {
// This can fail for add-ons that can't be uninstalled.
// Just ignore.
}
}
}
});
}
if ("Locked" in param) {
for (let ID of param.Locked) {
manager.disallowFeature(`modify-extension:${ID}`);
......@@ -1046,6 +1051,8 @@ function runOnce(actionName, callback) {
* callback once when the policy is set, then never again.
* runOncePerModification runs the callback once each time the policy value
* changes from its previous value.
* If the callback that was passed is an async function, you can await on this
* function to await for the callback.
*
* @param {string} actionName
* A given name which will be used to track if this callback has run.
......@@ -1057,16 +1064,19 @@ function runOnce(actionName, callback) {
* string.
* @param {Function} callback
* The callback to be run when the pref value changes
* @returns Promise
* A promise that will resolve once the callback finishes running.
*
*/
function runOncePerModification(actionName, policyValue, callback) {
async function runOncePerModification(actionName, policyValue, callback) {
let prefName = `browser.policies.runOncePerModification.${actionName}`;
let oldPolicyValue = Services.prefs.getStringPref(prefName, undefined);
if (policyValue === oldPolicyValue) {
log.debug(`Not running action ${actionName} again because the policy's value is unchanged`);
return;
return Promise.resolve();
}
Services.prefs.setStringPref(prefName, policyValue);
callback();
return callback();
}
let gChromeURLSBlocked = false;
......
......@@ -5,7 +5,8 @@ support-files =
config_broken_json.json
opensearch.html
opensearchEngine.xml
policytest.xpi
policytest_v0.1.xpi
policytest_v0.2.xpi
policy_websitefilter_block.html
policy_websitefilter_exception.html
../../../../../toolkit/components/antitracking/test/browser/page.html
......
......@@ -3,6 +3,7 @@
"use strict";
const addonID = "policytest@mozilla.com";
const BASE_URL = "http://mochi.test:8888/browser/browser/components/enterprisepolicies/tests/browser";
add_task(async function test_addon_install() {
let installPromise = wait_for_addon_install();
......@@ -10,7 +11,7 @@ add_task(async function test_addon_install() {
"policies": {
"Extensions": {
"Install": [
"http://mochi.test:8888/browser/browser/components/enterprisepolicies/tests/browser/policytest.xpi",
`${BASE_URL}/policytest_v0.1.xpi`,
],
"Locked": [
addonID,
......@@ -21,6 +22,7 @@ add_task(async function test_addon_install() {
await installPromise;
let addon = await AddonManager.getAddonByID(addonID);
isnot(addon, null, "Addon not installed.");
is(addon.version, "0.1", "Addon version is correct");
Assert.deepEqual(addon.installTelemetryInfo, {source: "enterprise-policy"},
"Got the expected addon.installTelemetryInfo");
......@@ -41,7 +43,40 @@ add_task(async function test_addon_locked() {
BrowserTestUtils.removeTab(tab);
});
add_task(async function test_addon_reinstall() {
// Test that uninstalling and reinstalling the same addon ID works as expected.
// This can be used to update an addon.
let uninstallPromise = wait_for_addon_uninstall();
let installPromise = wait_for_addon_install();
await setupPolicyEngineWithJson({
"policies": {
"Extensions": {
"Uninstall": [
addonID,
],
"Install": [
`${BASE_URL}/policytest_v0.2.xpi`,
],
},
},
});
// Older version was uninstalled
await uninstallPromise;
// New version was installed
await installPromise;
let addon = await AddonManager.getAddonByID(addonID);
isnot(addon, null, "Addon still exists because the policy was used to update it.");
is(addon.version, "0.2", "New version is correct");
});
add_task(async function test_addon_uninstall() {
EnterprisePolicyTesting.resetRunOnceState();
let uninstallPromise = wait_for_addon_uninstall();
await setupPolicyEngineWithJson({
"policies": {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment