Bug 1330823 Part 1 refactor some existing permissions code
This patch is just cleanups that don't alter any current
behavior but make the changes for updates in part 2 simpler.
MozReview-Commit-ID: 18QEx7RxWxH
--- a/browser/modules/ExtensionsUI.jsm
+++ b/browser/modules/ExtensionsUI.jsm
@@ -123,18 +123,21 @@ this.ExtensionsUI = {
// there are multiple simultaneous installs happening, see
// bug 1329884 for a longer explanation.
let progressNotification = target.ownerGlobal.PopupNotifications.getNotification("addon-progress", target);
if (progressNotification) {
progressNotification.remove();
}
let reply = answer => {
- Services.obs.notifyObservers(subject, "webextension-permission-response",
- JSON.stringify(answer));
+ if (answer) {
+ info.resolve();
+ } else {
+ info.reject();
+ }
};
let perms = info.addon.userPermissions;
if (!perms) {
reply(true);
} else {
info.permissions = perms;
this.showPermissionsPrompt(target, info).then(reply);
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -384,16 +384,30 @@ this.ExtensionData = class {
}
}
const EXP_PATTERN = /^experiments\.\w+/;
result.permissions = [...this.permissions]
.filter(p => !result.hosts.includes(p) && !EXP_PATTERN.test(p));
return result;
}
+ // Compute the difference between two sets of permissions, suitable
+ // for presenting to the user.
+ static comparePermissions(oldPermissions, newPermissions) {
+ // See bug 1331769: should we do something more complicated to
+ // compare host permissions?
+ // e.g., if we go from <all_urls> to a specific host or from
+ // a *.domain.com to specific-host.domain.com that's actually a
+ // drop in permissions but the simple test below will cause a prompt.
+ return {
+ hosts: newPermissions.hosts.filter(perm => !oldPermissions.hosts.includes(perm)),
+ permissions: newPermissions.permissions.filter(perm => !oldPermissions.permissions.includes(perm)),
+ };
+ }
+
// Reads the extension's |manifest.json| file, and stores its
// parsed contents in |this.manifest|.
readManifest() {
return Promise.all([
this.readJSON("manifest.json"),
Management.lazyInit(),
]).then(([manifest]) => {
this.manifest = manifest;
--- a/toolkit/mozapps/extensions/AddonManager.jsm
+++ b/toolkit/mozapps/extensions/AddonManager.jsm
@@ -1375,25 +1375,17 @@ var AddonManagerInternal = {
// escape() does not properly encode + symbols in any embedded FVF strings.
return uri.replace(/\+/g, "%2B");
},
_updatePromptHandler(info) {
let oldPerms = info.existingAddon.userPermissions || {hosts: [], permissions: []};
let newPerms = info.addon.userPermissions;
- // See bug 1331769: should we do something more complicated to
- // compare host permissions?
- // e.g., if we go from <all_urls> to a specific host or from
- // a *.domain.com to specific-host.domain.com that's actually a
- // drop in permissions but the simple test below will cause a prompt.
- let difference = {
- hosts: newPerms.hosts.filter(perm => !oldPerms.hosts.includes(perm)),
- permissions: newPerms.permissions.filter(perm => !oldPerms.permissions.includes(perm)),
- };
+ let difference = Extension.comparePermissions(oldPerms, newPerms);
// If there are no new permissions, just go ahead with the update
if (difference.hosts.length == 0 && difference.permissions.length == 0) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
let subject = {wrappedJSObject: {
@@ -2842,34 +2834,22 @@ var AddonManagerInternal = {
// is false).
// In the remaining cases, a confirmation prompt is displayed but the
// application may override it either by implementing the
// "@mozilla.org/addons/web-install-prompt;1" contract or by setting
// the customConfirmationUI preference and responding to the
// "addon-install-confirmation" notification. If the application
// does not implement its own prompt, use the built-in xul dialog.
if (info.addon.userPermissions && WEBEXT_PERMISSION_PROMPTS) {
- const observer = {
- observe(subject, topic, data) {
- if (topic == "webextension-permission-response"
- && subject.wrappedJSObject.info.addon == info.addon) {
- let answer = JSON.parse(data);
- Services.obs.removeObserver(observer, "webextension-permission-response");
- if (answer) {
- resolve();
- } else {
- reject();
- }
- }
+ let subject = {
+ wrappedJSObject: {
+ target: browser,
+ info: Object.assign({resolve, reject}, info),
}
};
-
- Services.obs.addObserver(observer, "webextension-permission-response", false);
-
- let subject = {wrappedJSObject: {target: browser, info}};
Services.obs.notifyObservers(subject, "webextension-permission-prompt", null);
} else if (requireConfirm) {
// The methods below all want to call the install() or cancel()
// method on the provided AddonInstall object to either accept
// or reject the confirmation. Fit that into our promise-based
// control flow by wrapping the install object. However,
// xpInstallConfirm.xul matches the install object it is passed
// with the argument passed to an InstallListener, so give it