2015-09-20 10:56:10 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_download_item.h"
|
|
|
|
|
2015-09-24 07:55:45 +00:00
|
|
|
#include <map>
|
|
|
|
|
2015-11-04 08:54:36 +00:00
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2015-09-20 10:56:10 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2015-09-24 07:55:45 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2015-09-20 10:56:10 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
|
|
|
#include "atom/common/node_includes.h"
|
2015-09-24 07:55:45 +00:00
|
|
|
#include "base/memory/linked_ptr.h"
|
2016-02-02 11:05:58 +00:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2015-09-24 08:31:41 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-09-20 10:56:10 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-09-24 08:31:41 +00:00
|
|
|
#include "net/base/filename_util.h"
|
2015-09-20 10:56:10 +00:00
|
|
|
|
2015-09-21 05:45:42 +00:00
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Converter<content::DownloadItem::DownloadState> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
content::DownloadItem::DownloadState state) {
|
|
|
|
std::string download_state;
|
|
|
|
switch (state) {
|
|
|
|
case content::DownloadItem::COMPLETE:
|
|
|
|
download_state = "completed";
|
|
|
|
break;
|
|
|
|
case content::DownloadItem::CANCELLED:
|
|
|
|
download_state = "cancelled";
|
|
|
|
break;
|
|
|
|
case content::DownloadItem::INTERRUPTED:
|
2015-09-25 05:13:11 +00:00
|
|
|
download_state = "interrupted";
|
2015-09-21 05:45:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ConvertToV8(isolate, download_state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2015-09-20 10:56:10 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
namespace {
|
2016-02-02 11:51:15 +00:00
|
|
|
|
2015-09-20 10:56:10 +00:00
|
|
|
// The wrapDownloadItem funtion which is implemented in JavaScript
|
|
|
|
using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
|
|
|
WrapDownloadItemCallback g_wrap_download_item;
|
2015-09-24 07:55:45 +00:00
|
|
|
|
|
|
|
std::map<uint32, linked_ptr<v8::Global<v8::Value>>> g_download_item_objects;
|
2016-02-02 11:51:15 +00:00
|
|
|
|
2015-09-20 10:56:10 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-02-02 11:05:58 +00:00
|
|
|
DownloadItem::DownloadItem(content::DownloadItem* download_item)
|
|
|
|
: download_item_(download_item) {
|
2015-09-20 10:56:10 +00:00
|
|
|
download_item_->AddObserver(this);
|
2016-02-02 11:05:58 +00:00
|
|
|
AttachAsUserData(download_item);
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadItem::~DownloadItem() {
|
2016-02-02 11:51:15 +00:00
|
|
|
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);
|
2015-09-20 11:28:33 +00:00
|
|
|
}
|
2015-09-20 10:56:10 +00:00
|
|
|
|
2015-09-24 07:55:45 +00:00
|
|
|
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
|
2016-02-02 11:54:41 +00:00
|
|
|
if (download_item_->IsDone()) {
|
|
|
|
Emit("done", item->GetState());
|
|
|
|
|
|
|
|
// Destroy the item once item is downloaded.
|
|
|
|
base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure());
|
|
|
|
} else {
|
|
|
|
Emit("updated");
|
|
|
|
}
|
2015-09-24 07:55:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 07:38:43 +00:00
|
|
|
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) {
|
2016-02-02 11:51:15 +00:00
|
|
|
download_item_ = nullptr;
|
|
|
|
// Destroy the native class immediately when downloadItem is destroyed.
|
|
|
|
delete this;
|
2015-09-20 11:28:33 +00:00
|
|
|
}
|
2015-09-20 10:56:10 +00:00
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
void DownloadItem::Pause() {
|
|
|
|
download_item_->Pause();
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
void DownloadItem::Resume() {
|
|
|
|
download_item_->Resume();
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
void DownloadItem::Cancel() {
|
|
|
|
download_item_->Cancel(true);
|
|
|
|
download_item_->Remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 DownloadItem::GetReceivedBytes() const {
|
|
|
|
return download_item_->GetReceivedBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 DownloadItem::GetTotalBytes() const {
|
|
|
|
return download_item_->GetTotalBytes();
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
std::string DownloadItem::GetMimeType() const {
|
2015-09-20 10:56:10 +00:00
|
|
|
return download_item_->GetMimeType();
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
bool DownloadItem::HasUserGesture() const {
|
2015-09-20 10:56:10 +00:00
|
|
|
return download_item_->HasUserGesture();
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
std::string DownloadItem::GetFilename() const {
|
2015-11-13 08:03:40 +00:00
|
|
|
return base::UTF16ToUTF8(net::GenerateFileName(GetURL(),
|
2015-09-24 08:31:41 +00:00
|
|
|
GetContentDisposition(),
|
|
|
|
std::string(),
|
|
|
|
download_item_->GetSuggestedFilename(),
|
|
|
|
GetMimeType(),
|
|
|
|
std::string()).LossyDisplayName());
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
std::string DownloadItem::GetContentDisposition() const {
|
2015-09-21 01:38:38 +00:00
|
|
|
return download_item_->GetContentDisposition();
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
const GURL& DownloadItem::GetURL() const {
|
|
|
|
return download_item_->GetURL();
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
void DownloadItem::SetSavePath(const base::FilePath& path) {
|
|
|
|
save_path_ = path;
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:11:39 +00:00
|
|
|
base::FilePath DownloadItem::GetSavePath() const {
|
|
|
|
return save_path_;
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 08:04:46 +00:00
|
|
|
// static
|
|
|
|
void DownloadItem::BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> prototype) {
|
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype)
|
2015-12-03 07:38:43 +00:00
|
|
|
.MakeDestroyable()
|
2015-09-20 10:56:10 +00:00
|
|
|
.SetMethod("pause", &DownloadItem::Pause)
|
|
|
|
.SetMethod("resume", &DownloadItem::Resume)
|
|
|
|
.SetMethod("cancel", &DownloadItem::Cancel)
|
2015-09-24 07:55:45 +00:00
|
|
|
.SetMethod("getReceivedBytes", &DownloadItem::GetReceivedBytes)
|
2015-09-20 10:56:10 +00:00
|
|
|
.SetMethod("getTotalBytes", &DownloadItem::GetTotalBytes)
|
|
|
|
.SetMethod("getMimeType", &DownloadItem::GetMimeType)
|
|
|
|
.SetMethod("hasUserGesture", &DownloadItem::HasUserGesture)
|
2015-09-24 08:31:41 +00:00
|
|
|
.SetMethod("getFilename", &DownloadItem::GetFilename)
|
2015-09-24 07:55:45 +00:00
|
|
|
.SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition)
|
2016-02-02 12:11:39 +00:00
|
|
|
.SetMethod("getURL", &DownloadItem::GetURL)
|
|
|
|
.SetMethod("setSavePath", &DownloadItem::SetSavePath)
|
|
|
|
.SetMethod("getSavePath", &DownloadItem::GetSavePath);
|
2015-09-20 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:55:45 +00:00
|
|
|
// static
|
2015-09-20 10:56:10 +00:00
|
|
|
mate::Handle<DownloadItem> DownloadItem::Create(
|
|
|
|
v8::Isolate* isolate, content::DownloadItem* item) {
|
2016-02-02 11:05:58 +00:00
|
|
|
auto existing = TrackableObject::FromWrappedClass(isolate, item);
|
|
|
|
if (existing)
|
|
|
|
return mate::CreateHandle(isolate, static_cast<DownloadItem*>(existing));
|
|
|
|
|
2015-09-20 10:56:10 +00:00
|
|
|
auto handle = mate::CreateHandle(isolate, new DownloadItem(item));
|
|
|
|
g_wrap_download_item.Run(handle.ToV8());
|
2016-02-02 11:51:15 +00:00
|
|
|
|
|
|
|
// Reference this object in case it got garbage collected.
|
|
|
|
g_download_item_objects[handle->weak_map_id()] = make_linked_ptr(
|
2015-09-24 07:55:45 +00:00
|
|
|
new v8::Global<v8::Value>(isolate, handle.ToV8()));
|
2015-09-20 10:56:10 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:54:36 +00:00
|
|
|
void ClearWrapDownloadItem() {
|
|
|
|
g_wrap_download_item.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWrapDownloadItem(const WrapDownloadItemCallback& callback) {
|
|
|
|
g_wrap_download_item = callback;
|
|
|
|
|
|
|
|
// Cleanup the wrapper on exit.
|
|
|
|
atom::AtomBrowserMainParts::Get()->RegisterDestructionCallback(
|
|
|
|
base::Bind(ClearWrapDownloadItem));
|
|
|
|
}
|
|
|
|
|
2015-09-20 10:56:10 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.SetMethod("_setWrapDownloadItem", &atom::api::SetWrapDownloadItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_download_item, Initialize);
|