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/files/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/download/download_target_determiner.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/profiles/profile_manager.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/pref_names.h"
30 #include "components/pref_registry/pref_registry_syncable.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/download_manager.h"
33 #include "content/public/browser/save_page_type.h"
35 #if defined(OS_CHROMEOS)
36 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
37 #include "chrome/browser/chromeos/drive/file_system_util.h"
38 #include "chrome/browser/chromeos/file_manager/path_util.h"
42 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
45 using content::BrowserContext
;
46 using content::BrowserThread
;
47 using content::DownloadManager
;
51 // Consider downloads 'dangerous' if they go to the home directory on Linux and
52 // to the desktop on any platform.
53 bool DownloadPathIsDangerous(const base::FilePath
& download_path
) {
55 base::FilePath home_dir
= base::GetHomeDir();
56 if (download_path
== home_dir
) {
61 #if defined(OS_ANDROID)
62 // Android does not have a desktop dir.
65 base::FilePath desktop_dir
;
66 if (!PathService::Get(base::DIR_USER_DESKTOP
, &desktop_dir
)) {
70 return (download_path
== desktop_dir
);
74 class DefaultDownloadDirectory
{
76 const base::FilePath
& path() const { return path_
; }
79 friend struct base::DefaultLazyInstanceTraits
<DefaultDownloadDirectory
>;
81 DefaultDownloadDirectory() {
82 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS
, &path_
)) {
85 if (DownloadPathIsDangerous(path_
)) {
86 // This is only useful on platforms that support
87 // DIR_DEFAULT_DOWNLOADS_SAFE.
88 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE
, &path_
)) {
96 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory
);
99 base::LazyInstance
<DefaultDownloadDirectory
>
100 g_default_download_directory
= LAZY_INSTANCE_INITIALIZER
;
104 DownloadPrefs::DownloadPrefs(Profile
* profile
) : profile_(profile
) {
105 PrefService
* prefs
= profile
->GetPrefs();
107 #if defined(OS_CHROMEOS)
108 // On Chrome OS, the default download directory is different for each profile.
109 // If the profile-unaware default path (from GetDefaultDownloadDirectory())
110 // is set (this happens during the initial preference registration in static
111 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile().
112 // file_manager::util::MigratePathFromOldFormat will do this.
113 const char* path_pref
[] = {
114 prefs::kSaveFileDefaultDirectory
,
115 prefs::kDownloadDefaultDirectory
117 for (size_t i
= 0; i
< arraysize(path_pref
); ++i
) {
118 const base::FilePath current
= prefs
->GetFilePath(path_pref
[i
]);
119 base::FilePath migrated
;
120 if (!current
.empty() &&
121 file_manager::util::MigratePathFromOldFormat(
122 profile_
, current
, &migrated
)) {
123 prefs
->SetFilePath(path_pref
[i
], migrated
);
127 // Ensure that the default download directory exists.
128 BrowserThread::PostTask(
129 BrowserThread::FILE, FROM_HERE
,
130 base::Bind(base::IgnoreResult(&base::CreateDirectory
),
131 GetDefaultDownloadDirectoryForProfile()));
132 #endif // defined(OS_CHROMEOS)
134 #if defined(OS_WIN) || defined(OS_LINUX) || \
135 (defined(OS_MACOSX) && !defined(OS_IOS))
136 should_open_pdf_in_system_reader_
=
137 prefs
->GetBoolean(prefs::kOpenPdfDownloadInSystemReader
);
140 // If the download path is dangerous we forcefully reset it. But if we do
141 // so we set a flag to make sure we only do it once, to avoid fighting
142 // the user if he really wants it on an unsafe place such as the desktop.
143 if (!prefs
->GetBoolean(prefs::kDownloadDirUpgraded
)) {
144 base::FilePath current_download_dir
= prefs
->GetFilePath(
145 prefs::kDownloadDefaultDirectory
);
146 if (DownloadPathIsDangerous(current_download_dir
)) {
147 prefs
->SetFilePath(prefs::kDownloadDefaultDirectory
,
148 GetDefaultDownloadDirectoryForProfile());
150 prefs
->SetBoolean(prefs::kDownloadDirUpgraded
, true);
153 prompt_for_download_
.Init(prefs::kPromptForDownload
, prefs
);
154 download_path_
.Init(prefs::kDownloadDefaultDirectory
, prefs
);
155 save_file_path_
.Init(prefs::kSaveFileDefaultDirectory
, prefs
);
156 save_file_type_
.Init(prefs::kSaveFileType
, prefs
);
158 // We store any file extension that should be opened automatically at
159 // download completion in this pref.
160 std::string extensions_to_open
=
161 prefs
->GetString(prefs::kDownloadExtensionsToOpen
);
162 std::vector
<std::string
> extensions
;
163 base::SplitString(extensions_to_open
, ':', &extensions
);
165 for (size_t i
= 0; i
< extensions
.size(); ++i
) {
166 #if defined(OS_POSIX)
167 base::FilePath
path(extensions
[i
]);
168 #elif defined(OS_WIN)
169 base::FilePath
path(base::UTF8ToWide(extensions
[i
]));
171 if (!extensions
[i
].empty() &&
172 download_util::GetFileDangerLevel(path
) == download_util::NOT_DANGEROUS
)
173 auto_open_
.insert(path
.value());
177 DownloadPrefs::~DownloadPrefs() {}
180 void DownloadPrefs::RegisterProfilePrefs(
181 user_prefs::PrefRegistrySyncable
* registry
) {
182 registry
->RegisterBooleanPref(
183 prefs::kPromptForDownload
,
185 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
186 registry
->RegisterStringPref(
187 prefs::kDownloadExtensionsToOpen
,
189 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
190 registry
->RegisterBooleanPref(
191 prefs::kDownloadDirUpgraded
,
193 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
194 registry
->RegisterIntegerPref(
195 prefs::kSaveFileType
,
196 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML
,
197 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
199 const base::FilePath
& default_download_path
= GetDefaultDownloadDirectory();
200 registry
->RegisterFilePathPref(
201 prefs::kDownloadDefaultDirectory
,
202 default_download_path
,
203 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
204 registry
->RegisterFilePathPref(
205 prefs::kSaveFileDefaultDirectory
,
206 default_download_path
,
207 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
208 #if defined(OS_WIN) || defined(OS_LINUX) || \
209 (defined(OS_MACOSX) && !defined(OS_IOS))
210 registry
->RegisterBooleanPref(
211 prefs::kOpenPdfDownloadInSystemReader
,
213 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
217 base::FilePath
DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const {
218 #if defined(OS_CHROMEOS)
219 return file_manager::util::GetDownloadsFolderForProfile(profile_
);
221 return GetDefaultDownloadDirectory();
226 const base::FilePath
& DownloadPrefs::GetDefaultDownloadDirectory() {
227 return g_default_download_directory
.Get().path();
231 DownloadPrefs
* DownloadPrefs::FromDownloadManager(
232 DownloadManager
* download_manager
) {
233 ChromeDownloadManagerDelegate
* delegate
=
234 static_cast<ChromeDownloadManagerDelegate
*>(
235 download_manager
->GetDelegate());
236 return delegate
->download_prefs();
240 DownloadPrefs
* DownloadPrefs::FromBrowserContext(
241 content::BrowserContext
* context
) {
242 return FromDownloadManager(BrowserContext::GetDownloadManager(context
));
245 base::FilePath
DownloadPrefs::DownloadPath() const {
246 #if defined(OS_CHROMEOS)
247 // If the download path is under /drive, and DriveIntegrationService isn't
248 // available (which it isn't for incognito mode, for instance), use the
249 // default download directory (/Downloads).
250 if (drive::util::IsUnderDriveMountPoint(*download_path_
)) {
251 drive::DriveIntegrationService
* integration_service
=
252 drive::DriveIntegrationServiceFactory::FindForProfile(profile_
);
253 if (!integration_service
|| !integration_service
->is_enabled())
254 return GetDefaultDownloadDirectoryForProfile();
257 return *download_path_
;
260 void DownloadPrefs::SetDownloadPath(const base::FilePath
& path
) {
261 download_path_
.SetValue(path
);
262 SetSaveFilePath(path
);
265 base::FilePath
DownloadPrefs::SaveFilePath() const {
266 return *save_file_path_
;
269 void DownloadPrefs::SetSaveFilePath(const base::FilePath
& path
) {
270 save_file_path_
.SetValue(path
);
273 void DownloadPrefs::SetSaveFileType(int type
) {
274 save_file_type_
.SetValue(type
);
277 bool DownloadPrefs::PromptForDownload() const {
278 // If the DownloadDirectory policy is set, then |prompt_for_download_| should
280 DCHECK(!download_path_
.IsManaged() || !prompt_for_download_
.GetValue());
281 return *prompt_for_download_
;
284 bool DownloadPrefs::IsDownloadPathManaged() const {
285 return download_path_
.IsManaged();
288 bool DownloadPrefs::IsAutoOpenUsed() const {
289 #if defined(OS_WIN) || defined(OS_LINUX) || \
290 (defined(OS_MACOSX) && !defined(OS_IOS))
291 if (ShouldOpenPdfInSystemReader())
294 return !auto_open_
.empty();
297 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension(
298 const base::FilePath
& path
) const {
299 base::FilePath::StringType extension
= path
.Extension();
300 if (extension
.empty())
302 DCHECK(extension
[0] == base::FilePath::kExtensionSeparator
);
303 extension
.erase(0, 1);
304 #if defined(OS_WIN) || defined(OS_LINUX) || \
305 (defined(OS_MACOSX) && !defined(OS_IOS))
306 if (extension
== FILE_PATH_LITERAL("pdf") && ShouldOpenPdfInSystemReader())
309 return auto_open_
.find(extension
) != auto_open_
.end();
312 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(
313 const base::FilePath
& file_name
) {
314 base::FilePath::StringType extension
= file_name
.Extension();
315 if (extension
.empty())
317 DCHECK(extension
[0] == base::FilePath::kExtensionSeparator
);
318 extension
.erase(0, 1);
320 auto_open_
.insert(extension
);
325 void DownloadPrefs::DisableAutoOpenBasedOnExtension(
326 const base::FilePath
& file_name
) {
327 base::FilePath::StringType extension
= file_name
.Extension();
328 if (extension
.empty())
330 DCHECK(extension
[0] == base::FilePath::kExtensionSeparator
);
331 extension
.erase(0, 1);
332 auto_open_
.erase(extension
);
336 #if defined(OS_WIN) || defined(OS_LINUX) || \
337 (defined(OS_MACOSX) && !defined(OS_IOS))
338 void DownloadPrefs::SetShouldOpenPdfInSystemReader(bool should_open
) {
339 if (should_open_pdf_in_system_reader_
== should_open
)
341 should_open_pdf_in_system_reader_
= should_open
;
342 profile_
->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInSystemReader
,
346 bool DownloadPrefs::ShouldOpenPdfInSystemReader() const {
348 if (IsAdobeReaderDefaultPDFViewer() &&
349 !DownloadTargetDeterminer::IsAdobeReaderUpToDate()) {
353 return should_open_pdf_in_system_reader_
;
357 void DownloadPrefs::ResetAutoOpen() {
358 #if defined(OS_WIN) || defined(OS_LINUX) || \
359 (defined(OS_MACOSX) && !defined(OS_IOS))
360 SetShouldOpenPdfInSystemReader(false);
366 void DownloadPrefs::SaveAutoOpenState() {
367 std::string extensions
;
368 for (AutoOpenSet::iterator it
= auto_open_
.begin();
369 it
!= auto_open_
.end(); ++it
) {
370 #if defined(OS_POSIX)
371 std::string this_extension
= *it
;
372 #elif defined(OS_WIN)
373 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor?
374 std::string this_extension
= base::SysWideToUTF8(*it
);
376 extensions
+= this_extension
+ ":";
378 if (!extensions
.empty())
379 extensions
.erase(extensions
.size() - 1);
381 profile_
->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen
, extensions
);
384 bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
385 const base::FilePath::StringType
& a
,
386 const base::FilePath::StringType
& b
) const {
387 return base::FilePath::CompareLessIgnoreCase(a
, b
);