tor-browser

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

MockPromptCollection.sys.mjs (3290B)


      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  WrapPrivileged: "resource://testing-common/WrapPrivileged.sys.mjs",
      9 });
     10 
     11 const Cm = Components.manager;
     12 
     13 const CONTRACT_ID = "@mozilla.org/embedcomp/prompt-collection;1";
     14 
     15 Cu.crashIfNotInAutomation();
     16 
     17 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
     18 var oldClassID = "";
     19 var newClassID = Services.uuid.generateUUID();
     20 var newFactory = function (window) {
     21  return {
     22    createInstance(aIID) {
     23      return new MockPromptCollectionInstance(window).QueryInterface(aIID);
     24    },
     25    QueryInterface: ChromeUtils.generateQI(["nsIFactory"]),
     26  };
     27 };
     28 
     29 export var MockPromptCollection = {
     30  init(browsingContext) {
     31    this.window = browsingContext.window;
     32 
     33    this.reset();
     34    this.factory = newFactory(this.window);
     35    if (!registrar.isCIDRegistered(newClassID)) {
     36      oldClassID = registrar.contractIDToCID(CONTRACT_ID);
     37      registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);
     38    }
     39  },
     40 
     41  reset() {
     42    this.returnBeforeUnloadCheck = false;
     43    this.returnConfirmRepost = false;
     44    this.returnConfirmFolderUpload = false;
     45    this.showConfirmFolderUploadCallback = null;
     46  },
     47 
     48  cleanup() {
     49    var previousFactory = this.factory;
     50    this.reset();
     51    this.factory = null;
     52 
     53    registrar.unregisterFactory(newClassID, previousFactory);
     54    if (oldClassID != "") {
     55      registrar.registerFactory(oldClassID, "", CONTRACT_ID, null);
     56    }
     57  },
     58 };
     59 
     60 function MockPromptCollectionInstance(window) {
     61  this.window = window;
     62  this.showConfirmFolderUploadCallback = null;
     63  this.showConfirmFolderUploadCallbackWrapped = null;
     64  this.directoryName = "";
     65 }
     66 
     67 MockPromptCollectionInstance.prototype = {
     68  QueryInterface: ChromeUtils.generateQI(["nsIPromptCollection"]),
     69 
     70  asyncBeforeUnloadCheck() {
     71    return Promise.resolve(MockPromptCollection.returnBeforeUnloadCheck);
     72  },
     73 
     74  confirmRepost() {
     75    return MockPromptCollection.returnConfirmRepost;
     76  },
     77 
     78  confirmFolderUpload(aBrowsingContext, aDirectoryName) {
     79    this.directoryName = aDirectoryName;
     80 
     81    if (
     82      typeof MockPromptCollection.showConfirmFolderUploadCallback == "function"
     83    ) {
     84      if (
     85        MockPromptCollection.showConfirmFolderUploadCallback !=
     86        this.showConfirmFolderUploadCallback
     87      ) {
     88        this.showConfirmFolderUploadCallback =
     89          MockPromptCollection.showConfirmFolderUploadCallback;
     90        if (Cu.isXrayWrapper(this.window)) {
     91          this.showConfirmFolderUploadCallbackWrapped =
     92            lazy.WrapPrivileged.wrapCallback(
     93              MockPromptCollection.showConfirmFolderUploadCallback,
     94              this.window
     95            );
     96        } else {
     97          this.showConfirmFolderUploadCallbackWrapped =
     98            this.showConfirmFolderUploadCallback;
     99        }
    100      }
    101 
    102      try {
    103        let returnValue = this.showConfirmFolderUploadCallbackWrapped(this);
    104        if (typeof returnValue == "boolean") {
    105          return returnValue;
    106        }
    107      } catch (e) {
    108        return false;
    109      }
    110    }
    111 
    112    return MockPromptCollection.returnConfirmFolderUpload;
    113  },
    114 };