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, new WebAPIException(result.error));
61 delete callbacks[result.downloadId];
67 function nextDownloadId() {
71 function SetReadOnlyProperty(obj, n, v) {
72 Object.defineProperty(obj, n, { value: v, writable: false });
77 DOWNLOADING: 'DOWNLOADING',
80 COMPLETED: 'COMPLETED',
84 var DownloadNetworkType = {
90 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
91 validator_.isConstructorCall(this, tizen.DownloadRequest);
93 var url_ = converter_.toString(url);
94 var destination_ = destination === undefined ? '' : converter_.toString(destination);
95 var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
99 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
100 networkType_ = 'ALL';
102 networkType_ = networkType;
105 Object.defineProperties(this, {
111 set: function(value) {
112 if (value !== null) {
113 url_ = converter_.toString(value);
122 set: function(value) {
123 if (value !== null) {
124 destination_ = converter_.toString(value);
133 set: function(value) {
134 if (value !== null) {
135 fileName_ = converter_.toString(value);
144 set: function(value) {
145 if (value === null || value in DownloadNetworkType) {
146 networkType_ = value;
153 value: httpHeader === undefined ? {} : httpHeader
158 function DownloadManager() {
159 // constructor of DownloadManager
162 DownloadManager.prototype.start = function() {
163 var args = validator_.validateArgs(arguments, [
165 name: 'downloadRequest',
166 type: types_.PLATFORM_OBJECT,
167 values: tizen.DownloadRequest
170 name: 'downloadCallback',
171 type: types_.LISTENER,
172 values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
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()
187 if (args.downloadCallback) {
188 this.setListener(nativeParam.downloadId, args.downloadCallback);
191 var result = native_.callSync('DownloadManager_start', nativeParam);
193 if (native_.isFailure(result)) {
194 if ('NetworkError' === result.error.name) {
197 throw native_.getErrorObject(result);
200 requests[nativeParam.downloadId] = args.downloadRequest;
202 return nativeParam.downloadId;
205 DownloadManager.prototype.cancel = function() {
206 var args = validator_.validateArgs(arguments, [
207 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
211 downloadId: args.downloadId
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'
220 var result = native_.callSync('DownloadManager_cancel', nativeParam);
222 if (native_.isFailure(result)) {
223 throw native_.getErrorObject(result);
227 DownloadManager.prototype.pause = function() {
228 var args = validator_.validateArgs(arguments, [
229 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
233 downloadId: args.downloadId
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'
242 var result = native_.callSync('DownloadManager_pause', nativeParam);
244 if (native_.isFailure(result)) {
245 throw native_.getErrorObject(result);
249 DownloadManager.prototype.resume = function() {
250 var args = validator_.validateArgs(arguments, [
251 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
255 downloadId: args.downloadId
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'
264 var result = native_.callSync('DownloadManager_resume', nativeParam);
266 if (native_.isFailure(result)) {
267 throw native_.getErrorObject(result);
271 DownloadManager.prototype.getState = function() {
272 var args = validator_.validateArgs(arguments, [
273 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
277 downloadId: args.downloadId
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'
286 var result = native_.callSync('DownloadManager_getState', nativeParam);
288 if (native_.isSuccess(result)) {
289 return native_.getResultObject(result);
291 throw native_.getErrorObject(result);
295 DownloadManager.prototype.getDownloadRequest = function() {
296 var args = validator_.validateArgs(arguments, [
297 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
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'
306 return requests[args.downloadId];
309 DownloadManager.prototype.getMIMEType = function() {
310 var args = validator_.validateArgs(arguments, [
311 { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
315 downloadId: args.downloadId
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'
324 var result = native_.callSync('DownloadManager_getMIMEType', nativeParam);
326 if (native_.isSuccess(result)) {
327 return native_.getResultObject(result);
329 throw native_.getErrorObject(result);
333 DownloadManager.prototype.setListener = function() {
334 var args = validator_.validateArgs(arguments, [
335 { name: 'downloadId', type: types_.LONG },
337 name: 'downloadCallback',
338 type: types_.LISTENER,
339 values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']
343 if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
344 native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
346 callbacks[args.downloadId] = args.downloadCallback;
349 exports = new DownloadManager();