Bug 1869043 add a main thread record of track audio outputs r=padenot
[gecko.git] / remote / webdriver-bidi / modules / Intercept.sys.mjs
blob4e3a9bb9e7baee76030f828d4898669bac62f9b5
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/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   getSeenNodesForBrowsingContext:
9     "chrome://remote/content/shared/webdriver/Session.sys.mjs",
10   TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
11 });
13 /**
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:
18  *
19  *     - {Map<BrowsingContext, Array<string>>} seenNodeIds
20  *         DOM nodes that need to be added to the navigable seen nodes map.
21  *
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.
27  *
28  * @returns {object}
29  *     The payload with the extra data removed if it was present.
30  */
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(
39         sessionId,
40         browsingContext
41       );
43       nodeIds.forEach(nodeId => seenNodes.add(nodeId));
44     });
46     delete payload._extraData;
47   }
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
55     );
56   }
58   return payload;
61 function addContextIdToSerializedWindow(serialized) {
62   if (serialized.value) {
63     switch (serialized.type) {
64       case "array":
65       case "htmlcollection":
66       case "nodelist":
67       case "set": {
68         serialized.value = serialized.value.map(value =>
69           addContextIdToSerializedWindow(value)
70         );
71         break;
72       }
74       case "map":
75       case "object": {
76         serialized.value = serialized.value.map(([key, value]) => [
77           key,
78           addContextIdToSerializedWindow(value),
79         ]);
80         break;
81       }
83       case "window": {
84         if (serialized.value.isTopBrowsingContext) {
85           const browsingContext = BrowsingContext.getCurrentTopByBrowserId(
86             serialized.value.context
87           );
89           serialized.value = {
90             context: lazy.TabManager.getIdForBrowsingContext(browsingContext),
91           };
92         }
93         break;
94       }
95     }
96   } else if (serialized.exception) {
97     serialized.exception = addContextIdToSerializedWindow(serialized.exception);
98   }
100   return serialized;