Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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. |
Tim van der Lippe | 3cd66dc | 2020-01-10 15:26:22 | [diff] [blame] | 4 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 5 | /* eslint-disable rulesdir/no_underscored_properties */ |
| 6 | |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 7 | import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars |
Tim van der Lippe | e00b92f | 2021-03-31 16:52:17 | [diff] [blame] | 8 | import * as SDK from '../core/sdk/sdk.js'; |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 9 | import * as UI from '../ui/ui.js'; // eslint-disable-line no-unused-vars |
| 10 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 11 | export class ExecutionContextSelector implements SDK.SDKModel.SDKModelObserver<SDK.RuntimeModel.RuntimeModel> { |
| 12 | _targetManager: SDK.SDKModel.TargetManager; |
| 13 | _context: UI.Context.Context; |
| 14 | _lastSelectedContextId?: string; |
| 15 | _ignoreContextChanged?: boolean; |
| 16 | |
| 17 | constructor(targetManager: SDK.SDKModel.TargetManager, context: UI.Context.Context) { |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 18 | context.addFlavorChangeListener(SDK.RuntimeModel.ExecutionContext, this._executionContextChanged, this); |
| 19 | context.addFlavorChangeListener(SDK.SDKModel.Target, this._targetChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 20 | |
| 21 | targetManager.addModelListener( |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 22 | SDK.RuntimeModel.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 23 | this); |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 24 | targetManager.addModelListener( |
| 25 | SDK.RuntimeModel.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, |
| 26 | this._onExecutionContextDestroyed, this); |
| 27 | targetManager.addModelListener( |
| 28 | SDK.RuntimeModel.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextOrderChanged, |
| 29 | this._onExecutionContextOrderChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | this._targetManager = targetManager; |
| 31 | this._context = context; |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 32 | targetManager.observeModels(SDK.RuntimeModel.RuntimeModel, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 33 | } |
| 34 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 35 | modelAdded(runtimeModel: SDK.RuntimeModel.RuntimeModel): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | // Defer selecting default target since we need all clients to get their |
| 37 | // targetAdded notifications first. |
Mathias Bynens | a0d6045 | 2020-11-30 11:45:33 | [diff] [blame] | 38 | queueMicrotask(deferred.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 40 | function deferred(this: ExecutionContextSelector): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | // We always want the second context for the service worker targets. |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 42 | if (!this._context.flavor(SDK.SDKModel.Target)) { |
| 43 | this._context.setFlavor(SDK.SDKModel.Target, runtimeModel.target()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 44 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 48 | modelRemoved(runtimeModel: SDK.RuntimeModel.RuntimeModel): void { |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 49 | const currentExecutionContext = this._context.flavor(SDK.RuntimeModel.ExecutionContext); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 50 | if (currentExecutionContext && currentExecutionContext.runtimeModel === runtimeModel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 51 | this._currentExecutionContextGone(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 52 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 54 | const models = this._targetManager.models(SDK.RuntimeModel.RuntimeModel); |
| 55 | if (this._context.flavor(SDK.SDKModel.Target) === runtimeModel.target() && models.length) { |
| 56 | this._context.setFlavor(SDK.SDKModel.Target, models[0].target()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 57 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 58 | } |
| 59 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 60 | _executionContextChanged(event: Common.EventTarget.EventTargetEvent): void { |
| 61 | const newContext = (event.data as SDK.RuntimeModel.ExecutionContext | null); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | if (newContext) { |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 63 | this._context.setFlavor(SDK.SDKModel.Target, newContext.target()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 64 | if (!this._ignoreContextChanged) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 65 | this._lastSelectedContextId = this._contextPersistentId(newContext); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 66 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 70 | _contextPersistentId(executionContext: SDK.RuntimeModel.ExecutionContext): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 71 | return executionContext.isDefault ? executionContext.target().name() + ':' + executionContext.frameId : ''; |
| 72 | } |
| 73 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 74 | _targetChanged(event: Common.EventTarget.EventTargetEvent): void { |
| 75 | const newTarget = (event.data as SDK.SDKModel.Target | null); |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 76 | const currentContext = this._context.flavor(SDK.RuntimeModel.ExecutionContext); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 78 | if (!newTarget || (currentContext && currentContext.target() === newTarget)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 80 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 82 | const runtimeModel = newTarget.model(SDK.RuntimeModel.RuntimeModel); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | const executionContexts = runtimeModel ? runtimeModel.executionContexts() : []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 84 | if (!executionContexts.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 86 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 87 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 88 | let newContext: SDK.RuntimeModel.ExecutionContext|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 89 | for (let i = 0; i < executionContexts.length && !newContext; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 90 | if (this._shouldSwitchToContext(executionContexts[i])) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 91 | newContext = executionContexts[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 92 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 93 | } |
| 94 | for (let i = 0; i < executionContexts.length && !newContext; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 95 | if (this._isDefaultContext(executionContexts[i])) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 96 | newContext = executionContexts[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 97 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | } |
| 99 | this._ignoreContextChanged = true; |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 100 | this._context.setFlavor(SDK.RuntimeModel.ExecutionContext, newContext || executionContexts[0]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 101 | this._ignoreContextChanged = false; |
| 102 | } |
| 103 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 104 | _shouldSwitchToContext(executionContext: SDK.RuntimeModel.ExecutionContext): boolean { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 105 | if (this._lastSelectedContextId && this._lastSelectedContextId === this._contextPersistentId(executionContext)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 107 | } |
| 108 | if (!this._lastSelectedContextId && this._isDefaultContext(executionContext)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 109 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 110 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 114 | _isDefaultContext(executionContext: SDK.RuntimeModel.ExecutionContext): boolean { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 115 | if (!executionContext.isDefault || !executionContext.frameId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 116 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 117 | } |
| 118 | if (executionContext.target().parentTarget()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 119 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 120 | } |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 121 | const resourceTreeModel = executionContext.target().model(SDK.ResourceTreeModel.ResourceTreeModel); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | const frame = resourceTreeModel && resourceTreeModel.frameForId(executionContext.frameId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 123 | if (frame && frame.isTopFrame()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 125 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 129 | _onExecutionContextCreated(event: Common.EventTarget.EventTargetEvent): void { |
| 130 | this._switchContextIfNecessary((event.data as SDK.RuntimeModel.ExecutionContext)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 131 | } |
| 132 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 133 | _onExecutionContextDestroyed(event: Common.EventTarget.EventTargetEvent): void { |
| 134 | const executionContext = (event.data as SDK.RuntimeModel.ExecutionContext); |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 135 | if (this._context.flavor(SDK.RuntimeModel.ExecutionContext) === executionContext) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 136 | this._currentExecutionContextGone(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 137 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 138 | } |
| 139 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 140 | _onExecutionContextOrderChanged(event: Common.EventTarget.EventTargetEvent): void { |
| 141 | const runtimeModel = (event.data as SDK.RuntimeModel.RuntimeModel); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | const executionContexts = runtimeModel.executionContexts(); |
| 143 | for (let i = 0; i < executionContexts.length; i++) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 144 | if (this._switchContextIfNecessary(executionContexts[i])) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 145 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 146 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 150 | _switchContextIfNecessary(executionContext: SDK.RuntimeModel.ExecutionContext): boolean { |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 151 | if (!this._context.flavor(SDK.RuntimeModel.ExecutionContext) || this._shouldSwitchToContext(executionContext)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 152 | this._ignoreContextChanged = true; |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 153 | this._context.setFlavor(SDK.RuntimeModel.ExecutionContext, executionContext); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | this._ignoreContextChanged = false; |
| 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 160 | _currentExecutionContextGone(): void { |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 161 | const runtimeModels = this._targetManager.models(SDK.RuntimeModel.RuntimeModel); |
Jan Scheffler | b4eb22d | 2021-04-05 20:38:36 | [diff] [blame^] | 162 | let newContext: SDK.RuntimeModel.ExecutionContext|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 163 | for (let i = 0; i < runtimeModels.length && !newContext; ++i) { |
| 164 | const executionContexts = runtimeModels[i].executionContexts(); |
| 165 | for (const executionContext of executionContexts) { |
| 166 | if (this._isDefaultContext(executionContext)) { |
| 167 | newContext = executionContext; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | if (!newContext) { |
| 173 | for (let i = 0; i < runtimeModels.length && !newContext; ++i) { |
| 174 | const executionContexts = runtimeModels[i].executionContexts(); |
| 175 | if (executionContexts.length) { |
| 176 | newContext = executionContexts[0]; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | this._ignoreContextChanged = true; |
Tim van der Lippe | 1002135 | 2020-02-13 15:59:21 | [diff] [blame] | 182 | this._context.setFlavor(SDK.RuntimeModel.ExecutionContext, newContext); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | this._ignoreContextChanged = false; |
| 184 | } |
Paul Lewis | c1d99fa | 2019-12-10 16:26:28 | [diff] [blame] | 185 | } |