From d56c1909dfdbd3df2f3f0560d154b87fe394692b Mon Sep 17 00:00:00 2001 From: liusi Date: Tue, 14 Feb 2017 14:39:33 +0800 Subject: [PATCH] only emit `download-item done` event once. --- atom/browser/api/atom_api_download_item.cc | 10 ++++++---- atom/browser/api/atom_api_download_item.h | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/atom/browser/api/atom_api_download_item.cc b/atom/browser/api/atom_api_download_item.cc index ccb99b6f60d..80e37ba9659 100644 --- a/atom/browser/api/atom_api_download_item.cc +++ b/atom/browser/api/atom_api_download_item.cc @@ -58,7 +58,7 @@ std::map> g_download_item_objects; DownloadItem::DownloadItem(v8::Isolate* isolate, content::DownloadItem* download_item) - : download_item_(download_item) { + : download_item_(download_item), done_emitted_(false) { download_item_->AddObserver(this); Init(isolate); AttachAsUserData(download_item); @@ -76,14 +76,16 @@ DownloadItem::~DownloadItem() { } void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { - if (download_item_->IsDone()) { + if (!download_item_->IsDone()) { + Emit("updated", item->GetState()); + + } else if (!done_emitted_) { Emit("done", item->GetState()); + done_emitted_ = true; // Destroy the item once item is downloaded. base::ThreadTaskRunnerHandle::Get()->PostTask( FROM_HERE, GetDestroyClosure()); - } else { - Emit("updated", item->GetState()); } } diff --git a/atom/browser/api/atom_api_download_item.h b/atom/browser/api/atom_api_download_item.h index fbc74b1c818..8840a4cff42 100644 --- a/atom/browser/api/atom_api_download_item.h +++ b/atom/browser/api/atom_api_download_item.h @@ -59,6 +59,7 @@ class DownloadItem : public mate::TrackableObject, private: base::FilePath save_path_; content::DownloadItem* download_item_; + bool done_emitted_; DISALLOW_COPY_AND_ASSIGN(DownloadItem); };