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