Chromium Code Reviews| 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 char safe_browsing_id[] = "Safe Browsing ID"; | |
|
noelutz
2011/11/16 02:59:38
nit: make that const?
Randy Smith (Not in Mondays)
2011/11/18 02:02:57
Done.
| |
| 51 | |
| 52 // The state of a safebrowsing check. If there is no data associated with the | |
| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 if (Extension::IsExtension(path)) | 164 if (Extension::IsExtension(path)) |
| 149 return false; | 165 return false; |
| 150 DCHECK(extension[0] == FilePath::kExtensionSeparator); | 166 DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 151 extension.erase(0, 1); | 167 extension.erase(0, 1); |
| 152 return download_prefs_->IsAutoOpenEnabledForExtension(extension); | 168 return download_prefs_->IsAutoOpenEnabledForExtension(extension); |
| 153 } | 169 } |
| 154 | 170 |
| 155 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { | 171 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { |
| 156 #if defined(ENABLE_SAFE_BROWSING) | 172 #if defined(ENABLE_SAFE_BROWSING) |
| 157 // See if there is already a pending SafeBrowsing check for that download. | 173 // See if there is already a pending SafeBrowsing check for that download. |
| 158 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->id()); | 174 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( |
| 159 if (it != safe_browsing_state_.end()) { | 175 item->GetExternalData(&safe_browsing_id)); |
| 160 SafeBrowsingState state = it->second; | 176 if (state) |
| 161 if (!state.pending) { | 177 // Don't complete the download until we have an answer. |
| 162 safe_browsing_state_.erase(it); | 178 return !state->pending; |
| 163 } | 179 |
| 164 return !state.pending; | |
| 165 } | |
| 166 // Begin the safe browsing download protection check. | 180 // Begin the safe browsing download protection check. |
| 167 SafeBrowsingService* sb_service = | 181 SafeBrowsingService* sb_service = |
| 168 g_browser_process->safe_browsing_service(); | 182 g_browser_process->safe_browsing_service(); |
| 169 if (sb_service && sb_service->download_protection_service() && | 183 if (sb_service && sb_service->download_protection_service() && |
| 170 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { | 184 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { |
| 171 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " | 185 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " |
| 172 << item->DebugString(false); | 186 << item->DebugString(false); |
| 173 sb_service->download_protection_service()->CheckClientDownload( | 187 sb_service->download_protection_service()->CheckClientDownload( |
| 174 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), | 188 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), |
| 175 base::Bind( | 189 base::Bind( |
| 176 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, | 190 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, |
| 177 this, | 191 this, |
| 178 item->id())); | 192 item->id())); |
| 179 SafeBrowsingState state; | 193 SafeBrowsingState* state(new SafeBrowsingState()); |
| 180 state.pending = true; | 194 state->pending = true; |
| 181 state.verdict = DownloadProtectionService::SAFE; | 195 state->verdict = DownloadProtectionService::SAFE; |
| 182 safe_browsing_state_[item->id()] = state; | 196 item->SetExternalData(&safe_browsing_id, state); |
| 183 return false; | 197 return false; |
| 184 } | 198 } |
| 185 #endif | 199 #endif |
| 186 return true; | 200 return true; |
| 187 } | 201 } |
| 188 | 202 |
| 189 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { | 203 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { |
| 190 if (!IsExtensionDownload(item)) { | 204 if (!IsExtensionDownload(item)) { |
| 191 return true; | 205 return true; |
| 192 } | 206 } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 download_history_->CheckVisitedReferrerBefore( | 330 download_history_->CheckVisitedReferrerBefore( |
| 317 download_id, download->referrer_url(), | 331 download_id, download->referrer_url(), |
| 318 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 332 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 319 base::Unretained(this))); | 333 base::Unretained(this))); |
| 320 } | 334 } |
| 321 | 335 |
| 322 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 336 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
| 323 int32 download_id, | 337 int32 download_id, |
| 324 DownloadProtectionService::DownloadCheckResult result) { | 338 DownloadProtectionService::DownloadCheckResult result) { |
| 325 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); | 339 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); |
| 326 if (!item) { | 340 if (!item) |
| 327 safe_browsing_state_.erase(download_id); // Just in case. | |
| 328 return; | 341 return; |
| 329 } | |
| 330 | 342 |
| 331 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 343 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
| 332 << " verdict = " << result; | 344 << " verdict = " << result; |
| 333 // TODO(noelutz): | 345 // TODO(noelutz): |
| 334 // 1) display a warning if the result is DANGEROUS. | 346 // 1) display a warning if the result is DANGEROUS. |
| 335 // 2) make sure we haven't already displayed a warning for the URL. | 347 // 2) make sure we haven't already displayed a warning for the URL. |
| 336 // 3) disable the existing dangerous file warning for executables. | 348 // 3) disable the existing dangerous file warning for executables. |
| 337 | 349 |
| 338 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->id()); | 350 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( |
| 339 DCHECK(it != safe_browsing_state_.end() && it->second.pending); | 351 item->GetExternalData(&safe_browsing_id)); |
| 340 if (it != safe_browsing_state_.end()) { | 352 DCHECK(state); |
| 341 it->second.pending = false; | 353 state->pending = false; |
|
noelutz
2011/11/16 02:59:38
nit: add an if here? Otherwise, this will crash i
Randy Smith (Not in Mondays)
2011/11/18 02:02:57
I wince, just because when I plot that case forwar
| |
| 342 it->second.verdict = result; | 354 state->verdict = result; |
| 343 } | |
| 344 download_manager_->MaybeCompleteDownload(item); | 355 download_manager_->MaybeCompleteDownload(item); |
| 345 } | 356 } |
| 346 | 357 |
| 347 // content::NotificationObserver implementation. | 358 // content::NotificationObserver implementation. |
| 348 void ChromeDownloadManagerDelegate::Observe( | 359 void ChromeDownloadManagerDelegate::Observe( |
| 349 int type, | 360 int type, |
| 350 const content::NotificationSource& source, | 361 const content::NotificationSource& source, |
| 351 const content::NotificationDetails& details) { | 362 const content::NotificationDetails& details) { |
| 352 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); | 363 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); |
| 353 | 364 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 572 int32 download_id, int64 db_handle) { | 583 int32 download_id, int64 db_handle) { |
| 573 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 584 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 574 // call this function with an invalid |db_handle|. For instance, this can | 585 // call this function with an invalid |db_handle|. For instance, this can |
| 575 // happen when the history database is offline. We cannot have multiple | 586 // happen when the history database is offline. We cannot have multiple |
| 576 // DownloadItems with the same invalid db_handle, so we need to assign a | 587 // DownloadItems with the same invalid db_handle, so we need to assign a |
| 577 // unique |db_handle| here. | 588 // unique |db_handle| here. |
| 578 if (db_handle == DownloadItem::kUninitializedHandle) | 589 if (db_handle == DownloadItem::kUninitializedHandle) |
| 579 db_handle = download_history_->GetNextFakeDbHandle(); | 590 db_handle = download_history_->GetNextFakeDbHandle(); |
| 580 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 591 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
| 581 } | 592 } |
| OLD | NEW |