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 var args = validator_.validateArgs(arguments, [
184 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
185 {'name' : 'downloadCallback', 'type': types_.LISTENER,
186 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
187 optional: true, nullable: true}
191 'url': args.downloadRequest.url,
192 'destination': args.downloadRequest.destination,
193 'fileName': args.downloadRequest.fileName,
194 'networkType': args.downloadRequest.networkType,
195 'httpHeader': args.downloadRequest.httpHeader,
196 'callbackId': nextCallbackId()
199 if (args.downloadCallback) {
200 this.setListener(nativeParam.callbackId, args.downloadCallback);
204 callNative('DownloadManager_start', nativeParam);
206 if ('NetworkError' === e.name) {
212 requests[nativeParam.callbackId] = args.downloadRequest;
214 return nativeParam.callbackId;
217 DownloadManager.prototype.cancel = function() {
218 var args = validator_.validateArgs(arguments, [
219 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
223 'downloadId': args.downloadId
226 if (typeof requests[args.downloadId] === 'undefined')
227 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
228 'the identifier does not match any download operation in progress');
231 callNative('DownloadManager_cancel', nativeParam);
237 DownloadManager.prototype.pause = function() {
238 var args = validator_.validateArgs(arguments, [
239 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
243 'downloadId': args.downloadId
246 if (typeof requests[args.downloadId] === 'undefined')
247 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
248 'the identifier does not match any download operation in progress');
251 callNative('DownloadManager_pause', nativeParam);
257 DownloadManager.prototype.resume = function() {
258 var args = validator_.validateArgs(arguments, [
259 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
263 'downloadId': args.downloadId
266 if (typeof requests[args.downloadId] === 'undefined')
267 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
268 'the identifier does not match any download operation in progress');
271 callNative('DownloadManager_resume', nativeParam);
277 DownloadManager.prototype.getState = function() {
278 var args = validator_.validateArgs(arguments, [
279 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
283 'downloadId': args.downloadId
286 if (typeof requests[args.downloadId] === 'undefined')
287 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
288 'the identifier does not match any download operation in progress');
291 return callNative('DownloadManager_getState', nativeParam);
297 DownloadManager.prototype.getDownloadRequest = function() {
298 var args = validator_.validateArgs(arguments, [
299 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
302 if (typeof requests[args.downloadId] === 'undefined')
303 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
304 '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(WebAPIException.INVALID_VALUES_ERR,
320 'the identifier does not match any download operation in progress');
323 return callNative('DownloadManager_getMIMEType', nativeParam);
329 DownloadManager.prototype.setListener = function() {
330 var args = validator_.validateArgs(arguments, [
331 {'name' : 'downloadId', 'type': types_.LONG},
332 {'name' : 'downloadCallback', 'type': types_.LISTENER,
333 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
336 callbacks[args.downloadId] = args.downloadCallback;
341 exports = new DownloadManager();