Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / network / RequestJSONView.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.RequestView}
34  * @param {!WebInspector.NetworkRequest} request
35  * @param {!WebInspector.ParsedJSON} parsedJSON
36  */
37 WebInspector.RequestJSONView = function(request, parsedJSON)
38 {
39     WebInspector.RequestView.call(this, request);
40     this._parsedJSON = parsedJSON;
41     this.element.classList.add("json");
42 }
43
44 // "false", "true", "null", ",", "{", "}", "[", "]", number, double-quoted string.
45 WebInspector.RequestJSONView._jsonToken = new RegExp('(?:false|true|null|[,\\{\\}\\[\\]]|(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)|(?:\"(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))*\"))', 'g');
46
47 // Escaped unicode char.
48 WebInspector.RequestJSONView._escapedUnicode = new RegExp('\\\\(?:([^u])|u(.{4}))', 'g');
49
50 // Map from escaped char to its literal value.
51 WebInspector.RequestJSONView._standardEscapes = {'"': '"', '/': '/', '\\': '\\', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'};
52
53 /**
54  * @param {string} full
55  * @param {string} standard
56  * @param {string} unicode
57  * @return {string}
58  */
59 WebInspector.RequestJSONView._unescape = function(full, standard, unicode)
60 {
61     return standard ? WebInspector.RequestJSONView._standardEscapes[standard] : String.fromCharCode(parseInt(unicode, 16));
62 }
63
64 /**
65  * @param {string} text
66  * @return {string}
67  */
68 WebInspector.RequestJSONView._unescapeString = function(text)
69 {
70     return text.indexOf("\\") === -1 ? text : text.replace(WebInspector.RequestJSONView._escapedUnicode, WebInspector.RequestJSONView._unescape);
71 }
72
73 /**
74  * @return {*}
75  */
76 WebInspector.RequestJSONView._buildObjectFromJSON = function(text)
77 {
78     var regExp = WebInspector.RequestJSONView._jsonToken;
79     regExp.lastIndex = 0;
80     var result = [];
81     var tip = result;
82     var stack = [];
83     var key = undefined;
84     var token = undefined;
85     var lastToken = undefined;
86     while (true) {
87         var match = regExp.exec(text);
88         if (match === null)
89             break;
90         lastToken = token;
91         token = match[0];
92         var code = token.charCodeAt(0);
93         if ((code === 0x5b) || (code === 0x7b)) { // [ or {
94             var newTip = (code === 0x5b) ? [] : {};
95             tip[key || tip.length] = newTip;
96             stack.push(tip);
97             tip = newTip;
98         } else if ((code === 0x5d) || (code === 0x7d)) { // ] or }
99             tip = stack.pop();
100             if (!tip)
101                 break;
102         } else if (code === 0x2C) { // ,
103             if ((tip instanceof Array) && (lastToken === undefined || lastToken === "[" || lastToken === ","))
104                 tip[tip.length] = undefined;
105         } else if (code === 0x22) { // "
106             token = WebInspector.RequestJSONView._unescapeString(token.substring(1, token.length - 1));
107             if (!key) {
108                 if (tip instanceof Array) {
109                   key = tip.length;
110                 } else {
111                     key = token || "";
112                     continue;
113                 }
114             }
115             tip[key] = token;
116         } else if (code === 0x66) { // f
117             tip[key || tip.length] = false;
118         } else if (code === 0x6e) { // n
119             tip[key || tip.length] = null;
120         } else if (code === 0x74) { // t
121             tip[key || tip.length] = true;
122         } else { // sign or digit
123             tip[key || tip.length] = +(token);
124         }
125         key = undefined;
126     }
127     return (result.length > 1) ? result : result[0];
128 }
129
130 /**
131  * @param {string} text
132  * @return {?WebInspector.ParsedJSON}
133  */
134 WebInspector.RequestJSONView.parseJSON = function(text)
135 {
136     // Trim stubs like "while(1)", "for(;;)", weird numbers, etc. We need JSON start.
137     var inner = WebInspector.RequestJSONView._findBrackets(text, "{", "}");
138     var inner2 = WebInspector.RequestJSONView._findBrackets(text, "[", "]");
139     inner = inner2.length > inner.length ? inner2 : inner;
140     var inner3 = WebInspector.RequestJSONView._findBrackets(text, "(", ")");
141     if (inner3.length - 2 > inner.length) {
142         inner = inner3;
143         ++inner.start;
144         --inner.end;
145     }
146     if (inner.length === -1)
147         return null;
148
149
150     var prefix = text.substring(0, inner.start);
151     var suffix = text.substring(inner.end + 1);
152     text = text.substring(inner.start, inner.end + 1);
153
154     try {
155         return new WebInspector.ParsedJSON(WebInspector.RequestJSONView._buildObjectFromJSON(text), prefix, suffix);
156     } catch (e) {
157         return null;
158     }
159 }
160
161 /**
162  * @param {string} text
163  * @param {string} open
164  * @param {string} close
165  * @return {{start: number, end: number, length: number}}
166  */
167 WebInspector.RequestJSONView._findBrackets = function(text, open, close)
168 {
169     var start = text.indexOf(open);
170     var end = text.lastIndexOf(close);
171     var length = end - start - 1;
172     if (start == -1 || end == -1 || end < start)
173         length = -1;
174     return {start: start, end: end, length: length};
175 }
176
177 WebInspector.RequestJSONView.prototype = {
178     wasShown: function()
179     {
180         this._initialize();
181     },
182
183     _initialize: function()
184     {
185         if (this._initialized)
186             return;
187         this._initialized = true;
188
189         var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.data);
190         var title = this._parsedJSON.prefix + obj.description + this._parsedJSON.suffix;
191         var section = new WebInspector.ObjectPropertiesSection(obj, title);
192         section.expand();
193         section.editable = false;
194         this.element.appendChild(section.element);
195     },
196
197     __proto__: WebInspector.RequestView.prototype
198 }
199
200 /**
201  * @constructor
202  */
203 WebInspector.ParsedJSON = function(data, prefix, suffix)
204 {
205     this.data = data;
206     this.prefix = prefix;
207     this.suffix = suffix;
208 }