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);
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);
45 else if (result.status == 'paused') {
46 if (callback.onpaused) {
47 callback.onpaused(result.callbackId);
50 else if (result.status == 'canceled') {
51 if (callback.oncanceled) {
52 callback.oncanceled(result.callbackId);
55 else if (result.status == 'completed') {
56 if (callback.oncompleted) {
57 var fullPath = result.fullPath;
58 callback.oncompleted(result.callbackId, fullPath);
61 else if (result.status == 'error') {
62 if (callback.onfailed) {
63 callback.onfailed(result.callbackId,
64 new WebAPIException(result.error));
69 function nextCallbackId() {
73 function callNative(cmd, args) {
74 var json = {'cmd': cmd, 'args': args};
75 var argjson = JSON.stringify(json);
76 var resultString = extension.internal.sendSyncMessage(argjson);
77 var result = JSON.parse(resultString);
79 if (typeof result !== 'object') {
80 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
83 if (result.status == 'success') {
88 } else if (result.status == 'error') {
89 var err = result.error;
91 throw new WebAPIException(err);
98 function callNativeWithCallback(cmd, args, callback) {
100 var id = nextCallbackId();
101 args.callbackId = id;
102 callbacks[id] = callback;
105 return callNative(cmd, args);
108 function SetReadOnlyProperty(obj, n, v) {
109 Object.defineProperty(obj, n, {value: v, writable: false});
112 var DownloadState = {
114 'DOWNLOADING': 'DOWNLOADING',
116 'CANCELED': 'CANCELED',
117 'COMPLETED': 'COMPLETED',
121 var DownloadNetworkType = {
122 'CELLULAR': 'CELLULAR',
127 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
128 validator_.isConstructorCall(this, tizen.DownloadRequest);
133 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
134 networkType_ = 'ALL';
136 networkType_ = networkType;
139 Object.defineProperties(this, {
145 set: function(value) {
146 if (value !== null) {
154 value: destination === undefined ? '' : destination,
159 value: fileName === undefined ? '' : fileName,
166 set: function(value) {
167 if (value === null || value in DownloadNetworkType) {
168 networkType_ = value;
175 value: httpHeader === undefined ? {} : httpHeader,
181 function DownloadManager() {
182 // constructor of DownloadManager
185 DownloadManager.prototype.start = function() {
186 xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.DOWNLOAD);
188 var args = validator_.validateArgs(arguments, [
189 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
190 {'name' : 'downloadCallback', 'type': types_.LISTENER,
191 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
192 optional: true, nullable: true}
196 'url': args.downloadRequest.url,
197 'destination': args.downloadRequest.destination,
198 'fileName': args.downloadRequest.fileName,
199 'networkType': args.downloadRequest.networkType,
200 'httpHeader': args.downloadRequest.httpHeader,
201 'callbackId': nextCallbackId()
204 if (args.downloadCallback) {
205 this.setListener(nativeParam.callbackId, args.downloadCallback);
209 callNative('DownloadManager_start', nativeParam);
211 if ('NetworkError' === e.name) {
217 requests[nativeParam.callbackId] = args.downloadRequest;
219 return nativeParam.callbackId;
222 DownloadManager.prototype.cancel = function() {
223 var args = validator_.validateArgs(arguments, [
224 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
228 'downloadId': args.downloadId
231 if (typeof requests[args.downloadId] === 'undefined')
232 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
233 'the identifier does not match any download operation in progress');
236 callNative('DownloadManager_cancel', nativeParam);
242 DownloadManager.prototype.pause = function() {
243 var args = validator_.validateArgs(arguments, [
244 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
248 'downloadId': args.downloadId
251 if (typeof requests[args.downloadId] === 'undefined')
252 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
253 'the identifier does not match any download operation in progress');
256 callNative('DownloadManager_pause', nativeParam);
262 DownloadManager.prototype.resume = function() {
263 var args = validator_.validateArgs(arguments, [
264 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
268 'downloadId': args.downloadId
271 if (typeof requests[args.downloadId] === 'undefined')
272 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
273 'the identifier does not match any download operation in progress');
276 callNative('DownloadManager_resume', nativeParam);
282 DownloadManager.prototype.getState = function() {
283 var args = validator_.validateArgs(arguments, [
284 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
288 'downloadId': args.downloadId
291 if (typeof requests[args.downloadId] === 'undefined')
292 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
293 'the identifier does not match any download operation in progress');
296 return callNative('DownloadManager_getState', nativeParam);
302 DownloadManager.prototype.getDownloadRequest = function() {
303 var args = validator_.validateArgs(arguments, [
304 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
307 if (typeof requests[args.downloadId] === 'undefined')
308 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
309 'the identifier does not match any download operation in progress');
311 return requests[args.downloadId];
314 DownloadManager.prototype.getMIMEType = function() {
315 var args = validator_.validateArgs(arguments, [
316 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
320 'downloadId': args.downloadId
323 if (typeof requests[args.downloadId] === 'undefined')
324 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
325 'the identifier does not match any download operation in progress');
328 return callNative('DownloadManager_getMIMEType', nativeParam);
334 DownloadManager.prototype.setListener = function() {
335 var args = validator_.validateArgs(arguments, [
336 {'name' : 'downloadId', 'type': types_.LONG},
337 {'name' : 'downloadCallback', 'type': types_.LISTENER,
338 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
341 callbacks[args.downloadId] = args.downloadCallback;
346 exports = new DownloadManager();