Chromium Code Reviews| 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" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Ensure that the default download directory exists. | 127 // Ensure that the default download directory exists. |
| 128 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
| 129 BrowserThread::FILE, FROM_HERE, | 129 BrowserThread::FILE, FROM_HERE, |
| 130 base::Bind(base::IgnoreResult(&base::CreateDirectory), | 130 base::Bind(base::IgnoreResult(&base::CreateDirectory), |
| 131 GetDefaultDownloadDirectoryForProfile())); | 131 GetDefaultDownloadDirectoryForProfile())); |
| 132 #endif // defined(OS_CHROMEOS) | 132 #endif // defined(OS_CHROMEOS) |
| 133 | 133 |
| 134 #if defined(OS_WIN) | 134 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 135 should_open_pdf_in_adobe_reader_ = | 135 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 136 prefs->GetBoolean(prefs::kOpenPdfDownloadInAdobeReader); | 136 should_open_pdf_in_system_reader_ = |
| 137 prefs->GetBoolean(prefs::kOpenPdfDownloadInSystemReader); | |
| 137 #endif | 138 #endif |
| 138 | 139 |
| 139 // If the download path is dangerous we forcefully reset it. But if we do | 140 // If the download path is dangerous we forcefully reset it. But if we do |
| 140 // so we set a flag to make sure we only do it once, to avoid fighting | 141 // so we set a flag to make sure we only do it once, to avoid fighting |
| 141 // the user if he really wants it on an unsafe place such as the desktop. | 142 // the user if he really wants it on an unsafe place such as the desktop. |
| 142 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { | 143 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
| 143 base::FilePath current_download_dir = prefs->GetFilePath( | 144 base::FilePath current_download_dir = prefs->GetFilePath( |
| 144 prefs::kDownloadDefaultDirectory); | 145 prefs::kDownloadDefaultDirectory); |
| 145 if (DownloadPathIsDangerous(current_download_dir)) { | 146 if (DownloadPathIsDangerous(current_download_dir)) { |
| 146 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, | 147 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 | 198 |
| 198 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); | 199 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); |
| 199 registry->RegisterFilePathPref( | 200 registry->RegisterFilePathPref( |
| 200 prefs::kDownloadDefaultDirectory, | 201 prefs::kDownloadDefaultDirectory, |
| 201 default_download_path, | 202 default_download_path, |
| 202 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 203 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 203 registry->RegisterFilePathPref( | 204 registry->RegisterFilePathPref( |
| 204 prefs::kSaveFileDefaultDirectory, | 205 prefs::kSaveFileDefaultDirectory, |
| 205 default_download_path, | 206 default_download_path, |
| 206 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 207 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 207 #if defined(OS_WIN) | 208 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 209 (defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 208 registry->RegisterBooleanPref( | 210 registry->RegisterBooleanPref( |
| 209 prefs::kOpenPdfDownloadInAdobeReader, | 211 prefs::kOpenPdfDownloadInSystemReader, |
| 210 false, | 212 false, |
| 211 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 213 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 212 #endif | 214 #endif |
| 213 } | 215 } |
| 214 | 216 |
| 215 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { | 217 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { |
| 216 #if defined(OS_CHROMEOS) | 218 #if defined(OS_CHROMEOS) |
| 217 return file_manager::util::GetDownloadsFolderForProfile(profile_); | 219 return file_manager::util::GetDownloadsFolderForProfile(profile_); |
| 218 #else | 220 #else |
| 219 return GetDefaultDownloadDirectory(); | 221 return GetDefaultDownloadDirectory(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 // always be false. | 279 // always be false. |
| 278 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); | 280 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); |
| 279 return *prompt_for_download_; | 281 return *prompt_for_download_; |
| 280 } | 282 } |
| 281 | 283 |
| 282 bool DownloadPrefs::IsDownloadPathManaged() const { | 284 bool DownloadPrefs::IsDownloadPathManaged() const { |
| 283 return download_path_.IsManaged(); | 285 return download_path_.IsManaged(); |
| 284 } | 286 } |
| 285 | 287 |
| 286 bool DownloadPrefs::IsAutoOpenUsed() const { | 288 bool DownloadPrefs::IsAutoOpenUsed() const { |
| 287 #if defined(OS_WIN) | 289 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 288 if (ShouldOpenPdfInAdobeReader()) | 290 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 291 if (ShouldOpenPdfInSystemReader()) | |
| 289 return true; | 292 return true; |
| 290 #endif | 293 #endif |
| 291 return !auto_open_.empty(); | 294 return !auto_open_.empty(); |
| 292 } | 295 } |
| 293 | 296 |
| 294 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( | 297 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( |
| 295 const base::FilePath& path) const { | 298 const base::FilePath& path) const { |
| 296 base::FilePath::StringType extension = path.Extension(); | 299 base::FilePath::StringType extension = path.Extension(); |
| 297 if (extension.empty()) | 300 if (extension.empty()) |
| 298 return false; | 301 return false; |
| 299 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 300 extension.erase(0, 1); | 303 extension.erase(0, 1); |
| 301 #if defined(OS_WIN) | 304 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 302 if (extension == FILE_PATH_LITERAL("pdf") && | 305 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 303 DownloadTargetDeterminer::IsAdobeReaderUpToDate() && | 306 if (extension == FILE_PATH_LITERAL("pdf") && ShouldOpenPdfInSystemReader()) |
| 304 ShouldOpenPdfInAdobeReader()) | |
| 305 return true; | 307 return true; |
| 306 #endif | 308 #endif |
| 307 return auto_open_.find(extension) != auto_open_.end(); | 309 return auto_open_.find(extension) != auto_open_.end(); |
| 308 } | 310 } |
| 309 | 311 |
| 310 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 312 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
| 311 const base::FilePath& file_name) { | 313 const base::FilePath& file_name) { |
| 312 base::FilePath::StringType extension = file_name.Extension(); | 314 base::FilePath::StringType extension = file_name.Extension(); |
| 313 if (extension.empty()) | 315 if (extension.empty()) |
| 314 return false; | 316 return false; |
| 315 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 317 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 316 extension.erase(0, 1); | 318 extension.erase(0, 1); |
| 317 | 319 |
| 318 auto_open_.insert(extension); | 320 auto_open_.insert(extension); |
| 319 SaveAutoOpenState(); | 321 SaveAutoOpenState(); |
| 320 return true; | 322 return true; |
| 321 } | 323 } |
| 322 | 324 |
| 323 void DownloadPrefs::DisableAutoOpenBasedOnExtension( | 325 void DownloadPrefs::DisableAutoOpenBasedOnExtension( |
| 324 const base::FilePath& file_name) { | 326 const base::FilePath& file_name) { |
| 325 base::FilePath::StringType extension = file_name.Extension(); | 327 base::FilePath::StringType extension = file_name.Extension(); |
| 326 if (extension.empty()) | 328 if (extension.empty()) |
| 327 return; | 329 return; |
| 328 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 330 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 329 extension.erase(0, 1); | 331 extension.erase(0, 1); |
| 330 auto_open_.erase(extension); | 332 auto_open_.erase(extension); |
| 331 SaveAutoOpenState(); | 333 SaveAutoOpenState(); |
| 332 } | 334 } |
| 333 | 335 |
| 334 #if defined(OS_WIN) | 336 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 335 void DownloadPrefs::SetShouldOpenPdfInAdobeReader(bool should_open) { | 337 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 336 if (should_open_pdf_in_adobe_reader_ == should_open) | 338 void DownloadPrefs::SetShouldOpenPdfInSystemReader(bool should_open) { |
| 339 if (should_open_pdf_in_system_reader_ == should_open) | |
| 337 return; | 340 return; |
| 338 should_open_pdf_in_adobe_reader_ = should_open; | 341 should_open_pdf_in_system_reader_ = should_open; |
| 339 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInAdobeReader, | 342 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInSystemReader, |
| 340 should_open); | 343 should_open); |
| 341 } | 344 } |
| 342 | 345 |
| 343 bool DownloadPrefs::ShouldOpenPdfInAdobeReader() const { | 346 bool DownloadPrefs::ShouldOpenPdfInSystemReader() const { |
| 344 return should_open_pdf_in_adobe_reader_; | 347 #if defined(OS_WIN) |
| 348 if (!DownloadTargetDeterminer::IsAdobeReaderUpToDate()) | |
|
asanka
2014/10/28 21:32:50
if (<is adobe the default reader> && IsAdobeReader
palmer
2014/10/28 22:38:39
Done.
| |
| 349 return false; | |
| 350 #endif | |
| 351 return should_open_pdf_in_system_reader_; | |
| 345 } | 352 } |
| 346 #endif | 353 #endif |
| 347 | 354 |
| 348 void DownloadPrefs::ResetAutoOpen() { | 355 void DownloadPrefs::ResetAutoOpen() { |
| 349 #if defined(OS_WIN) | 356 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 350 SetShouldOpenPdfInAdobeReader(false); | 357 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 358 SetShouldOpenPdfInSystemReader(false); | |
| 351 #endif | 359 #endif |
| 352 auto_open_.clear(); | 360 auto_open_.clear(); |
| 353 SaveAutoOpenState(); | 361 SaveAutoOpenState(); |
| 354 } | 362 } |
| 355 | 363 |
| 356 void DownloadPrefs::SaveAutoOpenState() { | 364 void DownloadPrefs::SaveAutoOpenState() { |
| 357 std::string extensions; | 365 std::string extensions; |
| 358 for (AutoOpenSet::iterator it = auto_open_.begin(); | 366 for (AutoOpenSet::iterator it = auto_open_.begin(); |
| 359 it != auto_open_.end(); ++it) { | 367 it != auto_open_.end(); ++it) { |
| 360 #if defined(OS_POSIX) | 368 #if defined(OS_POSIX) |
| 361 std::string this_extension = *it; | 369 std::string this_extension = *it; |
| 362 #elif defined(OS_WIN) | 370 #elif defined(OS_WIN) |
| 363 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? | 371 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? |
| 364 std::string this_extension = base::SysWideToUTF8(*it); | 372 std::string this_extension = base::SysWideToUTF8(*it); |
| 365 #endif | 373 #endif |
| 366 extensions += this_extension + ":"; | 374 extensions += this_extension + ":"; |
| 367 } | 375 } |
| 368 if (!extensions.empty()) | 376 if (!extensions.empty()) |
| 369 extensions.erase(extensions.size() - 1); | 377 extensions.erase(extensions.size() - 1); |
| 370 | 378 |
| 371 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 379 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
| 372 } | 380 } |
| 373 | 381 |
| 374 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 382 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
| 375 const base::FilePath::StringType& a, | 383 const base::FilePath::StringType& a, |
| 376 const base::FilePath::StringType& b) const { | 384 const base::FilePath::StringType& b) const { |
| 377 return base::FilePath::CompareLessIgnoreCase(a, b); | 385 return base::FilePath::CompareLessIgnoreCase(a, b); |
| 378 } | 386 } |
| OLD | NEW |