tor-browser

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

NetworkThrottlingMenu.js (3981B)


      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 "use strict";
      6 
      7 const {
      8  createFactory,
      9  PureComponent,
     10 } = require("resource://devtools/client/shared/vendor/react.mjs");
     11 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
     12 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs");
     13 
     14 const throttlingProfiles = require("resource://devtools/client/shared/components/throttling/profiles.js");
     15 const Types = require("resource://devtools/client/shared/components/throttling/types.js");
     16 
     17 // Localization
     18 const { LocalizationHelper } = require("resource://devtools/shared/l10n.js");
     19 const L10N = new LocalizationHelper(
     20  "devtools/client/locales/network-throttling.properties"
     21 );
     22 const NO_THROTTLING_LABEL = L10N.getStr("responsive.noThrottling");
     23 const OFFLINE_LABEL = L10N.getStr("responsive.offline");
     24 
     25 loader.lazyGetter(this, "MenuItem", () => {
     26  const menuItemClass = require("resource://devtools/client/shared/components/menu/MenuItem.js");
     27  const menuItem = createFactory(menuItemClass);
     28  menuItem.DUMMY_ICON = menuItemClass.DUMMY_ICON;
     29  return menuItem;
     30 });
     31 
     32 loader.lazyGetter(this, "MenuButton", () => {
     33  return createFactory(
     34    require("resource://devtools/client/shared/components/menu/MenuButton.js")
     35  );
     36 });
     37 loader.lazyGetter(this, "MenuList", () => {
     38  return createFactory(
     39    require("resource://devtools/client/shared/components/menu/MenuList.js")
     40  );
     41 });
     42 
     43 /**
     44 * This component represents selector button that can be used
     45 * to throttle network bandwidth.
     46 */
     47 class NetworkThrottlingMenu extends PureComponent {
     48  static get propTypes() {
     49    return {
     50      networkThrottling: PropTypes.shape(Types.networkThrottling).isRequired,
     51      onChangeNetworkThrottling: PropTypes.func.isRequired,
     52      toolboxDoc: PropTypes.object.isRequired,
     53    };
     54  }
     55 
     56  renderThrottlingMenu() {
     57    const { networkThrottling, onChangeNetworkThrottling } = this.props;
     58 
     59    const menuItems = throttlingProfiles.profiles.map(profile => {
     60      const isOffline =
     61        throttlingProfiles.PROFILE_CONSTANTS.OFFLINE === profile.id;
     62      return MenuItem({
     63        id: profile.id,
     64        key: profile.id,
     65        className: "network-throttling-item",
     66        checked:
     67          networkThrottling.enabled && profile.id == networkThrottling.profile,
     68        icon: null,
     69        label: isOffline ? OFFLINE_LABEL : profile.menuItemLabel,
     70        tooltip: isOffline ? OFFLINE_LABEL : profile.description,
     71        onClick: () => onChangeNetworkThrottling(true, profile.id),
     72      });
     73    });
     74 
     75    menuItems.unshift(dom.hr({ key: "separator" }));
     76 
     77    menuItems.unshift(
     78      MenuItem({
     79        id: NO_THROTTLING_LABEL,
     80        key: NO_THROTTLING_LABEL,
     81        className: "network-throttling-item",
     82        checked: !networkThrottling.enabled,
     83        icon: null,
     84        label: NO_THROTTLING_LABEL,
     85        tooltip: NO_THROTTLING_LABEL,
     86        onClick: () => onChangeNetworkThrottling(false, ""),
     87      })
     88    );
     89    return MenuList({}, menuItems);
     90  }
     91 
     92  render() {
     93    const { networkThrottling, toolboxDoc } = this.props;
     94    const label = networkThrottling.enabled
     95      ? networkThrottling.profile
     96      : NO_THROTTLING_LABEL;
     97 
     98    let title = NO_THROTTLING_LABEL;
     99 
    100    if (networkThrottling.enabled) {
    101      const id = networkThrottling.profile;
    102      const selectedProfile = throttlingProfiles.profiles.find(
    103        profile => profile.id === id
    104      );
    105      title = selectedProfile.description;
    106    }
    107 
    108    return MenuButton(
    109      {
    110        id: "network-throttling",
    111        menuId: "network-throttling-menu",
    112        toolboxDoc,
    113        className: "devtools-button devtools-dropdown-button",
    114        icon: null,
    115        label,
    116        title,
    117      },
    118      () => this.renderThrottlingMenu()
    119    );
    120  }
    121 }
    122 
    123 module.exports = NetworkThrottlingMenu;