From 0700f08d6da7c7c7cabb18a878a3fc63efdb2346 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Sun, 26 Jul 2015 16:08:29 +0800 Subject: [PATCH 1/7] Copy pref_names files from Chromium. --- atom/browser/web_dialog_helper.cc | 7 +++---- chromium_src/chrome/common/pref_names.cc | 11 +++++++++++ chromium_src/chrome/common/pref_names.h | 11 +++++++++++ filenames.gypi | 2 ++ 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 chromium_src/chrome/common/pref_names.cc diff --git a/atom/browser/web_dialog_helper.cc b/atom/browser/web_dialog_helper.cc index 93cb32cba7c..7b467cfcdfc 100644 --- a/atom/browser/web_dialog_helper.cc +++ b/atom/browser/web_dialog_helper.cc @@ -15,6 +15,7 @@ #include "base/files/file_path.h" #include "base/prefs/pref_service.h" #include "base/strings/utf_string_conversions.h" +#include "chrome/common/pref_names.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/common/file_chooser_file_info.h" @@ -23,8 +24,6 @@ namespace { -const char kSelectFileLastDirectory[] = "selectfile.last_directory"; - file_dialog::Filters GetFileTypesFromAcceptType( const std::vector& accept_types) { file_dialog::Filters filters; @@ -111,7 +110,7 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, AtomBrowserContext* browser_context = static_cast( window_->web_contents()->GetBrowserContext()); base::FilePath default_file_path = browser_context->prefs()->GetFilePath( - kSelectFileLastDirectory).Append(params.default_file_name); + prefs::kSelectFileLastDirectory).Append(params.default_file_name); if (file_dialog::ShowOpenDialog(window_, base::UTF16ToUTF8(params.title), default_file_path, @@ -125,7 +124,7 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, result.push_back(info); } if (!paths.empty()) { - browser_context->prefs()->SetFilePath(kSelectFileLastDirectory, + browser_context->prefs()->SetFilePath(prefs::kSelectFileLastDirectory, paths[0].DirName()); } } diff --git a/chromium_src/chrome/common/pref_names.cc b/chromium_src/chrome/common/pref_names.cc new file mode 100644 index 00000000000..3871fde2c2f --- /dev/null +++ b/chromium_src/chrome/common/pref_names.cc @@ -0,0 +1,11 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/pref_names.h" + +namespace prefs { + +const char kSelectFileLastDirectory[] = "selectfile.last_directory"; + +} // namespace prefs diff --git a/chromium_src/chrome/common/pref_names.h b/chromium_src/chrome/common/pref_names.h index e69de29bb2d..78bd2a04800 100644 --- a/chromium_src/chrome/common/pref_names.h +++ b/chromium_src/chrome/common/pref_names.h @@ -0,0 +1,11 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Constants for the names of various preferences, for easier changing. + +namespace prefs { + +extern const char kSelectFileLastDirectory[]; + +} // namespace prefs diff --git a/filenames.gypi b/filenames.gypi index ed51fc8fb47..82f9ea95d07 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -364,6 +364,8 @@ 'chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.cc', 'chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.h', 'chromium_src/chrome/common/chrome_utility_messages.h', + 'chromium_src/chrome/common/pref_names.cc', + 'chromium_src/chrome/common/pref_names.h', 'chromium_src/chrome/common/print_messages.cc', 'chromium_src/chrome/common/print_messages.h', 'chromium_src/chrome/common/tts_messages.h', From 2aa17debc8daa253eb81e0893f458125e5e324a1 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Sun, 26 Jul 2015 16:17:55 +0800 Subject: [PATCH 2/7] prefs' key needs to be registered before using. --- atom/browser/atom_browser_context.cc | 8 ++++++++ atom/browser/atom_browser_context.h | 1 + 2 files changed, 9 insertions(+) diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 1874d5b03b1..65307d0ef7c 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -15,10 +15,13 @@ #include "atom/common/chrome_version.h" #include "atom/common/options_switches.h" #include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/prefs/pref_registry_simple.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "base/threading/sequenced_worker_pool.h" #include "base/threading/worker_pool.h" +#include "chrome/common/pref_names.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/url_constants.h" #include "content/public/common/user_agent.h" @@ -146,4 +149,9 @@ content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() { return guest_manager_.get(); } +void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) { + pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory, + base::FilePath()); +} + } // namespace atom diff --git a/atom/browser/atom_browser_context.h b/atom/browser/atom_browser_context.h index 513cc86bca6..d64a894070f 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -31,6 +31,7 @@ class AtomBrowserContext : public brightray::BrowserContext { // content::BrowserContext: content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; content::BrowserPluginGuestManager* GetGuestManager() override; + void RegisterPrefs(PrefRegistrySimple* pref_registry) override; AtomURLRequestJobFactory* job_factory() const { return job_factory_; } From fef53d18c4b694799b775bb53a764d8c803ceb2b Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Sun, 26 Jul 2015 16:30:02 +0800 Subject: [PATCH 3/7] Add `session.setDownloadPath` API. --- atom/browser/api/atom_api_session.cc | 11 ++++++++++- atom/browser/api/atom_api_session.h | 1 + atom/browser/atom_browser_context.cc | 2 ++ atom/browser/atom_download_manager_delegate.cc | 13 +++++++++++++ chromium_src/chrome/common/pref_names.cc | 1 + chromium_src/chrome/common/pref_names.h | 1 + 6 files changed, 28 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 483b730d1c7..c864598a319 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -10,8 +10,11 @@ #include "atom/browser/api/atom_api_cookies.h" #include "atom/browser/atom_browser_context.h" #include "atom/common/native_mate_converters/gurl_converter.h" -#include "base/thread_task_runner_handle.h" +#include "base/files/file_path.h" +#include "base/prefs/pref_service.h" #include "base/strings/string_util.h" +#include "base/thread_task_runner_handle.h" +#include "chrome/common/pref_names.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/storage_partition.h" #include "native_mate/callback.h" @@ -251,6 +254,11 @@ void Session::SetProxy(const std::string& proxy, base::Bind(&SetProxyInIO, base::Unretained(getter), proxy, callback)); } +void Session::SetDownloadPath(const std::string& path) { + browser_context_->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory, + base::FilePath(path)); +} + v8::Local Session::Cookies(v8::Isolate* isolate) { if (cookies_.IsEmpty()) { auto handle = atom::api::Cookies::Create(isolate, browser_context_); @@ -266,6 +274,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( .SetMethod("clearCache", &Session::ClearCache) .SetMethod("clearStorageData", &Session::ClearStorageData) .SetMethod("setProxy", &Session::SetProxy) + .SetMethod("setDownloadPath", &Session::SetDownloadPath) .SetProperty("cookies", &Session::Cookies); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 3963ed21189..a8c0e244495 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -46,6 +46,7 @@ class Session: public mate::TrackableObject { void ClearCache(const net::CompletionCallback& callback); void ClearStorageData(mate::Arguments* args); void SetProxy(const std::string& proxy, const base::Closure& callback); + void SetDownloadPath(const std::string& path); v8::Local Cookies(v8::Isolate* isolate); v8::Global cookies_; diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 65307d0ef7c..f04fbca747e 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -152,6 +152,8 @@ content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() { void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) { pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory, base::FilePath()); + pref_registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, + base::FilePath()); } } // namespace atom diff --git a/atom/browser/atom_download_manager_delegate.cc b/atom/browser/atom_download_manager_delegate.cc index 46c4af2dc38..7e393f7d2c2 100644 --- a/atom/browser/atom_download_manager_delegate.cc +++ b/atom/browser/atom_download_manager_delegate.cc @@ -6,10 +6,13 @@ #include +#include "atom/browser/atom_browser_context.h" #include "atom/browser/native_window.h" #include "atom/browser/ui/file_dialog.h" #include "base/bind.h" #include "base/files/file_util.h" +#include "base/prefs/pref_service.h" +#include "chrome/common/pref_names.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_manager.h" @@ -77,6 +80,11 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated( return; } + // Remeber the last selected download directory. + AtomBrowserContext* browser_context = static_cast( + download_manager_->GetBrowserContext()); + browser_context->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory, + path.DirName()); callback.Run(path, content::DownloadItem::TARGET_DISPOSITION_PROMPT, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path); @@ -92,6 +100,11 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget( const content::DownloadTargetCallback& callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + AtomBrowserContext* browser_context = static_cast( + download_manager_->GetBrowserContext()); + default_download_path_ = browser_context->prefs()->GetFilePath( + prefs::kDownloadDefaultDirectory); + // If users didn't set download path, use 'Downloads' directory by default. if (default_download_path_.empty()) { auto path = download_manager_->GetBrowserContext()->GetPath(); default_download_path_ = path.Append(FILE_PATH_LITERAL("Downloads")); diff --git a/chromium_src/chrome/common/pref_names.cc b/chromium_src/chrome/common/pref_names.cc index 3871fde2c2f..3e3a73b9983 100644 --- a/chromium_src/chrome/common/pref_names.cc +++ b/chromium_src/chrome/common/pref_names.cc @@ -7,5 +7,6 @@ namespace prefs { const char kSelectFileLastDirectory[] = "selectfile.last_directory"; +const char kDownloadDefaultDirectory[] = "download.default_directory"; } // namespace prefs diff --git a/chromium_src/chrome/common/pref_names.h b/chromium_src/chrome/common/pref_names.h index 78bd2a04800..542a2d2c733 100644 --- a/chromium_src/chrome/common/pref_names.h +++ b/chromium_src/chrome/common/pref_names.h @@ -7,5 +7,6 @@ namespace prefs { extern const char kSelectFileLastDirectory[]; +extern const char kDownloadDefaultDirectory[]; } // namespace prefs From 7f0cb0ce1b00bc1122b4e6620fac9073693f517b Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Sun, 26 Jul 2015 16:52:02 +0800 Subject: [PATCH 4/7] :memo: session.setDownloadPath API. --- docs/api/browser-window.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index c8fc39a53f1..766b9ce9243 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1220,3 +1220,7 @@ proxy-uri = ["://"][":"] and use socks4://foopy2 for all other URLs. ``` + +### Session.setDownloadPath(path) + +* `path` String - The download location From 59c3efd44bbbcc1523bdf4f7715b30ecddc4aa84 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 27 Jul 2015 09:09:32 +0800 Subject: [PATCH 5/7] Correct the override comment. --- atom/browser/atom_browser_context.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atom/browser/atom_browser_context.h b/atom/browser/atom_browser_context.h index d64a894070f..3455916fa5b 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -31,6 +31,8 @@ class AtomBrowserContext : public brightray::BrowserContext { // content::BrowserContext: content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; content::BrowserPluginGuestManager* GetGuestManager() override; + + // brightray::BrowserContext:: void RegisterPrefs(PrefRegistrySimple* pref_registry) override; AtomURLRequestJobFactory* job_factory() const { return job_factory_; } From 877830e4a1159f7bd31eb0c91b6859a3d7cb351f Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 29 Jul 2015 14:03:07 +0800 Subject: [PATCH 6/7] No need for default_download_path_ member. --- atom/browser/atom_browser_context.h | 2 +- atom/browser/atom_download_manager_delegate.cc | 8 ++++---- atom/browser/atom_download_manager_delegate.h | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/atom/browser/atom_browser_context.h b/atom/browser/atom_browser_context.h index 3455916fa5b..c1ff613b8c0 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -32,7 +32,7 @@ class AtomBrowserContext : public brightray::BrowserContext { content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; content::BrowserPluginGuestManager* GetGuestManager() override; - // brightray::BrowserContext:: + // brightray::BrowserContext: void RegisterPrefs(PrefRegistrySimple* pref_registry) override; AtomURLRequestJobFactory* job_factory() const { return job_factory_; } diff --git a/atom/browser/atom_download_manager_delegate.cc b/atom/browser/atom_download_manager_delegate.cc index 7e393f7d2c2..b573a396332 100644 --- a/atom/browser/atom_download_manager_delegate.cc +++ b/atom/browser/atom_download_manager_delegate.cc @@ -102,12 +102,12 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget( AtomBrowserContext* browser_context = static_cast( download_manager_->GetBrowserContext()); - default_download_path_ = browser_context->prefs()->GetFilePath( + base::FilePath default_download_path = browser_context->prefs()->GetFilePath( prefs::kDownloadDefaultDirectory); // If users didn't set download path, use 'Downloads' directory by default. - if (default_download_path_.empty()) { + if (default_download_path.empty()) { auto path = download_manager_->GetBrowserContext()->GetPath(); - default_download_path_ = path.Append(FILE_PATH_LITERAL("Downloads")); + default_download_path = path.Append(FILE_PATH_LITERAL("Downloads")); } if (!download->GetForcedFilePath().empty()) { @@ -131,7 +131,7 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget( download->GetContentDisposition(), download->GetSuggestedFilename(), download->GetMimeType(), - default_download_path_, + default_download_path, download_path_callback)); return true; } diff --git a/atom/browser/atom_download_manager_delegate.h b/atom/browser/atom_download_manager_delegate.h index e2d82924329..2df3a7d45a6 100644 --- a/atom/browser/atom_download_manager_delegate.h +++ b/atom/browser/atom_download_manager_delegate.h @@ -47,7 +47,6 @@ class AtomDownloadManagerDelegate : public content::DownloadManagerDelegate { private: content::DownloadManager* download_manager_; - base::FilePath default_download_path_; base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(AtomDownloadManagerDelegate); From f40155645c4a1c60d00db5dc248575d9b78153d8 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 29 Jul 2015 14:13:28 +0800 Subject: [PATCH 7/7] :memo: say more about setDownloadPath API. --- docs/api/browser-window.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 766b9ce9243..b1c670a34a8 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1224,3 +1224,6 @@ proxy-uri = ["://"][":"] ### Session.setDownloadPath(path) * `path` String - The download location + +Sets download saving directory. By default, the download directory will be the +`Downloads` under the respective app folder.