blob: 50c7d67c1f73dcc830d50bffcf26470570e9c393 [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 */
30Bindings.NetworkProjectManager = class extends Common.Object {
31};
32
33Bindings.NetworkProjectManager.Events = {
34 FrameAttributionAdded: Symbol('FrameAttributionAdded'),
35 FrameAttributionRemoved: Symbol('FrameAttributionRemoved')
36};
37
38/**
39 * @unrestricted
40 */
41Bindings.NetworkProject = class {
42 /**
43 * @param {!Workspace.UISourceCode} uiSourceCode
44 * @param {string} frameId
45 */
46 static _resolveFrame(uiSourceCode, frameId) {
47 const target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode);
48 const resourceTreeModel = target && target.model(SDK.ResourceTreeModel);
49 return resourceTreeModel ? resourceTreeModel.frameForId(frameId) : null;
50 }
51
52 /**
53 * @param {!Workspace.UISourceCode} uiSourceCode
54 * @param {string} frameId
55 */
56 static setInitialFrameAttribution(uiSourceCode, frameId) {
57 const frame = Bindings.NetworkProject._resolveFrame(uiSourceCode, frameId);
58 if (!frame)
59 return;
60 /** @type {!Map<string, !{frame: !SDK.ResourceTreeFrame, count: number}>} */
61 const attribution = new Map();
62 attribution.set(frameId, {frame: frame, count: 1});
63 uiSourceCode[Bindings.NetworkProject._frameAttributionSymbol] = attribution;
64 }
65
66 /**
67 * @param {!Workspace.UISourceCode} fromUISourceCode
68 * @param {!Workspace.UISourceCode} toUISourceCode
69 */
70 static cloneInitialFrameAttribution(fromUISourceCode, toUISourceCode) {
71 const fromAttribution = fromUISourceCode[Bindings.NetworkProject._frameAttributionSymbol];
72 if (!fromAttribution)
73 return;
74 /** @type {!Map<string, !{frame: !SDK.ResourceTreeFrame, count: number}>} */
75 const toAttribution = new Map();
76 toUISourceCode[Bindings.NetworkProject._frameAttributionSymbol] = toAttribution;
77 for (const frameId of fromAttribution.keys()) {
78 const value = fromAttribution.get(frameId);
79 toAttribution.set(frameId, {frame: value.frame, count: value.count});
80 }
81 }
82
83 /**
84 * @param {!Workspace.UISourceCode} uiSourceCode
85 * @param {string} frameId
86 */
87 static addFrameAttribution(uiSourceCode, frameId) {
88 const frame = Bindings.NetworkProject._resolveFrame(uiSourceCode, frameId);
89 if (!frame)
90 return;
91 const frameAttribution = uiSourceCode[Bindings.NetworkProject._frameAttributionSymbol];
92 const attributionInfo = frameAttribution.get(frameId) || {frame: frame, count: 0};
93 attributionInfo.count += 1;
94 frameAttribution.set(frameId, attributionInfo);
95 if (attributionInfo.count !== 1)
96 return;
97
98 const data = {uiSourceCode: uiSourceCode, frame: frame};
99 Bindings.networkProjectManager.dispatchEventToListeners(
100 Bindings.NetworkProjectManager.Events.FrameAttributionAdded, data);
101 }
102
103 /**
104 * @param {!Workspace.UISourceCode} uiSourceCode
105 * @param {string} frameId
106 */
107 static removeFrameAttribution(uiSourceCode, frameId) {
108 const frameAttribution = uiSourceCode[Bindings.NetworkProject._frameAttributionSymbol];
109 const attributionInfo = frameAttribution.get(frameId);
110 console.assert(attributionInfo, 'Failed to remove frame attribution for url: ' + uiSourceCode.url());
111 attributionInfo.count -= 1;
112 if (attributionInfo.count > 0)
113 return;
114 frameAttribution.delete(frameId);
115 const data = {uiSourceCode: uiSourceCode, frame: attributionInfo.frame};
116 Bindings.networkProjectManager.dispatchEventToListeners(
117 Bindings.NetworkProjectManager.Events.FrameAttributionRemoved, data);
118 }
119
120 /**
121 * @param {!Workspace.UISourceCode} uiSourceCode
122 * @return {?SDK.Target} target
123 */
124 static targetForUISourceCode(uiSourceCode) {
125 return uiSourceCode.project()[Bindings.NetworkProject._targetSymbol] || null;
126 }
127
128 /**
129 * @param {!Workspace.Project} project
130 * @param {!SDK.Target} target
131 */
132 static setTargetForProject(project, target) {
133 project[Bindings.NetworkProject._targetSymbol] = target;
134 }
135
136 /**
137 * @param {!Workspace.UISourceCode} uiSourceCode
138 * @return {!Array<!SDK.ResourceTreeFrame>}
139 */
140 static framesForUISourceCode(uiSourceCode) {
141 const target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode);
142 const resourceTreeModel = target && target.model(SDK.ResourceTreeModel);
143 const attribution = uiSourceCode[Bindings.NetworkProject._frameAttributionSymbol];
144 if (!resourceTreeModel || !attribution)
145 return [];
146 const frames = Array.from(attribution.keys()).map(frameId => resourceTreeModel.frameForId(frameId));
147 return frames.filter(frame => !!frame);
148 }
149};
150
151Bindings.NetworkProject._targetSymbol = Symbol('target');
152Bindings.NetworkProject._frameAttributionSymbol = Symbol('Bindings.NetworkProject._frameAttributionSymbol');