tor-browser

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

ipprotection-message-bar.mjs (3598B)


      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 https://mozilla.org/MPL/2.0/. */
      4 
      5 import { MozLitElement } from "chrome://global/content/lit-utils.mjs";
      6 import { html, ifDefined } from "chrome://global/content/vendor/lit.all.mjs";
      7 
      8 // eslint-disable-next-line import/no-unassigned-import
      9 import "chrome://global/content/elements/moz-message-bar.mjs";
     10 
     11 const lazy = {};
     12 
     13 ChromeUtils.defineESModuleGetters(lazy, {
     14  URILoadingHelper: "resource:///modules/URILoadingHelper.sys.mjs",
     15 });
     16 
     17 /**
     18 * A custom element that handles the message bar for IP Protection.
     19 */
     20 export default class IPProtectionMessageBarElement extends MozLitElement {
     21  #MESSAGE_TYPE_MAP = new Map([
     22    ["generic-error", () => this.genericErrorTemplate()],
     23 
     24    ["info", () => this.infoMessageTemplate()],
     25    ["warning", () => this.warningMessageTemplate()],
     26  ]);
     27  DISMISS_EVENT = "ipprotection-message-bar:user-dismissed";
     28 
     29  static queries = {
     30    mozMessageBarEl: "moz-message-bar",
     31  };
     32 
     33  static properties = {
     34    type: { type: String },
     35    messageId: { type: String },
     36    messageLink: { type: String },
     37    messageLinkl10nId: { type: String },
     38  };
     39 
     40  constructor() {
     41    super();
     42 
     43    this.handleDismiss = this.handleDismiss.bind(this);
     44    this.handleClickSetingsLink = this.handleClickSettingsLink.bind(this);
     45  }
     46 
     47  connectedCallback() {
     48    super.connectedCallback();
     49  }
     50 
     51  disconnectedCallback() {
     52    super.disconnectedCallback();
     53 
     54    if (this.mozMessageBarEl) {
     55      this.mozMessageBarEl.removeEventListener(
     56        "message-bar:user-dismissed",
     57        this.handleDismiss
     58      );
     59    }
     60  }
     61 
     62  handleDismiss() {
     63    // Dispatch an ipprotection specific dismiss event to wrapping components to notify them
     64    // when the message was actually dismissed.
     65    this.dispatchEvent(
     66      new CustomEvent(this.DISMISS_EVENT, { bubbles: true, composed: true })
     67    );
     68  }
     69 
     70  genericErrorTemplate() {
     71    return html`
     72      <moz-message-bar
     73        type="error"
     74        data-l10n-id=${ifDefined(this.messageId)}
     75        dismissable
     76      >
     77      </moz-message-bar>
     78    `;
     79  }
     80 
     81  infoMessageTemplate() {
     82    return html`
     83      <moz-message-bar type="info" dismissable>
     84        <span
     85          slot="message"
     86          data-l10n-id=${ifDefined(this.messageId)}
     87          @click=${this.handleClickSettingsLink}
     88        >
     89          <a
     90            data-l10n-name=${ifDefined(this.messageLinkl10nId)}
     91            href=${ifDefined(this.messageLink)}
     92          ></a>
     93        </span>
     94      </moz-message-bar>
     95    `;
     96  }
     97 
     98  warningMessageTemplate() {
     99    return html`
    100      <moz-message-bar
    101        type="warning"
    102        data-l10n-id=${ifDefined(this.messageId)}
    103        dismissable
    104      >
    105      </moz-message-bar>
    106    `;
    107  }
    108 
    109  firstUpdated() {
    110    this.mozMessageBarEl.addEventListener(
    111      "message-bar:user-dismissed",
    112      this.handleDismiss,
    113      {
    114        once: true,
    115      }
    116    );
    117  }
    118 
    119  handleClickSettingsLink(event) {
    120    if (event.target.hasAttribute("href")) {
    121      event.preventDefault();
    122      lazy.URILoadingHelper.openTrustedLinkIn(window, this.messageLink, "tab");
    123 
    124      this.dispatchEvent(
    125        new CustomEvent("IPProtection:Close", { bubbles: true, composed: true })
    126      );
    127    }
    128  }
    129 
    130  render() {
    131    let messageBarTemplate = this.#MESSAGE_TYPE_MAP.get(this.type)();
    132 
    133    if (!messageBarTemplate) {
    134      return null;
    135    }
    136 
    137    return html` ${messageBarTemplate} `;
    138  }
    139 }
    140 
    141 customElements.define(
    142  "ipprotection-message-bar",
    143  IPProtectionMessageBarElement
    144 );