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
|
"use strict";
/* globals docShell */
var Ci = Components.interfaces;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "WebNavigationFrames",
"resource://gre/modules/WebNavigationFrames.jsm");
function loadListener(event) {
let document = event.target;
let window = document.defaultView;
let url = document.documentURI;
let windowId = WebNavigationFrames.getWindowId(window);
let parentWindowId = WebNavigationFrames.getParentWindowId(window);
sendAsyncMessage("Extension:DOMContentLoaded", {windowId, parentWindowId, url});
}
addEventListener("DOMContentLoaded", loadListener);
addMessageListener("Extension:DisableWebNavigation", () => {
removeEventListener("DOMContentLoaded", loadListener);
});
var FormSubmitListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
Ci.nsIFormSubmitObserver,
Ci.nsISupportsWeakReference]),
init() {
this.formSubmitWindows = new WeakSet();
Services.obs.addObserver(FormSubmitListener, "earlyformsubmit", false);
},
uninit() {
Services.obs.removeObserver(FormSubmitListener, "earlyformsubmit", false);
this.formSubmitWindows = new WeakSet();
},
notify: function(form, window, actionURI) {
try {
this.formSubmitWindows.add(window);
} catch (e) {
Cu.reportError("Error in FormSubmitListener.notify");
}
},
hasAndForget: function(window) {
let has = this.formSubmitWindows.has(window);
this.formSubmitWindows.delete(window);
return has;
},
};
var WebProgressListener = {
init: function() {
// This WeakMap (DOMWindow -> nsIURI) keeps track of the pathname and hash
// of the previous location for all the existent docShells.
this.previousURIMap = new WeakMap();
// Populate the above previousURIMap by iterating over the docShells tree.
for (let currentDocShell of WebNavigationFrames.iterateDocShellTree(docShell)) {
let win = currentDocShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
let {currentURI} = currentDocShell.QueryInterface(Ci.nsIWebNavigation);
this.previousURIMap.set(win, currentURI);
}
// This WeakSet of DOMWindows keeps track of the attempted refresh.
this.refreshAttemptedDOMWindows = new WeakSet();
let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebProgress);
webProgress.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_STATE_WINDOW |
Ci.nsIWebProgress.NOTIFY_REFRESH |
Ci.nsIWebProgress.NOTIFY_LOCATION);
},
uninit() {
if (!docShell) {
return;
}
let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebProgress);
webProgress.removeProgressListener(this);
},
onRefreshAttempted: function onRefreshAttempted(webProgress, URI, delay, sameURI) {
this.refreshAttemptedDOMWindows.add(webProgress.DOMWindow);
// If this function doesn't return true, the attempted refresh will be blocked.
return true;
},
onStateChange: function onStateChange(webProgress, request, stateFlags, status) {
let {originalURI, URI: locationURI} = request.QueryInterface(Ci.nsIChannel);
// Prevents "about", "chrome", "resource" and "moz-extension" URI schemes to be
// reported with the resolved "file" or "jar" URIs. (see Bug 1246125 for rationale)
if (locationURI.schemeIs("file") || locationURI.schemeIs("jar")) {
let shouldUseOriginalURI = originalURI.schemeIs("about") ||
originalURI.schemeIs("chrome") ||
originalURI.schemeIs("resource") ||
originalURI.schemeIs("moz-extension");
locationURI = shouldUseOriginalURI ? originalURI : locationURI;
}
this.sendStateChange({webProgress, locationURI, stateFlags, status});
// Based on the docs of the webNavigation.onCommitted event, it should be raised when:
// "The document might still be downloading, but at least part of
// the document has been received"
// and for some reason we don't fire onLocationChange for the
// initial navigation of a sub-frame.
// For the above two reasons, when the navigation event is related to
// a sub-frame we process the document change here and
// then send an "Extension:DocumentChange" message to the main process,
// where it will be turned into a webNavigation.onCommitted event.
// (see Bug 1264936 and Bug 125662 for rationale)
if ((webProgress.DOMWindow.top != webProgress.DOMWindow) &&
(stateFlags & Ci.nsIWebProgressListener.STATE_IS_DOCUMENT)) {
this.sendDocumentChange({webProgress, locationURI, request});
}
},
onLocationChange: function onLocationChange(webProgress, request, locationURI, flags) {
let {DOMWindow} = webProgress;
// Get the previous URI loaded in the DOMWindow.
let previousURI = this.previousURIMap.get(DOMWindow);
// Update the URI in the map with the new locationURI.
this.previousURIMap.set(DOMWindow, locationURI);
let isSameDocument = (flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT);
// When a frame navigation doesn't change the current loaded document
// (which can be due to history.pushState/replaceState or to a changed hash in the url),
// it is reported only to the onLocationChange, for this reason
// we process the history change here and then we are going to send
// an "Extension:HistoryChange" to the main process, where it will be turned
// into a webNavigation.onHistoryStateUpdated/onReferenceFragmentUpdated event.
if (isSameDocument) {
this.sendHistoryChange({webProgress, previousURI, locationURI, request});
} else if (webProgress.DOMWindow.top == webProgress.DOMWindow) {
// We have to catch the document changes from top level frames here,
// where we can detect the "server redirect" transition.
// (see Bug 1264936 and Bug 125662 for rationale)
this.sendDocumentChange({webProgress, locationURI, request});
}
},
sendStateChange({webProgress, locationURI, stateFlags, status}) {
let data = {
requestURL: locationURI.spec,
windowId: webProgress.DOMWindowID,
parentWindowId: WebNavigationFrames.getParentWindowId(webProgress.DOMWindow),
status,
stateFlags,
};
sendAsyncMessage("Extension:StateChange", data);
},
sendDocumentChange({webProgress, locationURI, request}) {
let {loadType, DOMWindow} = webProgress;
let frameTransitionData = this.getFrameTransitionData({loadType, request, DOMWindow});
let data = {
frameTransitionData,
location: locationURI ? locationURI.spec : "",
windowId: webProgress.DOMWindowID,
parentWindowId: WebNavigationFrames.getParentWindowId(webProgress.DOMWindow),
};
sendAsyncMessage("Extension:DocumentChange", data);
},
sendHistoryChange({webProgress, previousURI, locationURI, request}) {
let {loadType, DOMWindow} = webProgress;
let isHistoryStateUpdated = false;
let isReferenceFragmentUpdated = false;
let pathChanged = !(previousURI && locationURI.equalsExceptRef(previousURI));
let hashChanged = !(previousURI && previousURI.ref == locationURI.ref);
// When the location changes but the document is the same:
// - path not changed and hash changed -> |onReferenceFragmentUpdated|
// (even if it changed using |history.pushState|)
// - path not changed and hash not changed -> |onHistoryStateUpdated|
// (only if it changes using |history.pushState|)
// - path changed -> |onHistoryStateUpdated|
if (!pathChanged && hashChanged) {
isReferenceFragmentUpdated = true;
} else if (loadType & Ci.nsIDocShell.LOAD_CMD_PUSHSTATE) {
isHistoryStateUpdated = true;
} else if (loadType & Ci.nsIDocShell.LOAD_CMD_HISTORY) {
isHistoryStateUpdated = true;
}
if (isHistoryStateUpdated || isReferenceFragmentUpdated) {
let frameTransitionData = this.getFrameTransitionData({loadType, request, DOMWindow});
let data = {
frameTransitionData,
isHistoryStateUpdated, isReferenceFragmentUpdated,
location: locationURI ? locationURI.spec : "",
windowId: webProgress.DOMWindowID,
parentWindowId: WebNavigationFrames.getParentWindowId(webProgress.DOMWindow),
};
sendAsyncMessage("Extension:HistoryChange", data);
}
},
getFrameTransitionData({loadType, request, DOMWindow}) {
let frameTransitionData = {};
if (loadType & Ci.nsIDocShell.LOAD_CMD_HISTORY) {
frameTransitionData.forward_back = true;
}
if (loadType & Ci.nsIDocShell.LOAD_CMD_RELOAD) {
frameTransitionData.reload = true;
}
if (request instanceof Ci.nsIChannel) {
if (request.loadInfo.redirectChain.length) {
frameTransitionData.server_redirect = true;
}
}
if (FormSubmitListener.hasAndForget(DOMWindow)) {
frameTransitionData.form_submit = true;
}
if (this.refreshAttemptedDOMWindows.has(DOMWindow)) {
this.refreshAttemptedDOMWindows.delete(DOMWindow);
frameTransitionData.client_redirect = true;
}
return frameTransitionData;
},
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIWebProgressListener,
Ci.nsIWebProgressListener2,
Ci.nsISupportsWeakReference,
]),
};
var disabled = false;
WebProgressListener.init();
FormSubmitListener.init();
addEventListener("unload", () => {
if (!disabled) {
disabled = true;
WebProgressListener.uninit();
FormSubmitListener.uninit();
}
});
addMessageListener("Extension:DisableWebNavigation", () => {
if (!disabled) {
disabled = true;
WebProgressListener.uninit();
FormSubmitListener.uninit();
}
});
|