OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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. | |
4 | |
5 /** | |
6 * @implements {SDK.SDKModelObserver<!SDK.ResourceTreeModel>} | |
7 * @unrestricted | |
dgozman
2017/05/19 23:51:05
restrict it!
lushnikov
2017/06/09 01:29:08
Done.
| |
8 */ | |
9 Bindings.ResourceBindingManager = class { | |
10 /** | |
11 * @param {!SDK.TargetManager} targetManager | |
12 * @param {!Workspace.Workspace} workspace | |
13 */ | |
14 constructor(targetManager, workspace) { | |
15 this._workspace = workspace; | |
16 targetManager.observeModels(SDK.ResourceTreeModel, this); | |
17 } | |
18 | |
19 /** | |
20 * @override | |
21 * @param {!SDK.ResourceTreeModel} resourceModel | |
22 */ | |
23 modelAdded(resourceModel) { | |
24 new Bindings.ResourceBinding(this._workspace, resourceModel); | |
25 } | |
26 | |
27 /** | |
28 * @override | |
29 * @param {!SDK.ResourceTreeModel} resourceModel | |
30 */ | |
31 modelRemoved(resourceModel) { | |
32 resourceModel[Bindings.ResourceBinding._symbol].dispose(); | |
33 } | |
34 }; | |
35 | |
36 Bindings.ResourceBinding = class { | |
37 /** | |
38 * @param {!Workspace.Workspace} workspace | |
39 * @param {!SDK.ResourceTreeModel} resourceModel | |
40 */ | |
41 constructor(workspace, resourceModel) { | |
42 this._resourceModel = resourceModel; | |
dgozman
2017/05/19 23:51:05
this._resourceTreeModel
lushnikov
2017/06/09 01:29:08
Done.
| |
43 var target = resourceModel.target(); | |
44 resourceModel[Bindings.ResourceBinding._symbol] = this; | |
45 this._project = new Bindings.ContentProviderBasedProject( | |
46 workspace, 'resources:' + target.id(), Workspace.projectTypes.Network, ' ', false /* isServiceProject */); | |
47 Bindings.NetworkProject.setTargetForProject(this._project, target); | |
48 | |
49 /** @type {!Map<string, !Bindings.ResourceBinding.ResourceFile>} */ | |
50 this._resourceFiles = new Map(); | |
51 | |
52 this._eventListeners = [ | |
53 resourceModel.addEventListener(SDK.ResourceTreeModel.Events.ResourceAdded, this._resourceAdded, this), | |
54 resourceModel.addEventListener(SDK.ResourceTreeModel.Events.FrameWillNavig ate, this._frameWillNavigate, this), | |
55 resourceModel.addEventListener(SDK.ResourceTreeModel.Events.FrameDetached, this._frameDetached, this) | |
56 ]; | |
57 } | |
58 | |
59 /** | |
60 * @param {!SDK.Target} target | |
61 * @return {?Bindings.ResourceBinding} | |
62 */ | |
63 static forTarget(target) { | |
dgozman
2017/05/19 23:51:05
private
lushnikov
2017/06/09 01:29:08
Killed it.
| |
64 var resourceModel = target.model(SDK.ResourceTreeModel); | |
65 return resourceModel ? resourceModel[Bindings.ResourceBinding._symbol] || nu ll : null; | |
66 } | |
67 | |
68 /** | |
69 * @param {string} url | |
70 * @return {?Workspace.UISourceCode} | |
71 */ | |
72 uiSourceCodeForURL(url) { | |
73 return this._project.uiSourceCodeForURL(url); | |
74 } | |
75 | |
76 /** | |
77 * @param {!SDK.Resource} resource | |
78 */ | |
79 _acceptsResource(resource) { | |
80 var resourceType = resource.resourceType(); | |
81 // Only load selected resource types from resources. | |
82 if (resourceType !== Common.resourceTypes.Image && resourceType !== Common.r esourceTypes.Font && | |
83 resourceType !== Common.resourceTypes.Document && resourceType !== Commo n.resourceTypes.Manifest) | |
84 return false; | |
85 | |
86 // Ignore non-images and non-fonts. | |
87 if (resourceType === Common.resourceTypes.Image && resource.mimeType && !res ource.mimeType.startsWith('image')) | |
88 return false; | |
89 if (resourceType === Common.resourceTypes.Font && resource.mimeType && !reso urce.mimeType.includes('font')) | |
90 return false; | |
91 if ((resourceType === Common.resourceTypes.Image || resourceType === Common. resourceTypes.Font) && | |
92 resource.contentURL().startsWith('data:')) | |
93 return false; | |
94 return true; | |
95 } | |
96 | |
97 /** | |
98 * @param {!Common.Event} event | |
99 */ | |
100 _resourceAdded(event) { | |
101 var resource = /** @type {!SDK.Resource} */ (event.data); | |
102 if (!this._acceptsResource(resource)) | |
103 return; | |
104 | |
105 var resourceFile = this._resourceFiles.get(resource.url); | |
106 if (!resourceFile) { | |
107 resourceFile = new Bindings.ResourceBinding.ResourceFile(this._project, re source); | |
108 this._resourceFiles.set(resource.url, resourceFile); | |
109 } else { | |
110 resourceFile.addResource(resource); | |
111 } | |
112 } | |
113 | |
114 /** | |
115 * @param {!SDK.ResourceTreeFrame} frame | |
116 */ | |
117 _removeFrameResources(frame) { | |
118 for (var resource of frame.resources()) { | |
119 if (!this._acceptsResource(resource)) | |
120 continue; | |
121 var resourceFile = this._resourceFiles.get(resource.url); | |
122 if (resourceFile._resources.size === 1) { | |
123 resourceFile.dispose(); | |
124 this._resourceFiles.delete(resource.url); | |
125 } else { | |
126 resourceFile.removeResource(resource); | |
127 } | |
128 } | |
129 } | |
130 | |
131 /** | |
132 * @param {!Common.Event} event | |
133 */ | |
134 _frameWillNavigate(event) { | |
135 var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); | |
136 this._removeFrameResources(frame); | |
137 } | |
138 | |
139 /** | |
140 * @param {!Common.Event} event | |
141 */ | |
142 _frameDetached(event) { | |
143 var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); | |
144 this._removeFrameResources(frame); | |
145 } | |
146 | |
147 _resetForTest() { | |
148 for (var resourceFile of this._resourceFiles.valuesArray()) | |
149 resourceFile.dispose(); | |
150 this._resourceFiles.clear(); | |
151 } | |
152 | |
153 dispose() { | |
154 Common.EventTarget.removeEventListeners(this._eventListeners); | |
155 this._resourceModel[Bindings.ResourceBinding._symbol] = null; | |
156 for (var resourceFile of this._resourceFiles.valuesArray()) | |
157 resourceFile.dispose(); | |
158 this._resourceFiles.clear(); | |
159 this._project.removeProject(); | |
160 } | |
161 }; | |
162 | |
163 /** | |
164 * @implements {Common.ContentProvider} | |
165 */ | |
166 Bindings.ResourceBinding.ResourceFile = class { | |
167 constructor(project, resource) { | |
168 this._resources = new Set([resource]); | |
169 this._project = project; | |
170 this._uiSourceCode = this._project.createUISourceCode(resource.url, resource .contentType()); | |
171 Bindings.NetworkProject.setInitialFrameAttribution(this._uiSourceCode, resou rce.frameId); | |
172 this._project.addUISourceCodeWithProvider( | |
173 this._uiSourceCode, this, Bindings.resourceMetadata(resource), resource. mimeType); | |
174 } | |
175 | |
176 /** | |
177 * @param {!SDK.Resource} resource | |
178 */ | |
179 addResource(resource) { | |
180 this._resources.add(resource); | |
181 Bindings.NetworkProject.addFrameAttribution(this._uiSourceCode, resource.fra meId); | |
182 } | |
183 | |
184 /** | |
185 * @param {!SDK.Resource} resource | |
186 */ | |
187 removeResource(resource) { | |
188 this._resources.delete(resource); | |
189 Bindings.NetworkProject.removeFrameAttribution(this._uiSourceCode, resource. frameId); | |
190 } | |
191 | |
192 dispose() { | |
193 this._project.removeFile(this._uiSourceCode.url()); | |
194 } | |
195 | |
196 /** | |
197 * @override | |
198 * @return {string} | |
199 */ | |
200 contentURL() { | |
201 return this._resources.firstValue().contentURL(); | |
202 } | |
203 | |
204 /** | |
205 * @override | |
206 * @return {!Common.ResourceType} | |
207 */ | |
208 contentType() { | |
209 return this._resources.firstValue().contentType(); | |
210 } | |
211 | |
212 /** | |
213 * @override | |
214 * @return {!Promise<?string>} | |
215 */ | |
216 requestContent() { | |
217 return this._resources.firstValue().requestContent(); | |
218 } | |
219 | |
220 /** | |
221 * @override | |
222 * @param {string} query | |
223 * @param {boolean} caseSensitive | |
224 * @param {boolean} isRegex | |
225 * @param {function(!Array.<!Common.ContentProvider.SearchMatch>)} callback | |
226 */ | |
227 searchInContent(query, caseSensitive, isRegex, callback) { | |
228 this._resources.firstValue().searchInContent(query, caseSensitive, isRegex, callback); | |
229 } | |
230 }; | |
231 | |
232 Bindings.ResourceBinding._symbol = Symbol('Bindings.ResourceBinding._symbol'); | |
OLD | NEW |