-// Download
-
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var privUtils_ = xwalk.utils;
var validator_ = xwalk.utils.validator;
var types_ = validator_.Types;
-var check_ = xwalk.utils.type;
+var converter_ = xwalk.utils.converter;
+var native_ = new xwalk.utils.NativeManager(extension);
+var DOWNLOAD_MANAGER_LISTENER_ID = 'DownloadManagerListener';
-var callbackId = 0;
+var downloadId = 0;
var callbacks = {};
var requests = {};
+function DownloadManagerChangeCallback(result) {
+ var callback = callbacks[result.downloadId];
-extension.setMessageListener(function(json) {
-
- var result = JSON.parse(json);
- var callback = callbacks[result['callbackId']];
- //console.log("PostMessage received: " + result.status);
-
- if (result.status == 'progress') {
- var receivedSize = result.receivedSize;
- var totalSize = result.totalSize;
- callback.onprogress(result.callbackId, receivedSize, totalSize);
- }
- else if (result.status == 'paused') {
- callback.onpaused(result.callbackId);
- }
- else if (result.status == 'canceled') {
- callback.oncanceled(result.callbackId);
- }
- else if (result.status == 'completed') {
- var fullPath = result.fullPath;
- callback.oncompleted(result.callbackId, fullPath);
- }
- else if (result.status == 'error') {
- callback.onfailed(
- result.callbackId, new tizen.WebAPIError(result['error'].name, result['error'].message));
- }
-});
-
-function nextCallbackId() {
- return callbackId++;
-}
-
-function callNative(cmd, args) {
- var json = {'cmd': cmd, 'args': args};
- var argjson = JSON.stringify(json);
- var resultString = extension.internal.sendSyncMessage(argjson);
- var result = JSON.parse(resultString);
-
- if (typeof result !== 'object') {
- throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
- }
-
- if (result['status'] == 'success') {
- if (result['result']) {
- return result['result'];
+ if (!callback) {
+ privUtils_.log('Ignoring unknown callback: ' + result.downloadId);
+ return;
}
- return true;
- } else if (result['status'] == 'error') {
- var err = result['error'];
- if (err) {
- throw new WebAPIException(err.name, err.message);
- }
- return false;
- }
+ setTimeout(function() {
+ if (result.status == 'progress') {
+ if (callback.onprogress) {
+ var receivedSize = result.receivedSize;
+ var totalSize = result.totalSize;
+ callback.onprogress(result.downloadId, receivedSize, totalSize);
+ }
+ } else if (result.status == 'paused') {
+ if (callback.onpaused) {
+ callback.onpaused(result.downloadId);
+ }
+ } else if (result.status == 'canceled') {
+ if (callback.oncanceled) {
+ callback.oncanceled(result.downloadId);
+ }
+ } else if (result.status == 'completed') {
+ if (callback.oncompleted) {
+ var fullPath = result.fullPath;
+ callback.oncompleted(result.downloadId, fullPath);
+ delete callbacks[result.downloadId];
+ }
+ } else if (result.status == 'error') {
+ if (callback.onfailed) {
+ callback.onfailed(result.downloadId, new WebAPIException(result.error));
+ }
+ }
+ }, 0);
}
-
-function callNativeWithCallback(cmd, args, callback) {
- if (callback) {
- var id = nextCallbackId();
- args['callbackId'] = id;
- callbacks[id] = callback;
- }
-
- return callNative(cmd, args);
+function nextDownloadId() {
+ return ++downloadId;
}
function SetReadOnlyProperty(obj, n, v) {
- Object.defineProperty(obj, n, {value: v, writable: false});
+ Object.defineProperty(obj, n, { value: v, writable: false });
}
var DownloadState = {
- 'QUEUED': 'QUEUED',
- 'DOWNLOADING': 'DOWNLOADING',
- 'PAUSED': 'PAUSED',
- 'CANCELED': 'CANCELED',
- 'COMPLETED': 'COMPLETED',
- 'FAILED': 'FAILED'
+ QUEUED: 'QUEUED',
+ DOWNLOADING: 'DOWNLOADING',
+ PAUSED: 'PAUSED',
+ CANCELED: 'CANCELED',
+ COMPLETED: 'COMPLETED',
+ FAILED: 'FAILED',
+ ABANDONED: 'ABANDONED'
};
var DownloadNetworkType = {
- 'CELLULAR': 'CELLULAR',
- 'WIFI': 'WIFI',
- 'ALL': 'ALL'
+ CELLULAR: 'CELLULAR',
+ WIFI: 'WIFI',
+ ALL: 'ALL'
};
tizen.DownloadRequest = function(url, destination, fileName, networkType, httpHeader) {
- validator_.isConstructorCall(this, tizen.DownloadRequest);
- var args = validator_.validateArgs(arguments, [
- {'name' : 'url', 'type': types_.STRING, 'nullable': false, 'optional': false},
- {'name' : 'destination', 'type': types_.STRING, 'nullable': true, 'optional': true},
- {'name' : 'fileName', 'type': types_.STRING, 'nullable': true, 'optional': true},
- {'name' : 'networkType', 'type': types_.ENUM, 'values': ['CELLULAR', 'WIFI', 'ALL'],
- 'nullable' : true, 'optional': true},
- {'name' : 'httpHeader', 'type': types_.Dictionary, 'nullable': true, 'optional': true}
- ]);
-
- var url_ = url;
- var networkType_;
-
- if (networkType === undefined) networkType_ = 'ALL';
- else if (networkType in DownloadNetworkType) networkType_ = networkType;
-
- Object.defineProperties(this, {
- 'url': { enumerable: true,
- get: function() { return url_;},
- set: function(value) { if (value != null) { url_ = value; }} },
- 'destination': { writable: true, enumerable: true,
- value: destination === undefined ? '' : destination },
- 'fileName': { writable: true, enumerable: true,
- value: fileName === undefined ? '' : fileName },
- 'networkType': { enumerable: true,
- get: function() { return networkType_;},
- set: function(value) {
- if (value === null || value in DownloadNetworkType) { networkType_ = value; }} },
- 'httpHeader': { writable: true, enumerable: true,
- value: httpHeader === undefined ? {} : httpHeader }
- });
-};
+ validator_.isConstructorCall(this, tizen.DownloadRequest);
+
+ var url_ = converter_.toString(url);
+ var destination_ = destination === undefined ? '' : converter_.toString(destination);
+ var fileName_ = fileName === undefined ? '' : converter_.toString(fileName);
+ var networkType_;
+
+ if (networkType === undefined || !(networkType in DownloadNetworkType)) {
+ networkType_ = 'ALL';
+ } else {
+ networkType_ = networkType;
+ }
+
+ Object.defineProperties(this, {
+ url: {
+ enumerable: true,
+ get: function() {
+ return url_;
+ },
+ set: function(value) {
+ if (value !== null) {
+ url_ = converter_.toString(value);
+ }
+ }
+ },
+ destination: {
+ enumerable: true,
+ get: function() {
+ return destination_;
+ },
+ set: function(value) {
+ if (value !== null) {
+ destination_ = converter_.toString(value);
+ }
+ }
+ },
+ fileName: {
+ enumerable: true,
+ get: function() {
+ return fileName_;
+ },
+ set: function(value) {
+ if (value !== null) {
+ fileName_ = converter_.toString(value);
+ }
+ }
+ },
+ networkType: {
+ enumerable: true,
+ get: function() {
+ return networkType_;
+ },
+ set: function(value) {
+ if (value === null || value in DownloadNetworkType) {
+ networkType_ = value;
+ }
+ }
+ },
+ httpHeader: {
+ writable: true,
+ enumerable: true,
+ value: httpHeader === undefined ? {} : httpHeader
+ }
+ });
+};
function DownloadManager() {
- // constructor of DownloadManager
+ // constructor of DownloadManager
}
-DownloadManager.prototype.start = function(downloadRequest) {
- var args = validator_.validateArgs(arguments, [
- {'name' : 'downloadRequest', 'type': types_.PLATFORM_OBJECT, 'values': tizen.DownloadRequest},
- {'name' : 'downloadCallback', 'type': types_.LISTENER,
- 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
- optional: true, nullable: true}
- ]);
-
- var nativeParam = {
- 'url': args.downloadRequest.url,
- 'destination': args.downloadRequest.destination,
- 'fileName': args.downloadRequest.fileName,
- 'networkType': args.downloadRequest.networkType,
- 'httpHeader': args.downloadRequest.httpHeader,
- 'callbackId': nextCallbackId()
- };
-
- if (args.downloadCallback) {
- this.setListener(nativeParam.callbackId, args.downloadCallback);
- }
-
- try {
- var syncResult = callNative('DownloadManager_start', nativeParam);
- } catch (e) {
- throw e;
- }
-
- requests[nativeParam.callbackId] = args.downloadRequest;
-
- return nativeParam.callbackId;
-};
+DownloadManager.prototype.start = function() {
+ var args = validator_.validateArgs(arguments, [
+ {
+ name: 'downloadRequest',
+ type: types_.PLATFORM_OBJECT,
+ values: tizen.DownloadRequest
+ },
+ {
+ name: 'downloadCallback',
+ type: types_.LISTENER,
+ values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed'],
+ optional: true,
+ nullable: true
+ }
+ ]);
+
+ var nativeParam = {
+ url: args.downloadRequest.url,
+ destination: args.downloadRequest.destination,
+ fileName: args.downloadRequest.fileName,
+ networkType: args.downloadRequest.networkType,
+ httpHeader: args.downloadRequest.httpHeader,
+ downloadId: nextDownloadId()
+ };
+
+ if (args.downloadCallback) {
+ this.setListener(nativeParam.downloadId, args.downloadCallback);
+ }
-DownloadManager.prototype.cancel = function(downloadId) {
- var args = validator_.validateArgs(arguments, [
- {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
- ]);
+ var result = native_.callSync('DownloadManagerStart', nativeParam);
- var nativeParam = {
- 'downloadId': args.downloadId
- };
+ if (native_.isFailure(result)) {
+ if ('NetworkError' === result.error.name) {
+ return -1;
+ }
+ throw native_.getErrorObject(result);
+ }
- if (typeof requests[downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
+ requests[nativeParam.downloadId] = args.downloadRequest;
- try {
- var syncResult = callNative('DownloadManager_cancel', nativeParam);
- } catch (e) {
- throw e;
- }
+ return nativeParam.downloadId;
};
-DownloadManager.prototype.pause = function(downloadId) {
- var args = validator_.validateArgs(arguments, [
- {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
+DownloadManager.prototype.cancel = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
- var nativeParam = {
- 'downloadId': args.downloadId
- };
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
- if (typeof requests[downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
- try {
- var syncResult = callNative('DownloadManager_pause', nativeParam);
- } catch (e) {
- throw e;
- }
+ var result = native_.callSync('DownloadManagerCancel', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
};
-DownloadManager.prototype.resume = function(downloadId) {
- var args = validator_.validateArgs(arguments, [
- {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
+DownloadManager.prototype.pause = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
- var nativeParam = {
- 'downloadId': args.downloadId
- };
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
- if (typeof requests[downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
- try {
- var syncResult = callNative('DownloadManager_resume', nativeParam);
- } catch (e) {
- throw e;
- }
+ var result = native_.callSync('DownloadManagerPause', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
};
-DownloadManager.prototype.getState = function(downloadId) {
- var args = validator_.validateArgs(arguments, [
- {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
+DownloadManager.prototype.abandon = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
- var nativeParam = {
- 'downloadId': args.downloadId
- };
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
- if (typeof requests[downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
- try {
- var syncResult = callNative('DownloadManager_getState', nativeParam);
- } catch (e) {
- throw e;
- }
+ var result = native_.callSync('DownloadManagerAbandon', nativeParam);
- return syncResult;
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+
+ delete callbacks[args.downloadId];
};
-DownloadManager.prototype.getDownloadRequest = function(downloadId) {
- var args = validator_.validateArgs(arguments, [
- {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
+DownloadManager.prototype.resume = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
- if (typeof requests[downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
- return requests[args.downloadId];
+ var result = native_.callSync('DownloadManagerResume', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
};
-DownloadManager.prototype.getMIMEType = function(downloadId) {
- var args = validator_.validateArgs(arguments, [
- {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
+DownloadManager.prototype.getState = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
- var nativeParam = {
- 'downloadId': args.downloadId
- };
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
- if (typeof requests[downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
- try {
- var syncResult = callNative('DownloadManager_getMIMEType', nativeParam);
- } catch (e) {
- throw e;
- }
+ var result = native_.callSync('DownloadManagerGetState', nativeParam);
- return syncResult;
+ if (native_.isSuccess(result)) {
+ return native_.getResultObject(result);
+ } else {
+ throw native_.getErrorObject(result);
+ }
};
-DownloadManager.prototype.setListener = function(downloadId, downloadCallback) {
- var args = validator_.validateArgs(arguments, [
- {'name' : 'downloadId', 'type': types_.LONG},
- {'name' : 'downloadCallback', 'type': types_.LISTENER,
- 'values' : ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']}
- ]);
+DownloadManager.prototype.getDownloadRequest = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
- callbacks[args.downloadId] = args.downloadCallback;
+ return requests[args.downloadId];
};
+DownloadManager.prototype.getMIMEType = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
-exports = new DownloadManager();
+ if (typeof requests[args.downloadId] === 'undefined')
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'the identifier does not match any download operation in progress'
+ );
+
+ var result = native_.callSync('DownloadManagerGetMimeType', nativeParam);
+
+ if (native_.isSuccess(result)) {
+ return native_.getResultObject(result);
+ } else {
+ throw native_.getErrorObject(result);
+ }
+};
+DownloadManager.prototype.setListener = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG },
+ {
+ name: 'downloadCallback',
+ type: types_.LISTENER,
+ values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']
+ }
+ ]);
+
+ if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
+ native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
+ }
+ callbacks[args.downloadId] = args.downloadCallback;
+};
+
+exports = new DownloadManager();