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
|
/* 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 { ContentProcessDomain } from "chrome://remote/content/cdp/domains/ContentProcessDomain.sys.mjs";
const {
LOAD_FLAGS_BYPASS_CACHE,
LOAD_FLAGS_BYPASS_PROXY,
LOAD_FLAGS_NONE,
} = Ci.nsIWebNavigation;
export class Page extends ContentProcessDomain {
constructor(session) {
super(session);
this.enabled = false;
this.lifecycleEnabled = false;
// script id => { source, worldName }
this.scriptsToEvaluateOnLoad = new Map();
this.worldsToEvaluateOnLoad = new Set();
// This map is used to keep a reference to the loader id for
// those Page events, which do not directly rely on
// Network events. This might be a temporary solution until
// the Network observer could be queried for that. But right
// now this lives in the parent process.
this.frameIdToLoaderId = new Map();
this._onFrameAttached = this._onFrameAttached.bind(this);
this._onFrameDetached = this._onFrameDetached.bind(this);
this._onFrameNavigated = this._onFrameNavigated.bind(this);
this._onScriptLoaded = this._onScriptLoaded.bind(this);
this.session.contextObserver.on("script-loaded", this._onScriptLoaded);
}
destructor() {
this.setLifecycleEventsEnabled({ enabled: false });
this.session.contextObserver.off("script-loaded", this._onScriptLoaded);
this.disable();
super.destructor();
}
// commands
async enable() {
if (!this.enabled) {
this.session.contextObserver.on("frame-attached", this._onFrameAttached);
this.session.contextObserver.on("frame-detached", this._onFrameDetached);
this.session.contextObserver.on(
"frame-navigated",
this._onFrameNavigated
);
this.chromeEventHandler.addEventListener("readystatechange", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.addEventListener("pagehide", this, {
mozSystemGroup: true,
});
this.chromeEventHandler.addEventListener("unload", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.addEventListener("DOMContentLoaded", this, {
mozSystemGroup: true,
});
this.chromeEventHandler.addEventListener("hashchange", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.addEventListener("load", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.addEventListener("pageshow", this, {
mozSystemGroup: true,
});
this.enabled = true;
}
}
disable() {
if (this.enabled) {
this.session.contextObserver.off("frame-attached", this._onFrameAttached);
this.session.contextObserver.off("frame-detached", this._onFrameDetached);
this.session.contextObserver.off(
"frame-navigated",
this._onFrameNavigated
);
this.chromeEventHandler.removeEventListener("readystatechange", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.removeEventListener("pagehide", this, {
mozSystemGroup: true,
});
this.chromeEventHandler.removeEventListener("unload", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.removeEventListener("DOMContentLoaded", this, {
mozSystemGroup: true,
});
this.chromeEventHandler.removeEventListener("hashchange", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.removeEventListener("load", this, {
mozSystemGroup: true,
capture: true,
});
this.chromeEventHandler.removeEventListener("pageshow", this, {
mozSystemGroup: true,
});
this.enabled = false;
}
}
async reload(options = {}) {
const { ignoreCache } = options;
let flags = LOAD_FLAGS_NONE;
if (ignoreCache) {
flags |= LOAD_FLAGS_BYPASS_CACHE;
flags |= LOAD_FLAGS_BYPASS_PROXY;
}
this.docShell.reload(flags);
}
getFrameTree() {
const getFrames = context => {
const frameTree = {
frame: this._getFrameDetails({ context }),
};
if (context.children.length) {
const frames = [];
for (const childContext of context.children) {
frames.push(getFrames(childContext));
}
frameTree.childFrames = frames;
}
return frameTree;
};
return {
frameTree: getFrames(this.docShell.browsingContext),
};
}
/**
* Enqueues given script to be evaluated in every frame upon creation
*
* If `worldName` is specified, creates an execution context with the given name
* and evaluates given script in it.
*
* At this time, queued scripts do not get evaluated, hence `source` is marked as
* "unsupported".
*
* @param {Object} options
* @param {string} options.source (not supported)
* @param {string=} options.worldName
* @return {string} Page.ScriptIdentifier
*/
addScriptToEvaluateOnNewDocument(options = {}) {
const { source, worldName } = options;
if (worldName) {
this.worldsToEvaluateOnLoad.add(worldName);
}
const identifier = Services.uuid
.generateUUID()
.toString()
.slice(1, -1);
this.scriptsToEvaluateOnLoad.set(identifier, { worldName, source });
return { identifier };
}
/**
* Creates an isolated world for the given frame.
*
* Really it just creates an execution context with label "isolated".
*
* @param {Object} options
* @param {string} options.frameId
* Id of the frame in which the isolated world should be created.
* @param {string=} options.worldName
* An optional name which is reported in the Execution Context.
* @param {boolean=} options.grantUniversalAccess (not supported)
* This is a powerful option, use with caution.
*
* @return {number} Runtime.ExecutionContextId
* Execution context of the isolated world.
*/
createIsolatedWorld(options = {}) {
const { frameId, worldName } = options;
if (typeof frameId != "string") {
throw new TypeError("frameId: string value expected");
}
if (!["undefined", "string"].includes(typeof worldName)) {
throw new TypeError("worldName: string value expected");
}
const Runtime = this.session.domains.get("Runtime");
const contexts = Runtime._getContextsForFrame(frameId);
if (!contexts.length) {
throw new Error("No frame for given id found");
}
const defaultContext = Runtime._getDefaultContextForWindow(
contexts[0].windowId
);
const window = defaultContext.window;
const executionContextId = Runtime._onContextCreated("context-created", {
windowId: window.windowGlobalChild.innerWindowId,
window,
isDefault: false,
contextName: worldName,
contextType: "isolated",
});
return { executionContextId };
}
/**
* Controls whether page will emit lifecycle events.
*
* @param {Object} options
* @param {boolean} options.enabled
* If true, starts emitting lifecycle events.
*/
setLifecycleEventsEnabled(options = {}) {
const { enabled } = options;
this.lifecycleEnabled = enabled;
}
url() {
return this.content.location.href;
}
_onFrameAttached(name, { frameId, window }) {
const bc = BrowsingContext.get(frameId);
// Don't emit for top-level browsing contexts
if (!bc.parent) {
return;
}
// TODO: Use a unique identifier for frames (bug 1605359)
this.emit("Page.frameAttached", {
frameId: frameId.toString(),
parentFrameId: bc.parent.id.toString(),
stack: null,
});
// Usually both events are emitted when the "pagehide" event is received.
// But this wont happen for a new window or frame when the initial
// about:blank page has already loaded, and is being replaced with the
// final document.
if (!window.document.isInitialDocument) {
this.emit("Page.frameStartedLoading", { frameId: frameId.toString() });
const loaderId = this.frameIdToLoaderId.get(frameId);
const timestamp = Date.now() / 1000;
this.emitLifecycleEvent(frameId, loaderId, "init", timestamp);
}
}
_onFrameDetached(name, { frameId }) {
const bc = BrowsingContext.get(frameId);
// Don't emit for top-level browsing contexts
if (!bc.parent) {
return;
}
// TODO: Use a unique identifier for frames (bug 1605359)
this.emit("Page.frameDetached", { frameId: frameId.toString() });
}
_onFrameNavigated(name, { frameId }) {
const bc = BrowsingContext.get(frameId);
this.emit("Page.frameNavigated", {
frame: this._getFrameDetails({ context: bc }),
});
}
/**
* @param {Object=} options
* @param {number} options.windowId
* The inner window id of the window the script has been loaded for.
* @param {Window} options.window
* The window object of the document.
*/
_onScriptLoaded(name, options = {}) {
const { windowId, window } = options;
const Runtime = this.session.domains.get("Runtime");
for (const world of this.worldsToEvaluateOnLoad) {
Runtime._onContextCreated("context-created", {
windowId,
window,
isDefault: false,
contextName: world,
contextType: "isolated",
});
}
// TODO evaluate each onNewDoc script in the appropriate world
}
emitLifecycleEvent(frameId, loaderId, name, timestamp) {
if (this.lifecycleEnabled) {
this.emit("Page.lifecycleEvent", {
frameId: frameId.toString(),
loaderId,
name,
timestamp,
});
}
}
handleEvent({ type, target }) {
const timestamp = Date.now() / 1000;
// Some events such as "hashchange" use the window as the target, while
// others have a document.
const win = Window.isInstance(target) ? target : target.defaultView;
const frameId = win.docShell.browsingContext.id;
const isFrame = !!win.docShell.browsingContext.parent;
const loaderId = this.frameIdToLoaderId.get(frameId);
const url = win.location.href;
switch (type) {
case "DOMContentLoaded":
if (!isFrame) {
this.emit("Page.domContentEventFired", { timestamp });
}
this.emitLifecycleEvent(
frameId,
loaderId,
"DOMContentLoaded",
timestamp
);
break;
case "hashchange":
this.emit("Page.navigatedWithinDocument", {
frameId: frameId.toString(),
url,
});
break;
case "pagehide":
// Maybe better to bound to "unload" once we can register for this event
this.emit("Page.frameStartedLoading", { frameId: frameId.toString() });
this.emitLifecycleEvent(frameId, loaderId, "init", timestamp);
break;
case "load":
if (!isFrame) {
this.emit("Page.loadEventFired", { timestamp });
}
this.emitLifecycleEvent(frameId, loaderId, "load", timestamp);
// XXX this should most likely be sent differently
this.emit("Page.frameStoppedLoading", { frameId: frameId.toString() });
break;
case "readystatechange":
if (this.content.document.readyState === "loading") {
this.emitLifecycleEvent(frameId, loaderId, "init", timestamp);
}
}
}
_updateLoaderId(data) {
const { frameId, loaderId } = data;
this.frameIdToLoaderId.set(frameId, loaderId);
}
_contentRect() {
const docEl = this.content.document.documentElement;
return {
x: 0,
y: 0,
width: docEl.scrollWidth,
height: docEl.scrollHeight,
};
}
_devicePixelRatio() {
return (
this.content.browsingContext.overrideDPPX || this.content.devicePixelRatio
);
}
_getFrameDetails({ context, id }) {
const bc = context || BrowsingContext.get(id);
const frame = bc.embedderElement;
return {
id: bc.id.toString(),
parentId: bc.parent?.id.toString(),
loaderId: this.frameIdToLoaderId.get(bc.id),
url: bc.docShell.domWindow.location.href,
name: frame?.id || frame?.name,
securityOrigin: null,
mimeType: null,
};
}
_getScrollbarSize() {
const scrollbarHeight = {};
const scrollbarWidth = {};
this.content.windowUtils.getScrollbarSize(
false,
scrollbarWidth,
scrollbarHeight
);
return {
width: scrollbarWidth.value,
height: scrollbarHeight.value,
};
}
_layoutViewport() {
const scrollbarSize = this._getScrollbarSize();
return {
pageX: this.content.pageXOffset,
pageY: this.content.pageYOffset,
clientWidth: this.content.innerWidth - scrollbarSize.width,
clientHeight: this.content.innerHeight - scrollbarSize.height,
};
}
}
|