2023-01-06 16:31:23 +00: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/. */
|
|
|
|
|
|
|
|
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
|
|
|
|
|
|
|
|
const lazy = {};
|
|
|
|
|
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
|
|
element: "chrome://remote/content/marionette/element.sys.mjs",
|
|
|
|
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
|
|
|
|
Log: "chrome://remote/content/shared/Log.sys.mjs",
|
|
|
|
pprint: "chrome://remote/content/shared/Format.sys.mjs",
|
|
|
|
ShadowRoot: "chrome://remote/content/marionette/element.sys.mjs",
|
|
|
|
WebElement: "chrome://remote/content/marionette/element.sys.mjs",
|
|
|
|
WebReference: "chrome://remote/content/marionette/element.sys.mjs",
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "logger", () =>
|
|
|
|
lazy.Log.get(lazy.Log.TYPES.MARIONETTE)
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @namespace */
|
|
|
|
export const json = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone an object including collections.
|
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* Object to be cloned.
|
2023-01-06 16:31:24 +00:00
|
|
|
* @param {Set} seen
|
|
|
|
* List of objects already processed.
|
2023-01-06 16:31:23 +00:00
|
|
|
* @param {Function} cloneAlgorithm
|
|
|
|
* The clone algorithm to invoke for individual list entries or object
|
|
|
|
* properties.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* The cloned object.
|
|
|
|
*/
|
2023-01-06 16:31:24 +00:00
|
|
|
function cloneObject(value, seen, cloneAlgorithm) {
|
|
|
|
// Only proceed with cloning an object if it hasn't been seen yet.
|
|
|
|
if (seen.has(value)) {
|
|
|
|
throw new lazy.error.JavaScriptError("Cyclic object value");
|
2023-01-06 16:31:23 +00:00
|
|
|
}
|
2023-01-06 16:31:24 +00:00
|
|
|
seen.add(value);
|
|
|
|
|
|
|
|
let result;
|
2023-01-06 16:31:23 +00:00
|
|
|
|
2023-01-06 16:31:24 +00:00
|
|
|
if (lazy.element.isCollection(value)) {
|
|
|
|
result = [...value].map(entry => cloneAlgorithm(entry, seen));
|
|
|
|
} else {
|
|
|
|
// arbitrary objects
|
|
|
|
result = {};
|
|
|
|
for (let prop in value) {
|
|
|
|
try {
|
|
|
|
result[prop] = cloneAlgorithm(value[prop], seen);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.result == Cr.NS_ERROR_NOT_IMPLEMENTED) {
|
|
|
|
lazy.logger.debug(`Skipping ${prop}: ${e.message}`);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2023-01-06 16:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:31:24 +00:00
|
|
|
seen.delete(value);
|
|
|
|
|
2023-01-06 16:31:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone arbitrary objects to JSON-safe primitives that can be
|
|
|
|
* transported across processes and over the Marionette protocol.
|
|
|
|
*
|
|
|
|
* The marshaling rules are as follows:
|
|
|
|
*
|
|
|
|
* - Primitives are returned as is.
|
|
|
|
*
|
|
|
|
* - Collections, such as `Array`, `NodeList`, `HTMLCollection`
|
|
|
|
* et al. are transformed to arrays and then recursed.
|
|
|
|
*
|
|
|
|
* - Elements and ShadowRoots that are not known WebReference's are added to
|
|
|
|
* the `NodeCache`. For both the associated unique web reference identifier
|
|
|
|
* is returned.
|
|
|
|
*
|
|
|
|
* - Objects with custom JSON representations, i.e. if they have
|
|
|
|
* a callable `toJSON` function, are returned verbatim. This means
|
|
|
|
* their internal integrity _are not_ checked. Be careful.
|
|
|
|
*
|
2023-01-06 16:31:24 +00:00
|
|
|
* - If a cyclic references is detected a JavaScriptError is thrown.
|
2023-01-06 16:31:23 +00:00
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* Object to be cloned.
|
2023-01-06 16:31:24 +00:00
|
|
|
* @param {NodeCache} nodeCache
|
|
|
|
* Node cache that holds already seen WebElement and ShadowRoot references.
|
2023-01-06 16:31:23 +00:00
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* Same object as provided by `value` with the WebDriver specific
|
|
|
|
* elements replaced by WebReference's.
|
|
|
|
*
|
|
|
|
* @throws {JavaScriptError}
|
|
|
|
* If an object contains cyclic references.
|
|
|
|
* @throws {StaleElementReferenceError}
|
|
|
|
* If the element has gone stale, indicating it is no longer
|
|
|
|
* attached to the DOM.
|
|
|
|
*/
|
2023-01-06 16:31:24 +00:00
|
|
|
json.clone = function(value, nodeCache) {
|
|
|
|
function cloneJSON(value, seen) {
|
|
|
|
if (seen === undefined) {
|
|
|
|
seen = new Set();
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:31:23 +00:00
|
|
|
if ([undefined, null].includes(value)) {
|
|
|
|
return null;
|
2023-01-26 21:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const type = typeof value;
|
|
|
|
|
|
|
|
if (["boolean", "number", "string"].includes(type)) {
|
2023-01-06 16:31:23 +00:00
|
|
|
// Primitive values
|
|
|
|
return value;
|
2023-01-26 21:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluation of code might take place in mutable sandboxes, which are
|
|
|
|
// created to waive XRays by default. As such DOM nodes would have to be
|
|
|
|
// unwaived before accessing properties like "ownerGlobal" is possible.
|
|
|
|
const isNode = Node.isInstance(value);
|
|
|
|
if (isNode) {
|
|
|
|
value = Cu.unwaiveXrays(value);
|
|
|
|
}
|
|
|
|
|
2023-01-27 02:37:23 +00:00
|
|
|
if (
|
|
|
|
isNode &&
|
|
|
|
(lazy.element.isElement(value) || lazy.element.isShadowRoot(value))
|
|
|
|
) {
|
|
|
|
// Convert DOM elements (eg. HTMLElement, XULElement, et al) and
|
|
|
|
// ShadowRoot instances to WebReference references.
|
2023-01-06 16:31:23 +00:00
|
|
|
|
2023-01-27 02:37:23 +00:00
|
|
|
// Don't create a reference for stale elements.
|
2023-01-26 21:38:26 +00:00
|
|
|
if (lazy.element.isStale(value)) {
|
2023-01-06 16:31:23 +00:00
|
|
|
throw new lazy.error.StaleElementReferenceError(
|
2023-01-26 21:38:26 +00:00
|
|
|
lazy.pprint`The element ${value} is no longer attached to the DOM`
|
2023-01-06 16:31:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:38:26 +00:00
|
|
|
const nodeRef = nodeCache.getOrCreateNodeReference(value);
|
|
|
|
return lazy.WebReference.from(value, nodeRef).toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value.toJSON == "function") {
|
2023-01-06 16:31:23 +00:00
|
|
|
// custom JSON representation
|
|
|
|
let unsafeJSON;
|
|
|
|
try {
|
|
|
|
unsafeJSON = value.toJSON();
|
|
|
|
} catch (e) {
|
|
|
|
throw new lazy.error.JavaScriptError(`toJSON() failed with: ${e}`);
|
|
|
|
}
|
2023-01-26 21:38:26 +00:00
|
|
|
|
2023-01-06 16:31:24 +00:00
|
|
|
return cloneJSON(unsafeJSON, seen);
|
2023-01-06 16:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collections and arbitrary objects
|
2023-01-06 16:31:24 +00:00
|
|
|
return cloneObject(value, seen, cloneJSON);
|
2023-01-06 16:31:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 16:31:24 +00:00
|
|
|
return cloneJSON(value, new Set());
|
2023-01-06 16:31:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize an arbitrary object.
|
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* Arbitrary object.
|
2023-01-06 16:31:24 +00:00
|
|
|
* @param {NodeCache} nodeCache
|
|
|
|
* Node cache that holds already seen WebElement and ShadowRoot references.
|
2023-01-06 16:31:23 +00:00
|
|
|
* @param {WindowProxy} win
|
|
|
|
* Current window.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* Same object as provided by `value` with the WebDriver specific
|
|
|
|
* references replaced with real JavaScript objects.
|
|
|
|
*
|
|
|
|
* @throws {NoSuchElementError}
|
|
|
|
* If the WebElement reference has not been seen before.
|
|
|
|
* @throws {StaleElementReferenceError}
|
|
|
|
* If the element is stale, indicating it is no longer attached to the DOM.
|
|
|
|
*/
|
2023-01-06 16:31:24 +00:00
|
|
|
json.deserialize = function(value, nodeCache, win) {
|
|
|
|
function deserializeJSON(value, seen) {
|
|
|
|
if (seen === undefined) {
|
|
|
|
seen = new Set();
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:31:23 +00:00
|
|
|
if (value === undefined || value === null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (typeof value) {
|
|
|
|
case "boolean":
|
|
|
|
case "number":
|
|
|
|
case "string":
|
|
|
|
default:
|
|
|
|
return value;
|
|
|
|
|
|
|
|
case "object":
|
|
|
|
if (lazy.WebReference.isReference(value)) {
|
|
|
|
// Create a WebReference based on the WebElement identifier.
|
|
|
|
const webRef = lazy.WebReference.fromJSON(value);
|
|
|
|
|
2023-01-27 02:37:23 +00:00
|
|
|
if (
|
|
|
|
webRef instanceof lazy.WebElement ||
|
|
|
|
webRef instanceof lazy.ShadowRoot
|
|
|
|
) {
|
2023-01-16 18:04:41 +00:00
|
|
|
return lazy.element.getKnownElement(
|
|
|
|
win.browsingContext,
|
|
|
|
webRef.uuid,
|
|
|
|
nodeCache
|
|
|
|
);
|
2023-01-06 16:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WebFrame and WebWindow not supported yet
|
|
|
|
throw new lazy.error.UnsupportedOperationError();
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:31:24 +00:00
|
|
|
return cloneObject(value, seen, deserializeJSON);
|
2023-01-06 16:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:31:24 +00:00
|
|
|
return deserializeJSON(value, new Set());
|
2023-01-06 16:31:23 +00:00
|
|
|
};
|