1 // Copyright 2014 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}
9 WebInspector.ExecutionContextSelector = function()
11 WebInspector.targetManager.observeTargets(this);
12 WebInspector.context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChanged, this);
13 WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targetChanged, this);
15 WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
16 WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
19 WebInspector.ExecutionContextSelector.prototype = {
22 * @param {!WebInspector.Target} target
24 targetAdded: function(target)
26 // Defer selecting default target since we need all clients to get their
27 // targetAdded notifications first.
28 setImmediate(function() {
29 if (!WebInspector.context.flavor(WebInspector.Target) || WebInspector.isWorkerFrontend())
30 WebInspector.context.setFlavor(WebInspector.Target, target);
35 * @param {!WebInspector.Target} target
37 targetRemoved: function(target)
39 var currentExecutionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
40 if (currentExecutionContext && currentExecutionContext.target() === target)
41 this._currentExecutionContextGone();
43 var targets = WebInspector.targetManager.targets();
44 if (WebInspector.context.flavor(WebInspector.Target) === target && targets.length && !WebInspector.isWorkerFrontend())
45 WebInspector.context.setFlavor(WebInspector.Target, targets[0]);
49 * @param {!WebInspector.Event} event
51 _executionContextChanged: function(event)
53 var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.data);
55 WebInspector.context.setFlavor(WebInspector.Target, newContext.target());
59 * @param {!WebInspector.Event} event
61 _targetChanged: function(event)
63 var newTarget = /** @type {?WebInspector.Target} */(event.data);
64 var currentContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
66 if (!newTarget || (currentContext && currentContext.target() === newTarget))
69 var executionContexts = newTarget.runtimeModel.executionContexts();
70 if (!executionContexts.length)
73 var newContext = executionContexts[0];
74 for (var i = 1; i < executionContexts.length; ++i) {
75 if (executionContexts[i].isMainWorldContext)
76 newContext = executionContexts[i];
78 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
82 * @param {!WebInspector.Event} event
84 _onExecutionContextCreated: function(event)
86 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (event.data);
88 if (!WebInspector.context.flavor(WebInspector.ExecutionContext)) {
89 // FIXME(413886): Execution context for the main thread on the service/shared worker shadow page
90 // should never be sent to frontend. The worker frontend check below could be removed once this is fixed.
91 if (!WebInspector.isWorkerFrontend() || executionContext.target() !== WebInspector.targetManager.mainTarget())
92 WebInspector.context.setFlavor(WebInspector.ExecutionContext, executionContext);
97 * @param {!WebInspector.Event} event
99 _onExecutionContextDestroyed: function(event)
101 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
102 if (WebInspector.context.flavor(WebInspector.ExecutionContext) === executionContext)
103 this._currentExecutionContextGone();
106 _currentExecutionContextGone: function()
108 var targets = WebInspector.targetManager.targets();
109 var newContext = null;
110 for (var i = 0; i < targets.length; ++i) {
111 if (WebInspector.isWorkerFrontend() && targets[i] === WebInspector.targetManager.mainTarget())
113 var executionContexts = targets[i].runtimeModel.executionContexts();
114 if (executionContexts.length) {
115 newContext = executionContexts[0];
119 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
125 * @param {!Element} proxyElement
126 * @param {!Range} wordRange
127 * @param {boolean} force
128 * @param {function(!Array.<string>, number=)} completionsReadyCallback
130 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback)
132 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
133 if (!executionContext) {
134 completionsReadyCallback([]);
138 // Pass less stop characters to rangeOfWord so the range will be a more complete expression.
139 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOffset, " =:[({;,!+-*/&|^<>", proxyElement, "backward");
140 var expressionString = expressionRange.toString();
141 var prefix = wordRange.toString();
142 executionContext.completionsForExpression(expressionString, prefix, force, completionsReadyCallback);