From 12d311fd29d3551b754fcbd2c023637baf5488f7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 2 Feb 2016 19:51:15 +0800 Subject: [PATCH] Gracefully destroy downloadItem --- atom/browser/api/atom_api_download_item.cc | 27 ++++++++++++++++++--------- vendor/native_mate | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/atom/browser/api/atom_api_download_item.cc b/atom/browser/api/atom_api_download_item.cc index b0bcd5e..d8e87f2 100644 --- a/atom/browser/api/atom_api_download_item.cc +++ b/atom/browser/api/atom_api_download_item.cc @@ -48,6 +48,7 @@ namespace atom { namespace api { namespace { + // The wrapDownloadItem funtion which is implemented in JavaScript using WrapDownloadItemCallback = base::Callback)>; WrapDownloadItemCallback g_wrap_download_item; @@ -55,6 +56,7 @@ WrapDownloadItemCallback g_wrap_download_item; char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey"; std::map>> g_download_item_objects; + } // namespace DownloadItem::SavePathData::SavePathData(const base::FilePath& path) : @@ -73,6 +75,16 @@ DownloadItem::DownloadItem(content::DownloadItem* download_item) DownloadItem::~DownloadItem() { LOG(ERROR) << "~DownloadItem"; + if (download_item_) { + // Destroyed by either garbage collection or destroy(). + download_item_->RemoveObserver(this); + download_item_->Remove(); + } + + // Remove from the global map. + auto iter = g_download_item_objects.find(weak_map_id()); + if (iter != g_download_item_objects.end()) + g_download_item_objects.erase(iter); } void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { @@ -80,13 +92,9 @@ void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { } void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) { - download_item->RemoveObserver(this); - auto iter = g_download_item_objects.find(download_item->GetId()); - if (iter != g_download_item_objects.end()) - g_download_item_objects.erase(iter); - - // Destroy the native class in next tick. - base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure()); + download_item_ = nullptr; + // Destroy the native class immediately when downloadItem is destroyed. + delete this; } int64 DownloadItem::GetReceivedBytes() { @@ -135,7 +143,6 @@ void DownloadItem::Resume() { } void DownloadItem::Cancel() { - MarkDestroyed(); download_item_->Cancel(true); download_item_->Remove(); } @@ -167,7 +174,9 @@ mate::Handle DownloadItem::Create( auto handle = mate::CreateHandle(isolate, new DownloadItem(item)); g_wrap_download_item.Run(handle.ToV8()); - g_download_item_objects[item->GetId()] = make_linked_ptr( + + // Reference this object in case it got garbage collected. + g_download_item_objects[handle->weak_map_id()] = make_linked_ptr( new v8::Global(isolate, handle.ToV8())); return handle; } diff --git a/vendor/native_mate b/vendor/native_mate index 26520c5..e719eab 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit 26520c5cf4b6a60da2c5cba971393f94b82f5939 +Subproject commit e719eab878c264bb03188d0cd6eb9ad6882bc13a -- 2.7.4