Gracefully destroy downloadItem
This commit is contained in:
parent
65cf85808f
commit
12d311fd29
2 changed files with 19 additions and 10 deletions
|
@ -48,6 +48,7 @@ namespace atom {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// The wrapDownloadItem funtion which is implemented in JavaScript
|
// The wrapDownloadItem funtion which is implemented in JavaScript
|
||||||
using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
||||||
WrapDownloadItemCallback g_wrap_download_item;
|
WrapDownloadItemCallback g_wrap_download_item;
|
||||||
|
@ -55,6 +56,7 @@ WrapDownloadItemCallback g_wrap_download_item;
|
||||||
char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey";
|
char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey";
|
||||||
|
|
||||||
std::map<uint32, linked_ptr<v8::Global<v8::Value>>> g_download_item_objects;
|
std::map<uint32, linked_ptr<v8::Global<v8::Value>>> g_download_item_objects;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
DownloadItem::SavePathData::SavePathData(const base::FilePath& path) :
|
DownloadItem::SavePathData::SavePathData(const base::FilePath& path) :
|
||||||
|
@ -73,6 +75,16 @@ DownloadItem::DownloadItem(content::DownloadItem* download_item)
|
||||||
|
|
||||||
DownloadItem::~DownloadItem() {
|
DownloadItem::~DownloadItem() {
|
||||||
LOG(ERROR) << "~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) {
|
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
|
||||||
|
@ -80,13 +92,9 @@ void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) {
|
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) {
|
||||||
download_item->RemoveObserver(this);
|
download_item_ = nullptr;
|
||||||
auto iter = g_download_item_objects.find(download_item->GetId());
|
// Destroy the native class immediately when downloadItem is destroyed.
|
||||||
if (iter != g_download_item_objects.end())
|
delete this;
|
||||||
g_download_item_objects.erase(iter);
|
|
||||||
|
|
||||||
// Destroy the native class in next tick.
|
|
||||||
base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64 DownloadItem::GetReceivedBytes() {
|
int64 DownloadItem::GetReceivedBytes() {
|
||||||
|
@ -135,7 +143,6 @@ void DownloadItem::Resume() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadItem::Cancel() {
|
void DownloadItem::Cancel() {
|
||||||
MarkDestroyed();
|
|
||||||
download_item_->Cancel(true);
|
download_item_->Cancel(true);
|
||||||
download_item_->Remove();
|
download_item_->Remove();
|
||||||
}
|
}
|
||||||
|
@ -167,7 +174,9 @@ mate::Handle<DownloadItem> DownloadItem::Create(
|
||||||
|
|
||||||
auto handle = mate::CreateHandle(isolate, new DownloadItem(item));
|
auto handle = mate::CreateHandle(isolate, new DownloadItem(item));
|
||||||
g_wrap_download_item.Run(handle.ToV8());
|
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<v8::Value>(isolate, handle.ToV8()));
|
new v8::Global<v8::Value>(isolate, handle.ToV8()));
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
2
vendor/native_mate
vendored
2
vendor/native_mate
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit 26520c5cf4b6a60da2c5cba971393f94b82f5939
|
Subproject commit e719eab878c264bb03188d0cd6eb9ad6882bc13a
|
Loading…
Add table
Add a link
Reference in a new issue