Chromium Code Reviews| 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 /** | |
| 28 * @type {?Console.ConsoleFrameNameLookupInterface} | |
| 29 */ | |
| 30 this._frameNameLookupInstance = null; | |
| 31 var extension = self.runtime.extension(Console.ConsoleFrameNameLookupInterfa ce); | |
| 32 if (extension) { | |
| 33 extension.instance().then(instance => { | |
|
pfeldman
2017/04/04 19:03:15
That's super early. We are creating this all upon
allada
2017/04/05 01:49:45
Done.
| |
| 34 this._frameNameLookupInstance = instance; | |
| 35 this._optionByExecutionContext.keysArray().forEach(this._refreshTitle.bi nd(this)); | |
| 36 }); | |
| 37 } | |
| 38 | |
| 26 this._selectElement.addEventListener('change', this._executionContextChanged .bind(this), false); | 39 this._selectElement.addEventListener('change', this._executionContextChanged .bind(this), false); |
| 27 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont extChangedExternally, this); | 40 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont extChangedExternally, this); |
| 28 SDK.targetManager.observeModels(SDK.RuntimeModel, this); | 41 SDK.targetManager.observeModels(SDK.RuntimeModel, this); |
| 29 } | 42 } |
| 30 | 43 |
| 31 /** | 44 /** |
| 32 * @param {!SDK.ExecutionContext} executionContext | 45 * @param {!SDK.ExecutionContext} executionContext |
| 33 * @return {string} | 46 * @return {string} |
| 34 */ | 47 */ |
| 35 _titleFor(executionContext) { | 48 _titleFor(executionContext) { |
| 36 var target = executionContext.target(); | 49 var target = executionContext.target(); |
| 37 var depth = 0; | 50 var depth = 0; |
| 38 var label = executionContext.label() ? target.decorateLabel(executionContext .label()) : ''; | 51 var label = executionContext.label() ? target.decorateLabel(executionContext .label()) : ''; |
| 39 if (!executionContext.isDefault) | 52 if (!executionContext.isDefault) |
| 40 depth++; | 53 depth++; |
| 41 if (executionContext.frameId) { | 54 if (executionContext.frameId) { |
| 42 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); | 55 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); |
| 43 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon text.frameId); | 56 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon text.frameId); |
| 44 if (frame) { | 57 if (frame) { |
| 45 label = label || frame.displayName(); | 58 label = label || frame.displayName(); |
| 59 if (this._frameNameLookupInstance) { | |
| 60 var productName = this._frameNameLookupInstance.nameForFrame(frame); | |
| 61 if (productName) | |
| 62 label = productName.trimMiddle(15) + ' / ' + label; | |
|
caseq
2017/04/04 18:59:40
Why not trimEnd()? Also,
label = Common.UIString('
allada
2017/04/05 01:49:45
We can trimEnd() I was playing around with it and
| |
| 63 } | |
| 46 while (frame.parentFrame) { | 64 while (frame.parentFrame) { |
| 47 depth++; | 65 depth++; |
| 48 frame = frame.parentFrame; | 66 frame = frame.parentFrame; |
| 49 } | 67 } |
| 50 } | 68 } |
| 51 } | 69 } |
| 52 label = label || executionContext.origin; | 70 label = label || executionContext.origin; |
| 53 var targetDepth = 0; | 71 var targetDepth = 0; |
| 54 while (target.parentTarget()) { | 72 while (target.parentTarget()) { |
| 55 if (target.parentTarget().hasJSCapability()) { | 73 if (target.parentTarget().hasJSCapability()) { |
| 56 targetDepth++; | 74 targetDepth++; |
| 57 } else { | 75 } else { |
| 58 // Special casing service workers to be top-level. | 76 // Special casing service workers to be top-level. |
| 59 targetDepth = 0; | 77 targetDepth = 0; |
| 60 break; | 78 break; |
| 61 } | 79 } |
| 62 target = target.parentTarget(); | 80 target = target.parentTarget(); |
| 63 } | 81 } |
| 64 | 82 |
| 65 depth += targetDepth; | 83 depth += targetDepth; |
| 66 var prefix = new Array(4 * depth + 1).join('\u00a0'); | 84 var prefix = new Array(4 * depth + 1).join('\u00a0'); |
| 67 var maxLength = 50; | 85 var maxLength = 50; |
| 68 return (prefix + label).trimMiddle(maxLength); | 86 return (prefix + label).trimMiddle(maxLength); |
| 69 } | 87 } |
| 70 | 88 |
| 71 /** | 89 /** |
| 72 * @param {!SDK.ExecutionContext} executionContext | 90 * @param {!SDK.ExecutionContext} executionContext |
| 73 */ | 91 */ |
| 92 _refreshTitle(executionContext) { | |
| 93 var option = this._optionByExecutionContext.get(executionContext); | |
| 94 if (option) | |
| 95 option.text = this._titleFor(executionContext); | |
| 96 this._updateSelectionWarning(); | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * @param {!SDK.ExecutionContext} executionContext | |
| 101 */ | |
| 74 _executionContextCreated(executionContext) { | 102 _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. | 103 // 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. | 104 // This check could be removed once we do not send this context to frontend. |
| 77 if (!executionContext.target().hasJSCapability()) | 105 if (!executionContext.target().hasJSCapability()) |
| 78 return; | 106 return; |
| 79 | 107 |
| 80 var newOption = createElement('option'); | 108 var newOption = createElement('option'); |
| 81 newOption.__executionContext = executionContext; | 109 newOption.__executionContext = executionContext; |
| 82 newOption.text = this._titleFor(executionContext); | |
| 83 this._optionByExecutionContext.set(executionContext, newOption); | 110 this._optionByExecutionContext.set(executionContext, newOption); |
| 111 this._refreshTitle(executionContext); | |
| 84 var options = this._selectElement.options; | 112 var options = this._selectElement.options; |
| 85 var contexts = Array.prototype.map.call(options, mapping); | 113 var contexts = Array.prototype.map.call(options, mapping); |
| 86 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo del.executionContextComparator()); | 114 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo del.executionContextComparator()); |
| 87 this._selectElement.insertBefore(newOption, options[index]); | 115 this._selectElement.insertBefore(newOption, options[index]); |
| 88 | 116 |
| 89 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) | 117 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) |
| 90 this._select(newOption); | 118 this._select(newOption); |
| 91 | 119 |
| 92 /** | 120 /** |
| 93 * @param {!Element} option | 121 * @param {!Element} option |
| 94 * @return {!SDK.ExecutionContext} | 122 * @return {!SDK.ExecutionContext} |
| 95 */ | 123 */ |
| 96 function mapping(option) { | 124 function mapping(option) { |
| 97 return option.__executionContext; | 125 return option.__executionContext; |
| 98 } | 126 } |
| 99 } | 127 } |
| 100 | 128 |
| 101 /** | 129 /** |
| 102 * @param {!Common.Event} event | 130 * @param {!Common.Event} event |
| 103 */ | 131 */ |
| 104 _onExecutionContextCreated(event) { | 132 _onExecutionContextCreated(event) { |
| 105 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); | 133 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
| 106 this._executionContextCreated(executionContext); | 134 this._executionContextCreated(executionContext); |
| 107 this._updateSelectionWarning(); | 135 this._updateSelectionWarning(); |
| 108 } | 136 } |
| 109 | 137 |
| 110 /** | 138 /** |
| 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 | 139 * @param {!SDK.ExecutionContext} executionContext |
| 123 */ | 140 */ |
| 124 _executionContextDestroyed(executionContext) { | 141 _executionContextDestroyed(executionContext) { |
| 125 var option = this._optionByExecutionContext.remove(executionContext); | 142 var option = this._optionByExecutionContext.remove(executionContext); |
| 126 option.remove(); | 143 option.remove(); |
| 127 } | 144 } |
| 128 | 145 |
| 129 /** | 146 /** |
| 130 * @param {!Common.Event} event | 147 * @param {!Common.Event} event |
| 131 */ | 148 */ |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 | 236 |
| 220 /** | 237 /** |
| 221 * @return {?Element} | 238 * @return {?Element} |
| 222 */ | 239 */ |
| 223 _selectedOption() { | 240 _selectedOption() { |
| 224 if (this._selectElement.selectedIndex >= 0) | 241 if (this._selectElement.selectedIndex >= 0) |
| 225 return this._selectElement[this._selectElement.selectedIndex]; | 242 return this._selectElement[this._selectElement.selectedIndex]; |
| 226 return null; | 243 return null; |
| 227 } | 244 } |
| 228 }; | 245 }; |
| 246 | |
| 247 /** | |
| 248 * @interface | |
| 249 */ | |
| 250 Console.ConsoleFrameNameLookupInterface = function() {}; | |
| 251 | |
| 252 Console.ConsoleFrameNameLookupInterface.prototype = { | |
| 253 /** | |
| 254 * @param {!SDK.ResourceTreeFrame} frame | |
| 255 * @return {?string} | |
| 256 */ | |
| 257 nameForFrame(frame) {} | |
| 258 }; | |
| OLD | NEW |