From: Cheng Zhao Date: Tue, 2 Feb 2016 12:11:39 +0000 (+0800) Subject: Get rid of SavePathData X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=50d69fd9bbacf33e69bb489d9d16ee936c06f9f9;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git Get rid of SavePathData --- diff --git a/atom/browser/api/atom_api_download_item.cc b/atom/browser/api/atom_api_download_item.cc index da95b67..5a8befc 100644 --- a/atom/browser/api/atom_api_download_item.cc +++ b/atom/browser/api/atom_api_download_item.cc @@ -53,20 +53,10 @@ namespace { using WrapDownloadItemCallback = base::Callback)>; WrapDownloadItemCallback g_wrap_download_item; -char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey"; - std::map>> 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); @@ -74,7 +64,6 @@ 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); @@ -104,27 +93,36 @@ void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) { delete this; } -int64 DownloadItem::GetReceivedBytes() { - return download_item_->GetReceivedBytes(); +void DownloadItem::Pause() { + download_item_->Pause(); } -int64 DownloadItem::GetTotalBytes() { - return download_item_->GetTotalBytes(); +void DownloadItem::Resume() { + download_item_->Resume(); } -const GURL& DownloadItem::GetURL() { - return download_item_->GetURL(); +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(); } -std::string DownloadItem::GetMimeType() { +std::string DownloadItem::GetMimeType() const { return download_item_->GetMimeType(); } -bool DownloadItem::HasUserGesture() { +bool DownloadItem::HasUserGesture() const { return download_item_->HasUserGesture(); } -std::string DownloadItem::GetFilename() { +std::string DownloadItem::GetFilename() const { return base::UTF16ToUTF8(net::GenerateFileName(GetURL(), GetContentDisposition(), std::string(), @@ -133,25 +131,20 @@ std::string DownloadItem::GetFilename() { std::string()).LossyDisplayName()); } -std::string DownloadItem::GetContentDisposition() { +std::string DownloadItem::GetContentDisposition() const { return download_item_->GetContentDisposition(); } -void DownloadItem::SetSavePath(const base::FilePath& path) { - download_item_->SetUserData(UserDataKey(), new SavePathData(path)); -} - -void DownloadItem::Pause() { - download_item_->Pause(); +const GURL& DownloadItem::GetURL() const { + return download_item_->GetURL(); } -void DownloadItem::Resume() { - download_item_->Resume(); +void DownloadItem::SetSavePath(const base::FilePath& path) { + save_path_ = path; } -void DownloadItem::Cancel() { - download_item_->Cancel(true); - download_item_->Remove(); +base::FilePath DownloadItem::GetSavePath() const { + return save_path_; } // static @@ -164,12 +157,13 @@ void DownloadItem::BuildPrototype(v8::Isolate* isolate, .SetMethod("cancel", &DownloadItem::Cancel) .SetMethod("getReceivedBytes", &DownloadItem::GetReceivedBytes) .SetMethod("getTotalBytes", &DownloadItem::GetTotalBytes) - .SetMethod("getURL", &DownloadItem::GetURL) .SetMethod("getMimeType", &DownloadItem::GetMimeType) .SetMethod("hasUserGesture", &DownloadItem::HasUserGesture) .SetMethod("getFilename", &DownloadItem::GetFilename) .SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition) - .SetMethod("setSavePath", &DownloadItem::SetSavePath); + .SetMethod("getURL", &DownloadItem::GetURL) + .SetMethod("setSavePath", &DownloadItem::SetSavePath) + .SetMethod("getSavePath", &DownloadItem::GetSavePath); } // static @@ -188,11 +182,6 @@ mate::Handle DownloadItem::Create( return handle; } -// static -void* DownloadItem::UserDataKey() { - return &kDownloadItemSavePathKey; -} - void ClearWrapDownloadItem() { g_wrap_download_item.Reset(); } diff --git a/atom/browser/api/atom_api_download_item.h b/atom/browser/api/atom_api_download_item.h index 471913c..5806c01 100644 --- a/atom/browser/api/atom_api_download_item.h +++ b/atom/browser/api/atom_api_download_item.h @@ -20,22 +20,26 @@ namespace api { class DownloadItem : public mate::TrackableObject, 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_; - }; - static mate::Handle Create(v8::Isolate* isolate, content::DownloadItem* item); - static void* UserDataKey(); // mate::TrackableObject: static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); + void Pause(); + void Resume(); + void Cancel(); + int64 GetReceivedBytes() const; + int64 GetTotalBytes() const; + std::string GetMimeType() const; + bool HasUserGesture() const; + std::string GetFilename() const; + std::string GetContentDisposition() const; + const GURL& GetURL() const; + void SetSavePath(const base::FilePath& path); + base::FilePath GetSavePath() const; + protected: explicit DownloadItem(content::DownloadItem* download_item); ~DownloadItem(); @@ -44,19 +48,8 @@ class DownloadItem : public mate::TrackableObject, void OnDownloadUpdated(content::DownloadItem* download) override; void OnDownloadDestroyed(content::DownloadItem* download) override; - void Pause(); - void Resume(); - void Cancel(); - int64 GetReceivedBytes(); - int64 GetTotalBytes(); - std::string GetMimeType(); - bool HasUserGesture(); - std::string GetFilename(); - std::string GetContentDisposition(); - const GURL& GetURL(); - void SetSavePath(const base::FilePath& path); - private: + base::FilePath save_path_; content::DownloadItem* download_item_; DISALLOW_COPY_AND_ASSIGN(DownloadItem); diff --git a/atom/browser/atom_download_manager_delegate.cc b/atom/browser/atom_download_manager_delegate.cc index a5f5cc6..f5bdbbd 100644 --- a/atom/browser/atom_download_manager_delegate.cc +++ b/atom/browser/atom_download_manager_delegate.cc @@ -109,16 +109,24 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget( download->GetForcedFilePath()); return true; } - base::SupportsUserData::Data* save_path = download->GetUserData( - atom::api::DownloadItem::UserDataKey()); - if (save_path) { - const base::FilePath& default_download_path = - static_cast(save_path)->path(); - callback.Run(default_download_path, - content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, - content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, - default_download_path); - return true; + + // Try to get the save path from JS wrapper. + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + api::DownloadItem* download_item = api::DownloadItem::FromWrappedClass( + isolate, download); + if (download_item) { + base::FilePath save_path = download_item->GetSavePath(); + if (!save_path.empty()) { + callback.Run(save_path, + content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, + content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, + save_path); + return true; + } + } } AtomBrowserContext* browser_context = static_cast(