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