Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / components / ExecutionContextModel.js
blob95a55893fff06e4d96957ca16999fc01025d86f6
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.
5 /**
6 * @constructor
7 * @implements {WebInspector.TargetManager.Observer}
8 * @param {!Element} selectElement
9 */
10 WebInspector.ExecutionContextModel = function(selectElement)
12 this._selectElement = selectElement;
13 /**
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 = {
28 /**
29 * @param {!WebInspector.ExecutionContext} executionContext
30 * @return {string}
32 _titleFor: function(executionContext)
34 var result;
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);
39 } else {
40 var parsedUrl = executionContext.origin.asParsedURL();
41 var name = parsedUrl? parsedUrl.lastPathComponentWithFragment() : executionContext.name;
42 result = executionContext.target().decorateLabel(name);
44 } else {
45 result = "\u00a0\u00a0\u00a0\u00a0" + (executionContext.name || executionContext.origin);
48 var maxLength = 50;
49 return result.trimMiddle(maxLength);
52 /**
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())
60 return;
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);
74 /**
75 * @param {!Element} option
76 * @return {!WebInspector.ExecutionContext}
78 function mapping(option)
80 return option.__executionContext;
84 /**
85 * @param {!WebInspector.Event} event
87 _onExecutionContextCreated: function(event)
89 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (event.data);
90 this._executionContextCreated(executionContext);
93 /**
94 * @param {!WebInspector.ExecutionContext} executionContext
96 _executionContextDestroyed: function(executionContext)
98 var option = this._optionByExecutionContext.remove(executionContext);
99 option.remove();
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)
132 return;
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);
149 * @override
150 * @param {!WebInspector.Target} target
152 targetAdded: function(target)
154 target.runtimeModel.executionContexts().forEach(this._executionContextCreated, this);
158 * @override
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);
179 * @return {?Element}
181 _selectedOption: function()
183 if (this._selectElement.selectedIndex >= 0)
184 return this._selectElement[this._selectElement.selectedIndex];
185 return null;