Move download_item.h from content/public to components/download

https://chromium-review.googlesource.com/907687
This commit is contained in:
Aleksei Kuzmin 2018-04-10 17:29:26 +02:00 committed by Samuel Attard
parent a315d6330c
commit 4e580d5b39
9 changed files with 58 additions and 51 deletions

View file

@ -20,21 +20,22 @@
namespace mate { namespace mate {
template <> template <>
struct Converter<content::DownloadItem::DownloadState> { struct Converter<download::DownloadItem::DownloadState> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, static v8::Local<v8::Value> ToV8(
content::DownloadItem::DownloadState state) { v8::Isolate* isolate,
download::DownloadItem::DownloadState state) {
std::string download_state; std::string download_state;
switch (state) { switch (state) {
case content::DownloadItem::IN_PROGRESS: case download::DownloadItem::IN_PROGRESS:
download_state = "progressing"; download_state = "progressing";
break; break;
case content::DownloadItem::COMPLETE: case download::DownloadItem::COMPLETE:
download_state = "completed"; download_state = "completed";
break; break;
case content::DownloadItem::CANCELLED: case download::DownloadItem::CANCELLED:
download_state = "cancelled"; download_state = "cancelled";
break; break;
case content::DownloadItem::INTERRUPTED: case download::DownloadItem::INTERRUPTED:
download_state = "interrupted"; download_state = "interrupted";
break; break;
default: default:
@ -57,7 +58,7 @@ std::map<uint32_t, v8::Global<v8::Object>> g_download_item_objects;
} // namespace } // namespace
DownloadItem::DownloadItem(v8::Isolate* isolate, DownloadItem::DownloadItem(v8::Isolate* isolate,
content::DownloadItem* download_item) download::DownloadItem* download_item)
: download_item_(download_item) { : download_item_(download_item) {
download_item_->AddObserver(this); download_item_->AddObserver(this);
Init(isolate); Init(isolate);
@ -75,7 +76,7 @@ DownloadItem::~DownloadItem() {
g_download_item_objects.erase(weak_map_id()); g_download_item_objects.erase(weak_map_id());
} }
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { void DownloadItem::OnDownloadUpdated(download::DownloadItem* item) {
if (download_item_->IsDone()) { if (download_item_->IsDone()) {
Emit("done", item->GetState()); Emit("done", item->GetState());
// Destroy the item once item is downloaded. // Destroy the item once item is downloaded.
@ -86,7 +87,7 @@ void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
} }
} }
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) { void DownloadItem::OnDownloadDestroyed(download::DownloadItem* download_item) {
download_item_ = nullptr; download_item_ = nullptr;
// Destroy the native class immediately when downloadItem is destroyed. // Destroy the native class immediately when downloadItem is destroyed.
delete this; delete this;
@ -148,7 +149,7 @@ const std::vector<GURL>& DownloadItem::GetURLChain() const {
return download_item_->GetUrlChain(); return download_item_->GetUrlChain();
} }
content::DownloadItem::DownloadState DownloadItem::GetState() const { download::DownloadItem::DownloadState DownloadItem::GetState() const {
return download_item_->GetState(); return download_item_->GetState();
} }
@ -206,7 +207,7 @@ void DownloadItem::BuildPrototype(v8::Isolate* isolate,
// static // static
mate::Handle<DownloadItem> DownloadItem::Create(v8::Isolate* isolate, mate::Handle<DownloadItem> DownloadItem::Create(v8::Isolate* isolate,
content::DownloadItem* item) { download::DownloadItem* item) {
auto* existing = TrackableObject::FromWrappedClass(isolate, item); auto* existing = TrackableObject::FromWrappedClass(isolate, item);
if (existing) if (existing)
return mate::CreateHandle(isolate, static_cast<DownloadItem*>(existing)); return mate::CreateHandle(isolate, static_cast<DownloadItem*>(existing));

View file

@ -10,7 +10,7 @@
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "content/public/browser/download_item.h" #include "components/download/public/common/download_item.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "url/gurl.h" #include "url/gurl.h"
@ -19,10 +19,10 @@ namespace atom {
namespace api { namespace api {
class DownloadItem : public mate::TrackableObject<DownloadItem>, class DownloadItem : public mate::TrackableObject<DownloadItem>,
public content::DownloadItem::Observer { public download::DownloadItem::Observer {
public: public:
static mate::Handle<DownloadItem> Create(v8::Isolate* isolate, static mate::Handle<DownloadItem> Create(v8::Isolate* isolate,
content::DownloadItem* item); download::DownloadItem* item);
static void BuildPrototype(v8::Isolate* isolate, static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype); v8::Local<v8::FunctionTemplate> prototype);
@ -40,7 +40,7 @@ class DownloadItem : public mate::TrackableObject<DownloadItem>,
std::string GetContentDisposition() const; std::string GetContentDisposition() const;
const GURL& GetURL() const; const GURL& GetURL() const;
const std::vector<GURL>& GetURLChain() const; const std::vector<GURL>& GetURLChain() const;
content::DownloadItem::DownloadState GetState() const; download::DownloadItem::DownloadState GetState() const;
bool IsDone() const; bool IsDone() const;
void SetSavePath(const base::FilePath& path); void SetSavePath(const base::FilePath& path);
base::FilePath GetSavePath() const; base::FilePath GetSavePath() const;
@ -49,16 +49,16 @@ class DownloadItem : public mate::TrackableObject<DownloadItem>,
double GetStartTime() const; double GetStartTime() const;
protected: protected:
DownloadItem(v8::Isolate* isolate, content::DownloadItem* download_item); DownloadItem(v8::Isolate* isolate, download::DownloadItem* download_item);
~DownloadItem() override; ~DownloadItem() override;
// Override content::DownloadItem::Observer methods // Override download::DownloadItem::Observer methods
void OnDownloadUpdated(content::DownloadItem* download) override; void OnDownloadUpdated(download::DownloadItem* download) override;
void OnDownloadDestroyed(content::DownloadItem* download) override; void OnDownloadDestroyed(download::DownloadItem* download) override;
private: private:
base::FilePath save_path_; base::FilePath save_path_;
content::DownloadItem* download_item_; download::DownloadItem* download_item_;
DISALLOW_COPY_AND_ASSIGN(DownloadItem); DISALLOW_COPY_AND_ASSIGN(DownloadItem);
}; };

View file

@ -35,6 +35,7 @@
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager_delegate.h" #include "content/public/browser/download_manager_delegate.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -433,10 +434,10 @@ void DownloadIdCallback(content::DownloadManager* download_manager,
base::GenerateGUID(), id, path, path, url_chain, GURL(), GURL(), GURL(), base::GenerateGUID(), id, path, path, url_chain, GURL(), GURL(), GURL(),
GURL(), mime_type, mime_type, start_time, base::Time(), etag, GURL(), mime_type, mime_type, start_time, base::Time(), etag,
last_modified, offset, length, std::string(), last_modified, offset, length, std::string(),
content::DownloadItem::INTERRUPTED, download::DownloadItem::INTERRUPTED,
content::DownloadDangerType::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, content::DownloadDangerType::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT, false, base::Time(), content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT, false, base::Time(),
false, std::vector<content::DownloadItem::ReceivedSlice>()); false, std::vector<download::DownloadItem::ReceivedSlice>());
} }
void SetDevToolsNetworkEmulationClientIdInIO( void SetDevToolsNetworkEmulationClientIdInIO(
@ -506,16 +507,18 @@ Session::~Session() {
} }
void Session::OnDownloadCreated(content::DownloadManager* manager, void Session::OnDownloadCreated(content::DownloadManager* manager,
content::DownloadItem* item) { download::DownloadItem* item) {
if (item->IsSavePackageDownload()) if (item->IsSavePackageDownload())
return; return;
v8::Locker locker(isolate()); v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
auto handle = DownloadItem::Create(isolate(), item); auto handle = DownloadItem::Create(isolate(), item);
if (item->GetState() == content::DownloadItem::INTERRUPTED) if (item->GetState() == download::DownloadItem::INTERRUPTED)
handle->SetSavePath(item->GetTargetFilePath()); handle->SetSavePath(item->GetTargetFilePath());
bool prevent_default = Emit("will-download", handle, item->GetWebContents()); content::WebContents* web_contents =
content::DownloadItemUtils::GetWebContents(item);
bool prevent_default = Emit("will-download", handle, web_contents);
if (prevent_default) { if (prevent_default) {
item->Cancel(true); item->Cancel(true);
item->Remove(); item->Remove();

View file

@ -95,7 +95,7 @@ class Session : public mate::TrackableObject<Session>,
// content::DownloadManager::Observer: // content::DownloadManager::Observer:
void OnDownloadCreated(content::DownloadManager* manager, void OnDownloadCreated(content::DownloadManager* manager,
content::DownloadItem* item) override; download::DownloadItem* item) override;
private: private:
// Cached object. // Cached object.

View file

@ -1214,7 +1214,7 @@ void WebContents::DownloadURL(const GURL& url) {
content::BrowserContext::GetDownloadManager(browser_context); content::BrowserContext::GetDownloadManager(browser_context);
download_manager->DownloadUrl( download_manager->DownloadUrl(
content::DownloadUrlParameters::CreateForWebContentsMainFrame( download::DownloadUrlParameters::CreateForWebContentsMainFrame(
web_contents(), url, NO_TRAFFIC_ANNOTATION_YET)); web_contents(), url, NO_TRAFFIC_ANNOTATION_YET));
} }

View file

@ -22,7 +22,7 @@ SavePageHandler::SavePageHandler(content::WebContents* web_contents,
SavePageHandler::~SavePageHandler() {} SavePageHandler::~SavePageHandler() {}
void SavePageHandler::OnDownloadCreated(content::DownloadManager* manager, void SavePageHandler::OnDownloadCreated(content::DownloadManager* manager,
content::DownloadItem* item) { download::DownloadItem* item) {
// OnDownloadCreated is invoked during WebContents::SavePage, so the |item| // OnDownloadCreated is invoked during WebContents::SavePage, so the |item|
// here is the one stated by WebContents::SavePage. // here is the one stated by WebContents::SavePage.
item->AddObserver(this); item->AddObserver(this);
@ -48,12 +48,12 @@ bool SavePageHandler::Handle(const base::FilePath& full_path,
return result; return result;
} }
void SavePageHandler::OnDownloadUpdated(content::DownloadItem* item) { void SavePageHandler::OnDownloadUpdated(download::DownloadItem* item) {
if (item->IsDone()) { if (item->IsDone()) {
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
if (item->GetState() == content::DownloadItem::COMPLETE) { if (item->GetState() == download::DownloadItem::COMPLETE) {
callback_.Run(v8::Null(isolate)); callback_.Run(v8::Null(isolate));
} else { } else {
v8::Local<v8::String> error_message = v8::Local<v8::String> error_message =
@ -64,7 +64,7 @@ void SavePageHandler::OnDownloadUpdated(content::DownloadItem* item) {
} }
} }
void SavePageHandler::Destroy(content::DownloadItem* item) { void SavePageHandler::Destroy(download::DownloadItem* item) {
item->RemoveObserver(this); item->RemoveObserver(this);
delete this; delete this;
} }

View file

@ -7,7 +7,7 @@
#include <string> #include <string>
#include "content/public/browser/download_item.h" #include "components/download/public/common/download_item.h"
#include "content/public/browser/download_manager.h" #include "content/public/browser/download_manager.h"
#include "content/public/browser/save_page_type.h" #include "content/public/browser/save_page_type.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
@ -26,7 +26,7 @@ namespace api {
// A self-destroyed class for handling save page request. // A self-destroyed class for handling save page request.
class SavePageHandler : public content::DownloadManager::Observer, class SavePageHandler : public content::DownloadManager::Observer,
public content::DownloadItem::Observer { public download::DownloadItem::Observer {
public: public:
using SavePageCallback = base::Callback<void(v8::Local<v8::Value>)>; using SavePageCallback = base::Callback<void(v8::Local<v8::Value>)>;
@ -38,14 +38,14 @@ class SavePageHandler : public content::DownloadManager::Observer,
const content::SavePageType& save_type); const content::SavePageType& save_type);
private: private:
void Destroy(content::DownloadItem* item); void Destroy(download::DownloadItem* item);
// content::DownloadManager::Observer: // content::DownloadManager::Observer:
void OnDownloadCreated(content::DownloadManager* manager, void OnDownloadCreated(content::DownloadManager* manager,
content::DownloadItem* item) override; download::DownloadItem* item) override;
// content::DownloadItem::Observer: // download::DownloadItem::Observer:
void OnDownloadUpdated(content::DownloadItem* item) override; void OnDownloadUpdated(download::DownloadItem* item) override;
content::WebContents* web_contents_; // weak content::WebContents* web_contents_; // weak
SavePageCallback callback_; SavePageCallback callback_;

View file

@ -19,6 +19,7 @@
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager.h" #include "content/public/browser/download_manager.h"
#include "net/base/filename_util.h" #include "net/base/filename_util.h"
@ -57,7 +58,7 @@ AtomDownloadManagerDelegate::~AtomDownloadManagerDelegate() {
} }
} }
void AtomDownloadManagerDelegate::GetItemSavePath(content::DownloadItem* item, void AtomDownloadManagerDelegate::GetItemSavePath(download::DownloadItem* item,
base::FilePath* path) { base::FilePath* path) {
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
@ -79,7 +80,8 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
return; return;
NativeWindow* window = nullptr; NativeWindow* window = nullptr;
content::WebContents* web_contents = item->GetWebContents(); content::WebContents* web_contents =
content::DownloadItemUtils::GetWebContents(item);
auto* relay = auto* relay =
web_contents ? NativeWindowRelay::FromWebContents(web_contents) : nullptr; web_contents ? NativeWindowRelay::FromWebContents(web_contents) : nullptr;
if (relay) if (relay)
@ -116,7 +118,7 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
// Running the DownloadTargetCallback with an empty FilePath signals that the // Running the DownloadTargetCallback with an empty FilePath signals that the
// download should be cancelled. // download should be cancelled.
// If user cancels the file save dialog, run the callback with empty FilePath. // If user cancels the file save dialog, run the callback with empty FilePath.
callback.Run(path, content::DownloadItem::TARGET_DISPOSITION_PROMPT, callback.Run(path, download::DownloadItem::TARGET_DISPOSITION_PROMPT,
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path,
path.empty() ? content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED path.empty() ? content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
: content::DOWNLOAD_INTERRUPT_REASON_NONE); : content::DOWNLOAD_INTERRUPT_REASON_NONE);
@ -128,13 +130,13 @@ void AtomDownloadManagerDelegate::Shutdown() {
} }
bool AtomDownloadManagerDelegate::DetermineDownloadTarget( bool AtomDownloadManagerDelegate::DetermineDownloadTarget(
content::DownloadItem* download, download::DownloadItem* download,
const content::DownloadTargetCallback& callback) { const content::DownloadTargetCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!download->GetForcedFilePath().empty()) { if (!download->GetForcedFilePath().empty()) {
callback.Run(download->GetForcedFilePath(), callback.Run(download->GetForcedFilePath(),
content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
download->GetForcedFilePath(), download->GetForcedFilePath(),
content::DOWNLOAD_INTERRUPT_REASON_NONE); content::DOWNLOAD_INTERRUPT_REASON_NONE);
@ -145,9 +147,10 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget(
base::FilePath save_path; base::FilePath save_path;
GetItemSavePath(download, &save_path); GetItemSavePath(download, &save_path);
if (!save_path.empty()) { if (!save_path.empty()) {
callback.Run(save_path, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, callback.Run(save_path,
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, save_path, download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
content::DOWNLOAD_INTERRUPT_REASON_NONE); content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
save_path, content::DOWNLOAD_INTERRUPT_REASON_NONE);
return true; return true;
} }
@ -171,14 +174,14 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget(
} }
bool AtomDownloadManagerDelegate::ShouldOpenDownload( bool AtomDownloadManagerDelegate::ShouldOpenDownload(
content::DownloadItem* download, download::DownloadItem* download,
const content::DownloadOpenDelayedCallback& callback) { const content::DownloadOpenDelayedCallback& callback) {
return true; return true;
} }
void AtomDownloadManagerDelegate::GetNextId( void AtomDownloadManagerDelegate::GetNextId(
const content::DownloadIdCallback& callback) { const content::DownloadIdCallback& callback) {
static uint32_t next_id = content::DownloadItem::kInvalidId + 1; static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
callback.Run(next_id++); callback.Run(next_id++);
} }

View file

@ -31,16 +31,16 @@ class AtomDownloadManagerDelegate : public content::DownloadManagerDelegate {
// content::DownloadManagerDelegate: // content::DownloadManagerDelegate:
void Shutdown() override; void Shutdown() override;
bool DetermineDownloadTarget( bool DetermineDownloadTarget(
content::DownloadItem* download, download::DownloadItem* download,
const content::DownloadTargetCallback& callback) override; const content::DownloadTargetCallback& callback) override;
bool ShouldOpenDownload( bool ShouldOpenDownload(
content::DownloadItem* download, download::DownloadItem* download,
const content::DownloadOpenDelayedCallback& callback) override; const content::DownloadOpenDelayedCallback& callback) override;
void GetNextId(const content::DownloadIdCallback& callback) override; void GetNextId(const content::DownloadIdCallback& callback) override;
private: private:
// Get the save path set on the associated api::DownloadItem object // Get the save path set on the associated api::DownloadItem object
void GetItemSavePath(content::DownloadItem* item, base::FilePath* path); void GetItemSavePath(download::DownloadItem* item, base::FilePath* path);
content::DownloadManager* download_manager_; content::DownloadManager* download_manager_;
base::WeakPtrFactory<AtomDownloadManagerDelegate> weak_ptr_factory_; base::WeakPtrFactory<AtomDownloadManagerDelegate> weak_ptr_factory_;