Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | /** |
| 7 | * @implements {UI.ListWidget.Delegate} |
| 8 | * @unrestricted |
| 9 | */ |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame^] | 10 | export default class FrameworkBlackboxSettingsTab extends UI.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 11 | constructor() { |
| 12 | super(true); |
| 13 | this.registerRequiredCSS('settings/frameworkBlackboxSettingsTab.css'); |
| 14 | |
Chandani Shrestha | 97a703ee | 2019-06-03 22:45:11 | [diff] [blame] | 15 | const header = this.contentElement.createChild('div', 'header'); |
| 16 | header.textContent = ls`Framework Blackboxing`; |
| 17 | UI.ARIAUtils.markAsHeading(header, 1); |
Harley Li | 4ca2783 | 2019-05-23 23:14:51 | [diff] [blame] | 18 | this.contentElement.createChild('div', 'intro').textContent = |
| 19 | ls`Debugger will skip through the scripts and will not stop on exceptions thrown by them.`; |
| 20 | |
| 21 | const blackboxContentScripts = this.contentElement.createChild('div', 'blackbox-content-scripts'); |
| 22 | blackboxContentScripts.appendChild(UI.SettingsUI.createSettingCheckbox( |
| 23 | ls`Blackbox content scripts`, Common.moduleSetting('skipContentScripts'), true)); |
| 24 | blackboxContentScripts.title = ls`Blackbox content scripts (extension scripts in the page)`; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 25 | |
| 26 | this._blackboxLabel = Common.UIString('Blackbox'); |
| 27 | this._disabledLabel = Common.UIString('Disabled'); |
| 28 | |
| 29 | this._list = new UI.ListWidget(this); |
| 30 | this._list.element.classList.add('blackbox-list'); |
| 31 | this._list.registerRequiredCSS('settings/frameworkBlackboxSettingsTab.css'); |
| 32 | |
| 33 | const placeholder = createElementWithClass('div', 'blackbox-list-empty'); |
| 34 | placeholder.textContent = Common.UIString('No blackboxed patterns'); |
| 35 | this._list.setEmptyPlaceholder(placeholder); |
| 36 | this._list.show(this.contentElement); |
| 37 | const addPatternButton = |
| 38 | UI.createTextButton(Common.UIString('Add pattern...'), this._addButtonClicked.bind(this), 'add-button'); |
| 39 | this.contentElement.appendChild(addPatternButton); |
| 40 | |
| 41 | this._setting = Common.moduleSetting('skipStackFramesPattern'); |
| 42 | this._setting.addChangeListener(this._settingUpdated, this); |
| 43 | |
| 44 | this.setDefaultFocusedElement(addPatternButton); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @override |
| 49 | */ |
| 50 | wasShown() { |
| 51 | super.wasShown(); |
| 52 | this._settingUpdated(); |
| 53 | } |
| 54 | |
| 55 | _settingUpdated() { |
| 56 | this._list.clear(); |
| 57 | const patterns = this._setting.getAsArray(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 58 | for (let i = 0; i < patterns.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 59 | this._list.appendItem(patterns[i], true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 60 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | _addButtonClicked() { |
| 64 | this._list.addNewItem(this._setting.getAsArray().length, {pattern: '', disabled: false}); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @override |
| 69 | * @param {*} item |
| 70 | * @param {boolean} editable |
| 71 | * @return {!Element} |
| 72 | */ |
| 73 | renderItem(item, editable) { |
| 74 | const element = createElementWithClass('div', 'blackbox-list-item'); |
| 75 | const pattern = element.createChild('div', 'blackbox-pattern'); |
| 76 | pattern.textContent = item.pattern; |
Mandy Chen | 31930c4 | 2019-06-05 00:47:08 | [diff] [blame] | 77 | pattern.title = ls`Blackbox scripts whose names match '${item.pattern}'`; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 78 | element.createChild('div', 'blackbox-separator'); |
| 79 | element.createChild('div', 'blackbox-behavior').textContent = |
| 80 | item.disabled ? this._disabledLabel : this._blackboxLabel; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 81 | if (item.disabled) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 82 | element.classList.add('blackbox-disabled'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 83 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 84 | return element; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @override |
| 89 | * @param {*} item |
| 90 | * @param {number} index |
| 91 | */ |
| 92 | removeItemRequested(item, index) { |
| 93 | const patterns = this._setting.getAsArray(); |
| 94 | patterns.splice(index, 1); |
| 95 | this._setting.setAsArray(patterns); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @override |
| 100 | * @param {*} item |
| 101 | * @param {!UI.ListWidget.Editor} editor |
| 102 | * @param {boolean} isNew |
| 103 | */ |
| 104 | commitEdit(item, editor, isNew) { |
| 105 | item.pattern = editor.control('pattern').value.trim(); |
| 106 | item.disabled = editor.control('behavior').value === this._disabledLabel; |
| 107 | |
| 108 | const list = this._setting.getAsArray(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 109 | if (isNew) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | list.push(item); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 111 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 112 | this._setting.setAsArray(list); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @override |
| 117 | * @param {*} item |
| 118 | * @return {!UI.ListWidget.Editor} |
| 119 | */ |
| 120 | beginEdit(item) { |
| 121 | const editor = this._createEditor(); |
| 122 | editor.control('pattern').value = item.pattern; |
| 123 | editor.control('behavior').value = item.disabled ? this._disabledLabel : this._blackboxLabel; |
| 124 | return editor; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return {!UI.ListWidget.Editor} |
| 129 | */ |
| 130 | _createEditor() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 131 | if (this._editor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 132 | return this._editor; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 133 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 134 | |
| 135 | const editor = new UI.ListWidget.Editor(); |
| 136 | this._editor = editor; |
| 137 | const content = editor.contentElement(); |
| 138 | |
| 139 | const titles = content.createChild('div', 'blackbox-edit-row'); |
| 140 | titles.createChild('div', 'blackbox-pattern').textContent = Common.UIString('Pattern'); |
| 141 | titles.createChild('div', 'blackbox-separator blackbox-separator-invisible'); |
| 142 | titles.createChild('div', 'blackbox-behavior').textContent = Common.UIString('Behavior'); |
| 143 | |
| 144 | const fields = content.createChild('div', 'blackbox-edit-row'); |
Chandani Shrestha | 97a703ee | 2019-06-03 22:45:11 | [diff] [blame] | 145 | const pattern = editor.createInput('pattern', 'text', '/framework\\.js$', patternValidator.bind(this)); |
| 146 | UI.ARIAUtils.setAccessibleName(pattern, ls`Pattern`); |
| 147 | fields.createChild('div', 'blackbox-pattern').appendChild(pattern); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | fields.createChild('div', 'blackbox-separator blackbox-separator-invisible'); |
Chandani Shrestha | 97a703ee | 2019-06-03 22:45:11 | [diff] [blame] | 149 | const behavior = editor.createSelect('behavior', [this._blackboxLabel, this._disabledLabel], behaviorValidator); |
| 150 | UI.ARIAUtils.setAccessibleName(behavior, ls`Behavior`); |
| 151 | fields.createChild('div', 'blackbox-behavior').appendChild(behavior); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 152 | |
| 153 | return editor; |
| 154 | |
| 155 | /** |
| 156 | * @param {*} item |
| 157 | * @param {number} index |
| 158 | * @param {!HTMLInputElement|!HTMLSelectElement} input |
| 159 | * @this {Settings.FrameworkBlackboxSettingsTab} |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 160 | * @return {!UI.ListWidget.ValidatorResult} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 161 | */ |
| 162 | function patternValidator(item, index, input) { |
| 163 | const pattern = input.value.trim(); |
| 164 | const patterns = this._setting.getAsArray(); |
Chandani Shrestha | 810233b | 2019-07-29 20:04:16 | [diff] [blame] | 165 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 166 | if (!pattern.length) { |
Chandani Shrestha | 810233b | 2019-07-29 20:04:16 | [diff] [blame] | 167 | return {valid: false, errorMessage: ls`Pattern cannot be empty`}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 168 | } |
Chandani Shrestha | 810233b | 2019-07-29 20:04:16 | [diff] [blame] | 169 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 170 | for (let i = 0; i < patterns.length; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 171 | if (i !== index && patterns[i].pattern === pattern) { |
Chandani Shrestha | 810233b | 2019-07-29 20:04:16 | [diff] [blame] | 172 | return {valid: false, errorMessage: ls`Pattern already exists`}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 173 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | let regex; |
| 177 | try { |
| 178 | regex = new RegExp(pattern); |
| 179 | } catch (e) { |
| 180 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 181 | if (!regex) { |
Chandani Shrestha | 810233b | 2019-07-29 20:04:16 | [diff] [blame] | 182 | return {valid: false, errorMessage: ls`Pattern must be a valid regular expression`}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 183 | } else { |
Chandani Shrestha | 810233b | 2019-07-29 20:04:16 | [diff] [blame] | 184 | return {valid: true}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 185 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @param {*} item |
| 190 | * @param {number} index |
| 191 | * @param {!HTMLInputElement|!HTMLSelectElement} input |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 192 | * @return {!UI.ListWidget.ValidatorResult} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 193 | */ |
| 194 | function behaviorValidator(item, index, input) { |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 195 | return {valid: true}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 196 | } |
| 197 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame^] | 198 | } |
| 199 | |
| 200 | /* Legacy exported object */ |
| 201 | self.Settings = self.Settings || {}; |
| 202 | |
| 203 | /* Legacy exported object */ |
| 204 | Settings = Settings || {}; |
| 205 | |
| 206 | /** |
| 207 | * @constructor |
| 208 | */ |
| 209 | Settings.FrameworkBlackboxSettingsTab = FrameworkBlackboxSettingsTab; |