Bug 1798960 - Dispatch a cookiebannerhandled event when the click component handled a banner. r=timhuang

Differential Revision: https://phabricator.services.mozilla.com/D161630
This commit is contained in:
Paul Zuehlcke 2022-11-14 20:38:15 +00:00
parent 7f582a6faf
commit b7f8c65989
2 changed files with 57 additions and 14 deletions

View File

@ -125,7 +125,7 @@ class CookieBannerChild extends JSWindowActorChild {
try {
rules = await this.sendQuery("CookieBanner::GetClickRules", {});
} catch (e) {
lazy.logConsole.warn("Failed to get click rule from parent.");
lazy.logConsole.warn("Failed to get click rule from parent.", e);
return;
}
@ -138,7 +138,14 @@ class CookieBannerChild extends JSWindowActorChild {
this.#clickRules = rules;
await this.handleCookieBanner();
let { bannerHandled, matchedRule } = await this.handleCookieBanner();
if (bannerHandled) {
lazy.logConsole.info("Handled cookie banner.", {
url: this.document?.location.href,
rule: matchedRule,
});
this.sendAsyncMessage("CookieBanner::HandledBanner");
}
this.#maybeSendTestMessage();
}
@ -203,7 +210,7 @@ class CookieBannerChild extends JSWindowActorChild {
if (!rules.length) {
// The banner was never shown.
return;
return { bannerHandled: false };
}
// Hide the banner.
@ -219,12 +226,8 @@ class CookieBannerChild extends JSWindowActorChild {
this.#showBanner(matchedRule);
}
}
if (successClick) {
lazy.logConsole.info("Handled cookie banner.", {
url: this.document?.location.href,
rule: matchedRule,
});
}
return { bannerHandled: successClick, matchedRule };
}
/**

View File

@ -30,15 +30,49 @@ XPCOMUtils.defineLazyPreferenceGetter(
);
class CookieBannerParent extends JSWindowActorParent {
#isPrivateBrowsing() {
/**
* Get the browser associated with this window which is the top level embedder
* element. Returns null if the top embedder isn't a browser.
*/
get #browserElement() {
let topBC = this.browsingContext.top;
// Not all embedders are browsers. Skip other elements we can't do an
// isBrowserPrivate check on.
if (topBC.embedderElementType != "browser" || !topBC.embedderElement) {
// Not all embedders are browsers.
if (topBC.embedderElementType != "browser") {
return null;
}
return topBC.embedderElement;
}
#isPrivateBrowsing() {
let browser = this.#browserElement;
if (!browser) {
return false;
}
return lazy.PrivateBrowsingUtils.isBrowserPrivate(topBC.embedderElement);
return lazy.PrivateBrowsingUtils.isBrowserPrivate(browser);
}
/**
* Dispatches a custom "cookiebannerhandled" event on the chrome window.
*/
#notifyCookieBannerHandled() {
let chromeWin = this.browsingContext.topChromeWindow;
if (!chromeWin) {
return;
}
let windowUtils = chromeWin.windowUtils;
if (!windowUtils) {
return;
}
let event = new CustomEvent("cookiebannerhandled", {
bubbles: true,
cancelable: false,
detail: {
windowContext: this.manager,
},
});
windowUtils.dispatchEventToChromeOnly(chromeWin, event);
}
async receiveMessage(message) {
@ -51,6 +85,12 @@ class CookieBannerParent extends JSWindowActorParent {
return undefined;
}
// Forwards cookie banner handled signals to frontend consumers.
if (message.name == "CookieBanner::HandledBanner") {
this.#notifyCookieBannerHandled();
return undefined;
}
if (message.name != "CookieBanner::GetClickRules") {
return undefined;
}