Merge branch 'tizen_3.0' into tizen_4.0
[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                 delete callbacks[result.downloadId];
51             }
52         } else if (result.status == 'completed') {
53             if (callback.oncompleted) {
54                 var fullPath = result.fullPath;
55                 callback.oncompleted(result.downloadId, fullPath);
56                 delete callbacks[result.downloadId];
57             }
58         } else if (result.status == 'error') {
59             if (callback.onfailed) {
60                 callback.onfailed(result.downloadId, new WebAPIException(result.error));
61                 delete callbacks[result.downloadId];
62             }
63         }
64     }, 0);
65 }
66
67 function nextDownloadId() {
68     return ++downloadId;
69 }
70
71 function SetReadOnlyProperty(obj, n, v) {
72     Object.defineProperty(obj, n, { value: v, writable: false });
73 }
74
75 var DownloadState = {
76     QUEUED: 'QUEUED',
77     DOWNLOADING: 'DOWNLOADING',
78     PAUSED: 'PAUSED',
79     CANCELED: 'CANCELED',
80     COMPLETED: 'COMPLETED',
81     FAILED: 'FAILED'
82 };
83
84 var DownloadNetworkType = {
85     CELLULAR: 'CELLULAR',
86     WIFI: 'WIFI',
87     ALL: 'ALL'
88 };
89
90 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
91     validator_.isConstructorCall(this, tizen.DownloadRequest);
92
93     var url_ = converter_.toString(url);
94     var destination_ = destination === undefined ? '' : converter_.toString(destination);
95     var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
96
97     var networkType_;
98
99     if (networkType === undefined || !(networkType in DownloadNetworkType)) {
100         networkType_ = 'ALL';
101     } else {
102         networkType_ = networkType;
103     }
104
105     Object.defineProperties(this, {
106         url: {
107             enumerable: true,
108             get: function() {
109                 return url_;
110             },
111             set: function(value) {
112                 if (value !== null) {
113                     url_ = converter_.toString(value);
114                 }
115             }
116         },
117         destination: {
118             enumerable: true,
119             get: function() {
120                 return destination_;
121             },
122             set: function(value) {
123                 if (value !== null) {
124                     destination_ = converter_.toString(value);
125                 }
126             }
127         },
128         fileName: {
129             enumerable: true,
130             get: function() {
131                 return fileName_;
132             },
133             set: function(value) {
134                 if (value !== null) {
135                     fileName_ = converter_.toString(value);
136                 }
137             }
138         },
139         networkType: {
140             enumerable: true,
141             get: function() {
142                 return networkType_;
143             },
144             set: function(value) {
145                 if (value === null || value in DownloadNetworkType) {
146                     networkType_ = value;
147                 }
148             }
149         },
150         httpHeader: {
151             writable: true,
152             enumerable: true,
153             value: httpHeader === undefined ? {} : httpHeader
154         }
155     });
156 };
157
158 function DownloadManager() {
159     // constructor of DownloadManager
160 }
161
162 DownloadManager.prototype.start = function() {
163     var args = validator_.validateArgs(arguments, [
164         {
165             name: 'downloadRequest',
166             type: types_.PLATFORM_OBJECT,
167             values: tizen.DownloadRequest
168         },
169         {
170             name: 'downloadCallback',
171             type: types_.LISTENER,
172             values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
173             optional: true,
174             nullable: true
175         }
176     ]);
177
178     var nativeParam = {
179         url: args.downloadRequest.url,
180         destination: args.downloadRequest.destination,
181         fileName: args.downloadRequest.fileName,
182         networkType: args.downloadRequest.networkType,
183         httpHeader: args.downloadRequest.httpHeader,
184         downloadId: nextDownloadId()
185     };
186
187     if (args.downloadCallback) {
188         this.setListener(nativeParam.downloadId, args.downloadCallback);
189     }
190
191     var result = native_.callSync('DownloadManager_start', nativeParam);
192
193     if (native_.isFailure(result)) {
194         if ('NetworkError' === result.error.name) {
195             return -1;
196         }
197         throw native_.getErrorObject(result);
198     }
199
200     requests[nativeParam.downloadId] = args.downloadRequest;
201
202     return nativeParam.downloadId;
203 };
204
205 DownloadManager.prototype.cancel = function() {
206     var args = validator_.validateArgs(arguments, [
207         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
208     ]);
209
210     var nativeParam = {
211         downloadId: args.downloadId
212     };
213
214     if (typeof requests[args.downloadId] === 'undefined')
215         throw new WebAPIException(
216             WebAPIException.INVALID_VALUES_ERR,
217             'the identifier does not match any download operation in progress'
218         );
219
220     var result = native_.callSync('DownloadManager_cancel', nativeParam);
221
222     if (native_.isFailure(result)) {
223         throw native_.getErrorObject(result);
224     }
225 };
226
227 DownloadManager.prototype.pause = function() {
228     var args = validator_.validateArgs(arguments, [
229         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
230     ]);
231
232     var nativeParam = {
233         downloadId: args.downloadId
234     };
235
236     if (typeof requests[args.downloadId] === 'undefined')
237         throw new WebAPIException(
238             WebAPIException.INVALID_VALUES_ERR,
239             'the identifier does not match any download operation in progress'
240         );
241
242     var result = native_.callSync('DownloadManager_pause', nativeParam);
243
244     if (native_.isFailure(result)) {
245         throw native_.getErrorObject(result);
246     }
247 };
248
249 DownloadManager.prototype.resume = function() {
250     var args = validator_.validateArgs(arguments, [
251         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
252     ]);
253
254     var nativeParam = {
255         downloadId: args.downloadId
256     };
257
258     if (typeof requests[args.downloadId] === 'undefined')
259         throw new WebAPIException(
260             WebAPIException.INVALID_VALUES_ERR,
261             'the identifier does not match any download operation in progress'
262         );
263
264     var result = native_.callSync('DownloadManager_resume', nativeParam);
265
266     if (native_.isFailure(result)) {
267         throw native_.getErrorObject(result);
268     }
269 };
270
271 DownloadManager.prototype.getState = function() {
272     var args = validator_.validateArgs(arguments, [
273         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
274     ]);
275
276     var nativeParam = {
277         downloadId: args.downloadId
278     };
279
280     if (typeof requests[args.downloadId] === 'undefined')
281         throw new WebAPIException(
282             WebAPIException.INVALID_VALUES_ERR,
283             'the identifier does not match any download operation in progress'
284         );
285
286     var result = native_.callSync('DownloadManager_getState', nativeParam);
287
288     if (native_.isSuccess(result)) {
289         return native_.getResultObject(result);
290     } else {
291         throw native_.getErrorObject(result);
292     }
293 };
294
295 DownloadManager.prototype.getDownloadRequest = function() {
296     var args = validator_.validateArgs(arguments, [
297         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
298     ]);
299
300     if (typeof requests[args.downloadId] === 'undefined')
301         throw new WebAPIException(
302             WebAPIException.INVALID_VALUES_ERR,
303             'the identifier does not match any download operation in progress'
304         );
305
306     return requests[args.downloadId];
307 };
308
309 DownloadManager.prototype.getMIMEType = function() {
310     var args = validator_.validateArgs(arguments, [
311         { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
312     ]);
313
314     var nativeParam = {
315         downloadId: args.downloadId
316     };
317
318     if (typeof requests[args.downloadId] === 'undefined')
319         throw new WebAPIException(
320             WebAPIException.INVALID_VALUES_ERR,
321             'the identifier does not match any download operation in progress'
322         );
323
324     var result = native_.callSync('DownloadManager_getMIMEType', nativeParam);
325
326     if (native_.isSuccess(result)) {
327         return native_.getResultObject(result);
328     } else {
329         throw native_.getErrorObject(result);
330     }
331 };
332
333 DownloadManager.prototype.setListener = function() {
334     var args = validator_.validateArgs(arguments, [
335         { name: 'downloadId', type: types_.LONG },
336         {
337             name: 'downloadCallback',
338             type: types_.LISTENER,
339             values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']
340         }
341     ]);
342
343     if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
344         native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
345     }
346     callbacks[args.downloadId] = args.downloadCallback;
347 };
348
349 exports = new DownloadManager();