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.TargetManager.Observer} | 5 * @implements {SDK.TargetManager.Observer} |
6 * @unrestricted | 6 * @unrestricted |
7 */ | 7 */ |
8 Console.ConsoleContextSelector = class { | 8 Console.ConsoleContextSelector = class { |
9 /** | 9 /** |
10 * @param {!SDK.ExecutionContext} executionContext | |
11 * @param {boolean} formatForSelector | |
12 * @return {string} | |
13 */ | |
14 static titleForContext(executionContext, formatForSelector) { | |
15 var result = executionContext.label(formatForSelector /* showStatus */) || e xecutionContext.origin; | |
16 if (executionContext.isDefault && executionContext.frameId) { | |
17 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContext. target()); | |
18 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon text.frameId); | |
19 result = frame ? frame.displayName() : result; | |
20 } | |
21 result = executionContext.target().decorateLabel(result); | |
luoe
2017/01/19 23:22:59
It turns out that browser extensions can load dedi
| |
22 | |
23 if (formatForSelector) { | |
24 if (!executionContext.isDefault) | |
25 result = '\u00a0\u00a0\u00a0\u00a0' + result; | |
26 result = result.trimMiddle(50); | |
27 } | |
luoe
2017/01/19 23:22:59
Since the presentation logic for titles in the sel
| |
28 return result; | |
29 } | |
30 | |
31 /** | |
10 * @param {!Element} selectElement | 32 * @param {!Element} selectElement |
11 */ | 33 */ |
12 constructor(selectElement) { | 34 constructor(selectElement) { |
13 this._selectElement = selectElement; | 35 this._selectElement = selectElement; |
14 /** | 36 /** |
15 * @type {!Map.<!SDK.ExecutionContext, !Element>} | 37 * @type {!Map.<!SDK.ExecutionContext, !Element>} |
16 */ | 38 */ |
17 this._optionByExecutionContext = new Map(); | 39 this._optionByExecutionContext = new Map(); |
18 | 40 |
19 SDK.targetManager.observeTargets(this); | 41 SDK.targetManager.observeTargets(this); |
20 SDK.targetManager.addModelListener( | 42 SDK.targetManager.addModelListener( |
21 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, this. _onExecutionContextCreated, this); | 43 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, this. _onExecutionContextCreated, this); |
22 SDK.targetManager.addModelListener( | 44 SDK.targetManager.addModelListener( |
23 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, this. _onExecutionContextChanged, this); | 45 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, this. _onExecutionContextChanged, this); |
24 SDK.targetManager.addModelListener( | 46 SDK.targetManager.addModelListener( |
25 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, thi s._onExecutionContextDestroyed, this); | 47 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, thi s._onExecutionContextDestroyed, this); |
26 | 48 |
27 this._selectElement.addEventListener('change', this._executionContextChanged .bind(this), false); | 49 this._selectElement.addEventListener('change', this._executionContextChanged .bind(this), false); |
28 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont extChangedExternally, this); | 50 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont extChangedExternally, this); |
29 } | 51 } |
30 | 52 |
31 /** | 53 /** |
32 * @param {!SDK.ExecutionContext} executionContext | 54 * @param {!SDK.ExecutionContext} executionContext |
33 * @return {string} | |
34 */ | |
35 _titleFor(executionContext) { | |
36 var result; | |
37 if (executionContext.isDefault) { | |
38 if (executionContext.frameId) { | |
39 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContex t.target()); | |
40 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionC ontext.frameId); | |
41 result = frame ? frame.displayName() : executionContext.label(); | |
42 } else { | |
43 result = executionContext.target().decorateLabel(executionContext.label( )); | |
44 } | |
45 } else { | |
46 result = '\u00a0\u00a0\u00a0\u00a0' + (executionContext.label() || executi onContext.origin); | |
47 } | |
48 | |
49 var maxLength = 50; | |
50 return result.trimMiddle(maxLength); | |
51 } | |
52 | |
53 /** | |
54 * @param {!SDK.ExecutionContext} executionContext | |
55 */ | 55 */ |
56 _executionContextCreated(executionContext) { | 56 _executionContextCreated(executionContext) { |
57 // FIXME(413886): We never want to show execution context for the main threa d of shadow page in service/shared worker frontend. | 57 // FIXME(413886): We never want to show execution context for the main threa d of shadow page in service/shared worker frontend. |
58 // This check could be removed once we do not send this context to frontend. | 58 // This check could be removed once we do not send this context to frontend. |
59 if (!executionContext.target().hasJSCapability()) | 59 if (!executionContext.target().hasJSCapability()) |
60 return; | 60 return; |
61 | 61 |
62 var newOption = createElement('option'); | 62 var newOption = createElement('option'); |
63 newOption.__executionContext = executionContext; | 63 newOption.__executionContext = executionContext; |
64 newOption.text = this._titleFor(executionContext); | 64 newOption.text = Console.ConsoleContextSelector.titleForContext(executionCon text, true /* formatForSelector */); |
65 this._optionByExecutionContext.set(executionContext, newOption); | 65 this._optionByExecutionContext.set(executionContext, newOption); |
66 var options = this._selectElement.options; | 66 var options = this._selectElement.options; |
67 var contexts = Array.prototype.map.call(options, mapping); | 67 var contexts = Array.prototype.map.call(options, mapping); |
68 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo del.executionContextComparator()); | 68 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo del.executionContextComparator()); |
69 this._selectElement.insertBefore(newOption, options[index]); | 69 this._selectElement.insertBefore(newOption, options[index]); |
70 | 70 |
71 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) | 71 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) |
72 this._select(newOption); | 72 this._select(newOption); |
73 | 73 |
74 /** | 74 /** |
(...skipping 14 matching lines...) Expand all Loading... | |
89 this._updateSelectionWarning(); | 89 this._updateSelectionWarning(); |
90 } | 90 } |
91 | 91 |
92 /** | 92 /** |
93 * @param {!Common.Event} event | 93 * @param {!Common.Event} event |
94 */ | 94 */ |
95 _onExecutionContextChanged(event) { | 95 _onExecutionContextChanged(event) { |
96 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); | 96 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
97 var option = this._optionByExecutionContext.get(executionContext); | 97 var option = this._optionByExecutionContext.get(executionContext); |
98 if (option) | 98 if (option) |
99 option.text = this._titleFor(executionContext); | 99 option.text = Console.ConsoleContextSelector.titleForContext(executionCont ext, true /* formatForSelector */); |
100 this._updateSelectionWarning(); | 100 this._updateSelectionWarning(); |
101 } | 101 } |
102 | 102 |
103 /** | 103 /** |
104 * @param {!SDK.ExecutionContext} executionContext | 104 * @param {!SDK.ExecutionContext} executionContext |
105 */ | 105 */ |
106 _executionContextDestroyed(executionContext) { | 106 _executionContextDestroyed(executionContext) { |
107 var option = this._optionByExecutionContext.remove(executionContext); | 107 var option = this._optionByExecutionContext.remove(executionContext); |
108 option.remove(); | 108 option.remove(); |
109 } | 109 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 | 201 |
202 /** | 202 /** |
203 * @return {?Element} | 203 * @return {?Element} |
204 */ | 204 */ |
205 _selectedOption() { | 205 _selectedOption() { |
206 if (this._selectElement.selectedIndex >= 0) | 206 if (this._selectElement.selectedIndex >= 0) |
207 return this._selectElement[this._selectElement.selectedIndex]; | 207 return this._selectElement[this._selectElement.selectedIndex]; |
208 return null; | 208 return null; |
209 } | 209 } |
210 }; | 210 }; |
OLD | NEW |