1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_prefs.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/file_util.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/sys_string_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "chrome/browser/download/chrome_download_manager_delegate.h"
22 #include "chrome/browser/download/download_extensions.h"
23 #include "chrome/browser/download/download_service.h"
24 #include "chrome/browser/download/download_service_factory.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/pref_names.h"
29 #include "components/user_prefs/pref_registry_syncable.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/download_manager.h"
32 #include "content/public/browser/save_page_type.h"
34 #if defined(OS_CHROMEOS)
35 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
36 #include "chrome/browser/chromeos/drive/file_system_util.h"
37 #include "chrome/browser/chromeos/file_manager/path_util.h"
40 using content::BrowserContext
;
41 using content::BrowserThread
;
42 using content::DownloadManager
;
46 // Consider downloads 'dangerous' if they go to the home directory on Linux and
47 // to the desktop on any platform.
48 bool DownloadPathIsDangerous(const base::FilePath
& download_path
) {
50 base::FilePath home_dir
= base::GetHomeDir();
51 if (download_path
== home_dir
) {
56 #if defined(OS_ANDROID)
57 // Android does not have a desktop dir.
60 base::FilePath desktop_dir
;
61 if (!PathService::Get(base::DIR_USER_DESKTOP
, &desktop_dir
)) {
65 return (download_path
== desktop_dir
);
69 class DefaultDownloadDirectory
{
71 const base::FilePath
& path() const { return path_
; }
74 friend struct base::DefaultLazyInstanceTraits
<DefaultDownloadDirectory
>;
76 DefaultDownloadDirectory() {
77 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS
, &path_
)) {
80 if (DownloadPathIsDangerous(path_
)) {
81 // This is only useful on platforms that support
82 // DIR_DEFAULT_DOWNLOADS_SAFE.
83 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE
, &path_
)) {
91 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory
);
94 static base::LazyInstance
<DefaultDownloadDirectory
>
95 g_default_download_directory
= LAZY_INSTANCE_INITIALIZER
;
99 DownloadPrefs::DownloadPrefs(Profile
* profile
) : profile_(profile
) {
100 PrefService
* prefs
= profile
->GetPrefs();
102 #if defined(OS_CHROMEOS)
103 // On Chrome OS, the default download directory is different for each profile.
104 // If the profile-unaware default path (from GetDefaultDownloadDirectory())
105 // is set (this happens during the initial preference registration in static
106 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile().
107 // file_manager::util::MigratePathFromOldFormat will do this.
108 const char* path_pref
[] = {
109 prefs::kSaveFileDefaultDirectory
,
110 prefs::kDownloadDefaultDirectory
112 for (size_t i
= 0; i
< arraysize(path_pref
); ++i
) {
113 const base::FilePath current
= prefs
->GetFilePath(path_pref
[i
]);
114 base::FilePath migrated
;
115 if (!current
.empty() &&
116 file_manager::util::MigratePathFromOldFormat(
117 profile_
, current
, &migrated
)) {
118 prefs
->SetFilePath(path_pref
[i
], migrated
);
122 // Ensure that the default download directory exists.
123 BrowserThread::PostTask(
124 BrowserThread::FILE, FROM_HERE
,
125 base::Bind(base::IgnoreResult(&base::CreateDirectory
),
126 GetDefaultDownloadDirectoryForProfile()));
127 #endif // defined(OS_CHROMEOS)
129 // If the download path is dangerous we forcefully reset it. But if we do
130 // so we set a flag to make sure we only do it once, to avoid fighting
131 // the user if he really wants it on an unsafe place such as the desktop.
132 if (!prefs
->GetBoolean(prefs::kDownloadDirUpgraded
)) {
133 base::FilePath current_download_dir
= prefs
->GetFilePath(
134 prefs::kDownloadDefaultDirectory
);
135 if (DownloadPathIsDangerous(current_download_dir
)) {
136 prefs
->SetFilePath(prefs::kDownloadDefaultDirectory
,
137 GetDefaultDownloadDirectoryForProfile());
139 prefs
->SetBoolean(prefs::kDownloadDirUpgraded
, true);
142 prompt_for_download_
.Init(prefs::kPromptForDownload
, prefs
);
143 download_path_
.Init(prefs::kDownloadDefaultDirectory
, prefs
);
144 save_file_path_
.Init(prefs::kSaveFileDefaultDirectory
, prefs
);
145 save_file_type_
.Init(prefs::kSaveFileType
, prefs
);
147 // We store any file extension that should be opened automatically at
148 // download completion in this pref.
149 std::string extensions_to_open
=
150 prefs
->GetString(prefs::kDownloadExtensionsToOpen
);
151 std::vector
<std::string
> extensions
;
152 base::SplitString(extensions_to_open
, ':', &extensions
);
154 for (size_t i
= 0; i
< extensions
.size(); ++i
) {
155 #if defined(OS_POSIX)
156 base::FilePath
path(extensions
[i
]);
157 #elif defined(OS_WIN)
158 base::FilePath
path(base::UTF8ToWide(extensions
[i
]));
160 if (!extensions
[i
].empty() &&
161 download_util::GetFileDangerLevel(path
) == download_util::NOT_DANGEROUS
)
162 auto_open_
.insert(path
.value());
166 DownloadPrefs::~DownloadPrefs() {}
169 void DownloadPrefs::RegisterProfilePrefs(
170 user_prefs::PrefRegistrySyncable
* registry
) {
171 registry
->RegisterBooleanPref(
172 prefs::kPromptForDownload
,
174 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
175 registry
->RegisterStringPref(
176 prefs::kDownloadExtensionsToOpen
,
178 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
179 registry
->RegisterBooleanPref(
180 prefs::kDownloadDirUpgraded
,
182 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
183 registry
->RegisterIntegerPref(
184 prefs::kSaveFileType
,
185 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML
,
186 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
188 const base::FilePath
& default_download_path
= GetDefaultDownloadDirectory();
189 registry
->RegisterFilePathPref(
190 prefs::kDownloadDefaultDirectory
,
191 default_download_path
,
192 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
193 registry
->RegisterFilePathPref(
194 prefs::kSaveFileDefaultDirectory
,
195 default_download_path
,
196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
199 base::FilePath
DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const {
200 #if defined(OS_CHROMEOS)
201 return file_manager::util::GetDownloadsFolderForProfile(profile_
);
203 return GetDefaultDownloadDirectory();
208 const base::FilePath
& DownloadPrefs::GetDefaultDownloadDirectory() {
209 return g_default_download_directory
.Get().path();
213 DownloadPrefs
* DownloadPrefs::FromDownloadManager(
214 DownloadManager
* download_manager
) {
215 ChromeDownloadManagerDelegate
* delegate
=
216 static_cast<ChromeDownloadManagerDelegate
*>(
217 download_manager
->GetDelegate());
218 return delegate
->download_prefs();
222 DownloadPrefs
* DownloadPrefs::FromBrowserContext(
223 content::BrowserContext
* context
) {
224 return FromDownloadManager(BrowserContext::GetDownloadManager(context
));
227 base::FilePath
DownloadPrefs::DownloadPath() const {
228 #if defined(OS_CHROMEOS)
229 // If the download path is under /drive, and DriveIntegrationService isn't
230 // available (which it isn't for incognito mode, for instance), use the
231 // default download directory (/Downloads).
232 if (drive::util::IsUnderDriveMountPoint(*download_path_
)) {
233 drive::DriveIntegrationService
* integration_service
=
234 drive::DriveIntegrationServiceFactory::FindForProfile(profile_
);
235 if (!integration_service
|| !integration_service
->is_enabled())
236 return GetDefaultDownloadDirectoryForProfile();
239 return *download_path_
;
242 void DownloadPrefs::SetDownloadPath(const base::FilePath
& path
) {
243 download_path_
.SetValue(path
);
244 SetSaveFilePath(path
);
247 base::FilePath
DownloadPrefs::SaveFilePath() const {
248 return *save_file_path_
;
251 void DownloadPrefs::SetSaveFilePath(const base::FilePath
& path
) {
252 save_file_path_
.SetValue(path
);
255 void DownloadPrefs::SetSaveFileType(int type
) {
256 save_file_type_
.SetValue(type
);
259 bool DownloadPrefs::PromptForDownload() const {
260 // If the DownloadDirectory policy is set, then |prompt_for_download_| should
262 DCHECK(!download_path_
.IsManaged() || !prompt_for_download_
.GetValue());
263 return *prompt_for_download_
;
266 bool DownloadPrefs::IsDownloadPathManaged() const {
267 return download_path_
.IsManaged();
270 bool DownloadPrefs::IsAutoOpenUsed() const {
271 return !auto_open_
.empty();
274 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension(
275 const base::FilePath
& path
) const {
276 base::FilePath::StringType extension
= path
.Extension();
277 if (extension
.empty())
279 DCHECK(extension
[0] == base::FilePath::kExtensionSeparator
);
280 extension
.erase(0, 1);
281 return auto_open_
.find(extension
) != auto_open_
.end();
284 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(
285 const base::FilePath
& file_name
) {
286 base::FilePath::StringType extension
= file_name
.Extension();
287 if (extension
.empty())
289 DCHECK(extension
[0] == base::FilePath::kExtensionSeparator
);
290 extension
.erase(0, 1);
292 auto_open_
.insert(extension
);
297 void DownloadPrefs::DisableAutoOpenBasedOnExtension(
298 const base::FilePath
& file_name
) {
299 base::FilePath::StringType extension
= file_name
.Extension();
300 if (extension
.empty())
302 DCHECK(extension
[0] == base::FilePath::kExtensionSeparator
);
303 extension
.erase(0, 1);
304 auto_open_
.erase(extension
);
308 void DownloadPrefs::ResetAutoOpen() {
313 void DownloadPrefs::SaveAutoOpenState() {
314 std::string extensions
;
315 for (AutoOpenSet::iterator it
= auto_open_
.begin();
316 it
!= auto_open_
.end(); ++it
) {
317 #if defined(OS_POSIX)
318 std::string this_extension
= *it
;
319 #elif defined(OS_WIN)
320 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor?
321 std::string this_extension
= base::SysWideToUTF8(*it
);
323 extensions
+= this_extension
+ ":";
325 if (!extensions
.empty())
326 extensions
.erase(extensions
.size() - 1);
328 profile_
->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen
, extensions
);
331 bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
332 const base::FilePath::StringType
& a
,
333 const base::FilePath::StringType
& b
) const {
334 return base::FilePath::CompareLessIgnoreCase(a
, b
);