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