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);
19 if (result.status == 'progress') {
20 var receivedSize = result.receivedSize;
21 var totalSize = result.totalSize;
22 callback.onprogress(result.callbackId, receivedSize, totalSize);
24 else if (result.status == 'paused') {
25 callback.onpaused(result.callbackId);
27 else if (result.status == 'canceled') {
28 callback.oncanceled(result.callbackId);
30 else if (result.status == 'completed') {
31 var fullPath = result.fullPath;
32 callback.oncompleted(result.callbackId, fullPath);
34 else if (result.status == 'error') {
36 result.callbackId, new tizen.WebAPIError(result['error'].name, result['error'].message));
40 function nextCallbackId() {
44 function callNative(cmd, args) {
45 var json = {'cmd': cmd, 'args': args};
46 var argjson = JSON.stringify(json);
47 var resultString = extension.internal.sendSyncMessage(argjson);
48 var result = JSON.parse(resultString);
50 if (typeof result !== 'object') {
51 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
54 if (result['status'] == 'success') {
55 if (result['result']) {
56 return result['result'];
59 } else if (result['status'] == 'error') {
60 var err = result['error'];
62 throw new WebAPIException(err.name, err.message);
69 function callNativeWithCallback(cmd, args, callback) {
71 var id = nextCallbackId();
72 args['callbackId'] = id;
73 callbacks[id] = callback;
76 return callNative(cmd, args);
79 function SetReadOnlyProperty(obj, n, v) {
80 Object.defineProperty(obj, n, {value: v, writable: false});
85 'DOWNLOADING': 'DOWNLOADING',
87 'CANCELED': 'CANCELED',
88 'COMPLETED': 'COMPLETED',
92 var DownloadNetworkType = {
93 'CELLULAR': 'CELLULAR',
98 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
99 validator_.isConstructorCall(this, tizen.DownloadRequest);
100 var args = validator_.validateArgs(arguments, [
101 {'name' : 'url', 'type': types_.STRING, 'nullable': false, 'optional': false},
102 {'name' : 'destination', 'type': types_.STRING, 'nullable': true, 'optional': true},
103 {'name' : 'fileName', 'type': types_.STRING, 'nullable': true, 'optional': true},
104 {'name' : 'networkType', 'type': types_.ENUM, 'values': ['CELLULAR', 'WIFI', 'ALL'],
105 'nullable' : true, 'optional': true},
106 {'name' : 'httpHeader', 'type': types_.Dictionary, 'nullable': true, 'optional': true}
112 if (networkType === undefined) networkType_ = 'ALL';
113 else if (networkType in DownloadNetworkType) networkType_ = networkType;
115 Object.defineProperties(this, {
116 'url': { enumerable: true,
117 get: function() { return url_;},
118 set: function(value) { if (value != null) { url_ = value; }} },
119 'destination': { writable: true, enumerable: true,
120 value: destination === undefined ? '' : destination },
121 'fileName': { writable: true, enumerable: true,
122 value: fileName === undefined ? '' : fileName },
123 'networkType': { enumerable: true,
124 get: function() { return networkType_;},
125 set: function(value) {
126 if (value === null || value in DownloadNetworkType) { networkType_ = value; }} },
127 'httpHeader': { writable: true, enumerable: true,
128 value: httpHeader === undefined ? {} : httpHeader }
133 function DownloadManager() {
134 // constructor of DownloadManager
137 DownloadManager.prototype.start = function(downloadRequest) {
138 var args = validator_.validateArgs(arguments, [
139 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
140 {'name' : 'downloadCallback', 'type': types_.LISTENER,
141 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
142 optional: true, nullable: true}
146 'url': args.downloadRequest.url,
147 'destination': args.downloadRequest.destination,
148 'fileName': args.downloadRequest.fileName,
149 'networkType': args.downloadRequest.networkType,
150 'httpHeader': args.downloadRequest.httpHeader,
151 'callbackId': nextCallbackId()
154 if (args.downloadCallback) {
155 this.setListener(nativeParam.callbackId, args.downloadCallback);
159 var syncResult = callNative('DownloadManager_start', nativeParam);
164 requests[nativeParam.callbackId] = args.downloadRequest;
166 return nativeParam.callbackId;
169 DownloadManager.prototype.cancel = function(downloadId) {
170 var args = validator_.validateArgs(arguments, [
171 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
175 'downloadId': args.downloadId
178 if (typeof requests[downloadId] === 'undefined')
179 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
180 'the identifier does not match any download operation in progress');
183 var syncResult = callNative('DownloadManager_cancel', nativeParam);
189 DownloadManager.prototype.pause = function(downloadId) {
190 var args = validator_.validateArgs(arguments, [
191 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
195 'downloadId': args.downloadId
198 if (typeof requests[downloadId] === 'undefined')
199 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
200 'the identifier does not match any download operation in progress');
203 var syncResult = callNative('DownloadManager_pause', nativeParam);
209 DownloadManager.prototype.resume = function(downloadId) {
210 var args = validator_.validateArgs(arguments, [
211 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
215 'downloadId': args.downloadId
218 if (typeof requests[downloadId] === 'undefined')
219 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
220 'the identifier does not match any download operation in progress');
223 var syncResult = callNative('DownloadManager_resume', nativeParam);
229 DownloadManager.prototype.getState = function(downloadId) {
230 var args = validator_.validateArgs(arguments, [
231 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
235 'downloadId': args.downloadId
238 if (typeof requests[downloadId] === 'undefined')
239 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
240 'the identifier does not match any download operation in progress');
243 var syncResult = callNative('DownloadManager_getState', nativeParam);
251 DownloadManager.prototype.getDownloadRequest = function(downloadId) {
252 var args = validator_.validateArgs(arguments, [
253 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
256 if (typeof requests[downloadId] === 'undefined')
257 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
258 'the identifier does not match any download operation in progress');
260 return requests[args.downloadId];
263 DownloadManager.prototype.getMIMEType = function(downloadId) {
264 var args = validator_.validateArgs(arguments, [
265 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
269 'downloadId': args.downloadId
272 if (typeof requests[downloadId] === 'undefined')
273 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
274 'the identifier does not match any download operation in progress');
277 var syncResult = callNative('DownloadManager_getMIMEType', nativeParam);
285 DownloadManager.prototype.setListener = function(downloadId, downloadCallback) {
286 var args = validator_.validateArgs(arguments, [
287 {'name' : 'downloadId', 'type': types_.LONG},
288 {'name' : 'downloadCallback', 'type': types_.LISTENER,
289 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
292 callbacks[args.downloadId] = args.downloadCallback;
297 exports = new DownloadManager();