* 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 (!callback) {
- console.logd('Ignoring unknown callback: ' + result.callbackId);
- return;
- }
-
- if (result.status == 'progress') {
- if (callback.onprogress) {
- var receivedSize = result.receivedSize;
- var totalSize = result.totalSize;
- callback.onprogress(result.callbackId, receivedSize, totalSize);
- }
- }
- else if (result.status == 'paused') {
- if (callback.onpaused) {
- callback.onpaused(result.callbackId);
- }
- }
- else if (result.status == 'canceled') {
- if (callback.oncanceled) {
- callback.oncanceled(result.callbackId);
- }
- }
- else if (result.status == 'completed') {
- if (callback.oncompleted) {
- var fullPath = result.fullPath;
- callback.oncompleted(result.callbackId, fullPath);
- }
- }
- else if (result.status == 'error') {
- if (callback.onfailed) {
- callback.onfailed(result.callbackId,
- new WebAPIException(result.error));
+ if (!callback) {
+ privUtils_.log('Ignoring unknown callback: ' + result.downloadId);
+ return;
}
- }
-});
-
-function nextCallbackId() {
- return ++callbackId;
+ 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 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;
- }
- return true;
- } else if (result.status == 'error') {
- var err = result.error;
- if (err) {
- throw new WebAPIException(err);
- }
- return false;
- }
-}
-
-
-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 url_ = url;
- 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_ = 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() {
- xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.DOWNLOAD);
-
- 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 {
- callNative('DownloadManager_start', nativeParam);
- } catch (e) {
- if ('NetworkError' === e.name) {
- return -1;
+ 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);
+ }
+
+ var result = native_.callSync('DownloadManagerStart', nativeParam);
+
+ if (native_.isFailure(result)) {
+ if ('NetworkError' === result.error.name) {
+ return -1;
+ }
+ throw native_.getErrorObject(result);
}
- throw e;
- }
- requests[nativeParam.callbackId] = args.downloadRequest;
+ requests[nativeParam.downloadId] = args.downloadRequest;
- return nativeParam.callbackId;
+ return nativeParam.downloadId;
};
DownloadManager.prototype.cancel = function() {
- var args = validator_.validateArgs(arguments, [
- {name: 'downloadId', type: types_.LONG, 'nullable': false, 'optional': false}
- ]);
-
- var nativeParam = {
- 'downloadId': args.downloadId
- };
-
- if (typeof requests[args.downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
-
- try {
- callNative('DownloadManager_cancel', nativeParam);
- } catch (e) {
- throw e;
- }
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
+
+ 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('DownloadManagerCancel', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
};
DownloadManager.prototype.pause = function() {
- var args = validator_.validateArgs(arguments, [
- {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
-
- var nativeParam = {
- 'downloadId': args.downloadId
- };
-
- if (typeof requests[args.downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
-
- try {
- callNative('DownloadManager_pause', nativeParam);
- } catch (e) {
- throw e;
- }
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
+
+ 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('DownloadManagerPause', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+DownloadManager.prototype.abandon = function() {
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
+
+ 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('DownloadManagerAbandon', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+
+ delete callbacks[args.downloadId];
};
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[args.downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
-
- try {
- callNative('DownloadManager_resume', nativeParam);
- } catch (e) {
- throw e;
- }
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
+
+ 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('DownloadManagerResume', nativeParam);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
};
DownloadManager.prototype.getState = function() {
- var args = validator_.validateArgs(arguments, [
- {'name' : 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
-
- var nativeParam = {
- 'downloadId': args.downloadId
- };
-
- if (typeof requests[args.downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
-
- try {
- return callNative('DownloadManager_getState', nativeParam);
- } catch (e) {
- throw e;
- }
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
+
+ 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('DownloadManagerGetState', nativeParam);
+
+ if (native_.isSuccess(result)) {
+ return native_.getResultObject(result);
+ } else {
+ throw native_.getErrorObject(result);
+ }
};
DownloadManager.prototype.getDownloadRequest = function() {
- var args = validator_.validateArgs(arguments, [
- {'name': 'downloadId', 'type': types_.LONG, 'nullable': false, 'optional': false}
- ]);
+ 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');
+ 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];
+ 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
- };
-
- if (typeof requests[args.downloadId] === 'undefined')
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'the identifier does not match any download operation in progress');
-
- try {
- return callNative('DownloadManager_getMIMEType', nativeParam);
- } catch (e) {
- throw e;
- }
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG, nullable: false, optional: false }
+ ]);
+
+ var nativeParam = {
+ downloadId: args.downloadId
+ };
+
+ 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']}
- ]);
+ var args = validator_.validateArgs(arguments, [
+ { name: 'downloadId', type: types_.LONG },
+ {
+ name: 'downloadCallback',
+ type: types_.LISTENER,
+ values: ['onprogress', 'onpaused', 'oncanceled', 'oncompleted', 'onfailed']
+ }
+ ]);
- callbacks[args.downloadId] = args.downloadCallback;
+ if (!native_.isListenerSet(DOWNLOAD_MANAGER_LISTENER_ID)) {
+ native_.addListener(DOWNLOAD_MANAGER_LISTENER_ID, DownloadManagerChangeCallback);
+ }
+ callbacks[args.downloadId] = args.downloadCallback;
};
-
-
exports = new DownloadManager();
-