tor-browser

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

PageStyleParent.sys.mjs (2389B)


      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 PageStyleParent extends JSWindowActorParent {
      6  // This has the most recent information about the content stylesheets for
      7  // that actor. It's populated via the PageStyle:Add and PageStyle:Clear
      8  // messages from the content process. It has the following structure:
      9  //
     10  // filteredStyleSheets (Array):
     11  //   An Array of objects with a filtered list representing all stylesheets
     12  //   that the current page offers. Each object has the following members:
     13  //
     14  //   title (String):
     15  //     The title of the stylesheet
     16  //
     17  //   disabled (bool):
     18  //     Whether or not the stylesheet is currently applied
     19  //
     20  //   href (String):
     21  //     The URL of the stylesheet. Stylesheets loaded via a data URL will
     22  //     have this property set to null.
     23  //
     24  // preferredStyleSheetSet (bool):
     25  //   Whether or not the user currently has the "Default" style selected
     26  //   for the current page.
     27  #styleSheetInfo = null;
     28 
     29  receiveMessage(msg) {
     30    // Check if things are alive:
     31    let browser = this.browsingContext.top.embedderElement;
     32    if (!browser || browser.ownerGlobal.closed) {
     33      return;
     34    }
     35 
     36    // We always store information at the top of the frame tree.
     37    let actor =
     38      this.browsingContext.top.currentWindowGlobal.getActor("PageStyle");
     39    switch (msg.name) {
     40      case "PageStyle:Add":
     41        actor.addSheetInfo(msg.data);
     42        break;
     43      case "PageStyle:Clear":
     44        if (actor == this) {
     45          this.#styleSheetInfo = null;
     46        }
     47        break;
     48    }
     49  }
     50 
     51  /**
     52   * Add/append styleSheets to the _pageStyleSheets weakmap.
     53   *
     54   * @param newSheetData
     55   *        The stylesheet data, including new stylesheets to add,
     56   *        and the preferred stylesheet set for this document.
     57   */
     58  addSheetInfo(newSheetData) {
     59    let info = this.getSheetInfo();
     60    info.filteredStyleSheets.push(...newSheetData.filteredStyleSheets);
     61    info.preferredStyleSheetSet ||= newSheetData.preferredStyleSheetSet;
     62  }
     63 
     64  getSheetInfo() {
     65    if (!this.#styleSheetInfo) {
     66      this.#styleSheetInfo = {
     67        filteredStyleSheets: [],
     68        preferredStyleSheetSet: true,
     69      };
     70    }
     71    return this.#styleSheetInfo;
     72  }
     73 }