tor-browser

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

AboutMessagePreviewChild.sys.mjs (1752B)


      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 export class AboutMessagePreviewChild extends JSWindowActorChild {
      6  handleEvent(event) {
      7    console.log(`Received page event ${event.type}`);
      8  }
      9 
     10  actorCreated() {
     11    this.exportFunctions();
     12  }
     13 
     14  exportFunctions() {
     15    if (this.contentWindow) {
     16      for (const name of [
     17        "MPShowMessage",
     18        "MPIsEnabled",
     19        "MPShouldShowHint",
     20        "MPToggleLights",
     21      ]) {
     22        Cu.exportFunction(this[name].bind(this), this.contentWindow, {
     23          defineAs: name,
     24        });
     25      }
     26    }
     27  }
     28 
     29  /**
     30   * Check if the Message Preview feature is enabled. This reflects the value of
     31   * the pref `browser.newtabpage.activity-stream.asrouter.devtoolsEnabled`.
     32   *
     33   * @returns {boolean}
     34   */
     35  MPIsEnabled() {
     36    return Services.prefs.getBoolPref(
     37      "browser.newtabpage.activity-stream.asrouter.devtoolsEnabled",
     38      false
     39    );
     40  }
     41 
     42  /**
     43   * Check the browser theme and switch it.
     44   */
     45  MPToggleLights() {
     46    const isDark = this.contentWindow.matchMedia(
     47      "(prefers-color-scheme: dark)"
     48    ).matches;
     49    this.sendAsyncMessage(`MessagePreview:CHANGE_THEME`, { isDark });
     50  }
     51 
     52  /**
     53   * Route a message to the parent process to be displayed with the relevant
     54   * messaging surface.
     55   *
     56   * @param {object} message
     57   */
     58  MPShowMessage(message) {
     59    this.sendAsyncMessage(`MessagePreview:SHOW_MESSAGE`, message);
     60  }
     61 
     62  /**
     63   * Check if a hint should be shown about how to enable Message Preview.
     64   *
     65   * @returns {boolean}
     66   */
     67  MPShouldShowHint() {
     68    return !this.MPIsEnabled();
     69  }
     70 }