fune/browser/base/content/test/protectionsUI/head.js
Gijs Kruitbosch 387e23f588 Bug 1634032 - use a template to wrap the protections popup while it's not needed, r=nhnt11
This also only updates per-category block information in the protections panel
while it's open or when it's about to be shown. To do this, the patch:

1. changes the `categoryItem` getter on all blockers to avoid memo'izing null when the popup is unavailable;
2. changes the `updateCategoryItem` method on all blockers to deal with `categoryItem` being null;
3. stops calling `updateCategoryItem` from the blockers' `init` method, instead delegating this responsibility
   to gProtectionsHandler doing so when the popup first becomes available. Note that we still need (2) because
   pref changes can trip us calling into `updateCategoryItem` anyway. We cannot avoid instantiating the pref
   getters because they are relied on by some of the `isDetected` and `isBlocking` implementations.
4. reorganizes `onContentBlockingEvent` so it updates the icon, reports telemetry, and updates internal state -
   but only updates the panel if it's visible, and otherwise simply memorizes the last event (which is just
   a number!);
5. ensures showing the panel updates the panel's category data based on the memorized state from (4).

Differential Revision: https://phabricator.services.mozilla.com/D81926
2020-07-10 21:32:09 +00:00

220 lines
5.5 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
const { Sqlite } = ChromeUtils.import("resource://gre/modules/Sqlite.jsm");
XPCOMUtils.defineLazyServiceGetter(
this,
"TrackingDBService",
"@mozilla.org/tracking-db-service;1",
"nsITrackingDBService"
);
XPCOMUtils.defineLazyGetter(this, "TRACK_DB_PATH", function() {
return OS.Path.join(OS.Constants.Path.profileDir, "protections.sqlite");
});
ChromeUtils.defineModuleGetter(
this,
"ContentBlockingAllowList",
"resource://gre/modules/ContentBlockingAllowList.jsm"
);
var { UrlClassifierTestUtils } = ChromeUtils.import(
"resource://testing-common/UrlClassifierTestUtils.jsm"
);
async function openProtectionsPanel(toast, win = window) {
let popupShownPromise = BrowserTestUtils.waitForEvent(
win,
"popupshown",
true,
e => e.target.id == "protections-popup"
);
let shieldIconContainer = win.document.getElementById(
"tracking-protection-icon-container"
);
// Move out than move over the shield icon to trigger the hover event in
// order to fetch tracker count.
EventUtils.synthesizeMouseAtCenter(
win.gURLBar.textbox,
{
type: "mousemove",
},
win
);
EventUtils.synthesizeMouseAtCenter(
shieldIconContainer,
{
type: "mousemove",
},
win
);
if (!toast) {
EventUtils.synthesizeMouseAtCenter(shieldIconContainer, {}, win);
} else {
win.gProtectionsHandler.showProtectionsPopup({ toast });
}
await popupShownPromise;
}
async function openProtectionsPanelWithKeyNav() {
let popupShownPromise = BrowserTestUtils.waitForEvent(
window,
"popupshown",
true,
e => e.target.id == "protections-popup"
);
gURLBar.focus();
// This will trigger the focus event for the shield icon for pre-fetching
// the tracker count.
EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
EventUtils.synthesizeKey("KEY_Enter", {});
await popupShownPromise;
}
async function closeProtectionsPanel(win = window) {
let protectionsPopup = win.document.getElementById("protections-popup");
if (!protectionsPopup) {
return;
}
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
protectionsPopup,
"popuphidden"
);
PanelMultiView.hidePopup(protectionsPopup);
await popuphiddenPromise;
}
function checkClickTelemetry(objectName, value, source = "protectionspopup") {
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS
).parent;
let buttonEvents = events.filter(
e =>
e[1] == `security.ui.${source}` &&
e[2] == "click" &&
e[3] == objectName &&
e[4] === value
);
is(buttonEvents.length, 1, `recorded ${objectName} telemetry event`);
}
async function addTrackerDataIntoDB(count) {
const insertSQL =
"INSERT INTO events (type, count, timestamp)" +
"VALUES (:type, :count, date(:timestamp));";
let db = await Sqlite.openConnection({ path: TRACK_DB_PATH });
let date = new Date().toISOString();
await db.execute(insertSQL, {
type: TrackingDBService.TRACKERS_ID,
count,
timestamp: date,
});
await db.close();
}
async function waitForAboutProtectionsTab() {
let tab = await BrowserTestUtils.waitForNewTab(
gBrowser,
"about:protections",
true
);
// When the graph is built it means the messaging has finished,
// we can close the tab.
await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
await ContentTaskUtils.waitForCondition(() => {
let bars = content.document.querySelectorAll(".graph-bar");
return bars.length;
}, "The graph has been built");
});
return tab;
}
/**
* Waits for a load (or custom) event to finish in a given tab. If provided
* load an uri into the tab.
*
* @param tab
* The tab to load into.
* @param [optional] url
* The url to load, or the current url.
* @return {Promise} resolved when the event is handled.
* @resolves to the received event
* @rejects if a valid load event is not received within a meaningful interval
*/
function promiseTabLoadEvent(tab, url) {
info("Wait tab event: load");
function handle(loadedUrl) {
if (loadedUrl === "about:blank" || (url && loadedUrl !== url)) {
info(`Skipping spurious load event for ${loadedUrl}`);
return false;
}
info("Tab event received: load");
return true;
}
let loaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, handle);
if (url) {
BrowserTestUtils.loadURI(tab.linkedBrowser, url);
}
return loaded;
}
function waitForSecurityChange(numChanges = 1, win = null) {
if (!win) {
win = window;
}
return new Promise(resolve => {
let n = 0;
let listener = {
onSecurityChange() {
n = n + 1;
info("Received onSecurityChange event " + n + " of " + numChanges);
if (n >= numChanges) {
win.gBrowser.removeProgressListener(listener);
resolve(n);
}
},
};
win.gBrowser.addProgressListener(listener);
});
}
function waitForContentBlockingEvent(numChanges = 1, win = null) {
if (!win) {
win = window;
}
return new Promise(resolve => {
let n = 0;
let listener = {
onContentBlockingEvent(webProgress, request, event) {
n = n + 1;
info(
`Received onContentBlockingEvent event: ${event} (${n} of ${numChanges})`
);
if (n >= numChanges) {
win.gBrowser.removeProgressListener(listener);
resolve(n);
}
},
};
win.gBrowser.addProgressListener(listener);
});
}