tor-browser

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

LightweightThemeChild.sys.mjs (2220B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /**
      6 * LightweightThemeChild forwards theme data to in-content pages.
      7 */
      8 export class LightweightThemeChild extends JSWindowActorChild {
      9  constructor() {
     10    super();
     11    this._initted = false;
     12    Services.cpmm.sharedData.addEventListener("change", this);
     13  }
     14 
     15  didDestroy() {
     16    Services.cpmm.sharedData.removeEventListener("change", this);
     17  }
     18 
     19  _getChromeOuterWindowID() {
     20    try {
     21      // Getting the browserChild throws an exception when it is null.
     22      let browserChild = this.docShell.browserChild;
     23      if (browserChild) {
     24        return browserChild.chromeOuterWindowID;
     25      }
     26    } catch (ex) {}
     27 
     28    if (
     29      Services.appinfo.processType === Services.appinfo.PROCESS_TYPE_DEFAULT
     30    ) {
     31      return this.browsingContext.topChromeWindow.docShell.outerWindowID;
     32    }
     33 
     34    // Return a false-y outerWindowID if we can't manage to get a proper one.
     35    // Note that no outerWindowID will ever have this ID.
     36    return 0;
     37  }
     38 
     39  /**
     40   * Handles "change" events on the child sharedData map, and notifies
     41   * our content page if its theme data was among the changed keys.
     42   */
     43  handleEvent(event) {
     44    switch (event.type) {
     45      // Make sure to update the theme data on first page show.
     46      case "pageshow":
     47      case "DOMContentLoaded":
     48        if (!this._initted && this._getChromeOuterWindowID()) {
     49          this._initted = true;
     50          this.update();
     51        }
     52        break;
     53 
     54      case "change":
     55        if (
     56          event.changedKeys.includes(`theme/${this._getChromeOuterWindowID()}`)
     57        ) {
     58          this.update();
     59        }
     60        break;
     61    }
     62  }
     63 
     64  /**
     65   * Forward the theme data to the page.
     66   */
     67  update() {
     68    const event = Cu.cloneInto(
     69      {
     70        detail: {
     71          data: Services.cpmm.sharedData.get(
     72            `theme/${this._getChromeOuterWindowID()}`
     73          ),
     74        },
     75      },
     76      this.contentWindow
     77    );
     78    this.contentWindow.dispatchEvent(
     79      new this.contentWindow.CustomEvent("LightweightTheme:Set", event)
     80    );
     81  }
     82 }