Bug 1528978 - Use content pref service to persist warned hosts. r=johannh
authorNihanth Subramanya <nhnt11@gmail.com>
Sat, 27 Apr 2019 21:40:02 +0000
changeset 530769 d42051b63b49d203bfa0abb1e20818d65731c000
parent 530768 19bf83121d3138bfd7718e93ce8b05e478b079b7
child 530770 cb0bbf72229c206ca96d63259047942b0467762a
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1528978
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1528978 - Use content pref service to persist warned hosts. r=johannh Differential Revision: https://phabricator.services.mozilla.com/D21552
browser/extensions/fxmonitor/privileged/api.js
--- a/browser/extensions/fxmonitor/privileged/api.js
+++ b/browser/extensions/fxmonitor/privileged/api.js
@@ -36,23 +36,16 @@ this.fxmonitor = class extends Extension
     FirefoxMonitor.stopObserving();
   }
 };
 
 this.FirefoxMonitor = {
   // Map of breached site host -> breach metadata.
   domainMap: new Map(),
 
-  // Set of hosts for which the user has already been shown,
-  // and interacted with, the popup.
-  warnedHostsSet: new Set(),
-
-  // The above set is persisted as a JSON string in this pref.
-  kWarnedHostsPref: "extensions.fxmonitor.warnedHosts",
-
   // Reference to the extension object from the WebExtension context.
   // Used for getting URIs for resources packaged in the extension.
   extension: null,
 
   // Whether we've started observing for the user visiting a breached site.
   observerAdded: false,
 
   // This is here for documentation, will be redefined to a lazy getter
@@ -126,16 +119,20 @@ this.FirefoxMonitor = {
   _delayedInited: false,
   async delayedInit() {
     if (this._delayedInited) {
       return;
     }
 
     this._delayedInited = true;
 
+    XPCOMUtils.defineLazyServiceGetter(this, "_contentPrefService",
+      "@mozilla.org/content-pref/service;1",
+      "nsIContentPrefService2");
+
     // Expire our telemetry on November 1, at which time
     // we should redo data-review.
     let telemetryExpiryDate = new Date(2019, 10, 1); // Month is zero-index
     let today = new Date();
     let expired = today.getTime() > telemetryExpiryDate.getTime();
 
     Services.telemetry.registerEvents("fxmonitor", {
       "interaction": {
@@ -150,27 +147,16 @@ this.FirefoxMonitor = {
         record_on_release: true,
         expired,
       },
     });
 
     let telemetryEnabled = !Preferences.get(this.kTelemetryDisabledPref);
     Services.telemetry.setEventRecordingEnabled("fxmonitor", telemetryEnabled);
 
-    let warnedHostsJSON = Preferences.get(this.kWarnedHostsPref, "");
-    if (warnedHostsJSON) {
-      try {
-        let json = JSON.parse(warnedHostsJSON);
-        this.warnedHostsSet = new Set(json);
-      } catch (ex) {
-        // Invalid JSON, invalidate the pref.
-        Preferences.reset(this.kWarnedHostsPref);
-      }
-    }
-
     XPCOMUtils.defineLazyPreferenceGetter(this, "FirefoxMonitorURL",
       this.kFirefoxMonitorURLPref, this.kDefaultFirefoxMonitorURL);
 
     XPCOMUtils.defineLazyPreferenceGetter(this, "firstAlertShown",
       this.kFirstAlertShownPref, false);
 
     await this.loadStrings();
     await this.loadBreaches();
@@ -350,18 +336,39 @@ this.FirefoxMonitor = {
       return;
     }
 
     EveryWindow.unregisterCallback(this.kNotificationID);
 
     this.observerAdded = false;
   },
 
-  warnIfNeeded(browser, host) {
-    if (!this.enabled || this.warnedHostsSet.has(host) || !this.domainMap.has(host)) {
+  async hostAlreadyWarned(browser, host) {
+    return new Promise((resolve, reject) => {
+      this._contentPrefService.getByDomainAndName(
+        host,
+        "extensions.fxmonitor.hostAlreadyWarned",
+        browser.loadContext,
+        {
+          handleCompletion: () => resolve(false),
+          handleResult: (result) => resolve(result.value),
+        });
+    });
+  },
+
+  rememberWarnedHost(browser, host) {
+    this._contentPrefService.set(
+      host,
+      "extensions.fxmonitor.hostAlreadyWarned",
+      true,
+      browser.loadContext);
+  },
+
+  async warnIfNeeded(browser, host) {
+    if (!this.enabled || await this.hostAlreadyWarned(browser, host) || !this.domainMap.has(host)) {
       return;
     }
 
     let site = this.domainMap.get(host);
 
     // We only alert for breaches that were found up to 2 months ago,
     // except for the very first alert we show the user - in which case,
     // we include breaches found in the last three years.
@@ -373,18 +380,17 @@ this.FirefoxMonitor = {
     }
 
     if (new Date(site.AddedDate).getTime() < breachDateThreshold.getTime()) {
       return;
     } else if (!this.firstAlertShown) {
       Preferences.set(this.kFirstAlertShownPref, true);
     }
 
-    this.warnedHostsSet.add(host);
-    Preferences.set(this.kWarnedHostsPref, JSON.stringify([...this.warnedHostsSet]));
+    this.rememberWarnedHost(browser, host);
 
     let doc = browser.ownerDocument;
     let win = doc.defaultView;
     let panelUI = this.panelUIsByWindow.get(win);
     if (!panelUI) {
       panelUI = this.setupPanelUI(win);
     }