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
|
/* 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";
var { ExtensionParent } = ChromeUtils.import(
"resource://gre/modules/ExtensionParent.jsm"
);
var { HiddenExtensionPage, promiseExtensionViewLoaded } = ExtensionParent;
ChromeUtils.defineModuleGetter(
this,
"ExtensionTelemetry",
"resource://gre/modules/ExtensionTelemetry.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm"
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
"DELAYED_STARTUP",
"extensions.webextensions.background-delayed-startup"
);
XPCOMUtils.defineLazyGetter(this, "serviceWorkerManager", () => {
return Cc["@mozilla.org/serviceworkers/manager;1"].getService(
Ci.nsIServiceWorkerManager
);
});
// Responsible for the background_page section of the manifest.
class BackgroundPage extends HiddenExtensionPage {
constructor(extension, options) {
super(extension, "background");
this.page = options.page || null;
this.isGenerated = !!options.scripts;
if (this.page) {
this.url = this.extension.baseURI.resolve(this.page);
} else if (this.isGenerated) {
this.url = this.extension.baseURI.resolve(
"_generated_background_page.html"
);
}
}
async build() {
const { extension } = this;
ExtensionTelemetry.backgroundPageLoad.stopwatchStart(extension, this);
let context;
try {
await this.createBrowserElement();
if (!this.browser) {
throw new Error(
"Extension shut down before the background page was created"
);
}
extension._backgroundPageFrameLoader = this.browser.frameLoader;
extensions.emit("extension-browser-inserted", this.browser);
let contextPromise = promiseExtensionViewLoaded(this.browser);
this.browser.loadURI(this.url, {
triggeringPrincipal: extension.principal,
});
context = await contextPromise;
} catch (e) {
// Extension was down before the background page has loaded.
Cu.reportError(e);
ExtensionTelemetry.backgroundPageLoad.stopwatchCancel(extension, this);
if (extension.persistentListeners) {
EventManager.clearPrimedListeners(this.extension, false);
}
extension.emit("background-page-aborted");
return;
}
ExtensionTelemetry.backgroundPageLoad.stopwatchFinish(extension, this);
if (context) {
// Wait until all event listeners registered by the script so far
// to be handled.
await Promise.all(context.listenerPromises);
context.listenerPromises = null;
}
if (extension.persistentListeners) {
// |this.extension| may be null if the extension was shut down.
// In that case, we still want to clear the primed listeners,
// but not update the persistent listeners in the startupData.
EventManager.clearPrimedListeners(extension, !!this.extension);
}
extension.emit("background-page-started");
}
shutdown() {
this.extension._backgroundPageFrameLoader = null;
super.shutdown();
}
}
// Responsible for the background.service_worker section of the manifest.
class BackgroundWorker {
constructor(extension, options) {
this.registrationInfo = null;
this.extension = extension;
this.workerScript = options.service_worker;
if (!this.workerScript) {
throw new Error("Missing mandatory background.service_worker property");
}
}
async build() {
const regInfo = await serviceWorkerManager.registerForAddonPrincipal(
this.extension.principal
);
this.registrationInfo = regInfo.QueryInterface(
Ci.nsIServiceWorkerRegistrationInfo
);
}
shutdown() {
if (this.registrationInfo) {
this.registrationInfo.forceShutdown();
this.registrationInfo = null;
}
}
}
this.backgroundPage = class extends ExtensionAPI {
async build() {
if (this.bgInstance) {
return;
}
let { extension } = this;
let { manifest } = extension;
let BackgroundClass = manifest.background.service_worker
? BackgroundWorker
: BackgroundPage;
this.bgInstance = new BackgroundClass(extension, manifest.background);
return this.bgInstance.build();
}
onManifestEntry(entryName) {
let { extension } = this;
this.bgInstance = null;
// When in PPB background pages all run in a private context. This check
// simply avoids an extraneous error in the console since the BaseContext
// will throw.
if (
PrivateBrowsingUtils.permanentPrivateBrowsing &&
!extension.privateBrowsingAllowed
) {
return;
}
// Used by runtime messaging to wait for background page listeners.
let bgStartupPromise = new Promise(resolve => {
let done = () => {
extension.off("background-page-started", done);
extension.off("background-page-aborted", done);
extension.off("shutdown", done);
resolve();
};
extension.on("background-page-started", done);
extension.on("background-page-aborted", done);
extension.on("shutdown", done);
});
extension.wakeupBackground = () => {
extension.emit("background-page-event");
extension.wakeupBackground = () => bgStartupPromise;
return bgStartupPromise;
};
if (extension.startupReason !== "APP_STARTUP" || !DELAYED_STARTUP) {
return this.build();
}
EventManager.primeListeners(extension);
extension.once("start-background-page", async () => {
if (!this.extension) {
// Extension was shut down. Don't build the background page.
// Primed listeners have been cleared in onShutdown.
return;
}
await this.build();
});
// There are two ways to start the background page:
// 1. If a primed event fires, then start the background page as
// soon as we have painted a browser window. Note that we have
// to touch browserPaintedPromise here to initialize the listener
// or else we can miss it if the event occurs after the first
// window is painted but before #2
// 2. After all windows have been restored.
extension.once("background-page-event", async () => {
await ExtensionParent.browserPaintedPromise;
extension.emit("start-background-page");
});
ExtensionParent.browserStartupPromise.then(() => {
extension.emit("start-background-page");
});
}
onShutdown() {
if (this.bgInstance) {
this.bgInstance.shutdown();
this.bgInstance = null;
} else {
EventManager.clearPrimedListeners(this.extension, false);
}
}
};
|