Bug 1440022: hook up blocklist-clients to broadcast messages r?kitcambridge draft
authorEthan Glasser-Camp <ethan@betacantrips.com>
Tue, 08 May 2018 11:53:06 -0400
changeset 792612 629aeb8f81ddec0d27925f77bdf2b5d6c7f3dd9d
parent 792611 7c7ae83601344b12cba61239587474660113e639
push id109163
push userbmo:eglassercamp@mozilla.com
push dateTue, 08 May 2018 18:36:12 +0000
reviewerskitcambridge
bugs1440022
milestone61.0a1
Bug 1440022: hook up blocklist-clients to broadcast messages r?kitcambridge MozReview-Commit-ID: 3TYBqH94rSD
services/common/blocklist-clients.js
--- a/services/common/blocklist-clients.js
+++ b/services/common/blocklist-clients.js
@@ -5,23 +5,26 @@
 "use strict";
 
 var EXPORTED_SYMBOLS = [
   "initialize",
   "AddonBlocklistClient",
   "PluginBlocklistClient",
   "GfxBlocklistClient",
   "PinningBlocklistClient",
+  "blocklistBroadcastHandler",
 ];
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm", {});
 
 ChromeUtils.defineModuleGetter(this, "RemoteSettings",
                                "resource://services-common/remote-settings.js");
+ChromeUtils.defineModuleGetter(this, "pushBroadcastService",
+                               "resource://gre/modules/PushBroadcastService.jsm");
 
 const PREF_BLOCKLIST_BUCKET                  = "services.blocklist.bucket";
 const PREF_BLOCKLIST_ONECRL_COLLECTION       = "services.blocklist.onecrl.collection";
 const PREF_BLOCKLIST_ONECRL_CHECKED_SECONDS  = "services.blocklist.onecrl.checked";
 const PREF_BLOCKLIST_ADDONS_COLLECTION       = "services.blocklist.addons.collection";
 const PREF_BLOCKLIST_ADDONS_CHECKED_SECONDS  = "services.blocklist.addons.checked";
 const PREF_BLOCKLIST_PLUGINS_COLLECTION      = "services.blocklist.plugins.collection";
 const PREF_BLOCKLIST_PLUGINS_CHECKED_SECONDS = "services.blocklist.plugins.checked";
@@ -127,16 +130,18 @@ async function updateJSONBlocklist(clien
 }
 
 var AddonBlocklistClient;
 var GfxBlocklistClient;
 var OneCRLBlocklistClient;
 var PinningBlocklistClient;
 var PluginBlocklistClient;
 
+let blocklistClientsByBroadcastID = new Map();
+
 function initialize() {
   OneCRLBlocklistClient = RemoteSettings(Services.prefs.getCharPref(PREF_BLOCKLIST_ONECRL_COLLECTION), {
     bucketName: Services.prefs.getCharPref(PREF_BLOCKLIST_BUCKET),
     lastCheckTimePref: PREF_BLOCKLIST_ONECRL_CHECKED_SECONDS,
     signerName: "onecrl.content-signature.mozilla.org",
   });
   OneCRLBlocklistClient.on("change", updateCertBlocklist);
 
@@ -159,10 +164,43 @@ function initialize() {
   GfxBlocklistClient.on("change", updateJSONBlocklist.bind(null, GfxBlocklistClient));
 
   PinningBlocklistClient = RemoteSettings(Services.prefs.getCharPref(PREF_BLOCKLIST_PINNING_COLLECTION), {
     bucketName: Services.prefs.getCharPref(PREF_BLOCKLIST_PINNING_BUCKET),
     lastCheckTimePref: PREF_BLOCKLIST_PINNING_CHECKED_SECONDS,
     signerName: "pinning-preload.content-signature.mozilla.org",
   });
   PinningBlocklistClient.on("change", updatePinningList);
+
+  const allBlocklistsRemoteSettings = [
+    OneCRLBlocklistClient,
+    AddonBlocklistClient,
+    GfxBlocklistClient,
+    PluginBlocklistClient,
+    PinningBlocklistClient,
+  ];
+
+  allBlocklistsRemoteSettings.forEach(async client => {
+    const broadcastID = `remote-settings/${client.bucketName}_${client.collectionName}`;
+    blocklistClientsByBroadcastID.set(broadcastID, client);
+
+    const currentVersion = await client.openCollection(async collection => {
+      return (await collection.db.getLastModified());
+    });
+    const moduleInfo = {
+      moduleURI: "resource://services-common/blocklist-clients.js",
+      attributeName: "blocklistBroadcastHandler",
+    };
+    pushBroadcastService.addListener(broadcastID, currentVersion.toString(),
+                                     moduleInfo);
+  });
 }
 
+var blocklistBroadcastHandler = {
+  async receivedBroadcastMessage(data, broadcastID) {
+    initialize();
+    const client = blocklistClientsByBroadcastID.get(broadcastID);
+    if (!client) {
+      throw new Error(`unknown blocklist broadcastID: ${broadcastID}`);
+    }
+    await client.maybeSync();
+  }
+};