Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1377)

Side by Side Diff: Source/devtools/front_end/components/ExecutionContextSelector.js

Issue 1171753002: DevTools console context defaults to the Service Worker, should not. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 * @param {!WebInspector.TargetManager} targetManager
9 * @param {!WebInspector.Context} context
8 */ 10 */
9 WebInspector.ExecutionContextSelector = function() 11 WebInspector.ExecutionContextSelector = function(targetManager, context)
10 { 12 {
11 WebInspector.targetManager.observeTargets(this); 13 targetManager.observeTargets(this);
12 WebInspector.context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChanged, this); 14 context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executi onContextChanged, this);
13 WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targ etChanged, this); 15 context.addFlavorChangeListener(WebInspector.Target, this._targetChanged, th is);
14 16
15 WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebIn spector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCre ated, this); 17 targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.Runti meModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
16 WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebIn spector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextD estroyed, this); 18 targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.Runti meModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, thi s);
19 this._targetManager = targetManager;
20 this._context = context;
17 } 21 }
18 22
19 WebInspector.ExecutionContextSelector.prototype = { 23 WebInspector.ExecutionContextSelector.prototype = {
20 24
21 /** 25 /**
22 * @override 26 * @override
23 * @param {!WebInspector.Target} target 27 * @param {!WebInspector.Target} target
24 */ 28 */
25 targetAdded: function(target) 29 targetAdded: function(target)
26 { 30 {
27 if (!target.hasJSContext()) 31 if (!target.hasJSContext())
28 return; 32 return;
29 // Defer selecting default target since we need all clients to get their 33 // Defer selecting default target since we need all clients to get their
30 // targetAdded notifications first. 34 // targetAdded notifications first.
31 setImmediate(function() { 35 setImmediate(deferred.bind(this));
36
37 /**
38 * @this {WebInspector.ExecutionContextSelector}
39 */
40 function deferred()
41 {
32 // We always want the second context for the service worker targets. 42 // We always want the second context for the service worker targets.
33 if (!WebInspector.context.flavor(WebInspector.Target)) 43 if (!this._context.flavor(WebInspector.Target))
34 WebInspector.context.setFlavor(WebInspector.Target, target); 44 this._context.setFlavor(WebInspector.Target, target);
35 }); 45 }
36 }, 46 },
37 47
38 /** 48 /**
39 * @override 49 * @override
40 * @param {!WebInspector.Target} target 50 * @param {!WebInspector.Target} target
41 */ 51 */
42 targetRemoved: function(target) 52 targetRemoved: function(target)
43 { 53 {
44 if (!target.hasJSContext()) 54 if (!target.hasJSContext())
45 return; 55 return;
46 var currentExecutionContext = WebInspector.context.flavor(WebInspector.E xecutionContext); 56 var currentExecutionContext = this._context.flavor(WebInspector.Executio nContext);
47 if (currentExecutionContext && currentExecutionContext.target() === targ et) 57 if (currentExecutionContext && currentExecutionContext.target() === targ et)
48 this._currentExecutionContextGone(); 58 this._currentExecutionContextGone();
49 59
50 var targets = WebInspector.targetManager.targetsWithJSContext(); 60 var targets = this._targetManager.targetsWithJSContext();
51 if (WebInspector.context.flavor(WebInspector.Target) === target && targe ts.length) 61 if (this._context.flavor(WebInspector.Target) === target && targets.leng th)
52 WebInspector.context.setFlavor(WebInspector.Target, targets[0]); 62 this._context.setFlavor(WebInspector.Target, targets[0]);
53 }, 63 },
54 64
55 /** 65 /**
56 * @param {!WebInspector.Event} event 66 * @param {!WebInspector.Event} event
57 */ 67 */
58 _executionContextChanged: function(event) 68 _executionContextChanged: function(event)
59 { 69 {
60 var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.da ta); 70 var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.da ta);
61 if (newContext) 71 if (newContext) {
62 WebInspector.context.setFlavor(WebInspector.Target, newContext.targe t()); 72 this._context.setFlavor(WebInspector.Target, newContext.target());
73 if (!this._contextIsGoingAway)
74 this._lastSelectedContextId = this._contextPersistentId(newConte xt);
75 }
63 }, 76 },
64 77
65 /** 78 /**
79 * @param {!WebInspector.ExecutionContext} executionContext
80 * @return {string}
81 */
82 _contextPersistentId: function(executionContext)
83 {
84 return executionContext.isMainWorldContext ? executionContext.target().n ame() + ":" + executionContext.frameId : "";
85 },
86
87 /**
66 * @param {!WebInspector.Event} event 88 * @param {!WebInspector.Event} event
67 */ 89 */
68 _targetChanged: function(event) 90 _targetChanged: function(event)
69 { 91 {
70 var newTarget = /** @type {?WebInspector.Target} */(event.data); 92 var newTarget = /** @type {?WebInspector.Target} */(event.data);
71 var currentContext = WebInspector.context.flavor(WebInspector.ExecutionC ontext); 93 var currentContext = this._context.flavor(WebInspector.ExecutionContext) ;
72 94
73 if (!newTarget || (currentContext && currentContext.target() === newTarg et)) 95 if (!newTarget || (currentContext && currentContext.target() === newTarg et))
74 return; 96 return;
75 97
76 var executionContexts = newTarget.runtimeModel.executionContexts(); 98 var executionContexts = newTarget.runtimeModel.executionContexts();
77 if (!executionContexts.length) 99 if (!executionContexts.length)
78 return; 100 return;
79 101
80 var newContext = executionContexts[0]; 102 var newContext = executionContexts[0];
81 for (var i = 1; i < executionContexts.length; ++i) { 103 for (var i = 1; i < executionContexts.length; ++i) {
82 if (executionContexts[i].isMainWorldContext) 104 if (executionContexts[i].isMainWorldContext)
83 newContext = executionContexts[i]; 105 newContext = executionContexts[i];
84 } 106 }
85 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext ); 107 this._context.setFlavor(WebInspector.ExecutionContext, newContext);
86 }, 108 },
87 109
88 /** 110 /**
89 * @param {!WebInspector.Event} event 111 * @param {!WebInspector.Event} event
90 */ 112 */
91 _onExecutionContextCreated: function(event) 113 _onExecutionContextCreated: function(event)
92 { 114 {
93 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (ev ent.data); 115 var executionContext = /** @type {!WebInspector.ExecutionContext} */ (ev ent.data);
94 116 if (!this._context.flavor(WebInspector.ExecutionContext) || (this._lastS electedContextId && this._lastSelectedContextId === this._contextPersistentId(ex ecutionContext)))
95 if (!WebInspector.context.flavor(WebInspector.ExecutionContext)) 117 this._context.setFlavor(WebInspector.ExecutionContext, executionCont ext);
96 WebInspector.context.setFlavor(WebInspector.ExecutionContext, execut ionContext);
97 }, 118 },
98 119
99 /** 120 /**
100 * @param {!WebInspector.Event} event 121 * @param {!WebInspector.Event} event
101 */ 122 */
102 _onExecutionContextDestroyed: function(event) 123 _onExecutionContextDestroyed: function(event)
103 { 124 {
104 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (eve nt.data); 125 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (eve nt.data);
105 if (WebInspector.context.flavor(WebInspector.ExecutionContext) === execu tionContext) 126 if (this._context.flavor(WebInspector.ExecutionContext) === executionCon text)
106 this._currentExecutionContextGone(); 127 this._currentExecutionContextGone();
107 }, 128 },
108 129
109 _currentExecutionContextGone: function() 130 _currentExecutionContextGone: function()
110 { 131 {
111 var targets = WebInspector.targetManager.targetsWithJSContext(); 132 var targets = this._targetManager.targetsWithJSContext();
112 var newContext = null; 133 var newContext = null;
113 for (var i = 0; i < targets.length; ++i) { 134 for (var i = 0; i < targets.length; ++i) {
114 if (targets[i].isServiceWorker()) 135 if (targets[i].isServiceWorker())
115 continue; 136 continue;
116 var executionContexts = targets[i].runtimeModel.executionContexts(); 137 var executionContexts = targets[i].runtimeModel.executionContexts();
117 if (executionContexts.length) { 138 if (executionContexts.length) {
118 newContext = executionContexts[0]; 139 newContext = executionContexts[0];
119 break; 140 break;
120 } 141 }
121 } 142 }
122 WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext ); 143 this._contextIsGoingAway = true;
144 this._context.setFlavor(WebInspector.ExecutionContext, newContext);
145 this._contextIsGoingAway = false;
123 } 146 }
124
125 } 147 }
126 148
127 /** 149 /**
128 * @param {!Element} proxyElement 150 * @param {!Element} proxyElement
129 * @param {!Range} wordRange 151 * @param {!Range} wordRange
130 * @param {boolean} force 152 * @param {boolean} force
131 * @param {function(!Array.<string>, number=)} completionsReadyCallback 153 * @param {function(!Array.<string>, number=)} completionsReadyCallback
132 */ 154 */
133 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback) 155 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback)
134 { 156 {
135 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionCon text); 157 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionCon text);
136 if (!executionContext) { 158 if (!executionContext) {
137 completionsReadyCallback([]); 159 completionsReadyCallback([]);
138 return; 160 return;
139 } 161 }
140 162
141 // Pass less stop characters to rangeOfWord so the range will be a more comp lete expression. 163 // Pass less stop characters to rangeOfWord so the range will be a more comp lete expression.
142 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOf fset, " =:({;,!+-*/&|^<>", proxyElement, "backward"); 164 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOf fset, " =:({;,!+-*/&|^<>", proxyElement, "backward");
143 var expressionString = expressionRange.toString(); 165 var expressionString = expressionRange.toString();
144 166
145 // The "[" is also a stop character, except when it's the last character of the expression. 167 // The "[" is also a stop character, except when it's the last character of the expression.
146 var pos = expressionString.lastIndexOf("[", expressionString.length - 2); 168 var pos = expressionString.lastIndexOf("[", expressionString.length - 2);
147 if (pos !== -1) 169 if (pos !== -1)
148 expressionString = expressionString.substr(pos + 1); 170 expressionString = expressionString.substr(pos + 1);
149 171
150 var prefix = wordRange.toString(); 172 var prefix = wordRange.toString();
151 executionContext.completionsForExpression(expressionString, prefix, force, c ompletionsReadyCallback); 173 executionContext.completionsForExpression(expressionString, prefix, force, c ompletionsReadyCallback);
152 } 174 }
OLDNEW
« no previous file with comments | « LayoutTests/inspector/sources/debugger-ui/last-execution-context-expected.txt ('k') | Source/devtools/front_end/main/Main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698