Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / network / RequestPreviewView.js
1 /*
2  * Copyright (C) 2011 Google Inc. 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.RequestContentView}
34  * @param {!WebInspector.NetworkRequest} request
35  */
36 WebInspector.RequestPreviewView = function(request, responseView)
37 {
38     WebInspector.RequestContentView.call(this, request);
39     this._responseView = responseView;
40 }
41
42 WebInspector.RequestPreviewView.prototype = {
43     contentLoaded: function()
44     {
45         if (!this.request.content && !this.request.contentError()) {
46             if (!this._emptyView) {
47                 this._emptyView = this._createEmptyView();
48                 this._emptyView.show(this.element);
49                 this.innerView = this._emptyView;
50             }
51         } else {
52             if (this._emptyView) {
53                 this._emptyView.detach();
54                 delete this._emptyView;
55             }
56
57             if (!this._previewView)
58                 this._previewView = this._createPreviewView();
59             this._previewView.show(this.element);
60             this.innerView = this._previewView;
61         }
62     },
63
64     _createEmptyView: function()
65     {
66         return this._createMessageView(WebInspector.UIString("This request has no preview available."));
67     },
68
69     /**
70      * @param {string} message
71      * @return {!WebInspector.EmptyView}
72      */
73     _createMessageView: function(message)
74     {
75         return new WebInspector.EmptyView(message);
76     },
77
78     _jsonView: function()
79     {
80         var parsedJSON = WebInspector.RequestJSONView.parseJSON(this.request.content);
81         if (parsedJSON)
82             return new WebInspector.RequestJSONView(this.request, parsedJSON);
83     },
84
85     /**
86      * @return {?WebInspector.RequestHTMLView}
87      */
88     _htmlErrorPreview: function()
89     {
90         var whitelist = ["text/html", "text/plain", "application/xhtml+xml"];
91         if (whitelist.indexOf(this.request.mimeType) === -1)
92             return null;
93
94         var dataURL = this.request.asDataURL();
95         if (dataURL === null)
96             return null;
97
98         return new WebInspector.RequestHTMLView(this.request, dataURL);
99     },
100
101     _createPreviewView: function()
102     {
103         if (this.request.contentError())
104             return this._createMessageView(WebInspector.UIString("Failed to load response data"));
105
106         var mimeType = this.request.mimeType || "";
107         if (mimeType === "application/json" || mimeType.endsWith("+json")) {
108             var jsonView = this._jsonView();
109             if (jsonView)
110                 return jsonView;
111         }
112
113         if (this.request.hasErrorStatusCode()) {
114             var htmlErrorPreview = this._htmlErrorPreview();
115             if (htmlErrorPreview)
116                 return htmlErrorPreview;
117         }
118
119         if (this.request.type === WebInspector.resourceTypes.XHR) {
120             var jsonView = this._jsonView();
121             if (jsonView)
122                 return jsonView;
123             var htmlErrorPreview = this._htmlErrorPreview();
124             if (htmlErrorPreview)
125                 return htmlErrorPreview;
126         }
127
128         if (this._responseView.sourceView)
129             return this._responseView.sourceView;
130
131         if (this.request.type === WebInspector.resourceTypes.Other)
132             return this._createEmptyView();
133
134         return WebInspector.RequestView.nonSourceViewForRequest(this.request);
135     },
136
137     __proto__: WebInspector.RequestContentView.prototype
138 }