Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: chrome/browser/download/chrome_download_manager_delegate.cc

Issue 565053002: [Downloads] Gracefully handle SafeBrowsing check failures. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/download/download_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 71
72 #if defined(FULL_SAFE_BROWSING) 72 #if defined(FULL_SAFE_BROWSING)
73 73
74 // String pointer used for identifying safebrowing data associated with 74 // String pointer used for identifying safebrowing data associated with
75 // a download item. 75 // a download item.
76 const char kSafeBrowsingUserDataKey[] = "Safe Browsing ID"; 76 const char kSafeBrowsingUserDataKey[] = "Safe Browsing ID";
77 77
78 // The state of a safebrowsing check. 78 // The state of a safebrowsing check.
79 class SafeBrowsingState : public DownloadCompletionBlocker { 79 class SafeBrowsingState : public DownloadCompletionBlocker {
80 public: 80 public:
81 SafeBrowsingState() 81 SafeBrowsingState() {}
82 : verdict_(DownloadProtectionService::SAFE) {
83 }
84
85 virtual ~SafeBrowsingState(); 82 virtual ~SafeBrowsingState();
86 83
87 // The verdict that we got from calling CheckClientDownload. Only valid to
88 // call if |is_complete()|.
89 DownloadProtectionService::DownloadCheckResult verdict() const {
90 return verdict_;
91 }
92
93 void SetVerdict(DownloadProtectionService::DownloadCheckResult result) {
94 verdict_ = result;
95 CompleteDownload();
96 }
97
98 private: 84 private:
99 DownloadProtectionService::DownloadCheckResult verdict_;
100
101 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingState); 85 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingState);
102 }; 86 };
103 87
104 SafeBrowsingState::~SafeBrowsingState() {} 88 SafeBrowsingState::~SafeBrowsingState() {}
105 89
106 #endif // FULL_SAFE_BROWSING 90 #endif // FULL_SAFE_BROWSING
107 91
108 // Used with GetPlatformDownloadPath() to indicate which platform path to 92 // Used with GetPlatformDownloadPath() to indicate which platform path to
109 // return. 93 // return.
110 enum PlatformDownloadPathType { 94 enum PlatformDownloadPathType {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // Danger type is (in order of preference): 136 // Danger type is (in order of preference):
153 // * DANGEROUS_URL, if the URL is a known malware site. 137 // * DANGEROUS_URL, if the URL is a known malware site.
154 // * MAYBE_DANGEROUS_CONTENT, if the content will be scanned for 138 // * MAYBE_DANGEROUS_CONTENT, if the content will be scanned for
155 // malware. I.e. |is_content_check_supported| is true. 139 // malware. I.e. |is_content_check_supported| is true.
156 // * NOT_DANGEROUS. 140 // * NOT_DANGEROUS.
157 void CheckDownloadUrlDone( 141 void CheckDownloadUrlDone(
158 const DownloadTargetDeterminerDelegate::CheckDownloadUrlCallback& callback, 142 const DownloadTargetDeterminerDelegate::CheckDownloadUrlCallback& callback,
159 bool is_content_check_supported, 143 bool is_content_check_supported,
160 DownloadProtectionService::DownloadCheckResult result) { 144 DownloadProtectionService::DownloadCheckResult result) {
161 content::DownloadDangerType danger_type; 145 content::DownloadDangerType danger_type;
162 if (result == DownloadProtectionService::SAFE) { 146 if (result == DownloadProtectionService::SAFE ||
147 result == DownloadProtectionService::UNKNOWN) {
163 // If this type of files is handled by the enhanced SafeBrowsing download 148 // If this type of files is handled by the enhanced SafeBrowsing download
164 // protection, mark it as potentially dangerous content until we are done 149 // protection, mark it as potentially dangerous content until we are done
165 // with scanning it. 150 // with scanning it.
166 if (is_content_check_supported) 151 if (is_content_check_supported)
167 danger_type = content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT; 152 danger_type = content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT;
168 else 153 else
169 danger_type = content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; 154 danger_type = content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
170 } else { 155 } else {
171 // If the URL is malicious, we'll use that as the danger type. The results 156 // If the URL is malicious, we'll use that as the danger type. The results
172 // of the content check, if one is performed, will be ignored. 157 // of the content check, if one is performed, will be ignored.
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 // static 271 // static
287 void ChromeDownloadManagerDelegate::DisableSafeBrowsing(DownloadItem* item) { 272 void ChromeDownloadManagerDelegate::DisableSafeBrowsing(DownloadItem* item) {
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
289 #if defined(FULL_SAFE_BROWSING) 274 #if defined(FULL_SAFE_BROWSING)
290 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( 275 SafeBrowsingState* state = static_cast<SafeBrowsingState*>(
291 item->GetUserData(&kSafeBrowsingUserDataKey)); 276 item->GetUserData(&kSafeBrowsingUserDataKey));
292 if (!state) { 277 if (!state) {
293 state = new SafeBrowsingState(); 278 state = new SafeBrowsingState();
294 item->SetUserData(&kSafeBrowsingUserDataKey, state); 279 item->SetUserData(&kSafeBrowsingUserDataKey, state);
295 } 280 }
296 state->SetVerdict(DownloadProtectionService::SAFE); 281 state->CompleteDownload();
297 #endif 282 #endif
298 } 283 }
299 284
300 bool ChromeDownloadManagerDelegate::IsDownloadReadyForCompletion( 285 bool ChromeDownloadManagerDelegate::IsDownloadReadyForCompletion(
301 DownloadItem* item, 286 DownloadItem* item,
302 const base::Closure& internal_complete_callback) { 287 const base::Closure& internal_complete_callback) {
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
304 #if defined(FULL_SAFE_BROWSING) 289 #if defined(FULL_SAFE_BROWSING)
305 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( 290 SafeBrowsingState* state = static_cast<SafeBrowsingState*>(
306 item->GetUserData(&kSafeBrowsingUserDataKey)); 291 item->GetUserData(&kSafeBrowsingUserDataKey));
307 if (!state) { 292 if (!state) {
308 // Begin the safe browsing download protection check. 293 // Begin the safe browsing download protection check.
309 DownloadProtectionService* service = GetDownloadProtectionService(); 294 DownloadProtectionService* service = GetDownloadProtectionService();
310 if (service) { 295 if (service) {
311 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " 296 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = "
312 << item->DebugString(false); 297 << item->DebugString(false);
313 state = new SafeBrowsingState(); 298 state = new SafeBrowsingState();
314 state->set_callback(internal_complete_callback); 299 state->set_callback(internal_complete_callback);
315 item->SetUserData(&kSafeBrowsingUserDataKey, state); 300 item->SetUserData(&kSafeBrowsingUserDataKey, state);
316 service->CheckClientDownload( 301 service->CheckClientDownload(
317 item, 302 item,
318 base::Bind( 303 base::Bind(&ChromeDownloadManagerDelegate::CheckClientDownloadDone,
319 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, 304 weak_ptr_factory_.GetWeakPtr(),
320 weak_ptr_factory_.GetWeakPtr(), 305 item->GetId()));
321 item->GetId())); 306 return false;
307 }
308
309 // In case the service was disabled between the download starting and now,
310 // we need to restore the danger state.
311 content::DownloadDangerType danger_type = item->GetDangerType();
312 if (DownloadItemModel(item).IsDangerousFileBasedOnType() &&
313 (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS ||
314 danger_type ==
315 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT)) {
316 DVLOG(2) << __FUNCTION__
317 << "() SB service disabled. Marking download as DANGEROUS FILE";
318 item->OnContentCheckCompleted(
319 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
320 content::BrowserThread::PostTask(
321 content::BrowserThread::UI, FROM_HERE, internal_complete_callback);
322 return false; 322 return false;
323 } 323 }
324 } else if (!state->is_complete()) { 324 } else if (!state->is_complete()) {
325 // Don't complete the download until we have an answer. 325 // Don't complete the download until we have an answer.
326 state->set_callback(internal_complete_callback); 326 state->set_callback(internal_complete_callback);
327 return false; 327 return false;
328 } 328 }
329
329 #endif 330 #endif
330 return true; 331 return true;
331 } 332 }
332 333
333 void ChromeDownloadManagerDelegate::ShouldCompleteDownloadInternal( 334 void ChromeDownloadManagerDelegate::ShouldCompleteDownloadInternal(
334 uint32 download_id, 335 uint32 download_id,
335 const base::Closure& user_complete_callback) { 336 const base::Closure& user_complete_callback) {
336 DownloadItem* item = download_manager_->GetDownload(download_id); 337 DownloadItem* item = download_manager_->GetDownload(download_id);
337 if (!item) 338 if (!item)
338 return; 339 return;
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) 630 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
630 << " verdict = " << result; 631 << " verdict = " << result;
631 // We only mark the content as being dangerous if the download's safety state 632 // We only mark the content as being dangerous if the download's safety state
632 // has not been set to DANGEROUS yet. We don't want to show two warnings. 633 // has not been set to DANGEROUS yet. We don't want to show two warnings.
633 if (item->GetDangerType() == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS || 634 if (item->GetDangerType() == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS ||
634 item->GetDangerType() == 635 item->GetDangerType() ==
635 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT) { 636 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT) {
636 content::DownloadDangerType danger_type = 637 content::DownloadDangerType danger_type =
637 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; 638 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
638 switch (result) { 639 switch (result) {
640 case DownloadProtectionService::UNKNOWN:
641 // The check failed or was inconclusive.
642 if (DownloadItemModel(item).IsDangerousFileBasedOnType())
643 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE;
644 break;
639 case DownloadProtectionService::SAFE: 645 case DownloadProtectionService::SAFE:
640 // Do nothing. 646 // Do nothing.
641 break; 647 break;
642 case DownloadProtectionService::DANGEROUS: 648 case DownloadProtectionService::DANGEROUS:
643 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; 649 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT;
644 break; 650 break;
645 case DownloadProtectionService::UNCOMMON: 651 case DownloadProtectionService::UNCOMMON:
646 danger_type = content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT; 652 danger_type = content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT;
647 break; 653 break;
648 case DownloadProtectionService::DANGEROUS_HOST: 654 case DownloadProtectionService::DANGEROUS_HOST:
649 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST; 655 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST;
650 break; 656 break;
651 case DownloadProtectionService::POTENTIALLY_UNWANTED: 657 case DownloadProtectionService::POTENTIALLY_UNWANTED:
652 danger_type = content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED; 658 danger_type = content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED;
653 break; 659 break;
654 } 660 }
655 661
656 if (danger_type != content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) 662 if (danger_type != content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)
657 item->OnContentCheckCompleted(danger_type); 663 item->OnContentCheckCompleted(danger_type);
658 } 664 }
659 665
660 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( 666 SafeBrowsingState* state = static_cast<SafeBrowsingState*>(
661 item->GetUserData(&kSafeBrowsingUserDataKey)); 667 item->GetUserData(&kSafeBrowsingUserDataKey));
662 state->SetVerdict(result); 668 state->CompleteDownload();
663 } 669 }
664 #endif // FULL_SAFE_BROWSING 670 #endif // FULL_SAFE_BROWSING
665 671
666 // content::NotificationObserver implementation. 672 // content::NotificationObserver implementation.
667 void ChromeDownloadManagerDelegate::Observe( 673 void ChromeDownloadManagerDelegate::Observe(
668 int type, 674 int type,
669 const content::NotificationSource& source, 675 const content::NotificationSource& source,
670 const content::NotificationDetails& details) { 676 const content::NotificationDetails& details) {
671 #if defined(ENABLE_EXTENSIONS) 677 #if defined(ENABLE_EXTENSIONS)
672 DCHECK(type == extensions::NOTIFICATION_CRX_INSTALLER_DONE); 678 DCHECK(type == extensions::NOTIFICATION_CRX_INSTALLER_DONE);
673 679
674 registrar_.Remove(this, extensions::NOTIFICATION_CRX_INSTALLER_DONE, source); 680 registrar_.Remove(this, extensions::NOTIFICATION_CRX_INSTALLER_DONE, source);
675 681
676 scoped_refptr<extensions::CrxInstaller> installer = 682 scoped_refptr<extensions::CrxInstaller> installer =
677 content::Source<extensions::CrxInstaller>(source).ptr(); 683 content::Source<extensions::CrxInstaller>(source).ptr();
678 content::DownloadOpenDelayedCallback callback = 684 content::DownloadOpenDelayedCallback callback =
679 crx_installers_[installer.get()]; 685 crx_installers_[installer.get()];
680 crx_installers_.erase(installer.get()); 686 crx_installers_.erase(installer.get());
681 callback.Run(installer->did_handle_successfully()); 687 callback.Run(installer->did_handle_successfully());
682 #endif 688 #endif
683 } 689 }
684 690
685 void ChromeDownloadManagerDelegate::OnDownloadTargetDetermined( 691 void ChromeDownloadManagerDelegate::OnDownloadTargetDetermined(
686 int32 download_id, 692 int32 download_id,
687 const content::DownloadTargetCallback& callback, 693 const content::DownloadTargetCallback& callback,
688 scoped_ptr<DownloadTargetInfo> target_info) { 694 scoped_ptr<DownloadTargetInfo> target_info) {
689 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
690 DownloadItem* item = download_manager_->GetDownload(download_id); 696 DownloadItem* item = download_manager_->GetDownload(download_id);
691 if (!target_info->target_path.empty() && item && 697 if (item) {
692 IsOpenInBrowserPreferreredForFile(target_info->target_path) && 698 if (!target_info->target_path.empty() &&
693 target_info->is_filetype_handled_safely) 699 IsOpenInBrowserPreferreredForFile(target_info->target_path) &&
694 DownloadItemModel(item).SetShouldPreferOpeningInBrowser(true); 700 target_info->is_filetype_handled_safely)
701 DownloadItemModel(item).SetShouldPreferOpeningInBrowser(true);
702
703 if (target_info->is_dangerous_file)
704 DownloadItemModel(item).SetIsDangerousFileBasedOnType(true);
705 }
695 callback.Run(target_info->target_path, 706 callback.Run(target_info->target_path,
696 target_info->target_disposition, 707 target_info->target_disposition,
697 target_info->danger_type, 708 target_info->danger_type,
698 target_info->intermediate_path); 709 target_info->intermediate_path);
699 } 710 }
700 711
701 bool ChromeDownloadManagerDelegate::IsOpenInBrowserPreferreredForFile( 712 bool ChromeDownloadManagerDelegate::IsOpenInBrowserPreferreredForFile(
702 const base::FilePath& path) { 713 const base::FilePath& path) {
703 // On Windows, PDFs should open in Acrobat Reader if the user chooses. 714 // On Windows, PDFs should open in Acrobat Reader if the user chooses.
704 #if defined(OS_WIN) 715 #if defined(OS_WIN)
(...skipping 17 matching lines...) Expand all
722 path.MatchesExtension(FILE_PATH_LITERAL(".xht")) || 733 path.MatchesExtension(FILE_PATH_LITERAL(".xht")) ||
723 path.MatchesExtension(FILE_PATH_LITERAL(".xhtm")) || 734 path.MatchesExtension(FILE_PATH_LITERAL(".xhtm")) ||
724 path.MatchesExtension(FILE_PATH_LITERAL(".xhtml")) || 735 path.MatchesExtension(FILE_PATH_LITERAL(".xhtml")) ||
725 path.MatchesExtension(FILE_PATH_LITERAL(".xsl")) || 736 path.MatchesExtension(FILE_PATH_LITERAL(".xsl")) ||
726 path.MatchesExtension(FILE_PATH_LITERAL(".xslt"))) { 737 path.MatchesExtension(FILE_PATH_LITERAL(".xslt"))) {
727 return true; 738 return true;
728 } 739 }
729 #endif 740 #endif
730 return false; 741 return false;
731 } 742 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/download/download_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698