blob: b21a3c42d24c256737d14a984b72413bfe21e8e2 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
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 Franklin2c1e9442020-07-20 10:11:4830// @ts-nocheck
31// TODO(crbug.com/1011811): Enable TypeScript compiler checks
Tim van der Lippe55cd4ef2020-01-23 17:31:3932
33import * as Common from '../common/common.js';
34import * as SDK from '../sdk/sdk.js';
35import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars
36
Paul Lewis67665be2020-03-17 13:15:1037/**
38 * @type {!NetworkProjectManager}
39 */
40let networkProjectManagerInstance;
41
42export 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 Reformat4c46d092018-04-07 15:32:3761
Tim van der Lippe07232042019-10-08 15:58:0762export const Events = {
Blink Reformat4c46d092018-04-07 15:32:3763 FrameAttributionAdded: Symbol('FrameAttributionAdded'),
64 FrameAttributionRemoved: Symbol('FrameAttributionRemoved')
65};
66
67/**
68 * @unrestricted
69 */
Paul Lewisef05f962020-01-09 15:21:4670export class NetworkProject {
Blink Reformat4c46d092018-04-07 15:32:3771 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:3972 * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode
Blink Reformat4c46d092018-04-07 15:32:3773 * @param {string} frameId
74 */
75 static _resolveFrame(uiSourceCode, frameId) {
Tim van der Lippe07232042019-10-08 15:58:0776 const target = NetworkProject.targetForUISourceCode(uiSourceCode);
Tim van der Lippe55cd4ef2020-01-23 17:31:3977 const resourceTreeModel = target && target.model(SDK.ResourceTreeModel.ResourceTreeModel);
Blink Reformat4c46d092018-04-07 15:32:3778 return resourceTreeModel ? resourceTreeModel.frameForId(frameId) : null;
79 }
80
81 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:3982 * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode
Blink Reformat4c46d092018-04-07 15:32:3783 * @param {string} frameId
84 */
85 static setInitialFrameAttribution(uiSourceCode, frameId) {
Tim van der Lippe07232042019-10-08 15:58:0786 const frame = NetworkProject._resolveFrame(uiSourceCode, frameId);
Tim van der Lippe1d6e57a2019-09-30 11:55:3487 if (!frame) {
Blink Reformat4c46d092018-04-07 15:32:3788 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3489 }
Tim van der Lippe55cd4ef2020-01-23 17:31:3990 /** @type {!Map<string, !{frame: !SDK.ResourceTreeModel.ResourceTreeFrame, count: number}>} */
Blink Reformat4c46d092018-04-07 15:32:3791 const attribution = new Map();
92 attribution.set(frameId, {frame: frame, count: 1});
Tim van der Lippe07232042019-10-08 15:58:0793 uiSourceCode[_frameAttributionSymbol] = attribution;
Blink Reformat4c46d092018-04-07 15:32:3794 }
95
96 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:3997 * @param {!Workspace.UISourceCode.UISourceCode} fromUISourceCode
98 * @param {!Workspace.UISourceCode.UISourceCode} toUISourceCode
Blink Reformat4c46d092018-04-07 15:32:3799 */
100 static cloneInitialFrameAttribution(fromUISourceCode, toUISourceCode) {
Tim van der Lippe07232042019-10-08 15:58:07101 const fromAttribution = fromUISourceCode[_frameAttributionSymbol];
Tim van der Lippe1d6e57a2019-09-30 11:55:34102 if (!fromAttribution) {
Blink Reformat4c46d092018-04-07 15:32:37103 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34104 }
Tim van der Lippe55cd4ef2020-01-23 17:31:39105 /** @type {!Map<string, !{frame: !SDK.ResourceTreeModel.ResourceTreeFrame, count: number}>} */
Blink Reformat4c46d092018-04-07 15:32:37106 const toAttribution = new Map();
Tim van der Lippe07232042019-10-08 15:58:07107 toUISourceCode[_frameAttributionSymbol] = toAttribution;
Blink Reformat4c46d092018-04-07 15:32:37108 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 Lippe55cd4ef2020-01-23 17:31:39115 * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode
Blink Reformat4c46d092018-04-07 15:32:37116 * @param {string} frameId
117 */
118 static addFrameAttribution(uiSourceCode, frameId) {
Tim van der Lippe07232042019-10-08 15:58:07119 const frame = NetworkProject._resolveFrame(uiSourceCode, frameId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34120 if (!frame) {
Blink Reformat4c46d092018-04-07 15:32:37121 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34122 }
Tim van der Lippe07232042019-10-08 15:58:07123 const frameAttribution = uiSourceCode[_frameAttributionSymbol];
Blink Reformat4c46d092018-04-07 15:32:37124 const attributionInfo = frameAttribution.get(frameId) || {frame: frame, count: 0};
125 attributionInfo.count += 1;
126 frameAttribution.set(frameId, attributionInfo);
Tim van der Lippe1d6e57a2019-09-30 11:55:34127 if (attributionInfo.count !== 1) {
Blink Reformat4c46d092018-04-07 15:32:37128 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34129 }
Blink Reformat4c46d092018-04-07 15:32:37130
131 const data = {uiSourceCode: uiSourceCode, frame: frame};
Paul Lewis67665be2020-03-17 13:15:10132 NetworkProjectManager.instance().dispatchEventToListeners(Events.FrameAttributionAdded, data);
Blink Reformat4c46d092018-04-07 15:32:37133 }
134
135 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:39136 * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode
Blink Reformat4c46d092018-04-07 15:32:37137 * @param {string} frameId
138 */
139 static removeFrameAttribution(uiSourceCode, frameId) {
Tim van der Lippe07232042019-10-08 15:58:07140 const frameAttribution = uiSourceCode[_frameAttributionSymbol];
Tim van der Lippe1d6e57a2019-09-30 11:55:34141 if (!frameAttribution) {
Alexey Kozyatinskiy4c2bb7b2018-07-03 21:22:58142 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34143 }
Blink Reformat4c46d092018-04-07 15:32:37144 const attributionInfo = frameAttribution.get(frameId);
145 console.assert(attributionInfo, 'Failed to remove frame attribution for url: ' + uiSourceCode.url());
Michael Liao4e79dd62020-05-14 21:09:15146 if (!attributionInfo) {
147 return;
148 }
Blink Reformat4c46d092018-04-07 15:32:37149 attributionInfo.count -= 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:34150 if (attributionInfo.count > 0) {
Blink Reformat4c46d092018-04-07 15:32:37151 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34152 }
Blink Reformat4c46d092018-04-07 15:32:37153 frameAttribution.delete(frameId);
154 const data = {uiSourceCode: uiSourceCode, frame: attributionInfo.frame};
Paul Lewis67665be2020-03-17 13:15:10155 NetworkProjectManager.instance().dispatchEventToListeners(Events.FrameAttributionRemoved, data);
Blink Reformat4c46d092018-04-07 15:32:37156 }
157
158 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:39159 * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode
160 * @return {?SDK.SDKModel.Target} target
Blink Reformat4c46d092018-04-07 15:32:37161 */
162 static targetForUISourceCode(uiSourceCode) {
Tim van der Lippe07232042019-10-08 15:58:07163 return uiSourceCode.project()[_targetSymbol] || null;
Blink Reformat4c46d092018-04-07 15:32:37164 }
165
166 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:39167 * @param {!Workspace.Workspace.Project} project
168 * @param {!SDK.SDKModel.Target} target
Blink Reformat4c46d092018-04-07 15:32:37169 */
170 static setTargetForProject(project, target) {
Tim van der Lippe07232042019-10-08 15:58:07171 project[_targetSymbol] = target;
Blink Reformat4c46d092018-04-07 15:32:37172 }
173
174 /**
Tim van der Lippe55cd4ef2020-01-23 17:31:39175 * @param {!Workspace.UISourceCode.UISourceCode} uiSourceCode
176 * @return {!Array<!SDK.ResourceTreeModel.ResourceTreeFrame>}
Blink Reformat4c46d092018-04-07 15:32:37177 */
178 static framesForUISourceCode(uiSourceCode) {
Tim van der Lippe07232042019-10-08 15:58:07179 const target = NetworkProject.targetForUISourceCode(uiSourceCode);
Tim van der Lippe55cd4ef2020-01-23 17:31:39180 const resourceTreeModel = target && target.model(SDK.ResourceTreeModel.ResourceTreeModel);
Tim van der Lippe07232042019-10-08 15:58:07181 const attribution = uiSourceCode[_frameAttributionSymbol];
Tim van der Lippe1d6e57a2019-09-30 11:55:34182 if (!resourceTreeModel || !attribution) {
Blink Reformat4c46d092018-04-07 15:32:37183 return [];
Tim van der Lippe1d6e57a2019-09-30 11:55:34184 }
Blink Reformat4c46d092018-04-07 15:32:37185 const frames = Array.from(attribution.keys()).map(frameId => resourceTreeModel.frameForId(frameId));
186 return frames.filter(frame => !!frame);
187 }
Tim van der Lippe07232042019-10-08 15:58:07188}
Blink Reformat4c46d092018-04-07 15:32:37189
Tim van der Lippec96ccd92019-11-29 16:23:54190const _targetSymbol = Symbol('target');
191const _frameAttributionSymbol = Symbol('_frameAttributionSymbol');