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 */ | |
8 Bindings.ResourceBindingManager = class { | |
lushnikov
2017/06/12 23:17:53
Let's drift towards the following naming throughou
lushnikov
2017/06/13 00:49:14
Done.
| |
9 /** | |
10 * @param {!SDK.TargetManager} targetManager | |
11 * @param {!Workspace.Workspace} workspace | |
12 */ | |
13 constructor(targetManager, workspace) { | |
14 this._workspace = workspace; | |
15 /** @type {!Map<!SDK.ResourceTreeModel, !Bindings.ResourceBinding>} */ | |
16 this._modelToBinding = new Map(); | |
17 targetManager.observeModels(SDK.ResourceTreeModel, this); | |
18 } | |
19 | |
20 /** | |
21 * @override | |
22 * @param {!SDK.ResourceTreeModel} resourceTreeModel | |
23 */ | |
24 modelAdded(resourceTreeModel) { | |
25 var binding = new Bindings.ResourceBinding(this._workspace, resourceTreeMode l); | |
26 this._modelToBinding.set(resourceTreeModel, binding); | |
27 } | |
28 | |
29 /** | |
30 * @override | |
31 * @param {!SDK.ResourceTreeModel} resourceTreeModel | |
32 */ | |
33 modelRemoved(resourceTreeModel) { | |
34 var binding = this._modelToBinding.get(resourceTreeModel); | |
35 binding.dispose(); | |
36 this._modelToBinding.delete(resourceTreeModel); | |
37 } | |
38 | |
39 /** | |
40 * @param {!SDK.Target} target | |
41 * @return {?Bindings.ResourceBinding} | |
42 */ | |
43 _bindingForTarget(target) { | |
44 var resourceTreeModel = target.model(SDK.ResourceTreeModel); | |
45 return resourceTreeModel ? this._modelToBinding.get(resourceTreeModel) : nul l; | |
46 } | |
47 | |
48 /** | |
49 * @param {!SDK.CSSLocation} cssLocation | |
50 * @return {?Workspace.UILocation} | |
51 */ | |
52 cssLocationToUILocation(cssLocation) { | |
53 var binding = this._bindingForTarget(cssLocation.cssModel().target()); | |
54 if (!binding) | |
55 return null; | |
56 var uiSourceCode = binding.uiSourceCodeForURL(cssLocation.url); | |
57 return uiSourceCode ? uiSourceCode.uiLocation(cssLocation.lineNumber, cssLoc ation.columnNumber) : null; | |
58 } | |
59 | |
60 /** | |
61 * @param {!SDK.DebuggerModel.Location} jsLocation | |
62 * @return {?Workspace.UILocation} | |
63 */ | |
64 jsLocationToUILocation(jsLocation) { | |
65 var script = jsLocation.script(); | |
66 if (!script) | |
67 return null; | |
68 var binding = this._bindingForTarget(jsLocation.debuggerModel.target()); | |
69 if (!binding) | |
70 return null; | |
71 var uiSourceCode = binding.uiSourceCodeForURL(script.sourceURL); | |
72 return uiSourceCode ? uiSourceCode.uiLocation(jsLocation.lineNumber, jsLocat ion.columnNumber) : null; | |
73 } | |
74 | |
75 /** | |
76 * @param {!SDK.Target} target | |
77 */ | |
78 _resetForTest(target) { | |
79 var resourceTreeModel = target.model(SDK.ResourceTreeModel); | |
80 var binding = resourceTreeModel ? this._modelToBinding.get(resourceTreeModel ) : null; | |
lushnikov
2017/06/12 23:17:53
if (binding)
binding._resetForTest();
lushnikov
2017/06/13 00:49:14
Done.
| |
81 return binding ? binding._resetForTest() : null; | |
82 } | |
83 }; | |
84 | |
85 Bindings.ResourceBinding = class { | |
lushnikov
2017/06/12 23:17:53
Bindings.ResourceMapping.ModelInfo =
lushnikov
2017/06/13 00:49:13
Done.
| |
86 /** | |
87 * @param {!Workspace.Workspace} workspace | |
88 * @param {!SDK.ResourceTreeModel} resourceTreeModel | |
89 */ | |
90 constructor(workspace, resourceTreeModel) { | |
91 var target = resourceTreeModel.target(); | |
92 this._project = new Bindings.ContentProviderBasedProject( | |
93 workspace, 'resources:' + target.id(), Workspace.projectTypes.Network, ' ', false /* isServiceProject */); | |
94 Bindings.NetworkProject.setTargetForProject(this._project, target); | |
95 | |
96 /** @type {!Map<string, !Bindings.ResourceBinding.ResourceFile>} */ | |
97 this._resourceFiles = new Map(); | |
98 | |
99 this._eventListeners = [ | |
100 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.ResourceAd ded, this._resourceAdded, this), | |
101 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.FrameWillN avigate, this._frameWillNavigate, this), | |
102 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.FrameDetac hed, this._frameDetached, this) | |
103 ]; | |
104 } | |
105 | |
106 /** | |
107 * @param {string} url | |
108 * @return {?Workspace.UISourceCode} | |
109 */ | |
110 uiSourceCodeForURL(url) { | |
lushnikov
2017/06/12 23:17:53
inline
lushnikov
2017/06/13 00:49:14
Done.
| |
111 return this._project.uiSourceCodeForURL(url); | |
112 } | |
113 | |
114 /** | |
115 * @param {!SDK.Resource} resource | |
116 */ | |
117 _acceptsResource(resource) { | |
118 var resourceType = resource.resourceType(); | |
119 // Only load selected resource types from resources. | |
120 if (resourceType !== Common.resourceTypes.Image && resourceType !== Common.r esourceTypes.Font && | |
121 resourceType !== Common.resourceTypes.Document && resourceType !== Commo n.resourceTypes.Manifest) | |
122 return false; | |
123 | |
124 // Ignore non-images and non-fonts. | |
125 if (resourceType === Common.resourceTypes.Image && resource.mimeType && !res ource.mimeType.startsWith('image')) | |
126 return false; | |
127 if (resourceType === Common.resourceTypes.Font && resource.mimeType && !reso urce.mimeType.includes('font')) | |
128 return false; | |
129 if ((resourceType === Common.resourceTypes.Image || resourceType === Common. resourceTypes.Font) && | |
130 resource.contentURL().startsWith('data:')) | |
131 return false; | |
132 return true; | |
133 } | |
134 | |
135 /** | |
136 * @param {!Common.Event} event | |
137 */ | |
138 _resourceAdded(event) { | |
139 var resource = /** @type {!SDK.Resource} */ (event.data); | |
140 if (!this._acceptsResource(resource)) | |
141 return; | |
142 | |
143 var resourceFile = this._resourceFiles.get(resource.url); | |
144 if (!resourceFile) { | |
145 resourceFile = new Bindings.ResourceBinding.ResourceFile(this._project, re source); | |
146 this._resourceFiles.set(resource.url, resourceFile); | |
147 } else { | |
148 resourceFile.addResource(resource); | |
lushnikov
2017/06/12 23:17:53
call this unconditionally
lushnikov
2017/06/13 00:49:14
Kept as-is to be aligned with StylesSOurceMapping
| |
149 } | |
150 } | |
151 | |
152 /** | |
153 * @param {!SDK.ResourceTreeFrame} frame | |
154 */ | |
155 _removeFrameResources(frame) { | |
156 for (var resource of frame.resources()) { | |
157 if (!this._acceptsResource(resource)) | |
158 continue; | |
159 var resourceFile = this._resourceFiles.get(resource.url); | |
160 if (resourceFile._resources.size === 1) { | |
lushnikov
2017/06/12 23:17:53
if (resourceFile.removeResource(resource))
this.
lushnikov
2017/06/13 00:49:13
Kept as-is to be aligned with StylesSourceMapping
| |
161 resourceFile.dispose(); | |
162 this._resourceFiles.delete(resource.url); | |
163 } else { | |
164 resourceFile.removeResource(resource); | |
165 } | |
166 } | |
167 } | |
168 | |
169 /** | |
170 * @param {!Common.Event} event | |
171 */ | |
172 _frameWillNavigate(event) { | |
173 var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); | |
174 this._removeFrameResources(frame); | |
175 } | |
176 | |
177 /** | |
178 * @param {!Common.Event} event | |
179 */ | |
180 _frameDetached(event) { | |
181 var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); | |
182 this._removeFrameResources(frame); | |
183 } | |
184 | |
185 _resetForTest() { | |
186 for (var resourceFile of this._resourceFiles.valuesArray()) | |
187 resourceFile.dispose(); | |
188 this._resourceFiles.clear(); | |
189 } | |
190 | |
191 dispose() { | |
192 Common.EventTarget.removeEventListeners(this._eventListeners); | |
193 for (var resourceFile of this._resourceFiles.valuesArray()) | |
194 resourceFile.dispose(); | |
195 this._resourceFiles.clear(); | |
196 this._project.removeProject(); | |
197 } | |
198 }; | |
199 | |
200 /** | |
201 * @implements {Common.ContentProvider} | |
202 */ | |
203 Bindings.ResourceBinding.ResourceFile = class { | |
204 constructor(project, resource) { | |
lushnikov
2017/06/12 23:17:53
jsdoc
lushnikov
2017/06/13 00:49:13
Done.
| |
205 this._resources = new Set([resource]); | |
206 this._project = project; | |
207 this._uiSourceCode = this._project.createUISourceCode(resource.url, resource .contentType()); | |
208 Bindings.NetworkProject.setInitialFrameAttribution(this._uiSourceCode, resou rce.frameId); | |
209 this._project.addUISourceCodeWithProvider( | |
210 this._uiSourceCode, this, Bindings.resourceMetadata(resource), resource. mimeType); | |
211 } | |
212 | |
213 /** | |
214 * @param {!SDK.Resource} resource | |
215 */ | |
216 addResource(resource) { | |
217 this._resources.add(resource); | |
218 Bindings.NetworkProject.addFrameAttribution(this._uiSourceCode, resource.fra meId); | |
219 } | |
220 | |
221 /** | |
222 * @param {!SDK.Resource} resource | |
223 */ | |
224 removeResource(resource) { | |
225 this._resources.delete(resource); | |
226 Bindings.NetworkProject.removeFrameAttribution(this._uiSourceCode, resource. frameId); | |
227 } | |
228 | |
229 dispose() { | |
230 this._project.removeFile(this._uiSourceCode.url()); | |
231 } | |
232 | |
233 /** | |
234 * @override | |
235 * @return {string} | |
236 */ | |
237 contentURL() { | |
238 return this._resources.firstValue().contentURL(); | |
239 } | |
240 | |
241 /** | |
242 * @override | |
243 * @return {!Common.ResourceType} | |
244 */ | |
245 contentType() { | |
246 return this._resources.firstValue().contentType(); | |
247 } | |
248 | |
249 /** | |
250 * @override | |
251 * @return {!Promise<?string>} | |
252 */ | |
253 requestContent() { | |
254 return this._resources.firstValue().requestContent(); | |
255 } | |
256 | |
257 /** | |
258 * @override | |
259 * @param {string} query | |
260 * @param {boolean} caseSensitive | |
261 * @param {boolean} isRegex | |
262 * @return {!Promise<!Array<!Common.ContentProvider.SearchMatch>>} | |
263 */ | |
264 searchInContent(query, caseSensitive, isRegex) { | |
265 return this._resources.firstValue().searchInContent(query, caseSensitive, is Regex); | |
266 } | |
267 }; | |
OLD | NEW |