Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / components / ExecutionContextSelector.js
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.
4
5 /**
6  * @constructor
7  * @implements {WebInspector.TargetManager.Observer}
8  */
9 WebInspector.ExecutionContextSelector = function() {
10     WebInspector.targetManager.observeTargets(this);
11 }
12
13 WebInspector.ExecutionContextSelector.prototype = {
14
15     /**
16      * @param {!WebInspector.Target} target
17      */
18     targetAdded: function(target)
19     {
20         target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
21         target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
22     },
23
24     /**
25      * @param {!WebInspector.Target} target
26      */
27     targetRemoved: function(target)
28     {
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();
33     },
34
35     /**
36      * @param {!WebInspector.Event} event
37      */
38     _onExecutionContextCreated: function(event)
39     {
40         var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
41         if (!WebInspector.context.flavor(WebInspector.ExecutionContext))
42             WebInspector.context.setFlavor(WebInspector.ExecutionContext, executionContext);
43     },
44
45     /**
46      * @param {!WebInspector.Event} event
47      */
48     _onExecutionContextDestroyed: function(event)
49     {
50         var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
51         if (WebInspector.context.flavor(WebInspector.ExecutionContext) === executionContext)
52             this._currentExecutionContextGone();
53     },
54
55     _currentExecutionContextGone: function()
56     {
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];
63                 break;
64             }
65         }
66         WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
67     }
68
69 }
70
71 /**
72  * @param {!Element} proxyElement
73  * @param {!Range} wordRange
74  * @param {boolean} force
75  * @param {function(!Array.<string>, number=)} completionsReadyCallback
76  */
77 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback)
78 {
79     var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
80     if (!executionContext) {
81         completionsReadyCallback([]);
82         return;
83     }
84
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);
90 }