This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
/
ASRouterTriggerListeners.jsm
138 lines (119 loc) · 4.6 KB
/
ASRouterTriggerListeners.jsm
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
/* 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 {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
/**
* A Map from trigger IDs to singleton trigger listeners. Each listener must
* have idempotent `init` and `uninit` methods.
*/
this.ASRouterTriggerListeners = new Map([
/**
* Attach listeners to every browser window to detect location changes, and
* notify the trigger handler whenever we navigate to a URL with a hostname
* we're looking for.
*/
["openURL", {
_initialized: false,
_triggerHandler: null,
_hosts: null,
/**
* Wait for browser startup to finish to avoid accessing uninitialized
* properties
*/
async _checkStartupFinished(win) {
if (!win.gBrowserInit.delayedStartupFinished) {
await new Promise(resolve => {
let delayedStartupObserver = (subject, topic) => {
if (topic === "browser-delayed-startup-finished" && subject === win) {
Services.obs.removeObserver(delayedStartupObserver, "browser-delayed-startup-finished");
resolve();
}
};
Services.obs.addObserver(delayedStartupObserver, "browser-delayed-startup-finished");
});
}
},
/*
* If the listener is already initialised, `init` will replace the trigger
* handler and add any new hosts to `this._hosts`.
*/
async init(triggerHandler, hosts = []) {
if (!this._initialized) {
this.onLocationChange = this.onLocationChange.bind(this);
// Listen for new windows being opened
Services.ww.registerNotification(this);
// Add listeners to all existing browser windows
for (let win of Services.wm.getEnumerator("navigator:browser")) {
if (win.closed || PrivateBrowsingUtils.isWindowPrivate(win)) {
continue;
}
await this._checkStartupFinished(win);
win.gBrowser.addTabsProgressListener(this);
}
this._initialized = true;
}
this._triggerHandler = triggerHandler;
if (this._hosts) {
hosts.forEach(h => this._hosts.add(h));
} else {
this._hosts = new Set(hosts); // Clone the hosts to avoid unexpected behaviour
}
},
uninit() {
if (this._initialized) {
Services.ww.unregisterNotification(this);
for (let win of Services.wm.getEnumerator("navigator:browser")) {
if (win.closed || PrivateBrowsingUtils.isWindowPrivate(win)) {
continue;
}
win.gBrowser.removeTabsProgressListener(this);
}
this._initialized = false;
this._triggerHandler = null;
this._hosts = null;
}
},
onLocationChange(aBrowser, aWebProgress, aRequest, aLocationURI, aFlags) {
const location = aLocationURI ? aLocationURI.spec : "";
// Some websites trigger redirect events after they finish loading even
// though the location remains the same. This results in onLocationChange
// events to be fired twice.
const isSameDocument = !!(aFlags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT);
if (location && aWebProgress.isTopLevel && !isSameDocument) {
try {
const host = (new URL(location)).hostname;
if (this._hosts.has(host)) {
this._triggerHandler(aBrowser, {id: "openURL", param: host});
}
} catch (e) {} // Couldn't parse location URL
}
},
observe(win, topic, data) {
let onLoad;
switch (topic) {
case "domwindowopened":
if (!(win instanceof Ci.nsIDOMWindow) || win.closed || PrivateBrowsingUtils.isWindowPrivate(win)) {
break;
}
onLoad = () => {
// Ignore non-browser windows.
if (win.document.documentElement.getAttribute("windowtype") === "navigator:browser") {
win.gBrowser.addTabsProgressListener(this);
}
};
win.addEventListener("load", onLoad, {once: true});
break;
case "domwindowclosed":
if ((win instanceof Ci.nsIDOMWindow) &&
win.document.documentElement.getAttribute("windowtype") === "navigator:browser") {
win.gBrowser.removeTabsProgressListener(this);
}
break;
}
},
}],
]);
const EXPORTED_SYMBOLS = ["ASRouterTriggerListeners"];