UploadedFile should support a callback for upload progress
[WebKit-https.git] / Websites / perf.webkit.org / public / v3 / privileged-api.js
1 "use strict";
2
3 class PrivilegedAPI {
4
5     static sendRequest(path, data, options = {useFormData: false})
6     {
7         const clonedData = {};
8         for (let key in data)
9             clonedData[key] = data[key];
10
11         const fullPath = '/privileged-api/' + path;
12         const post = options.useFormData
13             ? () => RemoteAPI.postFormDataWithStatus(fullPath, clonedData, options)
14             : () => RemoteAPI.postJSONWithStatus(fullPath, clonedData, options);
15
16         return this.requestCSRFToken().then((token) => {
17             clonedData['token'] = token;
18             return post().catch((status) => {
19                 if (status != 'InvalidToken')
20                     return Promise.reject(status);
21                 this._token = null;
22                 return this.requestCSRFToken().then((token) => {
23                     clonedData['token'] = token;
24                     return post();
25                 });
26             });
27         });
28     }
29
30     static requestCSRFToken()
31     {
32         const maxNetworkLatency = 3 * 60 * 1000; /* 3 minutes */
33         if (this._token && this._expiration > Date.now() + maxNetworkLatency)
34             return Promise.resolve(this._token);
35
36         return RemoteAPI.postJSONWithStatus('/privileged-api/generate-csrf-token').then((result) => {
37             this._token = result['token'];
38             this._expiration = new Date(result['expiration']);
39             return this._token;
40         });
41     }
42
43 }
44
45 PrivilegedAPI._token = null;
46 PrivilegedAPI._expiration = null;
47
48 if (typeof module != 'undefined')
49     module.exports.PrivilegedAPI = PrivilegedAPI;