| <!DOCTYPE html> |
| <!-- |
| Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| Use of this source code is governed by a BSD-style license that can be |
| found in the LICENSE file. |
| --> |
| |
| <link rel="import" href="/tracing/base/base.html"> |
| |
| <script> |
| 'use strict'; |
| |
| tr.exportTo('tr.b', function() { |
| var fs; |
| if (tr.isNode) |
| fs = require('fs'); |
| |
| function guessBinary(url) { |
| return /[.]gz$/.test(url) || /[.]zip$/.test(url); |
| } |
| function xhr(method, url, async, opt_data, force_binary) { |
| var req = new XMLHttpRequest(); |
| req.overrideMimeType('text/plain; charset=x-user-defined'); |
| req.open(method, url, async); |
| |
| var isBinary = force_binary; |
| |
| if (isBinary === undefined) { |
| guessBinary(url); |
| if (isBinary && async) |
| req.responseType = 'arraybuffer'; |
| } |
| |
| var data = opt_data !== undefined ? opt_data : null; |
| |
| if (!async) { |
| req.send(data); |
| if (req.status == 200) { |
| return req.responseText; |
| } else { |
| throw new Error('XHR failed with status ' + req.status); |
| } |
| } |
| |
| var p = new Promise(function(resolve, reject) { |
| req.onreadystatechange = function(aEvt) { |
| if (req.readyState == 4) { |
| window.setTimeout(function() { |
| if (req.status == 200) { |
| if (req.responseType === 'arraybuffer') |
| return resolve(req.response); |
| return resolve(req.responseText); |
| } else { |
| reject(new Error('XHR failed with status ' + req.status)); |
| } |
| }, 0); |
| } |
| }; |
| }); |
| req.send(data); |
| return p; |
| } |
| |
| function getAsync(url) { |
| // Browser. |
| if (!tr.isHeadless) |
| return xhr('GET', url, true); |
| |
| // Node or vinn prep. |
| var filename; |
| if (url.startsWith('file:///')) |
| filename = url.substring(7); |
| else |
| filename = global.HTMLImportsLoader.hrefToAbsolutePath(url); |
| var isBinary = guessBinary(url); |
| |
| // Node. |
| if (tr.isNode) { |
| var encoding = isBinary ? undefined : 'utf8'; |
| return new Promise(function(resolve, reject) { |
| fs.readFile(filename, encoding, function(err, data) { |
| if (err) { |
| reject(err); |
| return; |
| } |
| resolve(data); |
| }); |
| }); |
| } |
| |
| // Vinn. |
| return Promise.resolve().then(function() { |
| if (isBinary) |
| return readbuffer(filename); |
| return read(filename); |
| }); |
| } |
| |
| function getSync(url) { |
| // Browser. |
| if (!tr.isHeadless) |
| return xhr('GET', url, false); |
| |
| // Node or vinn prep. |
| var filename; |
| if (url.startsWith('file:///')) // posix |
| filename = url.substring(7); |
| else if (url.startsWith('file://') && url[8] === ':') // win |
| filename = url.substring(7); |
| else |
| filename = global.HTMLImportsLoader.hrefToAbsolutePath(url); |
| var isBinary = guessBinary(url); |
| |
| // Node. |
| if (tr.isNode) { |
| var encoding = isBinary ? undefined : 'utf8'; |
| return fs.readFileSync(filename, encoding); |
| } |
| |
| // Vinn. |
| if (isBinary) |
| return readbuffer(filename); |
| return read(filename); |
| } |
| |
| function postAsync(url, data) { |
| if (tr.isHeadless) |
| throw new Error('Only supported inside a browser'); |
| return xhr('POST', url, true, data); |
| } |
| |
| function postTextAsync(url, data) { |
| if (tr.isHeadless) |
| throw new Error('Only supported inside a browser'); |
| return xhr('POST', url, true, data, false); |
| } |
| |
| return { |
| getAsync: getAsync, |
| getSync: getSync, |
| postAsync: postAsync |
| }; |
| }); |
| </script> |