blob: 71d753f7e45666ba89a3ee6ac95ddcb9a42e3e3f [file] [log] [blame]
<!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() {
function guessBinary(url) {
return /[.]gz$/.test(url) || /[.]zip$/.test(url);
}
function get(url, async) {
var req = new XMLHttpRequest();
req.overrideMimeType('text/plain; charset=x-user-defined');
req.open('GET', url, async);
var isBinary = guessBinary(url);
if (isBinary && async)
req.responseType = 'arraybuffer';
if (!async) {
req.send(null);
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();
return p;
}
function getAsync(url) {
if (!tr.isHeadless)
return get(url, true);
var filename;
if (url.startsWith('file:///'))
filename = url.substring(7);
else
filename = global.hrefToAbsolutePath(url);
var isBinary = guessBinary(url);
return Promise.resolve().then(function() {
if (isBinary)
return readbuffer(filename);
return read(filename);
});
}
function getSync(url) {
if (!tr.isHeadless)
return get(url, false);
var filename;
if (url.startsWith('file:///'))
filename = url.substring(7);
else
filename = global.hrefToAbsolutePath(url);
var isBinary = guessBinary(url);
if (isBinary)
return readbuffer(filename);
return read(filename);
}
return {
getAsync: getAsync,
getSync: getSync
};
});
</script>