Redefine 'will-download' design.

This commit is contained in:
Haojian Wu 2015-09-24 15:55:45 +08:00
parent 5ef9c7e1a1
commit 0861d5d44b
11 changed files with 149 additions and 101 deletions

View file

@ -4,9 +4,13 @@
#include "atom/browser/api/atom_api_download_item.h"
#include <map>
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/node_includes.h"
#include "base/memory/linked_ptr.h"
#include "native_mate/dictionary.h"
namespace mate {
@ -43,27 +47,36 @@ namespace {
// The wrapDownloadItem funtion which is implemented in JavaScript
using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>;
WrapDownloadItemCallback g_wrap_download_item;
char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey";
std::map<uint32, linked_ptr<v8::Global<v8::Value>>> g_download_item_objects;
} // namespace
DownloadItem::SavePathData::SavePathData(const base::FilePath& path) :
path_(path) {
}
const base::FilePath& DownloadItem::SavePathData::path() {
return path_;
}
DownloadItem::DownloadItem(content::DownloadItem* download_item) :
download_item_(download_item) {
download_item_->AddObserver(this);
}
DownloadItem::~DownloadItem() {
download_item_->RemoveObserver(this);
Destroy();
}
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
if (download_item_ == item) {
download_item_->IsDone() ?
Emit("done", item->GetState()) : Emit("updated");
}
}
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) {
if (download_item_ == download) {
void DownloadItem::Destroy() {
if (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);
download_item_ = nullptr;
}
}
@ -71,8 +84,12 @@ bool DownloadItem::IsDestroyed() const {
return download_item_ == nullptr;
}
void DownloadItem::Destroy() {
download_item_ = nullptr;
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
download_item_->IsDone() ? Emit("done", item->GetState()) : Emit("updated");
}
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) {
Destroy();
}
int64 DownloadItem::GetReceivedBytes() {
@ -83,7 +100,7 @@ int64 DownloadItem::GetTotalBytes() {
return download_item_->GetTotalBytes();
}
const GURL& DownloadItem::GetURL() {
const GURL& DownloadItem::GetUrl() {
return download_item_->GetURL();
}
@ -103,6 +120,10 @@ std::string DownloadItem::GetContentDisposition() {
return download_item_->GetContentDisposition();
}
void DownloadItem::SetSavePath(const base::FilePath& path) {
download_item_->SetUserData(UserDataKey(), new SavePathData(path));
}
void DownloadItem::Pause() {
download_item_->Pause();
}
@ -121,13 +142,14 @@ mate::ObjectTemplateBuilder DownloadItem::GetObjectTemplateBuilder(
.SetMethod("pause", &DownloadItem::Pause)
.SetMethod("resume", &DownloadItem::Resume)
.SetMethod("cancel", &DownloadItem::Cancel)
.SetMethod("getReceiveBytes", &DownloadItem::GetReceivedBytes)
.SetMethod("getReceivedBytes", &DownloadItem::GetReceivedBytes)
.SetMethod("getTotalBytes", &DownloadItem::GetTotalBytes)
.SetMethod("getURL", &DownloadItem::GetURL)
.SetMethod("getUrl", &DownloadItem::GetUrl)
.SetMethod("getMimeType", &DownloadItem::GetMimeType)
.SetMethod("hasUserGesture", &DownloadItem::HasUserGesture)
.SetMethod("getSuggestedFilename", &DownloadItem::GetSuggestedFilename)
.SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition);
.SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition)
.SetMethod("setSavePath", &DownloadItem::SetSavePath);
}
void SetWrapDownloadItem(const WrapDownloadItemCallback& callback) {
@ -138,13 +160,21 @@ void ClearWrapDownloadItem() {
g_wrap_download_item.Reset();
}
// static
mate::Handle<DownloadItem> DownloadItem::Create(
v8::Isolate* isolate, content::DownloadItem* item) {
auto handle = mate::CreateHandle(isolate, new DownloadItem(item));
g_wrap_download_item.Run(handle.ToV8());
g_download_item_objects[item->GetId()] = make_linked_ptr(
new v8::Global<v8::Value>(isolate, handle.ToV8()));
return handle;
}
// static
void* DownloadItem::UserDataKey() {
return &kDownloadItemSavePathKey;
}
} // namespace api
} // namespace atom

View file

@ -8,6 +8,7 @@
#include <string>
#include "atom/browser/api/trackable_object.h"
#include "base/files/file_path.h"
#include "content/public/browser/download_item.h"
#include "native_mate/handle.h"
#include "url/gurl.h"
@ -16,9 +17,17 @@ namespace atom {
namespace api {
class DownloadItem : public mate::TrackableObject<DownloadItem>,
class DownloadItem : public mate::EventEmitter,
public content::DownloadItem::Observer {
public:
class SavePathData : public base::SupportsUserData::Data {
public:
explicit SavePathData(const base::FilePath& path);
const base::FilePath& path();
private:
base::FilePath path_;
};
explicit DownloadItem(content::DownloadItem* download_item);
~DownloadItem();
static mate::Handle<DownloadItem> Create(v8::Isolate* isolate,
@ -37,16 +46,17 @@ class DownloadItem : public mate::TrackableObject<DownloadItem>,
bool HasUserGesture();
std::string GetSuggestedFilename();
std::string GetContentDisposition();
const GURL& GetURL();
const GURL& GetUrl();
void SetSavePath(const base::FilePath& path);
static void* UserDataKey();
private:
// mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
bool IsDestroyed() const override;
// mate::TrackableObject:
void Destroy() override;
void Destroy();
content::DownloadItem* download_item_;

View file

@ -10,7 +10,6 @@
#include "atom/browser/api/atom_api_cookies.h"
#include "atom/browser/api/atom_api_download_item.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_download_manager_delegate.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
@ -295,13 +294,6 @@ void Session::SetDownloadPath(const base::FilePath& path) {
prefs::kDownloadDefaultDirectory, path);
}
void Session::SetOpenDownloadDialog(bool open_download_dialog) {
AtomDownloadManagerDelegate* delegate =
static_cast<AtomDownloadManagerDelegate*>(
browser_context()->GetDownloadManagerDelegate());
delegate->SetOpenDownloadDialog(open_download_dialog);
}
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
if (cookies_.IsEmpty()) {
auto handle = atom::api::Cookies::Create(isolate, browser_context());
@ -318,7 +310,6 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
.SetMethod("clearStorageData", &Session::ClearStorageData)
.SetMethod("setProxy", &Session::SetProxy)
.SetMethod("setDownloadPath", &Session::SetDownloadPath)
.SetMethod("setOpenDownloadDialog", &Session::SetOpenDownloadDialog)
.SetProperty("cookies", &Session::Cookies);
}

View file

@ -43,8 +43,6 @@ class Session: public mate::TrackableObject<Session>,
AtomBrowserContext* browser_context() const { return browser_context_.get(); }
void SetOpenDownloadDialog(bool open_download_dialog);
protected:
explicit Session(AtomBrowserContext* browser_context);
~Session();

View file

@ -15,7 +15,7 @@ wrapDownloadItem = (download_item) ->
# download_item is an Event Emitter.
download_item.__proto__ = EventEmitter.prototype
# Be compatible with old APIs.
download_item.url = download_item.getURL()
download_item.url = download_item.getUrl()
download_item.filename = download_item.getSuggestedFilename()
download_item.mimeType = download_item.getMimeType()
download_item.hasUserGesture = download_item.hasUserGesture()