tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Intercept.sys.mjs (3136B)


      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/. */
      4 
      5 const lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  getSeenNodesForBrowsingContext:
      9    "chrome://remote/content/shared/webdriver/Session.sys.mjs",
     10  NavigableManager: "chrome://remote/content/shared/NavigableManager.sys.mjs",
     11 });
     12 
     13 /**
     14 * The serialization of JavaScript objects in the content process might produce
     15 * extra data that needs to be transferred 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;
     35 
     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      );
     42 
     43      nodeIds.forEach(nodeId => seenNodes.add(nodeId));
     44    });
     45 
     46    delete payload._extraData;
     47  }
     48 
     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  }
     57 
     58  return payload;
     59 }
     60 
     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      }
     73 
     74      case "map":
     75      case "object": {
     76        serialized.value = serialized.value.map(([key, value]) => [
     77          key,
     78          addContextIdToSerializedWindow(value),
     79        ]);
     80        break;
     81      }
     82 
     83      case "window": {
     84        if (serialized.value.isTopBrowsingContext) {
     85          const browsingContext = BrowsingContext.getCurrentTopByBrowserId(
     86            serialized.value.context
     87          );
     88 
     89          serialized.value = {
     90            context:
     91              lazy.NavigableManager.getIdForBrowsingContext(browsingContext),
     92          };
     93        }
     94        break;
     95      }
     96    }
     97  } else if (serialized.exception) {
     98    serialized.exception = addContextIdToSerializedWindow(serialized.exception);
     99  }
    100 
    101  return serialized;
    102 }