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 validator_ = xwalk.utils.validator;
18 var types_ = validator_.Types;
19 var check_ = xwalk.utils.type;
27 extension.setMessageListener(function(json) {
29 var result = JSON.parse(json);
30 var callback = callbacks[result.callbackId];
31 //console.log("PostMessage received: " + result.status);
34 console.logd('Ignoring unknown callback: ' + result.callbackId);
37 setTimeout(function() {
38 if (result.status == 'progress') {
39 if (callback.onprogress) {
40 var receivedSize = result.receivedSize;
41 var totalSize = result.totalSize;
42 callback.onprogress(result.callbackId, receivedSize, totalSize);
44 } else if (result.status == 'paused') {
45 if (callback.onpaused) {
46 callback.onpaused(result.callbackId);
48 } else if (result.status == 'canceled') {
49 if (callback.oncanceled) {
50 callback.oncanceled(result.callbackId);
52 } else if (result.status == 'completed') {
53 if (callback.oncompleted) {
54 var fullPath = result.fullPath;
55 callback.oncompleted(result.callbackId, fullPath);
57 } else if (result.status == 'error') {
58 if (callback.onfailed) {
59 callback.onfailed(result.callbackId,
60 new WebAPIException(result.error));
66 function nextCallbackId() {
70 function callNative(cmd, args) {
71 var json = {'cmd': cmd, 'args': args};
72 var argjson = JSON.stringify(json);
73 var resultString = extension.internal.sendSyncMessage(argjson);
74 var result = JSON.parse(resultString);
76 if (typeof result !== 'object') {
77 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
80 if (result.status == 'success') {
85 } else if (result.status == 'error') {
86 var err = result.error;
88 throw new WebAPIException(err);
95 function callNativeWithCallback(cmd, args, callback) {
97 var id = nextCallbackId();
99 callbacks[id] = callback;
102 return callNative(cmd, args);
105 function SetReadOnlyProperty(obj, n, v) {
106 Object.defineProperty(obj, n, {value: v, writable: false});
109 var DownloadState = {
111 'DOWNLOADING': 'DOWNLOADING',
113 'CANCELED': 'CANCELED',
114 'COMPLETED': 'COMPLETED',
118 var DownloadNetworkType = {
119 'CELLULAR': 'CELLULAR',
124 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
125 validator_.isConstructorCall(this, tizen.DownloadRequest);
130 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
131 networkType_ = 'ALL';
133 networkType_ = networkType;
136 Object.defineProperties(this, {
142 set: function(value) {
143 if (value !== null) {
151 value: destination === undefined ? '' : destination,
156 value: fileName === undefined ? '' : fileName,
163 set: function(value) {
164 if (value === null || value in DownloadNetworkType) {
165 networkType_ = value;
172 value: httpHeader === undefined ? {} : httpHeader,
178 function DownloadManager() {
179 // constructor of DownloadManager
182 DownloadManager.prototype.start = function() {
183 xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.DOWNLOAD);
185 var args = validator_.validateArgs(arguments, [
186 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
187 {'name' : 'downloadCallback', 'type': types_.LISTENER,
188 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
189 optional: true, nullable: true}
193 'url': args.downloadRequest.url,
194 'destination': args.downloadRequest.destination,
195 'fileName': args.downloadRequest.fileName,
196 'networkType': args.downloadRequest.networkType,
197 'httpHeader': args.downloadRequest.httpHeader,
198 'callbackId': nextCallbackId()
201 if (args.downloadCallback) {
202 this.setListener(nativeParam.callbackId, args.downloadCallback);
206 callNative('DownloadManager_start', nativeParam);
208 if ('NetworkError' === e.name) {
214 requests[nativeParam.callbackId] = args.downloadRequest;
216 return nativeParam.callbackId;
219 DownloadManager.prototype.cancel = 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');
233 callNative('DownloadManager_cancel', nativeParam);
239 DownloadManager.prototype.pause = 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');
253 callNative('DownloadManager_pause', nativeParam);
259 DownloadManager.prototype.resume = 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');
273 callNative('DownloadManager_resume', nativeParam);
279 DownloadManager.prototype.getState = function() {
280 var args = validator_.validateArgs(arguments, [
281 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
285 'downloadId': args.downloadId
288 if (typeof requests[args.downloadId] === 'undefined')
289 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
290 'the identifier does not match any download operation in progress');
293 return callNative('DownloadManager_getState', nativeParam);
299 DownloadManager.prototype.getDownloadRequest = function() {
300 var args = validator_.validateArgs(arguments, [
301 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
304 if (typeof requests[args.downloadId] === 'undefined')
305 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
306 'the identifier does not match any download operation in progress');
308 return requests[args.downloadId];
311 DownloadManager.prototype.getMIMEType = function() {
312 var args = validator_.validateArgs(arguments, [
313 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
317 'downloadId': args.downloadId
320 if (typeof requests[args.downloadId] === 'undefined')
321 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
322 'the identifier does not match any download operation in progress');
325 return callNative('DownloadManager_getMIMEType', nativeParam);
331 DownloadManager.prototype.setListener = function() {
332 var args = validator_.validateArgs(arguments, [
333 {'name' : 'downloadId', 'type': types_.LONG},
334 {'name' : 'downloadCallback', 'type': types_.LISTENER,
335 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
338 callbacks[args.downloadId] = args.downloadCallback;
343 exports = new DownloadManager();