| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/download/download_prefs.h" | 5 #include "chrome/browser/download/download_prefs.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 21 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 22 #include "chrome/browser/download/download_extensions.h" | 22 #include "chrome/browser/download/download_extensions.h" |
| 23 #include "chrome/browser/download/download_service.h" | 23 #include "chrome/browser/download/download_service.h" |
| 24 #include "chrome/browser/download/download_service_factory.h" | 24 #include "chrome/browser/download/download_service_factory.h" |
| 25 #include "chrome/browser/download/download_target_determiner.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/profiles/profile_manager.h" | 27 #include "chrome/browser/profiles/profile_manager.h" |
| 27 #include "chrome/common/chrome_paths.h" | 28 #include "chrome/common/chrome_paths.h" |
| 28 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 29 #include "components/pref_registry/pref_registry_syncable.h" | 30 #include "components/pref_registry/pref_registry_syncable.h" |
| 30 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/download_manager.h" | 32 #include "content/public/browser/download_manager.h" |
| 32 #include "content/public/browser/save_page_type.h" | 33 #include "content/public/browser/save_page_type.h" |
| 33 | 34 |
| 34 #if defined(OS_CHROMEOS) | 35 #if defined(OS_CHROMEOS) |
| 35 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 36 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
| 36 #include "chrome/browser/chromeos/drive/file_system_util.h" | 37 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 37 #include "chrome/browser/chromeos/file_manager/path_util.h" | 38 #include "chrome/browser/chromeos/file_manager/path_util.h" |
| 38 #endif | 39 #endif |
| 39 | 40 |
| 41 #if defined(OS_WIN) |
| 42 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| 43 #endif |
| 44 |
| 40 using content::BrowserContext; | 45 using content::BrowserContext; |
| 41 using content::BrowserThread; | 46 using content::BrowserThread; |
| 42 using content::DownloadManager; | 47 using content::DownloadManager; |
| 43 | 48 |
| 44 namespace { | 49 namespace { |
| 45 | 50 |
| 46 // Consider downloads 'dangerous' if they go to the home directory on Linux and | 51 // Consider downloads 'dangerous' if they go to the home directory on Linux and |
| 47 // to the desktop on any platform. | 52 // to the desktop on any platform. |
| 48 bool DownloadPathIsDangerous(const base::FilePath& download_path) { | 53 bool DownloadPathIsDangerous(const base::FilePath& download_path) { |
| 49 #if defined(OS_LINUX) | 54 #if defined(OS_LINUX) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 NOTREACHED(); | 89 NOTREACHED(); |
| 85 } | 90 } |
| 86 } | 91 } |
| 87 } | 92 } |
| 88 | 93 |
| 89 base::FilePath path_; | 94 base::FilePath path_; |
| 90 | 95 |
| 91 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory); | 96 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory); |
| 92 }; | 97 }; |
| 93 | 98 |
| 94 static base::LazyInstance<DefaultDownloadDirectory> | 99 base::LazyInstance<DefaultDownloadDirectory> |
| 95 g_default_download_directory = LAZY_INSTANCE_INITIALIZER; | 100 g_default_download_directory = LAZY_INSTANCE_INITIALIZER; |
| 96 | 101 |
| 97 } // namespace | 102 } // namespace |
| 98 | 103 |
| 99 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) { | 104 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) { |
| 100 PrefService* prefs = profile->GetPrefs(); | 105 PrefService* prefs = profile->GetPrefs(); |
| 101 | 106 |
| 102 #if defined(OS_CHROMEOS) | 107 #if defined(OS_CHROMEOS) |
| 103 // On Chrome OS, the default download directory is different for each profile. | 108 // On Chrome OS, the default download directory is different for each profile. |
| 104 // If the profile-unaware default path (from GetDefaultDownloadDirectory()) | 109 // If the profile-unaware default path (from GetDefaultDownloadDirectory()) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 119 } | 124 } |
| 120 } | 125 } |
| 121 | 126 |
| 122 // Ensure that the default download directory exists. | 127 // Ensure that the default download directory exists. |
| 123 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
| 124 BrowserThread::FILE, FROM_HERE, | 129 BrowserThread::FILE, FROM_HERE, |
| 125 base::Bind(base::IgnoreResult(&base::CreateDirectory), | 130 base::Bind(base::IgnoreResult(&base::CreateDirectory), |
| 126 GetDefaultDownloadDirectoryForProfile())); | 131 GetDefaultDownloadDirectoryForProfile())); |
| 127 #endif // defined(OS_CHROMEOS) | 132 #endif // defined(OS_CHROMEOS) |
| 128 | 133 |
| 134 #if defined(OS_WIN) |
| 135 should_open_pdf_in_adobe_reader_ = |
| 136 prefs->GetBoolean(prefs::kOpenPdfDownloadInAdobeReader); |
| 137 #endif |
| 138 |
| 129 // If the download path is dangerous we forcefully reset it. But if we do | 139 // 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 | 140 // 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. | 141 // the user if he really wants it on an unsafe place such as the desktop. |
| 132 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { | 142 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
| 133 base::FilePath current_download_dir = prefs->GetFilePath( | 143 base::FilePath current_download_dir = prefs->GetFilePath( |
| 134 prefs::kDownloadDefaultDirectory); | 144 prefs::kDownloadDefaultDirectory); |
| 135 if (DownloadPathIsDangerous(current_download_dir)) { | 145 if (DownloadPathIsDangerous(current_download_dir)) { |
| 136 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, | 146 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
| 137 GetDefaultDownloadDirectoryForProfile()); | 147 GetDefaultDownloadDirectoryForProfile()); |
| 138 } | 148 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 197 |
| 188 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); | 198 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); |
| 189 registry->RegisterFilePathPref( | 199 registry->RegisterFilePathPref( |
| 190 prefs::kDownloadDefaultDirectory, | 200 prefs::kDownloadDefaultDirectory, |
| 191 default_download_path, | 201 default_download_path, |
| 192 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 202 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 193 registry->RegisterFilePathPref( | 203 registry->RegisterFilePathPref( |
| 194 prefs::kSaveFileDefaultDirectory, | 204 prefs::kSaveFileDefaultDirectory, |
| 195 default_download_path, | 205 default_download_path, |
| 196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 206 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 207 #if defined(OS_WIN) |
| 208 registry->RegisterBooleanPref( |
| 209 prefs::kOpenPdfDownloadInAdobeReader, |
| 210 false, |
| 211 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 212 #endif |
| 197 } | 213 } |
| 198 | 214 |
| 199 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { | 215 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { |
| 200 #if defined(OS_CHROMEOS) | 216 #if defined(OS_CHROMEOS) |
| 201 return file_manager::util::GetDownloadsFolderForProfile(profile_); | 217 return file_manager::util::GetDownloadsFolderForProfile(profile_); |
| 202 #else | 218 #else |
| 203 return GetDefaultDownloadDirectory(); | 219 return GetDefaultDownloadDirectory(); |
| 204 #endif | 220 #endif |
| 205 } | 221 } |
| 206 | 222 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // always be false. | 277 // always be false. |
| 262 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); | 278 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); |
| 263 return *prompt_for_download_; | 279 return *prompt_for_download_; |
| 264 } | 280 } |
| 265 | 281 |
| 266 bool DownloadPrefs::IsDownloadPathManaged() const { | 282 bool DownloadPrefs::IsDownloadPathManaged() const { |
| 267 return download_path_.IsManaged(); | 283 return download_path_.IsManaged(); |
| 268 } | 284 } |
| 269 | 285 |
| 270 bool DownloadPrefs::IsAutoOpenUsed() const { | 286 bool DownloadPrefs::IsAutoOpenUsed() const { |
| 287 #if defined(OS_WIN) |
| 288 if (ShouldOpenPdfInAdobeReader()) |
| 289 return true; |
| 290 #endif |
| 271 return !auto_open_.empty(); | 291 return !auto_open_.empty(); |
| 272 } | 292 } |
| 273 | 293 |
| 274 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( | 294 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( |
| 275 const base::FilePath& path) const { | 295 const base::FilePath& path) const { |
| 276 base::FilePath::StringType extension = path.Extension(); | 296 base::FilePath::StringType extension = path.Extension(); |
| 277 if (extension.empty()) | 297 if (extension.empty()) |
| 278 return false; | 298 return false; |
| 279 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 299 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 280 extension.erase(0, 1); | 300 extension.erase(0, 1); |
| 301 #if defined(OS_WIN) |
| 302 if (extension == FILE_PATH_LITERAL("pdf") && |
| 303 DownloadTargetDeterminer::IsAdobeReaderUpToDate() && |
| 304 ShouldOpenPdfInAdobeReader()) |
| 305 return true; |
| 306 #endif |
| 281 return auto_open_.find(extension) != auto_open_.end(); | 307 return auto_open_.find(extension) != auto_open_.end(); |
| 282 } | 308 } |
| 283 | 309 |
| 284 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 310 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
| 285 const base::FilePath& file_name) { | 311 const base::FilePath& file_name) { |
| 286 base::FilePath::StringType extension = file_name.Extension(); | 312 base::FilePath::StringType extension = file_name.Extension(); |
| 287 if (extension.empty()) | 313 if (extension.empty()) |
| 288 return false; | 314 return false; |
| 289 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 315 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 290 extension.erase(0, 1); | 316 extension.erase(0, 1); |
| 291 | 317 |
| 292 auto_open_.insert(extension); | 318 auto_open_.insert(extension); |
| 293 SaveAutoOpenState(); | 319 SaveAutoOpenState(); |
| 294 return true; | 320 return true; |
| 295 } | 321 } |
| 296 | 322 |
| 297 void DownloadPrefs::DisableAutoOpenBasedOnExtension( | 323 void DownloadPrefs::DisableAutoOpenBasedOnExtension( |
| 298 const base::FilePath& file_name) { | 324 const base::FilePath& file_name) { |
| 299 base::FilePath::StringType extension = file_name.Extension(); | 325 base::FilePath::StringType extension = file_name.Extension(); |
| 300 if (extension.empty()) | 326 if (extension.empty()) |
| 301 return; | 327 return; |
| 302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 328 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 303 extension.erase(0, 1); | 329 extension.erase(0, 1); |
| 304 auto_open_.erase(extension); | 330 auto_open_.erase(extension); |
| 305 SaveAutoOpenState(); | 331 SaveAutoOpenState(); |
| 306 } | 332 } |
| 307 | 333 |
| 334 #if defined(OS_WIN) |
| 335 void DownloadPrefs::SetShouldOpenPdfInAdobeReader(bool should_open) { |
| 336 if (should_open_pdf_in_adobe_reader_ == should_open) |
| 337 return; |
| 338 should_open_pdf_in_adobe_reader_ = should_open; |
| 339 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInAdobeReader, |
| 340 should_open); |
| 341 } |
| 342 |
| 343 bool DownloadPrefs::ShouldOpenPdfInAdobeReader() const { |
| 344 return should_open_pdf_in_adobe_reader_; |
| 345 } |
| 346 #endif |
| 347 |
| 308 void DownloadPrefs::ResetAutoOpen() { | 348 void DownloadPrefs::ResetAutoOpen() { |
| 349 #if defined(OS_WIN) |
| 350 SetShouldOpenPdfInAdobeReader(false); |
| 351 #endif |
| 309 auto_open_.clear(); | 352 auto_open_.clear(); |
| 310 SaveAutoOpenState(); | 353 SaveAutoOpenState(); |
| 311 } | 354 } |
| 312 | 355 |
| 313 void DownloadPrefs::SaveAutoOpenState() { | 356 void DownloadPrefs::SaveAutoOpenState() { |
| 314 std::string extensions; | 357 std::string extensions; |
| 315 for (AutoOpenSet::iterator it = auto_open_.begin(); | 358 for (AutoOpenSet::iterator it = auto_open_.begin(); |
| 316 it != auto_open_.end(); ++it) { | 359 it != auto_open_.end(); ++it) { |
| 317 #if defined(OS_POSIX) | 360 #if defined(OS_POSIX) |
| 318 std::string this_extension = *it; | 361 std::string this_extension = *it; |
| 319 #elif defined(OS_WIN) | 362 #elif defined(OS_WIN) |
| 320 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? | 363 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? |
| 321 std::string this_extension = base::SysWideToUTF8(*it); | 364 std::string this_extension = base::SysWideToUTF8(*it); |
| 322 #endif | 365 #endif |
| 323 extensions += this_extension + ":"; | 366 extensions += this_extension + ":"; |
| 324 } | 367 } |
| 325 if (!extensions.empty()) | 368 if (!extensions.empty()) |
| 326 extensions.erase(extensions.size() - 1); | 369 extensions.erase(extensions.size() - 1); |
| 327 | 370 |
| 328 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 371 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
| 329 } | 372 } |
| 330 | 373 |
| 331 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 374 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
| 332 const base::FilePath::StringType& a, | 375 const base::FilePath::StringType& a, |
| 333 const base::FilePath::StringType& b) const { | 376 const base::FilePath::StringType& b) const { |
| 334 return base::FilePath::CompareLessIgnoreCase(a, b); | 377 return base::FilePath::CompareLessIgnoreCase(a, b); |
| 335 } | 378 } |
| OLD | NEW |