1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 ChromeUtils.defineESModuleGetters(lazy, {
8 getSeenNodesForBrowsingContext:
9 "chrome://remote/content/shared/webdriver/Session.sys.mjs",
10 TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
14 * The serialization of JavaScript objects in the content process might produce
15 * extra data that needs to be transfered and then processed by the parent
16 * process. This extra data is part of the payload as returned by commands
17 * and events and can contain the following:
19 * - {Map<BrowsingContext, Array<string>>} seenNodeIds
20 * DOM nodes that need to be added to the navigable seen nodes map.
22 * @param {string} sessionId
23 * Id of the WebDriver session
24 * @param {object} payload
25 * Payload of the response for the command and event that might contain
26 * a `_extraData` field.
29 * The payload with the extra data removed if it was present.
31 export function processExtraData(sessionId, payload) {
32 // Process extra data if present and delete it from the payload
33 if ("_extraData" in payload) {
34 const { seenNodeIds } = payload._extraData;
36 // Updates the seen nodes for the current session and browsing context.
37 seenNodeIds?.forEach((nodeIds, browsingContext) => {
38 const seenNodes = lazy.getSeenNodesForBrowsingContext(
43 nodeIds.forEach(nodeId => seenNodes.add(nodeId));
46 delete payload._extraData;
49 // Find serialized WindowProxy and resolve browsing context to a navigable id.
50 if (payload?.result) {
51 payload.result = addContextIdToSerializedWindow(payload.result);
52 } else if (payload.exceptionDetails) {
53 payload.exceptionDetails = addContextIdToSerializedWindow(
54 payload.exceptionDetails
61 function addContextIdToSerializedWindow(serialized) {
62 if (serialized.value) {
63 switch (serialized.type) {
65 case "htmlcollection":
68 serialized.value = serialized.value.map(value =>
69 addContextIdToSerializedWindow(value)
76 serialized.value = serialized.value.map(([key, value]) => [
78 addContextIdToSerializedWindow(value),
84 if (serialized.value.isTopBrowsingContext) {
85 const browsingContext = BrowsingContext.getCurrentTopByBrowserId(
86 serialized.value.context
90 context: lazy.TabManager.getIdForBrowsingContext(browsingContext),
96 } else if (serialized.exception) {
97 serialized.exception = addContextIdToSerializedWindow(serialized.exception);