summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/content/abuse-reports.js
blob: b5180734409e81042574839ca44c65bfa7df963b (plain)
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
/* 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 */

/**
 * 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.import(
  "resource://gre/modules/AbuseReporter.jsm"
);

// Message Bars definitions.
const ABUSE_REPORT_MESSAGE_BARS = {
  // Idle message-bar (used while the submission is still ongoing).
  submitting: { id: "submitting", actions: ["cancel"] },
  // Submitted report message-bar.
  submitted: {
    id: "submitted",
    actionAddonTypeSuffix: true,
    actions: ["remove", "keep"],
    dismissable: true,
  },
  // Submitted report message-bar (with no remove actions).
  "submitted-no-remove-action": {
    id: "submitted-noremove",
    dismissable: true,
  },
  // Submitted report and remove addon message-bar.
  "submitted-and-removed": {
    id: "removed",
    addonTypeSuffix: true,
    dismissable: true,
  },
  // 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: {
    id: "aborted",
    type: "generic",
    dismissable: true,
  },
  // Errors message bars.
  ERROR_ADDON_NOTFOUND: {
    id: "error",
    type: "error",
    dismissable: true,
  },
  ERROR_CLIENT: {
    id: "error",
    type: "error",
    dismissable: true,
  },
  ERROR_NETWORK: {
    id: "error",
    actions: ["retry", "cancel"],
    type: "error",
  },
  ERROR_RECENT_SUBMIT: {
    id: "error-recent-submit",
    actions: ["retry", "cancel"],
    type: "error",
  },
  ERROR_SERVER: {
    id: "error",
    actions: ["retry", "cancel"],
    type: "error",
  },
  ERROR_UNKNOWN: {
    id: "error",
    actions: ["retry", "cancel"],
    type: "error",
  },
};

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,
        },
      })
    );
  }
}

window.openAbuseReport = 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 getMessageL10n = id => `abuse-report-messagebar-${id}`;
  const getActionL10n = action => getMessageL10n(`action-${action}`);

  const barInfo = ABUSE_REPORT_MESSAGE_BARS[definitionId];
  if (!barInfo) {
    throw new Error(`message-bar definition not found: ${definitionId}`);
  }
  const { id, dismissable, actions, type } = barInfo;
  const messageEl = document.createElement("span");

  // The message element includes an addon-name span (also filled by
  // Fluent), which can be used to apply custom styles to the addon name
  // included in the message bar (if needed).
  const addonNameEl = document.createElement("span");
  addonNameEl.setAttribute("data-l10n-name", "addon-name");
  messageEl.append(addonNameEl);

  // TODO(Bug 1789718): Remove after the deprecated XPIProvider-based
  // implementation is also removed.
  const mappingAddonType =
    addonType === "sitepermission-deprecated" ? "sitepermission" : addonType;

  document.l10n.setAttributes(
    messageEl,
    getMessageL10n(barInfo.addonTypeSuffix ? `${id}-${mappingAddonType}` : id),
    { "addon-name": addonName || addonId }
  );

  const barActions = actions
    ? actions.map(action => {
        // Some of the message bars require a different per addonType
        // Fluent id for their actions.
        const actionId = barInfo.actionAddonTypeSuffix
          ? `${action}-${mappingAddonType}`
          : action;
        const buttonEl = document.createElement("button");
        buttonEl.addEventListener("click", () => onaction && onaction(action));
        document.l10n.setAttributes(buttonEl, getActionL10n(actionId));
        return buttonEl;
      })
    : [];

  const messagebar = document.createElement("message-bar");
  messagebar.setAttribute("type", type || "generic");
  if (dismissable) {
    messagebar.setAttribute("dismissable", "");
  }
  messagebar.append(messageEl, ...barActions);
  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,
  });
});