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