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.name, result.error.message));
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.name, err.message);
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);
198 requests[nativeParam.callbackId] = args.downloadRequest;
200 return nativeParam.callbackId;
203 DownloadManager.prototype.cancel = function() {
204 var args = validator_.validateArgs(arguments, [
205 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
209 'downloadId': args.downloadId
212 if (typeof requests[args.downloadId] === 'undefined')
213 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
214 'the identifier does not match any download operation in progress');
217 callNative('DownloadManager_cancel', nativeParam);
223 DownloadManager.prototype.pause = function() {
224 var args = validator_.validateArgs(arguments, [
225 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
229 'downloadId': args.downloadId
232 if (typeof requests[args.downloadId] === 'undefined')
233 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
234 'the identifier does not match any download operation in progress');
237 callNative('DownloadManager_pause', nativeParam);
243 DownloadManager.prototype.resume = function() {
244 var args = validator_.validateArgs(arguments, [
245 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
249 'downloadId': args.downloadId
252 if (typeof requests[args.downloadId] === 'undefined')
253 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
254 'the identifier does not match any download operation in progress');
257 callNative('DownloadManager_resume', nativeParam);
263 DownloadManager.prototype.getState = function() {
264 var args = validator_.validateArgs(arguments, [
265 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
269 'downloadId': args.downloadId
272 if (typeof requests[args.downloadId] === 'undefined')
273 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
274 'the identifier does not match any download operation in progress');
277 return callNative('DownloadManager_getState', nativeParam);
283 DownloadManager.prototype.getDownloadRequest = function() {
284 var args = validator_.validateArgs(arguments, [
285 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
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');
292 return requests[args.downloadId];
295 DownloadManager.prototype.getMIMEType = function() {
296 var args = validator_.validateArgs(arguments, [
297 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
301 'downloadId': args.downloadId
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');
309 return callNative('DownloadManager_getMIMEType', nativeParam);
315 DownloadManager.prototype.setListener = function() {
316 var args = validator_.validateArgs(arguments, [
317 {'name' : 'downloadId', 'type': types_.LONG},
318 {'name' : 'downloadCallback', 'type': types_.LISTENER,
319 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
322 callbacks[args.downloadId] = args.downloadCallback;
327 exports = new DownloadManager();