Web Inspector: implement blackboxing of script resources
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / SVGImageResourceClusterContentView.js
1 /*
2  * Copyright (C) 2017 Devin Rousso <webkit@devinrousso.com>. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.SVGImageResourceClusterContentView = class SVGImageResourceClusterContentView extends WI.ClusterContentView
27 {
28     constructor(resource)
29     {
30         super(resource);
31
32         this._resource = resource;
33
34         let createPathComponent = (displayName, className, identifier) => {
35             const textOnly = false;
36             const showSelectorArrows = true;
37             let pathComponent = new WI.HierarchicalPathComponent(displayName, className, identifier, textOnly, showSelectorArrows);
38             pathComponent.addEventListener(WI.HierarchicalPathComponent.Event.SiblingWasSelected, this._pathComponentSelected, this);
39             pathComponent.comparisonData = resource;
40             return pathComponent;
41         };
42
43         this._imagePathComponent = createPathComponent(WI.UIString("Image"), "image-icon", WI.SVGImageResourceClusterContentView.Identifier.Image);
44         this._sourcePathComponent = createPathComponent(WI.UIString("Source"), "source-icon", WI.SVGImageResourceClusterContentView.Identifier.Source);
45
46         this._imagePathComponent.nextSibling = this._sourcePathComponent;
47         this._sourcePathComponent.previousSibling = this._imagePathComponent;
48
49         this._currentContentViewSetting = new WI.Setting("svg-image-resource-cluster-current-view-" + this._resource.url.hash, WI.SVGImageResourceClusterContentView.Identifier.Image);
50     }
51
52     // Public
53
54     get resource() { return this._resource; }
55
56     get selectionPathComponents()
57     {
58         let currentContentView = this._contentViewContainer.currentContentView;
59         if (!currentContentView)
60             return [];
61
62         // Append the current view's path components to the path component representing the current view.
63         let components = [this._pathComponentForContentView(currentContentView)];
64         return components.concat(currentContentView.selectionPathComponents);
65     }
66
67     // Protected
68
69     shown()
70     {
71         super.shown();
72
73         if (this._shownInitialContent)
74             return;
75
76         this._showContentViewForIdentifier(this._currentContentViewSetting.value);
77     }
78
79     closed()
80     {
81         super.closed();
82
83         this._shownInitialContent = false;
84     }
85
86     saveToCookie(cookie)
87     {
88         cookie[WI.SVGImageResourceClusterContentView.ContentViewIdentifierCookieKey] = this._currentContentViewSetting.value;
89     }
90
91     restoreFromCookie(cookie)
92     {
93         let contentView = this._showContentViewForIdentifier(cookie[WI.SVGImageResourceClusterContentView.ContentViewIdentifierCookieKey]);
94         if (typeof contentView.revealPosition === "function" && "lineNumber" in cookie && "columnNumber" in cookie)
95             contentView.revealPosition(new WI.SourceCodePosition(cookie.lineNumber, cookie.columnNumber));
96     }
97
98     // Private
99
100     _pathComponentForContentView(contentView)
101     {
102         console.assert(contentView);
103         if (!contentView)
104             return null;
105         if (contentView instanceof WI.ImageResourceContentView)
106             return this._imagePathComponent;
107         if (contentView instanceof WI.TextContentView)
108             return this._sourcePathComponent;
109         console.error("Unknown contentView.");
110         return null;
111     }
112
113     _identifierForContentView(contentView)
114     {
115         console.assert(contentView);
116         if (!contentView)
117             return null;
118         if (contentView instanceof WI.ImageResourceContentView)
119             return WI.SVGImageResourceClusterContentView.Identifier.Image;
120         if (contentView instanceof WI.TextContentView)
121             return WI.SVGImageResourceClusterContentView.Identifier.Source;
122         console.error("Unknown contentView.");
123         return null;
124     }
125
126     _showContentViewForIdentifier(identifier)
127     {
128         let contentViewToShow = null;
129
130         switch (identifier) {
131         case WI.SVGImageResourceClusterContentView.Identifier.Image:
132             contentViewToShow = new WI.ImageResourceContentView(this._resource);
133             break;
134
135         case WI.SVGImageResourceClusterContentView.Identifier.Source:
136             contentViewToShow = new WI.TextContentView("", this._resource.mimeType);
137
138             this._resource.requestContent().then((result) => {
139                 if (typeof result.content === "string") {
140                     contentViewToShow.textEditor.string = result.content;
141                     return;
142                 }
143
144                 blobAsText(result.content, (text) => {
145                     contentViewToShow.textEditor.string = text;
146                 });
147             });
148             break;
149
150         default:
151             // Default to showing the image.
152             contentViewToShow = new WI.ImageResourceContentView(this._resource);
153             break;
154         }
155
156         this._currentContentViewSetting.value = this._identifierForContentView(contentViewToShow);
157
158         return this.contentViewContainer.showContentView(contentViewToShow);
159     }
160
161     _pathComponentSelected(event)
162     {
163         this._showContentViewForIdentifier(event.data.pathComponent.representedObject);
164     }
165 };
166
167 WI.SVGImageResourceClusterContentView.ContentViewIdentifierCookieKey = "svg-image-resource-cluster-content-view-identifier";
168
169 WI.SVGImageResourceClusterContentView.Identifier = {
170     Image: "image",
171     Source: "source",
172 };