Refractor in completed event in DownloadItem.

* Rename `completed` to `done`, making it align with Chromium's style.
* Add 'state' in `done` event's result. It can check the download item final
status: cancelled, completed, interrupted.
This commit is contained in:
Haojian Wu 2015-09-21 13:45:42 +08:00
parent 997ac91fe0
commit 57bf0cb615
2 changed files with 40 additions and 5 deletions

View file

@ -9,6 +9,32 @@
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
namespace mate {
template<>
struct Converter<content::DownloadItem::DownloadState> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
content::DownloadItem::DownloadState state) {
std::string download_state;
switch (state) {
case content::DownloadItem::COMPLETE:
download_state = "completed";
break;
case content::DownloadItem::CANCELLED:
download_state = "cancelled";
break;
case content::DownloadItem::INTERRUPTED:
download_state = "interrputed";
break;
default:
break;
}
return ConvertToV8(isolate, download_state);
}
};
} // namespace mate
namespace atom { namespace atom {
namespace api { namespace api {
@ -29,8 +55,10 @@ DownloadItem::~DownloadItem() {
} }
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
if (download_item_ == item) if (download_item_ == item) {
download_item_->IsDone() ? Emit("completed") : Emit("updated"); download_item_->IsDone() ?
Emit("done", item->GetState()) : Emit("updated");
}
} }
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) { void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) {

View file

@ -16,8 +16,8 @@ win.webContents.session.on('will-download', function(event, item, webContents) {
item.on('updated', function() { item.on('updated', function() {
console.log('Recived bytes: ' + item.getReceiveBytes()); console.log('Recived bytes: ' + item.getReceiveBytes());
}); });
item.on('completed', function() { item.on('done', function(e, state) {
if (item.getReceiveBytes() >= item.getTotalBytes()) { if (state == "completed") {
console.log("Download successfully"); console.log("Download successfully");
} else { } else {
console.log("Download is cancelled or interrupted that can't be resumed"); console.log("Download is cancelled or interrupted that can't be resumed");
@ -32,7 +32,13 @@ win.webContents.session.on('will-download', function(event, item, webContents) {
Emits when the `downloadItem` gets updated. Emits when the `downloadItem` gets updated.
### Event: 'completed' ### Event: 'done'
* `event` Event
* `state` String
* `completed` - The download completed successfully.
* `cancelled` - The download has been cancelled.
* `interrupted` - An error broke the connection with the file server.
Emits when the download is in a terminal state. This includes a completed Emits when the download is in a terminal state. This includes a completed
download, a cancelled download(via `downloadItem.cancel()`), and interrputed download, a cancelled download(via `downloadItem.cancel()`), and interrputed
@ -77,6 +83,7 @@ dialog, the actual name of saved file will be different with the suggested one.
### `downloadItem.getTotalBytes()` ### `downloadItem.getTotalBytes()`
Returns a `Integer` represents the total size in bytes of the download item. Returns a `Integer` represents the total size in bytes of the download item.
If the size is unknown, it returns 0.
### `downloadItem.getReceivedBytes()` ### `downloadItem.getReceivedBytes()`