summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/lib/sdk/remote/child.js
blob: 4ccfa661aedf4df69dff49aacc12415ee1a752d0 (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
/* 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";

const { isChildLoader } = require('./core');
if (!isChildLoader)
  throw new Error("Cannot load sdk/remote/child in a main process loader.");

const { Ci, Cc, Cu } = require('chrome');
const runtime = require('../system/runtime');
const { Class } = require('../core/heritage');
const { Namespace } = require('../core/namespace');
const { omit } = require('../util/object');
const { when } = require('../system/unload');
const { EventTarget } = require('../event/target');
const { emit } = require('../event/core');
const { Disposable } = require('../core/disposable');
const { EventParent } = require('./utils');
const { addListItem, removeListItem } = require('../util/list');

const loaderID = require('@loader/options').loaderID;

const MAIN_PROCESS = Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;

const mm = Cc['@mozilla.org/childprocessmessagemanager;1'].
           getService(Ci.nsISyncMessageSender);

const ns = Namespace();

const process = {
  port: new EventTarget(),
  get id() {
    return runtime.processID;
  },
  get isRemote() {
    return runtime.processType != MAIN_PROCESS;
  }
};
exports.process = process;

function definePort(obj, name) {
  obj.port.emit = (event, ...args) => {
    let manager = ns(obj).messageManager;
    if (!manager)
      return;

    manager.sendAsyncMessage(name, { loaderID, event, args });
  };
}

function messageReceived({ data, objects }) {
  // Ignore messages from other loaders
  if (data.loaderID != loaderID)
    return;

  let keys = Object.keys(objects);
  if (keys.length) {
    // If any objects are CPOWs then ignore this message. We don't want child
    // processes interracting with CPOWs
    if (!keys.every(name => !Cu.isCrossProcessWrapper(objects[name])))
      return;

    data.args.push(objects);
  }

  emit(this.port, data.event, this, ...data.args);
}

ns(process).messageManager = mm;
definePort(process, 'sdk/remote/process/message');
let processMessageReceived = messageReceived.bind(process);
mm.addMessageListener('sdk/remote/process/message', processMessageReceived);

when(() => {
  mm.removeMessageListener('sdk/remote/process/message', processMessageReceived);
  frames = null;
});

process.port.on('sdk/remote/require', (process, uri) => {
  require(uri);
});

function listenerEquals(a, b) {
  for (let prop of ["type", "callback", "isCapturing"]) {
    if (a[prop] != b[prop])
      return false;
  }
  return true;
}

function listenerFor(type, callback, isCapturing = false) {
  return {
    type,
    callback,
    isCapturing,
    registeredCallback: undefined,
    get args() {
      return [
        this.type,
        this.registeredCallback ? this.registeredCallback : this.callback,
        this.isCapturing
      ];
    }
  };
}

function removeListenerFromArray(array, listener) {
  let index = array.findIndex(l => listenerEquals(l, listener));
  if (index < 0)
    return;
  array.splice(index, 1);
}

function getListenerFromArray(array, listener) {
  return array.find(l => listenerEquals(l, listener));
}

function arrayContainsListener(array, listener) {
  return !!getListenerFromArray(array, listener);
}

function makeFrameEventListener(frame, callback) {
  return callback.bind(frame);
}

var FRAME_ID = 0;
var tabMap = new Map();

const Frame = Class({
  implements: [ Disposable ],
  extends: EventTarget,
  setup: function(contentFrame) {
    // This ID should be unique for this loader across all processes
    let priv = ns(this);

    priv.id = runtime.processID + ":" + FRAME_ID++;

    priv.contentFrame = contentFrame;
    priv.messageManager = contentFrame;
    priv.domListeners = [];

    tabMap.set(contentFrame.docShell, this);

    priv.messageReceived = messageReceived.bind(this);
    priv.messageManager.addMessageListener('sdk/remote/frame/message', priv.messageReceived);

    this.port = new EventTarget();
    definePort(this, 'sdk/remote/frame/message');

    priv.messageManager.sendAsyncMessage('sdk/remote/frame/attach', {
      loaderID,
      frameID: priv.id,
      processID: runtime.processID
    });

    frames.attachItem(this);
  },

  dispose: function() {
    let priv = ns(this);

    emit(this, 'detach', this);

    for (let listener of priv.domListeners)
      priv.contentFrame.removeEventListener(...listener.args);

    priv.messageManager.removeMessageListener('sdk/remote/frame/message', priv.messageReceived);
    tabMap.delete(priv.contentFrame.docShell);
    priv.contentFrame = null;
  },

  get content() {
    return ns(this).contentFrame.content;
  },

  get isTab() {
    let docShell = ns(this).contentFrame.docShell;
    if (process.isRemote) {
      // We don't want to roundtrip to the main process to get this property.
      // This hack relies on the host app having defined webBrowserChrome only
      // in frames that are part of the tabs. Since only Firefox has remote
      // processes right now and does this this works.
      let tabchild = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                             .getInterface(Ci.nsITabChild);
      return !!tabchild.webBrowserChrome;
    }
    else {
      // This is running in the main process so we can break out to the browser
      // And check we can find a tab for the browser element directly.
      let browser = docShell.chromeEventHandler;
      let tab = require('../tabs/utils').getTabForBrowser(browser);
      return !!tab;
    }
  },

  addEventListener: function(...args) {
    let priv = ns(this);

    let listener = listenerFor(...args);
    if (arrayContainsListener(priv.domListeners, listener))
      return;

    listener.registeredCallback = makeFrameEventListener(this, listener.callback);

    priv.domListeners.push(listener);
    priv.contentFrame.addEventListener(...listener.args);
  },

  removeEventListener: function(...args) {
    let priv = ns(this);

    let listener = getListenerFromArray(priv.domListeners, listenerFor(...args));
    if (!listener)
      return;

    removeListenerFromArray(priv.domListeners, listener);
    priv.contentFrame.removeEventListener(...listener.args);
  }
});

const FrameList = Class({
  implements: [ EventParent, Disposable ],
  extends: EventTarget,
  setup: function() {
    EventParent.prototype.initialize.call(this);

    this.port = new EventTarget();
    ns(this).domListeners = [];

    this.on('attach', frame => {
      for (let listener of ns(this).domListeners)
        frame.addEventListener(...listener.args);
    });
  },

  dispose: function() {
    // The only case where we get destroyed is when the loader is unloaded in
    // which case each frame will clean up its own event listeners.
    ns(this).domListeners = null;
  },

  getFrameForWindow: function(window) {
    let docShell = window.QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIDocShell);

    return tabMap.get(docShell) || null;
  },

  addEventListener: function(...args) {
    let listener = listenerFor(...args);
    if (arrayContainsListener(ns(this).domListeners, listener))
      return;

    ns(this).domListeners.push(listener);
    for (let frame of this)
      frame.addEventListener(...listener.args);
  },

  removeEventListener: function(...args) {
    let listener = listenerFor(...args);
    if (!arrayContainsListener(ns(this).domListeners, listener))
      return;

    removeListenerFromArray(ns(this).domListeners, listener);
    for (let frame of this)
      frame.removeEventListener(...listener.args);
  }
});
var frames = exports.frames = new FrameList();

function registerContentFrame(contentFrame) {
  let frame = new Frame(contentFrame);
}
exports.registerContentFrame = registerContentFrame;

function unregisterContentFrame(contentFrame) {
  let frame = tabMap.get(contentFrame.docShell);
  if (!frame)
    return;

  frame.destroy();
}
exports.unregisterContentFrame = unregisterContentFrame;