Index: third_party/WebKit/Source/devtools/front_end/components/ExecutionContextSelector.js |
diff --git a/third_party/WebKit/Source/devtools/front_end/components/ExecutionContextSelector.js b/third_party/WebKit/Source/devtools/front_end/components/ExecutionContextSelector.js |
index c7d3d914acf015d89daa96acd6a85af098d61a75..12759bfff2620aa4b97328a9c45558a877a02a44 100644 |
--- a/third_party/WebKit/Source/devtools/front_end/components/ExecutionContextSelector.js |
+++ b/third_party/WebKit/Source/devtools/front_end/components/ExecutionContextSelector.js |
@@ -1,267 +1,262 @@ |
// Copyright 2014 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
- |
/** |
- * @constructor |
* @implements {WebInspector.TargetManager.Observer} |
- * @param {!WebInspector.TargetManager} targetManager |
- * @param {!WebInspector.Context} context |
+ * @unrestricted |
*/ |
-WebInspector.ExecutionContextSelector = function(targetManager, context) |
-{ |
+WebInspector.ExecutionContextSelector = class { |
+ /** |
+ * @param {!WebInspector.TargetManager} targetManager |
+ * @param {!WebInspector.Context} context |
+ */ |
+ constructor(targetManager, context) { |
targetManager.observeTargets(this, WebInspector.Target.Capability.JS); |
context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChanged, this); |
context.addFlavorChangeListener(WebInspector.Target, this._targetChanged, this); |
- targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this); |
- targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this); |
- targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextOrderChanged, this._onExecutionContextOrderChanged, this); |
+ targetManager.addModelListener( |
+ WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextCreated, |
+ this._onExecutionContextCreated, this); |
+ targetManager.addModelListener( |
+ WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, |
+ this._onExecutionContextDestroyed, this); |
+ targetManager.addModelListener( |
+ WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextOrderChanged, |
+ this._onExecutionContextOrderChanged, this); |
this._targetManager = targetManager; |
this._context = context; |
-}; |
+ } |
-WebInspector.ExecutionContextSelector.prototype = { |
+ /** |
+ * @param {!Element} proxyElement |
+ * @param {!Range} wordRange |
+ * @param {boolean} force |
+ * @param {function(!Array.<string>, number=)} completionsReadyCallback |
+ */ |
+ static completionsForTextPromptInCurrentContext(proxyElement, wordRange, force, completionsReadyCallback) { |
+ var expressionRange = wordRange.cloneRange(); |
+ expressionRange.collapse(true); |
+ expressionRange.setStartBefore(proxyElement); |
+ WebInspector.ExecutionContextSelector |
+ .completionsForTextInCurrentContext(expressionRange.toString(), wordRange.toString(), force) |
+ .then(completionsReadyCallback); |
+ } |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- */ |
- targetAdded: function(target) |
- { |
- // Defer selecting default target since we need all clients to get their |
- // targetAdded notifications first. |
- setImmediate(deferred.bind(this)); |
+ /** |
+ * @param {string} text |
+ * @param {string} completionsPrefix |
+ * @param {boolean=} force |
+ * @return {!Promise<!Array<string>>} |
+ */ |
+ static completionsForTextInCurrentContext(text, completionsPrefix, force) { |
+ var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext); |
+ if (!executionContext) |
+ return Promise.resolve([]); |
+ var index; |
+ var stopChars = new Set(' =:({;,!+-*/&|^<>`'.split('')); |
+ for (index = text.length - 1; index >= 0; index--) { |
+ // Pass less stop characters to rangeOfWord so the range will be a more complete expression. |
+ if (stopChars.has(text.charAt(index))) |
+ break; |
+ } |
+ var clippedExpression = text.substring(index + 1); |
+ var bracketCount = 0; |
- /** |
- * @this {WebInspector.ExecutionContextSelector} |
- */ |
- function deferred() |
- { |
- // We always want the second context for the service worker targets. |
- if (!this._context.flavor(WebInspector.Target)) |
- this._context.setFlavor(WebInspector.Target, target); |
- } |
- }, |
+ index = clippedExpression.length - 1; |
+ while (index >= 0) { |
+ var character = clippedExpression.charAt(index); |
+ if (character === ']') |
+ bracketCount++; |
+ // Allow an open bracket at the end for property completion. |
+ if (character === '[' && index < clippedExpression.length - 1) { |
+ bracketCount--; |
+ if (bracketCount < 0) |
+ break; |
+ } |
+ index--; |
+ } |
+ clippedExpression = clippedExpression.substring(index + 1); |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- */ |
- targetRemoved: function(target) |
- { |
- var currentExecutionContext = this._context.flavor(WebInspector.ExecutionContext); |
- if (currentExecutionContext && currentExecutionContext.target() === target) |
- this._currentExecutionContextGone(); |
+ return executionContext.completionsForExpression(clippedExpression, completionsPrefix, force); |
+ } |
- var targets = this._targetManager.targets(WebInspector.Target.Capability.JS); |
- if (this._context.flavor(WebInspector.Target) === target && targets.length) |
- this._context.setFlavor(WebInspector.Target, targets[0]); |
- }, |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ */ |
+ targetAdded(target) { |
+ // Defer selecting default target since we need all clients to get their |
+ // targetAdded notifications first. |
+ setImmediate(deferred.bind(this)); |
/** |
- * @param {!WebInspector.Event} event |
+ * @this {WebInspector.ExecutionContextSelector} |
*/ |
- _executionContextChanged: function(event) |
- { |
- var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.data); |
- if (newContext) { |
- this._context.setFlavor(WebInspector.Target, newContext.target()); |
- if (!this._ignoreContextChanged) |
- this._lastSelectedContextId = this._contextPersistentId(newContext); |
- } |
- }, |
+ function deferred() { |
+ // We always want the second context for the service worker targets. |
+ if (!this._context.flavor(WebInspector.Target)) |
+ this._context.setFlavor(WebInspector.Target, target); |
+ } |
+ } |
- /** |
- * @param {!WebInspector.ExecutionContext} executionContext |
- * @return {string} |
- */ |
- _contextPersistentId: function(executionContext) |
- { |
- return executionContext.isDefault ? executionContext.target().name() + ":" + executionContext.frameId : ""; |
- }, |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ */ |
+ targetRemoved(target) { |
+ var currentExecutionContext = this._context.flavor(WebInspector.ExecutionContext); |
+ if (currentExecutionContext && currentExecutionContext.target() === target) |
+ this._currentExecutionContextGone(); |
- /** |
- * @param {!WebInspector.Event} event |
- */ |
- _targetChanged: function(event) |
- { |
- var newTarget = /** @type {?WebInspector.Target} */(event.data); |
- var currentContext = this._context.flavor(WebInspector.ExecutionContext); |
+ var targets = this._targetManager.targets(WebInspector.Target.Capability.JS); |
+ if (this._context.flavor(WebInspector.Target) === target && targets.length) |
+ this._context.setFlavor(WebInspector.Target, targets[0]); |
+ } |
- if (!newTarget || (currentContext && currentContext.target() === newTarget)) |
- return; |
+ /** |
+ * @param {!WebInspector.Event} event |
+ */ |
+ _executionContextChanged(event) { |
+ var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.data); |
+ if (newContext) { |
+ this._context.setFlavor(WebInspector.Target, newContext.target()); |
+ if (!this._ignoreContextChanged) |
+ this._lastSelectedContextId = this._contextPersistentId(newContext); |
+ } |
+ } |
- var executionContexts = newTarget.runtimeModel.executionContexts(); |
- if (!executionContexts.length) |
- return; |
+ /** |
+ * @param {!WebInspector.ExecutionContext} executionContext |
+ * @return {string} |
+ */ |
+ _contextPersistentId(executionContext) { |
+ return executionContext.isDefault ? executionContext.target().name() + ':' + executionContext.frameId : ''; |
+ } |
- var newContext = null; |
- for (var i = 0; i < executionContexts.length && !newContext; ++i) { |
- if (this._shouldSwitchToContext(executionContexts[i])) |
- newContext = executionContexts[i]; |
- } |
- for (var i = 0; i < executionContexts.length && !newContext; ++i) { |
- if (this._isDefaultContext(executionContexts[i])) |
- newContext = executionContexts[i]; |
- } |
- this._ignoreContextChanged = true; |
- this._context.setFlavor(WebInspector.ExecutionContext, newContext || executionContexts[0]); |
- this._ignoreContextChanged = false; |
- }, |
+ /** |
+ * @param {!WebInspector.Event} event |
+ */ |
+ _targetChanged(event) { |
+ var newTarget = /** @type {?WebInspector.Target} */ (event.data); |
+ var currentContext = this._context.flavor(WebInspector.ExecutionContext); |
- /** |
- * @param {!WebInspector.ExecutionContext} executionContext |
- * @return {boolean} |
- */ |
- _shouldSwitchToContext: function(executionContext) |
- { |
- if (this._lastSelectedContextId && this._lastSelectedContextId === this._contextPersistentId(executionContext)) |
- return true; |
- if (!this._lastSelectedContextId && this._isDefaultContext(executionContext)) |
- return true; |
- return false; |
- }, |
+ if (!newTarget || (currentContext && currentContext.target() === newTarget)) |
+ return; |
- /** |
- * @param {!WebInspector.ExecutionContext} executionContext |
- * @return {boolean} |
- */ |
- _isDefaultContext: function(executionContext) |
- { |
- if (!executionContext.isDefault || !executionContext.frameId) |
- return false; |
- if (executionContext.target().parentTarget()) |
- return false; |
- var resourceTreeModel = WebInspector.ResourceTreeModel.fromTarget(executionContext.target()); |
- var frame = resourceTreeModel && resourceTreeModel.frameForId(executionContext.frameId); |
- if (frame && frame.isMainFrame()) |
- return true; |
- return false; |
- }, |
+ var executionContexts = newTarget.runtimeModel.executionContexts(); |
+ if (!executionContexts.length) |
+ return; |
- /** |
- * @param {!WebInspector.Event} event |
- */ |
- _onExecutionContextCreated: function(event) |
- { |
- this._switchContextIfNecessary(/** @type {!WebInspector.ExecutionContext} */ (event.data)); |
- }, |
+ var newContext = null; |
+ for (var i = 0; i < executionContexts.length && !newContext; ++i) { |
+ if (this._shouldSwitchToContext(executionContexts[i])) |
+ newContext = executionContexts[i]; |
+ } |
+ for (var i = 0; i < executionContexts.length && !newContext; ++i) { |
+ if (this._isDefaultContext(executionContexts[i])) |
+ newContext = executionContexts[i]; |
+ } |
+ this._ignoreContextChanged = true; |
+ this._context.setFlavor(WebInspector.ExecutionContext, newContext || executionContexts[0]); |
+ this._ignoreContextChanged = false; |
+ } |
- /** |
- * @param {!WebInspector.Event} event |
- */ |
- _onExecutionContextDestroyed: function(event) |
- { |
- var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data); |
- if (this._context.flavor(WebInspector.ExecutionContext) === executionContext) |
- this._currentExecutionContextGone(); |
- }, |
+ /** |
+ * @param {!WebInspector.ExecutionContext} executionContext |
+ * @return {boolean} |
+ */ |
+ _shouldSwitchToContext(executionContext) { |
+ if (this._lastSelectedContextId && this._lastSelectedContextId === this._contextPersistentId(executionContext)) |
+ return true; |
+ if (!this._lastSelectedContextId && this._isDefaultContext(executionContext)) |
+ return true; |
+ return false; |
+ } |
- /** |
- * @param {!WebInspector.Event} event |
- */ |
- _onExecutionContextOrderChanged: function(event) |
- { |
- var runtimeModel = /** @type {!WebInspector.RuntimeModel} */ (event.data); |
- var executionContexts = runtimeModel.executionContexts(); |
- for (var i = 0; i < executionContexts.length; i++) { |
- if (this._switchContextIfNecessary(executionContexts[i])) |
- break; |
- } |
- }, |
+ /** |
+ * @param {!WebInspector.ExecutionContext} executionContext |
+ * @return {boolean} |
+ */ |
+ _isDefaultContext(executionContext) { |
+ if (!executionContext.isDefault || !executionContext.frameId) |
+ return false; |
+ if (executionContext.target().parentTarget()) |
+ return false; |
+ var resourceTreeModel = WebInspector.ResourceTreeModel.fromTarget(executionContext.target()); |
+ var frame = resourceTreeModel && resourceTreeModel.frameForId(executionContext.frameId); |
+ if (frame && frame.isMainFrame()) |
+ return true; |
+ return false; |
+ } |
- /** |
- * @param {!WebInspector.ExecutionContext} executionContext |
- * @return {boolean} |
- */ |
- _switchContextIfNecessary: function(executionContext) |
- { |
- if (!this._context.flavor(WebInspector.ExecutionContext) || this._shouldSwitchToContext(executionContext)) { |
- this._ignoreContextChanged = true; |
- this._context.setFlavor(WebInspector.ExecutionContext, executionContext); |
- this._ignoreContextChanged = false; |
- return true; |
- } |
- return false; |
- }, |
+ /** |
+ * @param {!WebInspector.Event} event |
+ */ |
+ _onExecutionContextCreated(event) { |
+ this._switchContextIfNecessary(/** @type {!WebInspector.ExecutionContext} */ (event.data)); |
+ } |
- _currentExecutionContextGone: function() |
- { |
- var targets = this._targetManager.targets(WebInspector.Target.Capability.JS); |
- var newContext = null; |
- for (var i = 0; i < targets.length && !newContext; ++i) { |
- var executionContexts = targets[i].runtimeModel.executionContexts(); |
- for (var executionContext of executionContexts) { |
- if (this._isDefaultContext(executionContext)) { |
- newContext = executionContext; |
- break; |
- } |
- } |
- } |
- if (!newContext) { |
- for (var i = 0; i < targets.length && !newContext; ++i) { |
- var executionContexts = targets[i].runtimeModel.executionContexts(); |
- if (executionContexts.length) { |
- newContext = executionContexts[0]; |
- break; |
- } |
- } |
- } |
- this._ignoreContextChanged = true; |
- this._context.setFlavor(WebInspector.ExecutionContext, newContext); |
- this._ignoreContextChanged = false; |
+ /** |
+ * @param {!WebInspector.Event} event |
+ */ |
+ _onExecutionContextDestroyed(event) { |
+ var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data); |
+ if (this._context.flavor(WebInspector.ExecutionContext) === executionContext) |
+ this._currentExecutionContextGone(); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.Event} event |
+ */ |
+ _onExecutionContextOrderChanged(event) { |
+ var runtimeModel = /** @type {!WebInspector.RuntimeModel} */ (event.data); |
+ var executionContexts = runtimeModel.executionContexts(); |
+ for (var i = 0; i < executionContexts.length; i++) { |
+ if (this._switchContextIfNecessary(executionContexts[i])) |
+ break; |
} |
-}; |
+ } |
-/** |
- * @param {!Element} proxyElement |
- * @param {!Range} wordRange |
- * @param {boolean} force |
- * @param {function(!Array.<string>, number=)} completionsReadyCallback |
- */ |
-WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback) |
-{ |
- var expressionRange = wordRange.cloneRange(); |
- expressionRange.collapse(true); |
- expressionRange.setStartBefore(proxyElement); |
- WebInspector.ExecutionContextSelector.completionsForTextInCurrentContext(expressionRange.toString(), wordRange.toString(), force).then(completionsReadyCallback); |
-}; |
-/** |
- * @param {string} text |
- * @param {string} completionsPrefix |
- * @param {boolean=} force |
- * @return {!Promise<!Array<string>>} |
- */ |
-WebInspector.ExecutionContextSelector.completionsForTextInCurrentContext = function(text, completionsPrefix, force) |
-{ |
- var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext); |
- if (!executionContext) |
- return Promise.resolve([]); |
- var index; |
- var stopChars = new Set(" =:({;,!+-*/&|^<>`".split("")); |
- for (index = text.length - 1; index >= 0; index--) { |
- // Pass less stop characters to rangeOfWord so the range will be a more complete expression. |
- if (stopChars.has(text.charAt(index))) |
- break; |
+ /** |
+ * @param {!WebInspector.ExecutionContext} executionContext |
+ * @return {boolean} |
+ */ |
+ _switchContextIfNecessary(executionContext) { |
+ if (!this._context.flavor(WebInspector.ExecutionContext) || this._shouldSwitchToContext(executionContext)) { |
+ this._ignoreContextChanged = true; |
+ this._context.setFlavor(WebInspector.ExecutionContext, executionContext); |
+ this._ignoreContextChanged = false; |
+ return true; |
} |
- var clippedExpression = text.substring(index + 1); |
- var bracketCount = 0; |
+ return false; |
+ } |
- index = clippedExpression.length - 1; |
- while (index >= 0) { |
- var character = clippedExpression.charAt(index); |
- if (character === "]") |
- bracketCount++; |
- // Allow an open bracket at the end for property completion. |
- if (character === "[" && index < clippedExpression.length - 1) { |
- bracketCount--; |
- if (bracketCount < 0) |
- break; |
+ _currentExecutionContextGone() { |
+ var targets = this._targetManager.targets(WebInspector.Target.Capability.JS); |
+ var newContext = null; |
+ for (var i = 0; i < targets.length && !newContext; ++i) { |
+ var executionContexts = targets[i].runtimeModel.executionContexts(); |
+ for (var executionContext of executionContexts) { |
+ if (this._isDefaultContext(executionContext)) { |
+ newContext = executionContext; |
+ break; |
} |
- index--; |
+ } |
} |
- clippedExpression = clippedExpression.substring(index + 1); |
- |
- return executionContext.completionsForExpression(clippedExpression, completionsPrefix, force); |
+ if (!newContext) { |
+ for (var i = 0; i < targets.length && !newContext; ++i) { |
+ var executionContexts = targets[i].runtimeModel.executionContexts(); |
+ if (executionContexts.length) { |
+ newContext = executionContexts[0]; |
+ break; |
+ } |
+ } |
+ } |
+ this._ignoreContextChanged = true; |
+ this._context.setFlavor(WebInspector.ExecutionContext, newContext); |
+ this._ignoreContextChanged = false; |
+ } |
}; |
+ |
+ |