summaryrefslogtreecommitdiffstats
path: root/toolkit/actors/PopupBlockingChild.sys.mjs
blob: 053f9683c598c1991f7174e97df3c7cc2ec83182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint no-unused-vars: ["error", {args: "none"}] */

// The maximum number of popup information we'll send to the parent.
const MAX_SENT_POPUPS = 15;

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

export class PopupBlockingChild extends JSWindowActorChild {
  constructor() {
    super();
    this.weakDocStates = new WeakMap();
  }

  /**
   * Returns the state for the current document referred to via
   * this.document. If no such state exists, creates it, stores it
   * and returns it.
   */
  get docState() {
    let state = this.weakDocStates.get(this.document);
    if (!state) {
      state = {
        popupData: [],
      };
      this.weakDocStates.set(this.document, state);
    }

    return state;
  }

  receiveMessage(msg) {
    switch (msg.name) {
      case "UnblockPopup": {
        let i = msg.data.index;
        let state = this.docState;
        let popupData = state.popupData[i];
        if (popupData) {
          let dwi = popupData.requestingWindow;

          // If we have a requesting window and the requesting document is
          // still the current document, open the popup.
          if (dwi && dwi.document == popupData.requestingDocument) {
            dwi.open(
              popupData.popupWindowURISpec,
              popupData.popupWindowName,
              popupData.popupWindowFeatures
            );
          }
        }
        break;
      }

      case "GetBlockedPopupList": {
        let state = this.docState;
        let length = Math.min(state.popupData.length, MAX_SENT_POPUPS);

        let result = [];

        for (let i = 0; i < length; ++i) {
          let popup = state.popupData[i];

          let popupWindowURISpec = popup.popupWindowURISpec;

          if (this.contentWindow.location.href == popupWindowURISpec) {
            popupWindowURISpec = "<self>";
          } else {
            // Limit 500 chars to be sent because the URI will be cropped
            // by the UI anyway, and data: URIs can be significantly larger.
            popupWindowURISpec = popupWindowURISpec.substring(0, 500);
          }

          result.push({
            popupWindowURISpec,
          });
        }

        return result;
      }
    }

    return null;
  }

  handleEvent(event) {
    switch (event.type) {
      case "DOMPopupBlocked":
        this.onPopupBlocked(event);
        break;
      case "pageshow": {
        this.onPageShow(event);
        break;
      }
    }
  }

  onPopupBlocked(event) {
    if (event.target != this.document) {
      return;
    }

    let state = this.docState;

    // Avoid spamming the parent process with too many blocked popups.
    if (state.popupData.length >= PopupBlockingChild.maxReportedPopups) {
      return;
    }

    let popup = {
      popupWindowURISpec: event.popupWindowURI
        ? event.popupWindowURI.spec
        : "about:blank",
      popupWindowFeatures: event.popupWindowFeatures,
      popupWindowName: event.popupWindowName,
      requestingWindow: event.requestingWindow,
      requestingDocument: event.requestingWindow.document,
    };

    state.popupData.push(popup);
    this.updateBlockedPopups(true);
  }

  onPageShow(event) {
    if (event.target != this.document) {
      return;
    }

    this.updateBlockedPopups(false);
  }

  updateBlockedPopups(shouldNotify) {
    this.sendAsyncMessage("UpdateBlockedPopups", {
      shouldNotify,
      count: this.docState.popupData.length,
    });
  }
}

XPCOMUtils.defineLazyPreferenceGetter(
  PopupBlockingChild,
  "maxReportedPopups",
  "privacy.popups.maxReported"
);