/* * Copyright (C) 2013, 2015 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ WI.ResourceClusterContentView = class ResourceClusterContentView extends WI.ClusterContentView { constructor(resource) { super(resource); this._resource = resource; this._resource.addEventListener(WI.Resource.Event.TypeDidChange, this._resourceTypeDidChange, this); this._resource.addEventListener(WI.Resource.Event.LoadingDidFinish, this._resourceLoadingDidFinish, this); function createPathComponent(displayName, className, identifier) { let pathComponent = new WI.HierarchicalPathComponent(displayName, className, identifier, false, true); pathComponent.addEventListener(WI.HierarchicalPathComponent.Event.SiblingWasSelected, this._pathComponentSelected, this); pathComponent.comparisonData = resource; return pathComponent; } this._requestPathComponent = createPathComponent.call(this, WI.UIString("Request"), WI.ResourceClusterContentView.RequestIconStyleClassName, WI.ResourceClusterContentView.RequestIdentifier); this._responsePathComponent = createPathComponent.call(this, WI.UIString("Response"), WI.ResourceClusterContentView.ResponseIconStyleClassName, WI.ResourceClusterContentView.ResponseIdentifier); this._requestPathComponent.nextSibling = this._responsePathComponent; this._responsePathComponent.previousSibling = this._requestPathComponent; this._currentContentViewSetting = new WI.Setting("resource-current-view-" + this._resource.url.hash, WI.ResourceClusterContentView.ResponseIdentifier); } // Public get resource() { return this._resource; } get responseContentView() { if (this._responseContentView) return this._responseContentView; switch (this._resource.type) { case WI.Resource.Type.Document: case WI.Resource.Type.Script: case WI.Resource.Type.Stylesheet: this._responseContentView = new WI.TextResourceContentView(this._resource); break; case WI.Resource.Type.XHR: case WI.Resource.Type.Fetch: case WI.Resource.Type.Ping: case WI.Resource.Type.Beacon: // FIXME: Web Inspector: XHR / Fetch for non-text content should not show garbled text // The response content for these requests may not always be text. this._responseContentView = new WI.TextResourceContentView(this._resource); break; case WI.Resource.Type.Image: if (this._resource.mimeTypeComponents.type === "image/svg+xml") this._responseContentView = new WI.SVGImageResourceClusterContentView(this._resource); else this._responseContentView = new WI.ImageResourceContentView(this._resource); break; case WI.Resource.Type.Font: this._responseContentView = new WI.FontResourceContentView(this._resource); break; case WI.Resource.Type.WebSocket: this._responseContentView = new WI.WebSocketContentView(this._resource); break; default: this._responseContentView = new WI.GenericResourceContentView(this._resource); break; } return this._responseContentView; } get requestContentView() { if (!this._canShowRequestContentView()) return null; if (this._requestContentView) return this._requestContentView; this._requestContentView = new WI.TextContentView(this._resource.requestData || "", this._resource.requestDataContentType); return this._requestContentView; } get selectionPathComponents() { var currentContentView = this._contentViewContainer.currentContentView; if (!currentContentView) return []; if (!this._canShowRequestContentView()) return currentContentView.selectionPathComponents; // Append the current view's path components to the path component representing the current view. var components = [this._pathComponentForContentView(currentContentView)]; return components.concat(currentContentView.selectionPathComponents); } shown() { super.shown(); if (this._shownInitialContent) return; this._showContentViewForIdentifier(this._currentContentViewSetting.value); } closed() { super.closed(); this._shownInitialContent = false; } saveToCookie(cookie) { cookie[WI.ResourceClusterContentView.ContentViewIdentifierCookieKey] = this._currentContentViewSetting.value; } restoreFromCookie(cookie) { var contentView = this._showContentViewForIdentifier(cookie[WI.ResourceClusterContentView.ContentViewIdentifierCookieKey]); if (typeof contentView.revealPosition === "function" && "lineNumber" in cookie && "columnNumber" in cookie) contentView.revealPosition(new WI.SourceCodePosition(cookie.lineNumber, cookie.columnNumber)); } showRequest() { this._shownInitialContent = true; return this._showContentViewForIdentifier(WI.ResourceClusterContentView.RequestIdentifier); } showResponse(positionToReveal, textRangeToSelect, forceUnformatted) { this._shownInitialContent = true; if (!this._resource.finished) { this._positionToReveal = positionToReveal; this._textRangeToSelect = textRangeToSelect; this._forceUnformatted = forceUnformatted; } var responseContentView = this._showContentViewForIdentifier(WI.ResourceClusterContentView.ResponseIdentifier); if (typeof responseContentView.revealPosition === "function") responseContentView.revealPosition(positionToReveal, textRangeToSelect, forceUnformatted); return responseContentView; } // Private _canShowRequestContentView() { var requestData = this._resource.requestData; if (!requestData) return false; var requestDataContentType = this._resource.requestDataContentType; if (requestDataContentType && requestDataContentType.match(/^application\/x-www-form-urlencoded\s*(;.*)?$/i)) return false; return true; } _pathComponentForContentView(contentView) { console.assert(contentView); if (!contentView) return null; if (contentView === this._requestContentView) return this._requestPathComponent; if (contentView === this._responseContentView) return this._responsePathComponent; console.error("Unknown contentView."); return null; } _identifierForContentView(contentView) { console.assert(contentView); if (!contentView) return null; if (contentView === this._requestContentView) return WI.ResourceClusterContentView.RequestIdentifier; if (contentView === this._responseContentView) return WI.ResourceClusterContentView.ResponseIdentifier; console.error("Unknown contentView."); return null; } _showContentViewForIdentifier(identifier) { var contentViewToShow = null; switch (identifier) { case WI.ResourceClusterContentView.RequestIdentifier: contentViewToShow = this._canShowRequestContentView() ? this.requestContentView : null; break; case WI.ResourceClusterContentView.ResponseIdentifier: contentViewToShow = this.responseContentView; break; } if (!contentViewToShow) contentViewToShow = this.responseContentView; console.assert(contentViewToShow); this._currentContentViewSetting.value = this._identifierForContentView(contentViewToShow); return this.contentViewContainer.showContentView(contentViewToShow); } _pathComponentSelected(event) { this._showContentViewForIdentifier(event.data.pathComponent.representedObject); } _resourceTypeDidChange(event) { // Since resource views are based on the type, we need to make a new content view and tell the container to replace this // content view with the new one. Make a new ResourceContentView which will use the new resource type to make the correct // concrete ResourceContentView subclass. var currentResponseContentView = this._responseContentView; if (!currentResponseContentView) return; this._responseContentView = null; this.contentViewContainer.replaceContentView(currentResponseContentView, this.responseContentView); } _resourceLoadingDidFinish(event) { if ("_positionToReveal" in this) { if (this._contentViewContainer.currentContentView === this._responseContentView) this._responseContentView.revealPosition(this._positionToReveal, this._textRangeToSelect, this._forceUnformatted); delete this._positionToReveal; delete this._textRangeToSelect; delete this._forceUnformatted; } } }; WI.ResourceClusterContentView.ContentViewIdentifierCookieKey = "resource-cluster-content-view-identifier"; WI.ResourceClusterContentView.RequestIconStyleClassName = "request-icon"; WI.ResourceClusterContentView.ResponseIconStyleClassName = "response-icon"; WI.ResourceClusterContentView.RequestIdentifier = "request"; WI.ResourceClusterContentView.ResponseIdentifier = "response";