tor-browser

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

head.js (4886B)


      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 "use strict";
      6 
      7 /* eslint no-unused-vars: [2, {"vars": "local"}] */
      8 
      9 Services.scriptloader.loadSubScript(
     10  "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js",
     11  this
     12 );
     13 
     14 const {
     15  DevToolsClient,
     16 } = require("resource://devtools/client/devtools-client.js");
     17 const {
     18  DevToolsServer,
     19 } = require("resource://devtools/server/devtools-server.js");
     20 
     21 async function _initResourceCommandFromCommands(
     22  commands,
     23  { listenForWorkers = false } = {}
     24 ) {
     25  const targetCommand = commands.targetCommand;
     26  if (listenForWorkers) {
     27    targetCommand.listenForWorkers = true;
     28  }
     29  await targetCommand.startListening();
     30 
     31  //Bug 1709065: Stop exporting resourceCommand and use commands.resourceCommand
     32  //And rename all these methods
     33  return {
     34    client: commands.client,
     35    commands,
     36    resourceCommand: commands.resourceCommand,
     37    targetCommand,
     38  };
     39 }
     40 
     41 /**
     42 * Instantiate a ResourceCommand for the given tab.
     43 *
     44 * @param {Tab} tab
     45 *        The browser frontend's tab to connect to.
     46 * @param {object} options
     47 * @param {boolean} options.listenForWorkers
     48 * @return {object} object
     49 * @return {ResourceCommand} object.resourceCommand
     50 *         The underlying resource command interface.
     51 * @return {object} object.commands
     52 *         The commands object defined by modules from devtools/shared/commands.
     53 * @return {DevToolsClient} object.client
     54 *         The underlying client instance.
     55 * @return {TargetCommand} object.targetCommand
     56 *         The underlying target list instance.
     57 */
     58 async function initResourceCommand(tab, options) {
     59  const commands = await CommandsFactory.forTab(tab);
     60  return _initResourceCommandFromCommands(commands, options);
     61 }
     62 
     63 /**
     64 * Instantiate a multi-process ResourceCommand, watching all type of targets.
     65 *
     66 * @return {object} object
     67 * @return {ResourceCommand} object.resourceCommand
     68 *         The underlying resource command interface.
     69 * @return {object} object.commands
     70 *         The commands object defined by modules from devtools/shared/commands.
     71 * @return {DevToolsClient} object.client
     72 *         The underlying client instance.
     73 * @return {DevToolsClient} object.targetCommand
     74 *         The underlying target list instance.
     75 */
     76 async function initMultiProcessResourceCommand() {
     77  const commands = await CommandsFactory.forMainProcess();
     78  return _initResourceCommandFromCommands(commands);
     79 }
     80 
     81 // Copied from devtools/shared/webconsole/test/chrome/common.js
     82 function checkObject(object, expected) {
     83  if (object && object.getGrip) {
     84    object = object.getGrip();
     85  }
     86 
     87  for (const name of Object.keys(expected)) {
     88    const expectedValue = expected[name];
     89    const value = object[name];
     90    checkValue(name, value, expectedValue);
     91  }
     92 }
     93 
     94 function checkValue(name, value, expected) {
     95  if (expected === null) {
     96    is(value, null, `'${name}' is null`);
     97  } else if (expected === undefined) {
     98    is(value, expected, `'${name}' is undefined`);
     99  } else if (
    100    typeof expected == "string" ||
    101    typeof expected == "number" ||
    102    typeof expected == "boolean"
    103  ) {
    104    is(value, expected, "property '" + name + "'");
    105  } else if (expected instanceof RegExp) {
    106    ok(expected.test(value), name + ": " + expected + " matched " + value);
    107  } else if (Array.isArray(expected)) {
    108    info("checking array for property '" + name + "'");
    109    ok(Array.isArray(value), `property '${name}' is an array`);
    110 
    111    is(value.length, expected.length, "Array has expected length");
    112    if (value.length !== expected.length) {
    113      is(JSON.stringify(value, null, 2), JSON.stringify(expected, null, 2));
    114    } else {
    115      checkObject(value, expected);
    116    }
    117  } else if (typeof expected == "object") {
    118    info("checking object for property '" + name + "'");
    119    checkObject(value, expected);
    120  }
    121 }
    122 
    123 async function triggerNetworkRequests(browser, commands) {
    124  for (let i = 0; i < commands.length; i++) {
    125    await SpecialPowers.spawn(browser, [commands[i]], async function (code) {
    126      const script = content.document.createElement("script");
    127      script.append(
    128        content.document.createTextNode(
    129          `async function triggerRequest() {${code}}`
    130        )
    131      );
    132      content.document.body.append(script);
    133      await content.wrappedJSObject.triggerRequest();
    134      script.remove();
    135    });
    136  }
    137 }
    138 
    139 /**
    140 * Get the stylesheet text for a given stylesheet resource.
    141 *
    142 * @param {object} styleSheetResource
    143 * @returns Promise<String>
    144 */
    145 async function getStyleSheetResourceText(styleSheetResource) {
    146  const styleSheetsFront =
    147    await styleSheetResource.targetFront.getFront("stylesheets");
    148  const res = await styleSheetsFront.getText(styleSheetResource.resourceId);
    149  return res.string();
    150 }