tor-browser

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

api.js (2542B)


      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 { FileUtils } = ChromeUtils.importESModule(
      6  "resource://gre/modules/FileUtils.sys.mjs"
      7 );
      8 
      9 /* globals ExtensionAPI, Services, XPCOMUtils */
     10 
     11 XPCOMUtils.defineLazyServiceGetter(
     12  this,
     13  "resProto",
     14  "@mozilla.org/network/protocol;1?name=resource",
     15  Ci.nsISubstitutingProtocolHandler
     16 );
     17 
     18 async function tpsStartup() {
     19  try {
     20    var { TPS } = ChromeUtils.importESModule("resource://tps/tps.sys.mjs");
     21    let { goQuitApplication } = ChromeUtils.importESModule(
     22      "resource://tps/quit.sys.mjs"
     23    );
     24    TPS.goQuitApplication = goQuitApplication;
     25 
     26    let testFile = Services.prefs.getStringPref("testing.tps.testFile", "");
     27    let testPhase = Services.prefs.getStringPref("testing.tps.testPhase", "");
     28    if (!testFile || !testPhase) {
     29      // Note: this quits.
     30      TPS.DumpError(
     31        "TPS no longer takes arguments from the command line. " +
     32          "instead you need to pass preferences  `testing.tps.{testFile,testPhase}` " +
     33          "and optionally `testing.tps.{logFile,ignoreUnusedEngines}`.\n"
     34      );
     35    }
     36 
     37    let logFile = Services.prefs.getStringPref("testing.tps.logFile", "");
     38    let ignoreUnusedEngines = Services.prefs.getBoolPref(
     39      "testing.tps.ignoreUnusedEngines",
     40      false
     41    );
     42    let options = { ignoreUnusedEngines };
     43    let testFileUri = Services.io.newFileURI(new FileUtils.File(testFile)).spec;
     44 
     45    try {
     46      await TPS.RunTestPhase(testFileUri, testPhase, logFile, options);
     47    } catch (err) {
     48      TPS.DumpError("TestPhase failed", err);
     49    }
     50  } catch (e) {
     51    if (typeof TPS != "undefined") {
     52      // Note: This calls quit() under the hood
     53      TPS.DumpError("Test initialization failed", e);
     54    }
     55    dump(`TPS test initialization failed: ${e} - ${e.stack}\n`);
     56    // Try and quit right away, no reason to wait around for python
     57    // to kill us if initialization failed.
     58    Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
     59  }
     60 }
     61 
     62 this.tps = class extends ExtensionAPI {
     63  onStartup() {
     64    resProto.setSubstitution(
     65      "tps",
     66      Services.io.newURI("resource/", null, this.extension.rootURI)
     67    );
     68    /* Ignore the platform's online/offline status while running tests. */
     69    Services.io.manageOfflineStatus = false;
     70    Services.io.offline = false;
     71    tpsStartup();
     72  }
     73 
     74  onShutdown() {
     75    resProto.setSubstitution("tps", null);
     76  }
     77 };