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 check_ = xwalk.utils.type;
28 extension.setMessageListener(function(json) {
30 var result = JSON.parse(json);
31 var callback = callbacks[result.callbackId];
32 //privUtils_.log("PostMessage received: " + result.status);
35 privUtils_.log('Ignoring unknown callback: ' + result.callbackId);
38 setTimeout(function() {
39 if (result.status == 'progress') {
40 if (callback.onprogress) {
41 var receivedSize = result.receivedSize;
42 var totalSize = result.totalSize;
43 callback.onprogress(result.callbackId, receivedSize, totalSize);
45 } else if (result.status == 'paused') {
46 if (callback.onpaused) {
47 callback.onpaused(result.callbackId);
49 } else if (result.status == 'canceled') {
50 if (callback.oncanceled) {
51 callback.oncanceled(result.callbackId);
53 } else if (result.status == 'completed') {
54 if (callback.oncompleted) {
55 var fullPath = result.fullPath;
56 callback.oncompleted(result.callbackId, fullPath);
58 } else if (result.status == 'error') {
59 if (callback.onfailed) {
60 callback.onfailed(result.callbackId,
61 new WebAPIException(result.error));
67 function nextCallbackId() {
71 function callNative(cmd, args) {
72 var json = {'cmd': cmd, 'args': args};
73 var argjson = JSON.stringify(json);
74 var resultString = extension.internal.sendSyncMessage(argjson);
75 var result = JSON.parse(resultString);
77 if (typeof result !== 'object') {
78 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
81 if (result.status == 'success') {
86 } else if (result.status == 'error') {
87 var err = result.error;
89 throw new WebAPIException(err);
96 function callNativeWithCallback(cmd, args, callback) {
98 var id = nextCallbackId();
100 callbacks[id] = callback;
103 return callNative(cmd, args);
106 function SetReadOnlyProperty(obj, n, v) {
107 Object.defineProperty(obj, n, {value: v, writable: false});
110 var DownloadState = {
112 'DOWNLOADING': 'DOWNLOADING',
114 'CANCELED': 'CANCELED',
115 'COMPLETED': 'COMPLETED',
119 var DownloadNetworkType = {
120 'CELLULAR': 'CELLULAR',
125 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
126 validator_.isConstructorCall(this, tizen.DownloadRequest);
131 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
132 networkType_ = 'ALL';
134 networkType_ = networkType;
137 Object.defineProperties(this, {
143 set: function(value) {
144 if (value !== null) {
152 value: destination === undefined ? '' : destination,
157 value: fileName === undefined ? '' : fileName,
164 set: function(value) {
165 if (value === null || value in DownloadNetworkType) {
166 networkType_ = value;
173 value: httpHeader === undefined ? {} : httpHeader,
179 function DownloadManager() {
180 // constructor of DownloadManager
183 DownloadManager.prototype.start = function() {
184 var args = validator_.validateArgs(arguments, [
185 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
186 {'name' : 'downloadCallback', 'type': types_.LISTENER,
187 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
188 optional: true, nullable: true}
192 'url': args.downloadRequest.url,
193 'destination': args.downloadRequest.destination,
194 'fileName': args.downloadRequest.fileName,
195 'networkType': args.downloadRequest.networkType,
196 'httpHeader': args.downloadRequest.httpHeader,
197 'callbackId': nextCallbackId()
200 if (args.downloadCallback) {
201 this.setListener(nativeParam.callbackId, args.downloadCallback);
205 callNative('DownloadManager_start', nativeParam);
207 if ('NetworkError' === e.name) {
213 requests[nativeParam.callbackId] = args.downloadRequest;
215 return nativeParam.callbackId;
218 DownloadManager.prototype.cancel = function() {
219 var args = validator_.validateArgs(arguments, [
220 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
224 'downloadId': args.downloadId
227 if (typeof requests[args.downloadId] === 'undefined')
228 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
229 'the identifier does not match any download operation in progress');
232 callNative('DownloadManager_cancel', nativeParam);
238 DownloadManager.prototype.pause = function() {
239 var args = validator_.validateArgs(arguments, [
240 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
244 'downloadId': args.downloadId
247 if (typeof requests[args.downloadId] === 'undefined')
248 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
249 'the identifier does not match any download operation in progress');
252 callNative('DownloadManager_pause', nativeParam);
258 DownloadManager.prototype.resume = function() {
259 var args = validator_.validateArgs(arguments, [
260 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
264 'downloadId': args.downloadId
267 if (typeof requests[args.downloadId] === 'undefined')
268 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
269 'the identifier does not match any download operation in progress');
272 callNative('DownloadManager_resume', nativeParam);
278 DownloadManager.prototype.getState = function() {
279 var args = validator_.validateArgs(arguments, [
280 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
284 'downloadId': args.downloadId
287 if (typeof requests[args.downloadId] === 'undefined')
288 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
289 'the identifier does not match any download operation in progress');
292 return callNative('DownloadManager_getState', nativeParam);
298 DownloadManager.prototype.getDownloadRequest = function() {
299 var args = validator_.validateArgs(arguments, [
300 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
303 if (typeof requests[args.downloadId] === 'undefined')
304 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
305 'the identifier does not match any download operation in progress');
307 return requests[args.downloadId];
310 DownloadManager.prototype.getMIMEType = function() {
311 var args = validator_.validateArgs(arguments, [
312 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
316 'downloadId': args.downloadId
319 if (typeof requests[args.downloadId] === 'undefined')
320 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
321 'the identifier does not match any download operation in progress');
324 return callNative('DownloadManager_getMIMEType', nativeParam);
330 DownloadManager.prototype.setListener = function() {
331 var args = validator_.validateArgs(arguments, [
332 {'name' : 'downloadId', 'type': types_.LONG},
333 {'name' : 'downloadCallback', 'type': types_.LISTENER,
334 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
337 callbacks[args.downloadId] = args.downloadCallback;
342 exports = new DownloadManager();