Skip to content
This repository was archived by the owner on Jan 4, 2019. It is now read-only.

Commit 524ff84

Browse files
committed
fix download handling for cr62
pre-req for brave/browser-laptop#11326
1 parent 8465aa7 commit 524ff84

14 files changed

+215
-108
lines changed

atom/browser/api/atom_api_download_item.cc

+25-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ std::map<uint32_t, v8::Global<v8::Object>> g_download_item_objects;
5858

5959
DownloadItem::DownloadItem(v8::Isolate* isolate,
6060
content::DownloadItem* download_item)
61-
: download_item_(download_item) {
61+
: download_item_(download_item),
62+
prompt_(download_item->GetTargetDisposition() ==
63+
content::DownloadItem::TARGET_DISPOSITION_PROMPT) {
6264
download_item_->AddObserver(this);
6365
Init(isolate);
6466
AttachAsUserData(download_item);
@@ -87,6 +89,10 @@ void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
8789
}
8890
}
8991

92+
void DownloadItem::OnDownloadRemoved(content::DownloadItem* download) {
93+
Emit("removed");
94+
}
95+
9096
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) {
9197
download_item_ = nullptr;
9298
// Destroy the native class immediately when downloadItem is destroyed.
@@ -111,7 +117,6 @@ bool DownloadItem::CanResume() const {
111117

112118
void DownloadItem::Cancel() {
113119
download_item_->Cancel(true);
114-
download_item_->Remove();
115120
}
116121

117122
int64_t DownloadItem::GetReceivedBytes() const {
@@ -139,6 +144,10 @@ std::string DownloadItem::GetFilename() const {
139144
std::string()).LossyDisplayName());
140145
}
141146

147+
base::FilePath DownloadItem::GetTargetFilePath() const {
148+
return download_item_->GetTargetFilePath();
149+
}
150+
142151
std::string DownloadItem::GetContentDisposition() const {
143152
return download_item_->GetContentDisposition();
144153
}
@@ -163,10 +172,18 @@ base::FilePath DownloadItem::GetSavePath() const {
163172
return save_path_;
164173
}
165174

166-
bool DownloadItem::PromptForSaveLocation() const {
167-
return download_item_->GetTargetDisposition() ==
168-
content::DownloadItem::TARGET_DISPOSITION_PROMPT;
175+
void DownloadItem::SetPrompt(bool prompt) {
176+
prompt_ = prompt;
169177
}
178+
179+
bool DownloadItem::ShouldPrompt() {
180+
return prompt_;
181+
}
182+
183+
std::string DownloadItem::GetGuid() const {
184+
return download_item_->GetGuid();
185+
}
186+
170187
// static
171188
void DownloadItem::BuildPrototype(v8::Isolate* isolate,
172189
v8::Local<v8::FunctionTemplate> prototype) {
@@ -183,13 +200,15 @@ void DownloadItem::BuildPrototype(v8::Isolate* isolate,
183200
.SetMethod("getMimeType", &DownloadItem::GetMimeType)
184201
.SetMethod("hasUserGesture", &DownloadItem::HasUserGesture)
185202
.SetMethod("getFilename", &DownloadItem::GetFilename)
203+
.SetMethod("getTargetFilePath", &DownloadItem::GetTargetFilePath)
186204
.SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition)
187205
.SetMethod("getURL", &DownloadItem::GetURL)
188206
.SetMethod("getState", &DownloadItem::GetState)
189207
.SetMethod("isDone", &DownloadItem::IsDone)
190208
.SetMethod("setSavePath", &DownloadItem::SetSavePath)
191209
.SetMethod("getSavePath", &DownloadItem::GetSavePath)
192-
.SetMethod("promptForSaveLocation", &DownloadItem::PromptForSaveLocation);
210+
.SetMethod("getGuid", &DownloadItem::GetGuid)
211+
.SetMethod("setPrompt", &DownloadItem::SetPrompt);
193212
}
194213

195214
// static

atom/browser/api/atom_api_download_item.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,30 @@ class DownloadItem : public mate::TrackableObject<DownloadItem>,
3636
std::string GetMimeType() const;
3737
bool HasUserGesture() const;
3838
std::string GetFilename() const;
39+
base::FilePath GetTargetFilePath() const;
3940
std::string GetContentDisposition() const;
4041
const GURL& GetURL() const;
4142
content::DownloadItem::DownloadState GetState() const;
4243
bool IsDone() const;
4344
void SetSavePath(const base::FilePath& path);
4445
base::FilePath GetSavePath() const;
45-
bool PromptForSaveLocation() const;
46+
std::string GetGuid() const;
47+
void SetPrompt(bool prompt);
48+
bool ShouldPrompt();
4649

4750
protected:
4851
DownloadItem(v8::Isolate* isolate, content::DownloadItem* download_item);
4952
~DownloadItem();
5053

5154
// Override content::DownloadItem::Observer methods
5255
void OnDownloadUpdated(content::DownloadItem* download) override;
56+
void OnDownloadRemoved(content::DownloadItem* download) override;
5357
void OnDownloadDestroyed(content::DownloadItem* download) override;
5458

5559
private:
5660
base::FilePath save_path_;
5761
content::DownloadItem* download_item_;
62+
bool prompt_;
5863

5964
DISALLOW_COPY_AND_ASSIGN(DownloadItem);
6065
};

atom/browser/api/atom_api_session.cc

+15-1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ Session::Session(v8::Isolate* isolate, Profile* profile)
355355
content::BrowserContext::GetDownloadManager(profile)->
356356
AddObserver(this);
357357

358+
auto user_prefs_registrar = profile_->user_prefs_change_registrar();
359+
if (!user_prefs_registrar->IsObserved(prefs::kDownloadDefaultDirectory)) {
360+
user_prefs_registrar->Add(
361+
prefs::kDownloadDefaultDirectory,
362+
base::Bind(&Session::DefaultDownloadDirectoryChanged,
363+
base::Unretained(this)));
364+
}
365+
358366
Init(isolate);
359367
AttachAsUserData(profile);
360368
}
@@ -370,6 +378,12 @@ std::string Session::Partition() {
370378
profile_)->partition_with_prefix();
371379
}
372380

381+
void Session::DefaultDownloadDirectoryChanged() {
382+
base::FilePath default_download_path(profile_->GetPrefs()->GetFilePath(
383+
prefs::kDownloadDefaultDirectory));
384+
Emit("default-download-directory-changed", default_download_path);
385+
}
386+
373387
void Session::OnDownloadCreated(content::DownloadManager* manager,
374388
content::DownloadItem* item) {
375389
if (item->IsSavePackageDownload())
@@ -446,7 +460,7 @@ void Session::SetProxy(const net::ProxyConfig& config,
446460
}
447461

448462
void Session::SetDownloadPath(const base::FilePath& path) {
449-
profile_->prefs()->SetFilePath(
463+
profile_->GetPrefs()->SetFilePath(
450464
prefs::kDownloadDefaultDirectory, path);
451465
}
452466

atom/browser/api/atom_api_session.h

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class Session: public mate::TrackableObject<Session>,
102102
content::DownloadItem* item) override;
103103

104104
private:
105+
void DefaultDownloadDirectoryChanged();
106+
105107
// Cached object.
106108
v8::Global<v8::Value> cookies_;
107109
v8::Global<v8::Value> protocol_;

atom/browser/api/atom_api_web_contents.cc

+41-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "atom/browser/api/atom_api_web_contents.h"
1111

1212
#include "atom/browser/api/atom_api_debugger.h"
13+
#include "atom/browser/api/atom_api_download_item.h"
1314
#include "atom/browser/api/atom_api_session.h"
1415
#include "atom/browser/api/atom_api_web_request.h"
1516
#include "atom/browser/api/atom_api_window.h"
@@ -125,6 +126,16 @@ using guest_view::GuestViewManager;
125126

126127
namespace mate {
127128

129+
template<>
130+
struct Converter<content::DownloadItem*> {
131+
static v8::Local<v8::Value> ToV8(
132+
v8::Isolate* isolate, content::DownloadItem* val) {
133+
if (!val)
134+
return v8::Null(isolate);
135+
return atom::api::DownloadItem::Create(isolate, val).ToV8();
136+
}
137+
};
138+
128139
template<>
129140
struct Converter<atom::SetSizeParams> {
130141
static bool FromV8(v8::Isolate* isolate,
@@ -369,7 +380,8 @@ WebContents::WebContents(v8::Isolate* isolate,
369380
request_id_(0),
370381
enable_devtools_(true),
371382
is_being_destroyed_(false),
372-
guest_delegate_(nullptr) {
383+
guest_delegate_(nullptr),
384+
weak_ptr_factory_(this) {
373385
if (type == REMOTE) {
374386
Init(isolate);
375387
AttachAsUserData(web_contents);
@@ -388,7 +400,8 @@ WebContents::WebContents(v8::Isolate* isolate,
388400
request_id_(0),
389401
enable_devtools_(true),
390402
is_being_destroyed_(false),
391-
guest_delegate_(nullptr) {
403+
guest_delegate_(nullptr),
404+
weak_ptr_factory_(this) {
392405
CreateWebContents(isolate, options, create_params);
393406
}
394407

@@ -397,7 +410,8 @@ WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
397410
request_id_(0),
398411
enable_devtools_(true),
399412
is_being_destroyed_(false),
400-
guest_delegate_(nullptr) {
413+
guest_delegate_(nullptr),
414+
weak_ptr_factory_(this) {
401415
mate::Handle<api::Session> session = SessionFromOptions(isolate, options);
402416

403417
content::WebContents::CreateParams create_params(session->browser_context());
@@ -1604,16 +1618,37 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
16041618
web_contents()->GetController().LoadURLWithParams(params);
16051619
}
16061620

1621+
void OnDownloadStarted(base::Callback<void(content::DownloadItem*)> callback,
1622+
content::DownloadItem* item,
1623+
content::DownloadInterruptReason reason) {
1624+
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
1625+
base::Bind(callback, item));
1626+
}
1627+
16071628
void WebContents::DownloadURL(const GURL& url,
1608-
bool prompt_for_location) {
1629+
mate::Arguments* args) {
16091630
auto browser_context = web_contents()->GetBrowserContext();
16101631
auto download_manager =
16111632
content::BrowserContext::GetDownloadManager(browser_context);
16121633

16131634
auto params = content::DownloadUrlParameters::CreateForWebContentsMainFrame(
16141635
web_contents(), url, NO_TRAFFIC_ANNOTATION_YET);
1615-
if (prompt_for_location)
1616-
params->set_prompt(prompt_for_location);
1636+
1637+
bool prompt_for_location = false;
1638+
if (args->GetNext(&prompt_for_location) && prompt_for_location) {
1639+
prompt_for_location = true;
1640+
}
1641+
params->set_prompt(prompt_for_location);
1642+
1643+
base::string16 suggested_name;
1644+
if (args->GetNext(&suggested_name)) {
1645+
params->set_suggested_name(suggested_name);
1646+
}
1647+
1648+
base::Callback<void(content::DownloadItem*)> callback;
1649+
if (args && args->GetNext(&callback)) {
1650+
params->set_callback(base::Bind(OnDownloadStarted, callback));
1651+
}
16171652

16181653
download_manager->DownloadUrl(std::move(params));
16191654
}

atom/browser/api/atom_api_web_contents.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "atom/browser/common_web_contents_delegate.h"
1515
#include "atom/common/options_switches.h"
1616
#include "base/memory/memory_pressure_listener.h"
17+
#include "base/memory/weak_ptr.h"
1718
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
1819
#include "content/common/cursors/webcursor.h"
1920
#include "content/common/view_messages.h"
@@ -155,7 +156,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
155156
bool Equal(const WebContents* web_contents) const;
156157
void LoadURL(const GURL& url, const mate::Dictionary& options);
157158
void Reload(bool ignore_cache);
158-
void DownloadURL(const GURL& url, bool prompt_for_location = false);
159+
void DownloadURL(const GURL& url, mate::Arguments* args);
160+
159161
GURL GetURL() const;
160162
base::string16 GetTitle() const;
161163
bool IsInitialBlankNavigation() const;
@@ -558,6 +560,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
558560
// the context menu params for the current context menu;
559561
content::ContextMenuParams context_menu_params_;
560562

563+
base::WeakPtrFactory<WebContents> weak_ptr_factory_;
564+
561565
std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
562566
DISALLOW_COPY_AND_ASSIGN(WebContents);
563567
};

atom/browser/atom_browser_context.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,9 @@ std::vector<std::string> AtomBrowserContext::GetCookieableSchemes() {
155155
void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) {
156156
pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
157157
base::FilePath());
158-
base::FilePath download_dir;
159-
PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir);
158+
// moved to user_prefs in brave_browser_context
160159
pref_registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
161-
download_dir);
160+
base::FilePath());
162161
pref_registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
163162
}
164163

0 commit comments

Comments
 (0)