2019-01-29 18:18:42 +03:00
|
|
|
/* 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 EXPORTED_SYMBOLS = ["Page"];
|
|
|
|
|
2019-12-11 23:49:46 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
|
2019-03-10 15:51:01 +03:00
|
|
|
const { ContentProcessDomain } = ChromeUtils.import(
|
|
|
|
"chrome://remote/content/domains/ContentProcessDomain.jsm"
|
|
|
|
);
|
2019-01-29 18:18:42 +03:00
|
|
|
const { UnsupportedError } = ChromeUtils.import(
|
|
|
|
"chrome://remote/content/Error.jsm"
|
|
|
|
);
|
|
|
|
|
2019-12-11 23:49:46 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
|
|
|
this,
|
|
|
|
"uuidGen",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator"
|
|
|
|
);
|
|
|
|
|
2019-11-06 18:32:55 +03:00
|
|
|
const {
|
|
|
|
LOAD_FLAGS_BYPASS_CACHE,
|
|
|
|
LOAD_FLAGS_BYPASS_PROXY,
|
|
|
|
LOAD_FLAGS_NONE,
|
|
|
|
} = Ci.nsIWebNavigation;
|
|
|
|
|
2019-03-10 15:51:01 +03:00
|
|
|
class Page extends ContentProcessDomain {
|
2019-03-10 15:50:55 +03:00
|
|
|
constructor(session) {
|
|
|
|
super(session);
|
2019-11-19 22:42:18 +03:00
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
this.enabled = false;
|
2019-11-19 22:42:18 +03:00
|
|
|
this.lifecycleEnabled = false;
|
2019-12-11 23:49:46 +03:00
|
|
|
// script id => { source, worldName }
|
|
|
|
this.scriptsToEvaluateOnLoad = new Map();
|
|
|
|
this.worldsToEvaluateOnLoad = new Set();
|
2019-05-14 18:18:51 +03:00
|
|
|
|
2019-12-10 00:52:56 +03:00
|
|
|
this._onFrameNavigated = this._onFrameNavigated.bind(this);
|
2019-12-11 23:49:46 +03:00
|
|
|
this._onScriptLoaded = this._onScriptLoaded.bind(this);
|
|
|
|
|
|
|
|
this.contextObserver.on("script-loaded", this._onScriptLoaded);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
destructor() {
|
2019-11-19 22:42:18 +03:00
|
|
|
this.setLifecycleEventsEnabled({ enabled: false });
|
2019-12-11 23:49:46 +03:00
|
|
|
this.contextObserver.off("script-loaded", this._onScriptLoaded);
|
2019-01-29 18:18:42 +03:00
|
|
|
this.disable();
|
2019-05-25 20:36:37 +03:00
|
|
|
|
|
|
|
super.destructor();
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// commands
|
|
|
|
|
|
|
|
async enable() {
|
|
|
|
if (!this.enabled) {
|
|
|
|
this.enabled = true;
|
2019-12-10 00:52:56 +03:00
|
|
|
this.contextObserver.on("frame-navigated", this._onFrameNavigated);
|
2019-05-14 18:18:51 +03:00
|
|
|
|
2020-03-24 10:46:37 +03:00
|
|
|
this.chromeEventHandler.addEventListener("readystatechange", this, {
|
2019-02-11 21:05:12 +03:00
|
|
|
mozSystemGroup: true,
|
2020-03-24 10:46:37 +03:00
|
|
|
capture: true,
|
2019-02-11 21:05:12 +03:00
|
|
|
});
|
2019-11-19 22:39:37 +03:00
|
|
|
this.chromeEventHandler.addEventListener("pagehide", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
2020-03-24 10:46:37 +03:00
|
|
|
this.chromeEventHandler.addEventListener("unload", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
this.chromeEventHandler.addEventListener("DOMContentLoaded", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
|
|
|
this.chromeEventHandler.addEventListener("load", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
2019-02-11 21:05:12 +03:00
|
|
|
this.chromeEventHandler.addEventListener("pageshow", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
if (this.enabled) {
|
2019-12-10 00:52:56 +03:00
|
|
|
this.contextObserver.off("frame-navigated", this._onFrameNavigated);
|
2019-05-14 18:18:51 +03:00
|
|
|
|
2020-03-24 10:46:37 +03:00
|
|
|
this.chromeEventHandler.removeEventListener("readystatechange", this, {
|
2019-02-11 21:05:12 +03:00
|
|
|
mozSystemGroup: true,
|
2020-03-24 10:46:37 +03:00
|
|
|
capture: true,
|
2019-02-11 21:05:12 +03:00
|
|
|
});
|
2019-11-19 22:39:37 +03:00
|
|
|
this.chromeEventHandler.removeEventListener("pagehide", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
2020-03-24 10:46:37 +03:00
|
|
|
this.chromeEventHandler.removeEventListener("unload", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
this.chromeEventHandler.removeEventListener("DOMContentLoaded", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
|
|
|
this.chromeEventHandler.removeEventListener("load", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
2019-02-11 21:05:12 +03:00
|
|
|
this.chromeEventHandler.removeEventListener("pageshow", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
2019-01-29 18:18:42 +03:00
|
|
|
this.enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async navigate({ url, referrer, transitionType, frameId } = {}) {
|
2020-01-06 15:55:35 +03:00
|
|
|
if (frameId && frameId != this.docShell.browsingContext.id.toString()) {
|
2019-01-29 18:18:42 +03:00
|
|
|
throw new UnsupportedError("frameId not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
loadFlags: transitionToLoadFlag(transitionType),
|
|
|
|
referrerURI: referrer,
|
2019-02-11 21:05:12 +03:00
|
|
|
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
|
2019-01-29 18:18:42 +03:00
|
|
|
};
|
2019-02-11 21:05:12 +03:00
|
|
|
this.docShell.loadURI(url, opts);
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-04-30 14:09:30 +03:00
|
|
|
return {
|
2020-01-06 15:55:35 +03:00
|
|
|
frameId: this.docShell.browsingContext.id.toString(),
|
2019-04-30 14:09:30 +03:00
|
|
|
};
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 18:32:55 +03:00
|
|
|
async reload({ ignoreCache }) {
|
|
|
|
let flags = LOAD_FLAGS_NONE;
|
|
|
|
if (ignoreCache) {
|
|
|
|
flags |= LOAD_FLAGS_BYPASS_CACHE;
|
|
|
|
flags |= LOAD_FLAGS_BYPASS_PROXY;
|
|
|
|
}
|
|
|
|
this.docShell.reload(flags);
|
2019-05-07 15:52:49 +03:00
|
|
|
}
|
|
|
|
|
2019-03-11 15:50:26 +03:00
|
|
|
getFrameTree() {
|
2020-01-06 15:55:35 +03:00
|
|
|
const frameId = this.docShell.browsingContext.id.toString();
|
2019-03-11 15:50:26 +03:00
|
|
|
return {
|
|
|
|
frameTree: {
|
|
|
|
frame: {
|
2019-05-14 12:07:36 +03:00
|
|
|
id: frameId,
|
|
|
|
url: this.content.location.href,
|
|
|
|
loaderId: null,
|
|
|
|
securityOrigin: null,
|
|
|
|
mimeType: null,
|
2019-03-11 15:50:26 +03:00
|
|
|
},
|
|
|
|
childFrames: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:49:46 +03:00
|
|
|
/**
|
|
|
|
* 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 = uuidGen
|
|
|
|
.generateUUID()
|
|
|
|
.toString()
|
|
|
|
.slice(1, -1);
|
|
|
|
this.scriptsToEvaluateOnLoad.set(identifier, { worldName, source });
|
|
|
|
|
|
|
|
return { identifier };
|
|
|
|
}
|
2019-12-10 00:52:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param {string=} options.worldName
|
|
|
|
* @param {boolean=} options.grantUniversalAccess (not supported)
|
|
|
|
* This is a powerful option, use with caution.
|
|
|
|
* @return {number} Runtime.ExecutionContextId
|
|
|
|
*/
|
|
|
|
createIsolatedWorld(options = {}) {
|
|
|
|
const { frameId, worldName } = options;
|
2020-01-06 15:55:35 +03:00
|
|
|
if (frameId && frameId != this.docShell.browsingContext.id.toString()) {
|
2019-12-10 00:52:56 +03:00
|
|
|
throw new UnsupportedError("frameId not supported");
|
|
|
|
}
|
|
|
|
const Runtime = this.session.domains.get("Runtime");
|
|
|
|
|
|
|
|
const executionContextId = Runtime._onContextCreated("context-created", {
|
|
|
|
windowId: this.content.windowUtils.currentInnerWindowID,
|
|
|
|
window: this.content,
|
|
|
|
isDefault: false,
|
|
|
|
contextName: worldName,
|
|
|
|
contextType: "isolated",
|
|
|
|
});
|
|
|
|
|
|
|
|
return { executionContextId };
|
|
|
|
}
|
2019-03-11 15:50:26 +03:00
|
|
|
|
2019-11-19 22:42:18 +03:00
|
|
|
/**
|
|
|
|
* Controls whether page will emit lifecycle events.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {boolean} options.enabled
|
|
|
|
* If true, starts emitting lifecycle events.
|
|
|
|
*/
|
2019-12-19 13:40:55 +03:00
|
|
|
setLifecycleEventsEnabled(options = {}) {
|
2019-11-19 22:42:18 +03:00
|
|
|
const { enabled } = options;
|
|
|
|
|
|
|
|
this.lifecycleEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
url() {
|
2019-02-11 21:05:12 +03:00
|
|
|
return this.content.location.href;
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:52:56 +03:00
|
|
|
_onFrameNavigated(name, { frameId, window }) {
|
2019-05-14 18:18:51 +03:00
|
|
|
const url = window.location.href;
|
|
|
|
this.emit("Page.frameNavigated", {
|
|
|
|
frame: {
|
|
|
|
id: frameId,
|
|
|
|
// frameNavigated is only emitted for the top level document
|
|
|
|
// so that it never has a parent.
|
|
|
|
parentId: null,
|
|
|
|
url,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:49:46 +03:00
|
|
|
_onScriptLoaded(name) {
|
|
|
|
const Runtime = this.session.domains.get("Runtime");
|
|
|
|
for (const world of this.worldsToEvaluateOnLoad) {
|
|
|
|
Runtime._onContextCreated("context-created", {
|
|
|
|
windowId: this.content.windowUtils.currentInnerWindowID,
|
|
|
|
window: this.content,
|
|
|
|
isDefault: false,
|
|
|
|
contextName: world,
|
|
|
|
contextType: "isolated",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// TODO evaluate each onNewDoc script in the appropriate world
|
|
|
|
}
|
|
|
|
|
2019-11-20 01:08:05 +03:00
|
|
|
emitLifecycleEvent(frameId, loaderId, name, timestamp) {
|
|
|
|
if (this.lifecycleEnabled) {
|
|
|
|
this.emit("Page.lifecycleEvent", { frameId, loaderId, name, timestamp });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 14:09:30 +03:00
|
|
|
handleEvent({ type, target }) {
|
2019-11-20 01:08:05 +03:00
|
|
|
const isFrame = target.defaultView != this.content;
|
|
|
|
|
|
|
|
if (isFrame) {
|
2019-04-30 14:09:30 +03:00
|
|
|
// Ignore iframes for now
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
const timestamp = Date.now();
|
2020-01-06 15:55:35 +03:00
|
|
|
const frameId = target.defaultView.docShell.browsingContext.id.toString();
|
2019-04-30 14:09:30 +03:00
|
|
|
const url = target.location.href;
|
2019-01-29 18:18:42 +03:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "DOMContentLoaded":
|
|
|
|
this.emit("Page.domContentEventFired", { timestamp });
|
2019-11-20 01:08:05 +03:00
|
|
|
if (!isFrame) {
|
|
|
|
this.emitLifecycleEvent(
|
|
|
|
frameId,
|
|
|
|
/* loaderId */ null,
|
|
|
|
"DOMContentLoaded",
|
|
|
|
timestamp
|
|
|
|
);
|
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
break;
|
2019-07-05 11:56:48 +03:00
|
|
|
|
2019-11-19 22:39:37 +03:00
|
|
|
case "pagehide":
|
2019-11-19 22:42:18 +03:00
|
|
|
// Maybe better to bound to "unload" once we can register for this event
|
2019-11-19 22:39:37 +03:00
|
|
|
this.emit("Page.frameStartedLoading", { frameId });
|
2019-11-20 01:08:05 +03:00
|
|
|
if (!isFrame) {
|
|
|
|
this.emitLifecycleEvent(
|
|
|
|
frameId,
|
|
|
|
/* loaderId */ null,
|
|
|
|
"init",
|
|
|
|
timestamp
|
|
|
|
);
|
|
|
|
}
|
2019-11-19 22:39:37 +03:00
|
|
|
break;
|
|
|
|
|
2020-03-24 10:46:37 +03:00
|
|
|
case "load":
|
2019-11-15 00:22:16 +03:00
|
|
|
this.emit("Page.loadEventFired", { timestamp });
|
2019-11-20 01:08:05 +03:00
|
|
|
if (!isFrame) {
|
|
|
|
this.emitLifecycleEvent(
|
|
|
|
frameId,
|
|
|
|
/* loaderId */ null,
|
|
|
|
"load",
|
|
|
|
timestamp
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-11 15:50:26 +03:00
|
|
|
// XXX this should most likely be sent differently
|
2019-11-06 17:42:20 +03:00
|
|
|
this.emit("Page.navigatedWithinDocument", { frameId, url });
|
|
|
|
this.emit("Page.frameStoppedLoading", { frameId });
|
2019-01-29 18:18:42 +03:00
|
|
|
break;
|
2020-03-24 10:46:37 +03:00
|
|
|
|
|
|
|
case "readystatechange":
|
|
|
|
if (this.content.document.readState === "loading") {
|
|
|
|
this.emitLifecycleEvent(
|
|
|
|
frameId,
|
|
|
|
/* loaderId */ null,
|
|
|
|
"init",
|
|
|
|
timestamp
|
|
|
|
);
|
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 21:27:17 +03:00
|
|
|
|
2020-01-23 04:51:53 +03:00
|
|
|
_contentRect() {
|
2019-12-12 21:27:17 +03:00
|
|
|
const docEl = this.content.document.documentElement;
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: docEl.scrollWidth,
|
|
|
|
height: docEl.scrollHeight,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-24 00:31:30 +03:00
|
|
|
_devicePixelRatio() {
|
|
|
|
return this.content.devicePixelRatio;
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:27:17 +03:00
|
|
|
_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,
|
|
|
|
};
|
|
|
|
}
|
2019-02-12 17:44:54 +03:00
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
|
|
|
|
function transitionToLoadFlag(transitionType) {
|
|
|
|
switch (transitionType) {
|
|
|
|
case "reload":
|
2019-03-13 17:35:57 +03:00
|
|
|
return Ci.nsIWebNavigation.LOAD_FLAGS_IS_REFRESH;
|
2019-01-29 18:18:42 +03:00
|
|
|
case "link":
|
|
|
|
default:
|
2019-03-13 17:35:57 +03:00
|
|
|
return Ci.nsIWebNavigation.LOAD_FLAGS_IS_LINK;
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|