Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/front_end/bindings/ResourceBinding.js |
| diff --git a/third_party/WebKit/Source/devtools/front_end/bindings/ResourceBinding.js b/third_party/WebKit/Source/devtools/front_end/bindings/ResourceBinding.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a63e20dec574f177b43fe3f95c52234553011c56 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/front_end/bindings/ResourceBinding.js |
| @@ -0,0 +1,232 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @implements {SDK.SDKModelObserver<!SDK.ResourceTreeModel>} |
| + * @unrestricted |
|
dgozman
2017/05/19 23:51:05
restrict it!
lushnikov
2017/06/09 01:29:08
Done.
|
| + */ |
| +Bindings.ResourceBindingManager = class { |
| + /** |
| + * @param {!SDK.TargetManager} targetManager |
| + * @param {!Workspace.Workspace} workspace |
| + */ |
| + constructor(targetManager, workspace) { |
| + this._workspace = workspace; |
| + targetManager.observeModels(SDK.ResourceTreeModel, this); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {!SDK.ResourceTreeModel} resourceModel |
| + */ |
| + modelAdded(resourceModel) { |
| + new Bindings.ResourceBinding(this._workspace, resourceModel); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {!SDK.ResourceTreeModel} resourceModel |
| + */ |
| + modelRemoved(resourceModel) { |
| + resourceModel[Bindings.ResourceBinding._symbol].dispose(); |
| + } |
| +}; |
| + |
| +Bindings.ResourceBinding = class { |
| + /** |
| + * @param {!Workspace.Workspace} workspace |
| + * @param {!SDK.ResourceTreeModel} resourceModel |
| + */ |
| + constructor(workspace, resourceModel) { |
| + this._resourceModel = resourceModel; |
|
dgozman
2017/05/19 23:51:05
this._resourceTreeModel
lushnikov
2017/06/09 01:29:08
Done.
|
| + var target = resourceModel.target(); |
| + resourceModel[Bindings.ResourceBinding._symbol] = this; |
| + this._project = new Bindings.ContentProviderBasedProject( |
| + workspace, 'resources:' + target.id(), Workspace.projectTypes.Network, '', false /* isServiceProject */); |
| + Bindings.NetworkProject.setTargetForProject(this._project, target); |
| + |
| + /** @type {!Map<string, !Bindings.ResourceBinding.ResourceFile>} */ |
| + this._resourceFiles = new Map(); |
| + |
| + this._eventListeners = [ |
| + resourceModel.addEventListener(SDK.ResourceTreeModel.Events.ResourceAdded, this._resourceAdded, this), |
| + resourceModel.addEventListener(SDK.ResourceTreeModel.Events.FrameWillNavigate, this._frameWillNavigate, this), |
| + resourceModel.addEventListener(SDK.ResourceTreeModel.Events.FrameDetached, this._frameDetached, this) |
| + ]; |
| + } |
| + |
| + /** |
| + * @param {!SDK.Target} target |
| + * @return {?Bindings.ResourceBinding} |
| + */ |
| + static forTarget(target) { |
|
dgozman
2017/05/19 23:51:05
private
lushnikov
2017/06/09 01:29:08
Killed it.
|
| + var resourceModel = target.model(SDK.ResourceTreeModel); |
| + return resourceModel ? resourceModel[Bindings.ResourceBinding._symbol] || null : null; |
| + } |
| + |
| + /** |
| + * @param {string} url |
| + * @return {?Workspace.UISourceCode} |
| + */ |
| + uiSourceCodeForURL(url) { |
| + return this._project.uiSourceCodeForURL(url); |
| + } |
| + |
| + /** |
| + * @param {!SDK.Resource} resource |
| + */ |
| + _acceptsResource(resource) { |
| + var resourceType = resource.resourceType(); |
| + // Only load selected resource types from resources. |
| + if (resourceType !== Common.resourceTypes.Image && resourceType !== Common.resourceTypes.Font && |
| + resourceType !== Common.resourceTypes.Document && resourceType !== Common.resourceTypes.Manifest) |
| + return false; |
| + |
| + // Ignore non-images and non-fonts. |
| + if (resourceType === Common.resourceTypes.Image && resource.mimeType && !resource.mimeType.startsWith('image')) |
| + return false; |
| + if (resourceType === Common.resourceTypes.Font && resource.mimeType && !resource.mimeType.includes('font')) |
| + return false; |
| + if ((resourceType === Common.resourceTypes.Image || resourceType === Common.resourceTypes.Font) && |
| + resource.contentURL().startsWith('data:')) |
| + return false; |
| + return true; |
| + } |
| + |
| + /** |
| + * @param {!Common.Event} event |
| + */ |
| + _resourceAdded(event) { |
| + var resource = /** @type {!SDK.Resource} */ (event.data); |
| + if (!this._acceptsResource(resource)) |
| + return; |
| + |
| + var resourceFile = this._resourceFiles.get(resource.url); |
| + if (!resourceFile) { |
| + resourceFile = new Bindings.ResourceBinding.ResourceFile(this._project, resource); |
| + this._resourceFiles.set(resource.url, resourceFile); |
| + } else { |
| + resourceFile.addResource(resource); |
| + } |
| + } |
| + |
| + /** |
| + * @param {!SDK.ResourceTreeFrame} frame |
| + */ |
| + _removeFrameResources(frame) { |
| + for (var resource of frame.resources()) { |
| + if (!this._acceptsResource(resource)) |
| + continue; |
| + var resourceFile = this._resourceFiles.get(resource.url); |
| + if (resourceFile._resources.size === 1) { |
| + resourceFile.dispose(); |
| + this._resourceFiles.delete(resource.url); |
| + } else { |
| + resourceFile.removeResource(resource); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * @param {!Common.Event} event |
| + */ |
| + _frameWillNavigate(event) { |
| + var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); |
| + this._removeFrameResources(frame); |
| + } |
| + |
| + /** |
| + * @param {!Common.Event} event |
| + */ |
| + _frameDetached(event) { |
| + var frame = /** @type {!SDK.ResourceTreeFrame} */ (event.data); |
| + this._removeFrameResources(frame); |
| + } |
| + |
| + _resetForTest() { |
| + for (var resourceFile of this._resourceFiles.valuesArray()) |
| + resourceFile.dispose(); |
| + this._resourceFiles.clear(); |
| + } |
| + |
| + dispose() { |
| + Common.EventTarget.removeEventListeners(this._eventListeners); |
| + this._resourceModel[Bindings.ResourceBinding._symbol] = null; |
| + for (var resourceFile of this._resourceFiles.valuesArray()) |
| + resourceFile.dispose(); |
| + this._resourceFiles.clear(); |
| + this._project.removeProject(); |
| + } |
| +}; |
| + |
| +/** |
| + * @implements {Common.ContentProvider} |
| + */ |
| +Bindings.ResourceBinding.ResourceFile = class { |
| + constructor(project, resource) { |
| + this._resources = new Set([resource]); |
| + this._project = project; |
| + this._uiSourceCode = this._project.createUISourceCode(resource.url, resource.contentType()); |
| + Bindings.NetworkProject.setInitialFrameAttribution(this._uiSourceCode, resource.frameId); |
| + this._project.addUISourceCodeWithProvider( |
| + this._uiSourceCode, this, Bindings.resourceMetadata(resource), resource.mimeType); |
| + } |
| + |
| + /** |
| + * @param {!SDK.Resource} resource |
| + */ |
| + addResource(resource) { |
| + this._resources.add(resource); |
| + Bindings.NetworkProject.addFrameAttribution(this._uiSourceCode, resource.frameId); |
| + } |
| + |
| + /** |
| + * @param {!SDK.Resource} resource |
| + */ |
| + removeResource(resource) { |
| + this._resources.delete(resource); |
| + Bindings.NetworkProject.removeFrameAttribution(this._uiSourceCode, resource.frameId); |
| + } |
| + |
| + dispose() { |
| + this._project.removeFile(this._uiSourceCode.url()); |
| + } |
| + |
| + /** |
| + * @override |
| + * @return {string} |
| + */ |
| + contentURL() { |
| + return this._resources.firstValue().contentURL(); |
| + } |
| + |
| + /** |
| + * @override |
| + * @return {!Common.ResourceType} |
| + */ |
| + contentType() { |
| + return this._resources.firstValue().contentType(); |
| + } |
| + |
| + /** |
| + * @override |
| + * @return {!Promise<?string>} |
| + */ |
| + requestContent() { |
| + return this._resources.firstValue().requestContent(); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {string} query |
| + * @param {boolean} caseSensitive |
| + * @param {boolean} isRegex |
| + * @param {function(!Array.<!Common.ContentProvider.SearchMatch>)} callback |
| + */ |
| + searchInContent(query, caseSensitive, isRegex, callback) { |
| + this._resources.firstValue().searchInContent(query, caseSensitive, isRegex, callback); |
| + } |
| +}; |
| + |
| +Bindings.ResourceBinding._symbol = Symbol('Bindings.ResourceBinding._symbol'); |