Bug 1585615 - check content blocking event parameter bitwisely; r=johannh
authorLiang-Heng Chen <xeonchen@gmail.com>
Fri, 18 Oct 2019 13:01:16 +0000
changeset 559474 747c5f90f2c3916f6d7bc537bd25deac589ae3b9
parent 559473 3517b23f7cc488f367d7aa126938b257b7416184
child 559475 c1b0190a53476e04a3f79d056d177af0c30adfa0
push id12177
push usercsabou@mozilla.com
push dateMon, 21 Oct 2019 14:52:16 +0000
treeherdermozilla-beta@1918a9cd33bc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1585615
milestone71.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 1585615 - check content blocking event parameter bitwisely; r=johannh content blocking event should be checked bitwisely to see if targeted bits are all matched. Differential Revision: https://phabricator.services.mozilla.com/D48170
browser/components/newtab/lib/ASRouterTriggerListeners.jsm
browser/components/newtab/lib/CFRMessageProvider.jsm
browser/components/newtab/test/browser/browser_asrouter_trigger_listeners.js
--- a/browser/components/newtab/lib/ASRouterTriggerListeners.jsm
+++ b/browser/components/newtab/lib/ASRouterTriggerListeners.jsm
@@ -509,22 +509,25 @@ this.ASRouterTriggerListeners = new Map(
         this._events = [];
         this._sessionPageLoad = 0;
       },
 
       observe(aSubject, aTopic, aData) {
         switch (aTopic) {
           case "SiteProtection:ContentBlockingEvent":
             const { browser, host, event } = aSubject.wrappedJSObject;
-            if (this._events.includes(event)) {
+            for (let ev of this._events) {
+              if ((ev & event) !== ev) {
+                continue;
+              }
               this._triggerHandler(browser, {
                 id: "trackingProtection",
                 param: {
                   host,
-                  type: event,
+                  type: ev,
                 },
                 context: {
                   pageLoad: this._sessionPageLoad,
                 },
               });
             }
             break;
           case "SiteProtection:ContentBlockingMilestone":
--- a/browser/components/newtab/lib/CFRMessageProvider.jsm
+++ b/browser/components/newtab/lib/CFRMessageProvider.jsm
@@ -636,24 +636,28 @@ const CFR_MESSAGES = [
         secondary: [
           {
             label: { string_id: "cfr-doorhanger-socialtracking-close-button" },
             event: "BLOCK",
           },
         ],
       },
     },
-    targeting: "pageLoad >= 4",
+    targeting: "pageLoad >= 4 && firefoxVersion >= 71",
     frequency: {
       lifetime: 2,
       custom: [{ period: 2 * 86400 * 1000, cap: 1 }],
     },
     trigger: {
       id: "trackingProtection",
-      params: [Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT],
+      params: [
+        Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT,
+        Ci.nsIWebProgressListener.STATE_LOADED_SOCIALTRACKING_CONTENT |
+          Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+      ],
     },
   },
   {
     id: "FINGERPRINTERS_PROTECTION",
     template: "cfr_doorhanger",
     content: {
       layout: "icon_and_message",
       category: "cfrFeatures",
@@ -682,17 +686,17 @@ const CFR_MESSAGES = [
         secondary: [
           {
             label: { string_id: "cfr-doorhanger-socialtracking-close-button" },
             event: "BLOCK",
           },
         ],
       },
     },
-    targeting: "pageLoad >= 4",
+    targeting: "pageLoad >= 4 && firefoxVersion >= 71",
     frequency: {
       lifetime: 2,
       custom: [{ period: 2 * 86400 * 1000, cap: 1 }],
     },
     trigger: {
       id: "trackingProtection",
       params: [Ci.nsIWebProgressListener.STATE_BLOCKED_FINGERPRINTING_CONTENT],
     },
@@ -728,17 +732,17 @@ const CFR_MESSAGES = [
         secondary: [
           {
             label: { string_id: "cfr-doorhanger-socialtracking-close-button" },
             event: "BLOCK",
           },
         ],
       },
     },
-    targeting: "pageLoad >= 4",
+    targeting: "pageLoad >= 4 && && firefoxVersion >= 71",
     frequency: {
       lifetime: 2,
       custom: [{ period: 2 * 86400 * 1000, cap: 1 }],
     },
     trigger: {
       id: "trackingProtection",
       params: [Ci.nsIWebProgressListener.STATE_BLOCKED_CRYPTOMINING_CONTENT],
     },
--- a/browser/components/newtab/test/browser/browser_asrouter_trigger_listeners.js
+++ b/browser/components/newtab/test/browser/browser_asrouter_trigger_listeners.js
@@ -176,50 +176,65 @@ add_task(async function check_newSavedLo
     }
   );
 });
 
 add_task(async function check_trackingProtection_listener() {
   const TEST_URL =
     "https://example.com/browser/browser/components/newtab/test/browser/red_page.html";
 
-  const contentBlockingEvent = 1234;
+  const event1 = 0x0001;
+  const event2 = 0x0010;
+  const event3 = 0x0100;
+  const event4 = 0x1000;
+
+  // Initialise listener to listen 2 events, for any incoming event e,
+  // it will be triggered if and only if:
+  // 1. (e & event1) && (e & event2)
+  // 2. (e & event3)
+  const bindEvents = [event1 | event2, event3];
+
   let observerEvent = 0;
   let pageLoadSum = 0;
   const triggerHandler = (target, trigger) => {
     const {
       id,
-      param: { host },
+      param: { host, type },
       context: { pageLoad },
     } = trigger;
     is(id, "trackingProtection", "should match event name");
     is(host, TEST_URL, "should match test URL");
+    is(
+      bindEvents.filter(e => (type & e) === e).length,
+      1,
+      `event ${type} is valid`
+    );
+    ok(pageLoadSum <= pageLoad, "pageLoad is non-decreasing");
 
     observerEvent += 1;
-    pageLoadSum += pageLoad;
+    pageLoadSum = pageLoad;
   };
   const trackingProtectionListener = ASRouterTriggerListeners.get(
     "trackingProtection"
   );
 
   // Previously initialized by the Router
   trackingProtectionListener.uninit();
 
-  // Initialise listener
-  await trackingProtectionListener.init(triggerHandler, [contentBlockingEvent]);
+  await trackingProtectionListener.init(triggerHandler, bindEvents);
 
   await BrowserTestUtils.withNewTab(
     TEST_URL,
     async function triggerTrackingProtection(browser) {
       Services.obs.notifyObservers(
         {
           wrappedJSObject: {
             browser,
             host: TEST_URL,
-            event: contentBlockingEvent + 1,
+            event: event1, // won't trigger
           },
         },
         "SiteProtection:ContentBlockingEvent"
       );
     }
   );
 
   is(observerEvent, 0, "shouldn't receive unrelated observer notification");
@@ -228,49 +243,83 @@ add_task(async function check_trackingPr
   await BrowserTestUtils.withNewTab(
     TEST_URL,
     async function triggerTrackingProtection(browser) {
       Services.obs.notifyObservers(
         {
           wrappedJSObject: {
             browser,
             host: TEST_URL,
-            event: contentBlockingEvent,
+            event: event3, // will trigger
           },
         },
         "SiteProtection:ContentBlockingEvent"
       );
 
       await BrowserTestUtils.waitForCondition(
         () => observerEvent !== 0,
         "Wait for the observer notification to run"
       );
       is(observerEvent, 1, "should receive observer notification");
       is(pageLoadSum, 2, "should receive observer notification");
+
+      Services.obs.notifyObservers(
+        {
+          wrappedJSObject: {
+            browser,
+            host: TEST_URL,
+            event: event1 | event2 | event4, // still trigger
+          },
+        },
+        "SiteProtection:ContentBlockingEvent"
+      );
+
+      await BrowserTestUtils.waitForCondition(
+        () => observerEvent !== 1,
+        "Wait for the observer notification to run"
+      );
+      is(observerEvent, 2, "should receive another observer notification");
+      is(pageLoadSum, 2, "should receive another observer notification");
+
+      Services.obs.notifyObservers(
+        {
+          wrappedJSObject: {
+            browser,
+            host: TEST_URL,
+            event: event1, // no trigger
+          },
+        },
+        "SiteProtection:ContentBlockingEvent"
+      );
+
+      await new Promise(resolve => executeSoon(resolve));
+      is(observerEvent, 2, "shouldn't receive unrelated notification");
+      is(pageLoadSum, 2, "shouldn't receive unrelated notification");
     }
   );
 
   // Uninitialise listener
   trackingProtectionListener.uninit();
 
   await BrowserTestUtils.withNewTab(
     TEST_URL,
     async function triggerTrackingProtectionAfterUninit(browser) {
       Services.obs.notifyObservers(
         {
           wrappedJSObject: {
             browser,
             host: TEST_URL,
-            event: contentBlockingEvent,
+            event: event3, // wont trigger after uninit
           },
         },
         "SiteProtection:ContentBlockingEvent"
       );
       await new Promise(resolve => executeSoon(resolve));
-      is(observerEvent, 1, "shouldn't receive obs. notification after uninit");
+      is(observerEvent, 2, "shouldn't receive obs. notification after uninit");
+      is(pageLoadSum, 2, "shouldn't receive obs. notification after uninit");
     }
   );
 });
 
 add_task(async function check_trackingProtectionMilestone_listener() {
   const TEST_URL =
     "https://example.com/browser/browser/components/newtab/test/browser/red_page.html";