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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* 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";
ChromeUtils.defineESModuleGetters(this, {
GeckoViewTabBridge: "resource://gre/modules/GeckoViewTab.sys.mjs",
mobileWindowTracker: "resource://gre/modules/GeckoViewWebExtension.sys.mjs",
});
const getBrowserWindow = window => {
return window.browsingContext.topChromeWindow;
};
const tabListener = {
tabReadyInitialized: false,
tabReadyPromises: new WeakMap(),
initializingTabs: new WeakSet(),
initTabReady() {
if (!this.tabReadyInitialized) {
windowTracker.addListener("progress", this);
this.tabReadyInitialized = true;
}
},
onLocationChange(browser, webProgress, request, locationURI, flags) {
if (webProgress.isTopLevel) {
const { tab } = browser.ownerGlobal;
// Ignore initial about:blank
if (!request && this.initializingTabs.has(tab)) {
return;
}
// Now we are certain that the first page in the tab was loaded.
this.initializingTabs.delete(tab);
// browser.innerWindowID is now set, resolve the promises if any.
const deferred = this.tabReadyPromises.get(tab);
if (deferred) {
deferred.resolve(tab);
this.tabReadyPromises.delete(tab);
}
}
},
/**
* Returns a promise that resolves when the tab is ready.
* Tabs created via the `tabs.create` method are "ready" once the location
* changes to the requested URL. Other tabs are assumed to be ready once their
* inner window ID is known.
*
* @param {NativeTab} nativeTab The native tab object.
* @returns {Promise} Resolves with the given tab once ready.
*/
awaitTabReady(nativeTab) {
let deferred = this.tabReadyPromises.get(nativeTab);
if (!deferred) {
deferred = Promise.withResolvers();
if (
!this.initializingTabs.has(nativeTab) &&
(nativeTab.browser.innerWindowID ||
nativeTab.browser.currentURI.spec === "about:blank")
) {
deferred.resolve(nativeTab);
} else {
this.initTabReady();
this.tabReadyPromises.set(nativeTab, deferred);
}
}
return deferred.promise;
},
};
this.tabs = class extends ExtensionAPIPersistent {
tabEventRegistrar({ event, listener }) {
const { extension } = this;
const { tabManager } = extension;
return ({ fire }) => {
const listener2 = (eventName, eventData, ...args) => {
if (!tabManager.canAccessTab(eventData.nativeTab)) {
return;
}
listener(fire, eventData, ...args);
};
tabTracker.on(event, listener2);
return {
unregister() {
tabTracker.off(event, listener2);
},
convert(_fire) {
fire = _fire;
},
};
};
}
PERSISTENT_EVENTS = {
onActivated({ fire, context }, params) {
const listener = (eventName, event) => {
const { windowId, tabId, isPrivate } = event;
if (isPrivate && !context.privateBrowsingAllowed) {
return;
}
// In GeckoView each window has only one tab, so previousTabId is omitted.
fire.async({ windowId, tabId });
};
mobileWindowTracker.on("tab-activated", listener);
return {
unregister() {
mobileWindowTracker.off("tab-activated", listener);
},
convert(_fire, _context) {
fire = _fire;
context = _context;
},
};
},
onCreated: this.tabEventRegistrar({
event: "tab-created",
listener: (fire, event) => {
const { tabManager } = this.extension;
fire.async(tabManager.convert(event.nativeTab));
},
}),
onRemoved: this.tabEventRegistrar({
event: "tab-removed",
listener: (fire, event) => {
fire.async(event.tabId, {
windowId: event.windowId,
isWindowClosing: event.isWindowClosing,
});
},
}),
onUpdated({ fire }, params) {
const { tabManager } = this.extension;
const restricted = ["url", "favIconUrl", "title"];
function sanitize(tab, changeInfo) {
const result = {};
let nonempty = false;
for (const prop in changeInfo) {
// In practice, changeInfo contains at most one property from
// restricted. Therefore it is not necessary to cache the value
// of tab.hasTabPermission outside the loop.
if (!restricted.includes(prop) || tab.hasTabPermission) {
nonempty = true;
result[prop] = changeInfo[prop];
}
}
return [nonempty, result];
}
const fireForTab = (tab, changed) => {
const [needed, changeInfo] = sanitize(tab, changed);
if (needed) {
fire.async(tab.id, changeInfo, tab.convert());
}
};
const listener = event => {
const needed = [];
let nativeTab;
switch (event.type) {
case "pagetitlechanged": {
const window = getBrowserWindow(event.target.ownerGlobal);
nativeTab = window.tab;
needed.push("title");
break;
}
case "DOMAudioPlaybackStarted":
case "DOMAudioPlaybackStopped": {
const window = event.target.ownerGlobal;
nativeTab = window.tab;
needed.push("audible");
break;
}
}
if (!nativeTab) {
return;
}
const tab = tabManager.getWrapper(nativeTab);
const changeInfo = {};
for (const prop of needed) {
changeInfo[prop] = tab[prop];
}
fireForTab(tab, changeInfo);
};
const statusListener = ({ browser, status, url }) => {
const { tab } = browser.ownerGlobal;
if (tab) {
const changed = { status };
if (url) {
changed.url = url;
}
fireForTab(tabManager.wrapTab(tab), changed);
}
};
windowTracker.addListener("status", statusListener);
windowTracker.addListener("pagetitlechanged", listener);
return {
unregister() {
windowTracker.removeListener("status", statusListener);
windowTracker.removeListener("pagetitlechanged", listener);
},
convert(_fire) {
fire = _fire;
},
};
},
};
getAPI(context) {
const { extension } = context;
const { tabManager } = extension;
const extensionApi = this;
const module = "tabs";
function getTabOrActive(tabId) {
if (tabId !== null) {
return tabTracker.getTab(tabId);
}
return tabTracker.activeTab;
}
async function promiseTabWhenReady(tabId) {
let tab;
if (tabId !== null) {
tab = tabManager.get(tabId);
} else {
tab = tabManager.getWrapper(tabTracker.activeTab);
}
if (!tab) {
throw new ExtensionError(
tabId == null ? "Cannot access activeTab" : `Invalid tab ID: ${tabId}`
);
}
await tabListener.awaitTabReady(tab.nativeTab);
return tab;
}
function loadURIInTab(nativeTab, url) {
const { browser } = nativeTab;
let flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
let { principal } = context;
const isAboutUrl = url.startsWith("about:");
if (
isAboutUrl ||
(url.startsWith("moz-extension://") &&
!context.checkLoadURL(url, { dontReportErrors: true }))
) {
// Falling back to content here as about: requires it, however is safe.
principal =
Services.scriptSecurityManager.getLoadContextContentPrincipal(
Services.io.newURI(url),
browser.loadContext
);
}
if (isAboutUrl) {
// Make sure things like about:blank and other about: URIs never
// inherit, and instead always get a NullPrincipal.
flags |= Ci.nsIWebNavigation.LOAD_FLAGS_DISALLOW_INHERIT_PRINCIPAL;
}
browser.fixupAndLoadURIString(url, {
flags,
triggeringPrincipal: principal,
});
}
return {
tabs: {
onActivated: new EventManager({
context,
module,
event: "onActivated",
extensionApi,
}).api(),
onCreated: new EventManager({
context,
module,
event: "onCreated",
extensionApi,
}).api(),
/**
* Since multiple tabs currently can't be highlighted, onHighlighted
* essentially acts an alias for tabs.onActivated but returns
* the tabId in an array to match the API.
*
* @see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/Tabs/onHighlighted
*/
onHighlighted: makeGlobalEvent(
context,
"tabs.onHighlighted",
"Tab:Selected",
(fire, data) => {
const tab = tabManager.get(data.id);
fire.async({ tabIds: [tab.id], windowId: tab.windowId });
}
),
// Some events below are not be persisted because they are not implemented.
// They do not have an "extensionApi" property with an entry in
// PERSISTENT_EVENTS, but instead an empty "register" method.
onAttached: new EventManager({
context,
name: "tabs.onAttached",
register: fire => {
return () => {};
},
}).api(),
onDetached: new EventManager({
context,
name: "tabs.onDetached",
register: fire => {
return () => {};
},
}).api(),
onRemoved: new EventManager({
context,
module,
event: "onRemoved",
extensionApi,
}).api(),
onReplaced: new EventManager({
context,
name: "tabs.onReplaced",
register: fire => {
return () => {};
},
}).api(),
onMoved: new EventManager({
context,
name: "tabs.onMoved",
register: fire => {
return () => {};
},
}).api(),
onUpdated: new EventManager({
context,
module,
event: "onUpdated",
extensionApi,
}).api(),
async create({
active,
cookieStoreId,
discarded,
index,
openInReaderMode,
pinned,
title,
url,
} = {}) {
if (active === null) {
active = true;
}
tabListener.initTabReady();
if (url !== null) {
url = context.uri.resolve(url);
if (
!url.startsWith("moz-extension://") &&
!context.checkLoadURL(url, { dontReportErrors: true })
) {
return Promise.reject({ message: `Illegal URL: ${url}` });
}
}
if (cookieStoreId) {
cookieStoreId = getUserContextIdForCookieStoreId(
extension,
cookieStoreId,
false // TODO bug 1372178: support creation of private browsing tabs
);
}
cookieStoreId = cookieStoreId ? cookieStoreId.toString() : undefined;
const nativeTab = await GeckoViewTabBridge.createNewTab({
extensionId: context.extension.id,
createProperties: {
active,
cookieStoreId,
discarded,
index,
openInReaderMode,
pinned,
url,
},
});
// Make sure things like about:blank URIs never inherit,
// and instead always get a NullPrincipal.
if (url !== null) {
tabListener.initializingTabs.add(nativeTab);
} else {
url = "about:blank";
}
loadURIInTab(nativeTab, url);
if (active) {
const newWindow = nativeTab.browser.ownerGlobal;
mobileWindowTracker.setTabActive(newWindow, true);
}
return tabManager.convert(nativeTab);
},
async remove(tabs) {
if (!Array.isArray(tabs)) {
tabs = [tabs];
}
await Promise.all(
tabs.map(async tabId => {
const windowId = GeckoViewTabBridge.tabIdToWindowId(tabId);
const window = windowTracker.getWindow(windowId, context, false);
if (!window) {
throw new ExtensionError(`Invalid tab ID ${tabId}`);
}
await GeckoViewTabBridge.closeTab({
window,
extensionId: context.extension.id,
});
})
);
},
async update(
tabId,
{ active, autoDiscardable, highlighted, muted, pinned, url } = {}
) {
const nativeTab = getTabOrActive(tabId);
const window = nativeTab.browser.ownerGlobal;
if (url !== null) {
url = context.uri.resolve(url);
if (
!url.startsWith("moz-extension://") &&
!context.checkLoadURL(url, { dontReportErrors: true })
) {
return Promise.reject({ message: `Illegal URL: ${url}` });
}
}
await GeckoViewTabBridge.updateTab({
window,
extensionId: context.extension.id,
updateProperties: {
active,
autoDiscardable,
highlighted,
muted,
pinned,
url,
},
});
if (url !== null) {
loadURIInTab(nativeTab, url);
}
// FIXME: openerTabId, successorTabId
if (active) {
mobileWindowTracker.setTabActive(window, true);
}
return tabManager.convert(nativeTab);
},
async reload(tabId, reloadProperties) {
const nativeTab = getTabOrActive(tabId);
let flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
if (reloadProperties && reloadProperties.bypassCache) {
flags |= Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
}
nativeTab.browser.reloadWithFlags(flags);
},
async get(tabId) {
return tabManager.get(tabId).convert();
},
async getCurrent() {
if (context.tabId) {
return tabManager.get(context.tabId).convert();
}
},
async query(queryInfo) {
return Array.from(tabManager.query(queryInfo, context), tab =>
tab.convert()
);
},
async captureTab(tabId, options) {
const nativeTab = getTabOrActive(tabId);
await tabListener.awaitTabReady(nativeTab);
const { browser } = nativeTab;
const tab = tabManager.wrapTab(nativeTab);
return tab.capture(context, browser.fullZoom, options);
},
async captureVisibleTab(windowId, options) {
const window =
windowId == null
? windowTracker.topWindow
: windowTracker.getWindow(windowId, context);
const tab = tabManager.wrapTab(window.tab);
await tabListener.awaitTabReady(tab.nativeTab);
const zoom = window.browsingContext.fullZoom;
return tab.capture(context, zoom, options);
},
async detectLanguage(tabId) {
const tab = await promiseTabWhenReady(tabId);
const results = await tab.queryContent("DetectLanguage", {});
return results[0];
},
async executeScript(tabId, details) {
const tab = await promiseTabWhenReady(tabId);
return tab.executeScript(context, details);
},
async insertCSS(tabId, details) {
const tab = await promiseTabWhenReady(tabId);
return tab.insertCSS(context, details);
},
async removeCSS(tabId, details) {
const tab = await promiseTabWhenReady(tabId);
return tab.removeCSS(context, details);
},
goForward(tabId) {
const { browser } = getTabOrActive(tabId);
browser.goForward();
},
goBack(tabId) {
const { browser } = getTabOrActive(tabId);
browser.goBack();
},
},
};
}
};
|