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
3 // found in the LICENSE file.
7 * @implements {WebInspector.TargetManager.Observer}
8 * @param {!Element} selectElement
10 WebInspector
.ExecutionContextModel = function(selectElement
)
12 this._selectElement
= selectElement
;
14 * @type {!Map.<!WebInspector.ExecutionContext, !Element>}
16 this._optionByExecutionContext
= new Map();
18 WebInspector
.targetManager
.observeTargets(this);
19 WebInspector
.targetManager
.addModelListener(WebInspector
.RuntimeModel
, WebInspector
.RuntimeModel
.Events
.ExecutionContextCreated
, this._onExecutionContextCreated
, this);
20 WebInspector
.targetManager
.addModelListener(WebInspector
.RuntimeModel
, WebInspector
.RuntimeModel
.Events
.ExecutionContextDestroyed
, this._onExecutionContextDestroyed
, this);
21 WebInspector
.targetManager
.addModelListener(WebInspector
.ResourceTreeModel
, WebInspector
.ResourceTreeModel
.EventTypes
.FrameNavigated
, this._onFrameNavigated
, this);
23 this._selectElement
.addEventListener("change", this._executionContextChanged
.bind(this), false);
24 WebInspector
.context
.addFlavorChangeListener(WebInspector
.ExecutionContext
, this._executionContextChangedExternally
, this);
27 WebInspector
.ExecutionContextModel
.prototype = {
29 * @param {!WebInspector.ExecutionContext} executionContext
32 _titleFor: function(executionContext
)
35 if (executionContext
.isMainWorldContext
) {
36 if (executionContext
.frameId
) {
37 var frame
= executionContext
.target().resourceTreeModel
.frameForId(executionContext
.frameId
);
38 result
= frame
? frame
.displayName() : (executionContext
.origin
|| executionContext
.name
);
40 var parsedUrl
= executionContext
.origin
.asParsedURL();
41 var name
= parsedUrl
? parsedUrl
.lastPathComponentWithFragment() : executionContext
.name
;
42 result
= executionContext
.target().decorateLabel(name
);
45 result
= "\u00a0\u00a0\u00a0\u00a0" + (executionContext
.name
|| executionContext
.origin
);
49 return result
.trimMiddle(maxLength
);
53 * @param {!WebInspector.ExecutionContext} executionContext
55 _executionContextCreated: function(executionContext
)
57 // FIXME(413886): We never want to show execution context for the main thread of shadow page in service/shared worker frontend.
58 // This check could be removed once we do not send this context to frontend.
59 if (executionContext
.target().isServiceWorker())
62 var newOption
= createElement("option");
63 newOption
.__executionContext
= executionContext
;
64 newOption
.text
= this._titleFor(executionContext
);
65 this._optionByExecutionContext
.set(executionContext
, newOption
);
66 var options
= this._selectElement
.options
;
67 var contexts
= Array
.prototype.map
.call(options
, mapping
);
68 var index
= insertionIndexForObjectInListSortedByFunction(executionContext
, contexts
, WebInspector
.ExecutionContext
.comparator
);
69 this._selectElement
.insertBefore(newOption
, options
[index
]);
71 if (executionContext
=== WebInspector
.context
.flavor(WebInspector
.ExecutionContext
))
72 this._select(newOption
);
75 * @param {!Element} option
76 * @return {!WebInspector.ExecutionContext}
78 function mapping(option
)
80 return option
.__executionContext
;
85 * @param {!WebInspector.Event} event
87 _onExecutionContextCreated: function(event
)
89 var executionContext
= /** @type {!WebInspector.ExecutionContext} */ (event
.data
);
90 this._executionContextCreated(executionContext
);
94 * @param {!WebInspector.ExecutionContext} executionContext
96 _executionContextDestroyed: function(executionContext
)
98 var option
= this._optionByExecutionContext
.remove(executionContext
);
103 * @param {!WebInspector.Event} event
105 _onExecutionContextDestroyed: function(event
)
107 var executionContext
= /** @type {!WebInspector.ExecutionContext} */ (event
.data
);
108 this._executionContextDestroyed(executionContext
);
112 * @param {!WebInspector.Event} event
114 _onFrameNavigated: function(event
)
116 var frame
= /** @type {!WebInspector.ResourceTreeFrame} */ (event
.data
);
117 var executionContexts
= this._optionByExecutionContext
.keysArray();
118 for (var i
= 0; i
< executionContexts
.length
; ++i
) {
119 var context
= executionContexts
[i
];
120 if (context
.frameId
=== frame
.id
)
121 this._optionByExecutionContext
.get(context
).text
= this._titleFor(context
);
126 * @param {!WebInspector.Event} event
128 _executionContextChangedExternally: function(event
)
130 var executionContext
= /** @type {?WebInspector.ExecutionContext} */ (event
.data
);
131 if (!executionContext
)
134 var options
= this._selectElement
.options
;
135 for (var i
= 0; i
< options
.length
; ++i
) {
136 if (options
[i
].__executionContext
=== executionContext
)
137 this._select(options
[i
]);
141 _executionContextChanged: function()
143 var option
= this._selectedOption();
144 var newContext
= option
? option
.__executionContext
: null;
145 WebInspector
.context
.setFlavor(WebInspector
.ExecutionContext
, newContext
);
150 * @param {!WebInspector.Target} target
152 targetAdded: function(target
)
154 target
.runtimeModel
.executionContexts().forEach(this._executionContextCreated
, this);
159 * @param {!WebInspector.Target} target
161 targetRemoved: function(target
)
163 var executionContexts
= this._optionByExecutionContext
.keysArray();
164 for (var i
= 0; i
< executionContexts
.length
; ++i
) {
165 if (executionContexts
[i
].target() === target
)
166 this._executionContextDestroyed(executionContexts
[i
]);
171 * @param {!Element} option
173 _select: function(option
)
175 this._selectElement
.selectedIndex
= Array
.prototype.indexOf
.call(/** @type {?} */ (this._selectElement
), option
);
181 _selectedOption: function()
183 if (this._selectElement
.selectedIndex
>= 0)
184 return this._selectElement
[this._selectElement
.selectedIndex
];