summaryrefslogtreecommitdiffstats
path: root/browser/components/protections/content/monitor-card.mjs
blob: ef693a761e0235c9d353cb5d77303eb5b1e75a3f (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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* 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-env mozilla/remote-page */

const MONITOR_URL = RPMGetStringPref(
  "browser.contentblocking.report.monitor.url",
  ""
);
const MONITOR_SIGN_IN_URL = RPMGetStringPref(
  "browser.contentblocking.report.monitor.sign_in_url",
  ""
);
const HOW_IT_WORKS_URL_PREF = RPMGetFormatURLPref(
  "browser.contentblocking.report.monitor.how_it_works.url"
);
const MONITOR_PREFERENCES_URL = RPMGetFormatURLPref(
  "browser.contentblocking.report.monitor.preferences_url"
);
const MONITOR_HOME_PAGE_URL = RPMGetFormatURLPref(
  "browser.contentblocking.report.monitor.home_page_url"
);

export default class MonitorClass {
  constructor(doc) {
    this.doc = doc;
  }

  init() {
    // Wait for monitor data and display the card.
    this.getMonitorData();

    let monitorAboutLink = this.doc.getElementById("monitor-link");
    monitorAboutLink.addEventListener("click", () => {
      this.doc.sendTelemetryEvent("click", "mtr_about_link");
    });

    const storedEmailLink = this.doc.getElementById(
      "monitor-stored-emails-link"
    );
    storedEmailLink.href = MONITOR_PREFERENCES_URL;
    storedEmailLink.addEventListener(
      "click",
      this.onClickMonitorButton.bind(this)
    );

    const knownBreachesLink = this.doc.getElementById(
      "monitor-known-breaches-link"
    );
    knownBreachesLink.href = MONITOR_HOME_PAGE_URL;
    knownBreachesLink.addEventListener(
      "click",
      this.onClickMonitorButton.bind(this)
    );

    const exposedPasswordsLink = this.doc.getElementById(
      "monitor-exposed-passwords-link"
    );
    exposedPasswordsLink.href = MONITOR_HOME_PAGE_URL;
    exposedPasswordsLink.addEventListener(
      "click",
      this.onClickMonitorButton.bind(this)
    );
  }

  onClickMonitorButton(evt) {
    RPMSendAsyncMessage("ClearMonitorCache");
    switch (evt.currentTarget.id) {
      case "monitor-partial-breaches-link":
        this.doc.sendTelemetryEvent(
          "click",
          "mtr_report_link",
          "resolve_breaches"
        );
        break;
      case "monitor-breaches-link":
        if (evt.currentTarget.classList.contains("no-breaches-resolved")) {
          this.doc.sendTelemetryEvent(
            "click",
            "mtr_report_link",
            "manage_breaches"
          );
        } else {
          this.doc.sendTelemetryEvent(
            "click",
            "mtr_report_link",
            "view_report"
          );
        }
        break;
      case "monitor-stored-emails-link":
        this.doc.sendTelemetryEvent(
          "click",
          "mtr_report_link",
          "stored_emails"
        );
        break;
      case "monitor-known-breaches-link":
        const knownBreaches = this.doc.querySelector(
          "span[data-type='known-breaches']"
        );
        if (knownBreaches.classList.contains("known-resolved-breaches")) {
          this.doc.sendTelemetryEvent(
            "click",
            "mtr_report_link",
            "known_resolved_breaches"
          );
        } else if (
          knownBreaches.classList.contains("known-unresolved-breaches")
        ) {
          this.doc.sendTelemetryEvent(
            "click",
            "mtr_report_link",
            "known_unresolved_breaches"
          );
        }
        break;
      case "monitor-exposed-passwords-link":
        const exposedPasswords = this.doc.querySelector(
          "span[data-type='exposed-passwords']"
        );
        if (
          exposedPasswords.classList.contains("passwords-exposed-all-breaches")
        ) {
          this.doc.sendTelemetryEvent(
            "click",
            "mtr_report_link",
            "exposed_passwords_all_breaches"
          );
        } else if (
          exposedPasswords.classList.contains(
            "passwords-exposed-unresolved-breaches"
          )
        ) {
          this.doc.sendTelemetryEvent(
            "click",
            "mtr_report_link",
            "exposed_passwords_unresolved_breaches"
          );
        }
        break;
    }
  }

  /**
   * Retrieves the monitor data and displays this data in the card.
   */
  getMonitorData() {
    RPMSendQuery("FetchMonitorData", {}).then(monitorData => {
      // Once data for the user is retrieved, display the monitor card.
      this.buildContent(monitorData);

      // Show the Monitor card.
      const monitorUI = this.doc.querySelector(".card.monitor-card.loading");
      monitorUI.classList.remove("loading");
    });
  }

  buildContent(monitorData) {
    const headerContent = this.doc.querySelector(
      "#monitor-header-content span"
    );
    const monitorCard = this.doc.querySelector(".card.monitor-card");
    if (!monitorData.error) {
      monitorCard.classList.add("has-logins");
      this.doc.l10n.setAttributes(
        headerContent,
        "monitor-header-content-signed-in"
      );
      this.renderContentForUserWithAccount(monitorData);
    } else {
      monitorCard.classList.add("no-logins");
      const signUpForMonitorLink = this.doc.getElementById(
        "sign-up-for-monitor-link"
      );
      signUpForMonitorLink.href = this.buildMonitorUrl(monitorData.userEmail);
      this.doc.l10n.setAttributes(signUpForMonitorLink, "monitor-sign-up-link");
      this.doc.l10n.setAttributes(
        headerContent,
        "monitor-header-content-no-account"
      );
      signUpForMonitorLink.addEventListener("click", () => {
        this.doc.sendTelemetryEvent("click", "mtr_signup_button");
      });
    }
  }

  /**
   * Builds the appropriate URL that takes the user to the Monitor website's
   * sign-up/sign-in page.
   *
   * @param {string | null} email
   *        Optional. The email used to direct the user to the Monitor website's OAuth
   *        sign-in flow. If null, then direct user to just the Monitor website.
   *
   * @returns {string} URL to Monitor website.
   */
  buildMonitorUrl(email = null) {
    return email
      ? `${MONITOR_SIGN_IN_URL}${encodeURIComponent(email)}`
      : MONITOR_URL;
  }

  renderContentForUserWithAccount(monitorData) {
    const {
      numBreaches,
      numBreachesResolved,
      passwords,
      passwordsResolved,
      monitoredEmails,
    } = monitorData;
    const monitorCardBody = this.doc.querySelector(
      ".card.monitor-card .card-body"
    );
    monitorCardBody.classList.remove("hidden");

    const howItWorksLink = this.doc.getElementById("monitor-link");
    howItWorksLink.href = HOW_IT_WORKS_URL_PREF;

    const storedEmail = this.doc.querySelector(
      "span[data-type='stored-emails']"
    );
    storedEmail.textContent = monitoredEmails;
    const infoMonitoredAddresses = this.doc.getElementById(
      "info-monitored-addresses"
    );
    this.doc.l10n.setAttributes(
      infoMonitoredAddresses,
      "info-monitored-emails",
      { count: monitoredEmails }
    );

    const knownBreaches = this.doc.querySelector(
      "span[data-type='known-breaches']"
    );
    const exposedPasswords = this.doc.querySelector(
      "span[data-type='exposed-passwords']"
    );

    const infoKnownBreaches = this.doc.getElementById("info-known-breaches");
    const infoExposedPasswords = this.doc.getElementById(
      "info-exposed-passwords"
    );

    const breachesWrapper = this.doc.querySelector(".monitor-breaches-wrapper");
    const partialBreachesWrapper = this.doc.querySelector(
      ".monitor-partial-breaches-wrapper"
    );
    const breachesTitle = this.doc.getElementById("monitor-breaches-title");
    const breachesIcon = this.doc.getElementById("monitor-breaches-icon");
    const breachesDesc = this.doc.getElementById(
      "monitor-breaches-description"
    );
    const breachesLink = this.doc.getElementById("monitor-breaches-link");
    if (numBreaches) {
      if (!numBreachesResolved) {
        partialBreachesWrapper.classList.add("hidden");
        knownBreaches.textContent = numBreaches;
        knownBreaches.classList.add("known-unresolved-breaches");
        knownBreaches.classList.remove("known-resolved-breaches");
        this.doc.l10n.setAttributes(
          infoKnownBreaches,
          "info-known-breaches-found",
          { count: numBreaches }
        );
        exposedPasswords.textContent = passwords;
        exposedPasswords.classList.add("passwords-exposed-all-breaches");
        exposedPasswords.classList.remove(
          "passwords-exposed-unresolved-breaches"
        );
        this.doc.l10n.setAttributes(
          infoExposedPasswords,
          "info-exposed-passwords-found",
          { count: passwords }
        );

        breachesIcon.setAttribute(
          "src",
          "chrome://browser/skin/protections/new-feature.svg"
        );
        this.doc.l10n.setAttributes(
          breachesTitle,
          "monitor-breaches-unresolved-title"
        );
        this.doc.l10n.setAttributes(
          breachesDesc,
          "monitor-breaches-unresolved-description"
        );
        this.doc.l10n.setAttributes(
          breachesLink,
          "monitor-manage-breaches-link"
        );
        breachesLink.classList.add("no-breaches-resolved");
      } else if (numBreaches == numBreachesResolved) {
        partialBreachesWrapper.classList.add("hidden");
        knownBreaches.textContent = numBreachesResolved;
        knownBreaches.classList.remove("known-unresolved-breaches");
        knownBreaches.classList.add("known-resolved-breaches");
        this.doc.l10n.setAttributes(
          infoKnownBreaches,
          "info-known-breaches-resolved",
          { count: numBreachesResolved }
        );
        let unresolvedPasswords = passwords - passwordsResolved;
        exposedPasswords.textContent = unresolvedPasswords;
        exposedPasswords.classList.remove("passwords-exposed-all-breaches");
        exposedPasswords.classList.add("passwords-exposed-unresolved-breaches");
        this.doc.l10n.setAttributes(
          infoExposedPasswords,
          "info-exposed-passwords-resolved",
          { count: unresolvedPasswords }
        );

        breachesIcon.setAttribute(
          "src",
          "chrome://browser/skin/protections/resolved-breach.svg"
        );
        this.doc.l10n.setAttributes(
          breachesTitle,
          "monitor-breaches-resolved-title"
        );
        this.doc.l10n.setAttributes(
          breachesDesc,
          "monitor-breaches-resolved-description"
        );
        this.doc.l10n.setAttributes(breachesLink, "monitor-view-report-link");
      } else {
        breachesWrapper.classList.add("hidden");
        knownBreaches.textContent = numBreachesResolved;
        knownBreaches.classList.remove("known-unresolved-breaches");
        knownBreaches.classList.add("known-resolved-breaches");
        this.doc.l10n.setAttributes(
          infoKnownBreaches,
          "info-known-breaches-resolved",
          { count: numBreachesResolved }
        );
        let unresolvedPasswords = passwords - passwordsResolved;
        exposedPasswords.textContent = unresolvedPasswords;
        exposedPasswords.classList.remove("passwords-exposed-all-breaches");
        exposedPasswords.classList.add("passwords-exposed-unresolved-breaches");
        this.doc.l10n.setAttributes(
          infoExposedPasswords,
          "info-exposed-passwords-resolved",
          { count: unresolvedPasswords }
        );

        const partialBreachesTitle = document.getElementById(
          "monitor-partial-breaches-title"
        );
        this.doc.l10n.setAttributes(
          partialBreachesTitle,
          "monitor-partial-breaches-title",
          {
            numBreaches,
            numBreachesResolved,
          }
        );

        const progressBar = this.doc.querySelector(".progress-bar");
        const partialBreachesMotivationTitle = document.getElementById(
          "monitor-partial-breaches-motivation-title"
        );

        let percentageResolved = Math.floor(
          (numBreachesResolved / numBreaches) * 100
        );
        progressBar.setAttribute("value", 100 - percentageResolved);
        switch (true) {
          case percentageResolved > 0 && percentageResolved < 25:
            this.doc.l10n.setAttributes(
              partialBreachesMotivationTitle,
              "monitor-partial-breaches-motivation-title-start"
            );
            break;

          case percentageResolved >= 25 && percentageResolved < 75:
            this.doc.l10n.setAttributes(
              partialBreachesMotivationTitle,
              "monitor-partial-breaches-motivation-title-middle"
            );
            break;

          case percentageResolved >= 75 && percentageResolved < 100:
            this.doc.l10n.setAttributes(
              partialBreachesMotivationTitle,
              "monitor-partial-breaches-motivation-title-end"
            );
            break;
        }

        const partialBreachesPercentage = document.getElementById(
          "monitor-partial-breaches-percentage"
        );
        this.doc.l10n.setAttributes(
          partialBreachesPercentage,
          "monitor-partial-breaches-percentage",
          { percentageResolved }
        );

        const partialBreachesLink = document.getElementById(
          "monitor-partial-breaches-link"
        );
        partialBreachesLink.setAttribute("href", MONITOR_HOME_PAGE_URL);
        partialBreachesLink.addEventListener(
          "click",
          this.onClickMonitorButton.bind(this)
        );
      }
    } else {
      partialBreachesWrapper.classList.add("hidden");
      knownBreaches.textContent = numBreaches;
      knownBreaches.classList.add("known-unresolved-breaches");
      knownBreaches.classList.remove("known-resolved-breaches");
      this.doc.l10n.setAttributes(
        infoKnownBreaches,
        "info-known-breaches-found",
        { count: numBreaches }
      );
      exposedPasswords.textContent = passwords;
      exposedPasswords.classList.add("passwords-exposed-all-breaches");
      exposedPasswords.classList.remove(
        "passwords-exposed-unresolved-breaches"
      );
      this.doc.l10n.setAttributes(
        infoExposedPasswords,
        "info-exposed-passwords-found",
        { count: passwords }
      );

      breachesIcon.setAttribute(
        "src",
        "chrome://browser/skin/protections/resolved-breach.svg"
      );
      this.doc.l10n.setAttributes(breachesTitle, "monitor-no-breaches-title");
      this.doc.l10n.setAttributes(
        breachesDesc,
        "monitor-no-breaches-description"
      );
      this.doc.l10n.setAttributes(breachesLink, "monitor-view-report-link");
    }

    breachesLink.setAttribute("href", MONITOR_HOME_PAGE_URL);
    breachesLink.addEventListener(
      "click",
      this.onClickMonitorButton.bind(this)
    );
  }
}