| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "content/browser/download/download_manager.h" | 36 #include "content/browser/download/download_manager.h" |
| 37 #include "content/browser/download/download_status_updater.h" | 37 #include "content/browser/download/download_status_updater.h" |
| 38 #include "content/browser/tab_contents/tab_contents.h" | 38 #include "content/browser/tab_contents/tab_contents.h" |
| 39 #include "content/public/browser/notification_source.h" | 39 #include "content/public/browser/notification_source.h" |
| 40 #include "grit/generated_resources.h" | 40 #include "grit/generated_resources.h" |
| 41 #include "ui/base/l10n/l10n_util.h" | 41 #include "ui/base/l10n/l10n_util.h" |
| 42 | 42 |
| 43 using content::BrowserThread; | 43 using content::BrowserThread; |
| 44 using safe_browsing::DownloadProtectionService; | 44 using safe_browsing::DownloadProtectionService; |
| 45 | 45 |
| 46 namespace { |
| 47 |
| 48 // String pointer used for identifying safebrowing data associated with |
| 49 // a download item. |
| 50 static const char safe_browsing_id[] = "Safe Browsing ID"; |
| 51 |
| 52 // The state of a safebrowsing check. |
| 53 struct SafeBrowsingState : public DownloadItem::ExternalData { |
| 54 // If true the SafeBrowsing check is not done yet. |
| 55 bool pending; |
| 56 // The verdict that we got from calling CheckClientDownload. |
| 57 safe_browsing::DownloadProtectionService::DownloadCheckResult verdict; |
| 58 }; |
| 59 |
| 60 } |
| 61 |
| 46 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 62 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 47 : profile_(profile), | 63 : profile_(profile), |
| 48 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { | 64 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { |
| 49 } | 65 } |
| 50 | 66 |
| 51 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 67 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 52 } | 68 } |
| 53 | 69 |
| 54 bool ChromeDownloadManagerDelegate::IsExtensionDownload( | 70 bool ChromeDownloadManagerDelegate::IsExtensionDownload( |
| 55 const DownloadItem* item) { | 71 const DownloadItem* item) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 if (Extension::IsExtension(path)) | 162 if (Extension::IsExtension(path)) |
| 147 return false; | 163 return false; |
| 148 DCHECK(extension[0] == FilePath::kExtensionSeparator); | 164 DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 149 extension.erase(0, 1); | 165 extension.erase(0, 1); |
| 150 return download_prefs_->IsAutoOpenEnabledForExtension(extension); | 166 return download_prefs_->IsAutoOpenEnabledForExtension(extension); |
| 151 } | 167 } |
| 152 | 168 |
| 153 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { | 169 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { |
| 154 #if defined(ENABLE_SAFE_BROWSING) | 170 #if defined(ENABLE_SAFE_BROWSING) |
| 155 // See if there is already a pending SafeBrowsing check for that download. | 171 // See if there is already a pending SafeBrowsing check for that download. |
| 156 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->GetId()); | 172 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( |
| 157 if (it != safe_browsing_state_.end()) { | 173 item->GetExternalData(&safe_browsing_id)); |
| 158 SafeBrowsingState state = it->second; | 174 if (state) |
| 159 if (!state.pending) { | 175 // Don't complete the download until we have an answer. |
| 160 safe_browsing_state_.erase(it); | 176 return !state->pending; |
| 161 } | 177 |
| 162 return !state.pending; | |
| 163 } | |
| 164 // Begin the safe browsing download protection check. | 178 // Begin the safe browsing download protection check. |
| 165 DownloadProtectionService* service = GetDownloadProtectionService(); | 179 DownloadProtectionService* service = GetDownloadProtectionService(); |
| 166 if (service) { | 180 if (service) { |
| 167 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " | 181 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " |
| 168 << item->DebugString(false); | 182 << item->DebugString(false); |
| 183 state = new SafeBrowsingState(); |
| 184 state->pending = true; |
| 185 state->verdict = DownloadProtectionService::SAFE; |
| 186 item->SetExternalData(&safe_browsing_id, state); |
| 169 service->CheckClientDownload( | 187 service->CheckClientDownload( |
| 170 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), | 188 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), |
| 171 base::Bind( | 189 base::Bind( |
| 172 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, | 190 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, |
| 173 this, | 191 this, |
| 174 item->GetId())); | 192 item->GetId())); |
| 175 SafeBrowsingState state; | |
| 176 state.pending = true; | |
| 177 state.verdict = DownloadProtectionService::SAFE; | |
| 178 safe_browsing_state_[item->GetId()] = state; | |
| 179 return false; | 193 return false; |
| 180 } | 194 } |
| 181 #endif | 195 #endif |
| 182 return true; | 196 return true; |
| 183 } | 197 } |
| 184 | 198 |
| 185 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { | 199 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { |
| 186 if (!IsExtensionDownload(item)) { | 200 if (!IsExtensionDownload(item)) { |
| 187 return true; | 201 return true; |
| 188 } | 202 } |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 download_history_->CheckVisitedReferrerBefore( | 338 download_history_->CheckVisitedReferrerBefore( |
| 325 download_id, download->GetReferrerUrl(), | 339 download_id, download->GetReferrerUrl(), |
| 326 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 340 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 327 base::Unretained(this))); | 341 base::Unretained(this))); |
| 328 } | 342 } |
| 329 | 343 |
| 330 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 344 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
| 331 int32 download_id, | 345 int32 download_id, |
| 332 DownloadProtectionService::DownloadCheckResult result) { | 346 DownloadProtectionService::DownloadCheckResult result) { |
| 333 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); | 347 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); |
| 334 if (!item) { | 348 if (!item) |
| 335 safe_browsing_state_.erase(download_id); // Just in case. | |
| 336 return; | 349 return; |
| 337 } | |
| 338 | 350 |
| 339 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 351 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
| 340 << " verdict = " << result; | 352 << " verdict = " << result; |
| 341 // We only mark the content as being dangerous if the download's safety state | 353 // We only mark the content as being dangerous if the download's safety state |
| 342 // has not been set to DANGEROUS yet. We don't want to show two warnings. | 354 // has not been set to DANGEROUS yet. We don't want to show two warnings. |
| 343 if (result == DownloadProtectionService::DANGEROUS && | 355 if (result == DownloadProtectionService::DANGEROUS && |
| 344 item->GetSafetyState() == DownloadItem::SAFE) | 356 item->GetSafetyState() == DownloadItem::SAFE) |
| 345 item->MarkContentDangerous(); | 357 item->MarkContentDangerous(); |
| 346 | 358 |
| 347 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->GetId()); | 359 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( |
| 348 DCHECK(it != safe_browsing_state_.end() && it->second.pending); | 360 item->GetExternalData(&safe_browsing_id)); |
| 349 if (it != safe_browsing_state_.end()) { | 361 DCHECK(state); |
| 350 it->second.pending = false; | 362 if (state) { |
| 351 it->second.verdict = result; | 363 state->pending = false; |
| 364 state->verdict = result; |
| 352 } | 365 } |
| 353 item->MaybeCompleteDownload(); | 366 item->MaybeCompleteDownload(); |
| 354 } | 367 } |
| 355 | 368 |
| 356 // content::NotificationObserver implementation. | 369 // content::NotificationObserver implementation. |
| 357 void ChromeDownloadManagerDelegate::Observe( | 370 void ChromeDownloadManagerDelegate::Observe( |
| 358 int type, | 371 int type, |
| 359 const content::NotificationSource& source, | 372 const content::NotificationSource& source, |
| 360 const content::NotificationDetails& details) { | 373 const content::NotificationDetails& details) { |
| 361 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); | 374 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 int32 download_id, int64 db_handle) { | 604 int32 download_id, int64 db_handle) { |
| 592 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 605 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 593 // call this function with an invalid |db_handle|. For instance, this can | 606 // call this function with an invalid |db_handle|. For instance, this can |
| 594 // happen when the history database is offline. We cannot have multiple | 607 // happen when the history database is offline. We cannot have multiple |
| 595 // DownloadItems with the same invalid db_handle, so we need to assign a | 608 // DownloadItems with the same invalid db_handle, so we need to assign a |
| 596 // unique |db_handle| here. | 609 // unique |db_handle| here. |
| 597 if (db_handle == DownloadItem::kUninitializedHandle) | 610 if (db_handle == DownloadItem::kUninitializedHandle) |
| 598 db_handle = download_history_->GetNextFakeDbHandle(); | 611 db_handle = download_history_->GetNextFakeDbHandle(); |
| 599 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 612 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
| 600 } | 613 } |
| OLD | NEW |