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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/* 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] */
/* import-globals-from aboutaddonsCommon.js */
/* exported openAbuseReport */
/* global windowRoot */
/**
* This script is part of the HTML about:addons page and it provides some
* helpers used for the Abuse Reporting submission (and related message bars).
*/
const { AbuseReporter } = ChromeUtils.importESModule(
"resource://gre/modules/AbuseReporter.sys.mjs"
);
// Message Bars definitions.
const ABUSE_REPORT_MESSAGE_BARS = {
// Idle message-bar (used while the submission is still ongoing).
submitting: {
actions: ["cancel"],
l10n: {
id: "abuse-report-messagebar-submitting2",
actionIds: {
cancel: "abuse-report-messagebar-action-cancel",
},
},
},
// Submitted report message-bar.
submitted: {
actions: ["remove", "keep"],
dismissable: true,
l10n: {
id: "abuse-report-messagebar-submitted2",
actionIdsPerAddonType: {
extension: {
remove: "abuse-report-messagebar-action-remove-extension",
keep: "abuse-report-messagebar-action-keep-extension",
},
sitepermission: {
remove: "abuse-report-messagebar-action-remove-sitepermission",
keep: "abuse-report-messagebar-action-keep-sitepermission",
},
theme: {
remove: "abuse-report-messagebar-action-remove-theme",
keep: "abuse-report-messagebar-action-keep-theme",
},
},
},
},
// Submitted report message-bar (with no remove actions).
"submitted-no-remove-action": {
dismissable: true,
l10n: { id: "abuse-report-messagebar-submitted-noremove2" },
},
// Submitted report and remove addon message-bar.
"submitted-and-removed": {
dismissable: true,
l10n: {
idsPerAddonType: {
extension: "abuse-report-messagebar-removed-extension2",
sitepermission: "abuse-report-messagebar-removed-sitepermission2",
theme: "abuse-report-messagebar-removed-theme2",
},
},
},
// The "aborted report" message bar is rendered as a generic informative one,
// because aborting a report is triggered by a user choice.
ERROR_ABORTED_SUBMIT: {
type: "info",
dismissable: true,
l10n: { id: "abuse-report-messagebar-aborted2" },
},
// Errors message bars.
ERROR_ADDON_NOTFOUND: {
type: "error",
dismissable: true,
l10n: { id: "abuse-report-messagebar-error2" },
},
ERROR_CLIENT: {
type: "error",
dismissable: true,
l10n: { id: "abuse-report-messagebar-error2" },
},
ERROR_NETWORK: {
actions: ["retry", "cancel"],
type: "error",
l10n: {
id: "abuse-report-messagebar-error2",
actionIds: {
retry: "abuse-report-messagebar-action-retry",
cancel: "abuse-report-messagebar-action-cancel",
},
},
},
ERROR_RECENT_SUBMIT: {
actions: ["retry", "cancel"],
type: "error",
l10n: {
id: "abuse-report-messagebar-error-recent-submit2",
actionIds: {
retry: "abuse-report-messagebar-action-retry",
cancel: "abuse-report-messagebar-action-cancel",
},
},
},
ERROR_SERVER: {
actions: ["retry", "cancel"],
type: "error",
l10n: {
id: "abuse-report-messagebar-error2",
actionIds: {
retry: "abuse-report-messagebar-action-retry",
cancel: "abuse-report-messagebar-action-cancel",
},
},
},
ERROR_UNKNOWN: {
actions: ["retry", "cancel"],
type: "error",
l10n: {
id: "abuse-report-messagebar-error2",
actionIds: {
retry: "abuse-report-messagebar-action-retry",
cancel: "abuse-report-messagebar-action-cancel",
},
},
},
};
async function openAbuseReport({ addonId, reportEntryPoint }) {
try {
const reportDialog = await AbuseReporter.openDialog(
addonId,
reportEntryPoint,
window.docShell.chromeEventHandler
);
// Warn the user before the about:addons tab while an
// abuse report dialog is still open, and close the
// report dialog if the user choose to close the related
// about:addons tab.
const beforeunloadListener = evt => evt.preventDefault();
const unloadListener = () => reportDialog.close();
const clearUnloadListeners = () => {
window.removeEventListener("beforeunload", beforeunloadListener);
window.removeEventListener("unload", unloadListener);
};
window.addEventListener("beforeunload", beforeunloadListener);
window.addEventListener("unload", unloadListener);
reportDialog.promiseReport
.then(
report => {
if (report) {
submitReport({ report });
}
},
err => {
Cu.reportError(
`Unexpected abuse report panel error: ${err} :: ${err.stack}`
);
reportDialog.close();
}
)
.then(clearUnloadListeners);
} catch (err) {
// Log the detailed error to the browser console.
Cu.reportError(err);
document.dispatchEvent(
new CustomEvent("abuse-report:create-error", {
detail: {
addonId,
addon: err.addon,
errorType: err.errorType,
},
})
);
}
}
// Unlike the openAbuseReport function, technically this method wouldn't need
// to be async, but it is so that both the implementations will be providing
// the same type signatures (returning a promise) to the callers, independently
// from which abuse reporting feature is enabled.
async function openAbuseReportAMOForm({ addonId, reportEntryPoint }) {
const amoUrl = AbuseReporter.getAMOFormURL({ addonId });
windowRoot.ownerGlobal.openTrustedLinkIn(amoUrl, "tab", {
// Make sure the newly open tab is going to be focused, independently
// from general user prefs.
forceForeground: true,
});
}
window.openAbuseReport = AbuseReporter.amoFormEnabled
? openAbuseReportAMOForm
: openAbuseReport;
// Helper function used to create abuse report message bars in the
// HTML about:addons page.
function createReportMessageBar(
definitionId,
{ addonId, addonName, addonType },
{ onclose, onaction } = {}
) {
const barInfo = ABUSE_REPORT_MESSAGE_BARS[definitionId];
if (!barInfo) {
throw new Error(`message-bar definition not found: ${definitionId}`);
}
const { dismissable, actions, type, l10n } = barInfo;
// TODO(Bug 1789718): Remove after the deprecated XPIProvider-based
// implementation is also removed.
const mappingAddonType =
addonType === "sitepermission-deprecated" ? "sitepermission" : addonType;
const getMessageL10n = () => {
return l10n.idsPerAddonType
? l10n.idsPerAddonType[mappingAddonType]
: l10n.id;
};
const getActionL10n = action => {
return l10n.actionIdsPerAddonType
? l10n.actionIdsPerAddonType[mappingAddonType][action]
: l10n.actionIds[action];
};
const messagebar = document.createElement("moz-message-bar");
document.l10n.setAttributes(messagebar, getMessageL10n(), {
"addon-name": addonName || addonId,
});
messagebar.setAttribute("data-l10n-attrs", "message");
actions?.forEach(action => {
const buttonEl = document.createElement("button");
buttonEl.addEventListener("click", () => onaction && onaction(action));
document.l10n.setAttributes(buttonEl, getActionL10n(action));
buttonEl.setAttribute("slot", "actions");
messagebar.appendChild(buttonEl);
});
messagebar.setAttribute("type", type || "info");
messagebar.dismissable = dismissable;
messagebar.addEventListener("message-bar:close", onclose, { once: true });
document.getElementById("abuse-reports-messages").append(messagebar);
document.dispatchEvent(
new CustomEvent("abuse-report:new-message-bar", {
detail: { definitionId, messagebar },
})
);
return messagebar;
}
async function submitReport({ report }) {
const { addon } = report;
const addonId = addon.id;
const addonName = addon.name;
const addonType = addon.type;
// Ensure that the tab that originated the report dialog is selected
// when the user is submitting the report.
const { gBrowser } = window.windowRoot.ownerGlobal;
if (gBrowser && gBrowser.getTabForBrowser) {
let tab = gBrowser.getTabForBrowser(window.docShell.chromeEventHandler);
gBrowser.selectedTab = tab;
}
// Create a message bar while we are still submitting the report.
const mbSubmitting = createReportMessageBar(
"submitting",
{ addonId, addonName, addonType },
{
onaction: action => {
if (action === "cancel") {
report.abort();
mbSubmitting.remove();
}
},
}
);
try {
await report.submit();
mbSubmitting.remove();
// Create a submitted message bar when the submission has been
// successful.
let barId;
if (
!(addon.permissions & AddonManager.PERM_CAN_UNINSTALL) &&
!isPending(addon, "uninstall")
) {
// Do not offer remove action if the addon can't be uninstalled.
barId = "submitted-no-remove-action";
} else if (report.reportEntryPoint === "uninstall") {
// With reportEntryPoint "uninstall" a specific message bar
// is going to be used.
barId = "submitted-and-removed";
} else {
// All the other reportEntryPoint ("menu" and "toolbar_context_menu")
// use the same kind of message bar.
barId = "submitted";
}
const mbInfo = createReportMessageBar(
barId,
{
addonId,
addonName,
addonType,
},
{
onaction: action => {
mbInfo.remove();
// action "keep" doesn't require any further action,
// just handle "remove".
if (action === "remove") {
report.addon.uninstall(true);
}
},
}
);
} catch (err) {
// Log the complete error in the console.
console.error("Error submitting abuse report for", addonId, err);
mbSubmitting.remove();
// The report has a submission error, create a error message bar which
// may optionally allow the user to retry to submit the same report.
const barId =
err.errorType in ABUSE_REPORT_MESSAGE_BARS
? err.errorType
: "ERROR_UNKNOWN";
const mbError = createReportMessageBar(
barId,
{
addonId,
addonName,
addonType,
},
{
onaction: action => {
mbError.remove();
switch (action) {
case "retry":
submitReport({ report });
break;
case "cancel":
report.abort();
break;
}
},
}
);
}
}
document.addEventListener("abuse-report:submit", ({ detail }) => {
submitReport(detail);
});
document.addEventListener("abuse-report:create-error", ({ detail }) => {
const { addonId, addon, errorType } = detail;
const barId =
errorType in ABUSE_REPORT_MESSAGE_BARS ? errorType : "ERROR_UNKNOWN";
createReportMessageBar(barId, {
addonId,
addonName: addon && addon.name,
addonType: addon && addon.type,
});
});
|