summaryrefslogtreecommitdiffstats
path: root/toolkit/actors/PopupBlockingParent.sys.mjs
blob: adb49a1ba22a132a5127d4f676828569fcd9aeb9 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* 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/. */

/**
 * This class manages all popup blocking operations on a <xul:browser>, including
 * notifying the UI about updates to the blocked popups, and allowing popups to
 * be unblocked.
 */
export class PopupBlocker {
  constructor(browser) {
    this._browser = browser;
    this._allBlockedPopupCounts = new WeakMap();
    this._shouldShowNotification = false;
  }

  /**
   * Returns whether or not there are new blocked popups for the associated
   * <xul:browser> that the user might need to be notified about.
   */
  get shouldShowNotification() {
    return this._shouldShowNotification;
  }

  /**
   * Should be called by the UI when the user has been notified about blocked
   * popups for the associated <xul:browser>.
   */
  didShowNotification() {
    this._shouldShowNotification = false;
  }

  /**
   * Synchronously returns the most recent count of blocked popups for
   * the associated <xul:browser>.
   *
   * @return {Number}
   *   The total number of blocked popups for this <xul:browser>.
   */
  getBlockedPopupCount() {
    let totalBlockedPopups = 0;

    let contextsToVisit = [this._browser.browsingContext];
    while (contextsToVisit.length) {
      let currentBC = contextsToVisit.pop();
      let windowGlobal = currentBC.currentWindowGlobal;

      if (!windowGlobal) {
        continue;
      }

      let popupCountForGlobal =
        this._allBlockedPopupCounts.get(windowGlobal) || 0;
      totalBlockedPopups += popupCountForGlobal;
      contextsToVisit.push(...currentBC.children);
    }

    return totalBlockedPopups;
  }

  /**
   * Asynchronously retrieve information about the popups that have
   * been blocked for the associated <xul:browser>. This information
   * can be used to unblock those popups.
   *
   * @return {Promise}
   * @resolves {Array}
   *   When the blocked popup information has been gathered,
   *   resolves with an Array of Objects with the following properties:
   *
   *   browsingContext {BrowsingContext}
   *     The BrowsingContext that the popup was blocked for.
   *
   *   innerWindowId {Number}
   *     The inner window ID for the blocked popup. This is used to differentiate
   *     popups that were blocked from one page load to the next.
   *
   *   popupWindowURISpec {String}
   *     A string representing part or all of the URI that tried to be opened in a
   *     popup.
   */
  async getBlockedPopups() {
    let contextsToVisit = [this._browser.browsingContext];
    let result = [];
    while (contextsToVisit.length) {
      let currentBC = contextsToVisit.pop();
      let windowGlobal = currentBC.currentWindowGlobal;

      if (!windowGlobal) {
        continue;
      }

      let popupCountForGlobal =
        this._allBlockedPopupCounts.get(windowGlobal) || 0;
      if (popupCountForGlobal) {
        let actor = windowGlobal.getActor("PopupBlocking");
        let popups = await actor.sendQuery("GetBlockedPopupList");

        for (let popup of popups) {
          if (!popup.popupWindowURISpec) {
            continue;
          }

          result.push({
            browsingContext: currentBC,
            innerWindowId: windowGlobal.innerWindowId,
            popupWindowURISpec: popup.popupWindowURISpec,
          });
        }
      }

      contextsToVisit.push(...currentBC.children);
    }

    return result;
  }

  /**
   * Unblocks a popup that had been blocked. The information passed should
   * come from the list of blocked popups returned via getBlockedPopups().
   *
   * Unblocking a popup causes that popup to open.
   *
   * @param browsingContext {BrowsingContext}
   *   The BrowsingContext that the popup was blocked for.
   *
   * @param innerWindowId {Number}
   *   The inner window ID for the blocked popup. This is used to differentiate popups
   *   that were blocked from one page load to the next.
   *
   * @param popupIndex {Number}
   *   The index of the entry in the Array returned by getBlockedPopups().
   */
  unblockPopup(browsingContext, innerWindowId, popupIndex) {
    let popupFrame = browsingContext.top.embedderElement;
    let popupBrowser = popupFrame.outerBrowser
      ? popupFrame.outerBrowser
      : popupFrame;

    if (this._browser != popupBrowser) {
      throw new Error(
        "Attempting to unblock popup in a BrowsingContext no longer hosted in this browser."
      );
    }

    let windowGlobal = browsingContext.currentWindowGlobal;

    if (!windowGlobal || windowGlobal.innerWindowId != innerWindowId) {
      // The inner window has moved on since the user clicked on
      // the blocked popups dropdown, so we'll just exit silently.
      return;
    }

    let actor = browsingContext.currentWindowGlobal.getActor("PopupBlocking");
    actor.sendAsyncMessage("UnblockPopup", { index: popupIndex });
  }

  /**
   * Goes through the most recent list of blocked popups for the associated
   * <xul:browser> and unblocks all of them. Unblocking a popup causes the popup
   * to open.
   */
  async unblockAllPopups() {
    let popups = await this.getBlockedPopups();
    for (let i = 0; i < popups.length; ++i) {
      let popup = popups[i];
      this.unblockPopup(popup.browsingContext, popup.innerWindowId, i);
    }
  }

  /**
   * Fires a DOMUpdateBlockedPopups chrome-only event so that the UI can
   * update itself to represent the current state of popup blocking for
   * the associated <xul:browser>.
   */
  updateBlockedPopupsUI() {
    let event = this._browser.ownerDocument.createEvent("Events");
    event.initEvent("DOMUpdateBlockedPopups", true, true);
    this._browser.dispatchEvent(event);
  }

  /** Private methods **/

  /**
   * Updates the current popup count for a particular BrowsingContext based
   * on messages from the underlying process.
   *
   * This should only be called by a PopupBlockingParent instance.
   *
   * @param browsingContext {BrowsingContext}
   *   The BrowsingContext to update the internal blocked popup count for.
   *
   * @param blockedPopupData {Object}
   *   An Object representing information about how many popups are blocked
   *   for the BrowsingContext. The Object has the following properties:
   *
   *   count {Number}
   *     The total number of blocked popups for the BrowsingContext.
   *
   *   shouldNotify {Boolean}
   *     Whether or not the list of blocked popups has changed in such a way that
   *     the UI should be updated about it.
   */
  _updateBlockedPopupEntries(browsingContext, blockedPopupData) {
    let windowGlobal = browsingContext.currentWindowGlobal;
    let { count, shouldNotify } = blockedPopupData;

    if (!this.shouldShowNotification && shouldNotify) {
      this._shouldShowNotification = true;
    }

    if (windowGlobal) {
      this._allBlockedPopupCounts.set(windowGlobal, count);
    }

    this.updateBlockedPopupsUI();
  }
}

/**
 * To keep things properly encapsulated, these should only be instantiated via
 * the PopupBlocker class for a particular <xul:browser>.
 *
 * Instantiated for a WindowGlobalParent for a BrowsingContext in one of two cases:
 *
 *   1. One or more popups have been blocked for the underlying frame represented
 *      by the WindowGlobalParent.
 *
 *   2. Something in the parent process is querying a frame for information about
 *      any popups that may have been blocked inside of it.
 */
export class PopupBlockingParent extends JSWindowActorParent {
  didDestroy() {
    this.updatePopupCountForBrowser({ count: 0, shouldNotify: false });
  }

  receiveMessage(message) {
    if (message.name == "UpdateBlockedPopups") {
      this.updatePopupCountForBrowser({
        count: message.data.count,
        shouldNotify: message.data.shouldNotify,
      });
    }
  }

  /**
   * Updates the PopupBlocker for the <xul:browser> associated with this
   * PopupBlockingParent with the most recent count of blocked popups.
   *
   * @param data {Object}
   *   An Object with the following properties:
   *
   *     count {Number}:
   *       The number of blocked popups for the underlying document.
   *
   *     shouldNotify {Boolean}:
   *       Whether or not the list of blocked popups has changed in such a way that
   *       the UI should be updated about it.
   */
  updatePopupCountForBrowser(data) {
    let browser = this.browsingContext.top.embedderElement;
    if (!browser) {
      return;
    }

    browser.popupBlocker._updateBlockedPopupEntries(this.browsingContext, data);
  }
}