1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* eslint max-len: ["error", 80] */
"use strict";
/* exported attachUpdateHandler, detachUpdateHandler, gBrowser,
* getBrowserElement, installAddonsFromFilePicker,
* isCorrectlySigned, isDisabledUnsigned, isDiscoverEnabled,
* isPending, loadReleaseNotes, openOptionsInTab, promiseEvent,
* shouldShowPermissionsPrompt, showPermissionsPrompt,
* PREF_UI_LASTCATEGORY */
const { AddonSettings } = ChromeUtils.import(
"resource://gre/modules/addons/AddonSettings.jsm"
);
var { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const HTML_NS = "http://www.w3.org/1999/xhtml";
ChromeUtils.defineModuleGetter(
this,
"Extension",
"resource://gre/modules/Extension.jsm"
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
"XPINSTALL_ENABLED",
"xpinstall.enabled",
true
);
const PREF_DISCOVER_ENABLED = "extensions.getAddons.showPane";
const PREF_UI_LASTCATEGORY = "extensions.ui.lastCategory";
function isDiscoverEnabled() {
try {
if (!Services.prefs.getBoolPref(PREF_DISCOVER_ENABLED)) {
return false;
}
} catch (e) {}
if (!XPINSTALL_ENABLED) {
return false;
}
return true;
}
function getBrowserElement() {
return window.docShell.chromeEventHandler;
}
function promiseEvent(event, target, capture = false) {
return new Promise(resolve => {
target.addEventListener(event, resolve, { capture, once: true });
});
}
function installPromptHandler(info) {
const install = this;
let oldPerms = info.existingAddon.userPermissions;
if (!oldPerms) {
// Updating from a legacy add-on, let it proceed
return Promise.resolve();
}
let newPerms = info.addon.userPermissions;
let difference = Extension.comparePermissions(oldPerms, newPerms);
// If there are no new permissions, just proceed
if (!difference.origins.length && !difference.permissions.length) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
let subject = {
wrappedJSObject: {
target: getBrowserElement(),
info: {
type: "update",
addon: info.addon,
icon: info.addon.iconURL,
// Reference to the related AddonInstall object (used in
// AMTelemetry to link the recorded event to the other events from
// the same install flow).
install,
permissions: difference,
resolve,
reject,
},
},
};
Services.obs.notifyObservers(subject, "webextension-permission-prompt");
});
}
function attachUpdateHandler(install) {
install.promptHandler = installPromptHandler;
}
function detachUpdateHandler(install) {
if (install?.promptHandler === installPromptHandler) {
install.promptHandler = null;
}
}
async function loadReleaseNotes(uri) {
const res = await fetch(uri.spec, { credentials: "omit" });
if (!res.ok) {
throw new Error("Error loading release notes");
}
// Load the content.
const text = await res.text();
// Setup the content sanitizer.
const ParserUtils = Cc["@mozilla.org/parserutils;1"].getService(
Ci.nsIParserUtils
);
const flags =
ParserUtils.SanitizerDropMedia |
ParserUtils.SanitizerDropNonCSSPresentation |
ParserUtils.SanitizerDropForms;
// Sanitize and parse the content to a fragment.
const context = document.createElementNS(HTML_NS, "div");
return ParserUtils.parseFragment(text, flags, false, uri, context);
}
function openOptionsInTab(optionsURL) {
let mainWindow = window.windowRoot.ownerGlobal;
if ("switchToTabHavingURI" in mainWindow) {
mainWindow.switchToTabHavingURI(optionsURL, true, {
relatedToCurrent: true,
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
return true;
}
return false;
}
function shouldShowPermissionsPrompt(addon) {
if (!addon.isWebExtension || addon.seen) {
return false;
}
let perms = addon.userPermissions;
return perms?.origins.length || perms?.permissions.length;
}
function showPermissionsPrompt(addon) {
return new Promise(resolve => {
const permissions = addon.userPermissions;
const target = getBrowserElement();
const onAddonEnabled = () => {
// The user has just enabled a sideloaded extension, if the permission
// can be changed for the extension, show the post-install panel to
// give the user that opportunity.
if (
addon.permissions & AddonManager.PERM_CAN_CHANGE_PRIVATEBROWSING_ACCESS
) {
Services.obs.notifyObservers(
{ addon, target },
"webextension-install-notify"
);
}
resolve();
};
const subject = {
wrappedJSObject: {
target,
info: {
type: "sideload",
addon,
icon: addon.iconURL,
permissions,
resolve() {
addon.markAsSeen();
addon.enable().then(onAddonEnabled);
},
reject() {
// Ignore a cancelled permission prompt.
},
},
},
};
Services.obs.notifyObservers(subject, "webextension-permission-prompt");
});
}
// Stub tabbrowser implementation for use by the tab-modal alert code
// when an alert/prompt/confirm method is called in a WebExtensions options_ui
// page (See Bug 1385548 for rationale).
var gBrowser = {
getTabModalPromptBox(browser) {
const parentWindow = window.docShell.chromeEventHandler.ownerGlobal;
if (parentWindow.gBrowser) {
return parentWindow.gBrowser.getTabModalPromptBox(browser);
}
return null;
},
};
function isCorrectlySigned(addon) {
// Add-ons without an "isCorrectlySigned" property are correctly signed as
// they aren't the correct type for signing.
return addon.isCorrectlySigned !== false;
}
function isDisabledUnsigned(addon) {
let signingRequired =
addon.type == "locale"
? AddonSettings.LANGPACKS_REQUIRE_SIGNING
: AddonSettings.REQUIRE_SIGNING;
return signingRequired && !isCorrectlySigned(addon);
}
function isPending(addon, action) {
const amAction = AddonManager["PENDING_" + action.toUpperCase()];
return !!(addon.pendingOperations & amAction);
}
async function installAddonsFromFilePicker() {
let [dialogTitle, filterName] = await document.l10n.formatMessages([
{ id: "addon-install-from-file-dialog-title" },
{ id: "addon-install-from-file-filter-name" },
]);
const nsIFilePicker = Ci.nsIFilePicker;
var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, dialogTitle.value, nsIFilePicker.modeOpenMultiple);
try {
fp.appendFilter(filterName.value, "*.xpi;*.jar;*.zip");
fp.appendFilters(nsIFilePicker.filterAll);
} catch (e) {}
return new Promise(resolve => {
fp.open(async result => {
if (result != nsIFilePicker.returnOK) {
return;
}
let installTelemetryInfo = {
source: "about:addons",
method: "install-from-file",
};
let browser = getBrowserElement();
let installs = [];
for (let file of fp.files) {
let install = await AddonManager.getInstallForFile(
file,
null,
installTelemetryInfo
);
AddonManager.installAddonFromAOM(
browser,
document.documentURIObject,
install
);
installs.push(install);
}
resolve(installs);
});
});
}
|