Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Jack Franklin | 2c1e944 | 2020-07-20 10:11:48 | [diff] [blame] | 30 | // @ts-nocheck |
| 31 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 32 | |
| 33 | import * as Common from '../common/common.js'; |
| 34 | import * as SDK from '../sdk/sdk.js'; |
| 35 | import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars |
| 36 | |
Paul Lewis | 67665be | 2020-03-17 13:15:10 | [diff] [blame] | 37 | /** |
| 38 | * @type {!NetworkProjectManager} |
| 39 | */ |
| 40 | let networkProjectManagerInstance; |
| 41 | |
| 42 | export class NetworkProjectManager extends Common.ObjectWrapper.ObjectWrapper { |
| 43 | /** |
| 44 | * @private |
| 45 | */ |
| 46 | constructor() { |
| 47 | super(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param {{forceNew: boolean}} opts |
| 52 | */ |
| 53 | static instance({forceNew} = {forceNew: false}) { |
| 54 | if (!networkProjectManagerInstance || forceNew) { |
| 55 | networkProjectManagerInstance = new NetworkProjectManager(); |
| 56 | } |
| 57 | |
| 58 | return networkProjectManagerInstance; |
| 59 | } |
| 60 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 62 | export const Events = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 63 | FrameAttributionAdded: Symbol('FrameAttributionAdded'), |
| 64 | FrameAttributionRemoved: Symbol('FrameAttributionRemoved') |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * @unrestricted |
| 69 | */ |
Paul Lewis | ef05f96 | 2020-01-09 15:21:46 | [diff] [blame] | 70 | export class NetworkProject { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 71 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 72 | * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | * @param {string} frameId |
| 74 | */ |
| 75 | static _resolveFrame(uiSourceCode, frameId) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 76 | const target = NetworkProject.targetForUISourceCode(uiSourceCode); |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 77 | const resourceTreeModel = target && target.model(SDK.ResourceTreeModel.ResourceTreeModel); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 78 | return resourceTreeModel ? resourceTreeModel.frameForId(frameId) : null; |
| 79 | } |
| 80 | |
| 81 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 82 | * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | * @param {string} frameId |
| 84 | */ |
| 85 | static setInitialFrameAttribution(uiSourceCode, frameId) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 86 | const frame = NetworkProject._resolveFrame(uiSourceCode, frameId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 87 | if (!frame) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 89 | } |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 90 | /** @type {!Map<string, !{frame: !SDK.ResourceTreeModel.ResourceTreeFrame, count: number}>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 91 | const attribution = new Map(); |
| 92 | attribution.set(frameId, {frame: frame, count: 1}); |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 93 | uiSourceCode[_frameAttributionSymbol] = attribution; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 97 | * @param {!Workspace.UISourceCode.UISourceCode} fromUISourceCode |
| 98 | * @param {!Workspace.UISourceCode.UISourceCode} toUISourceCode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 99 | */ |
| 100 | static cloneInitialFrameAttribution(fromUISourceCode, toUISourceCode) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 101 | const fromAttribution = fromUISourceCode[_frameAttributionSymbol]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 102 | if (!fromAttribution) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 103 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 104 | } |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 105 | /** @type {!Map<string, !{frame: !SDK.ResourceTreeModel.ResourceTreeFrame, count: number}>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | const toAttribution = new Map(); |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 107 | toUISourceCode[_frameAttributionSymbol] = toAttribution; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | for (const frameId of fromAttribution.keys()) { |
| 109 | const value = fromAttribution.get(frameId); |
| 110 | toAttribution.set(frameId, {frame: value.frame, count: value.count}); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 115 | * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 116 | * @param {string} frameId |
| 117 | */ |
| 118 | static addFrameAttribution(uiSourceCode, frameId) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 119 | const frame = NetworkProject._resolveFrame(uiSourceCode, frameId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 120 | if (!frame) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 121 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 122 | } |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 123 | const frameAttribution = uiSourceCode[_frameAttributionSymbol]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | const attributionInfo = frameAttribution.get(frameId) || {frame: frame, count: 0}; |
| 125 | attributionInfo.count += 1; |
| 126 | frameAttribution.set(frameId, attributionInfo); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 127 | if (attributionInfo.count !== 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 129 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 130 | |
| 131 | const data = {uiSourceCode: uiSourceCode, frame: frame}; |
Paul Lewis | 67665be | 2020-03-17 13:15:10 | [diff] [blame] | 132 | NetworkProjectManager.instance().dispatchEventToListeners(Events.FrameAttributionAdded, data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 136 | * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 137 | * @param {string} frameId |
| 138 | */ |
| 139 | static removeFrameAttribution(uiSourceCode, frameId) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 140 | const frameAttribution = uiSourceCode[_frameAttributionSymbol]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 141 | if (!frameAttribution) { |
Alexey Kozyatinskiy | 4c2bb7b | 2018-07-03 21:22:58 | [diff] [blame] | 142 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 143 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | const attributionInfo = frameAttribution.get(frameId); |
| 145 | console.assert(attributionInfo, 'Failed to remove frame attribution for url: ' + uiSourceCode.url()); |
Michael Liao | 4e79dd6 | 2020-05-14 21:09:15 | [diff] [blame] | 146 | if (!attributionInfo) { |
| 147 | return; |
| 148 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 149 | attributionInfo.count -= 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 150 | if (attributionInfo.count > 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 151 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 152 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 153 | frameAttribution.delete(frameId); |
| 154 | const data = {uiSourceCode: uiSourceCode, frame: attributionInfo.frame}; |
Paul Lewis | 67665be | 2020-03-17 13:15:10 | [diff] [blame] | 155 | NetworkProjectManager.instance().dispatchEventToListeners(Events.FrameAttributionRemoved, data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 159 | * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode |
| 160 | * @return {?SDK.SDKModel.Target} target |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 161 | */ |
| 162 | static targetForUISourceCode(uiSourceCode) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 163 | return uiSourceCode.project()[_targetSymbol] || null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 167 | * @param {!Workspace.Workspace.Project} project |
| 168 | * @param {!SDK.SDKModel.Target} target |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 169 | */ |
| 170 | static setTargetForProject(project, target) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 171 | project[_targetSymbol] = target; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 175 | * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode |
| 176 | * @return {!Array<!SDK.ResourceTreeModel.ResourceTreeFrame>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 177 | */ |
| 178 | static framesForUISourceCode(uiSourceCode) { |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 179 | const target = NetworkProject.targetForUISourceCode(uiSourceCode); |
Tim van der Lippe | 55cd4ef | 2020-01-23 17:31:39 | [diff] [blame] | 180 | const resourceTreeModel = target && target.model(SDK.ResourceTreeModel.ResourceTreeModel); |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 181 | const attribution = uiSourceCode[_frameAttributionSymbol]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 182 | if (!resourceTreeModel || !attribution) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | return []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 184 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 185 | const frames = Array.from(attribution.keys()).map(frameId => resourceTreeModel.frameForId(frameId)); |
| 186 | return frames.filter(frame => !!frame); |
| 187 | } |
Tim van der Lippe | 0723204 | 2019-10-08 15:58:07 | [diff] [blame] | 188 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 190 | const _targetSymbol = Symbol('target'); |
| 191 | const _frameAttributionSymbol = Symbol('_frameAttributionSymbol'); |