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 1874d5b03b1..f04fbca747e 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,11 @@ content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() { return guest_manager_.get(); } +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_browser_context.h b/atom/browser/atom_browser_context.h index 513cc86bca6..c1ff613b8c0 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -32,6 +32,9 @@ class AtomBrowserContext : public brightray::BrowserContext { content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; content::BrowserPluginGuestManager* GetGuestManager() override; + // brightray::BrowserContext: + void RegisterPrefs(PrefRegistrySimple* pref_registry) override; + AtomURLRequestJobFactory* job_factory() const { return job_factory_; } private: diff --git a/atom/browser/atom_download_manager_delegate.cc b/atom/browser/atom_download_manager_delegate.cc index 46c4af2dc38..b573a396332 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,9 +100,14 @@ bool AtomDownloadManagerDelegate::DetermineDownloadTarget( const content::DownloadTargetCallback& callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - if (default_download_path_.empty()) { + AtomBrowserContext* browser_context = static_cast( + download_manager_->GetBrowserContext()); + 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()) { 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()) { @@ -118,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); diff --git a/atom/browser/web_dialog_helper.cc b/atom/browser/web_dialog_helper.cc index a37d357d47d..c3d2a1d0f23 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; @@ -112,7 +111,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, @@ -126,7 +125,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..3e3a73b9983 --- /dev/null +++ b/chromium_src/chrome/common/pref_names.cc @@ -0,0 +1,12 @@ +// 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"; +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 e69de29bb2d..542a2d2c733 100644 --- a/chromium_src/chrome/common/pref_names.h +++ b/chromium_src/chrome/common/pref_names.h @@ -0,0 +1,12 @@ +// 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[]; +extern const char kDownloadDefaultDirectory[]; + +} // namespace prefs diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index cb4941daaa7..fcd3c08a960 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1239,3 +1239,10 @@ proxy-uri = ["://"][":"] and use socks4://foopy2 for all other URLs. ``` + +### 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. 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',