OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @constructor | 6 * @constructor |
7 * @implements {WebInspector.TargetManager.Observer} | 7 * @implements {WebInspector.TargetManager.Observer} |
8 */ | 8 */ |
9 WebInspector.ExecutionContextSelector = function() { | 9 WebInspector.ExecutionContextSelector = function() { |
10 WebInspector.targetManager.observeTargets(this); | 10 WebInspector.targetManager.observeTargets(this); |
11 } | 11 } |
12 | 12 |
13 WebInspector.ExecutionContextSelector.prototype = { | 13 WebInspector.ExecutionContextSelector.prototype = { |
14 | 14 |
15 /** | 15 /** |
16 * @param {!WebInspector.Target} target | 16 * @param {!WebInspector.Target} target |
17 */ | 17 */ |
18 targetAdded: function(target) | 18 targetAdded: function(target) |
19 { | 19 { |
| 20 if (!WebInspector.context.flavor(WebInspector.Target)) |
| 21 WebInspector.context.setFlavor(WebInspector.Target, target); |
| 22 |
20 target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.Ex
ecutionContextCreated, this._onExecutionContextCreated, this); | 23 target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.Ex
ecutionContextCreated, this._onExecutionContextCreated, this); |
21 target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.Ex
ecutionContextDestroyed, this._onExecutionContextDestroyed, this); | 24 target.runtimeModel.addEventListener(WebInspector.RuntimeModel.Events.Ex
ecutionContextDestroyed, this._onExecutionContextDestroyed, this); |
22 }, | 25 }, |
23 | 26 |
24 /** | 27 /** |
25 * @param {!WebInspector.Target} target | 28 * @param {!WebInspector.Target} target |
26 */ | 29 */ |
27 targetRemoved: function(target) | 30 targetRemoved: function(target) |
28 { | 31 { |
29 target.runtimeModel.removeEventListener(WebInspector.RuntimeModel.Events
.ExecutionContextCreated, this._onExecutionContextCreated, this); | 32 target.runtimeModel.removeEventListener(WebInspector.RuntimeModel.Events
.ExecutionContextCreated, this._onExecutionContextCreated, this); |
30 target.runtimeModel.removeEventListener(WebInspector.RuntimeModel.Events
.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this); | 33 target.runtimeModel.removeEventListener(WebInspector.RuntimeModel.Events
.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this); |
31 if (WebInspector.context.flavor(WebInspector.ExecutionContext).target()
=== target) | 34 var currentExecutionContext = this.currentExecutionContext(); |
| 35 if (currentExecutionContext && currentExecutionContext.target() === targ
et || this.currentTarget() === target) |
32 this._currentExecutionContextGone(); | 36 this._currentExecutionContextGone(); |
33 }, | 37 }, |
34 | 38 |
35 /** | 39 /** |
36 * @param {!WebInspector.Event} event | 40 * @param {!WebInspector.Event} event |
37 */ | 41 */ |
38 _onExecutionContextCreated: function(event) | 42 _onExecutionContextCreated: function(event) |
39 { | 43 { |
40 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (eve
nt.data); | 44 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (eve
nt.data); |
41 if (!WebInspector.context.flavor(WebInspector.ExecutionContext)) | 45 if (!WebInspector.context.flavor(WebInspector.ExecutionContext)) |
42 WebInspector.context.setFlavor(WebInspector.ExecutionContext, execut
ionContext); | 46 this.setCurrentExecutionContext(executionContext); |
43 }, | 47 }, |
44 | 48 |
45 /** | 49 /** |
46 * @param {!WebInspector.Event} event | 50 * @param {!WebInspector.Event} event |
47 */ | 51 */ |
48 _onExecutionContextDestroyed: function(event) | 52 _onExecutionContextDestroyed: function(event) |
49 { | 53 { |
50 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (eve
nt.data); | 54 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (eve
nt.data); |
51 if (WebInspector.context.flavor(WebInspector.ExecutionContext) === execu
tionContext) | 55 if (WebInspector.context.flavor(WebInspector.ExecutionContext) === execu
tionContext) |
52 this._currentExecutionContextGone(); | 56 this._currentExecutionContextGone(); |
53 }, | 57 }, |
54 | 58 |
55 _currentExecutionContextGone: function() | 59 _currentExecutionContextGone: function() |
56 { | 60 { |
57 var targets = WebInspector.targetManager.targets(); | 61 var targets = WebInspector.targetManager.targets(); |
58 var newContext = null; | 62 var newContext = null; |
59 for (var i = 0; i < targets.length; ++i) { | 63 for (var i = 0; i < targets.length; ++i) { |
60 var executionContexts = targets[i].runtimeModel.executionContexts(); | 64 var executionContexts = targets[i].runtimeModel.executionContexts(); |
61 if (executionContexts.length) { | 65 if (executionContexts.length) { |
62 newContext = executionContexts[0]; | 66 this.setCurrentExecutionContext(executionContexts[0]); |
63 break; | 67 return; |
64 } | 68 } |
65 } | 69 } |
| 70 WebInspector.context.setFlavor(WebInspector.ExecutionContext, null); |
| 71 WebInspector.context.setFlavor(WebInspector.Target, targets.length ? tar
gets[0] : null); |
| 72 }, |
| 73 |
| 74 /** |
| 75 * @param {?WebInspector.ExecutionContext} executionContext |
| 76 */ |
| 77 setCurrentExecutionContext: function(executionContext) |
| 78 { |
| 79 if (executionContext) |
| 80 WebInspector.context.setFlavor(WebInspector.Target, executionContext
.target()); |
| 81 WebInspector.context.setFlavor(WebInspector.ExecutionContext, executionC
ontext); |
| 82 }, |
| 83 |
| 84 /** |
| 85 * @param {?WebInspector.Target} target |
| 86 */ |
| 87 setCurrentTarget: function(target) |
| 88 { |
| 89 WebInspector.context.setFlavor(WebInspector.Target, target); |
| 90 var executionContexts = target ? target.runtimeModel.executionContexts()
: []; |
| 91 if (!executionContexts.length) |
| 92 return; |
| 93 |
| 94 var newContext = executionContexts[0]; |
| 95 for (var i = 1; i < executionContexts.length; ++i) { |
| 96 if (executionContexts[i].isMainWorldContext) |
| 97 newContext = executionContexts[i]; |
| 98 } |
66 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext
); | 99 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext
); |
| 100 }, |
| 101 |
| 102 /** |
| 103 * @return {?WebInspector.ExecutionContext} |
| 104 */ |
| 105 currentExecutionContext: function() |
| 106 { |
| 107 return WebInspector.context.flavor(WebInspector.ExecutionContext); |
| 108 }, |
| 109 |
| 110 /** |
| 111 * @return {?WebInspector.Target} |
| 112 */ |
| 113 currentTarget: function() |
| 114 { |
| 115 return WebInspector.context.flavor(WebInspector.Target); |
| 116 }, |
| 117 |
| 118 /** |
| 119 * @param {function(new:T, ...)} flavorType |
| 120 * @param {function(?T)} listener |
| 121 * @param {!Object=} thisObject |
| 122 * @template T |
| 123 */ |
| 124 _addChangeListener: function(flavorType, listener, thisObject) |
| 125 { |
| 126 WebInspector.context.addFlavorChangeListener(flavorType, callback); |
| 127 |
| 128 /** |
| 129 * @param {!WebInspector.Event} event |
| 130 */ |
| 131 function callback(event) |
| 132 { |
| 133 listener.call(thisObject, event.data); |
| 134 } |
| 135 |
| 136 }, |
| 137 |
| 138 /** |
| 139 * @param {function(?WebInspector.ExecutionContext)} listener |
| 140 * @param {!Object=} thisObject |
| 141 */ |
| 142 addExecutionContextChangeListener: function(listener, thisObject) |
| 143 { |
| 144 this._addChangeListener(WebInspector.ExecutionContext, listener, thisObje
ct); |
| 145 }, |
| 146 |
| 147 /** |
| 148 * @param {function(?WebInspector.Target)} listener |
| 149 * @param {!Object=} thisObject |
| 150 */ |
| 151 addTargetChangeListener: function(listener, thisObject) |
| 152 { |
| 153 this._addChangeListener(WebInspector.Target, listener, thisObject); |
67 } | 154 } |
68 | |
69 } | 155 } |
70 | 156 |
71 /** | 157 /** |
72 * @param {!Element} proxyElement | 158 * @param {!Element} proxyElement |
73 * @param {!Range} wordRange | 159 * @param {!Range} wordRange |
74 * @param {boolean} force | 160 * @param {boolean} force |
75 * @param {function(!Array.<string>, number=)} completionsReadyCallback | 161 * @param {function(!Array.<string>, number=)} completionsReadyCallback |
76 */ | 162 */ |
77 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext =
function(proxyElement, wordRange, force, completionsReadyCallback) | 163 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext =
function(proxyElement, wordRange, force, completionsReadyCallback) |
78 { | 164 { |
79 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionCon
text); | 165 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionCon
text); |
80 if (!executionContext) { | 166 if (!executionContext) { |
81 completionsReadyCallback([]); | 167 completionsReadyCallback([]); |
82 return; | 168 return; |
83 } | 169 } |
84 | 170 |
85 // Pass less stop characters to rangeOfWord so the range will be a more comp
lete expression. | 171 // Pass less stop characters to rangeOfWord so the range will be a more comp
lete expression. |
86 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOf
fset, " =:[({;,!+-*/&|^<>", proxyElement, "backward"); | 172 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOf
fset, " =:[({;,!+-*/&|^<>", proxyElement, "backward"); |
87 var expressionString = expressionRange.toString(); | 173 var expressionString = expressionRange.toString(); |
88 var prefix = wordRange.toString(); | 174 var prefix = wordRange.toString(); |
89 executionContext.completionsForExpression(expressionString, prefix, force, c
ompletionsReadyCallback); | 175 executionContext.completionsForExpression(expressionString, prefix, force, c
ompletionsReadyCallback); |
90 } | 176 } |
| 177 |
| 178 /** |
| 179 * @type {!WebInspector.ExecutionContextSelector} |
| 180 */ |
| 181 WebInspector.executionContextSelector; |
OLD | NEW |