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
|
/* 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 lazy = {};
// TODO delete this?
ChromeUtils.defineModuleGetter(
lazy,
"AboutNewTab",
"resource:///modules/AboutNewTab.jsm"
);
ChromeUtils.defineESModuleGetters(lazy, {
AboutHomeStartupCache: "resource:///modules/BrowserGlue.sys.mjs",
});
const { RemotePages } = ChromeUtils.import(
"resource://gre/modules/remotepagemanager/RemotePageManagerParent.jsm"
);
const {
actionCreators: ac,
actionTypes: at,
actionUtils: au,
} = ChromeUtils.importESModule(
"resource://activity-stream/common/Actions.sys.mjs"
);
const ABOUT_NEW_TAB_URL = "about:newtab";
const ABOUT_HOME_URL = "about:home";
const DEFAULT_OPTIONS = {
dispatch(action) {
throw new Error(
`\nMessageChannel: Received action ${action.type}, but no dispatcher was defined.\n`
);
},
pageURL: ABOUT_NEW_TAB_URL,
outgoingMessageName: "ActivityStream:MainToContent",
incomingMessageName: "ActivityStream:ContentToMain",
};
class ActivityStreamMessageChannel {
/**
* ActivityStreamMessageChannel - This module connects a Redux store to a RemotePageManager in Firefox.
* Call .createChannel to start the connection, and .destroyChannel to destroy it.
* You should use the BroadcastToContent, AlsoToOneContent, and AlsoToMain action creators
* in common/Actions.sys.mjs to help you create actions that will be automatically routed
* to the correct location.
*
* @param {object} options
* @param {function} options.dispatch The dispatch method from a Redux store
* @param {string} options.pageURL The URL to which a RemotePageManager should be attached.
* Note that if it is about:newtab, the existing RemotePageManager
* for about:newtab will also be disabled
* @param {string} options.outgoingMessageName The name of the message sent to child processes
* @param {string} options.incomingMessageName The name of the message received from child processes
* @return {ActivityStreamMessageChannel}
*/
constructor(options = {}) {
Object.assign(this, DEFAULT_OPTIONS, options);
this.channel = null;
this.middleware = this.middleware.bind(this);
this.onMessage = this.onMessage.bind(this);
this.onNewTabLoad = this.onNewTabLoad.bind(this);
this.onNewTabUnload = this.onNewTabUnload.bind(this);
this.onNewTabInit = this.onNewTabInit.bind(this);
}
/**
* middleware - Redux middleware that looks for AlsoToOneContent and BroadcastToContent type
* actions, and sends them out.
*
* @param {object} store A redux store
* @return {function} Redux middleware
*/
middleware(store) {
return next => action => {
const skipMain = action.meta && action.meta.skipMain;
if (!this.channel && !skipMain) {
next(action);
return;
}
if (au.isSendToOneContent(action)) {
this.send(action);
} else if (au.isBroadcastToContent(action)) {
this.broadcast(action);
} else if (au.isSendToPreloaded(action)) {
this.sendToPreloaded(action);
}
if (!skipMain) {
next(action);
}
};
}
/**
* onActionFromContent - Handler for actions from a content processes
*
* @param {object} action A Redux action
* @param {string} targetId The portID of the port that sent the message
*/
onActionFromContent(action, targetId) {
this.dispatch(ac.AlsoToMain(action, this.validatePortID(targetId)));
}
/**
* broadcast - Sends an action to all ports
*
* @param {object} action A Redux action
*/
broadcast(action) {
// We're trying to update all tabs, so signal the AboutHomeStartupCache
// that its likely time to refresh the cache.
lazy.AboutHomeStartupCache.onPreloadedNewTabMessage();
this.channel.sendAsyncMessage(this.outgoingMessageName, action);
}
/**
* send - Sends an action to a specific port
*
* @param {obj} action A redux action; it should contain a portID in the meta.toTarget property
*/
send(action) {
const targetId = action.meta && action.meta.toTarget;
const target = this.getTargetById(targetId);
try {
target.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The target page is closed/closing by the user or test, so just ignore.
}
}
/**
* A valid portID is a combination of process id and port
* https://searchfox.org/mozilla-central/rev/196560b95f191b48ff7cba7c2ba9237bba6b5b6a/toolkit/components/remotepagemanager/RemotePageManagerChild.jsm#14
*/
validatePortID(id) {
if (typeof id !== "string" || !id.includes(":")) {
console.error("Invalid portID");
}
return id;
}
/**
* getIdByTarget - Retrieve the id of a message target, if it exists in this.targets
*
* @param {obj} targetObj A message target
* @return {string|null} The unique id of the target, if it exists.
*/
getTargetById(id) {
this.validatePortID(id);
for (let port of this.channel.messagePorts) {
if (port.portID === id) {
return port;
}
}
return null;
}
/**
* sendToPreloaded - Sends an action to each preloaded browser, if any
*
* @param {obj} action A redux action
*/
sendToPreloaded(action) {
// We're trying to update the preloaded about:newtab, so signal
// the AboutHomeStartupCache that its likely time to refresh
// the cache.
lazy.AboutHomeStartupCache.onPreloadedNewTabMessage();
const preloadedBrowsers = this.getPreloadedBrowser();
if (preloadedBrowsers && action.data) {
for (let preloadedBrowser of preloadedBrowsers) {
try {
preloadedBrowser.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The preloaded page is no longer available, so just ignore.
}
}
}
}
/**
* getPreloadedBrowser - Retrieve the port of any preloaded browsers
*
* @return {Array|null} An array of ports belonging to the preloaded browsers, or null
* if there aren't any preloaded browsers
*/
getPreloadedBrowser() {
let preloadedPorts = [];
for (let port of this.channel.messagePorts) {
if (this.isPreloadedBrowser(port.browser)) {
preloadedPorts.push(port);
}
}
return preloadedPorts.length ? preloadedPorts : null;
}
/**
* isPreloadedBrowser - Returns true if the passed browser has been preloaded
* for faster rendering of new tabs.
*
* @param {<browser>} A <browser> to check.
* @return {bool} True if the browser is preloaded.
* if there aren't any preloaded browsers
*/
isPreloadedBrowser(browser) {
return browser.getAttribute("preloadedState") === "preloaded";
}
/**
* createChannel - Create RemotePages channel to establishing message passing
* between the main process and child pages
*/
createChannel() {
// Receive AboutNewTab's Remote Pages instance, if it exists, on override
const channel =
this.pageURL === ABOUT_NEW_TAB_URL &&
lazy.AboutNewTab.overridePageListener(true);
this.channel =
channel || new RemotePages([ABOUT_HOME_URL, ABOUT_NEW_TAB_URL]);
this.channel.addMessageListener("RemotePage:Init", this.onNewTabInit);
this.channel.addMessageListener("RemotePage:Load", this.onNewTabLoad);
this.channel.addMessageListener("RemotePage:Unload", this.onNewTabUnload);
this.channel.addMessageListener(this.incomingMessageName, this.onMessage);
}
simulateMessagesForExistingTabs() {
// Some pages might have already loaded, so we won't get the usual message
for (const target of this.channel.messagePorts) {
const simulatedMsg = {
target: Object.assign({ simulated: true }, target),
};
this.onNewTabInit(simulatedMsg);
if (target.loaded) {
this.onNewTabLoad(simulatedMsg);
}
}
}
/**
* destroyChannel - Destroys the RemotePages channel
*/
destroyChannel() {
this.channel.removeMessageListener("RemotePage:Init", this.onNewTabInit);
this.channel.removeMessageListener("RemotePage:Load", this.onNewTabLoad);
this.channel.removeMessageListener(
"RemotePage:Unload",
this.onNewTabUnload
);
this.channel.removeMessageListener(
this.incomingMessageName,
this.onMessage
);
if (this.pageURL === ABOUT_NEW_TAB_URL) {
lazy.AboutNewTab.reset(this.channel);
} else {
this.channel.destroy();
}
this.channel = null;
}
/**
* onNewTabInit - Handler for special RemotePage:Init message fired
* by RemotePages
*
* @param {obj} msg The messsage from a page that was just initialized
*/
onNewTabInit(msg) {
this.onActionFromContent(
{
type: at.NEW_TAB_INIT,
data: msg.target,
},
msg.target.portID
);
}
/**
* onNewTabLoad - Handler for special RemotePage:Load message fired by RemotePages
*
* @param {obj} msg The messsage from a page that was just loaded
*/
onNewTabLoad(msg) {
let { browser } = msg.target;
if (
this.isPreloadedBrowser(browser) &&
browser.ownerGlobal.windowState !== browser.ownerGlobal.STATE_MINIMIZED &&
!browser.ownerGlobal.isFullyOccluded
) {
// As a perceived performance optimization, if this loaded Activity Stream
// happens to be a preloaded browser in a window that is not minimized or
// occluded, have it render its layers to the compositor now to increase
// the odds that by the time we switch to the tab, the layers are already
// ready to present to the user.
browser.renderLayers = true;
}
this.onActionFromContent({ type: at.NEW_TAB_LOAD }, msg.target.portID);
}
/**
* onNewTabUnloadLoad - Handler for special RemotePage:Unload message fired by RemotePages
*
* @param {obj} msg The messsage from a page that was just unloaded
*/
onNewTabUnload(msg) {
this.onActionFromContent({ type: at.NEW_TAB_UNLOAD }, msg.target.portID);
}
/**
* onMessage - Handles custom messages from content. It expects all messages to
* be formatted as Redux actions, and dispatches them to this.store
*
* @param {obj} msg A custom message from content
* @param {obj} msg.action A Redux action (e.g. {type: "HELLO_WORLD"})
* @param {obj} msg.target A message target
*/
onMessage(msg) {
const { portID } = msg.target;
if (!msg.data || !msg.data.type) {
console.error(
new Error(`Received an improperly formatted message from ${portID}`)
);
return;
}
let action = {};
Object.assign(action, msg.data);
// target is used to access a browser reference that came from the content
// and should only be used in feeds (not reducers)
action._target = msg.target;
this.onActionFromContent(action, portID);
}
}
const EXPORTED_SYMBOLS = ["ActivityStreamMessageChannel", "DEFAULT_OPTIONS"];
|