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);
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];
57 } else if (result.status == 'error') {
58 if (callback.onfailed) {
59 callback.onfailed(result.downloadId, new WebAPIException(result.error));
65 function nextDownloadId() {
69 function SetReadOnlyProperty(obj, n, v) {
70 Object.defineProperty(obj, n, { value: v, writable: false });
75 DOWNLOADING: 'DOWNLOADING',
78 COMPLETED: 'COMPLETED',
80 ABANDONED: 'ABANDONED'
83 var DownloadNetworkType = {
89 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
90 validator_.isConstructorCall(this, tizen.DownloadRequest);
92 var url_ = converter_.toString(url);
93 var destination_ = destination === undefined ? '' : converter_.toString(destination);
94 var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
98 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
101 networkType_ = networkType;
104 Object.defineProperties(this, {
110 set: function(value) {
111 if (value !== null) {
112 url_ = converter_.toString(value);
121 set: function(value) {
122 if (value !== null) {
123 destination_ = converter_.toString(value);
132 set: function(value) {
133 if (value !== null) {
134 fileName_ = converter_.toString(value);
143 set: function(value) {
144 if (value === null || value in DownloadNetworkType) {
145 networkType_ = value;
152 value: httpHeader === undefined ? {} : httpHeader
157 function DownloadManager() {
158 // constructor of DownloadManager
161 DownloadManager.prototype.start = function() {
162 var args = validator_.validateArgs(arguments, [
164 name: 'downloadRequest',
165 type: types_.PLATFORM_OBJECT,
166 values: tizen.DownloadRequest
169 name: 'downloadCallback',
170 type: types_.LISTENER,
171 values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
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()
186 if (args.downloadCallback) {
187 this.setListener(nativeParam.downloadId, args.downloadCallback);
190 var result = native_.callSync('DownloadManagerStart', nativeParam);
192 if (native_.isFailure(result)) {
193 if ('NetworkError' === result.error.name) {
196 throw native_.getErrorObject(result);
199 requests[nativeParam.downloadId] = args.downloadRequest;
201 return nativeParam.downloadId;
204 DownloadManager.prototype.cancel = function() {
205 var args = validator_.validateArgs(arguments, [
206 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
210 downloadId: args.downloadId
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'
219 var result = native_.callSync('DownloadManagerCancel', nativeParam);
221 if (native_.isFailure(result)) {
222 throw native_.getErrorObject(result);
226 DownloadManager.prototype.pause = function() {
227 var args = validator_.validateArgs(arguments, [
228 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
232 downloadId: args.downloadId
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'
241 var result = native_.callSync('DownloadManagerPause', nativeParam);
243 if (native_.isFailure(result)) {
244 throw native_.getErrorObject(result);
248 DownloadManager.prototype.abandon = function() {
249 var args = validator_.validateArgs(arguments, [
250 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
254 downloadId: args.downloadId
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'
263 var result = native_.callSync('DownloadManagerAbandon', nativeParam);
265 if (native_.isFailure(result)) {
266 throw native_.getErrorObject(result);
269 delete callbacks[args.downloadId];
272 DownloadManager.prototype.resume = function() {
273 var args = validator_.validateArgs(arguments, [
274 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
278 downloadId: args.downloadId
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'
287 var result = native_.callSync('DownloadManagerResume', nativeParam);
289 if (native_.isFailure(result)) {
290 throw native_.getErrorObject(result);
294 DownloadManager.prototype.getState = function() {
295 var args = validator_.validateArgs(arguments, [
296 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
300 downloadId: args.downloadId
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'
309 var result = native_.callSync('DownloadManagerGetState', nativeParam);
311 if (native_.isSuccess(result)) {
312 return native_.getResultObject(result);
314 throw native_.getErrorObject(result);
318 DownloadManager.prototype.getDownloadRequest = function() {
319 var args = validator_.validateArgs(arguments, [
320 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
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'
329 return requests[args.downloadId];
332 DownloadManager.prototype.getMIMEType = function() {
333 var args = validator_.validateArgs(arguments, [
334 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
338 downloadId: args.downloadId
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'
347 var result = native_.callSync('DownloadManagerGetMimeType', nativeParam);
349 if (native_.isSuccess(result)) {
350 return native_.getResultObject(result);
352 throw native_.getErrorObject(result);
356 DownloadManager.prototype.setListener = function() {
357 var args = validator_.validateArgs(arguments, [
358 { name: 'downloadId', type: types_.LONG },
360 name: 'downloadCallback',
361 type: types_.LISTENER,
362 values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']
366 if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
367 native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
369 callbacks[args.downloadId] = args.downloadCallback;
372 exports = new DownloadManager();