UploadedFile should support a callback for upload progress
[WebKit-https.git] / Websites / perf.webkit.org / public / v3 / remote.js
1 "use strict";
2
3 class BrowserRemoteAPI extends CommonRemoteAPI {
4
5     sendHttpRequest(path, method, contentType, content, options = {})
6     {
7         console.assert(!path.startsWith('http:') && !path.startsWith('https:') && !path.startsWith('file:'));
8
9         return new Promise((resolve, reject) => {
10             Instrumentation.startMeasuringTime('Remote', 'sendHttpRequest');
11
12             function onload() {
13                 Instrumentation.endMeasuringTime('Remote', 'sendHttpRequest');
14                 if (xhr.status != 200)
15                     return reject(xhr.status);
16                 resolve({statusCode: xhr.status, responseText: xhr.responseText});
17             };
18
19             function onerror() {
20                 Instrumentation.endMeasuringTime('Remote', 'sendHttpRequest');
21                 reject(xhr.status);
22             }
23
24             const xhr = new XMLHttpRequest;
25             xhr.onload = onload;
26             xhr.onabort = onerror;
27             xhr.onerror = onerror;
28
29             if (content && options.uploadProgressCallback) {
30                 xhr.upload.onprogress = (event) => {
31                     options.uploadProgressCallback(event.lengthComputable ? {total: event.total, loaded: event.loaded} : null);
32                 }
33             }
34
35             xhr.open(method, path, true);
36             if (contentType)
37                 xhr.setRequestHeader('Content-Type', contentType);
38             if (content)
39                 xhr.send(content);
40             else
41                 xhr.send();
42         });
43     }
44
45     sendHttpRequestWithFormData(path, formData, options)
46     {
47         return this.sendHttpRequest(path, 'POST', null, formData, options); // Content-type is set by the browser.
48     }
49
50 }
51
52 const RemoteAPI = new BrowserRemoteAPI;