| 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 {SDK.SDKModelObserver<!SDK.RuntimeModel>} | 5 * @implements {SDK.SDKModelObserver<!SDK.RuntimeModel>} |
| 6 * @unrestricted | 6 * @unrestricted |
| 7 */ | 7 */ |
| 8 Console.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.<!SDK.ExecutionContext, !Element>} | 15 * @type {!Map.<!SDK.ExecutionContext, !Element>} |
| 16 */ | 16 */ |
| 17 this._optionByExecutionContext = new Map(); | 17 this._optionByExecutionContext = new Map(); |
| 18 | 18 |
| 19 SDK.targetManager.addModelListener( | 19 SDK.targetManager.addModelListener( |
| 20 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, this.
_onExecutionContextCreated, this); | 20 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, this.
_onExecutionContextCreated, this); |
| 21 SDK.targetManager.addModelListener( | 21 SDK.targetManager.addModelListener( |
| 22 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, this.
_onExecutionContextChanged, this); | 22 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, |
| 23 event => this._refreshTitle(/** @type {!SDK.ExecutionContext} */ (event.
data))); |
| 23 SDK.targetManager.addModelListener( | 24 SDK.targetManager.addModelListener( |
| 24 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, thi
s._onExecutionContextDestroyed, this); | 25 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, thi
s._onExecutionContextDestroyed, this); |
| 25 | 26 |
| 27 /** @type {?Console.ConsoleFrameNameLookupInterface} */ |
| 28 this._frameNameLookupInstance = null; |
| 29 |
| 30 this._selectElement.addEventListener('focus', this._loadFrameNameLookupInsta
nceIfNeeded.bind(this)); |
| 26 this._selectElement.addEventListener('change', this._executionContextChanged
.bind(this), false); | 31 this._selectElement.addEventListener('change', this._executionContextChanged
.bind(this), false); |
| 27 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont
extChangedExternally, this); | 32 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont
extChangedExternally, this); |
| 28 SDK.targetManager.observeModels(SDK.RuntimeModel, this); | 33 SDK.targetManager.observeModels(SDK.RuntimeModel, this); |
| 29 } | 34 } |
| 30 | 35 |
| 36 _loadFrameNameLookupInstanceIfNeeded() { |
| 37 if (this._frameNameLookupInstance) |
| 38 return; |
| 39 var extension = self.runtime.extension(Console.ConsoleFrameNameLookupInterfa
ce); |
| 40 if (!extension) |
| 41 return; |
| 42 extension.instance().then(instance => { |
| 43 this._frameNameLookupInstance = instance; |
| 44 this._optionByExecutionContext.keysArray().forEach(this._refreshTitle.bind
(this)); |
| 45 }); |
| 46 } |
| 47 |
| 31 /** | 48 /** |
| 32 * @param {!SDK.ExecutionContext} executionContext | 49 * @param {!SDK.ExecutionContext} executionContext |
| 33 * @return {string} | 50 * @return {string} |
| 34 */ | 51 */ |
| 35 _titleFor(executionContext) { | 52 _titleFor(executionContext) { |
| 36 var target = executionContext.target(); | 53 var target = executionContext.target(); |
| 37 var depth = 0; | 54 var depth = 0; |
| 38 var label = executionContext.label() ? target.decorateLabel(executionContext
.label()) : ''; | 55 var label = executionContext.label() ? target.decorateLabel(executionContext
.label()) : ''; |
| 39 if (!executionContext.isDefault) | 56 if (!executionContext.isDefault) |
| 40 depth++; | 57 depth++; |
| 41 if (executionContext.frameId) { | 58 if (executionContext.frameId) { |
| 42 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); | 59 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); |
| 43 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon
text.frameId); | 60 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon
text.frameId); |
| 44 if (frame) { | 61 if (frame) { |
| 45 label = label || frame.displayName(); | 62 label = label || frame.displayName(); |
| 63 if (this._frameNameLookupInstance) { |
| 64 var productName = this._frameNameLookupInstance.nameForFrame(frame); |
| 65 if (productName) |
| 66 label = Common.UIString('%s / %s', productName.trimMiddle(15), label
); |
| 67 } |
| 46 while (frame.parentFrame) { | 68 while (frame.parentFrame) { |
| 47 depth++; | 69 depth++; |
| 48 frame = frame.parentFrame; | 70 frame = frame.parentFrame; |
| 49 } | 71 } |
| 50 } | 72 } |
| 51 } | 73 } |
| 52 label = label || executionContext.origin; | 74 label = label || executionContext.origin; |
| 53 var targetDepth = 0; | 75 var targetDepth = 0; |
| 54 while (target.parentTarget()) { | 76 while (target.parentTarget()) { |
| 55 if (target.parentTarget().hasJSCapability()) { | 77 if (target.parentTarget().hasJSCapability()) { |
| 56 targetDepth++; | 78 targetDepth++; |
| 57 } else { | 79 } else { |
| 58 // Special casing service workers to be top-level. | 80 // Special casing service workers to be top-level. |
| 59 targetDepth = 0; | 81 targetDepth = 0; |
| 60 break; | 82 break; |
| 61 } | 83 } |
| 62 target = target.parentTarget(); | 84 target = target.parentTarget(); |
| 63 } | 85 } |
| 64 | 86 |
| 65 depth += targetDepth; | 87 depth += targetDepth; |
| 66 var prefix = new Array(4 * depth + 1).join('\u00a0'); | 88 var prefix = new Array(4 * depth + 1).join('\u00a0'); |
| 67 var maxLength = 50; | 89 var maxLength = 50; |
| 68 return (prefix + label).trimMiddle(maxLength); | 90 return (prefix + label).trimMiddle(maxLength); |
| 69 } | 91 } |
| 70 | 92 |
| 71 /** | 93 /** |
| 72 * @param {!SDK.ExecutionContext} executionContext | 94 * @param {!SDK.ExecutionContext} executionContext |
| 73 */ | 95 */ |
| 96 _refreshTitle(executionContext) { |
| 97 var option = this._optionByExecutionContext.get(executionContext); |
| 98 if (option) |
| 99 option.text = this._titleFor(executionContext); |
| 100 this._updateSelectionWarning(); |
| 101 } |
| 102 |
| 103 /** |
| 104 * @param {!SDK.ExecutionContext} executionContext |
| 105 */ |
| 74 _executionContextCreated(executionContext) { | 106 _executionContextCreated(executionContext) { |
| 75 // FIXME(413886): We never want to show execution context for the main threa
d of shadow page in service/shared worker frontend. | 107 // FIXME(413886): We never want to show execution context for the main threa
d of shadow page in service/shared worker frontend. |
| 76 // This check could be removed once we do not send this context to frontend. | 108 // This check could be removed once we do not send this context to frontend. |
| 77 if (!executionContext.target().hasJSCapability()) | 109 if (!executionContext.target().hasJSCapability()) |
| 78 return; | 110 return; |
| 79 | 111 |
| 80 var newOption = createElement('option'); | 112 var newOption = createElement('option'); |
| 81 newOption.__executionContext = executionContext; | 113 newOption.__executionContext = executionContext; |
| 82 newOption.text = this._titleFor(executionContext); | |
| 83 this._optionByExecutionContext.set(executionContext, newOption); | 114 this._optionByExecutionContext.set(executionContext, newOption); |
| 115 this._refreshTitle(executionContext); |
| 84 var options = this._selectElement.options; | 116 var options = this._selectElement.options; |
| 85 var contexts = Array.prototype.map.call(options, mapping); | 117 var contexts = Array.prototype.map.call(options, mapping); |
| 86 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo
del.executionContextComparator()); | 118 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo
del.executionContextComparator()); |
| 87 this._selectElement.insertBefore(newOption, options[index]); | 119 this._selectElement.insertBefore(newOption, options[index]); |
| 88 | 120 |
| 89 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) | 121 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) |
| 90 this._select(newOption); | 122 this._select(newOption); |
| 91 | 123 |
| 92 /** | 124 /** |
| 93 * @param {!Element} option | 125 * @param {!Element} option |
| 94 * @return {!SDK.ExecutionContext} | 126 * @return {!SDK.ExecutionContext} |
| 95 */ | 127 */ |
| 96 function mapping(option) { | 128 function mapping(option) { |
| 97 return option.__executionContext; | 129 return option.__executionContext; |
| 98 } | 130 } |
| 99 } | 131 } |
| 100 | 132 |
| 101 /** | 133 /** |
| 102 * @param {!Common.Event} event | 134 * @param {!Common.Event} event |
| 103 */ | 135 */ |
| 104 _onExecutionContextCreated(event) { | 136 _onExecutionContextCreated(event) { |
| 105 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); | 137 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
| 106 this._executionContextCreated(executionContext); | 138 this._executionContextCreated(executionContext); |
| 107 this._updateSelectionWarning(); | 139 this._updateSelectionWarning(); |
| 108 } | 140 } |
| 109 | 141 |
| 110 /** | 142 /** |
| 111 * @param {!Common.Event} event | |
| 112 */ | |
| 113 _onExecutionContextChanged(event) { | |
| 114 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); | |
| 115 var option = this._optionByExecutionContext.get(executionContext); | |
| 116 if (option) | |
| 117 option.text = this._titleFor(executionContext); | |
| 118 this._updateSelectionWarning(); | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * @param {!SDK.ExecutionContext} executionContext | 143 * @param {!SDK.ExecutionContext} executionContext |
| 123 */ | 144 */ |
| 124 _executionContextDestroyed(executionContext) { | 145 _executionContextDestroyed(executionContext) { |
| 125 var option = this._optionByExecutionContext.remove(executionContext); | 146 var option = this._optionByExecutionContext.remove(executionContext); |
| 126 option.remove(); | 147 option.remove(); |
| 127 } | 148 } |
| 128 | 149 |
| 129 /** | 150 /** |
| 130 * @param {!Common.Event} event | 151 * @param {!Common.Event} event |
| 131 */ | 152 */ |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 240 |
| 220 /** | 241 /** |
| 221 * @return {?Element} | 242 * @return {?Element} |
| 222 */ | 243 */ |
| 223 _selectedOption() { | 244 _selectedOption() { |
| 224 if (this._selectElement.selectedIndex >= 0) | 245 if (this._selectElement.selectedIndex >= 0) |
| 225 return this._selectElement[this._selectElement.selectedIndex]; | 246 return this._selectElement[this._selectElement.selectedIndex]; |
| 226 return null; | 247 return null; |
| 227 } | 248 } |
| 228 }; | 249 }; |
| 250 |
| 251 /** |
| 252 * @interface |
| 253 */ |
| 254 Console.ConsoleFrameNameLookupInterface = function() {}; |
| 255 |
| 256 Console.ConsoleFrameNameLookupInterface.prototype = { |
| 257 /** |
| 258 * @param {!SDK.ResourceTreeFrame} frame |
| 259 * @return {?string} |
| 260 */ |
| 261 nameForFrame(frame) {} |
| 262 }; |
| OLD | NEW |