Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: Source/devtools/front_end/network/RequestJSONView.js

Issue 654083006: DevTools: NetworkPanel: use optimistic JSON parser for response preview. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 23 matching lines...) Expand all
34 * @param {!WebInspector.NetworkRequest} request 34 * @param {!WebInspector.NetworkRequest} request
35 * @param {!WebInspector.ParsedJSON} parsedJSON 35 * @param {!WebInspector.ParsedJSON} parsedJSON
36 */ 36 */
37 WebInspector.RequestJSONView = function(request, parsedJSON) 37 WebInspector.RequestJSONView = function(request, parsedJSON)
38 { 38 {
39 WebInspector.RequestView.call(this, request); 39 WebInspector.RequestView.call(this, request);
40 this._parsedJSON = parsedJSON; 40 this._parsedJSON = parsedJSON;
41 this.element.classList.add("json"); 41 this.element.classList.add("json");
42 } 42 }
43 43
44 // "false", "true", "null", ",", "{", "}", "[", "]", number, double-quoted strin g.
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.RequestJ SONView._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
44 /** 130 /**
45 * @param {string} text 131 * @param {string} text
46 * @return {?WebInspector.ParsedJSON} 132 * @return {?WebInspector.ParsedJSON}
47 */ 133 */
48 WebInspector.RequestJSONView.parseJSON = function(text) 134 WebInspector.RequestJSONView.parseJSON = function(text)
49 { 135 {
50 var prefix = ""; 136 // Trim stubs like "while(1)", "for(;;)", weird numbers, etc. We need JSON s tart.
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;
51 148
52 // Trim while(1), for(;;), weird numbers, etc. We need JSON start. 149
53 var start = /[{[]/.exec(text); 150 var prefix = text.substring(0, inner.start);
54 if (start && start.index) { 151 var suffix = text.substring(inner.end + 1);
55 prefix = text.substring(0, start.index); 152 text = text.substring(inner.start, inner.end + 1);
56 text = text.substring(start.index);
57 }
58 153
59 try { 154 try {
60 return new WebInspector.ParsedJSON(JSON.parse(text), prefix, ""); 155 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView._buildOb jectFromJSON(text), prefix, suffix);
61 } catch (e) { 156 } catch (e) {
62 return null; 157 return null;
63 } 158 }
64 } 159 }
65 160
66 /** 161 /**
67 * @param {string} text 162 * @param {string} text
68 * @return {?WebInspector.ParsedJSON} 163 * @param {string} open
164 * @param {string} close
165 * @return {{start: number, end: number, length: number}}
69 */ 166 */
70 WebInspector.RequestJSONView.parseJSONP = function(text) 167 WebInspector.RequestJSONView._findBrackets = function(text, open, close)
71 { 168 {
72 // Taking everything between first and last parentheses 169 var start = text.indexOf(open);
73 var start = text.indexOf("("); 170 var end = text.lastIndexOf(close);
74 var end = text.lastIndexOf(")"); 171 var length = end - start - 1;
75 if (start == -1 || end == -1 || end < start) 172 if (start == -1 || end == -1 || end < start)
76 return null; 173 length = -1;
77 174 return {start: start, end: end, length: length};
78 var prefix = text.substring(0, start + 1);
79 var suffix = text.substring(end);
80 text = text.substring(start + 1, end);
81
82 try {
83 return new WebInspector.ParsedJSON(JSON.parse(text), prefix, suffix);
84 } catch (e) {
85 return null;
86 }
87 } 175 }
88 176
89 WebInspector.RequestJSONView.prototype = { 177 WebInspector.RequestJSONView.prototype = {
90 wasShown: function() 178 wasShown: function()
91 { 179 {
92 this._initialize(); 180 this._initialize();
93 }, 181 },
94 182
95 _initialize: function() 183 _initialize: function()
96 { 184 {
(...skipping 14 matching lines...) Expand all
111 199
112 /** 200 /**
113 * @constructor 201 * @constructor
114 */ 202 */
115 WebInspector.ParsedJSON = function(data, prefix, suffix) 203 WebInspector.ParsedJSON = function(data, prefix, suffix)
116 { 204 {
117 this.data = data; 205 this.data = data;
118 this.prefix = prefix; 206 this.prefix = prefix;
119 this.suffix = suffix; 207 this.suffix = suffix;
120 } 208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698