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