Bug 1513490 - Refactor SafeBrowsing.jsm with feature list. r=gcp
authorDimi Lee <dlee@mozilla.com>
Tue, 15 Jan 2019 20:04:40 +0000
changeset 513956 33011122d2d517bdb61ca149e2f0ce20dae528a2
parent 513955 408372b773e38f1f08410e1b848eabb50232d87e
child 513957 d29c64bd30daa39854747f41bff56d7fb51ca3d3
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1513490
milestone66.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 1513490 - Refactor SafeBrowsing.jsm with feature list. r=gcp When we add a table to SafeBrowsing.jsm we need to add related code in various places. This patch simplify the work by providing a FEATURE table which defines the data required. Differential Revision: https://phabricator.services.mozilla.com/D16532
toolkit/components/url-classifier/SafeBrowsing.jsm
--- a/toolkit/components/url-classifier/SafeBrowsing.jsm
+++ b/toolkit/components/url-classifier/SafeBrowsing.jsm
@@ -27,37 +27,81 @@ function getLists(prefName) {
   // Splitting an empty string returns [''], we really want an empty array.
   if (!pref) {
     return [];
   }
 
   return pref.split(",").map(value => value.trim());
 }
 
-const tablePreferences = [
-  "urlclassifier.phishTable",
-  "urlclassifier.malwareTable",
-  "urlclassifier.downloadBlockTable",
-  "urlclassifier.downloadAllowTable",
-  "urlclassifier.passwordAllowTable",
-  "urlclassifier.trackingAnnotationTable",
-  "urlclassifier.trackingAnnotationWhitelistTable",
-  "urlclassifier.trackingTable",
-  "urlclassifier.trackingWhitelistTable",
-  "urlclassifier.blockedTable",
-  "urlclassifier.flashAllowTable",
-  "urlclassifier.flashAllowExceptTable",
-  "urlclassifier.flashTable",
-  "urlclassifier.flashExceptTable",
-  "urlclassifier.flashSubDocTable",
-  "urlclassifier.flashSubDocExceptTable",
+const FEATURES = [
+  { name: "phishing",
+    list: ["urlclassifier.phishTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("browser.safebrowsing.phishing.enabled");
+    },
+  },
+  { name: "malware",
+    list: ["urlclassifier.malwareTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled");
+    },
+  },
+  { name: "blockedURIs",
+    list: ["urlclassifier.blockedTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("browser.safebrowsing.blockedURIs.enabled");
+    },
+  },
+  { name: "passwords",
+    list: ["urlclassifier.passwordAllowTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("browser.safebrowsing.passwords.enabled");
+    },
+  },
+  { name: "downloads",
+    list: ["urlclassifier.downloadBlockTable",
+           "urlclassifier.downloadAllowTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("browser.safebrowsing.downloads.enabled") &&
+             Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled");
+    },
+  },
+  { name: "trackingAnnotation",
+    list: ["urlclassifier.trackingAnnotationTable",
+           "urlclassifier.trackingAnnotationWhitelistTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("privacy.trackingprotection.annotate_channels");
+    },
+  },
+  { name: "tracking",
+    list: ["urlclassifier.trackingTable",
+           "urlclassifier.trackingWhitelistTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("toolkit.telemetry.isGeckoViewMode", false) ||
+             Services.prefs.getBoolPref("privacy.trackingprotection.enabled") ||
+             Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled");
+    },
+  },
+  { name: "flashBlock",
+    list: ["urlclassifier.flashAllowTable",
+           "urlclassifier.flashAllowExceptTable",
+           "urlclassifier.flashTable",
+           "urlclassifier.flashExceptTable",
+           "urlclassifier.flashSubDocTable",
+           "urlclassifier.flashSubDocExceptTable"],
+    enabled() {
+      return Services.prefs.getBoolPref("plugins.flashBlock.enabled");
+    },
+  },
 ];
 
 var SafeBrowsing = {
 
+
   init() {
     if (this.initialized) {
       log("Already initialized");
       return;
     }
 
     Services.prefs.addObserver("browser.safebrowsing", this);
     Services.prefs.addObserver("privacy.trackingprotection", this);
@@ -90,88 +134,41 @@ var SafeBrowsing = {
       log("Invalid update url " + listname);
       return;
     }
 
     listManager.registerTable(listname, providerName, provider.updateURL, provider.gethashURL);
   },
 
   registerTables() {
-    for (let i = 0; i < this.phishingLists.length; ++i) {
-      this.registerTableWithURLs(this.phishingLists[i]);
-    }
-    for (let i = 0; i < this.malwareLists.length; ++i) {
-      this.registerTableWithURLs(this.malwareLists[i]);
-    }
-    for (let i = 0; i < this.downloadBlockLists.length; ++i) {
-      this.registerTableWithURLs(this.downloadBlockLists[i]);
-    }
-    for (let i = 0; i < this.downloadAllowLists.length; ++i) {
-      this.registerTableWithURLs(this.downloadAllowLists[i]);
-    }
-    for (let i = 0; i < this.passwordAllowLists.length; ++i) {
-      this.registerTableWithURLs(this.passwordAllowLists[i]);
-    }
-    for (let i = 0; i < this.trackingAnnotationLists.length; ++i) {
-      this.registerTableWithURLs(this.trackingAnnotationLists[i]);
-    }
-    for (let i = 0; i < this.trackingAnnotationWhitelists.length; ++i) {
-      this.registerTableWithURLs(this.trackingAnnotationWhitelists[i]);
-    }
-    for (let i = 0; i < this.trackingProtectionLists.length; ++i) {
-      this.registerTableWithURLs(this.trackingProtectionLists[i]);
-    }
-    for (let i = 0; i < this.trackingProtectionWhitelists.length; ++i) {
-      this.registerTableWithURLs(this.trackingProtectionWhitelists[i]);
-    }
-    for (let i = 0; i < this.blockedLists.length; ++i) {
-      this.registerTableWithURLs(this.blockedLists[i]);
-    }
-    for (let i = 0; i < this.flashLists.length; ++i) {
-      this.registerTableWithURLs(this.flashLists[i]);
-    }
+    this.features.forEach(feature => {
+      feature.list.forEach(table => {
+        this.registerTableWithURLs(table);
+      });
+    });
   },
 
   unregisterTables(obsoleteLists) {
     let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"].
       getService(Ci.nsIUrlListManager);
 
-    for (let i = 0; i < obsoleteLists.length; ++i) {
-      for (let j = 0; j < obsoleteLists[i].length; ++j) {
-        listManager.unregisterTable(obsoleteLists[i][j]);
-      }
-    }
+    obsoleteLists.forEach(list => {
+      list.forEach(table => {
+        listManager.unregisterTable(table);
+      });
+    });
   },
 
 
   initialized:          false,
-  phishingEnabled:      false,
-  malwareEnabled:       false,
-  downloadsEnabled:     false,
-  passwordsEnabled:     false,
-  trackingEnabled:      false,
-  blockedEnabled:       false,
-  trackingAnnotations:  false,
-  flashBlockEnabled:    false,
 
-  phishingLists:                [],
-  malwareLists:                 [],
-  downloadBlockLists:           [],
-  downloadAllowLists:           [],
-  passwordAllowLists:           [],
-  trackingAnnotationLists:      [],
-  trackingAnnotationWhiteLists: [],
-  trackingProtectionLists:      [],
-  trackingProtectionWhitelists: [],
-  blockedLists:                 [],
-  flashLists:                   [],
+  features:                [],
 
   updateURL:             null,
   gethashURL:            null,
-
   reportURL:             null,
 
   getReportURL(kind, info) {
     let pref;
     switch (kind) {
       case "Phish":
         pref = "browser.safebrowsing.reportPhishURL";
         break;
@@ -218,87 +215,38 @@ var SafeBrowsing = {
 
     this.readPrefs();
   },
 
   readPrefs() {
     loggingEnabled = Services.prefs.getBoolPref(PREF_DEBUG_ENABLED);
     log("reading prefs");
 
-    this.phishingEnabled = Services.prefs.getBoolPref("browser.safebrowsing.phishing.enabled");
-    this.malwareEnabled = Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled");
-    this.downloadsEnabled = Services.prefs.getBoolPref("browser.safebrowsing.downloads.enabled");
-    this.passwordsEnabled = Services.prefs.getBoolPref("browser.safebrowsing.passwords.enabled");
-    this.trackingEnabled = Services.prefs.getBoolPref("toolkit.telemetry.isGeckoViewMode", false) ||
-                           Services.prefs.getBoolPref("privacy.trackingprotection.enabled") ||
-                           Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled");
-    this.blockedEnabled = Services.prefs.getBoolPref("browser.safebrowsing.blockedURIs.enabled");
-    this.trackingAnnotations = Services.prefs.getBoolPref("privacy.trackingprotection.annotate_channels");
-    this.flashBlockEnabled = Services.prefs.getBoolPref("plugins.flashBlock.enabled");
 
-    let flashAllowTable, flashAllowExceptTable, flashTable,
-        flashExceptTable, flashSubDocTable,
-        flashSubDocExceptTable;
-
-    let obsoleteLists;
+    let obsoleteLists = [];
     // Make a copy of the original lists before we re-read the prefs.
     if (this.initialized) {
-      obsoleteLists = [this.phishingLists,
-                       this.malwareLists,
-                       this.downloadBlockLists,
-                       this.downloadAllowLists,
-                       this.passwordAllowLists,
-                       this.trackingAnnotationLists,
-                       this.trackingAnnotationWhitelists,
-                       this.trackingProtectionLists,
-                       this.trackingProtectionWhitelists,
-                       this.blockedLists,
-                       this.flashLists];
+      obsoleteLists = this.features.map(feature => {
+        return feature.list;
+      });
     }
 
-    [this.phishingLists,
-     this.malwareLists,
-     this.downloadBlockLists,
-     this.downloadAllowLists,
-     this.passwordAllowLists,
-     this.trackingAnnotationLists,
-     this.trackingAnnotationWhitelists,
-     this.trackingProtectionLists,
-     this.trackingProtectionWhitelists,
-     this.blockedLists,
-     flashAllowTable,
-     flashAllowExceptTable,
-     flashTable,
-     flashExceptTable,
-     flashSubDocTable,
-     flashSubDocExceptTable] = tablePreferences.map(getLists);
+    this.features = [];
+    for (let i = 0; i < FEATURES.length; ++i) {
+      this.features[i] = { name: FEATURES[i].name,
+                           list: [],
+                           enabled: FEATURES[i].enabled() };
+      FEATURES[i].list.forEach(pref => {
+        this.features[i].list.push(...getLists(pref));
+      });
+    }
 
-    this.flashLists = flashAllowTable.concat(flashAllowExceptTable,
-                                             flashTable,
-                                             flashExceptTable,
-                                             flashSubDocTable,
-                                             flashSubDocExceptTable);
-
-    if (obsoleteLists) {
-      let newLists = [this.phishingLists,
-                      this.malwareLists,
-                      this.downloadBlockLists,
-                      this.downloadAllowLists,
-                      this.passwordAllowLists,
-                      this.trackingAnnotationLists,
-                      this.trackingAnnotationWhitelists,
-                      this.trackingProtectionLists,
-                      this.trackingProtectionWhitelists,
-                      this.blockedLists,
-                      this.flashLists];
-
-      for (let i = 0; i < obsoleteLists.length; ++i) {
-        obsoleteLists[i] = obsoleteLists[i]
-          .filter(list => !newLists[i].includes(list));
-      }
+    for (let i = 0; i < obsoleteLists.length; ++i) {
+      obsoleteLists[i] = obsoleteLists[i]
+        .filter(list => !this.features[i].list.includes(list));
     }
 
     this.updateProviderURLs();
     this.registerTables();
     if (obsoleteLists) {
       this.unregisterTables(obsoleteLists);
     }
 
@@ -380,85 +328,37 @@ var SafeBrowsing = {
         }, this);
       } else {
         log("Update URL given but no lists managed for provider: " + provider);
       }
     }, this);
   },
 
   controlUpdateChecking() {
-    log("phishingEnabled:", this.phishingEnabled,
-        "malwareEnabled:", this.malwareEnabled,
-        "downloadsEnabled:", this.downloadsEnabled,
-        "passwordsEnabled:", this.passwordsEnabled,
-        "trackingEnabled:", this.trackingEnabled,
-        "blockedEnabled:", this.blockedEnabled,
-        "trackingAnnotations", this.trackingAnnotations,
-        "flashBlockEnabled", this.flashBlockEnabled);
+    if (loggingEnabled) {
+      this.features.forEach(feature => {
+        log("feature " + feature.name + ":");
+        log("  enabled:" + feature.enabled);
+        log("  tables:" + feature.list);
+      });
+    }
 
     let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"].
                       getService(Ci.nsIUrlListManager);
 
     listManager.disableAllUpdates();
 
-    for (let i = 0; i < this.phishingLists.length; ++i) {
-      if (this.phishingEnabled) {
-        listManager.enableUpdate(this.phishingLists[i]);
-      }
-    }
-    for (let i = 0; i < this.malwareLists.length; ++i) {
-      if (this.malwareEnabled) {
-        listManager.enableUpdate(this.malwareLists[i]);
-      }
-    }
-    for (let i = 0; i < this.downloadBlockLists.length; ++i) {
-      if (this.malwareEnabled && this.downloadsEnabled) {
-        listManager.enableUpdate(this.downloadBlockLists[i]);
-      }
-    }
-    for (let i = 0; i < this.downloadAllowLists.length; ++i) {
-      if (this.malwareEnabled && this.downloadsEnabled) {
-        listManager.enableUpdate(this.downloadAllowLists[i]);
-      }
-    }
-    for (let i = 0; i < this.passwordAllowLists.length; ++i) {
-      if (this.passwordsEnabled) {
-        listManager.enableUpdate(this.passwordAllowLists[i]);
+    this.features.forEach(feature => {
+      if (feature.enabled) {
+        feature.list.forEach(table => {
+          listManager.enableUpdate(table);
+        });
       }
-    }
-    for (let i = 0; i < this.trackingAnnotationLists.length; ++i) {
-      if (this.trackingAnnotations) {
-        listManager.enableUpdate(this.trackingAnnotationLists[i]);
-      }
-    }
-    for (let i = 0; i < this.trackingAnnotationWhitelists.length; ++i) {
-      if (this.trackingAnnotations) {
-        listManager.enableUpdate(this.trackingAnnotationWhitelists[i]);
-      }
-    }
-    for (let i = 0; i < this.trackingProtectionLists.length; ++i) {
-      if (this.trackingEnabled) {
-        listManager.enableUpdate(this.trackingProtectionLists[i]);
-      }
-    }
-    for (let i = 0; i < this.trackingProtectionWhitelists.length; ++i) {
-      if (this.trackingEnabled) {
-        listManager.enableUpdate(this.trackingProtectionWhitelists[i]);
-      }
-    }
-    for (let i = 0; i < this.blockedLists.length; ++i) {
-      if (this.blockedEnabled) {
-        listManager.enableUpdate(this.blockedLists[i]);
-      }
-    }
-    for (let i = 0; i < this.flashLists.length; ++i) {
-      if (this.flashBlockEnabled) {
-        listManager.enableUpdate(this.flashLists[i]);
-      }
-    }
+    });
+
     listManager.maybeToggleUpdateChecking();
   },
 
 
   addMozEntries() {
     // Add test entries to the DB.
     // XXX bug 779008 - this could be done by DB itself?
     const phishURL    = "itisatrap.org/firefox/its-a-trap.html";
@@ -534,9 +434,10 @@ var SafeBrowsing = {
       Services.obs.removeObserver(finished, "mozentries-update-finished");
       if (data == "error") {
         Cu.reportError("addMozEntries failed to update the db!");
       }
       resolve();
     };
     Services.obs.addObserver(finished, "mozentries-update-finished");
   }),
+
 };