tor-browser

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

ext-pageAction.js (12620B)


      1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim: set sts=2 sw=2 et tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 "use strict";
      8 
      9 ChromeUtils.defineESModuleGetters(this, {
     10  BrowserUsageTelemetry: "resource:///modules/BrowserUsageTelemetry.sys.mjs",
     11  ExtensionTelemetry: "resource://gre/modules/ExtensionTelemetry.sys.mjs",
     12  PageActions: "resource:///modules/PageActions.sys.mjs",
     13  PanelPopup: "resource:///modules/ExtensionPopups.sys.mjs",
     14 });
     15 
     16 var { DefaultWeakMap } = ExtensionUtils;
     17 
     18 var { ExtensionParent } = ChromeUtils.importESModule(
     19  "resource://gre/modules/ExtensionParent.sys.mjs"
     20 );
     21 var { PageActionBase } = ChromeUtils.importESModule(
     22  "resource://gre/modules/ExtensionActions.sys.mjs"
     23 );
     24 
     25 // WeakMap[Extension -> PageAction]
     26 let pageActionMap = new WeakMap();
     27 
     28 class PageAction extends PageActionBase {
     29  constructor(extension, buttonDelegate) {
     30    let tabContext = new TabContext(() => this.getContextData(null));
     31    super(tabContext, extension);
     32    this.buttonDelegate = buttonDelegate;
     33  }
     34 
     35  updateOnChange(target) {
     36    this.buttonDelegate.updateButton(target.ownerGlobal);
     37  }
     38 
     39  dispatchClick(tab, clickInfo) {
     40    this.buttonDelegate.emit("click", tab, clickInfo);
     41  }
     42 
     43  getTab(tabId) {
     44    if (tabId !== null) {
     45      return tabTracker.getTab(tabId);
     46    }
     47    return null;
     48  }
     49 }
     50 
     51 this.pageAction = class extends ExtensionAPIPersistent {
     52  static for(extension) {
     53    return pageActionMap.get(extension);
     54  }
     55 
     56  static onUpdate(id, manifest) {
     57    if (!("page_action" in manifest)) {
     58      // If the new version has no page action then mark this widget as hidden
     59      // in the telemetry. If it is already marked hidden then this will do
     60      // nothing.
     61      BrowserUsageTelemetry.recordWidgetChange(makeWidgetId(id), null, "addon");
     62    }
     63  }
     64 
     65  static onDisable(id) {
     66    BrowserUsageTelemetry.recordWidgetChange(makeWidgetId(id), null, "addon");
     67  }
     68 
     69  static onUninstall(id) {
     70    // If the telemetry already has this widget as hidden then this will not
     71    // record anything.
     72    BrowserUsageTelemetry.recordWidgetChange(makeWidgetId(id), null, "addon");
     73  }
     74 
     75  async onManifestEntry() {
     76    let { extension } = this;
     77    let options = extension.manifest.page_action;
     78 
     79    this.action = new PageAction(extension, this);
     80    await this.action.loadIconData();
     81 
     82    let widgetId = makeWidgetId(extension.id);
     83    this.id = widgetId + "-page-action";
     84 
     85    this.tabManager = extension.tabManager;
     86 
     87    this.browserStyle = options.browser_style;
     88 
     89    pageActionMap.set(extension, this);
     90 
     91    this.lastValues = new DefaultWeakMap(() => ({}));
     92 
     93    if (!this.browserPageAction) {
     94      let onPlacedHandler = (buttonNode, isPanel) => {
     95        // eslint-disable-next-line mozilla/balanced-listeners
     96        buttonNode.addEventListener("auxclick", event => {
     97          if (event.button !== 1 || event.target.disabled) {
     98            return;
     99          }
    100 
    101          // The panel is not automatically closed when middle-clicked.
    102          if (isPanel) {
    103            buttonNode.closest("#pageActionPanel").hidePopup();
    104          }
    105          let window = event.target.ownerGlobal;
    106          let tab = window.gBrowser.selectedTab;
    107          this.tabManager.addActiveTabPermission(tab);
    108          this.action.dispatchClick(tab, {
    109            button: event.button,
    110            modifiers: clickModifiersFromEvent(event),
    111          });
    112        });
    113      };
    114 
    115      this.browserPageAction = PageActions.addAction(
    116        new PageActions.Action({
    117          id: widgetId,
    118          extensionID: extension.id,
    119          title: this.action.getProperty(null, "title"),
    120          iconURL: this.action.getProperty(null, "icon"),
    121          pinnedToUrlbar: this.action.getPinned(),
    122          disabled: !this.action.getProperty(null, "enabled"),
    123          onCommand: event => {
    124            this.handleClick(event.target.ownerGlobal, {
    125              button: event.button || 0,
    126              modifiers: clickModifiersFromEvent(event),
    127            });
    128          },
    129          onBeforePlacedInWindow: browserWindow => {
    130            if (
    131              this.extension.hasPermission("menus") ||
    132              this.extension.hasPermission("contextMenus")
    133            ) {
    134              browserWindow.document.addEventListener("popupshowing", this);
    135            }
    136          },
    137          onPlacedInPanel: buttonNode => onPlacedHandler(buttonNode, true),
    138          onPlacedInUrlbar: buttonNode => onPlacedHandler(buttonNode, false),
    139          onRemovedFromWindow: browserWindow => {
    140            browserWindow.document.removeEventListener("popupshowing", this);
    141          },
    142        })
    143      );
    144 
    145      if (this.extension.startupReason != "APP_STARTUP") {
    146        // Make sure the browser telemetry has the correct state for this widget.
    147        // Defer loading BrowserUsageTelemetry until after startup is complete.
    148        ExtensionParent.browserStartupPromise.then(() => {
    149          BrowserUsageTelemetry.recordWidgetChange(
    150            widgetId,
    151            this.browserPageAction.pinnedToUrlbar
    152              ? "page-action-buttons"
    153              : null,
    154            "addon"
    155          );
    156        });
    157      }
    158 
    159      // If the page action is only enabled in some URLs, do pattern matching in
    160      // the active tabs and update the button if necessary.
    161      if (this.action.getProperty(null, "enabled") === undefined) {
    162        for (let window of windowTracker.browserWindows()) {
    163          let tab = window.gBrowser.selectedTab;
    164          if (this.action.isShownForTab(tab)) {
    165            this.updateButton(window);
    166          }
    167        }
    168      }
    169    }
    170  }
    171 
    172  onShutdown(isAppShutdown) {
    173    pageActionMap.delete(this.extension);
    174    this.action.onShutdown();
    175 
    176    // Removing the browser page action causes PageActions to forget about it
    177    // across app restarts, so don't remove it on app shutdown, but do remove
    178    // it on all other shutdowns since there's no guarantee the action will be
    179    // coming back.
    180    if (!isAppShutdown && this.browserPageAction) {
    181      this.browserPageAction.remove();
    182      this.browserPageAction = null;
    183    }
    184  }
    185 
    186  // Updates the page action button in the given window to reflect the
    187  // properties of the currently selected tab:
    188  //
    189  // Updates "tooltiptext" and "aria-label" to match "title" property.
    190  // Updates "image" to match the "icon" property.
    191  // Enables or disables the icon, based on the "enabled" and "patternMatching" properties.
    192  updateButton(window) {
    193    let tab = window.gBrowser.selectedTab;
    194    let tabData = this.action.getContextData(tab);
    195    let last = this.lastValues.get(window);
    196 
    197    window.requestAnimationFrame(() => {
    198      // If we get called just before shutdown, we might have been destroyed by
    199      // this point.
    200      if (!this.browserPageAction) {
    201        return;
    202      }
    203 
    204      let title = tabData.title || this.extension.name;
    205      if (last.title !== title) {
    206        this.browserPageAction.setTitle(title, window);
    207        last.title = title;
    208      }
    209 
    210      let enabled =
    211        tabData.enabled != null ? tabData.enabled : tabData.patternMatching;
    212      if (last.enabled !== enabled) {
    213        this.browserPageAction.setDisabled(!enabled, window);
    214        last.enabled = enabled;
    215      }
    216 
    217      let icon = tabData.icon;
    218      if (last.icon !== icon) {
    219        this.browserPageAction.setIconURL(icon, window);
    220        last.icon = icon;
    221      }
    222    });
    223  }
    224 
    225  /**
    226   * Triggers this page action for the given window, with the same effects as
    227   * if it were clicked by a user.
    228   *
    229   * This has no effect if the page action is hidden for the selected tab.
    230   *
    231   * @param {Window} window
    232   */
    233  triggerAction(window) {
    234    this.handleClick(window, { button: 0, modifiers: [] });
    235  }
    236 
    237  handleEvent(event) {
    238    switch (event.type) {
    239      case "popupshowing": {
    240        const menu = event.target;
    241        const trigger = menu.triggerNode;
    242        const getActionId = () => {
    243          let actionId = trigger.getAttribute("actionid");
    244          if (actionId) {
    245            return actionId;
    246          }
    247          // When a page action is clicked, triggerNode will be an ancestor of
    248          // a node corresponding to an action. triggerNode will be the page
    249          // action node itself when a page action is selected with the
    250          // keyboard. That's because the semantic meaning of page action is on
    251          // an hbox that contains an <image>.
    252          for (let n = trigger; n && !actionId; n = n.parentElement) {
    253            if (n.id == "page-action-buttons" || n.localName == "panelview") {
    254              // We reached the page-action-buttons or panelview container.
    255              // Stop looking; no action was found.
    256              break;
    257            }
    258            actionId = n.getAttribute("actionid");
    259          }
    260          return actionId;
    261        };
    262        if (
    263          menu.id === "pageActionContextMenu" &&
    264          trigger &&
    265          getActionId() === this.browserPageAction.id &&
    266          !this.browserPageAction.getDisabled(trigger.ownerGlobal) &&
    267          (this.extension.hasPermission("contextMenus") ||
    268            this.extension.hasPermission("menus"))
    269        ) {
    270          global.actionContextMenu({
    271            extension: this.extension,
    272            onPageAction: true,
    273            menu: menu,
    274          });
    275        }
    276        break;
    277      }
    278    }
    279  }
    280 
    281  // Handles a click event on the page action button for the given
    282  // window.
    283  // If the page action has a |popup| property, a panel is opened to
    284  // that URL. Otherwise, a "click" event is emitted, and dispatched to
    285  // the any click listeners in the add-on.
    286  async handleClick(window, clickInfo) {
    287    const { extension } = this;
    288 
    289    ExtensionTelemetry.pageActionPopupOpen.stopwatchStart(extension, this);
    290    let tab = window.gBrowser.selectedTab;
    291    let popupURL = this.action.triggerClickOrPopup(tab, clickInfo);
    292 
    293    // If the widget has a popup URL defined, we open a popup, but do not
    294    // dispatch a click event to the extension.
    295    // If it has no popup URL defined, we dispatch a click event, but do not
    296    // open a popup.
    297    if (popupURL) {
    298      if (this.popupNode && this.popupNode.panel.state !== "closed") {
    299        // The panel is being toggled closed.
    300        ExtensionTelemetry.pageActionPopupOpen.stopwatchCancel(extension, this);
    301        window.BrowserPageActions.togglePanelForAction(
    302          this.browserPageAction,
    303          this.popupNode.panel
    304        );
    305        return;
    306      }
    307 
    308      this.popupNode = new PanelPopup(
    309        extension,
    310        window.document,
    311        popupURL,
    312        this.browserStyle
    313      );
    314      // Remove popupNode when it is closed.
    315      this.popupNode.panel.addEventListener(
    316        "popuphiding",
    317        () => {
    318          this.popupNode = undefined;
    319        },
    320        { once: true }
    321      );
    322      await this.popupNode.contentReady;
    323      window.BrowserPageActions.togglePanelForAction(
    324        this.browserPageAction,
    325        this.popupNode.panel
    326      );
    327      ExtensionTelemetry.pageActionPopupOpen.stopwatchFinish(extension, this);
    328    } else {
    329      ExtensionTelemetry.pageActionPopupOpen.stopwatchCancel(extension, this);
    330    }
    331  }
    332 
    333  PERSISTENT_EVENTS = {
    334    onClicked({ context, fire }) {
    335      const { extension } = this;
    336      const { tabManager } = extension;
    337 
    338      let listener = async (_event, tab, clickInfo) => {
    339        if (fire.wakeup) {
    340          await fire.wakeup();
    341        }
    342        // TODO: we should double-check if the tab is already being closed by the time
    343        // the background script got started and we converted the primed listener.
    344        context?.withPendingBrowser(tab.linkedBrowser, () =>
    345          fire.sync(tabManager.convert(tab), clickInfo)
    346        );
    347      };
    348 
    349      this.on("click", listener);
    350      return {
    351        unregister: () => {
    352          this.off("click", listener);
    353        },
    354        convert(newFire, extContext) {
    355          fire = newFire;
    356          context = extContext;
    357        },
    358      };
    359    },
    360  };
    361 
    362  getAPI(context) {
    363    const { action } = this;
    364 
    365    return {
    366      pageAction: {
    367        ...action.api(context),
    368 
    369        onClicked: new EventManager({
    370          context,
    371          module: "pageAction",
    372          event: "onClicked",
    373          inputHandling: true,
    374          extensionApi: this,
    375        }).api(),
    376 
    377        openPopup: () => {
    378          let window = windowTracker.topWindow;
    379          this.triggerAction(window);
    380        },
    381      },
    382    };
    383  }
    384 };
    385 
    386 global.pageActionFor = this.pageAction.for;