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
|
/* 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/. */
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
AboutNewTab: "resource:///modules/AboutNewTab.jsm",
ASRouter: "resource://activity-stream/lib/ASRouter.jsm",
});
// A mapping of loaded new tab pages, where the mapping is:
// browser -> { actor, browser, browsingContext, portID, url, loaded }
let gLoadedTabs = new Map();
export class AboutNewTabParent extends JSWindowActorParent {
static get loadedTabs() {
return gLoadedTabs;
}
getTabDetails(message) {
let browsingContext = message.target.browsingContext;
let browser = browsingContext.top.embedderElement;
return browser ? gLoadedTabs.get(browser) : null;
}
handleEvent(event) {
if (event.type == "SwapDocShells") {
let oldBrowser = this.browsingContext.top.embedderElement;
let newBrowser = event.detail;
let tabDetails = gLoadedTabs.get(oldBrowser);
if (tabDetails) {
tabDetails.browser = newBrowser;
gLoadedTabs.delete(oldBrowser);
gLoadedTabs.set(newBrowser, tabDetails);
oldBrowser.removeEventListener("SwapDocShells", this);
newBrowser.addEventListener("SwapDocShells", this);
}
}
}
async receiveMessage(message) {
switch (message.name) {
case "AboutNewTabVisible":
await lazy.ASRouter.waitForInitialized;
lazy.ASRouter.sendTriggerMessage({
browser: this.browsingContext.top.embedderElement,
// triggerId and triggerContext
id: "defaultBrowserCheck",
context: { source: "newtab" },
});
break;
case "Init": {
let actor = message.target;
let browsingContext = actor.browsingContext;
let browser = browsingContext.top.embedderElement;
if (!browser) {
return;
}
let tabDetails = {
actor,
browser,
browsingContext,
portID: message.data.portID,
url: message.data.url,
};
gLoadedTabs.set(browser, tabDetails);
browser.addEventListener("SwapDocShells", this);
browser.addEventListener("EndSwapDocShells", this);
this.notifyActivityStreamChannel("onNewTabInit", message, tabDetails);
break;
}
case "Load":
this.notifyActivityStreamChannel("onNewTabLoad", message);
break;
case "Unload": {
let tabDetails = this.getTabDetails(message);
if (!tabDetails) {
// When closing a tab, the embedderElement can already be disconnected, so
// an a backup, look up the tab details by browsing context.
tabDetails = this.getByBrowsingContext(
message.target.browsingContext
);
}
if (!tabDetails) {
return;
}
tabDetails.browser.removeEventListener("EndSwapDocShells", this);
gLoadedTabs.delete(tabDetails.browser);
this.notifyActivityStreamChannel("onNewTabUnload", message, tabDetails);
break;
}
case "ActivityStream:ContentToMain":
this.notifyActivityStreamChannel("onMessage", message);
break;
}
}
notifyActivityStreamChannel(name, message, tabDetails) {
if (!tabDetails) {
tabDetails = this.getTabDetails(message);
if (!tabDetails) {
return;
}
}
let channel = this.getChannel();
if (!channel) {
return;
}
let messageToSend = {
target: message.target,
data: message.data || {},
};
channel[name](messageToSend, tabDetails);
}
getByBrowsingContext(expectedBrowsingContext) {
for (let tabDetails of AboutNewTabParent.loadedTabs.values()) {
if (tabDetails.browsingContext === expectedBrowsingContext) {
return tabDetails;
}
}
return null;
}
getChannel() {
return lazy.AboutNewTab.activityStream?.store?.getMessageChannel();
}
}
|