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
|
/* 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 = {};
ChromeUtils.defineESModuleGetters(lazy, {
AboutHomeStartupCache: "resource:///modules/BrowserGlue.sys.mjs",
AboutNewTabParent: "resource:///actors/AboutNewTabParent.sys.mjs",
});
const {
actionCreators: ac,
actionTypes: at,
actionUtils: au,
} = ChromeUtils.importESModule(
"resource://activity-stream/common/Actions.sys.mjs"
);
const ABOUT_NEW_TAB_URL = "about:newtab";
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 the new tab page actor.
* 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 the channel is attached, such as about:newtab.
* @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.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);
}
/**
* Get an iterator over the loaded tab objects.
*/
get loadedTabs() {
// In the test, AboutNewTabParent is not defined.
return lazy.AboutNewTabParent?.loadedTabs || new Map();
}
/**
* 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 (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();
for (let { actor } of this.loadedTabs.values()) {
try {
actor.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The target page is closed/closing by the user or test, so just ignore.
}
}
}
/**
* 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 a port number.
* It is generated in AboutNewTabChild.sys.mjs.
*/
validatePortID(id) {
if (typeof id !== "string" || !id.includes(":")) {
console.error("Invalid portID");
}
return id;
}
/**
* getTargetById - Retrieve the message target by portID, if it exists
*
* @param {string} id A portID
* @return {obj|null} The message target, if it exists.
*/
getTargetById(id) {
this.validatePortID(id);
for (let { portID, actor } of this.loadedTabs.values()) {
if (portID === id) {
return actor;
}
}
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 preloadedActors = this.getPreloadedActors();
if (preloadedActors && action.data) {
for (let preloadedActor of preloadedActors) {
try {
preloadedActor.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The preloaded page is no longer available, so just ignore.
}
}
}
}
/**
* getPreloadedActors - Retrieve the preloaded actors
*
* @return {Array|null} An array of actors belonging to the preloaded browsers, or null
* if there aren't any preloaded browsers
*/
getPreloadedActors() {
let preloadedActors = [];
for (let { actor, browser } of this.loadedTabs.values()) {
if (this.isPreloadedBrowser(browser)) {
preloadedActors.push(actor);
}
}
return preloadedActors.length ? preloadedActors : 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";
}
simulateMessagesForExistingTabs() {
// Some pages might have already loaded, so we won't get the usual message
for (const loadedTab of this.loadedTabs.values()) {
let simulatedDetails = {
actor: loadedTab.actor,
browser: loadedTab.browser,
browsingContext: loadedTab.browsingContext,
portID: loadedTab.portID,
url: loadedTab.url,
simulated: true,
};
this.onActionFromContent(
{
type: at.NEW_TAB_INIT,
data: simulatedDetails,
},
loadedTab.portID
);
if (loadedTab.loaded) {
this.tabLoaded(simulatedDetails);
}
}
}
/**
* onNewTabInit - Handler for special RemotePage:Init message fired
* on initialization.
*
* @param {obj} msg The messsage from a page that was just initialized
* @param {obj} tabDetails details about a loaded tab
*
* tabDetails contains:
* actor, browser, browsingContext, portID, url
*/
onNewTabInit(msg, tabDetails) {
this.onActionFromContent(
{
type: at.NEW_TAB_INIT,
data: tabDetails,
},
msg.data.portID
);
}
/**
* onNewTabLoad - Handler for special RemotePage:Load message fired on page load.
*
* @param {obj} msg The messsage from a page that was just loaded
* @param {obj} tabDetails details about a loaded tab, similar to onNewTabInit
*/
onNewTabLoad(msg, tabDetails) {
this.tabLoaded(tabDetails);
}
tabLoaded(tabDetails) {
tabDetails.loaded = true;
let { browser } = tabDetails;
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 }, tabDetails.portID);
}
/**
* onNewTabUnloadLoad - Handler for special RemotePage:Unload message fired
* on page unload.
*
* @param {obj} msg The messsage from a page that was just unloaded
* @param {obj} tabDetails details about a loaded tab, similar to onNewTabInit
*/
onNewTabUnload(msg, tabDetails) {
this.onActionFromContent({ type: at.NEW_TAB_UNLOAD }, tabDetails.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
* @param {obj} tabDetails details about a loaded tab, similar to onNewTabInit
*/
onMessage(msg, tabDetails) {
if (!msg.data || !msg.data.type) {
console.error(
new Error(
`Received an improperly formatted message from ${tabDetails.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 = {
browser: tabDetails.browser,
};
this.onActionFromContent(action, tabDetails.portID);
}
}
const EXPORTED_SYMBOLS = ["ActivityStreamMessageChannel", "DEFAULT_OPTIONS"];
|