} else if (result.status == 'canceled') {
if (callback.oncanceled) {
callback.oncanceled(result.downloadId);
- delete callbacks[result.downloadId];
}
} else if (result.status == 'completed') {
if (callback.oncompleted) {
} else if (result.status == 'error') {
if (callback.onfailed) {
callback.onfailed(result.downloadId, new WebAPIException(result.error));
- delete callbacks[result.downloadId];
}
}
}, 0);
PAUSED: 'PAUSED',
CANCELED: 'CANCELED',
COMPLETED: 'COMPLETED',
- FAILED: 'FAILED'
+ FAILED: 'FAILED',
+ ABANDONED: 'ABANDONED'
};
var DownloadNetworkType = {
}
};
+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('DownloadManager_abandon', 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 }
using std::placeholders::_1;
using std::placeholders::_2;
#define REGISTER_SYNC(c, x) RegisterSyncHandler(c, std::bind(&DownloadInstance::x, this, _1, _2));
- REGISTER_SYNC("DownloadManager_pause", DownloadManagerPause);
REGISTER_SYNC("DownloadManager_getMIMEType", DownloadManagerGetmimetype);
REGISTER_SYNC("DownloadManager_start", DownloadManagerStart);
REGISTER_SYNC("DownloadManager_cancel", DownloadManagerCancel);
+ REGISTER_SYNC("DownloadManager_pause", DownloadManagerPause);
+ REGISTER_SYNC("DownloadManager_abandon", DownloadManagerAbandon);
REGISTER_SYNC("DownloadManager_resume", DownloadManagerResume);
REGISTER_SYNC("DownloadManager_getState", DownloadManagerGetstate);
#undef REGISTER_SYNC
LoggerE("%s", get_error_message(ret));
}
- ret = download_destroy(di_ptr->native_download_id);
- if (ret != DOWNLOAD_ERROR_NONE) {
- LoggerE("%s", get_error_message(ret));
- }
-
picojson::value::object out;
out["status"] = picojson::value("canceled");
out["downloadId"] = picojson::value(static_cast<double>(download_id));
LoggerE("%s", get_error_message(ret));
}
- ret = download_destroy(di_ptr->native_download_id);
- if (DOWNLOAD_ERROR_NONE != ret) {
- LoggerE("%s", get_error_message(ret));
- }
-
out["downloadId"] = picojson::value(static_cast<double>(down_cb_ptr->download_id));
out["listenerId"] = picojson::value(kDownloadManagerListenerId);
if (!GetDownloadID(callbackId, downloadId)) {
LogAndReportError(
- common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
+ common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
"The identifier does not match any download operation in progress"),
- &out, ("The identifier %d does not match any download operation in progress", downloadId));
+ &out, ("downloadId: %d, found in WEB Api but missing in native API", callbackId));
return;
}
if (!GetDownloadID(callbackId, downloadId)) {
LogAndReportError(
- common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
+ common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
"The identifier does not match any download operation in progress"),
- &out, ("The identifier %d does not match any download operation in progress", downloadId));
+ &out, ("downloadId: %d, found in WEB Api but missing in native API", downloadId));
return;
}
}
}
+void DownloadInstance::DownloadManagerAbandon(const picojson::value& args, picojson::object& out) {
+ ScopeLogger();
+ CHECK_EXIST(args, "downloadId", out)
+ int callbackId, nativeDownloadId, ret;
+ callbackId = static_cast<int>(args.get("downloadId").get<double>());
+
+ if (!GetDownloadID(callbackId, nativeDownloadId)) {
+ LogAndReportError(
+ common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
+ "The identifier does not match any download operation in progress"),
+ &out, ("downloadId: %d, found in WEB Api but missing in native API", callbackId));
+ return;
+ }
+
+ ret = download_destroy(nativeDownloadId);
+ if (ret == DOWNLOAD_ERROR_NONE) {
+ if (di_map.find(callbackId) != di_map.end()) {
+ di_map.find(callbackId)->second->abandoned = true;
+ }
+ ReportSuccess(out);
+ } else {
+ LogAndReportError(convertError(ret), &out,
+ ("download_destroy error: %d (%s)", ret, get_error_message(ret)));
+ }
+}
+
void DownloadInstance::DownloadManagerResume(const picojson::value& args, picojson::object& out) {
ScopeLogger();
CHECK_EXIST(args, "downloadId", out)
- int downloadId, ret;
+ int nativeDownloadId, ret;
int callbackId = static_cast<int>(args.get("downloadId").get<double>());
- if (!GetDownloadID(callbackId, downloadId)) {
+ if ((di_map.end() != di_map.find(callbackId)) && (di_map.find(callbackId)->second->abandoned)) {
LogAndReportError(
- common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
+ common::PlatformResult(common::ErrorCode::INVALID_VALUES_ERR,
+ "The download operation with given identifier has been abandoned "
+ "and can not be resumed"),
+ &out,
+ ("The download operation with identifier: %d has been abandoned and can not be resumed",
+ callbackId));
+ return;
+ }
+
+ if (!GetDownloadID(callbackId, nativeDownloadId)) {
+ LogAndReportError(
+ common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
"The identifier does not match any download operation in progress"),
- &out, ("The identifier %d does not match any download operation in progress", downloadId));
+ &out, ("downloadId: %d, found in WEB Api but missing in native API", callbackId));
+ return;
+ }
+
+ download_state_e state;
+ ret = download_get_state(nativeDownloadId, &state);
+
+ if (ret == DOWNLOAD_ERROR_NONE) {
+ switch (state) {
+ case DOWNLOAD_STATE_QUEUED:
+ case DOWNLOAD_STATE_DOWNLOADING:
+ case DOWNLOAD_STATE_COMPLETED:
+ ReportSuccess(out);
+ return;
+ default:
+ LoggerD("Download state: %d", state);
+ }
+ }
+
+ ret = download_set_state_changed_cb(nativeDownloadId, OnStateChanged,
+ static_cast<void*>(download_callbacks[callbackId]));
+ if (ret != DOWNLOAD_ERROR_NONE) {
+ LogAndReportError(convertError(ret), &out, ("download_set_state_changed_cb error: %d (%s)", ret,
+ get_error_message(ret)));
+ return;
+ }
+
+ ret = download_set_progress_cb(nativeDownloadId, progress_changed_cb,
+ static_cast<void*>(download_callbacks[callbackId]));
+ if (ret != DOWNLOAD_ERROR_NONE) {
+ LogAndReportError(convertError(ret), &out,
+ ("download_set_progress_cb error: %d (%s)", ret, get_error_message(ret)));
return;
}
- ret = download_start(downloadId);
+ ret = download_start(nativeDownloadId);
if (ret == DOWNLOAD_ERROR_NONE) {
ReportSuccess(out);
int callbackId = static_cast<int>(args.get("downloadId").get<double>());
+ if (di_map.find(callbackId)->second->abandoned) {
+ stateValue = "ABANDONED";
+ ReportSuccess(picojson::value(stateValue), out);
+ return;
+ }
+
if (!GetDownloadID(callbackId, downloadId)) {
LogAndReportError(
common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,