| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @implements {WebInspector.TargetManager.Observer} | 5 * @implements {SDK.TargetManager.Observer} |
| 6 * @unrestricted | 6 * @unrestricted |
| 7 */ | 7 */ |
| 8 WebInspector.ConsoleContextSelector = class { | 8 Console.ConsoleContextSelector = class { |
| 9 /** | 9 /** |
| 10 * @param {!Element} selectElement | 10 * @param {!Element} selectElement |
| 11 */ | 11 */ |
| 12 constructor(selectElement) { | 12 constructor(selectElement) { |
| 13 this._selectElement = selectElement; | 13 this._selectElement = selectElement; |
| 14 /** | 14 /** |
| 15 * @type {!Map.<!WebInspector.ExecutionContext, !Element>} | 15 * @type {!Map.<!SDK.ExecutionContext, !Element>} |
| 16 */ | 16 */ |
| 17 this._optionByExecutionContext = new Map(); | 17 this._optionByExecutionContext = new Map(); |
| 18 | 18 |
| 19 WebInspector.targetManager.observeTargets(this); | 19 SDK.targetManager.observeTargets(this); |
| 20 WebInspector.targetManager.addModelListener( | 20 SDK.targetManager.addModelListener( |
| 21 WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionCon
textCreated, | 21 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, |
| 22 this._onExecutionContextCreated, this); | 22 this._onExecutionContextCreated, this); |
| 23 WebInspector.targetManager.addModelListener( | 23 SDK.targetManager.addModelListener( |
| 24 WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionCon
textChanged, | 24 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, |
| 25 this._onExecutionContextChanged, this); | 25 this._onExecutionContextChanged, this); |
| 26 WebInspector.targetManager.addModelListener( | 26 SDK.targetManager.addModelListener( |
| 27 WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionCon
textDestroyed, | 27 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, |
| 28 this._onExecutionContextDestroyed, this); | 28 this._onExecutionContextDestroyed, this); |
| 29 | 29 |
| 30 this._selectElement.addEventListener('change', this._executionContextChanged
.bind(this), false); | 30 this._selectElement.addEventListener('change', this._executionContextChanged
.bind(this), false); |
| 31 WebInspector.context.addFlavorChangeListener( | 31 UI.context.addFlavorChangeListener( |
| 32 WebInspector.ExecutionContext, this._executionContextChangedExternally,
this); | 32 SDK.ExecutionContext, this._executionContextChangedExternally, this); |
| 33 } | 33 } |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * @param {!WebInspector.ExecutionContext} executionContext | 36 * @param {!SDK.ExecutionContext} executionContext |
| 37 * @return {string} | 37 * @return {string} |
| 38 */ | 38 */ |
| 39 _titleFor(executionContext) { | 39 _titleFor(executionContext) { |
| 40 var result; | 40 var result; |
| 41 if (executionContext.isDefault) { | 41 if (executionContext.isDefault) { |
| 42 if (executionContext.frameId) { | 42 if (executionContext.frameId) { |
| 43 var resourceTreeModel = WebInspector.ResourceTreeModel.fromTarget(execut
ionContext.target()); | 43 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContex
t.target()); |
| 44 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionC
ontext.frameId); | 44 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionC
ontext.frameId); |
| 45 result = frame ? frame.displayName() : executionContext.label(); | 45 result = frame ? frame.displayName() : executionContext.label(); |
| 46 } else { | 46 } else { |
| 47 result = executionContext.target().decorateLabel(executionContext.label(
)); | 47 result = executionContext.target().decorateLabel(executionContext.label(
)); |
| 48 } | 48 } |
| 49 } else { | 49 } else { |
| 50 result = '\u00a0\u00a0\u00a0\u00a0' + (executionContext.label() || executi
onContext.origin); | 50 result = '\u00a0\u00a0\u00a0\u00a0' + (executionContext.label() || executi
onContext.origin); |
| 51 } | 51 } |
| 52 | 52 |
| 53 var maxLength = 50; | 53 var maxLength = 50; |
| 54 return result.trimMiddle(maxLength); | 54 return result.trimMiddle(maxLength); |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * @param {!WebInspector.ExecutionContext} executionContext | 58 * @param {!SDK.ExecutionContext} executionContext |
| 59 */ | 59 */ |
| 60 _executionContextCreated(executionContext) { | 60 _executionContextCreated(executionContext) { |
| 61 // FIXME(413886): We never want to show execution context for the main threa
d of shadow page in service/shared worker frontend. | 61 // FIXME(413886): We never want to show execution context for the main threa
d of shadow page in service/shared worker frontend. |
| 62 // This check could be removed once we do not send this context to frontend. | 62 // This check could be removed once we do not send this context to frontend. |
| 63 if (!executionContext.target().hasJSCapability()) | 63 if (!executionContext.target().hasJSCapability()) |
| 64 return; | 64 return; |
| 65 | 65 |
| 66 var newOption = createElement('option'); | 66 var newOption = createElement('option'); |
| 67 newOption.__executionContext = executionContext; | 67 newOption.__executionContext = executionContext; |
| 68 newOption.text = this._titleFor(executionContext); | 68 newOption.text = this._titleFor(executionContext); |
| 69 this._optionByExecutionContext.set(executionContext, newOption); | 69 this._optionByExecutionContext.set(executionContext, newOption); |
| 70 var options = this._selectElement.options; | 70 var options = this._selectElement.options; |
| 71 var contexts = Array.prototype.map.call(options, mapping); | 71 var contexts = Array.prototype.map.call(options, mapping); |
| 72 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo
del.executionContextComparator()); | 72 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo
del.executionContextComparator()); |
| 73 this._selectElement.insertBefore(newOption, options[index]); | 73 this._selectElement.insertBefore(newOption, options[index]); |
| 74 | 74 |
| 75 if (executionContext === WebInspector.context.flavor(WebInspector.ExecutionC
ontext)) | 75 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) |
| 76 this._select(newOption); | 76 this._select(newOption); |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * @param {!Element} option | 79 * @param {!Element} option |
| 80 * @return {!WebInspector.ExecutionContext} | 80 * @return {!SDK.ExecutionContext} |
| 81 */ | 81 */ |
| 82 function mapping(option) { | 82 function mapping(option) { |
| 83 return option.__executionContext; | 83 return option.__executionContext; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 /** | 87 /** |
| 88 * @param {!WebInspector.Event} event | 88 * @param {!Common.Event} event |
| 89 */ | 89 */ |
| 90 _onExecutionContextCreated(event) { | 90 _onExecutionContextCreated(event) { |
| 91 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (event.
data); | 91 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
| 92 this._executionContextCreated(executionContext); | 92 this._executionContextCreated(executionContext); |
| 93 this._updateSelectionWarning(); | 93 this._updateSelectionWarning(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * @param {!WebInspector.Event} event | 97 * @param {!Common.Event} event |
| 98 */ | 98 */ |
| 99 _onExecutionContextChanged(event) { | 99 _onExecutionContextChanged(event) { |
| 100 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (event.
data); | 100 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
| 101 var option = this._optionByExecutionContext.get(executionContext); | 101 var option = this._optionByExecutionContext.get(executionContext); |
| 102 if (option) | 102 if (option) |
| 103 option.text = this._titleFor(executionContext); | 103 option.text = this._titleFor(executionContext); |
| 104 this._updateSelectionWarning(); | 104 this._updateSelectionWarning(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 /** | 107 /** |
| 108 * @param {!WebInspector.ExecutionContext} executionContext | 108 * @param {!SDK.ExecutionContext} executionContext |
| 109 */ | 109 */ |
| 110 _executionContextDestroyed(executionContext) { | 110 _executionContextDestroyed(executionContext) { |
| 111 var option = this._optionByExecutionContext.remove(executionContext); | 111 var option = this._optionByExecutionContext.remove(executionContext); |
| 112 option.remove(); | 112 option.remove(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 /** | 115 /** |
| 116 * @param {!WebInspector.Event} event | 116 * @param {!Common.Event} event |
| 117 */ | 117 */ |
| 118 _onExecutionContextDestroyed(event) { | 118 _onExecutionContextDestroyed(event) { |
| 119 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (event.
data); | 119 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
| 120 this._executionContextDestroyed(executionContext); | 120 this._executionContextDestroyed(executionContext); |
| 121 this._updateSelectionWarning(); | 121 this._updateSelectionWarning(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 /** | 124 /** |
| 125 * @param {!WebInspector.Event} event | 125 * @param {!Common.Event} event |
| 126 */ | 126 */ |
| 127 _executionContextChangedExternally(event) { | 127 _executionContextChangedExternally(event) { |
| 128 var executionContext = /** @type {?WebInspector.ExecutionContext} */ (event.
data); | 128 var executionContext = /** @type {?SDK.ExecutionContext} */ (event.data); |
| 129 if (!executionContext) | 129 if (!executionContext) |
| 130 return; | 130 return; |
| 131 | 131 |
| 132 var options = this._selectElement.options; | 132 var options = this._selectElement.options; |
| 133 for (var i = 0; i < options.length; ++i) { | 133 for (var i = 0; i < options.length; ++i) { |
| 134 if (options[i].__executionContext === executionContext) | 134 if (options[i].__executionContext === executionContext) |
| 135 this._select(options[i]); | 135 this._select(options[i]); |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | 138 |
| 139 _executionContextChanged() { | 139 _executionContextChanged() { |
| 140 var option = this._selectedOption(); | 140 var option = this._selectedOption(); |
| 141 var newContext = option ? option.__executionContext : null; | 141 var newContext = option ? option.__executionContext : null; |
| 142 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext); | 142 UI.context.setFlavor(SDK.ExecutionContext, newContext); |
| 143 this._updateSelectionWarning(); | 143 this._updateSelectionWarning(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 _updateSelectionWarning() { | 146 _updateSelectionWarning() { |
| 147 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionCon
text); | 147 var executionContext = UI.context.flavor(SDK.ExecutionContext); |
| 148 this._selectElement.parentElement.classList.toggle( | 148 this._selectElement.parentElement.classList.toggle( |
| 149 'warning', !this._isTopContext(executionContext) && this._hasTopContext(
)); | 149 'warning', !this._isTopContext(executionContext) && this._hasTopContext(
)); |
| 150 } | 150 } |
| 151 | 151 |
| 152 /** | 152 /** |
| 153 * @param {?WebInspector.ExecutionContext} executionContext | 153 * @param {?SDK.ExecutionContext} executionContext |
| 154 * @return {boolean} | 154 * @return {boolean} |
| 155 */ | 155 */ |
| 156 _isTopContext(executionContext) { | 156 _isTopContext(executionContext) { |
| 157 if (!executionContext || !executionContext.isDefault) | 157 if (!executionContext || !executionContext.isDefault) |
| 158 return false; | 158 return false; |
| 159 var resourceTreeModel = WebInspector.ResourceTreeModel.fromTarget(executionC
ontext.target()); | 159 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContext.ta
rget()); |
| 160 var frame = executionContext.frameId && resourceTreeModel && resourceTreeMod
el.frameForId(executionContext.frameId); | 160 var frame = executionContext.frameId && resourceTreeModel && resourceTreeMod
el.frameForId(executionContext.frameId); |
| 161 if (!frame) | 161 if (!frame) |
| 162 return false; | 162 return false; |
| 163 return frame.isMainFrame(); | 163 return frame.isMainFrame(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 /** | 166 /** |
| 167 * @return {boolean} | 167 * @return {boolean} |
| 168 */ | 168 */ |
| 169 _hasTopContext() { | 169 _hasTopContext() { |
| 170 var options = this._selectElement.options; | 170 var options = this._selectElement.options; |
| 171 for (var i = 0; i < options.length; i++) { | 171 for (var i = 0; i < options.length; i++) { |
| 172 if (this._isTopContext(options[i].__executionContext)) | 172 if (this._isTopContext(options[i].__executionContext)) |
| 173 return true; | 173 return true; |
| 174 } | 174 } |
| 175 return false; | 175 return false; |
| 176 } | 176 } |
| 177 | 177 |
| 178 /** | 178 /** |
| 179 * @override | 179 * @override |
| 180 * @param {!WebInspector.Target} target | 180 * @param {!SDK.Target} target |
| 181 */ | 181 */ |
| 182 targetAdded(target) { | 182 targetAdded(target) { |
| 183 target.runtimeModel.executionContexts().forEach(this._executionContextCreate
d, this); | 183 target.runtimeModel.executionContexts().forEach(this._executionContextCreate
d, this); |
| 184 } | 184 } |
| 185 | 185 |
| 186 /** | 186 /** |
| 187 * @override | 187 * @override |
| 188 * @param {!WebInspector.Target} target | 188 * @param {!SDK.Target} target |
| 189 */ | 189 */ |
| 190 targetRemoved(target) { | 190 targetRemoved(target) { |
| 191 var executionContexts = this._optionByExecutionContext.keysArray(); | 191 var executionContexts = this._optionByExecutionContext.keysArray(); |
| 192 for (var i = 0; i < executionContexts.length; ++i) { | 192 for (var i = 0; i < executionContexts.length; ++i) { |
| 193 if (executionContexts[i].target() === target) | 193 if (executionContexts[i].target() === target) |
| 194 this._executionContextDestroyed(executionContexts[i]); | 194 this._executionContextDestroyed(executionContexts[i]); |
| 195 } | 195 } |
| 196 } | 196 } |
| 197 | 197 |
| 198 /** | 198 /** |
| 199 * @param {!Element} option | 199 * @param {!Element} option |
| 200 */ | 200 */ |
| 201 _select(option) { | 201 _select(option) { |
| 202 this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {
?} */ (this._selectElement), option); | 202 this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {
?} */ (this._selectElement), option); |
| 203 this._updateSelectionWarning(); | 203 this._updateSelectionWarning(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 /** | 206 /** |
| 207 * @return {?Element} | 207 * @return {?Element} |
| 208 */ | 208 */ |
| 209 _selectedOption() { | 209 _selectedOption() { |
| 210 if (this._selectElement.selectedIndex >= 0) | 210 if (this._selectElement.selectedIndex >= 0) |
| 211 return this._selectElement[this._selectElement.selectedIndex]; | 211 return this._selectElement[this._selectElement.selectedIndex]; |
| 212 return null; | 212 return null; |
| 213 } | 213 } |
| 214 }; | 214 }; |
| OLD | NEW |