| 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 19 matching lines...) Expand all Loading... |
| 30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/download_manager.h" | 31 #include "content/public/browser/download_manager.h" |
| 32 #include "content/public/browser/save_page_type.h" | 32 #include "content/public/browser/save_page_type.h" |
| 33 | 33 |
| 34 #if defined(OS_CHROMEOS) | 34 #if defined(OS_CHROMEOS) |
| 35 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 35 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
| 36 #include "chrome/browser/chromeos/drive/file_system_util.h" | 36 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 37 #include "chrome/browser/chromeos/file_manager/path_util.h" | 37 #include "chrome/browser/chromeos/file_manager/path_util.h" |
| 38 #endif | 38 #endif |
| 39 | 39 |
| 40 #if defined(OS_WIN) |
| 41 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| 42 #endif |
| 43 |
| 40 using content::BrowserContext; | 44 using content::BrowserContext; |
| 41 using content::BrowserThread; | 45 using content::BrowserThread; |
| 42 using content::DownloadManager; | 46 using content::DownloadManager; |
| 43 | 47 |
| 44 namespace { | 48 namespace { |
| 45 | 49 |
| 46 // Consider downloads 'dangerous' if they go to the home directory on Linux and | 50 // Consider downloads 'dangerous' if they go to the home directory on Linux and |
| 47 // to the desktop on any platform. | 51 // to the desktop on any platform. |
| 48 bool DownloadPathIsDangerous(const base::FilePath& download_path) { | 52 bool DownloadPathIsDangerous(const base::FilePath& download_path) { |
| 49 #if defined(OS_LINUX) | 53 #if defined(OS_LINUX) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 NOTREACHED(); | 88 NOTREACHED(); |
| 85 } | 89 } |
| 86 } | 90 } |
| 87 } | 91 } |
| 88 | 92 |
| 89 base::FilePath path_; | 93 base::FilePath path_; |
| 90 | 94 |
| 91 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory); | 95 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory); |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 static base::LazyInstance<DefaultDownloadDirectory> | 98 base::LazyInstance<DefaultDownloadDirectory> |
| 95 g_default_download_directory = LAZY_INSTANCE_INITIALIZER; | 99 g_default_download_directory = LAZY_INSTANCE_INITIALIZER; |
| 96 | 100 |
| 101 #if defined(OS_WIN) |
| 102 bool g_adobe_reader_up_to_date = false; |
| 103 |
| 104 // Callback when whether Adobe Reader is up to date is known. |
| 105 void OnGotAdobeReaderInfo(bool up_to_date) { |
| 106 g_adobe_reader_up_to_date = up_to_date; |
| 107 } |
| 108 #endif |
| 109 |
| 97 } // namespace | 110 } // namespace |
| 98 | 111 |
| 99 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) { | 112 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) { |
| 100 PrefService* prefs = profile->GetPrefs(); | 113 PrefService* prefs = profile->GetPrefs(); |
| 101 | 114 |
| 102 #if defined(OS_CHROMEOS) | 115 #if defined(OS_CHROMEOS) |
| 103 // On Chrome OS, the default download directory is different for each profile. | 116 // On Chrome OS, the default download directory is different for each profile. |
| 104 // If the profile-unaware default path (from GetDefaultDownloadDirectory()) | 117 // If the profile-unaware default path (from GetDefaultDownloadDirectory()) |
| 105 // is set (this happens during the initial preference registration in static | 118 // is set (this happens during the initial preference registration in static |
| 106 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile(). | 119 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile(). |
| 107 // file_manager::util::MigratePathFromOldFormat will do this. | 120 // file_manager::util::MigratePathFromOldFormat will do this. |
| 108 const char* path_pref[] = { | 121 const char* path_pref[] = { |
| 109 prefs::kSaveFileDefaultDirectory, | 122 prefs::kSaveFileDefaultDirectory, |
| 110 prefs::kDownloadDefaultDirectory | 123 prefs::kDownloadDefaultDirectory |
| 111 }; | 124 }; |
| 112 for (size_t i = 0; i < arraysize(path_pref); ++i) { | 125 for (size_t i = 0; i < arraysize(path_pref); ++i) { |
| 113 const base::FilePath current = prefs->GetFilePath(path_pref[i]); | 126 const base::FilePath current = prefs->GetFilePath(path_pref[i]); |
| 114 base::FilePath migrated; | 127 base::FilePath migrated; |
| 115 if (!current.empty() && | 128 if (!current.empty() && |
| 116 file_manager::util::MigratePathFromOldFormat( | 129 file_manager::util::MigratePathFromOldFormat( |
| 117 profile_, current, &migrated)) { | 130 profile_, current, &migrated)) { |
| 118 prefs->SetFilePath(path_pref[i], migrated); | 131 prefs->SetFilePath(path_pref[i], migrated); |
| 119 } | 132 } |
| 120 } | 133 } |
| 121 | 134 |
| 122 // Ensure that the default download directory exists. | 135 // Ensure that the default download directory exists. |
| 123 BrowserThread::PostTask( | 136 BrowserThread::PostBlockingPoolTask( |
| 124 BrowserThread::FILE, FROM_HERE, | 137 FROM_HERE, |
| 125 base::Bind(base::IgnoreResult(&base::CreateDirectory), | 138 base::Bind(base::IgnoreResult(&base::CreateDirectory), |
| 126 GetDefaultDownloadDirectoryForProfile())); | 139 GetDefaultDownloadDirectoryForProfile())); |
| 127 #endif // defined(OS_CHROMEOS) | 140 #endif // defined(OS_CHROMEOS) |
| 128 | 141 |
| 142 #if defined(OS_WIN) |
| 143 if (IsAdobeReaderDefaultPDFViewer()) { |
| 144 base::PostTaskAndReplyWithResult( |
| 145 BrowserThread::GetBlockingPool(), |
| 146 FROM_HERE, |
| 147 base::Bind(&::IsAdobeReaderUpToDate), |
| 148 base::Bind(&OnGotAdobeReaderInfo)); |
| 149 } |
| 150 |
| 151 should_open_pdf_in_adobe_reader_ = |
| 152 prefs->GetBoolean(prefs::kOpenPdfDownloadInAdobeReader); |
| 153 #endif |
| 154 |
| 129 // If the download path is dangerous we forcefully reset it. But if we do | 155 // 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 | 156 // 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. | 157 // the user if he really wants it on an unsafe place such as the desktop. |
| 132 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { | 158 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
| 133 base::FilePath current_download_dir = prefs->GetFilePath( | 159 base::FilePath current_download_dir = prefs->GetFilePath( |
| 134 prefs::kDownloadDefaultDirectory); | 160 prefs::kDownloadDefaultDirectory); |
| 135 if (DownloadPathIsDangerous(current_download_dir)) { | 161 if (DownloadPathIsDangerous(current_download_dir)) { |
| 136 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, | 162 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
| 137 GetDefaultDownloadDirectoryForProfile()); | 163 GetDefaultDownloadDirectoryForProfile()); |
| 138 } | 164 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 213 |
| 188 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); | 214 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); |
| 189 registry->RegisterFilePathPref( | 215 registry->RegisterFilePathPref( |
| 190 prefs::kDownloadDefaultDirectory, | 216 prefs::kDownloadDefaultDirectory, |
| 191 default_download_path, | 217 default_download_path, |
| 192 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 218 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 193 registry->RegisterFilePathPref( | 219 registry->RegisterFilePathPref( |
| 194 prefs::kSaveFileDefaultDirectory, | 220 prefs::kSaveFileDefaultDirectory, |
| 195 default_download_path, | 221 default_download_path, |
| 196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 222 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 223 #if defined(OS_WIN) |
| 224 registry->RegisterBooleanPref( |
| 225 prefs::kOpenPdfDownloadInAdobeReader, |
| 226 false, |
| 227 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 228 #endif |
| 197 } | 229 } |
| 198 | 230 |
| 231 #if defined(OS_WIN) |
| 232 // static |
| 233 bool DownloadPrefs::IsAdobeReaderUpToDate() { |
| 234 return g_adobe_reader_up_to_date; |
| 235 } |
| 236 #endif |
| 237 |
| 199 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { | 238 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { |
| 200 #if defined(OS_CHROMEOS) | 239 #if defined(OS_CHROMEOS) |
| 201 return file_manager::util::GetDownloadsFolderForProfile(profile_); | 240 return file_manager::util::GetDownloadsFolderForProfile(profile_); |
| 202 #else | 241 #else |
| 203 return GetDefaultDownloadDirectory(); | 242 return GetDefaultDownloadDirectory(); |
| 204 #endif | 243 #endif |
| 205 } | 244 } |
| 206 | 245 |
| 207 // static | 246 // static |
| 208 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() { | 247 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // always be false. | 300 // always be false. |
| 262 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); | 301 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); |
| 263 return *prompt_for_download_; | 302 return *prompt_for_download_; |
| 264 } | 303 } |
| 265 | 304 |
| 266 bool DownloadPrefs::IsDownloadPathManaged() const { | 305 bool DownloadPrefs::IsDownloadPathManaged() const { |
| 267 return download_path_.IsManaged(); | 306 return download_path_.IsManaged(); |
| 268 } | 307 } |
| 269 | 308 |
| 270 bool DownloadPrefs::IsAutoOpenUsed() const { | 309 bool DownloadPrefs::IsAutoOpenUsed() const { |
| 310 #if defined(OS_WIN) |
| 311 if (ShouldOpenPdfInAdobeReader()) |
| 312 return true; |
| 313 #endif |
| 271 return !auto_open_.empty(); | 314 return !auto_open_.empty(); |
| 272 } | 315 } |
| 273 | 316 |
| 274 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( | 317 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( |
| 275 const base::FilePath& path) const { | 318 const base::FilePath& path) const { |
| 276 base::FilePath::StringType extension = path.Extension(); | 319 base::FilePath::StringType extension = path.Extension(); |
| 277 if (extension.empty()) | 320 if (extension.empty()) |
| 278 return false; | 321 return false; |
| 279 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 322 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 280 extension.erase(0, 1); | 323 extension.erase(0, 1); |
| 324 #if defined(OS_WIN) |
| 325 if (extension == FILE_PATH_LITERAL("pdf") && |
| 326 g_adobe_reader_up_to_date && ShouldOpenPdfInAdobeReader()) |
| 327 return true; |
| 328 #endif |
| 281 return auto_open_.find(extension) != auto_open_.end(); | 329 return auto_open_.find(extension) != auto_open_.end(); |
| 282 } | 330 } |
| 283 | 331 |
| 284 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 332 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
| 285 const base::FilePath& file_name) { | 333 const base::FilePath& file_name) { |
| 286 base::FilePath::StringType extension = file_name.Extension(); | 334 base::FilePath::StringType extension = file_name.Extension(); |
| 287 if (extension.empty()) | 335 if (extension.empty()) |
| 288 return false; | 336 return false; |
| 289 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 337 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 290 extension.erase(0, 1); | 338 extension.erase(0, 1); |
| 291 | 339 |
| 292 auto_open_.insert(extension); | 340 auto_open_.insert(extension); |
| 293 SaveAutoOpenState(); | 341 SaveAutoOpenState(); |
| 294 return true; | 342 return true; |
| 295 } | 343 } |
| 296 | 344 |
| 297 void DownloadPrefs::DisableAutoOpenBasedOnExtension( | 345 void DownloadPrefs::DisableAutoOpenBasedOnExtension( |
| 298 const base::FilePath& file_name) { | 346 const base::FilePath& file_name) { |
| 299 base::FilePath::StringType extension = file_name.Extension(); | 347 base::FilePath::StringType extension = file_name.Extension(); |
| 300 if (extension.empty()) | 348 if (extension.empty()) |
| 301 return; | 349 return; |
| 302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 350 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 303 extension.erase(0, 1); | 351 extension.erase(0, 1); |
| 304 auto_open_.erase(extension); | 352 auto_open_.erase(extension); |
| 305 SaveAutoOpenState(); | 353 SaveAutoOpenState(); |
| 306 } | 354 } |
| 307 | 355 |
| 356 #if defined(OS_WIN) |
| 357 void DownloadPrefs::SetShouldOpenPdfInAdobeReader(bool should_open) { |
| 358 if (should_open_pdf_in_adobe_reader_ == should_open) |
| 359 return; |
| 360 should_open_pdf_in_adobe_reader_ = should_open; |
| 361 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInAdobeReader, |
| 362 should_open); |
| 363 } |
| 364 |
| 365 bool DownloadPrefs::ShouldOpenPdfInAdobeReader() const { |
| 366 return should_open_pdf_in_adobe_reader_; |
| 367 } |
| 368 #endif |
| 369 |
| 308 void DownloadPrefs::ResetAutoOpen() { | 370 void DownloadPrefs::ResetAutoOpen() { |
| 371 #if defined(OS_WIN) |
| 372 SetShouldOpenPdfInAdobeReader(false); |
| 373 #endif |
| 309 auto_open_.clear(); | 374 auto_open_.clear(); |
| 310 SaveAutoOpenState(); | 375 SaveAutoOpenState(); |
| 311 } | 376 } |
| 312 | 377 |
| 313 void DownloadPrefs::SaveAutoOpenState() { | 378 void DownloadPrefs::SaveAutoOpenState() { |
| 314 std::string extensions; | 379 std::string extensions; |
| 315 for (AutoOpenSet::iterator it = auto_open_.begin(); | 380 for (AutoOpenSet::iterator it = auto_open_.begin(); |
| 316 it != auto_open_.end(); ++it) { | 381 it != auto_open_.end(); ++it) { |
| 317 #if defined(OS_POSIX) | 382 #if defined(OS_POSIX) |
| 318 std::string this_extension = *it; | 383 std::string this_extension = *it; |
| 319 #elif defined(OS_WIN) | 384 #elif defined(OS_WIN) |
| 320 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? | 385 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? |
| 321 std::string this_extension = base::SysWideToUTF8(*it); | 386 std::string this_extension = base::SysWideToUTF8(*it); |
| 322 #endif | 387 #endif |
| 323 extensions += this_extension + ":"; | 388 extensions += this_extension + ":"; |
| 324 } | 389 } |
| 325 if (!extensions.empty()) | 390 if (!extensions.empty()) |
| 326 extensions.erase(extensions.size() - 1); | 391 extensions.erase(extensions.size() - 1); |
| 327 | 392 |
| 328 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 393 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
| 329 } | 394 } |
| 330 | 395 |
| 331 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 396 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
| 332 const base::FilePath::StringType& a, | 397 const base::FilePath::StringType& a, |
| 333 const base::FilePath::StringType& b) const { | 398 const base::FilePath::StringType& b) const { |
| 334 return base::FilePath::CompareLessIgnoreCase(a, b); | 399 return base::FilePath::CompareLessIgnoreCase(a, b); |
| 335 } | 400 } |
| OLD | NEW |