[Download] Added checks if requested network is available.
[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   var args = validator_.validateArgs(arguments, [
173     {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
174     {'name' : 'downloadCallback', 'type': types_.LISTENER,
175       'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
176       optional: true, nullable: true}
177   ]);
178
179   var nativeParam = {
180     'url': args.downloadRequest.url,
181     'destination': args.downloadRequest.destination,
182     'fileName': args.downloadRequest.fileName,
183     'networkType': args.downloadRequest.networkType,
184     'httpHeader': args.downloadRequest.httpHeader,
185     'callbackId': nextCallbackId()
186   };
187
188   if (args.downloadCallback) {
189     this.setListener(nativeParam.callbackId, args.downloadCallback);
190   }
191
192   try {
193     callNative('DownloadManager_start', nativeParam);
194   } catch (e) {
195     if ('NetworkError' === e.name) {
196       return -1;
197     }
198     throw e;
199   }
200
201   requests[nativeParam.callbackId] = args.downloadRequest;
202
203   return nativeParam.callbackId;
204 };
205
206 DownloadManager.prototype.cancel = function() {
207   var args = validator_.validateArgs(arguments, [
208     {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
209   ]);
210
211   var nativeParam = {
212     'downloadId': args.downloadId
213   };
214
215   if (typeof requests[args.downloadId] === 'undefined')
216     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
217         'the identifier does not match any download operation in progress');
218
219   try {
220     callNative('DownloadManager_cancel', nativeParam);
221   } catch (e) {
222     throw e;
223   }
224 };
225
226 DownloadManager.prototype.pause = function() {
227   var args = validator_.validateArgs(arguments, [
228     {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
229   ]);
230
231   var nativeParam = {
232     'downloadId': args.downloadId
233   };
234
235   if (typeof requests[args.downloadId] === 'undefined')
236     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
237         'the identifier does not match any download operation in progress');
238
239   try {
240     callNative('DownloadManager_pause', nativeParam);
241   } catch (e) {
242     throw e;
243   }
244 };
245
246 DownloadManager.prototype.resume = function() {
247   var args = validator_.validateArgs(arguments, [
248     {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
249   ]);
250
251   var nativeParam = {
252     'downloadId': args.downloadId
253   };
254
255   if (typeof requests[args.downloadId] === 'undefined')
256     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
257         'the identifier does not match any download operation in progress');
258
259   try {
260     callNative('DownloadManager_resume', nativeParam);
261   } catch (e) {
262     throw e;
263   }
264 };
265
266 DownloadManager.prototype.getState = function() {
267   var args = validator_.validateArgs(arguments, [
268     {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
269   ]);
270
271   var nativeParam = {
272     'downloadId': args.downloadId
273   };
274
275   if (typeof requests[args.downloadId] === 'undefined')
276     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
277         'the identifier does not match any download operation in progress');
278
279   try {
280     return callNative('DownloadManager_getState', nativeParam);
281   } catch (e) {
282     throw e;
283   }
284 };
285
286 DownloadManager.prototype.getDownloadRequest = function() {
287   var args = validator_.validateArgs(arguments, [
288     {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
289   ]);
290
291   if (typeof requests[args.downloadId] === 'undefined')
292     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
293         'the identifier does not match any download operation in progress');
294
295   return requests[args.downloadId];
296 };
297
298 DownloadManager.prototype.getMIMEType = function() {
299   var args = validator_.validateArgs(arguments, [
300     {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
301   ]);
302
303   var nativeParam = {
304     'downloadId': args.downloadId
305   };
306
307   if (typeof requests[args.downloadId] === 'undefined')
308     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
309         'the identifier does not match any download operation in progress');
310
311   try {
312     return callNative('DownloadManager_getMIMEType', nativeParam);
313   } catch (e) {
314     throw e;
315   }
316 };
317
318 DownloadManager.prototype.setListener = function() {
319   var args = validator_.validateArgs(arguments, [
320     {'name' : 'downloadId', 'type': types_.LONG},
321     {'name' : 'downloadCallback', 'type': types_.LISTENER,
322       'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
323   ]);
324
325   callbacks[args.downloadId] = args.downloadCallback;
326 };
327
328
329
330 exports = new DownloadManager();
331