Upstream version 11.40.277.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 {
11     WebInspector.targetManager.observeTargets(this);
12     WebInspector.context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChanged, this);
13     WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targetChanged, this);
14
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);
17 }
18
19 WebInspector.ExecutionContextSelector.prototype = {
20
21     /**
22      * @param {!WebInspector.Target} target
23      */
24     targetAdded: function(target)
25     {
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);
31         });
32     },
33
34     /**
35      * @param {!WebInspector.Target} target
36      */
37     targetRemoved: function(target)
38     {
39         var currentExecutionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
40         if (currentExecutionContext && currentExecutionContext.target() === target)
41             this._currentExecutionContextGone();
42
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]);
46     },
47
48     /**
49      * @param {!WebInspector.Event} event
50      */
51     _executionContextChanged: function(event)
52     {
53         var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.data);
54         if (newContext)
55             WebInspector.context.setFlavor(WebInspector.Target, newContext.target());
56     },
57
58     /**
59      * @param {!WebInspector.Event} event
60      */
61     _targetChanged: function(event)
62     {
63         var newTarget = /** @type {?WebInspector.Target} */(event.data);
64         var currentContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
65
66         if (!newTarget || (currentContext && currentContext.target() === newTarget))
67             return;
68
69         var executionContexts = newTarget.runtimeModel.executionContexts();
70         if (!executionContexts.length)
71             return;
72
73         var newContext = executionContexts[0];
74         for (var i = 1; i < executionContexts.length; ++i) {
75             if (executionContexts[i].isMainWorldContext)
76                 newContext = executionContexts[i];
77         }
78         WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
79     },
80
81     /**
82      * @param {!WebInspector.Event} event
83      */
84     _onExecutionContextCreated: function(event)
85     {
86         var executionContext = /** @type {!WebInspector.ExecutionContext} */ (event.data);
87
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);
93         }
94     },
95
96     /**
97      * @param {!WebInspector.Event} event
98      */
99     _onExecutionContextDestroyed: function(event)
100     {
101         var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
102         if (WebInspector.context.flavor(WebInspector.ExecutionContext) === executionContext)
103             this._currentExecutionContextGone();
104     },
105
106     _currentExecutionContextGone: function()
107     {
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())
112                 continue;
113             var executionContexts = targets[i].runtimeModel.executionContexts();
114             if (executionContexts.length) {
115                 newContext = executionContexts[0];
116                 break;
117             }
118         }
119         WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
120     }
121
122 }
123
124 /**
125  * @param {!Element} proxyElement
126  * @param {!Range} wordRange
127  * @param {boolean} force
128  * @param {function(!Array.<string>, number=)} completionsReadyCallback
129  */
130 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback)
131 {
132     var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
133     if (!executionContext) {
134         completionsReadyCallback([]);
135         return;
136     }
137
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);
143 }