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() {
10 WebInspector.targetManager.observeTargets(this);
13 WebInspector.ExecutionContextSelector.prototype = {
16 * @param {!WebInspector.Target} target
18 targetAdded: function(target)
20 target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
21 target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
25 * @param {!WebInspector.Target} target
27 targetRemoved: function(target)
29 target.runtimeModel.removeEventListener(WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
30 target.runtimeModel.removeEventListener(WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
31 if (WebInspector.context.flavor(WebInspector.ExecutionContext).target() === target)
32 this._currentExecutionContextGone();
36 * @param {!WebInspector.Event} event
38 _onExecutionContextCreated: function(event)
40 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
41 if (!WebInspector.context.flavor(WebInspector.ExecutionContext))
42 WebInspector.context.setFlavor(WebInspector.ExecutionContext, executionContext);
46 * @param {!WebInspector.Event} event
48 _onExecutionContextDestroyed: function(event)
50 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
51 if (WebInspector.context.flavor(WebInspector.ExecutionContext) === executionContext)
52 this._currentExecutionContextGone();
55 _currentExecutionContextGone: function()
57 var targets = WebInspector.targetManager.targets();
58 var newContext = null;
59 for (var i = 0; i < targets.length; ++i) {
60 var executionContexts = targets[i].runtimeModel.executionContexts();
61 if (executionContexts.length) {
62 newContext = executionContexts[0];
66 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
72 * @param {!Element} proxyElement
73 * @param {!Range} wordRange
74 * @param {boolean} force
75 * @param {function(!Array.<string>, number=)} completionsReadyCallback
77 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback)
79 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
80 if (!executionContext) {
81 completionsReadyCallback([]);
85 // Pass less stop characters to rangeOfWord so the range will be a more complete expression.
86 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOffset, " =:[({;,!+-*/&|^<>", proxyElement, "backward");
87 var expressionString = expressionRange.toString();
88 var prefix = wordRange.toString();
89 executionContext.completionsForExpression(expressionString, prefix, force, completionsReadyCallback);