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

Side by Side Diff: tracing/tracing/base/xhr.html

Issue 2776653002: [ESLint] Fix violations when enabling curly rule in eslint. (Closed)
Patch Set: Fix test Created 3 years, 9 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 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <!-- 2 <!--
3 Copyright (c) 2013 The Chromium Authors. All rights reserved. 3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be 4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file. 5 found in the LICENSE file.
6 --> 6 -->
7 7
8 <link rel="import" href="/tracing/base/base.html"> 8 <link rel="import" href="/tracing/base/base.html">
9 9
10 <script> 10 <script>
11 'use strict'; 11 'use strict';
12 12
13 tr.exportTo('tr.b', function() { 13 tr.exportTo('tr.b', function() {
14 var fs; 14 var fs;
15 if (tr.isNode) 15 if (tr.isNode) fs = require('fs');
16 fs = require('fs');
17 16
18 function guessBinary(url) { 17 function guessBinary(url) {
19 return /[.]gz$/.test(url) || /[.]zip$/.test(url); 18 return /[.]gz$/.test(url) || /[.]zip$/.test(url);
20 } 19 }
21 function xhr(method, url, async, opt_data, forceBinary) { 20 function xhr(method, url, async, opt_data, forceBinary) {
22 var req = new XMLHttpRequest(); 21 var req = new XMLHttpRequest();
23 req.overrideMimeType('text/plain; charset=x-user-defined'); 22 req.overrideMimeType('text/plain; charset=x-user-defined');
24 req.open(method, url, async); 23 req.open(method, url, async);
25 24
26 var isBinary = forceBinary; 25 var isBinary = forceBinary;
27 26
28 if (isBinary === undefined) { 27 if (isBinary === undefined) {
29 guessBinary(url); 28 guessBinary(url);
30 if (isBinary && async) 29 if (isBinary && async) req.responseType = 'arraybuffer';
31 req.responseType = 'arraybuffer';
32 } 30 }
33 31
34 var data = opt_data !== undefined ? opt_data : null; 32 var data = opt_data !== undefined ? opt_data : null;
35 33
36 if (!async) { 34 if (!async) {
37 req.send(data); 35 req.send(data);
38 if (req.status === 200) 36 if (req.status === 200) return req.responseText;
39 return req.responseText;
40 throw new Error('XHR failed with status ' + req.status + 37 throw new Error('XHR failed with status ' + req.status +
41 ' for url ' + url); 38 ' for url ' + url);
42 } 39 }
43 40
44 var p = new Promise(function(resolve, reject) { 41 var p = new Promise(function(resolve, reject) {
45 req.onreadystatechange = function(aEvt) { 42 req.onreadystatechange = function(aEvt) {
46 if (req.readyState === 4) { 43 if (req.readyState === 4) {
47 window.setTimeout(function() { 44 window.setTimeout(function() {
48 if (req.status === 200) { 45 if (req.status === 200) {
49 if (req.responseType === 'arraybuffer') 46 if (req.responseType === 'arraybuffer') {
50 return resolve(req.response); 47 return resolve(req.response);
48 }
51 return resolve(req.responseText); 49 return resolve(req.responseText);
52 } 50 }
53 reject(new Error('XHR failed with status ' + req.status + 51 reject(new Error('XHR failed with status ' + req.status +
54 ' for url ' + url)); 52 ' for url ' + url));
55 }, 0); 53 }, 0);
56 } 54 }
57 }; 55 };
58 }); 56 });
59 req.send(data); 57 req.send(data);
60 return p; 58 return p;
61 } 59 }
62 60
63 function getAsync(url) { 61 function getAsync(url) {
64 // Browser. 62 // Browser.
65 if (!tr.isHeadless) 63 if (!tr.isHeadless) return xhr('GET', url, true);
66 return xhr('GET', url, true);
67 64
68 // Node or vinn prep. 65 // Node or vinn prep.
69 var filename; 66 var filename;
70 if (url.startsWith('file:///')) 67 if (url.startsWith('file:///')) {
71 filename = url.substring(7); 68 filename = url.substring(7);
72 else 69 } else {
73 filename = global.HTMLImportsLoader.hrefToAbsolutePath(url); 70 filename = global.HTMLImportsLoader.hrefToAbsolutePath(url);
71 }
74 var isBinary = guessBinary(url); 72 var isBinary = guessBinary(url);
75 73
76 // Node. 74 // Node.
77 if (tr.isNode) { 75 if (tr.isNode) {
78 var encoding = isBinary ? undefined : 'utf8'; 76 var encoding = isBinary ? undefined : 'utf8';
79 return new Promise(function(resolve, reject) { 77 return new Promise(function(resolve, reject) {
80 fs.readFile(filename, encoding, function(err, data) { 78 fs.readFile(filename, encoding, function(err, data) {
81 if (err) { 79 if (err) {
82 reject(err); 80 reject(err);
83 return; 81 return;
84 } 82 }
85 resolve(data); 83 resolve(data);
86 }); 84 });
87 }); 85 });
88 } 86 }
89 87
90 // Vinn. 88 // Vinn.
91 return Promise.resolve().then(function() { 89 return Promise.resolve().then(function() {
92 if (isBinary) 90 if (isBinary) return readbuffer(filename);
93 return readbuffer(filename);
94 return read(filename); 91 return read(filename);
95 }); 92 });
96 } 93 }
97 94
98 function getSync(url) { 95 function getSync(url) {
99 // Browser. 96 // Browser.
100 if (!tr.isHeadless) 97 if (!tr.isHeadless) return xhr('GET', url, false);
101 return xhr('GET', url, false);
102 98
103 // Node or vinn prep. 99 // Node or vinn prep.
104 var filename; 100 var filename;
105 if (url.startsWith('file:///')) // posix 101 if (url.startsWith('file:///')) { // posix
106 filename = url.substring(7); 102 filename = url.substring(7);
107 else if (url.startsWith('file://') && url[8] === ':') // win 103 } else if (url.startsWith('file://') && url[8] === ':') { // win
108 filename = url.substring(7); 104 filename = url.substring(7);
109 else 105 } else {
110 filename = global.HTMLImportsLoader.hrefToAbsolutePath(url); 106 filename = global.HTMLImportsLoader.hrefToAbsolutePath(url);
107 }
111 var isBinary = guessBinary(url); 108 var isBinary = guessBinary(url);
112 109
113 // Node. 110 // Node.
114 if (tr.isNode) { 111 if (tr.isNode) {
115 var encoding = isBinary ? undefined : 'utf8'; 112 var encoding = isBinary ? undefined : 'utf8';
116 return fs.readFileSync(filename, encoding); 113 return fs.readFileSync(filename, encoding);
117 } 114 }
118 115
119 // Vinn. 116 // Vinn.
120 try { 117 try {
121 if (isBinary) 118 if (isBinary) return readbuffer(filename);
122 return readbuffer(filename);
123 return read(filename); 119 return read(filename);
124 } catch (ex) { 120 } catch (ex) {
125 if (ex.message) { 121 if (ex.message) {
126 ex.message += ' when reading ' + filename; 122 ex.message += ' when reading ' + filename;
127 throw ex; 123 throw ex;
128 } 124 }
129 throw new Error(ex + ' when reading' + filename); 125 throw new Error(ex + ' when reading' + filename);
130 } 126 }
131 } 127 }
132 128
133 function postAsync(url, data) { 129 function postAsync(url, data) {
134 if (tr.isHeadless) 130 if (tr.isHeadless) {
135 throw new Error('Only supported inside a browser'); 131 throw new Error('Only supported inside a browser');
132 }
136 return xhr('POST', url, true, data); 133 return xhr('POST', url, true, data);
137 } 134 }
138 135
139 function postTextAsync(url, data) { 136 function postTextAsync(url, data) {
140 if (tr.isHeadless) 137 if (tr.isHeadless) {
141 throw new Error('Only supported inside a browser'); 138 throw new Error('Only supported inside a browser');
139 }
142 return xhr('POST', url, true, data, false); 140 return xhr('POST', url, true, data, false);
143 } 141 }
144 142
145 return { 143 return {
146 getAsync, 144 getAsync,
147 getSync, 145 getSync,
148 postAsync, 146 postAsync,
149 }; 147 };
150 }); 148 });
151 </script> 149 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698