[datacontrol][common] Simplify instance methods registration (2)
[platform/core/api/webapi-plugins.git] / src / download / download_api.js
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 var privUtils_ = xwalk.utils;
18 var validator_ = xwalk.utils.validator;
19 var types_ = validator_.Types;
20 var converter_ = xwalk.utils.converter;
21 var native_ = new xwalk.utils.NativeManager(extension);
22
23 var DOWNLOAD_MANAGER_LISTENER_ID = 'DownloadManagerListener';
24
25 var downloadId = 0;
26 var callbacks = {};
27 var requests = {};
28
29 function DownloadManagerChangeCallback(result) {
30     var callback = callbacks[result.downloadId];
31
32     if (!callback) {
33         privUtils_.log('Ignoring unknown callback: ' + result.downloadId);
34         return;
35     }
36     setTimeout(function() {
37         if (result.status == 'progress') {
38             if (callback.onprogress) {
39                 var receivedSize = result.receivedSize;
40                 var totalSize = result.totalSize;
41                 callback.onprogress(result.downloadId, receivedSize, totalSize);
42             }
43         } else if (result.status == 'paused') {
44             if (callback.onpaused) {
45                 callback.onpaused(result.downloadId);
46             }
47         } else if (result.status == 'canceled') {
48             if (callback.oncanceled) {
49                 callback.oncanceled(result.downloadId);
50             }
51         } else if (result.status == 'completed') {
52             if (callback.oncompleted) {
53                 var fullPath = result.fullPath;
54                 callback.oncompleted(result.downloadId, fullPath);
55                 delete callbacks[result.downloadId];
56             }
57         } else if (result.status == 'error') {
58             if (callback.onfailed) {
59                 callback.onfailed(result.downloadId, new WebAPIException(result.error));
60             }
61         }
62     }, 0);
63 }
64
65 function nextDownloadId() {
66     return ++downloadId;
67 }
68
69 function SetReadOnlyProperty(obj, n, v) {
70     Object.defineProperty(obj, n, { value: v, writable: false });
71 }
72
73 var DownloadState = {
74     QUEUED: 'QUEUED',
75     DOWNLOADING: 'DOWNLOADING',
76     PAUSED: 'PAUSED',
77     CANCELED: 'CANCELED',
78     COMPLETED: 'COMPLETED',
79     FAILED: 'FAILED',
80     ABANDONED: 'ABANDONED'
81 };
82
83 var DownloadNetworkType = {
84     CELLULAR: 'CELLULAR',
85     WIFI: 'WIFI',
86     ALL: 'ALL'
87 };
88
89 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
90     validator_.isConstructorCall(this, tizen.DownloadRequest);
91
92     var url_ = converter_.toString(url);
93     var destination_ = destination === undefined ? '' : converter_.toString(destination);
94     var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
95
96     var networkType_;
97
98     if (networkType === undefined || !(networkType in DownloadNetworkType)) {
99         networkType_ = 'ALL';
100     } else {
101         networkType_ = networkType;
102     }
103
104     Object.defineProperties(this, {
105         url: {
106             enumerable: true,
107             get: function() {
108                 return url_;
109             },
110             set: function(value) {
111                 if (value !== null) {
112                     url_ = converter_.toString(value);
113                 }
114             }
115         },
116         destination: {
117             enumerable: true,
118             get: function() {
119                 return destination_;
120             },
121             set: function(value) {
122                 if (value !== null) {
123                     destination_ = converter_.toString(value);
124                 }
125             }
126         },
127         fileName: {
128             enumerable: true,
129             get: function() {
130                 return fileName_;
131             },
132             set: function(value) {
133                 if (value !== null) {
134                     fileName_ = converter_.toString(value);
135                 }
136             }
137         },
138         networkType: {
139             enumerable: true,
140             get: function() {
141                 return networkType_;
142             },
143             set: function(value) {
144                 if (value === null || value in DownloadNetworkType) {
145                     networkType_ = value;
146                 }
147             }
148         },
149         httpHeader: {
150             writable: true,
151             enumerable: true,
152             value: httpHeader === undefined ? {} : httpHeader
153         }
154     });
155 };
156
157 function DownloadManager() {
158     // constructor of DownloadManager
159 }
160
161 DownloadManager.prototype.start = function() {
162     var args = validator_.validateArgs(arguments, [
163         {
164             name: 'downloadRequest',
165             type: types_.PLATFORM_OBJECT,
166             values: tizen.DownloadRequest
167         },
168         {
169             name: 'downloadCallback',
170             type: types_.LISTENER,
171             values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
172             optional: true,
173             nullable: true
174         }
175     ]);
176
177     var nativeParam = {
178         url: args.downloadRequest.url,
179         destination: args.downloadRequest.destination,
180         fileName: args.downloadRequest.fileName,
181         networkType: args.downloadRequest.networkType,
182         httpHeader: args.downloadRequest.httpHeader,
183         downloadId: nextDownloadId()
184     };
185
186     if (args.downloadCallback) {
187         this.setListener(nativeParam.downloadId, args.downloadCallback);
188     }
189
190     var result = native_.callSync('DownloadManagerStart', nativeParam);
191
192     if (native_.isFailure(result)) {
193         if ('NetworkError' === result.error.name) {
194             return -1;
195         }
196         throw native_.getErrorObject(result);
197     }
198
199     requests[nativeParam.downloadId] = args.downloadRequest;
200
201     return nativeParam.downloadId;
202 };
203
204 DownloadManager.prototype.cancel = function() {
205     var args = validator_.validateArgs(arguments, [
206         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
207     ]);
208
209     var nativeParam = {
210         downloadId: args.downloadId
211     };
212
213     if (typeof requests[args.downloadId] === 'undefined')
214         throw new WebAPIException(
215             WebAPIException.INVALID_VALUES_ERR,
216             'the identifier does not match any download operation in progress'
217         );
218
219     var result = native_.callSync('DownloadManagerCancel', nativeParam);
220
221     if (native_.isFailure(result)) {
222         throw native_.getErrorObject(result);
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(
237             WebAPIException.INVALID_VALUES_ERR,
238             'the identifier does not match any download operation in progress'
239         );
240
241     var result = native_.callSync('DownloadManagerPause', nativeParam);
242
243     if (native_.isFailure(result)) {
244         throw native_.getErrorObject(result);
245     }
246 };
247
248 DownloadManager.prototype.abandon = 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(
259             WebAPIException.INVALID_VALUES_ERR,
260             'the identifier does not match any download operation in progress'
261         );
262
263     var result = native_.callSync('DownloadManagerAbandon', nativeParam);
264
265     if (native_.isFailure(result)) {
266         throw native_.getErrorObject(result);
267     }
268
269     delete callbacks[args.downloadId];
270 };
271
272 DownloadManager.prototype.resume = function() {
273     var args = validator_.validateArgs(arguments, [
274         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
275     ]);
276
277     var nativeParam = {
278         downloadId: args.downloadId
279     };
280
281     if (typeof requests[args.downloadId] === 'undefined')
282         throw new WebAPIException(
283             WebAPIException.INVALID_VALUES_ERR,
284             'the identifier does not match any download operation in progress'
285         );
286
287     var result = native_.callSync('DownloadManagerResume', nativeParam);
288
289     if (native_.isFailure(result)) {
290         throw native_.getErrorObject(result);
291     }
292 };
293
294 DownloadManager.prototype.getState = function() {
295     var args = validator_.validateArgs(arguments, [
296         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
297     ]);
298
299     var nativeParam = {
300         downloadId: args.downloadId
301     };
302
303     if (typeof requests[args.downloadId] === 'undefined')
304         throw new WebAPIException(
305             WebAPIException.INVALID_VALUES_ERR,
306             'the identifier does not match any download operation in progress'
307         );
308
309     var result = native_.callSync('DownloadManagerGetState', nativeParam);
310
311     if (native_.isSuccess(result)) {
312         return native_.getResultObject(result);
313     } else {
314         throw native_.getErrorObject(result);
315     }
316 };
317
318 DownloadManager.prototype.getDownloadRequest = function() {
319     var args = validator_.validateArgs(arguments, [
320         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
321     ]);
322
323     if (typeof requests[args.downloadId] === 'undefined')
324         throw new WebAPIException(
325             WebAPIException.INVALID_VALUES_ERR,
326             'the identifier does not match any download operation in progress'
327         );
328
329     return requests[args.downloadId];
330 };
331
332 DownloadManager.prototype.getMIMEType = function() {
333     var args = validator_.validateArgs(arguments, [
334         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
335     ]);
336
337     var nativeParam = {
338         downloadId: args.downloadId
339     };
340
341     if (typeof requests[args.downloadId] === 'undefined')
342         throw new WebAPIException(
343             WebAPIException.INVALID_VALUES_ERR,
344             'the identifier does not match any download operation in progress'
345         );
346
347     var result = native_.callSync('DownloadManagerGetMimeType', nativeParam);
348
349     if (native_.isSuccess(result)) {
350         return native_.getResultObject(result);
351     } else {
352         throw native_.getErrorObject(result);
353     }
354 };
355
356 DownloadManager.prototype.setListener = function() {
357     var args = validator_.validateArgs(arguments, [
358         { name: 'downloadId', type: types_.LONG },
359         {
360             name: 'downloadCallback',
361             type: types_.LISTENER,
362             values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']
363         }
364     ]);
365
366     if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
367         native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
368     }
369     callbacks[args.downloadId] = args.downloadCallback;
370 };
371
372 exports = new DownloadManager();