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 { |
| 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 * @param {string} url |
| 42 * @return {?Workspace.UISourceCode} |
| 43 */ |
| 44 uiSourceCodeForURL(target, url) { |
| 45 var resourceTreeModel = target.model(SDK.ResourceTreeModel); |
| 46 var binding = resourceTreeModel ? this._modelToBinding.get(resourceTreeModel
) : null; |
| 47 return binding ? binding.uiSourceCodeForURL(url) : null; |
| 48 } |
| 49 |
| 50 /** |
| 51 * @param {!SDK.Target} target |
| 52 * @param {!Workspace.UISourceCode} uiSourceCode |
| 53 * @return {boolean} |
| 54 */ |
| 55 ownsUISourceCode(target, uiSourceCode) { |
| 56 var resourceTreeModel = target.model(SDK.ResourceTreeModel); |
| 57 if (!resourceTreeModel) |
| 58 return false; |
| 59 var binding = this._modelToBinding.get(resourceTreeModel); |
| 60 if (!binding) |
| 61 return false; |
| 62 var resourceFile = binding._resourceFiles.get(uiSourceCode.url()); |
| 63 return resourceFile ? resourceFile._uiSourceCode === uiSourceCode : false; |
| 64 } |
| 65 |
| 66 /** |
| 67 * @param {!SDK.Target} target |
| 68 */ |
| 69 _resetForTest(target) { |
| 70 var resourceTreeModel = target.model(SDK.ResourceTreeModel); |
| 71 var binding = resourceTreeModel ? this._modelToBinding.get(resourceTreeModel
) : null; |
| 72 return binding ? binding._resetForTest() : null; |
| 73 } |
| 74 }; |
| 75 |
| 76 Bindings.ResourceBinding = class { |
| 77 /** |
| 78 * @param {!Workspace.Workspace} workspace |
| 79 * @param {!SDK.ResourceTreeModel} resourceTreeModel |
| 80 */ |
| 81 constructor(workspace, resourceTreeModel) { |
| 82 var target = resourceTreeModel.target(); |
| 83 this._project = new Bindings.ContentProviderBasedProject( |
| 84 workspace, 'resources:' + target.id(), Workspace.projectTypes.Network, '
', false /* isServiceProject */); |
| 85 Bindings.NetworkProject.setTargetForProject(this._project, target); |
| 86 |
| 87 /** @type {!Map<string, !Bindings.ResourceBinding.ResourceFile>} */ |
| 88 this._resourceFiles = new Map(); |
| 89 |
| 90 this._eventListeners = [ |
| 91 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.ResourceAd
ded, this._resourceAdded, this), |
| 92 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.FrameWillN
avigate, this._frameWillNavigate, this), |
| 93 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.FrameDetac
hed, this._frameDetached, this) |
| 94 ]; |
| 95 } |
| 96 |
| 97 /** |
| 98 * @param {string} url |
| 99 * @return {?Workspace.UISourceCode} |
| 100 */ |
| 101 uiSourceCodeForURL(url) { |
| 102 return this._project.uiSourceCodeForURL(url); |
| 103 } |
| 104 |
| 105 /** |
| 106 * @param {!SDK.Resource} resource |
| 107 */ |
| 108 _acceptsResource(resource) { |
| 109 var resourceType = resource.resourceType(); |
| 110 // Only load selected resource types from resources. |
| 111 if (resourceType !== Common.resourceTypes.Image && resourceType !== Common.r
esourceTypes.Font && |
| 112 resourceType !== Common.resourceTypes.Document && resourceType !== Commo
n.resourceTypes.Manifest) |
| 113 return false; |
| 114 |
| 115 // Ignore non-images and non-fonts. |
| 116 if (resourceType === Common.resourceTypes.Image && resource.mimeType && !res
ource.mimeType.startsWith('image')) |
| 117 return false; |
| 118 if (resourceType === Common.resourceTypes.Font && resource.mimeType && !reso
urce.mimeType.includes('font')) |
| 119 return false; |
| 120 if ((resourceType === Common.resourceTypes.Image || resourceType === Common.
resourceTypes.Font) && |
| 121 resource.contentURL().startsWith('data:')) |
| 122 return false; |
| 123 return true; |
| 124 } |
| 125 |
| 126 /** |
| 127 * @param {!Common.Event} event |
| 128 */ |
| 129 _resourceAdded(event) { |
| 130 var resource = /** @type {!SDK.Resource} */ (event.data); |
| 131 if (!this._acceptsResource(resource)) |
| 132 return; |
| 133 |
| 134 var resourceFile = this._resourceFiles.get(resource.url); |
| 135 if (!resourceFile) { |
| 136 resourceFile = new Bindings.ResourceBinding.ResourceFile(this._project, re
source); |
| 137 this._resourceFiles.set(resource.url, resourceFile); |
| 138 } else { |
| 139 resourceFile.addResource(resource); |
| 140 } |
| 141 } |
| 142 |
| 143 /** |
| 144 * @param {!SDK.ResourceTreeFrame} frame |
| 145 */ |
| 146 _removeFrameResources(frame) { |
| 147 for (var resource of frame.resources()) { |
| 148 if (!this._acceptsResource(resource)) |
| 149 continue; |
| 150 var resourceFile = this._resourceFiles.get(resource.url); |
| 151 if (resourceFile._resources.size === 1) { |
| 152 resourceFile.dispose(); |
| 153 this._resourceFiles.delete(resource.url); |
| 154 } else { |
| 155 resourceFile.removeResource(resource); |
| 156 } |
| 157 } |
| 158 } |
| 159 |
| 160 /** |
| 161 * @param {!Common.Event} event |
| 162 */ |
| 163 _frameWillNavigate(event) { |
| 164 var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); |
| 165 this._removeFrameResources(frame); |
| 166 } |
| 167 |
| 168 /** |
| 169 * @param {!Common.Event} event |
| 170 */ |
| 171 _frameDetached(event) { |
| 172 var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); |
| 173 this._removeFrameResources(frame); |
| 174 } |
| 175 |
| 176 _resetForTest() { |
| 177 for (var resourceFile of this._resourceFiles.valuesArray()) |
| 178 resourceFile.dispose(); |
| 179 this._resourceFiles.clear(); |
| 180 } |
| 181 |
| 182 dispose() { |
| 183 Common.EventTarget.removeEventListeners(this._eventListeners); |
| 184 for (var resourceFile of this._resourceFiles.valuesArray()) |
| 185 resourceFile.dispose(); |
| 186 this._resourceFiles.clear(); |
| 187 this._project.removeProject(); |
| 188 } |
| 189 }; |
| 190 |
| 191 /** |
| 192 * @implements {Common.ContentProvider} |
| 193 */ |
| 194 Bindings.ResourceBinding.ResourceFile = class { |
| 195 constructor(project, resource) { |
| 196 this._resources = new Set([resource]); |
| 197 this._project = project; |
| 198 this._uiSourceCode = this._project.createUISourceCode(resource.url, resource
.contentType()); |
| 199 Bindings.NetworkProject.setInitialFrameAttribution(this._uiSourceCode, resou
rce.frameId); |
| 200 this._project.addUISourceCodeWithProvider( |
| 201 this._uiSourceCode, this, Bindings.resourceMetadata(resource), resource.
mimeType); |
| 202 } |
| 203 |
| 204 /** |
| 205 * @param {!SDK.Resource} resource |
| 206 */ |
| 207 addResource(resource) { |
| 208 this._resources.add(resource); |
| 209 Bindings.NetworkProject.addFrameAttribution(this._uiSourceCode, resource.fra
meId); |
| 210 } |
| 211 |
| 212 /** |
| 213 * @param {!SDK.Resource} resource |
| 214 */ |
| 215 removeResource(resource) { |
| 216 this._resources.delete(resource); |
| 217 Bindings.NetworkProject.removeFrameAttribution(this._uiSourceCode, resource.
frameId); |
| 218 } |
| 219 |
| 220 dispose() { |
| 221 this._project.removeFile(this._uiSourceCode.url()); |
| 222 } |
| 223 |
| 224 /** |
| 225 * @override |
| 226 * @return {string} |
| 227 */ |
| 228 contentURL() { |
| 229 return this._resources.firstValue().contentURL(); |
| 230 } |
| 231 |
| 232 /** |
| 233 * @override |
| 234 * @return {!Common.ResourceType} |
| 235 */ |
| 236 contentType() { |
| 237 return this._resources.firstValue().contentType(); |
| 238 } |
| 239 |
| 240 /** |
| 241 * @override |
| 242 * @return {!Promise<?string>} |
| 243 */ |
| 244 requestContent() { |
| 245 return this._resources.firstValue().requestContent(); |
| 246 } |
| 247 |
| 248 /** |
| 249 * @override |
| 250 * @param {string} query |
| 251 * @param {boolean} caseSensitive |
| 252 * @param {boolean} isRegex |
| 253 * @return {!Promise<!Array<!Common.ContentProvider.SearchMatch>>} |
| 254 */ |
| 255 searchInContent(query, caseSensitive, isRegex) { |
| 256 return this._resources.firstValue().searchInContent(query, caseSensitive, is
Regex); |
| 257 } |
| 258 }; |
OLD | NEW |