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 JSON_ = xwalk.JSON;
18 var privUtils_ = xwalk.utils;
19 var validator_ = xwalk.utils.validator;
20 var types_ = validator_.Types;
21 var check_ = xwalk.utils.type;
29 extension.setMessageListener(function(json) {
31 var result = JSON_.parse(json);
32 var callback = callbacks[result.callbackId];
33 //privUtils_.log("PostMessage received: " + result.status);
36 privUtils_.log('Ignoring unknown callback: ' + result.callbackId);
39 setTimeout(function() {
40 if (result.status == 'progress') {
41 if (callback.onprogress) {
42 var receivedSize = result.receivedSize;
43 var totalSize = result.totalSize;
44 callback.onprogress(result.callbackId, receivedSize, totalSize);
46 } else if (result.status == 'paused') {
47 if (callback.onpaused) {
48 callback.onpaused(result.callbackId);
50 } else if (result.status == 'canceled') {
51 if (callback.oncanceled) {
52 callback.oncanceled(result.callbackId);
54 } else if (result.status == 'completed') {
55 if (callback.oncompleted) {
56 var fullPath = result.fullPath;
57 callback.oncompleted(result.callbackId, fullPath);
59 } else if (result.status == 'error') {
60 if (callback.onfailed) {
61 callback.onfailed(result.callbackId,
62 new WebAPIException(result.error));
68 function nextCallbackId() {
72 function callNative(cmd, args) {
73 var json = {'cmd': cmd, 'args': args};
74 var argjson = JSON_.stringify(json);
75 var resultString = extension.internal.sendSyncMessage(argjson);
76 var result = JSON_.parse(resultString);
78 if (typeof result !== 'object') {
79 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
82 if (result.status == 'success') {
87 } else if (result.status == 'error') {
88 var err = result.error;
90 throw new WebAPIException(err);
97 function callNativeWithCallback(cmd, args, callback) {
99 var id = nextCallbackId();
100 args.callbackId = id;
101 callbacks[id] = callback;
104 return callNative(cmd, args);
107 function SetReadOnlyProperty(obj, n, v) {
108 Object.defineProperty(obj, n, {value: v, writable: false});
111 var DownloadState = {
113 'DOWNLOADING': 'DOWNLOADING',
115 'CANCELED': 'CANCELED',
116 'COMPLETED': 'COMPLETED',
120 var DownloadNetworkType = {
121 'CELLULAR': 'CELLULAR',
126 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
127 validator_.isConstructorCall(this, tizen.DownloadRequest);
132 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
133 networkType_ = 'ALL';
135 networkType_ = networkType;
138 Object.defineProperties(this, {
144 set: function(value) {
145 if (value !== null) {
153 value: destination === undefined ? '' : destination,
158 value: fileName === undefined ? '' : fileName,
165 set: function(value) {
166 if (value === null || value in DownloadNetworkType) {
167 networkType_ = value;
174 value: httpHeader === undefined ? {} : httpHeader,
180 function DownloadManager() {
181 // constructor of DownloadManager
184 DownloadManager.prototype.start = function() {
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();