[Download] Privilege checks moved to JS.
[platform/core/api/webapi-plugins.git] / src / download / download_api.js
1 // Download
2
3 var validator_ = xwalk.utils.validator;
4 var types_ = validator_.Types;
5 var check_ = xwalk.utils.type;
6
7
8 var callbackId = 0;
9 var callbacks = {};
10 var requests = {};
11
12
13 extension.setMessageListener(function(json) {
14
15   var result = JSON.parse(json);
16   var callback = callbacks[result.callbackId];
17   //console.log("PostMessage received: " + result.status);
18
19   if (!callback) {
20     console.logd('Ignoring unknown callback: ' + result.callbackId);
21     return;
22   }
23
24   if (result.status == 'progress') {
25     if (callback.onprogress) {
26       var receivedSize = result.receivedSize;
27       var totalSize = result.totalSize;
28       callback.onprogress(result.callbackId, receivedSize, totalSize);
29     }
30   }
31   else if (result.status == 'paused') {
32     if (callback.onpaused) {
33       callback.onpaused(result.callbackId);
34     }
35   }
36   else if (result.status == 'canceled') {
37     if (callback.oncanceled) {
38       callback.oncanceled(result.callbackId);
39     }
40   }
41   else if (result.status == 'completed') {
42     if (callback.oncompleted) {
43       var fullPath = result.fullPath;
44       callback.oncompleted(result.callbackId, fullPath);
45     }
46   }
47   else if (result.status == 'error') {
48     if (callback.onfailed) {
49       callback.onfailed(result.callbackId,
50               new WebAPIException(result.error));
51     }
52   }
53 });
54
55 function nextCallbackId() {
56   return ++callbackId;
57 }
58
59 function callNative(cmd, args) {
60   var json = {'cmd': cmd, 'args': args};
61   var argjson = JSON.stringify(json);
62   var resultString = extension.internal.sendSyncMessage(argjson);
63   var result = JSON.parse(resultString);
64
65   if (typeof result !== 'object') {
66     throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
67   }
68
69   if (result.status == 'success') {
70     if (result.result) {
71       return result.result;
72     }
73     return true;
74   } else if (result.status == 'error') {
75     var err = result.error;
76     if (err) {
77       throw new WebAPIException(err);
78     }
79     return false;
80   }
81 }
82
83
84 function callNativeWithCallback(cmd, args, callback) {
85   if (callback) {
86     var id = nextCallbackId();
87     args.callbackId = id;
88     callbacks[id] = callback;
89   }
90
91   return callNative(cmd, args);
92 }
93
94 function SetReadOnlyProperty(obj, n, v) {
95   Object.defineProperty(obj, n, {value: v, writable: false});
96 }
97
98 var DownloadState = {
99   'QUEUED': 'QUEUED',
100   'DOWNLOADING': 'DOWNLOADING',
101   'PAUSED': 'PAUSED',
102   'CANCELED': 'CANCELED',
103   'COMPLETED': 'COMPLETED',
104   'FAILED': 'FAILED'
105 };
106
107 var DownloadNetworkType = {
108   'CELLULAR': 'CELLULAR',
109   'WIFI': 'WIFI',
110   'ALL': 'ALL'
111 };
112
113 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
114   validator_.isConstructorCall(this, tizen.DownloadRequest);
115
116   var url_ = url;
117   var networkType_;
118
119   if (networkType === undefined || !(networkType in DownloadNetworkType)) {
120     networkType_ = 'ALL';
121   } else {
122     networkType_ = networkType;
123   }
124
125   Object.defineProperties(this, {
126     'url': {
127       enumerable: true,
128       get: function() {
129         return url_;
130       },
131       set: function(value) {
132         if (value !== null) {
133           url_ = value;
134         }
135       },
136     },
137     'destination': {
138       writable: true,
139       enumerable: true,
140       value: destination === undefined ? '' : destination,
141     },
142     'fileName': {
143       writable: true,
144       enumerable: true,
145       value: fileName === undefined ? '' : fileName,
146     },
147     'networkType': {
148       enumerable: true,
149       get: function() {
150         return networkType_;
151       },
152       set: function(value) {
153         if (value === null || value in DownloadNetworkType) {
154           networkType_ = value;
155         }
156       },
157     },
158     'httpHeader': {
159       writable: true,
160       enumerable: true,
161       value: httpHeader === undefined ? {} : httpHeader,
162     }
163   });
164 };
165
166
167 function DownloadManager() {
168   // constructor of DownloadManager
169 }
170
171 DownloadManager.prototype.start = function() {
172   xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.DOWNLOAD);
173
174   var args = validator_.validateArgs(arguments, [
175     {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
176     {'name' : 'downloadCallback', 'type': types_.LISTENER,
177       'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
178       optional: true, nullable: true}
179   ]);
180
181   var nativeParam = {
182     'url': args.downloadRequest.url,
183     'destination': args.downloadRequest.destination,
184     'fileName': args.downloadRequest.fileName,
185     'networkType': args.downloadRequest.networkType,
186     'httpHeader': args.downloadRequest.httpHeader,
187     'callbackId': nextCallbackId()
188   };
189
190   if (args.downloadCallback) {
191     this.setListener(nativeParam.callbackId, args.downloadCallback);
192   }
193
194   try {
195     callNative('DownloadManager_start', nativeParam);
196   } catch (e) {
197     if ('NetworkError' === e.name) {
198       return -1;
199     }
200     throw e;
201   }
202
203   requests[nativeParam.callbackId] = args.downloadRequest;
204
205   return nativeParam.callbackId;
206 };
207
208 DownloadManager.prototype.cancel = function() {
209   var args = validator_.validateArgs(arguments, [
210     {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
211   ]);
212
213   var nativeParam = {
214     'downloadId': args.downloadId
215   };
216
217   if (typeof requests[args.downloadId] === 'undefined')
218     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
219         'the identifier does not match any download operation in progress');
220
221   try {
222     callNative('DownloadManager_cancel', nativeParam);
223   } catch (e) {
224     throw e;
225   }
226 };
227
228 DownloadManager.prototype.pause = function() {
229   var args = validator_.validateArgs(arguments, [
230     {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
231   ]);
232
233   var nativeParam = {
234     'downloadId': args.downloadId
235   };
236
237   if (typeof requests[args.downloadId] === 'undefined')
238     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
239         'the identifier does not match any download operation in progress');
240
241   try {
242     callNative('DownloadManager_pause', nativeParam);
243   } catch (e) {
244     throw e;
245   }
246 };
247
248 DownloadManager.prototype.resume = function() {
249   var args = validator_.validateArgs(arguments, [
250     {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
251   ]);
252
253   var nativeParam = {
254     'downloadId': args.downloadId
255   };
256
257   if (typeof requests[args.downloadId] === 'undefined')
258     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
259         'the identifier does not match any download operation in progress');
260
261   try {
262     callNative('DownloadManager_resume', nativeParam);
263   } catch (e) {
264     throw e;
265   }
266 };
267
268 DownloadManager.prototype.getState = function() {
269   var args = validator_.validateArgs(arguments, [
270     {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
271   ]);
272
273   var nativeParam = {
274     'downloadId': args.downloadId
275   };
276
277   if (typeof requests[args.downloadId] === 'undefined')
278     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
279         'the identifier does not match any download operation in progress');
280
281   try {
282     return callNative('DownloadManager_getState', nativeParam);
283   } catch (e) {
284     throw e;
285   }
286 };
287
288 DownloadManager.prototype.getDownloadRequest = function() {
289   var args = validator_.validateArgs(arguments, [
290     {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
291   ]);
292
293   if (typeof requests[args.downloadId] === 'undefined')
294     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
295         'the identifier does not match any download operation in progress');
296
297   return requests[args.downloadId];
298 };
299
300 DownloadManager.prototype.getMIMEType = function() {
301   var args = validator_.validateArgs(arguments, [
302     {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
303   ]);
304
305   var nativeParam = {
306     'downloadId': args.downloadId
307   };
308
309   if (typeof requests[args.downloadId] === 'undefined')
310     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
311         'the identifier does not match any download operation in progress');
312
313   try {
314     return callNative('DownloadManager_getMIMEType', nativeParam);
315   } catch (e) {
316     throw e;
317   }
318 };
319
320 DownloadManager.prototype.setListener = function() {
321   var args = validator_.validateArgs(arguments, [
322     {'name' : 'downloadId', 'type': types_.LONG},
323     {'name' : 'downloadCallback', 'type': types_.LISTENER,
324       'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
325   ]);
326
327   callbacks[args.downloadId] = args.downloadCallback;
328 };
329
330
331
332 exports = new DownloadManager();
333