| 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 * @implements {WebInspector.TargetManager.Observer} | 5 * @implements {SDK.TargetManager.Observer} |
| 6 * @unrestricted | 6 * @unrestricted |
| 7 */ | 7 */ |
| 8 WebInspector.ExecutionContextSelector = class { | 8 Components.ExecutionContextSelector = class { |
| 9 /** | 9 /** |
| 10 * @param {!WebInspector.TargetManager} targetManager | 10 * @param {!SDK.TargetManager} targetManager |
| 11 * @param {!WebInspector.Context} context | 11 * @param {!UI.Context} context |
| 12 */ | 12 */ |
| 13 constructor(targetManager, context) { | 13 constructor(targetManager, context) { |
| 14 targetManager.observeTargets(this, WebInspector.Target.Capability.JS); | 14 targetManager.observeTargets(this, SDK.Target.Capability.JS); |
| 15 context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executi
onContextChanged, this); | 15 context.addFlavorChangeListener(SDK.ExecutionContext, this._executionContext
Changed, this); |
| 16 context.addFlavorChangeListener(WebInspector.Target, this._targetChanged, th
is); | 16 context.addFlavorChangeListener(SDK.Target, this._targetChanged, this); |
| 17 | 17 |
| 18 targetManager.addModelListener( | 18 targetManager.addModelListener( |
| 19 WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionCon
textCreated, | 19 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, |
| 20 this._onExecutionContextCreated, this); | 20 this._onExecutionContextCreated, this); |
| 21 targetManager.addModelListener( | 21 targetManager.addModelListener( |
| 22 WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionCon
textDestroyed, | 22 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, |
| 23 this._onExecutionContextDestroyed, this); | 23 this._onExecutionContextDestroyed, this); |
| 24 targetManager.addModelListener( | 24 targetManager.addModelListener( |
| 25 WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionCon
textOrderChanged, | 25 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextOrderChanged, |
| 26 this._onExecutionContextOrderChanged, this); | 26 this._onExecutionContextOrderChanged, this); |
| 27 this._targetManager = targetManager; | 27 this._targetManager = targetManager; |
| 28 this._context = context; | 28 this._context = context; |
| 29 } | 29 } |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @override | 32 * @override |
| 33 * @param {!WebInspector.Target} target | 33 * @param {!SDK.Target} target |
| 34 */ | 34 */ |
| 35 targetAdded(target) { | 35 targetAdded(target) { |
| 36 // Defer selecting default target since we need all clients to get their | 36 // Defer selecting default target since we need all clients to get their |
| 37 // targetAdded notifications first. | 37 // targetAdded notifications first. |
| 38 setImmediate(deferred.bind(this)); | 38 setImmediate(deferred.bind(this)); |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * @this {WebInspector.ExecutionContextSelector} | 41 * @this {Components.ExecutionContextSelector} |
| 42 */ | 42 */ |
| 43 function deferred() { | 43 function deferred() { |
| 44 // We always want the second context for the service worker targets. | 44 // We always want the second context for the service worker targets. |
| 45 if (!this._context.flavor(WebInspector.Target)) | 45 if (!this._context.flavor(SDK.Target)) |
| 46 this._context.setFlavor(WebInspector.Target, target); | 46 this._context.setFlavor(SDK.Target, target); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * @override | 51 * @override |
| 52 * @param {!WebInspector.Target} target | 52 * @param {!SDK.Target} target |
| 53 */ | 53 */ |
| 54 targetRemoved(target) { | 54 targetRemoved(target) { |
| 55 var currentExecutionContext = this._context.flavor(WebInspector.ExecutionCon
text); | 55 var currentExecutionContext = this._context.flavor(SDK.ExecutionContext); |
| 56 if (currentExecutionContext && currentExecutionContext.target() === target) | 56 if (currentExecutionContext && currentExecutionContext.target() === target) |
| 57 this._currentExecutionContextGone(); | 57 this._currentExecutionContextGone(); |
| 58 | 58 |
| 59 var targets = this._targetManager.targets(WebInspector.Target.Capability.JS)
; | 59 var targets = this._targetManager.targets(SDK.Target.Capability.JS); |
| 60 if (this._context.flavor(WebInspector.Target) === target && targets.length) | 60 if (this._context.flavor(SDK.Target) === target && targets.length) |
| 61 this._context.setFlavor(WebInspector.Target, targets[0]); | 61 this._context.setFlavor(SDK.Target, targets[0]); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * @param {!WebInspector.Event} event | 65 * @param {!Common.Event} event |
| 66 */ | 66 */ |
| 67 _executionContextChanged(event) { | 67 _executionContextChanged(event) { |
| 68 var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.data); | 68 var newContext = /** @type {?SDK.ExecutionContext} */ (event.data); |
| 69 if (newContext) { | 69 if (newContext) { |
| 70 this._context.setFlavor(WebInspector.Target, newContext.target()); | 70 this._context.setFlavor(SDK.Target, newContext.target()); |
| 71 if (!this._ignoreContextChanged) | 71 if (!this._ignoreContextChanged) |
| 72 this._lastSelectedContextId = this._contextPersistentId(newContext); | 72 this._lastSelectedContextId = this._contextPersistentId(newContext); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * @param {!WebInspector.ExecutionContext} executionContext | 77 * @param {!SDK.ExecutionContext} executionContext |
| 78 * @return {string} | 78 * @return {string} |
| 79 */ | 79 */ |
| 80 _contextPersistentId(executionContext) { | 80 _contextPersistentId(executionContext) { |
| 81 return executionContext.isDefault ? executionContext.target().name() + ':' +
executionContext.frameId : ''; | 81 return executionContext.isDefault ? executionContext.target().name() + ':' +
executionContext.frameId : ''; |
| 82 } | 82 } |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * @param {!WebInspector.Event} event | 85 * @param {!Common.Event} event |
| 86 */ | 86 */ |
| 87 _targetChanged(event) { | 87 _targetChanged(event) { |
| 88 var newTarget = /** @type {?WebInspector.Target} */ (event.data); | 88 var newTarget = /** @type {?SDK.Target} */ (event.data); |
| 89 var currentContext = this._context.flavor(WebInspector.ExecutionContext); | 89 var currentContext = this._context.flavor(SDK.ExecutionContext); |
| 90 | 90 |
| 91 if (!newTarget || (currentContext && currentContext.target() === newTarget)) | 91 if (!newTarget || (currentContext && currentContext.target() === newTarget)) |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 var executionContexts = newTarget.runtimeModel.executionContexts(); | 94 var executionContexts = newTarget.runtimeModel.executionContexts(); |
| 95 if (!executionContexts.length) | 95 if (!executionContexts.length) |
| 96 return; | 96 return; |
| 97 | 97 |
| 98 var newContext = null; | 98 var newContext = null; |
| 99 for (var i = 0; i < executionContexts.length && !newContext; ++i) { | 99 for (var i = 0; i < executionContexts.length && !newContext; ++i) { |
| 100 if (this._shouldSwitchToContext(executionContexts[i])) | 100 if (this._shouldSwitchToContext(executionContexts[i])) |
| 101 newContext = executionContexts[i]; | 101 newContext = executionContexts[i]; |
| 102 } | 102 } |
| 103 for (var i = 0; i < executionContexts.length && !newContext; ++i) { | 103 for (var i = 0; i < executionContexts.length && !newContext; ++i) { |
| 104 if (this._isDefaultContext(executionContexts[i])) | 104 if (this._isDefaultContext(executionContexts[i])) |
| 105 newContext = executionContexts[i]; | 105 newContext = executionContexts[i]; |
| 106 } | 106 } |
| 107 this._ignoreContextChanged = true; | 107 this._ignoreContextChanged = true; |
| 108 this._context.setFlavor(WebInspector.ExecutionContext, newContext || executi
onContexts[0]); | 108 this._context.setFlavor(SDK.ExecutionContext, newContext || executionContext
s[0]); |
| 109 this._ignoreContextChanged = false; | 109 this._ignoreContextChanged = false; |
| 110 } | 110 } |
| 111 | 111 |
| 112 /** | 112 /** |
| 113 * @param {!WebInspector.ExecutionContext} executionContext | 113 * @param {!SDK.ExecutionContext} executionContext |
| 114 * @return {boolean} | 114 * @return {boolean} |
| 115 */ | 115 */ |
| 116 _shouldSwitchToContext(executionContext) { | 116 _shouldSwitchToContext(executionContext) { |
| 117 if (this._lastSelectedContextId && this._lastSelectedContextId === this._con
textPersistentId(executionContext)) | 117 if (this._lastSelectedContextId && this._lastSelectedContextId === this._con
textPersistentId(executionContext)) |
| 118 return true; | 118 return true; |
| 119 if (!this._lastSelectedContextId && this._isDefaultContext(executionContext)
) | 119 if (!this._lastSelectedContextId && this._isDefaultContext(executionContext)
) |
| 120 return true; | 120 return true; |
| 121 return false; | 121 return false; |
| 122 } | 122 } |
| 123 | 123 |
| 124 /** | 124 /** |
| 125 * @param {!WebInspector.ExecutionContext} executionContext | 125 * @param {!SDK.ExecutionContext} executionContext |
| 126 * @return {boolean} | 126 * @return {boolean} |
| 127 */ | 127 */ |
| 128 _isDefaultContext(executionContext) { | 128 _isDefaultContext(executionContext) { |
| 129 if (!executionContext.isDefault || !executionContext.frameId) | 129 if (!executionContext.isDefault || !executionContext.frameId) |
| 130 return false; | 130 return false; |
| 131 if (executionContext.target().parentTarget()) | 131 if (executionContext.target().parentTarget()) |
| 132 return false; | 132 return false; |
| 133 var resourceTreeModel = WebInspector.ResourceTreeModel.fromTarget(executionC
ontext.target()); | 133 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContext.ta
rget()); |
| 134 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionConte
xt.frameId); | 134 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionConte
xt.frameId); |
| 135 if (frame && frame.isMainFrame()) | 135 if (frame && frame.isMainFrame()) |
| 136 return true; | 136 return true; |
| 137 return false; | 137 return false; |
| 138 } | 138 } |
| 139 | 139 |
| 140 /** | 140 /** |
| 141 * @param {!WebInspector.Event} event | 141 * @param {!Common.Event} event |
| 142 */ | 142 */ |
| 143 _onExecutionContextCreated(event) { | 143 _onExecutionContextCreated(event) { |
| 144 this._switchContextIfNecessary(/** @type {!WebInspector.ExecutionContext} */
(event.data)); | 144 this._switchContextIfNecessary(/** @type {!SDK.ExecutionContext} */ (event.d
ata)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 /** | 147 /** |
| 148 * @param {!WebInspector.Event} event | 148 * @param {!Common.Event} event |
| 149 */ | 149 */ |
| 150 _onExecutionContextDestroyed(event) { | 150 _onExecutionContextDestroyed(event) { |
| 151 var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.d
ata); | 151 var executionContext = /** @type {!SDK.ExecutionContext}*/ (event.data); |
| 152 if (this._context.flavor(WebInspector.ExecutionContext) === executionContext
) | 152 if (this._context.flavor(SDK.ExecutionContext) === executionContext) |
| 153 this._currentExecutionContextGone(); | 153 this._currentExecutionContextGone(); |
| 154 } | 154 } |
| 155 | 155 |
| 156 /** | 156 /** |
| 157 * @param {!WebInspector.Event} event | 157 * @param {!Common.Event} event |
| 158 */ | 158 */ |
| 159 _onExecutionContextOrderChanged(event) { | 159 _onExecutionContextOrderChanged(event) { |
| 160 var runtimeModel = /** @type {!WebInspector.RuntimeModel} */ (event.data); | 160 var runtimeModel = /** @type {!SDK.RuntimeModel} */ (event.data); |
| 161 var executionContexts = runtimeModel.executionContexts(); | 161 var executionContexts = runtimeModel.executionContexts(); |
| 162 for (var i = 0; i < executionContexts.length; i++) { | 162 for (var i = 0; i < executionContexts.length; i++) { |
| 163 if (this._switchContextIfNecessary(executionContexts[i])) | 163 if (this._switchContextIfNecessary(executionContexts[i])) |
| 164 break; | 164 break; |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 /** | 168 /** |
| 169 * @param {!WebInspector.ExecutionContext} executionContext | 169 * @param {!SDK.ExecutionContext} executionContext |
| 170 * @return {boolean} | 170 * @return {boolean} |
| 171 */ | 171 */ |
| 172 _switchContextIfNecessary(executionContext) { | 172 _switchContextIfNecessary(executionContext) { |
| 173 if (!this._context.flavor(WebInspector.ExecutionContext) || this._shouldSwit
chToContext(executionContext)) { | 173 if (!this._context.flavor(SDK.ExecutionContext) || this._shouldSwitchToConte
xt(executionContext)) { |
| 174 this._ignoreContextChanged = true; | 174 this._ignoreContextChanged = true; |
| 175 this._context.setFlavor(WebInspector.ExecutionContext, executionContext); | 175 this._context.setFlavor(SDK.ExecutionContext, executionContext); |
| 176 this._ignoreContextChanged = false; | 176 this._ignoreContextChanged = false; |
| 177 return true; | 177 return true; |
| 178 } | 178 } |
| 179 return false; | 179 return false; |
| 180 } | 180 } |
| 181 | 181 |
| 182 _currentExecutionContextGone() { | 182 _currentExecutionContextGone() { |
| 183 var targets = this._targetManager.targets(WebInspector.Target.Capability.JS)
; | 183 var targets = this._targetManager.targets(SDK.Target.Capability.JS); |
| 184 var newContext = null; | 184 var newContext = null; |
| 185 for (var i = 0; i < targets.length && !newContext; ++i) { | 185 for (var i = 0; i < targets.length && !newContext; ++i) { |
| 186 var executionContexts = targets[i].runtimeModel.executionContexts(); | 186 var executionContexts = targets[i].runtimeModel.executionContexts(); |
| 187 for (var executionContext of executionContexts) { | 187 for (var executionContext of executionContexts) { |
| 188 if (this._isDefaultContext(executionContext)) { | 188 if (this._isDefaultContext(executionContext)) { |
| 189 newContext = executionContext; | 189 newContext = executionContext; |
| 190 break; | 190 break; |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 if (!newContext) { | 194 if (!newContext) { |
| 195 for (var i = 0; i < targets.length && !newContext; ++i) { | 195 for (var i = 0; i < targets.length && !newContext; ++i) { |
| 196 var executionContexts = targets[i].runtimeModel.executionContexts(); | 196 var executionContexts = targets[i].runtimeModel.executionContexts(); |
| 197 if (executionContexts.length) { | 197 if (executionContexts.length) { |
| 198 newContext = executionContexts[0]; | 198 newContext = executionContexts[0]; |
| 199 break; | 199 break; |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 this._ignoreContextChanged = true; | 203 this._ignoreContextChanged = true; |
| 204 this._context.setFlavor(WebInspector.ExecutionContext, newContext); | 204 this._context.setFlavor(SDK.ExecutionContext, newContext); |
| 205 this._ignoreContextChanged = false; | 205 this._ignoreContextChanged = false; |
| 206 } | 206 } |
| 207 }; | 207 }; |
| OLD | NEW |