summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/base/xhr.html
blob: f168ee43b099336e756d1b273c771ebd29dc222e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!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">
<link rel="import" href="/tracing/base/in_memory_trace_stream.html">

<script>
'use strict';

tr.exportTo('tr.b', function() {
  let fs;
  if (tr.isNode) fs = require('fs');

  function guessBinary(url) {
    return /[.]gz$/.test(url) || /[.]zip$/.test(url);
  }
  function xhr(method, url, async, opt_data, forceBinary) {
    const req = new XMLHttpRequest();
    req.overrideMimeType('text/plain; charset=x-user-defined');
    req.open(method, url, async);

    const isBinary = forceBinary;

    if (isBinary === undefined) {
      guessBinary(url);
      if (isBinary && async) req.responseType = 'arraybuffer';
    }

    const data = opt_data !== undefined ? opt_data : null;

    if (!async) {
      req.send(data);
      if (req.status === 200) return req.responseText;
      throw new Error('XHR failed with status ' + req.status +
          ' for url ' + url);
    }

    const p = new Promise(function(resolve, reject) {
      req.onreadystatechange = function(aEvt) {
        if (req.readyState !== 4) return;
        tr.b.timeout(0).then(() => {
          if (req.status !== 200) {
            reject(new Error('XHR failed with status ' + req.status +
                ' for url ' + url));
            return;
          }
          if (req.responseType === 'arraybuffer') {
            resolve(req.response);
            return;
          }
          resolve(req.responseText);
        });
      };
    });
    req.send(data);
    return p;
  }

  function getAsync(url) {
    // Browser.
    if (!tr.isHeadless) return xhr('GET', url, true);

    // Node or vinn prep.
    let filename;
    if (url.startsWith('file:///')) {
      filename = url.substring(7);
    } else {
      filename = global.HTMLImportsLoader.hrefToAbsolutePath(url);
    }
    const isBinary = guessBinary(url);

    // Node.
    if (tr.isNode) {
      const 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, asTraceStream) {
    // Browser.
    if (!tr.isHeadless) return xhr('GET', url, false);

    // Node or vinn prep.
    let 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);
    }
    const isBinary = guessBinary(url);

    // Node.
    if (tr.isNode) {
      const encoding = isBinary ? undefined : 'utf8';
      return fs.readFileSync(filename, encoding);
    }

    // Vinn.
    try {
      if (asTraceStream) {
        return new tr.b.InMemoryTraceStream(
            new Uint8Array(readbuffer(filename)), isBinary);
      } else if (isBinary) {
        return readbuffer(filename);
      }
      return read(filename);
    } catch (ex) {
      if (ex.message) {
        ex.message += ' when reading ' + filename;
        throw ex;
      }
      throw new Error(ex + ' when reading' + 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,
    getSync,
    postAsync,
  };
});
</script>