1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* eslint no-unused-vars: ["error", {args: "none"}] */
8 // The maximum number of popup information we'll send to the parent.
9 const MAX_SENT_POPUPS = 15;
11 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
13 export class PopupBlockingChild extends JSWindowActorChild {
16 this.weakDocStates = new WeakMap();
20 * Returns the state for the current document referred to via
21 * this.document. If no such state exists, creates it, stores it
25 let state = this.weakDocStates.get(this.document);
30 this.weakDocStates.set(this.document, state);
38 case "UnblockPopup": {
39 let i = msg.data.index;
40 let state = this.docState;
41 let popupData = state.popupData[i];
43 let dwi = popupData.requestingWindow;
45 // If we have a requesting window and the requesting document is
46 // still the current document, open the popup.
47 if (dwi && dwi.document == popupData.requestingDocument) {
49 popupData.popupWindowURISpec,
50 popupData.popupWindowName,
51 popupData.popupWindowFeatures
58 case "GetBlockedPopupList": {
59 let state = this.docState;
60 let length = Math.min(state.popupData.length, MAX_SENT_POPUPS);
64 for (let i = 0; i < length; ++i) {
65 let popup = state.popupData[i];
67 let popupWindowURISpec = popup.popupWindowURISpec;
69 if (this.contentWindow.location.href == popupWindowURISpec) {
70 popupWindowURISpec = "<self>";
72 // Limit 500 chars to be sent because the URI will be cropped
73 // by the UI anyway, and data: URIs can be significantly larger.
74 popupWindowURISpec = popupWindowURISpec.substring(0, 500);
91 case "DOMPopupBlocked":
92 this.onPopupBlocked(event);
95 this.onPageShow(event);
101 onPopupBlocked(event) {
102 if (event.target != this.document) {
106 let state = this.docState;
108 // Avoid spamming the parent process with too many blocked popups.
109 if (state.popupData.length >= PopupBlockingChild.maxReportedPopups) {
114 popupWindowURISpec: event.popupWindowURI
115 ? event.popupWindowURI.spec
117 popupWindowFeatures: event.popupWindowFeatures,
118 popupWindowName: event.popupWindowName,
119 requestingWindow: event.requestingWindow,
120 requestingDocument: event.requestingWindow.document,
123 state.popupData.push(popup);
124 this.updateBlockedPopups(true);
128 if (event.target != this.document) {
132 this.updateBlockedPopups(false);
135 updateBlockedPopups(shouldNotify) {
136 this.sendAsyncMessage("UpdateBlockedPopups", {
138 count: this.docState.popupData.length,
143 XPCOMUtils.defineLazyPreferenceGetter(
146 "privacy.popups.maxReported"