tor-browser

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

command.sys.mjs (2286B)


      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 import { Module } from "chrome://remote/content/shared/messagehandler/Module.sys.mjs";
      6 
      7 class CommandModule extends Module {
      8  constructor(messageHandler) {
      9    super(messageHandler);
     10    this._subscribedEvents = new Set();
     11 
     12    this._createdByMessageHandlerConstructor =
     13      this._isCreatedByMessageHandlerConstructor();
     14  }
     15  destroy() {}
     16 
     17  /**
     18   * Commands
     19   */
     20 
     21  _applySessionData(params) {
     22    if (params.category === "testCategory") {
     23      const filteredSessionData = params.sessionData.filter(item =>
     24        this.messageHandler.matchesContext(item.contextDescriptor)
     25      );
     26      for (const event of this._subscribedEvents.values()) {
     27        const hasSessionItem = filteredSessionData.some(
     28          item => item.value === event
     29        );
     30        // If there are no session items for this context, we should unsubscribe from the event.
     31        if (!hasSessionItem) {
     32          this._subscribedEvents.delete(event);
     33        }
     34      }
     35 
     36      // Subscribe to all events, which have an item in SessionData
     37      for (const { value } of filteredSessionData) {
     38        if (!this._subscribedEvents.has(value)) {
     39          this._subscribedEvents.add(value);
     40        }
     41      }
     42    }
     43 
     44    if (params.category === "browser_session_data_browser_element") {
     45      this.emitEvent("received-session-data", {
     46        contextId: this.messageHandler.contextId,
     47      });
     48    }
     49  }
     50 
     51  testWindowGlobalModule() {
     52    return "windowglobal-value";
     53  }
     54 
     55  testSetValue(params) {
     56    const { value } = params;
     57 
     58    this._testValue = value;
     59  }
     60 
     61  testGetValue() {
     62    return this._testValue;
     63  }
     64 
     65  testForwardToWindowGlobal() {
     66    return "forward-to-windowglobal-value";
     67  }
     68 
     69  testIsCreatedByMessageHandlerConstructor() {
     70    return this._createdByMessageHandlerConstructor;
     71  }
     72 
     73  _isCreatedByMessageHandlerConstructor() {
     74    let caller = Components.stack.caller;
     75    while (caller) {
     76      if (caller.name === this.messageHandler.constructor.name) {
     77        return true;
     78      }
     79      caller = caller.caller;
     80    }
     81    return false;
     82  }
     83 }
     84 
     85 export const command = CommandModule;