From f1f91828ba051c1548b7d67ac55fd3fac3a33231 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 2 Feb 2016 18:49:43 +0800 Subject: [PATCH] Rely on OnDownloadDestroyed to destroy downloadItem --- atom/browser/api/atom_api_download_item.cc | 16 +++------------- atom/browser/api/atom_api_download_item.h | 2 -- atom/browser/api/atom_api_session.cc | 4 +--- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/atom/browser/api/atom_api_download_item.cc b/atom/browser/api/atom_api_download_item.cc index c3beee5bb094..bb1c58f49b8a 100644 --- a/atom/browser/api/atom_api_download_item.cc +++ b/atom/browser/api/atom_api_download_item.cc @@ -12,7 +12,6 @@ #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/node_includes.h" #include "base/memory/linked_ptr.h" -#include "base/message_loop/message_loop.h" #include "base/strings/utf_string_conversions.h" #include "native_mate/dictionary.h" #include "net/base/filename_util.h" @@ -71,17 +70,6 @@ DownloadItem::DownloadItem(content::DownloadItem* download_item) : } DownloadItem::~DownloadItem() { - Destroy(download_item_); -} - -void DownloadItem::Destroy(content::DownloadItem* item) { - if (item) { - OnDownloadDestroyed(item); - MarkDestroyed(); - - // Destroy the native class in next tick. - base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure()); - } } void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { @@ -93,7 +81,9 @@ void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) { auto iter = g_download_item_objects.find(download_item->GetId()); if (iter != g_download_item_objects.end()) g_download_item_objects.erase(iter); - download_item_ = nullptr; + + // Destroy the wrapper once the download_item is destroyed. + delete this; } int64 DownloadItem::GetReceivedBytes() { diff --git a/atom/browser/api/atom_api_download_item.h b/atom/browser/api/atom_api_download_item.h index af388a567513..471913c2266a 100644 --- a/atom/browser/api/atom_api_download_item.h +++ b/atom/browser/api/atom_api_download_item.h @@ -36,8 +36,6 @@ class DownloadItem : public mate::TrackableObject, static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); - void Destroy(content::DownloadItem* download_item); - protected: explicit DownloadItem(content::DownloadItem* download_item); ~DownloadItem(); diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 04ab7a9f510e..e5c5198f0341 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -306,14 +306,12 @@ void Session::OnDownloadCreated(content::DownloadManager* manager, auto web_contents = item->GetWebContents(); if (SavePageHandler::IsSavePageTypes(item->GetMimeType())) return; - auto download_item_handle = DownloadItem::Create(isolate(), item); bool prevent_default = Emit( "will-download", - download_item_handle, + DownloadItem::Create(isolate(), item), api::WebContents::CreateFrom(isolate(), web_contents)); if (prevent_default) { item->Cancel(true); - download_item_handle->Destroy(item); item->Remove(); } }