tor-browser

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

WebDriverProcessDataChild.sys.mjs (2462B)


      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  Log: "chrome://remote/content/shared/Log.sys.mjs",
      9  NodeCache: "chrome://remote/content/shared/webdriver/NodeCache.sys.mjs",
     10 });
     11 
     12 ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
     13 
     14 // Observer to clean-up element references for closed browsing contexts.
     15 class BrowsingContextObserver {
     16  constructor(actor) {
     17    this.actor = actor;
     18  }
     19 
     20  async observe(subject, topic) {
     21    if (topic === "browsing-context-discarded") {
     22      this.actor.cleanUp({ browsingContext: subject });
     23    }
     24  }
     25 }
     26 
     27 export class WebDriverProcessDataChild extends JSProcessActorChild {
     28  #browsingContextObserver;
     29  #nodeCache;
     30 
     31  constructor() {
     32    super();
     33 
     34    // For now have a single reference store only. Once multiple WebDriver
     35    // sessions are supported, it needs to be hashed by the session id.
     36    this.#nodeCache = new lazy.NodeCache();
     37 
     38    // Register observer to cleanup element references when a browsing context
     39    // gets destroyed.
     40    this.#browsingContextObserver = new BrowsingContextObserver(this);
     41    Services.obs.addObserver(
     42      this.#browsingContextObserver,
     43      "browsing-context-discarded"
     44    );
     45  }
     46 
     47  actorCreated() {
     48    lazy.logger.trace(
     49      `WebDriverProcessData actor created for PID ${Services.appinfo.processID}`
     50    );
     51  }
     52 
     53  didDestroy() {
     54    Services.obs.removeObserver(
     55      this.#browsingContextObserver,
     56      "browsing-context-discarded"
     57    );
     58  }
     59 
     60  /**
     61   * Clean up all the process specific data.
     62   *
     63   * @param {object=} options
     64   * @param {BrowsingContext=} options.browsingContext
     65   *     If specified only clear data living in that browsing context.
     66   */
     67  cleanUp(options = {}) {
     68    const { browsingContext = null } = options;
     69 
     70    this.#nodeCache.clear({ browsingContext });
     71  }
     72 
     73  /**
     74   * Get the node cache.
     75   *
     76   * @returns {NodeCache}
     77   *     The cache containing DOM node references.
     78   */
     79  getNodeCache() {
     80    return this.#nodeCache;
     81  }
     82 
     83  async receiveMessage(msg) {
     84    switch (msg.name) {
     85      case "WebDriverProcessDataParent:CleanUp":
     86        return this.cleanUp(msg.data);
     87      default:
     88        return Promise.reject(
     89          new Error(`Unexpected message received: ${msg.name}`)
     90        );
     91    }
     92  }
     93 }