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/chrome_download_manager_delegate.h" | 5 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 #include "chrome/common/extensions/extension.h" | 37 #include "chrome/common/extensions/extension.h" |
| 38 #include "chrome/common/pref_names.h" | 38 #include "chrome/common/pref_names.h" |
| 39 #include "components/user_prefs/pref_registry_syncable.h" | 39 #include "components/user_prefs/pref_registry_syncable.h" |
| 40 #include "content/public/browser/download_item.h" | 40 #include "content/public/browser/download_item.h" |
| 41 #include "content/public/browser/download_manager.h" | 41 #include "content/public/browser/download_manager.h" |
| 42 #include "content/public/browser/notification_source.h" | 42 #include "content/public/browser/notification_source.h" |
| 43 | 43 |
| 44 #if defined(OS_CHROMEOS) | 44 #if defined(OS_CHROMEOS) |
| 45 #include "chrome/browser/chromeos/drive/download_handler.h" | 45 #include "chrome/browser/chromeos/drive/download_handler.h" |
| 46 #include "chrome/browser/chromeos/drive/file_system_util.h" | 46 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 47 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | |
| 48 #endif | 47 #endif |
| 49 | 48 |
| 50 using content::BrowserContext; | |
| 51 using content::BrowserThread; | 49 using content::BrowserThread; |
| 52 using content::DownloadId; | 50 using content::DownloadId; |
| 53 using content::DownloadItem; | 51 using content::DownloadItem; |
| 54 using content::DownloadManager; | 52 using content::DownloadManager; |
| 55 using safe_browsing::DownloadProtectionService; | 53 using safe_browsing::DownloadProtectionService; |
| 56 | 54 |
| 57 namespace { | 55 namespace { |
| 58 | 56 |
| 59 // String pointer used for identifying safebrowing data associated with | 57 // String pointer used for identifying safebrowing data associated with |
| 60 // a download item. | 58 // a download item. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 } else { | 128 } else { |
| 131 // If the URL is malicious, we'll use that as the danger type. The results | 129 // If the URL is malicious, we'll use that as the danger type. The results |
| 132 // of the content check, if one is performed, will be ignored. | 130 // of the content check, if one is performed, will be ignored. |
| 133 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 131 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
| 134 } | 132 } |
| 135 callback.Run(danger_type); | 133 callback.Run(danger_type); |
| 136 } | 134 } |
| 137 | 135 |
| 138 } // namespace | 136 } // namespace |
| 139 | 137 |
| 140 // static | |
| 141 void ChromeDownloadManagerDelegate::RegisterUserPrefs( | |
| 142 user_prefs::PrefRegistrySyncable* registry) { | |
| 143 const base::FilePath& default_download_path = | |
| 144 download_util::GetDefaultDownloadDirectory(); | |
| 145 registry->RegisterFilePathPref( | |
| 146 prefs::kSaveFileDefaultDirectory, | |
| 147 default_download_path, | |
| 148 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 149 } | |
| 150 | |
| 151 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 138 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 152 : profile_(profile), | 139 : profile_(profile), |
| 153 next_download_id_(0), | 140 next_download_id_(0), |
| 154 download_prefs_(new DownloadPrefs(profile)) { | 141 download_prefs_(new DownloadPrefs(profile)) { |
| 155 } | 142 } |
| 156 | 143 |
| 157 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 144 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 158 } | 145 } |
| 159 | 146 |
| 160 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 147 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
| 161 download_manager_ = dm; | 148 download_manager_ = dm; |
| 162 } | 149 } |
| 163 | 150 |
| 164 void ChromeDownloadManagerDelegate::Shutdown() { | 151 void ChromeDownloadManagerDelegate::Shutdown() { |
| 165 download_prefs_.reset(); | 152 download_prefs_.reset(); |
| 166 } | 153 } |
| 167 | 154 |
| 168 DownloadId ChromeDownloadManagerDelegate::GetNextId() { | 155 DownloadId ChromeDownloadManagerDelegate::GetNextId() { |
| 169 if (!profile_->IsOffTheRecord()) | 156 if (!profile_->IsOffTheRecord()) |
| 170 return DownloadId(this, next_download_id_++); | 157 return DownloadId(this, next_download_id_++); |
| 171 | 158 |
| 172 return BrowserContext::GetDownloadManager(profile_->GetOriginalProfile())-> | 159 return content::BrowserContext::GetDownloadManager( |
| 173 GetDelegate()->GetNextId(); | 160 profile_->GetOriginalProfile())->GetDelegate()->GetNextId(); |
| 174 } | 161 } |
| 175 | 162 |
| 176 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( | 163 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( |
| 177 DownloadItem* download, | 164 DownloadItem* download, |
| 178 const content::DownloadTargetCallback& callback) { | 165 const content::DownloadTargetCallback& callback) { |
| 179 DownloadTargetDeterminer::Start(download, | 166 DownloadTargetDeterminer::Start(download, |
| 180 download_prefs_.get(), | 167 download_prefs_.get(), |
| 181 last_download_path_, | |
| 182 this, | 168 this, |
| 183 callback); | 169 callback); |
| 184 return true; | 170 return true; |
| 185 } | 171 } |
| 186 | 172 |
| 187 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( | 173 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( |
| 188 const base::FilePath& path) { | 174 const base::FilePath& path) { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 190 if (path.Extension().empty()) | 176 if (path.Extension().empty()) |
| 191 return false; | 177 return false; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 275 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 290 #if defined(FULL_SAFE_BROWSING) | 276 #if defined(FULL_SAFE_BROWSING) |
| 291 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 277 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 292 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 278 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| 293 #else | 279 #else |
| 294 return false; | 280 return false; |
| 295 #endif | 281 #endif |
| 296 } | 282 } |
| 297 | 283 |
| 298 void ChromeDownloadManagerDelegate::GetSaveDir( | 284 void ChromeDownloadManagerDelegate::GetSaveDir( |
| 299 BrowserContext* browser_context, | 285 content::BrowserContext* browser_context, |
| 300 base::FilePath* website_save_dir, | 286 base::FilePath* website_save_dir, |
| 301 base::FilePath* download_save_dir, | 287 base::FilePath* download_save_dir, |
| 302 bool* skip_dir_check) { | 288 bool* skip_dir_check) { |
| 303 Profile* profile = Profile::FromBrowserContext(browser_context); | 289 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 304 PrefService* prefs = profile->GetPrefs(); | 290 PrefService* prefs = profile->GetPrefs(); |
| 305 | 291 |
| 306 // Check whether the preference for the preferred directory for | |
| 307 // saving file has been explicitly set. If not, and the preference | |
| 308 // for the default download directory has been set, initialize it | |
| 309 // with the latter. Note that the defaults for both are the same. | |
| 310 const PrefService::Preference* download_default_directory = | |
| 311 prefs->FindPreference(prefs::kDownloadDefaultDirectory); | |
| 312 if (!download_default_directory->IsDefaultValue() && | |
| 313 prefs->FindPreference( | |
| 314 prefs::kSaveFileDefaultDirectory)->IsDefaultValue()) { | |
| 315 prefs->Set(prefs::kSaveFileDefaultDirectory, | |
| 316 *(download_default_directory->GetValue())); | |
| 317 } | |
| 318 | |
| 319 // Get the directory from preference. | 292 // Get the directory from preference. |
| 320 *website_save_dir = prefs->GetFilePath(prefs::kSaveFileDefaultDirectory); | 293 *website_save_dir = download_prefs_->SaveFilePath(); |
| 321 DCHECK(!website_save_dir->empty()); | 294 DCHECK(!website_save_dir->empty()); |
| 322 | 295 |
| 323 *download_save_dir = prefs->GetFilePath(prefs::kDownloadDefaultDirectory); | 296 *download_save_dir = prefs->GetFilePath(prefs::kDownloadDefaultDirectory); |
| 324 | 297 |
| 325 *skip_dir_check = false; | 298 *skip_dir_check = false; |
| 326 #if defined(OS_CHROMEOS) | 299 #if defined(OS_CHROMEOS) |
| 327 *skip_dir_check = drive::util::IsUnderDriveMountPoint(*website_save_dir); | 300 *skip_dir_check = drive::util::IsUnderDriveMountPoint(*website_save_dir); |
| 328 #endif | 301 #endif |
| 329 } | 302 } |
| 330 | 303 |
| 331 void ChromeDownloadManagerDelegate::ChooseSavePath( | 304 void ChromeDownloadManagerDelegate::ChooseSavePath( |
| 332 content::WebContents* web_contents, | 305 content::WebContents* web_contents, |
| 333 const base::FilePath& suggested_path, | 306 const base::FilePath& suggested_path, |
| 334 const base::FilePath::StringType& default_extension, | 307 const base::FilePath::StringType& default_extension, |
| 335 bool can_save_as_complete, | 308 bool can_save_as_complete, |
| 336 const content::SavePackagePathPickedCallback& callback) { | 309 const content::SavePackagePathPickedCallback& callback) { |
| 337 // Deletes itself. | 310 // Deletes itself. |
| 338 #if defined(OS_CHROMEOS) | |
| 339 new SavePackageFilePickerChromeOS( | |
| 340 web_contents, | |
| 341 suggested_path, | |
| 342 can_save_as_complete, | |
| 343 callback); | |
| 344 #else | |
| 345 new SavePackageFilePicker( | 311 new SavePackageFilePicker( |
| 346 web_contents, | 312 web_contents, |
| 347 suggested_path, | 313 suggested_path, |
| 348 default_extension, | 314 default_extension, |
| 349 can_save_as_complete, | 315 can_save_as_complete, |
| 350 download_prefs_.get(), | 316 download_prefs_.get(), |
| 351 callback); | 317 callback); |
| 352 #endif | |
| 353 } | 318 } |
| 354 | 319 |
| 355 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { | 320 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { |
| 356 platform_util::OpenItem(GetPlatformDownloadPath(profile_, download)); | 321 platform_util::OpenItem(GetPlatformDownloadPath(profile_, download)); |
| 357 } | 322 } |
| 358 | 323 |
| 359 void ChromeDownloadManagerDelegate::ShowDownloadInShell( | 324 void ChromeDownloadManagerDelegate::ShowDownloadInShell( |
| 360 DownloadItem* download) { | 325 DownloadItem* download) { |
| 361 platform_util::ShowItemInFolder(GetPlatformDownloadPath(profile_, download)); | 326 platform_util::ShowItemInFolder(GetPlatformDownloadPath(profile_, download)); |
| 362 } | 327 } |
| 363 | 328 |
| 364 void ChromeDownloadManagerDelegate::CheckForFileExistence( | 329 void ChromeDownloadManagerDelegate::CheckForFileExistence( |
| 365 DownloadItem* download, | 330 DownloadItem* download, |
| 366 const content::CheckForFileExistenceCallback& callback) { | 331 const content::CheckForFileExistenceCallback& callback) { |
| 367 #if defined(OS_CHROMEOS) | 332 #if defined(OS_CHROMEOS) |
| 368 drive::DownloadHandler* drive_download_handler = | 333 drive::DownloadHandler* drive_download_handler = |
| 369 drive::DownloadHandler::GetForProfile(profile_); | 334 drive::DownloadHandler::GetForProfile(profile_); |
| 370 if (drive_download_handler && | 335 if (drive_download_handler && |
| 371 drive_download_handler->IsDriveDownload(download)) { | 336 drive_download_handler->IsDriveDownload(download)) { |
| 372 drive_download_handler->CheckForFileExistence(download, callback); | 337 drive_download_handler->CheckForFileExistence(download, callback); |
| 373 return; | 338 return; |
| 374 } | 339 } |
| 375 #endif | 340 #endif |
| 376 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::FILE, FROM_HERE, | 341 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::FILE, FROM_HERE, |
| 377 base::Bind(&file_util::PathExists, | 342 base::Bind(&file_util::PathExists, |
| 378 download->GetFullPath()), | 343 download->GetFullPath()), |
| 379 callback); | 344 callback); |
| 380 } | 345 } |
| 381 | 346 |
| 382 void ChromeDownloadManagerDelegate::ClearLastDownloadPath() { | |
| 383 last_download_path_.clear(); | |
| 384 } | |
| 385 | |
| 386 DownloadProtectionService* | 347 DownloadProtectionService* |
| 387 ChromeDownloadManagerDelegate::GetDownloadProtectionService() { | 348 ChromeDownloadManagerDelegate::GetDownloadProtectionService() { |
| 388 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 389 #if defined(FULL_SAFE_BROWSING) | 350 #if defined(FULL_SAFE_BROWSING) |
| 390 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); | 351 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); |
| 391 if (sb_service && sb_service->download_protection_service() && | 352 if (sb_service && sb_service->download_protection_service() && |
| 392 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { | 353 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { |
| 393 return sb_service->download_protection_service(); | 354 return sb_service->download_protection_service(); |
| 394 } | 355 } |
| 395 #endif | 356 #endif |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) { | 406 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) { |
| 446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 447 DownloadFilePicker::ShowFilePicker( | 408 DownloadFilePicker::ShowFilePicker( |
| 448 download, | 409 download, |
| 449 suggested_path, | 410 suggested_path, |
| 450 base::Bind(&ChromeDownloadManagerDelegate::OnDownloadPathSelected, | 411 base::Bind(&ChromeDownloadManagerDelegate::OnDownloadPathSelected, |
| 451 this, | 412 this, |
| 452 callback)); | 413 callback)); |
| 453 } | 414 } |
| 454 | 415 |
| 455 void ChromeDownloadManagerDelegate::OnDownloadPathSelected( | 416 void ChromeDownloadManagerDelegate::OnDownloadPathSelected( |
|
asanka
2013/05/07 15:15:12
Suggestion: This callback isn't required since set
benjhayden
2013/05/17 20:31:54
Done.
| |
| 456 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback, | 417 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback, |
| 457 const base::FilePath& virtual_path) { | 418 const base::FilePath& virtual_path) { |
| 458 if (!virtual_path.empty()) | 419 if (!virtual_path.empty()) |
| 459 last_download_path_ = virtual_path.DirName(); | 420 download_prefs_->SetSaveFilePath(virtual_path.DirName()); |
| 460 callback.Run(virtual_path); | 421 callback.Run(virtual_path); |
| 461 } | 422 } |
| 462 | 423 |
| 463 void ChromeDownloadManagerDelegate::DetermineLocalPath( | 424 void ChromeDownloadManagerDelegate::DetermineLocalPath( |
| 464 DownloadItem* download, | 425 DownloadItem* download, |
| 465 const base::FilePath& virtual_path, | 426 const base::FilePath& virtual_path, |
| 466 const DownloadTargetDeterminerDelegate::LocalPathCallback& callback) { | 427 const DownloadTargetDeterminerDelegate::LocalPathCallback& callback) { |
| 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 468 #if defined(OS_CHROMEOS) | 429 #if defined(OS_CHROMEOS) |
| 469 drive::DownloadHandler* drive_download_handler = | 430 drive::DownloadHandler* drive_download_handler = |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 549 registrar_.Remove(this, | 510 registrar_.Remove(this, |
| 550 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 511 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
| 551 source); | 512 source); |
| 552 | 513 |
| 553 scoped_refptr<extensions::CrxInstaller> installer = | 514 scoped_refptr<extensions::CrxInstaller> installer = |
| 554 content::Source<extensions::CrxInstaller>(source).ptr(); | 515 content::Source<extensions::CrxInstaller>(source).ptr(); |
| 555 content::DownloadOpenDelayedCallback callback = crx_installers_[installer]; | 516 content::DownloadOpenDelayedCallback callback = crx_installers_[installer]; |
| 556 crx_installers_.erase(installer.get()); | 517 crx_installers_.erase(installer.get()); |
| 557 callback.Run(installer->did_handle_successfully()); | 518 callback.Run(installer->did_handle_successfully()); |
| 558 } | 519 } |
| OLD | NEW |