3 var validator_ = xwalk.utils.validator;
4 var types_ = validator_.Types;
5 var check_ = xwalk.utils.type;
13 extension.setMessageListener(function(json) {
15 var result = JSON.parse(json);
16 var callback = callbacks[result.callbackId];
17 //console.log("PostMessage received: " + result.status);
20 console.logd('Ignoring unknown callback: ' + result.callbackId);
24 if (result.status == 'progress') {
25 if (callback.onprogress) {
26 var receivedSize = result.receivedSize;
27 var totalSize = result.totalSize;
28 callback.onprogress(result.callbackId, receivedSize, totalSize);
31 else if (result.status == 'paused') {
32 if (callback.onpaused) {
33 callback.onpaused(result.callbackId);
36 else if (result.status == 'canceled') {
37 if (callback.oncanceled) {
38 callback.oncanceled(result.callbackId);
41 else if (result.status == 'completed') {
42 if (callback.oncompleted) {
43 var fullPath = result.fullPath;
44 callback.oncompleted(result.callbackId, fullPath);
47 else if (result.status == 'error') {
48 if (callback.onfailed) {
49 callback.onfailed(result.callbackId,
50 new WebAPIException(result.error));
55 function nextCallbackId() {
59 function callNative(cmd, args) {
60 var json = {'cmd': cmd, 'args': args};
61 var argjson = JSON.stringify(json);
62 var resultString = extension.internal.sendSyncMessage(argjson);
63 var result = JSON.parse(resultString);
65 if (typeof result !== 'object') {
66 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
69 if (result.status == 'success') {
74 } else if (result.status == 'error') {
75 var err = result.error;
77 throw new WebAPIException(err);
84 function callNativeWithCallback(cmd, args, callback) {
86 var id = nextCallbackId();
88 callbacks[id] = callback;
91 return callNative(cmd, args);
94 function SetReadOnlyProperty(obj, n, v) {
95 Object.defineProperty(obj, n, {value: v, writable: false});
100 'DOWNLOADING': 'DOWNLOADING',
102 'CANCELED': 'CANCELED',
103 'COMPLETED': 'COMPLETED',
107 var DownloadNetworkType = {
108 'CELLULAR': 'CELLULAR',
113 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
114 validator_.isConstructorCall(this, tizen.DownloadRequest);
119 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
120 networkType_ = 'ALL';
122 networkType_ = networkType;
125 Object.defineProperties(this, {
131 set: function(value) {
132 if (value !== null) {
140 value: destination === undefined ? '' : destination,
145 value: fileName === undefined ? '' : fileName,
152 set: function(value) {
153 if (value === null || value in DownloadNetworkType) {
154 networkType_ = value;
161 value: httpHeader === undefined ? {} : httpHeader,
167 function DownloadManager() {
168 // constructor of DownloadManager
171 DownloadManager.prototype.start = function() {
172 xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.DOWNLOAD);
174 var args = validator_.validateArgs(arguments, [
175 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
176 {'name' : 'downloadCallback', 'type': types_.LISTENER,
177 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
178 optional: true, nullable: true}
182 'url': args.downloadRequest.url,
183 'destination': args.downloadRequest.destination,
184 'fileName': args.downloadRequest.fileName,
185 'networkType': args.downloadRequest.networkType,
186 'httpHeader': args.downloadRequest.httpHeader,
187 'callbackId': nextCallbackId()
190 if (args.downloadCallback) {
191 this.setListener(nativeParam.callbackId, args.downloadCallback);
195 callNative('DownloadManager_start', nativeParam);
197 if ('NetworkError' === e.name) {
203 requests[nativeParam.callbackId] = args.downloadRequest;
205 return nativeParam.callbackId;
208 DownloadManager.prototype.cancel = function() {
209 var args = validator_.validateArgs(arguments, [
210 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
214 'downloadId': args.downloadId
217 if (typeof requests[args.downloadId] === 'undefined')
218 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
219 'the identifier does not match any download operation in progress');
222 callNative('DownloadManager_cancel', nativeParam);
228 DownloadManager.prototype.pause = function() {
229 var args = validator_.validateArgs(arguments, [
230 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
234 'downloadId': args.downloadId
237 if (typeof requests[args.downloadId] === 'undefined')
238 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
239 'the identifier does not match any download operation in progress');
242 callNative('DownloadManager_pause', nativeParam);
248 DownloadManager.prototype.resume = function() {
249 var args = validator_.validateArgs(arguments, [
250 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
254 'downloadId': args.downloadId
257 if (typeof requests[args.downloadId] === 'undefined')
258 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
259 'the identifier does not match any download operation in progress');
262 callNative('DownloadManager_resume', nativeParam);
268 DownloadManager.prototype.getState = function() {
269 var args = validator_.validateArgs(arguments, [
270 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
274 'downloadId': args.downloadId
277 if (typeof requests[args.downloadId] === 'undefined')
278 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
279 'the identifier does not match any download operation in progress');
282 return callNative('DownloadManager_getState', nativeParam);
288 DownloadManager.prototype.getDownloadRequest = function() {
289 var args = validator_.validateArgs(arguments, [
290 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
293 if (typeof requests[args.downloadId] === 'undefined')
294 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
295 'the identifier does not match any download operation in progress');
297 return requests[args.downloadId];
300 DownloadManager.prototype.getMIMEType = function() {
301 var args = validator_.validateArgs(arguments, [
302 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
306 'downloadId': args.downloadId
309 if (typeof requests[args.downloadId] === 'undefined')
310 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
311 'the identifier does not match any download operation in progress');
314 return callNative('DownloadManager_getMIMEType', nativeParam);
320 DownloadManager.prototype.setListener = function() {
321 var args = validator_.validateArgs(arguments, [
322 {'name' : 'downloadId', 'type': types_.LONG},
323 {'name' : 'downloadCallback', 'type': types_.LISTENER,
324 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
327 callbacks[args.downloadId] = args.downloadCallback;
332 exports = new DownloadManager();