2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
23 var DOWNLOAD_MANAGER_LISTENER_ID = 'DownloadManagerListener';
29 function DownloadManagerChangeCallback(result) {
30 var callback = callbacks[result.downloadId];
33 privUtils_.log('Ignoring unknown callback: ' + result.downloadId);
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);
43 } else if (result.status == 'paused') {
44 if (callback.onpaused) {
45 callback.onpaused(result.downloadId);
47 } else if (result.status == 'canceled') {
48 if (callback.oncanceled) {
49 callback.oncanceled(result.downloadId);
50 delete callbacks[result.downloadId];
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];
58 } else if (result.status == 'error') {
59 if (callback.onfailed) {
60 callback.onfailed(result.downloadId,
61 new WebAPIException(result.error));
62 delete callbacks[result.downloadId];
68 function nextDownloadId() {
72 function SetReadOnlyProperty(obj, n, v) {
73 Object.defineProperty(obj, n, {value: v, writable: false});
78 'DOWNLOADING': 'DOWNLOADING',
80 'CANCELED': 'CANCELED',
81 'COMPLETED': 'COMPLETED',
85 var DownloadNetworkType = {
86 'CELLULAR': 'CELLULAR',
91 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
92 validator_.isConstructorCall(this, tizen.DownloadRequest);
94 var url_ = converter_.toString(url);
95 var destination_ = destination === undefined ? '' : converter_.toString(destination);
96 var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
100 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
101 networkType_ = 'ALL';
103 networkType_ = networkType;
106 Object.defineProperties(this, {
112 set: function(value) {
113 if (value !== null) {
114 url_ = converter_.toString(value);
123 set: function(value) {
124 if (value !== null) {
125 destination_ = converter_.toString(value);
134 set: function(value) {
135 if (value !== null) {
136 fileName_ = converter_.toString(value);
145 set: function(value) {
146 if (value === null || value in DownloadNetworkType) {
147 networkType_ = value;
154 value: httpHeader === undefined ? {} : httpHeader,
160 function DownloadManager() {
161 // constructor of DownloadManager
164 DownloadManager.prototype.start = function() {
165 var args = validator_.validateArgs(arguments, [
166 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
167 {'name' : 'downloadCallback', 'type': types_.LISTENER,
168 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
169 optional: true, nullable: true}
173 'url': args.downloadRequest.url,
174 'destination': args.downloadRequest.destination,
175 'fileName': args.downloadRequest.fileName,
176 'networkType': args.downloadRequest.networkType,
177 'httpHeader': args.downloadRequest.httpHeader,
178 'downloadId': nextDownloadId()
181 if (args.downloadCallback) {
182 this.setListener(nativeParam.downloadId, args.downloadCallback);
185 var result = native_.callSync('DownloadManager_start', nativeParam);
187 if (native_.isFailure(result)) {
188 if ('NetworkError' === result.error.name) {
191 throw native_.getErrorObject(result);
194 requests[nativeParam.downloadId] = args.downloadRequest;
196 return nativeParam.downloadId;
199 DownloadManager.prototype.cancel = function() {
200 var args = validator_.validateArgs(arguments, [
201 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
205 'downloadId': args.downloadId
208 if (typeof requests[args.downloadId] === 'undefined')
209 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
210 'the identifier does not match any download operation in progress');
212 var result = native_.callSync('DownloadManager_cancel', nativeParam);
214 if (native_.isFailure(result)) {
215 throw native_.getErrorObject(result);
219 DownloadManager.prototype.pause = function() {
220 var args = validator_.validateArgs(arguments, [
221 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
225 'downloadId': args.downloadId
228 if (typeof requests[args.downloadId] === 'undefined')
229 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
230 'the identifier does not match any download operation in progress');
232 var result = native_.callSync('DownloadManager_pause', nativeParam);
234 if (native_.isFailure(result)) {
235 throw native_.getErrorObject(result);
239 DownloadManager.prototype.resume = function() {
240 var args = validator_.validateArgs(arguments, [
241 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
245 'downloadId': args.downloadId
248 if (typeof requests[args.downloadId] === 'undefined')
249 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
250 'the identifier does not match any download operation in progress');
252 var result = native_.callSync('DownloadManager_resume', nativeParam);
254 if (native_.isFailure(result)) {
255 throw native_.getErrorObject(result);
259 DownloadManager.prototype.getState = function() {
260 var args = validator_.validateArgs(arguments, [
261 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
265 'downloadId': args.downloadId
268 if (typeof requests[args.downloadId] === 'undefined')
269 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
270 'the identifier does not match any download operation in progress');
272 var result = native_.callSync('DownloadManager_getState', nativeParam);
274 if (native_.isSuccess(result)) {
275 return native_.getResultObject(result);
277 throw native_.getErrorObject(result);
281 DownloadManager.prototype.getDownloadRequest = function() {
282 var args = validator_.validateArgs(arguments, [
283 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
286 if (typeof requests[args.downloadId] === 'undefined')
287 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
288 'the identifier does not match any download operation in progress');
290 return requests[args.downloadId];
293 DownloadManager.prototype.getMIMEType = function() {
294 var args = validator_.validateArgs(arguments, [
295 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
299 'downloadId': args.downloadId
302 if (typeof requests[args.downloadId] === 'undefined')
303 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
304 'the identifier does not match any download operation in progress');
306 var result = native_.callSync('DownloadManager_getMIMEType', nativeParam);
308 if (native_.isSuccess(result)) {
309 return native_.getResultObject(result);
311 throw native_.getErrorObject(result);
315 DownloadManager.prototype.setListener = function() {
316 var args = validator_.validateArgs(arguments, [
317 {'name' : 'downloadId', 'type': types_.LONG},
318 {'name' : 'downloadCallback', 'type': types_.LISTENER,
319 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
322 if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
323 native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
325 callbacks[args.downloadId] = args.downloadCallback;
328 exports = new DownloadManager();