summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/BrowserElementParent.jsm
blob: 73d528456e17fb0eb8f1ffae2bc0a96691b50ea3 (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
269
270
271
272
273
274
275
276
277
/* 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/. */

"use strict";

/* BrowserElementParent injects script to listen for certain events in the
 * child.  We then listen to messages from the child script and take
 * appropriate action here in the parent.
 */

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { BrowserElementPromptService } = ChromeUtils.import(
  "resource://gre/modules/BrowserElementPromptService.jsm"
);

function debug(msg) {
  // dump("BrowserElementParent - " + msg + "\n");
}

function handleWindowEvent(e) {
  if (this._browserElementParents) {
    let beps = ChromeUtils.nondeterministicGetWeakMapKeys(
      this._browserElementParents
    );
    beps.forEach(bep => bep._handleOwnerEvent(e));
  }
}

function BrowserElementParent() {
  debug("Creating new BrowserElementParent object");
}

BrowserElementParent.prototype = {
  classDescription: "BrowserElementAPI implementation",
  classID: Components.ID("{9f171ac4-0939-4ef8-b360-3408aedc3060}"),
  contractID: "@mozilla.org/dom/browser-element-api;1",
  QueryInterface: ChromeUtils.generateQI([
    "nsIBrowserElementAPI",
    "nsISupportsWeakReference",
  ]),

  setFrameLoader(frameLoader) {
    debug("Setting frameLoader");
    this._frameLoader = frameLoader;
    this._frameElement = frameLoader.ownerElement;
    if (!this._frameElement) {
      debug("No frame element?");
      return;
    }
    // Listen to visibilitychange on the iframe's owner window, and forward
    // changes down to the child.  We want to do this while registering as few
    // visibilitychange listeners on _window as possible, because such a listener
    // may live longer than this BrowserElementParent object.
    //
    // To accomplish this, we register just one listener on the window, and have
    // it reference a WeakMap whose keys are all the BrowserElementParent objects
    // on the window.  Then when the listener fires, we iterate over the
    // WeakMap's keys (which we can do, because we're chrome) to notify the
    // BrowserElementParents.
    if (!this._window._browserElementParents) {
      this._window._browserElementParents = new WeakMap();
      let handler = handleWindowEvent.bind(this._window);
      let windowEvents = ["visibilitychange"];
      for (let event of windowEvents) {
        Services.els.addSystemEventListener(
          this._window,
          event,
          handler,
          /* useCapture = */ true
        );
      }
    }

    this._window._browserElementParents.set(this, null);

    // Insert ourself into the prompt service.
    BrowserElementPromptService.mapFrameToBrowserElementParent(
      this._frameElement,
      this
    );
    this._setupMessageListener();
  },

  destroyFrameScripts() {
    debug("Destroying frame scripts");
    this._mm.sendAsyncMessage("browser-element-api:destroy");
  },

  _setupMessageListener() {
    this._mm = this._frameLoader.messageManager;
    this._mm.addMessageListener("browser-element-api:call", this);
  },

  receiveMessage(aMsg) {
    if (!this._isAlive()) {
      return undefined;
    }

    // Messages we receive are handed to functions which take a (data) argument,
    // where |data| is the message manager's data object.
    // We use a single message and dispatch to various function based
    // on data.msg_name
    let mmCalls = {
      hello: this._recvHello,
    };

    let mmSecuritySensitiveCalls = {
      showmodalprompt: this._handleShowModalPrompt,
    };

    if (aMsg.data.msg_name in mmCalls) {
      return mmCalls[aMsg.data.msg_name].apply(this, arguments);
    } else if (aMsg.data.msg_name in mmSecuritySensitiveCalls) {
      return mmSecuritySensitiveCalls[aMsg.data.msg_name].apply(
        this,
        arguments
      );
    }
    return undefined;
  },

  _removeMessageListener() {
    this._mm.removeMessageListener("browser-element-api:call", this);
  },

  /**
   * You shouldn't touch this._frameElement or this._window if _isAlive is
   * false.  (You'll likely get an exception if you do.)
   */
  _isAlive() {
    return (
      !Cu.isDeadWrapper(this._frameElement) &&
      !Cu.isDeadWrapper(this._frameElement.ownerDocument) &&
      !Cu.isDeadWrapper(this._frameElement.ownerGlobal)
    );
  },

  get _window() {
    return this._frameElement.ownerGlobal;
  },

  _sendAsyncMsg(msg, data) {
    try {
      if (!data) {
        data = {};
      }

      data.msg_name = msg;
      this._mm.sendAsyncMessage("browser-element-api:call", data);
    } catch (e) {
      return false;
    }
    return true;
  },

  _recvHello() {
    debug("recvHello");

    // Inform our child if our owner element's document is invisible.  Note
    // that we must do so here, rather than in the BrowserElementParent
    // constructor, because the BrowserElementChild may not be initialized when
    // we run our constructor.
    if (this._window.document.hidden) {
      this._ownerVisibilityChange();
    }
  },

  /**
   * Fire either a vanilla or a custom event, depending on the contents of
   * |data|.
   */
  _fireEventFromMsg(data) {
    let detail = data.json;
    let name = detail.msg_name;

    // For events that send a "_payload_" property, we just want to transmit
    // this in the event.
    if ("_payload_" in detail) {
      detail = detail._payload_;
    }

    debug("fireEventFromMsg: " + name + ", " + JSON.stringify(detail));
    let evt = this._createEvent(name, detail, /* cancelable = */ false);
    this._frameElement.dispatchEvent(evt);
  },

  _handleShowModalPrompt(data) {
    // Fire a showmodalprmopt event on the iframe.  When this method is called,
    // the child is spinning in a nested event loop waiting for an
    // unblock-modal-prompt message.
    //
    // If the embedder calls preventDefault() on the showmodalprompt event,
    // we'll block the child until event.detail.unblock() is called.
    //
    // Otherwise, if preventDefault() is not called, we'll send the
    // unblock-modal-prompt message to the child as soon as the event is done
    // dispatching.

    let detail = data.json;
    debug("handleShowPrompt " + JSON.stringify(detail));

    // Strip off the windowID property from the object we send along in the
    // event.
    let windowID = detail.windowID;
    delete detail.windowID;
    debug("Event will have detail: " + JSON.stringify(detail));
    let evt = this._createEvent(
      "showmodalprompt",
      detail,
      /* cancelable = */ true
    );

    let self = this;
    let unblockMsgSent = false;
    function sendUnblockMsg() {
      if (unblockMsgSent) {
        return;
      }
      unblockMsgSent = true;

      // We don't need to sanitize evt.detail.returnValue (e.g. converting the
      // return value of confirm() to a boolean); Gecko does that for us.

      let data = { windowID, returnValue: evt.detail.returnValue };
      self._sendAsyncMsg("unblock-modal-prompt", data);
    }

    Cu.exportFunction(sendUnblockMsg, evt.detail, { defineAs: "unblock" });

    this._frameElement.dispatchEvent(evt);

    if (!evt.defaultPrevented) {
      // Unblock the inner frame immediately.  Otherwise we'll unblock upon
      // evt.detail.unblock().
      sendUnblockMsg();
    }
  },

  _createEvent(evtName, detail, cancelable) {
    // This will have to change if we ever want to send a CustomEvent with null
    // detail.  For now, it's OK.
    if (detail !== undefined && detail !== null) {
      detail = Cu.cloneInto(detail, this._window);
      return new this._window.CustomEvent("mozbrowser" + evtName, {
        bubbles: true,
        cancelable,
        detail,
      });
    }

    return new this._window.Event("mozbrowser" + evtName, {
      bubbles: true,
      cancelable,
    });
  },

  _handleOwnerEvent(evt) {
    switch (evt.type) {
      case "visibilitychange":
        this._ownerVisibilityChange();
        break;
    }
  },

  /**
   * Called when the visibility of the window which owns this iframe changes.
   */
  _ownerVisibilityChange() {
    let bc = this._frameLoader?.browsingContext;
    if (bc) {
      bc.isActive = !this._window.document.hidden;
    }
  },
};

var EXPORTED_SYMBOLS = ["BrowserElementParent"];