| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/sys_string_conversions.h" | 21 #include "base/strings/sys_string_conversions.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 24 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 25 #include "chrome/browser/download/download_target_determiner.h" | 25 #include "chrome/browser/download/download_target_determiner.h" |
| 26 #include "chrome/browser/download/trusted_sources_manager.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/profiles/profile_manager.h" | 28 #include "chrome/browser/profiles/profile_manager.h" |
| 28 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
| 29 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 30 #include "chrome/common/safe_browsing/file_type_policies.h" | 31 #include "chrome/common/safe_browsing/file_type_policies.h" |
| 31 #include "components/pref_registry/pref_registry_syncable.h" | 32 #include "components/pref_registry/pref_registry_syncable.h" |
| 32 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 33 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/browser/download_item.h" |
| 34 #include "content/public/browser/download_manager.h" | 36 #include "content/public/browser/download_manager.h" |
| 35 #include "content/public/browser/save_page_type.h" | 37 #include "content/public/browser/save_page_type.h" |
| 36 | 38 |
| 37 #if defined(OS_CHROMEOS) | 39 #if defined(OS_CHROMEOS) |
| 38 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 40 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
| 39 #include "chrome/browser/chromeos/drive/file_system_util.h" | 41 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 40 #include "chrome/browser/chromeos/file_manager/path_util.h" | 42 #include "chrome/browser/chromeos/file_manager/path_util.h" |
| 41 #endif | 43 #endif |
| 42 | 44 |
| 43 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, | 151 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
| 150 GetDefaultDownloadDirectoryForProfile()); | 152 GetDefaultDownloadDirectoryForProfile()); |
| 151 } | 153 } |
| 152 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); | 154 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); |
| 153 } | 155 } |
| 154 | 156 |
| 155 prompt_for_download_.Init(prefs::kPromptForDownload, prefs); | 157 prompt_for_download_.Init(prefs::kPromptForDownload, prefs); |
| 156 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs); | 158 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs); |
| 157 save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs); | 159 save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs); |
| 158 save_file_type_.Init(prefs::kSaveFileType, prefs); | 160 save_file_type_.Init(prefs::kSaveFileType, prefs); |
| 161 safebrowsing_for_trusted_sources_enabled_.Init( |
| 162 prefs::kSafeBrowsingForTrustedSourcesEnabled, prefs); |
| 159 download_restriction_.Init(prefs::kDownloadRestrictions, prefs); | 163 download_restriction_.Init(prefs::kDownloadRestrictions, prefs); |
| 160 | 164 |
| 161 // We store any file extension that should be opened automatically at | 165 // We store any file extension that should be opened automatically at |
| 162 // download completion in this pref. | 166 // download completion in this pref. |
| 163 std::string extensions_to_open = | 167 std::string extensions_to_open = |
| 164 prefs->GetString(prefs::kDownloadExtensionsToOpen); | 168 prefs->GetString(prefs::kDownloadExtensionsToOpen); |
| 165 | 169 |
| 166 for (const auto& extension_string : base::SplitString( | 170 for (const auto& extension_string : base::SplitString( |
| 167 extensions_to_open, ":", | 171 extensions_to_open, ":", |
| 168 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 172 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 181 // IsAllowedToOpenAutomatically() needs a filename. | 185 // IsAllowedToOpenAutomatically() needs a filename. |
| 182 base::FilePath filename_with_extension = base::FilePath( | 186 base::FilePath filename_with_extension = base::FilePath( |
| 183 base::FilePath::StringType(1, base::FilePath::kExtensionSeparator) + | 187 base::FilePath::StringType(1, base::FilePath::kExtensionSeparator) + |
| 184 extension); | 188 extension); |
| 185 | 189 |
| 186 // Note that the list of file types that are not allowed to open | 190 // Note that the list of file types that are not allowed to open |
| 187 // automatically can change in the future. When the list is tightened, it is | 191 // automatically can change in the future. When the list is tightened, it is |
| 188 // expected that some entries in the users' auto open list will get dropped | 192 // expected that some entries in the users' auto open list will get dropped |
| 189 // permanently as a result. | 193 // permanently as a result. |
| 190 if (FileTypePolicies::GetInstance()->IsAllowedToOpenAutomatically( | 194 if (FileTypePolicies::GetInstance()->IsAllowedToOpenAutomatically( |
| 191 filename_with_extension)) | 195 filename_with_extension)) { |
| 192 auto_open_.insert(extension); | 196 auto_open_.insert(extension); |
| 197 } |
| 193 } | 198 } |
| 194 } | 199 } |
| 195 | 200 |
| 196 DownloadPrefs::~DownloadPrefs() {} | 201 DownloadPrefs::~DownloadPrefs() {} |
| 197 | 202 |
| 198 // static | 203 // static |
| 199 void DownloadPrefs::RegisterProfilePrefs( | 204 void DownloadPrefs::RegisterProfilePrefs( |
| 200 user_prefs::PrefRegistrySyncable* registry) { | 205 user_prefs::PrefRegistrySyncable* registry) { |
| 201 registry->RegisterBooleanPref( | 206 registry->RegisterBooleanPref( |
| 202 prefs::kPromptForDownload, | 207 prefs::kPromptForDownload, |
| 203 false, | 208 false, |
| 204 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 209 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 205 registry->RegisterStringPref(prefs::kDownloadExtensionsToOpen, std::string()); | 210 registry->RegisterStringPref(prefs::kDownloadExtensionsToOpen, std::string()); |
| 206 registry->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); | 211 registry->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); |
| 207 registry->RegisterIntegerPref(prefs::kSaveFileType, | 212 registry->RegisterIntegerPref(prefs::kSaveFileType, |
| 208 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); | 213 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); |
| 214 registry->RegisterIntegerPref(prefs::kDownloadRestrictions, 0); |
| 215 registry->RegisterBooleanPref(prefs::kSafeBrowsingForTrustedSourcesEnabled, |
| 216 true); |
| 209 | 217 |
| 210 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); | 218 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); |
| 211 registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, | 219 registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, |
| 212 default_download_path); | 220 default_download_path); |
| 213 registry->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, | 221 registry->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, |
| 214 default_download_path); | 222 default_download_path); |
| 215 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) | 223 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) |
| 216 registry->RegisterBooleanPref(prefs::kOpenPdfDownloadInSystemReader, false); | 224 registry->RegisterBooleanPref(prefs::kOpenPdfDownloadInSystemReader, false); |
| 217 #endif | 225 #endif |
| 218 } | 226 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 238 download_manager->GetDelegate()); | 246 download_manager->GetDelegate()); |
| 239 return delegate->download_prefs(); | 247 return delegate->download_prefs(); |
| 240 } | 248 } |
| 241 | 249 |
| 242 // static | 250 // static |
| 243 DownloadPrefs* DownloadPrefs::FromBrowserContext( | 251 DownloadPrefs* DownloadPrefs::FromBrowserContext( |
| 244 content::BrowserContext* context) { | 252 content::BrowserContext* context) { |
| 245 return FromDownloadManager(BrowserContext::GetDownloadManager(context)); | 253 return FromDownloadManager(BrowserContext::GetDownloadManager(context)); |
| 246 } | 254 } |
| 247 | 255 |
| 256 bool DownloadPrefs::IsFromTrustedSource(const content::DownloadItem& item) { |
| 257 if (!trusted_sources_manager_) |
| 258 trusted_sources_manager_.reset(TrustedSourcesManager::Create()); |
| 259 return trusted_sources_manager_->IsFromTrustedSource(item.GetURL()); |
| 260 } |
| 261 |
| 248 base::FilePath DownloadPrefs::DownloadPath() const { | 262 base::FilePath DownloadPrefs::DownloadPath() const { |
| 249 #if defined(OS_CHROMEOS) | 263 #if defined(OS_CHROMEOS) |
| 250 // If the download path is under /drive, and DriveIntegrationService isn't | 264 // If the download path is under /drive, and DriveIntegrationService isn't |
| 251 // available (which it isn't for incognito mode, for instance), use the | 265 // available (which it isn't for incognito mode, for instance), use the |
| 252 // default download directory (/Downloads). | 266 // default download directory (/Downloads). |
| 253 if (drive::util::IsUnderDriveMountPoint(*download_path_)) { | 267 if (drive::util::IsUnderDriveMountPoint(*download_path_)) { |
| 254 drive::DriveIntegrationService* integration_service = | 268 drive::DriveIntegrationService* integration_service = |
| 255 drive::DriveIntegrationServiceFactory::FindForProfile(profile_); | 269 drive::DriveIntegrationServiceFactory::FindForProfile(profile_); |
| 256 if (!integration_service || !integration_service->is_enabled()) | 270 if (!integration_service || !integration_service->is_enabled()) |
| 257 return GetDefaultDownloadDirectoryForProfile(); | 271 return GetDefaultDownloadDirectoryForProfile(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 return true; | 322 return true; |
| 309 #endif | 323 #endif |
| 310 | 324 |
| 311 return auto_open_.find(extension) != auto_open_.end(); | 325 return auto_open_.find(extension) != auto_open_.end(); |
| 312 } | 326 } |
| 313 | 327 |
| 314 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 328 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
| 315 const base::FilePath& file_name) { | 329 const base::FilePath& file_name) { |
| 316 base::FilePath::StringType extension = file_name.Extension(); | 330 base::FilePath::StringType extension = file_name.Extension(); |
| 317 if (!FileTypePolicies::GetInstance()->IsAllowedToOpenAutomatically( | 331 if (!FileTypePolicies::GetInstance()->IsAllowedToOpenAutomatically( |
| 318 file_name)) | 332 file_name)) { |
| 319 return false; | 333 return false; |
| 334 } |
| 320 | 335 |
| 321 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 336 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
| 322 extension.erase(0, 1); | 337 extension.erase(0, 1); |
| 323 | 338 |
| 324 auto_open_.insert(extension); | 339 auto_open_.insert(extension); |
| 325 SaveAutoOpenState(); | 340 SaveAutoOpenState(); |
| 326 return true; | 341 return true; |
| 327 } | 342 } |
| 328 | 343 |
| 329 void DownloadPrefs::DisableAutoOpenBasedOnExtension( | 344 void DownloadPrefs::DisableAutoOpenBasedOnExtension( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 extensions.erase(extensions.size() - 1); | 396 extensions.erase(extensions.size() - 1); |
| 382 | 397 |
| 383 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 398 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
| 384 } | 399 } |
| 385 | 400 |
| 386 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 401 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
| 387 const base::FilePath::StringType& a, | 402 const base::FilePath::StringType& a, |
| 388 const base::FilePath::StringType& b) const { | 403 const base::FilePath::StringType& b) const { |
| 389 return base::FilePath::CompareLessIgnoreCase(a, b); | 404 return base::FilePath::CompareLessIgnoreCase(a, b); |
| 390 } | 405 } |
| OLD | NEW |