only emit download-item done event once.

This commit is contained in:
liusi 2017-02-14 14:39:33 +08:00
parent d3c967401d
commit d56c1909df
2 changed files with 7 additions and 4 deletions

View file

@ -58,7 +58,7 @@ std::map<uint32_t, v8::Global<v8::Object>> 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());
}
}

View file

@ -59,6 +59,7 @@ class DownloadItem : public mate::TrackableObject<DownloadItem>,
private:
base::FilePath save_path_;
content::DownloadItem* download_item_;
bool done_emitted_;
DISALLOW_COPY_AND_ASSIGN(DownloadItem);
};