summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/components/ChromeDebugToolbar.js
blob: b126cf78fd0f2372aceb5042607a8b4b9198fd65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const {
  PureComponent,
  createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");

const FluentReact = require("resource://devtools/client/shared/vendor/fluent-react.js");
const Localized = createFactory(FluentReact.Localized);

const MODE_PREF = "devtools.browsertoolbox.scope";

const MODE_VALUES = {
  PARENT_PROCESS: "parent-process",
  EVERYTHING: "everything",
};

const MODE_DATA = {
  [MODE_VALUES.PARENT_PROCESS]: {
    containerL10nId: "toolbox-mode-parent-process-container",
    labelL10nId: "toolbox-mode-parent-process-label",
    subLabelL10nId: "toolbox-mode-parent-process-sub-label",
  },
  [MODE_VALUES.EVERYTHING]: {
    containerL10nId: "toolbox-mode-everything-container",
    labelL10nId: "toolbox-mode-everything-label",
    subLabelL10nId: "toolbox-mode-everything-sub-label",
  },
};

/**
 * Toolbar displayed on top of the regular toolbar in the Browser Toolbox and Browser Console,
 * displaying chrome-debugging-specific options.
 */
class ChromeDebugToolbar extends PureComponent {
  static get propTypes() {
    return {
      isBrowserConsole: PropTypes.bool,
    };
  }

  constructor(props) {
    super(props);

    this.state = {
      mode: Services.prefs.getCharPref(MODE_PREF),
    };

    this.onModePrefChanged = this.onModePrefChanged.bind(this);
    Services.prefs.addObserver(MODE_PREF, this.onModePrefChanged);
  }

  componentWillUnmount() {
    Services.prefs.removeObserver(MODE_PREF, this.onModePrefChanged);
  }

  onModePrefChanged() {
    this.setState({
      mode: Services.prefs.getCharPref(MODE_PREF),
    });
  }

  renderModeItem(value) {
    const { containerL10nId, labelL10nId, subLabelL10nId } = MODE_DATA[value];

    const checked = this.state.mode == value;
    return Localized(
      {
        id: containerL10nId,
        attrs: { title: true },
      },
      dom.label(
        {
          className: checked ? "selected" : null,
        },
        dom.input({
          type: `radio`,
          name: `chrome-debug-mode`,
          value,
          checked: checked || null,
          onChange: () => {
            Services.prefs.setCharPref(MODE_PREF, value);
          },
        }),
        Localized({ id: labelL10nId }, dom.span({ className: "mode__label" })),
        Localized(
          { id: subLabelL10nId },
          dom.span({ className: "mode__sublabel" })
        )
      )
    );
  }

  render() {
    return dom.header(
      {
        className: "chrome-debug-toolbar",
      },
      dom.section(
        {
          className: "chrome-debug-toolbar__modes",
        },
        Localized(
          {
            id: this.props.isBrowserConsole
              ? "toolbox-mode-browser-console-label"
              : "toolbox-mode-browser-toolbox-label",
          },
          dom.h3({})
        ),
        this.renderModeItem(MODE_VALUES.PARENT_PROCESS),
        this.renderModeItem(MODE_VALUES.EVERYTHING)
      )
    );
  }
}

module.exports = ChromeDebugToolbar;