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