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 var args = validator_.validateArgs(arguments, [
173 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
174 {'name' : 'downloadCallback', 'type': types_.LISTENER,
175 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
176 optional: true, nullable: true}
180 'url': args.downloadRequest.url,
181 'destination': args.downloadRequest.destination,
182 'fileName': args.downloadRequest.fileName,
183 'networkType': args.downloadRequest.networkType,
184 'httpHeader': args.downloadRequest.httpHeader,
185 'callbackId': nextCallbackId()
188 if (args.downloadCallback) {
189 this.setListener(nativeParam.callbackId, args.downloadCallback);
193 callNative('DownloadManager_start', nativeParam);
195 if ('NetworkError' === e.name) {
201 requests[nativeParam.callbackId] = args.downloadRequest;
203 return nativeParam.callbackId;
206 DownloadManager.prototype.cancel = function() {
207 var args = validator_.validateArgs(arguments, [
208 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
212 'downloadId': args.downloadId
215 if (typeof requests[args.downloadId] === 'undefined')
216 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
217 'the identifier does not match any download operation in progress');
220 callNative('DownloadManager_cancel', nativeParam);
226 DownloadManager.prototype.pause = function() {
227 var args = validator_.validateArgs(arguments, [
228 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
232 'downloadId': args.downloadId
235 if (typeof requests[args.downloadId] === 'undefined')
236 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
237 'the identifier does not match any download operation in progress');
240 callNative('DownloadManager_pause', nativeParam);
246 DownloadManager.prototype.resume = function() {
247 var args = validator_.validateArgs(arguments, [
248 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
252 'downloadId': args.downloadId
255 if (typeof requests[args.downloadId] === 'undefined')
256 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
257 'the identifier does not match any download operation in progress');
260 callNative('DownloadManager_resume', nativeParam);
266 DownloadManager.prototype.getState = function() {
267 var args = validator_.validateArgs(arguments, [
268 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
272 'downloadId': args.downloadId
275 if (typeof requests[args.downloadId] === 'undefined')
276 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
277 'the identifier does not match any download operation in progress');
280 return callNative('DownloadManager_getState', nativeParam);
286 DownloadManager.prototype.getDownloadRequest = function() {
287 var args = validator_.validateArgs(arguments, [
288 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
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');
295 return requests[args.downloadId];
298 DownloadManager.prototype.getMIMEType = function() {
299 var args = validator_.validateArgs(arguments, [
300 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
304 'downloadId': args.downloadId
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');
312 return callNative('DownloadManager_getMIMEType', nativeParam);
318 DownloadManager.prototype.setListener = function() {
319 var args = validator_.validateArgs(arguments, [
320 {'name' : 'downloadId', 'type': types_.LONG},
321 {'name' : 'downloadCallback', 'type': types_.LISTENER,
322 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
325 callbacks[args.downloadId] = args.downloadCallback;
330 exports = new DownloadManager();