Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / 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     _htmlView: function()
86     {
87         var dataURL = this.request.asDataURL();
88         if (dataURL !== null)
89             return new WebInspector.RequestHTMLView(this.request, dataURL);
90     },
91
92     _createPreviewView: function()
93     {
94         if (this.request.contentError())
95             return this._createMessageView(WebInspector.UIString("Failed to load response data"));
96
97         var jsonMediaTypeRE = /^application\/[^;]*\+json/;
98         if (this.request.mimeType === "application/json" || jsonMediaTypeRE.test(this.request.mimeType)) {
99             var jsonView = this._jsonView();
100             if (jsonView)
101                 return jsonView;
102         }
103
104         if (this.request.hasErrorStatusCode()) {
105             var htmlView = this._htmlView();
106             if (htmlView)
107                 return htmlView;
108         }
109
110         if (this.request.type === WebInspector.resourceTypes.XHR) {
111             var jsonView = this._jsonView();
112             if (jsonView)
113                 return jsonView;
114         }
115
116         if (this.request.type === WebInspector.resourceTypes.XHR && this.request.mimeType === "text/html") {
117             var htmlView = this._htmlView();
118             if (htmlView)
119                 return htmlView;
120         }
121
122         if (this._responseView.sourceView)
123             return this._responseView.sourceView;
124
125         if (this.request.type === WebInspector.resourceTypes.Other)
126             return this._createEmptyView();
127
128         return WebInspector.RequestView.nonSourceViewForRequest(this.request);
129     },
130
131     __proto__: WebInspector.RequestContentView.prototype
132 }