tor-browser

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

pause-scoped.js (2270B)


      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 const { ObjectActor } = require("resource://devtools/server/actors/object.js");
      8 
      9 class PauseScopedObjectActor extends ObjectActor {
     10  /**
     11   * Creates a pause-scoped actor for the specified object.
     12   *
     13   * @see ObjectActor
     14   */
     15  constructor(threadActor, obj, hooks) {
     16    super(threadActor, obj, hooks);
     17 
     18    const guardWithPaused = [
     19      "decompile",
     20      "displayString",
     21      "ownPropertyNames",
     22      "parameterNames",
     23      "property",
     24      "prototype",
     25      "prototypeAndProperties",
     26      "scope",
     27    ];
     28 
     29    for (const methodName of guardWithPaused) {
     30      this[methodName] = this.withPaused(this[methodName]);
     31    }
     32 
     33    // Cache this thread actor attribute as we may query it after the actor destruction.
     34    this.threadLifetimePool = this.threadActor.threadLifetimePool;
     35  }
     36 
     37  isThreadLifetimePool() {
     38    return this.getParent() === this.threadLifetimePool;
     39  }
     40 
     41  isPaused() {
     42    return this.threadActor ? this.threadActor.state === "paused" : true;
     43  }
     44 
     45  withPaused(method) {
     46    return function () {
     47      if (this.isPaused()) {
     48        return method.apply(this, arguments);
     49      }
     50 
     51      return {
     52        error: "wrongState",
     53        message:
     54          this.constructor.name +
     55          " actors can only be accessed while the thread is paused.",
     56      };
     57    };
     58  }
     59 
     60  /**
     61   * Handle a protocol request to promote a pause-lifetime grip to a
     62   * thread-lifetime grip.
     63   *
     64   * This method isn't used by DevTools frontend, but by VS Code Firefox adapter
     65   * in order to keep the object actor alive after resume and be able to remove
     66   * watchpoints.
     67   */
     68  threadGrip() {
     69    this.threadActor.promoteObjectToThreadLifetime(this);
     70    return {};
     71  }
     72 
     73  /**
     74   * Handle a protocol request to release a thread-lifetime grip.
     75   */
     76  destroy() {
     77    if (!this.isThreadLifetimePool()) {
     78      return {
     79        error: "notReleasable",
     80        message: "Only thread-lifetime actors can be released.",
     81      };
     82    }
     83 
     84    super.destroy();
     85    return null;
     86  }
     87 }
     88 
     89 exports.PauseScopedObjectActor = PauseScopedObjectActor;