| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 45 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 46 : profile_(profile), | 46 : profile_(profile), |
| 47 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { | 47 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { |
| 48 } | 48 } |
| 49 | 49 |
| 50 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 50 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool ChromeDownloadManagerDelegate::IsExtensionDownload( | 53 bool ChromeDownloadManagerDelegate::IsExtensionDownload( |
| 54 const DownloadItem* item) { | 54 const DownloadItem* item) { |
| 55 if (item->prompt_user_for_save_location()) | 55 if (item->PromptUserForSaveLocation()) |
| 56 return false; | 56 return false; |
| 57 | 57 |
| 58 return (item->mime_type() == Extension::kMimeType) || | 58 return (item->GetMimeType() == Extension::kMimeType) || |
| 59 UserScript::IsURLUserScript(item->GetURL(), item->mime_type()); | 59 UserScript::IsURLUserScript(item->GetURL(), item->GetMimeType()); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 62 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
| 63 download_manager_ = dm; | 63 download_manager_ = dm; |
| 64 download_history_.reset(new DownloadHistory(profile_)); | 64 download_history_.reset(new DownloadHistory(profile_)); |
| 65 download_history_->Load( | 65 download_history_->Load( |
| 66 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, | 66 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, |
| 67 base::Unretained(dm))); | 67 base::Unretained(dm))); |
| 68 } | 68 } |
| 69 | 69 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 82 // thread. | 82 // thread. |
| 83 DownloadItem* download = | 83 DownloadItem* download = |
| 84 download_manager_->GetActiveDownloadItem(download_id); | 84 download_manager_->GetActiveDownloadItem(download_id); |
| 85 if (!download) | 85 if (!download) |
| 86 return false; | 86 return false; |
| 87 | 87 |
| 88 #if defined(ENABLE_SAFE_BROWSING) | 88 #if defined(ENABLE_SAFE_BROWSING) |
| 89 // Create a client to verify download URL with safebrowsing. | 89 // Create a client to verify download URL with safebrowsing. |
| 90 // It deletes itself after the callback. | 90 // It deletes itself after the callback. |
| 91 scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient( | 91 scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient( |
| 92 download_id, download->url_chain(), download->referrer_url(), | 92 download_id, download->GetUrlChain(), download->GetReferrerUrl(), |
| 93 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)); | 93 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 94 sb_client->CheckDownloadUrl( | 94 sb_client->CheckDownloadUrl( |
| 95 base::Bind(&ChromeDownloadManagerDelegate::CheckDownloadUrlDone, | 95 base::Bind(&ChromeDownloadManagerDelegate::CheckDownloadUrlDone, |
| 96 base::Unretained(this))); | 96 base::Unretained(this))); |
| 97 #else | 97 #else |
| 98 CheckDownloadUrlDone(download_id, false); | 98 CheckDownloadUrlDone(download_id, false); |
| 99 #endif | 99 #endif |
| 100 return false; | 100 return false; |
| 101 } | 101 } |
| 102 | 102 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 FilePath* intermediate_path) { | 114 FilePath* intermediate_path) { |
| 115 if (item->IsDangerous()) { | 115 if (item->IsDangerous()) { |
| 116 // The download is not safe. It's name is already set to an intermediate | 116 // The download is not safe. It's name is already set to an intermediate |
| 117 // name, so no need to override. | 117 // name, so no need to override. |
| 118 return false; | 118 return false; |
| 119 } | 119 } |
| 120 | 120 |
| 121 // The download is a safe download. We need to rename it to its intermediate | 121 // The download is a safe download. We need to rename it to its intermediate |
| 122 // '.crdownload' path. The final name after user confirmation will be set | 122 // '.crdownload' path. The final name after user confirmation will be set |
| 123 // from DownloadItem::OnDownloadCompleting. | 123 // from DownloadItem::OnDownloadCompleting. |
| 124 *intermediate_path = download_util::GetCrDownloadPath(item->full_path()); | 124 *intermediate_path = download_util::GetCrDownloadPath(item->GetFullPath()); |
| 125 return true; | 125 return true; |
| 126 } | 126 } |
| 127 | 127 |
| 128 TabContents* ChromeDownloadManagerDelegate:: | 128 TabContents* ChromeDownloadManagerDelegate:: |
| 129 GetAlternativeTabContentsToNotifyForDownload() { | 129 GetAlternativeTabContentsToNotifyForDownload() { |
| 130 // Start the download in the last active browser. This is not ideal but better | 130 // Start the download in the last active browser. This is not ideal but better |
| 131 // than fully hiding the download from the user. | 131 // than fully hiding the download from the user. |
| 132 Browser* last_active = BrowserList::GetLastActiveWithProfile(profile_); | 132 Browser* last_active = BrowserList::GetLastActiveWithProfile(profile_); |
| 133 return last_active ? last_active->GetSelectedTabContents() : NULL; | 133 return last_active ? last_active->GetSelectedTabContents() : NULL; |
| 134 } | 134 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 156 // Begin the safe browsing download protection check. | 156 // Begin the safe browsing download protection check. |
| 157 SafeBrowsingService* sb_service = | 157 SafeBrowsingService* sb_service = |
| 158 g_browser_process->safe_browsing_service(); | 158 g_browser_process->safe_browsing_service(); |
| 159 if (sb_service && sb_service->download_protection_service() && | 159 if (sb_service && sb_service->download_protection_service() && |
| 160 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { | 160 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) { |
| 161 using safe_browsing::DownloadProtectionService; | 161 using safe_browsing::DownloadProtectionService; |
| 162 sb_service->download_protection_service()->CheckClientDownload( | 162 sb_service->download_protection_service()->CheckClientDownload( |
| 163 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), | 163 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), |
| 164 base::Bind( | 164 base::Bind( |
| 165 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, | 165 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, |
| 166 this, item->id())); | 166 this, item->GetId())); |
| 167 // For now, we won't delay the download for this. | 167 // For now, we won't delay the download for this. |
| 168 } | 168 } |
| 169 #else | 169 #else |
| 170 // Assume safe. | 170 // Assume safe. |
| 171 #endif | 171 #endif |
| 172 | 172 |
| 173 return true; | 173 return true; |
| 174 } | 174 } |
| 175 | 175 |
| 176 scoped_refptr<CrxInstaller> crx_installer = | 176 scoped_refptr<CrxInstaller> crx_installer = |
| 177 download_crx_util::OpenChromeExtension(profile_, *item); | 177 download_crx_util::OpenChromeExtension(profile_, *item); |
| 178 | 178 |
| 179 // CRX_INSTALLER_DONE will fire when the install completes. Observe() | 179 // CRX_INSTALLER_DONE will fire when the install completes. Observe() |
| 180 // will call DelayedDownloadOpened() on this item. If this DownloadItem is | 180 // will call DelayedDownloadOpened() on this item. If this DownloadItem is |
| 181 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. | 181 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. |
| 182 registrar_.Add(this, | 182 registrar_.Add(this, |
| 183 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 183 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
| 184 content::Source<CrxInstaller>(crx_installer.get())); | 184 content::Source<CrxInstaller>(crx_installer.get())); |
| 185 | 185 |
| 186 crx_installers_[crx_installer.get()] = item->id(); | 186 crx_installers_[crx_installer.get()] = item->GetId(); |
| 187 // The status text and percent complete indicator will change now | 187 // The status text and percent complete indicator will change now |
| 188 // that we are installing a CRX. Update observers so that they pick | 188 // that we are installing a CRX. Update observers so that they pick |
| 189 // up the change. | 189 // up the change. |
| 190 item->UpdateObservers(); | 190 item->UpdateObservers(); |
| 191 return false; | 191 return false; |
| 192 } | 192 } |
| 193 | 193 |
| 194 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 194 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 195 #if defined(ENABLE_SAFE_BROWSING) | 195 #if defined(ENABLE_SAFE_BROWSING) |
| 196 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 196 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 197 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 197 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| 198 #else | 198 #else |
| 199 return false; | 199 return false; |
| 200 #endif | 200 #endif |
| 201 } | 201 } |
| 202 | 202 |
| 203 void ChromeDownloadManagerDelegate::OnResponseCompleted(DownloadItem* item) { | 203 void ChromeDownloadManagerDelegate::OnResponseCompleted(DownloadItem* item) { |
| 204 #if defined(ENABLE_SAFE_BROWSING) | 204 #if defined(ENABLE_SAFE_BROWSING) |
| 205 // When hash is not available, it means either it is not calculated | 205 // When hash is not available, it means either it is not calculated |
| 206 // or there is error while it is calculated. We will skip the download hash | 206 // or there is error while it is calculated. We will skip the download hash |
| 207 // check in that case. | 207 // check in that case. |
| 208 if (item->hash().empty()) | 208 if (item->GetHash().empty()) |
| 209 return; | 209 return; |
| 210 | 210 |
| 211 scoped_refptr<DownloadSBClient> sb_client = | 211 scoped_refptr<DownloadSBClient> sb_client = |
| 212 new DownloadSBClient(item->id(), | 212 new DownloadSBClient(item->GetId(), |
| 213 item->url_chain(), | 213 item->GetUrlChain(), |
| 214 item->referrer_url(), | 214 item->GetReferrerUrl(), |
| 215 profile_->GetPrefs()->GetBoolean( | 215 profile_->GetPrefs()->GetBoolean( |
| 216 prefs::kSafeBrowsingEnabled)); | 216 prefs::kSafeBrowsingEnabled)); |
| 217 sb_client->CheckDownloadHash( | 217 sb_client->CheckDownloadHash( |
| 218 item->hash(), | 218 item->GetHash(), |
| 219 base::Bind(&ChromeDownloadManagerDelegate::CheckDownloadHashDone, | 219 base::Bind(&ChromeDownloadManagerDelegate::CheckDownloadHashDone, |
| 220 base::Unretained(this))); | 220 base::Unretained(this))); |
| 221 #endif | 221 #endif |
| 222 } | 222 } |
| 223 | 223 |
| 224 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | 224 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( |
| 225 DownloadItem* item) { | 225 DownloadItem* item) { |
| 226 download_history_->AddEntry(item, | 226 download_history_->AddEntry(item, |
| 227 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, | 227 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, |
| 228 base::Unretained(this))); | 228 base::Unretained(this))); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 311 |
| 312 DownloadItem* download = | 312 DownloadItem* download = |
| 313 download_manager_->GetActiveDownloadItem(download_id); | 313 download_manager_->GetActiveDownloadItem(download_id); |
| 314 if (!download) | 314 if (!download) |
| 315 return; | 315 return; |
| 316 | 316 |
| 317 if (is_dangerous_url) | 317 if (is_dangerous_url) |
| 318 download->MarkUrlDangerous(); | 318 download->MarkUrlDangerous(); |
| 319 | 319 |
| 320 download_history_->CheckVisitedReferrerBefore( | 320 download_history_->CheckVisitedReferrerBefore( |
| 321 download_id, download->referrer_url(), | 321 download_id, download->GetReferrerUrl(), |
| 322 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 322 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 323 base::Unretained(this))); | 323 base::Unretained(this))); |
| 324 } | 324 } |
| 325 | 325 |
| 326 // content::NotificationObserver implementation. | 326 // content::NotificationObserver implementation. |
| 327 void ChromeDownloadManagerDelegate::Observe( | 327 void ChromeDownloadManagerDelegate::Observe( |
| 328 int type, | 328 int type, |
| 329 const content::NotificationSource& source, | 329 const content::NotificationSource& source, |
| 330 const content::NotificationDetails& details) { | 330 const content::NotificationDetails& details) { |
| 331 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); | 331 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 348 bool visited_referrer_before) { | 348 bool visited_referrer_before) { |
| 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 350 | 350 |
| 351 DownloadItem* download = | 351 DownloadItem* download = |
| 352 download_manager_->GetActiveDownloadItem(download_id); | 352 download_manager_->GetActiveDownloadItem(download_id); |
| 353 if (!download) | 353 if (!download) |
| 354 return; | 354 return; |
| 355 | 355 |
| 356 // Check whether this download is for an extension install or not. | 356 // Check whether this download is for an extension install or not. |
| 357 // Allow extensions to be explicitly saved. | 357 // Allow extensions to be explicitly saved. |
| 358 DownloadStateInfo state = download->state_info(); | 358 DownloadStateInfo state = download->GetStateInfo(); |
| 359 | 359 |
| 360 if (state.force_file_name.empty()) { | 360 if (state.force_file_name.empty()) { |
| 361 FilePath generated_name; | 361 FilePath generated_name; |
| 362 download_util::GenerateFileNameFromRequest(*download, | 362 download_util::GenerateFileNameFromRequest(*download, |
| 363 &generated_name); | 363 &generated_name); |
| 364 | 364 |
| 365 // Freeze the user's preference for showing a Save As dialog. We're going | 365 // Freeze the user's preference for showing a Save As dialog. We're going |
| 366 // to bounce around a bunch of threads and we don't want to worry about race | 366 // to bounce around a bunch of threads and we don't want to worry about race |
| 367 // conditions where the user changes this pref out from under us. | 367 // conditions where the user changes this pref out from under us. |
| 368 if (download_prefs_->PromptForDownload()) { | 368 if (download_prefs_->PromptForDownload()) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 399 IsDangerousFile(*download, state, visited_referrer_before); | 399 IsDangerousFile(*download, state, visited_referrer_before); |
| 400 } | 400 } |
| 401 | 401 |
| 402 // We need to move over to the download thread because we don't want to stat | 402 // We need to move over to the download thread because we don't want to stat |
| 403 // the suggested path on the UI thread. | 403 // the suggested path on the UI thread. |
| 404 // We can only access preferences on the UI thread, so check the download path | 404 // We can only access preferences on the UI thread, so check the download path |
| 405 // now and pass the value to the FILE thread. | 405 // now and pass the value to the FILE thread. |
| 406 BrowserThread::PostTask( | 406 BrowserThread::PostTask( |
| 407 BrowserThread::FILE, FROM_HERE, | 407 BrowserThread::FILE, FROM_HERE, |
| 408 base::Bind(&ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists, | 408 base::Bind(&ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists, |
| 409 this, download->id(), state, | 409 this, download->GetId(), state, |
| 410 download_prefs_->download_path())); | 410 download_prefs_->download_path())); |
| 411 } | 411 } |
| 412 | 412 |
| 413 void ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists( | 413 void ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists( |
| 414 int32 download_id, | 414 int32 download_id, |
| 415 DownloadStateInfo state, | 415 DownloadStateInfo state, |
| 416 const FilePath& default_path) { | 416 const FilePath& default_path) { |
| 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 418 | 418 |
| 419 // Make sure the default download directory exists. | 419 // Make sure the default download directory exists. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 517 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 518 | 518 |
| 519 // Anything loaded directly from the address bar is OK. | 519 // Anything loaded directly from the address bar is OK. |
| 520 if (state.transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 520 if (state.transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 521 return false; | 521 return false; |
| 522 | 522 |
| 523 // Extensions that are not from the gallery are considered dangerous. | 523 // Extensions that are not from the gallery are considered dangerous. |
| 524 if (IsExtensionDownload(&download)) { | 524 if (IsExtensionDownload(&download)) { |
| 525 ExtensionService* service = profile_->GetExtensionService(); | 525 ExtensionService* service = profile_->GetExtensionService(); |
| 526 if (!service || !service->IsDownloadFromGallery(download.GetURL(), | 526 if (!service || !service->IsDownloadFromGallery(download.GetURL(), |
| 527 download.referrer_url())) | 527 download.GetReferrerUrl())) |
| 528 return true; | 528 return true; |
| 529 } | 529 } |
| 530 | 530 |
| 531 // Anything the user has marked auto-open is OK if it's user-initiated. | 531 // Anything the user has marked auto-open is OK if it's user-initiated. |
| 532 if (ShouldOpenFileBasedOnExtension(state.suggested_path) && | 532 if (ShouldOpenFileBasedOnExtension(state.suggested_path) && |
| 533 state.has_user_gesture) | 533 state.has_user_gesture) |
| 534 return false; | 534 return false; |
| 535 | 535 |
| 536 // "Allow on user gesture" is OK when we have a user gesture and the hosting | 536 // "Allow on user gesture" is OK when we have a user gesture and the hosting |
| 537 // page has been visited before today. | 537 // page has been visited before today. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 558 // TODO(noelutz): This function currently works as a callback place holder. | 558 // TODO(noelutz): This function currently works as a callback place holder. |
| 559 // Once we decide the hash check is reliable, we could move the | 559 // Once we decide the hash check is reliable, we could move the |
| 560 // MaybeCompleteDownload in OnAllDataSaved to this function. | 560 // MaybeCompleteDownload in OnAllDataSaved to this function. |
| 561 void ChromeDownloadManagerDelegate::CheckDownloadHashDone( | 561 void ChromeDownloadManagerDelegate::CheckDownloadHashDone( |
| 562 int32 download_id, | 562 int32 download_id, |
| 563 bool is_dangerous_hash) { | 563 bool is_dangerous_hash) { |
| 564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 565 DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id | 565 DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id |
| 566 << " is dangerous_hash: " << is_dangerous_hash; | 566 << " is dangerous_hash: " << is_dangerous_hash; |
| 567 } | 567 } |
| OLD | NEW |