summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/e10s-remote/remote-module.js
blob: cedf005a9f65e4e794b3bce947e1fa8052c82715 (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
/* 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/. */

const { when } = require('sdk/system/unload');
const { process, frames } = require('sdk/remote/child');
const { loaderID } = require('@loader/options');
const { processID } = require('sdk/system/runtime');
const system = require('sdk/system/events');
const { Cu } = require('chrome');
const { isChildLoader } = require('sdk/remote/core');
const { getOuterId } = require('sdk/window/utils');

function log(str) {
  console.log("remote[" + loaderID + "][" + processID + "]: " + str);
}

log("module loaded");

process.port.emit('sdk/test/load');

process.port.on('sdk/test/ping', (process, key) => {
  log("received process ping");
  process.port.emit('sdk/test/pong', key);
});

var frameCount = 0;
frames.forEvery(frame => {
  frameCount++;
  frame.on('detach', () => {
    frameCount--;
  });

  frame.port.on('sdk/test/ping', (frame, key) => {
    log("received frame ping");
    frame.port.emit('sdk/test/pong', key);
  });
});

frames.port.on('sdk/test/checkproperties', frame => {
  frame.port.emit('sdk/test/replyproperties', {
    isTab: frame.isTab
  });
});

process.port.on('sdk/test/count', () => {
  log("received count ping");
  process.port.emit('sdk/test/count', frameCount);
});

process.port.on('sdk/test/getprocessid', () => {
  process.port.emit('sdk/test/processid', processID);
});

frames.port.on('sdk/test/testunload', (frame) => {
  // Cache the content since the frame will have been destroyed by the time
  // we see the unload event.
  let content = frame.content;
  when((reason) => {
    content.location = "#unloaded:" + reason;
  });
});

frames.port.on('sdk/test/testdetachonunload', (frame) => {
  let content = frame.content;
  frame.on('detach', () => {
    console.log("Detach from " + frame.content.location);
    frame.content.location = "#unloaded";
  });
});

frames.port.on('sdk/test/sendevent', (frame) => {
  let doc = frame.content.document;

  let listener = () => {
    frame.port.emit('sdk/test/sawreply');
  }

  system.on("Test:Reply", listener);
  let event = new frame.content.CustomEvent("Test:Event");
  doc.dispatchEvent(event);
  system.off("Test:Reply", listener);
  frame.port.emit('sdk/test/eventsent');
});

process.port.on('sdk/test/parentload', () => {
  let loaded = false;
  let message = "";
  try {
    require('sdk/remote/parent');
    loaded = true;
  }
  catch (e) {
    message = "" + e;
  }

  process.port.emit('sdk/test/parentload',
    isChildLoader,
    loaded,
    message
  )
});

function listener(event) {
  // Use the raw observer service here since it will be usable even if the
  // loader has unloaded
  let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
  Services.obs.notifyObservers(null, "Test:Reply", "");
}

frames.port.on('sdk/test/registerframesevent', (frame) => {
  frames.addEventListener("Test:Event", listener, true);
});

frames.port.on('sdk/test/unregisterframesevent', (frame) => {
  frames.removeEventListener("Test:Event", listener, true);
});

frames.port.on('sdk/test/registerframeevent', (frame) => {
  frame.addEventListener("Test:Event", listener, true);
});

frames.port.on('sdk/test/unregisterframeevent', (frame) => {
  frame.removeEventListener("Test:Event", listener, true);
});

process.port.on('sdk/test/cpow', (process, arg, cpows) => {
  process.port.emit('sdk/test/cpow', arg, getOuterId(cpows.window));
});