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;
22 var converter_ = xwalk.utils.converter;
30 extension.setMessageListener(function(json) {
32 var result = JSON_.parse(json);
33 var callback = callbacks[result.callbackId];
34 //privUtils_.log("PostMessage received: " + result.status);
37 privUtils_.log('Ignoring unknown callback: ' + result.callbackId);
40 setTimeout(function() {
41 if (result.status == 'progress') {
42 if (callback.onprogress) {
43 var receivedSize = result.receivedSize;
44 var totalSize = result.totalSize;
45 callback.onprogress(result.callbackId, receivedSize, totalSize);
47 } else if (result.status == 'paused') {
48 if (callback.onpaused) {
49 callback.onpaused(result.callbackId);
51 } else if (result.status == 'canceled') {
52 if (callback.oncanceled) {
53 callback.oncanceled(result.callbackId);
55 } else if (result.status == 'completed') {
56 if (callback.oncompleted) {
57 var fullPath = result.fullPath;
58 callback.oncompleted(result.callbackId, fullPath);
60 } else if (result.status == 'error') {
61 if (callback.onfailed) {
62 callback.onfailed(result.callbackId,
63 new WebAPIException(result.error));
69 function nextCallbackId() {
73 function callNative(cmd, args) {
74 var json = {'cmd': cmd, 'args': args};
75 var argjson = JSON_.stringify(json);
76 var resultString = extension.internal.sendSyncMessage(argjson);
77 var result = JSON_.parse(resultString);
79 if (typeof result !== 'object') {
80 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
83 if (result.status == 'success') {
88 } else if (result.status == 'error') {
89 var err = result.error;
91 throw new WebAPIException(err);
98 function callNativeWithCallback(cmd, args, callback) {
100 var id = nextCallbackId();
101 args.callbackId = id;
102 callbacks[id] = callback;
105 return callNative(cmd, args);
108 function SetReadOnlyProperty(obj, n, v) {
109 Object.defineProperty(obj, n, {value: v, writable: false});
112 var DownloadState = {
114 'DOWNLOADING': 'DOWNLOADING',
116 'CANCELED': 'CANCELED',
117 'COMPLETED': 'COMPLETED',
121 var DownloadNetworkType = {
122 'CELLULAR': 'CELLULAR',
127 tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
128 validator_.isConstructorCall(this, tizen.DownloadRequest);
130 var url_ = converter_.toString(url);
131 var destination_ = destination === undefined ? '' : converter_.toString(destination);
132 var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
136 if (networkType === undefined || !(networkType in DownloadNetworkType)) {
137 networkType_ = 'ALL';
139 networkType_ = networkType;
142 Object.defineProperties(this, {
148 set: function(value) {
149 if (value !== null) {
150 url_ = converter_.toString(value);
159 set: function(value) {
160 if (value !== null) {
161 destination_ = converter_.toString(value);
170 set: function(value) {
171 if (value !== null) {
172 fileName_ = converter_.toString(value);
181 set: function(value) {
182 if (value === null || value in DownloadNetworkType) {
183 networkType_ = value;
190 value: httpHeader === undefined ? {} : httpHeader,
196 function DownloadManager() {
197 // constructor of DownloadManager
200 DownloadManager.prototype.start = function() {
201 var args = validator_.validateArgs(arguments, [
202 {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
203 {'name' : 'downloadCallback', 'type': types_.LISTENER,
204 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
205 optional: true, nullable: true}
209 'url': args.downloadRequest.url,
210 'destination': args.downloadRequest.destination,
211 'fileName': args.downloadRequest.fileName,
212 'networkType': args.downloadRequest.networkType,
213 'httpHeader': args.downloadRequest.httpHeader,
214 'callbackId': nextCallbackId()
217 if (args.downloadCallback) {
218 this.setListener(nativeParam.callbackId, args.downloadCallback);
222 callNative('DownloadManager_start', nativeParam);
224 if ('NetworkError' === e.name) {
230 requests[nativeParam.callbackId] = args.downloadRequest;
232 return nativeParam.callbackId;
235 DownloadManager.prototype.cancel = function() {
236 var args = validator_.validateArgs(arguments, [
237 {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
241 'downloadId': args.downloadId
244 if (typeof requests[args.downloadId] === 'undefined')
245 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
246 'the identifier does not match any download operation in progress');
249 callNative('DownloadManager_cancel', nativeParam);
255 DownloadManager.prototype.pause = function() {
256 var args = validator_.validateArgs(arguments, [
257 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
261 'downloadId': args.downloadId
264 if (typeof requests[args.downloadId] === 'undefined')
265 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
266 'the identifier does not match any download operation in progress');
269 callNative('DownloadManager_pause', nativeParam);
275 DownloadManager.prototype.resume = function() {
276 var args = validator_.validateArgs(arguments, [
277 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
281 'downloadId': args.downloadId
284 if (typeof requests[args.downloadId] === 'undefined')
285 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
286 'the identifier does not match any download operation in progress');
289 callNative('DownloadManager_resume', nativeParam);
295 DownloadManager.prototype.getState = 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_getState', nativeParam);
315 DownloadManager.prototype.getDownloadRequest = function() {
316 var args = validator_.validateArgs(arguments, [
317 {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
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');
324 return requests[args.downloadId];
327 DownloadManager.prototype.getMIMEType = function() {
328 var args = validator_.validateArgs(arguments, [
329 {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
333 'downloadId': args.downloadId
336 if (typeof requests[args.downloadId] === 'undefined')
337 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
338 'the identifier does not match any download operation in progress');
341 return callNative('DownloadManager_getMIMEType', nativeParam);
347 DownloadManager.prototype.setListener = function() {
348 var args = validator_.validateArgs(arguments, [
349 {'name' : 'downloadId', 'type': types_.LONG},
350 {'name' : 'downloadCallback', 'type': types_.LISTENER,
351 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
354 callbacks[args.downloadId] = args.downloadCallback;
359 exports = new DownloadManager();