summaryrefslogtreecommitdiffstats
path: root/toolkit/components/remotepagemanager/RemotePageManagerParent.jsm
blob: 4d19483c7eba70e504c636ace518bf8f060bfb5d (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 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";

var EXPORTED_SYMBOLS = ["RemotePages", "RemotePageManager"];

/*
 * Using the RemotePageManager:
 * * Create a new page listener by calling 'new RemotePages(URI)' which
 *   then injects functions like RPMGetBoolPref() into the registered page.
 *   One can then use those exported functions to communicate between
 *   child and parent.
 *
 * * When adding a new consumer of RPM that relies on other functionality
 *   then simple message passing provided by the RPM, then one has to
 *   whitelist permissions for the new URI within the RPMAccessManager
 *   from MessagePort.jsm.
 */

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { MessageListener, MessagePort } = ChromeUtils.import(
  "resource://gre/modules/remotepagemanager/MessagePort.jsm"
);

/**
 * Creates a RemotePages object which listens for new remote pages of some
 * particular URLs. A "RemotePage:Init" message will be dispatched to this
 * object for every page loaded. Message listeners added to this object receive
 * messages from all loaded pages from the requested urls.
 */
class RemotePages {
  constructor(urls) {
    this.urls = Array.isArray(urls) ? urls : [urls];
    this.messagePorts = new Set();
    this.listener = new MessageListener();
    this.destroyed = false;

    this.portCreated = this.portCreated.bind(this);
    this.portMessageReceived = this.portMessageReceived.bind(this);

    for (const url of this.urls) {
      RemotePageManager.addRemotePageListener(url, this.portCreated);
    }
  }

  destroy() {
    for (const url of this.urls) {
      RemotePageManager.removeRemotePageListener(url);
    }

    for (let port of this.messagePorts.values()) {
      this.removeMessagePort(port);
    }

    this.messagePorts = null;
    this.listener = null;
    this.destroyed = true;
  }

  // Called when a page matching one of the urls has loaded in a frame.
  portCreated(port) {
    this.messagePorts.add(port);

    port.loaded = false;
    port.addMessageListener("RemotePage:Load", this.portMessageReceived);
    port.addMessageListener("RemotePage:Unload", this.portMessageReceived);

    for (let name of this.listener.keys()) {
      this.registerPortListener(port, name);
    }

    this.listener.callListeners({ target: port, name: "RemotePage:Init" });
  }

  // A message has been received from one of the pages
  portMessageReceived(message) {
    switch (message.name) {
      case "RemotePage:Load":
        message.target.loaded = true;
        break;
      case "RemotePage:Unload":
        message.target.loaded = false;
        this.removeMessagePort(message.target);
        break;
    }

    this.listener.callListeners(message);
  }

  // A page has closed
  removeMessagePort(port) {
    for (let name of this.listener.keys()) {
      port.removeMessageListener(name, this.portMessageReceived);
    }

    port.removeMessageListener("RemotePage:Load", this.portMessageReceived);
    port.removeMessageListener("RemotePage:Unload", this.portMessageReceived);
    this.messagePorts.delete(port);
  }

  registerPortListener(port, name) {
    port.addMessageListener(name, this.portMessageReceived);
  }

  // Sends a message to all known pages
  sendAsyncMessage(name, data = null) {
    for (let port of this.messagePorts.values()) {
      try {
        port.sendAsyncMessage(name, data);
      } catch (e) {
        // Unless the port is in the process of unloading, something strange
        // happened but allow other ports to receive the message
        if (e.result !== Cr.NS_ERROR_NOT_INITIALIZED) {
          Cu.reportError(e);
        }
      }
    }
  }

  addMessageListener(name, callback) {
    if (this.destroyed) {
      throw new Error("RemotePages has been destroyed");
    }

    if (!this.listener.has(name)) {
      for (let port of this.messagePorts.values()) {
        this.registerPortListener(port, name);
      }
    }

    this.listener.addMessageListener(name, callback);
  }

  removeMessageListener(name, callback) {
    if (this.destroyed) {
      throw new Error("RemotePages has been destroyed");
    }

    this.listener.removeMessageListener(name, callback);
  }

  portsForBrowser(browser) {
    return [...this.messagePorts].filter(port => port.browser == browser);
  }
}

// Only exposes the public properties of the MessagePort
function publicMessagePort(port) {
  let properties = [
    "addMessageListener",
    "removeMessageListener",
    "sendAsyncMessage",
    "destroy",
  ];

  let clean = {};
  for (let property of properties) {
    clean[property] = port[property].bind(port);
  }

  Object.defineProperty(clean, "portID", {
    enumerable: true,
    get() {
      return port.portID;
    },
  });

  if (port instanceof ChromeMessagePort) {
    Object.defineProperty(clean, "browser", {
      enumerable: true,
      get() {
        return port.browser;
      },
    });

    Object.defineProperty(clean, "url", {
      enumerable: true,
      get() {
        return port.url;
      },
    });
  }

  return clean;
}

// The chome side of a message port
class ChromeMessagePort extends MessagePort {
  constructor(browser, portID, url) {
    super(browser.messageManager, portID);

    this._browser = browser;
    this._permanentKey = browser.permanentKey;
    this._url = url;

    Services.obs.addObserver(this, "message-manager-disconnect");
    this.publicPort = publicMessagePort(this);

    this.swapBrowsers = this.swapBrowsers.bind(this);
    this._browser.addEventListener("SwapDocShells", this.swapBrowsers);
  }

  get browser() {
    return this._browser;
  }

  get url() {
    return this._url;
  }

  // Called when the docshell is being swapped with another browser. We have to
  // update to use the new browser's message manager
  swapBrowsers({ detail: newBrowser }) {
    // We can see this event for the new browser before the swap completes so
    // check that the browser we're tracking has our permanentKey.
    if (this._browser.permanentKey != this._permanentKey) {
      return;
    }

    this._browser.removeEventListener("SwapDocShells", this.swapBrowsers);

    this._browser = newBrowser;
    this.swapMessageManager(newBrowser.messageManager);

    this._browser.addEventListener("SwapDocShells", this.swapBrowsers);
  }

  // Called when a message manager has been disconnected indicating that the
  // tab has closed or crashed
  observe(messageManager) {
    if (messageManager != this.messageManager) {
      return;
    }

    this.listener.callListeners({
      target: this.publicPort,
      name: "RemotePage:Unload",
      data: null,
    });
    this.destroy();
  }

  // Called when the content process is requesting some data.
  async handleRequest(name, data) {
    throw new Error(`Unknown request ${name}.`);
  }

  // Called when a message is received from the message manager.
  handleMessage(messagedata) {
    let message = {
      target: this.publicPort,
      name: messagedata.name,
      data: messagedata.data,
      browsingContextID: messagedata.browsingContextID,
    };
    this.listener.callListeners(message);

    if (messagedata.name == "RemotePage:Unload") {
      this.destroy();
    }
  }

  destroy() {
    try {
      this._browser.removeEventListener("SwapDocShells", this.swapBrowsers);
    } catch (e) {
      // It's possible the browser instance is already dead so we can just ignore
      // this error.
    }

    this._browser = null;
    Services.obs.removeObserver(this, "message-manager-disconnect");
    super.destroy.call(this);
  }
}

// Allows callers to register to connect to specific content pages. Registration
// is done through the addRemotePageListener method
var RemotePageManagerInternal = {
  // The currently registered remote pages
  pages: new Map(),

  // Initialises all the needed listeners
  init() {
    Services.mm.addMessageListener(
      "RemotePage:InitPort",
      this.initPort.bind(this)
    );
    this.updateProcessUrls();
  },

  updateProcessUrls() {
    Services.ppmm.sharedData.set(
      "RemotePageManager:urls",
      new Set(this.pages.keys())
    );
    Services.ppmm.sharedData.flush();
  },

  // Registers interest in a remote page. A callback is called with a port for
  // the new page when loading begins (i.e. the page hasn't actually loaded yet).
  // Only one callback can be registered per URL.
  addRemotePageListener(url, callback) {
    if (this.pages.has(url)) {
      throw new Error("Remote page already registered: " + url);
    }

    this.pages.set(url, callback);
    this.updateProcessUrls();
  },

  // Removes any interest in a remote page.
  removeRemotePageListener(url) {
    if (!this.pages.has(url)) {
      throw new Error("Remote page is not registered: " + url);
    }

    this.pages.delete(url);
    this.updateProcessUrls();
  },

  // A remote page has been created and a port is ready in the content side
  initPort({ target: browser, data: { url, portID } }) {
    let callback = this.pages.get(url);
    if (!callback) {
      Cu.reportError("Unexpected remote page load: " + url);
      return;
    }

    let port = new ChromeMessagePort(browser, portID, url);
    callback(port.publicPort);
  },
};

if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
  throw new Error("RemotePageManager can only be used in the main process.");
}

RemotePageManagerInternal.init();

// The public API for the above object
var RemotePageManager = {
  addRemotePageListener: RemotePageManagerInternal.addRemotePageListener.bind(
    RemotePageManagerInternal
  ),
  removeRemotePageListener: RemotePageManagerInternal.removeRemotePageListener.bind(
    RemotePageManagerInternal
  ),
};