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

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

Issue 8503018: Split DownloadItem into an ABC, an Impl, and a Mock. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: merge Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) 46 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile)
47 : profile_(profile), 47 : profile_(profile),
48 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { 48 download_prefs_(new DownloadPrefs(profile->GetPrefs())) {
49 } 49 }
50 50
51 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 51 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
52 } 52 }
53 53
54 bool ChromeDownloadManagerDelegate::IsExtensionDownload( 54 bool ChromeDownloadManagerDelegate::IsExtensionDownload(
55 const DownloadItem* item) { 55 const DownloadItem* item) {
56 if (item->prompt_user_for_save_location()) 56 if (item->PromptUserForSaveLocation())
57 return false; 57 return false;
58 58
59 return (item->mime_type() == Extension::kMimeType) || 59 return (item->GetMimeType() == Extension::kMimeType) ||
60 UserScript::IsURLUserScript(item->GetURL(), item->mime_type()); 60 UserScript::IsURLUserScript(item->GetURL(), item->GetMimeType());
61 } 61 }
62 62
63 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { 63 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) {
64 download_manager_ = dm; 64 download_manager_ = dm;
65 download_history_.reset(new DownloadHistory(profile_)); 65 download_history_.reset(new DownloadHistory(profile_));
66 download_history_->Load( 66 download_history_->Load(
67 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, 67 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete,
68 base::Unretained(dm))); 68 base::Unretained(dm)));
69 } 69 }
70 70
(...skipping 18 matching lines...) Expand all
89 #if defined(ENABLE_SAFE_BROWSING) 89 #if defined(ENABLE_SAFE_BROWSING)
90 DownloadProtectionService* service = GetDownloadProtectionService(); 90 DownloadProtectionService* service = GetDownloadProtectionService();
91 if (service) { 91 if (service) {
92 VLOG(2) << __FUNCTION__ << "() Start SB URL check for download = " 92 VLOG(2) << __FUNCTION__ << "() Start SB URL check for download = "
93 << download->DebugString(false); 93 << download->DebugString(false);
94 service->CheckDownloadUrl( 94 service->CheckDownloadUrl(
95 DownloadProtectionService::DownloadInfo::FromDownloadItem(*download), 95 DownloadProtectionService::DownloadInfo::FromDownloadItem(*download),
96 base::Bind( 96 base::Bind(
97 &ChromeDownloadManagerDelegate::CheckDownloadUrlDone, 97 &ChromeDownloadManagerDelegate::CheckDownloadUrlDone,
98 this, 98 this,
99 download->id())); 99 download->GetId()));
100 return false; 100 return false;
101 } 101 }
102 #endif 102 #endif
103 CheckDownloadUrlDone(download_id, DownloadProtectionService::SAFE); 103 CheckDownloadUrlDone(download_id, DownloadProtectionService::SAFE);
104 return false; 104 return false;
105 } 105 }
106 106
107 void ChromeDownloadManagerDelegate::ChooseDownloadPath( 107 void ChromeDownloadManagerDelegate::ChooseDownloadPath(
108 TabContents* tab_contents, 108 TabContents* tab_contents,
109 const FilePath& suggested_path, 109 const FilePath& suggested_path,
110 void* data) { 110 void* data) {
111 // Deletes itself. 111 // Deletes itself.
112 new DownloadFilePicker( 112 new DownloadFilePicker(
113 download_manager_, tab_contents, suggested_path, data); 113 download_manager_, tab_contents, suggested_path, data);
114 } 114 }
115 115
116 bool ChromeDownloadManagerDelegate::OverrideIntermediatePath( 116 bool ChromeDownloadManagerDelegate::OverrideIntermediatePath(
117 DownloadItem* item, 117 DownloadItem* item,
118 FilePath* intermediate_path) { 118 FilePath* intermediate_path) {
119 if (item->GetDangerType() != DownloadStateInfo::NOT_DANGEROUS) { 119 if (item->GetDangerType() != DownloadStateInfo::NOT_DANGEROUS) {
120 // The download might not be safe. It's name is already set to an 120 // The download might not be safe. It's name is already set to an
121 // intermediate name, so no need to override. 121 // intermediate name, so no need to override.
122 return false; 122 return false;
123 } 123 }
124 124
125 // The download is a safe download. We need to rename it to its intermediate 125 // The download is a safe download. We need to rename it to its intermediate
126 // '.crdownload' path. The final name after user confirmation will be set 126 // '.crdownload' path. The final name after user confirmation will be set
127 // from DownloadItem::OnDownloadCompleting. 127 // from DownloadItem::OnDownloadCompleting.
128 *intermediate_path = download_util::GetCrDownloadPath(item->full_path()); 128 *intermediate_path = download_util::GetCrDownloadPath(item->GetFullPath());
129 return true; 129 return true;
130 } 130 }
131 131
132 TabContents* ChromeDownloadManagerDelegate:: 132 TabContents* ChromeDownloadManagerDelegate::
133 GetAlternativeTabContentsToNotifyForDownload() { 133 GetAlternativeTabContentsToNotifyForDownload() {
134 // Start the download in the last active browser. This is not ideal but better 134 // Start the download in the last active browser. This is not ideal but better
135 // than fully hiding the download from the user. 135 // than fully hiding the download from the user.
136 Browser* last_active = BrowserList::GetLastActiveWithProfile(profile_); 136 Browser* last_active = BrowserList::GetLastActiveWithProfile(profile_);
137 return last_active ? last_active->GetSelectedTabContents() : NULL; 137 return last_active ? last_active->GetSelectedTabContents() : NULL;
138 } 138 }
139 139
140 140
141 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( 141 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension(
142 const FilePath& path) { 142 const FilePath& path) {
143 FilePath::StringType extension = path.Extension(); 143 FilePath::StringType extension = path.Extension();
144 if (extension.empty()) 144 if (extension.empty())
145 return false; 145 return false;
146 if (Extension::IsExtension(path)) 146 if (Extension::IsExtension(path))
147 return false; 147 return false;
148 DCHECK(extension[0] == FilePath::kExtensionSeparator); 148 DCHECK(extension[0] == FilePath::kExtensionSeparator);
149 extension.erase(0, 1); 149 extension.erase(0, 1);
150 return download_prefs_->IsAutoOpenEnabledForExtension(extension); 150 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
151 } 151 }
152 152
153 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { 153 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) {
154 #if defined(ENABLE_SAFE_BROWSING) 154 #if defined(ENABLE_SAFE_BROWSING)
155 // See if there is already a pending SafeBrowsing check for that download. 155 // See if there is already a pending SafeBrowsing check for that download.
156 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->id()); 156 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->GetId());
157 if (it != safe_browsing_state_.end()) { 157 if (it != safe_browsing_state_.end()) {
158 SafeBrowsingState state = it->second; 158 SafeBrowsingState state = it->second;
159 if (!state.pending) { 159 if (!state.pending) {
160 safe_browsing_state_.erase(it); 160 safe_browsing_state_.erase(it);
161 } 161 }
162 return !state.pending; 162 return !state.pending;
163 } 163 }
164 // Begin the safe browsing download protection check. 164 // Begin the safe browsing download protection check.
165 DownloadProtectionService* service = GetDownloadProtectionService(); 165 DownloadProtectionService* service = GetDownloadProtectionService();
166 if (service) { 166 if (service) {
167 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " 167 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = "
168 << item->DebugString(false); 168 << item->DebugString(false);
169 service->CheckClientDownload( 169 service->CheckClientDownload(
170 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), 170 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item),
171 base::Bind( 171 base::Bind(
172 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, 172 &ChromeDownloadManagerDelegate::CheckClientDownloadDone,
173 this, 173 this,
174 item->id())); 174 item->GetId()));
175 SafeBrowsingState state; 175 SafeBrowsingState state;
176 state.pending = true; 176 state.pending = true;
177 state.verdict = DownloadProtectionService::SAFE; 177 state.verdict = DownloadProtectionService::SAFE;
178 safe_browsing_state_[item->id()] = state; 178 safe_browsing_state_[item->GetId()] = state;
179 return false; 179 return false;
180 } 180 }
181 #endif 181 #endif
182 return true; 182 return true;
183 } 183 }
184 184
185 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { 185 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) {
186 if (!IsExtensionDownload(item)) { 186 if (!IsExtensionDownload(item)) {
187 return true; 187 return true;
188 } 188 }
189 189
190 scoped_refptr<CrxInstaller> crx_installer = 190 scoped_refptr<CrxInstaller> crx_installer =
191 download_crx_util::OpenChromeExtension(profile_, *item); 191 download_crx_util::OpenChromeExtension(profile_, *item);
192 192
193 // CRX_INSTALLER_DONE will fire when the install completes. Observe() 193 // CRX_INSTALLER_DONE will fire when the install completes. Observe()
194 // will call DelayedDownloadOpened() on this item. If this DownloadItem is 194 // will call DelayedDownloadOpened() on this item. If this DownloadItem is
195 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. 195 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called.
196 registrar_.Add(this, 196 registrar_.Add(this,
197 chrome::NOTIFICATION_CRX_INSTALLER_DONE, 197 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
198 content::Source<CrxInstaller>(crx_installer.get())); 198 content::Source<CrxInstaller>(crx_installer.get()));
199 199
200 crx_installers_[crx_installer.get()] = item->id(); 200 crx_installers_[crx_installer.get()] = item->GetId();
201 // The status text and percent complete indicator will change now 201 // The status text and percent complete indicator will change now
202 // that we are installing a CRX. Update observers so that they pick 202 // that we are installing a CRX. Update observers so that they pick
203 // up the change. 203 // up the change.
204 item->UpdateObservers(); 204 item->UpdateObservers();
205 return false; 205 return false;
206 } 206 }
207 207
208 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 208 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
209 #if defined(ENABLE_SAFE_BROWSING) 209 #if defined(ENABLE_SAFE_BROWSING)
210 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 210 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 download_manager_->GetActiveDownloadItem(download_id); 315 download_manager_->GetActiveDownloadItem(download_id);
316 if (!download) 316 if (!download)
317 return; 317 return;
318 318
319 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) 319 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false)
320 << " verdict = " << result; 320 << " verdict = " << result;
321 if (result == DownloadProtectionService::DANGEROUS) 321 if (result == DownloadProtectionService::DANGEROUS)
322 download->MarkUrlDangerous(); 322 download->MarkUrlDangerous();
323 323
324 download_history_->CheckVisitedReferrerBefore( 324 download_history_->CheckVisitedReferrerBefore(
325 download_id, download->referrer_url(), 325 download_id, download->GetReferrerUrl(),
326 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, 326 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone,
327 base::Unretained(this))); 327 base::Unretained(this)));
328 } 328 }
329 329
330 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( 330 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
331 int32 download_id, 331 int32 download_id,
332 DownloadProtectionService::DownloadCheckResult result) { 332 DownloadProtectionService::DownloadCheckResult result) {
333 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); 333 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id);
334 if (!item) { 334 if (!item) {
335 safe_browsing_state_.erase(download_id); // Just in case. 335 safe_browsing_state_.erase(download_id); // Just in case.
336 return; 336 return;
337 } 337 }
338 338
339 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) 339 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
340 << " verdict = " << result; 340 << " verdict = " << result;
341 // We only mark the content as being dangerous if the download's safety state 341 // 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. 342 // has not been set to DANGEROUS yet. We don't want to show two warnings.
343 if (result == DownloadProtectionService::DANGEROUS && 343 if (result == DownloadProtectionService::DANGEROUS &&
344 item->safety_state() == DownloadItem::SAFE) 344 item->GetSafetyState() == DownloadItem::SAFE)
345 item->MarkContentDangerous(); 345 item->MarkContentDangerous();
346 346
347 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->id()); 347 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->GetId());
348 DCHECK(it != safe_browsing_state_.end() && it->second.pending); 348 DCHECK(it != safe_browsing_state_.end() && it->second.pending);
349 if (it != safe_browsing_state_.end()) { 349 if (it != safe_browsing_state_.end()) {
350 it->second.pending = false; 350 it->second.pending = false;
351 it->second.verdict = result; 351 it->second.verdict = result;
352 } 352 }
353 download_manager_->MaybeCompleteDownload(item); 353 download_manager_->MaybeCompleteDownload(item);
354 } 354 }
355 355
356 // content::NotificationObserver implementation. 356 // content::NotificationObserver implementation.
357 void ChromeDownloadManagerDelegate::Observe( 357 void ChromeDownloadManagerDelegate::Observe(
(...skipping 20 matching lines...) Expand all
378 bool visited_referrer_before) { 378 bool visited_referrer_before) {
379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
380 380
381 DownloadItem* download = 381 DownloadItem* download =
382 download_manager_->GetActiveDownloadItem(download_id); 382 download_manager_->GetActiveDownloadItem(download_id);
383 if (!download) 383 if (!download)
384 return; 384 return;
385 385
386 // Check whether this download is for an extension install or not. 386 // Check whether this download is for an extension install or not.
387 // Allow extensions to be explicitly saved. 387 // Allow extensions to be explicitly saved.
388 DownloadStateInfo state = download->state_info(); 388 DownloadStateInfo state = download->GetStateInfo();
389 389
390 if (state.force_file_name.empty()) { 390 if (state.force_file_name.empty()) {
391 FilePath generated_name; 391 FilePath generated_name;
392 download_util::GenerateFileNameFromRequest(*download, 392 download_util::GenerateFileNameFromRequest(*download,
393 &generated_name); 393 &generated_name);
394 394
395 // Freeze the user's preference for showing a Save As dialog. We're going 395 // Freeze the user's preference for showing a Save As dialog. We're going
396 // to bounce around a bunch of threads and we don't want to worry about race 396 // to bounce around a bunch of threads and we don't want to worry about race
397 // conditions where the user changes this pref out from under us. 397 // conditions where the user changes this pref out from under us.
398 if (download_prefs_->PromptForDownload()) { 398 if (download_prefs_->PromptForDownload()) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 } 443 }
444 #endif 444 #endif
445 445
446 // We need to move over to the download thread because we don't want to stat 446 // We need to move over to the download thread because we don't want to stat
447 // the suggested path on the UI thread. 447 // the suggested path on the UI thread.
448 // We can only access preferences on the UI thread, so check the download path 448 // We can only access preferences on the UI thread, so check the download path
449 // now and pass the value to the FILE thread. 449 // now and pass the value to the FILE thread.
450 BrowserThread::PostTask( 450 BrowserThread::PostTask(
451 BrowserThread::FILE, FROM_HERE, 451 BrowserThread::FILE, FROM_HERE,
452 base::Bind(&ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists, 452 base::Bind(&ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists,
453 this, download->id(), state, 453 this, download->GetId(), state,
454 download_prefs_->download_path())); 454 download_prefs_->download_path()));
455 } 455 }
456 456
457 void ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists( 457 void ChromeDownloadManagerDelegate::CheckIfSuggestedPathExists(
458 int32 download_id, 458 int32 download_id,
459 DownloadStateInfo state, 459 DownloadStateInfo state,
460 const FilePath& default_path) { 460 const FilePath& default_path) {
461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
462 462
463 // Make sure the default download directory exists. 463 // Make sure the default download directory exists.
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
562 562
563 // Anything loaded directly from the address bar is OK. 563 // Anything loaded directly from the address bar is OK.
564 if (state.transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) 564 if (state.transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR)
565 return false; 565 return false;
566 566
567 // Extensions that are not from the gallery are considered dangerous. 567 // Extensions that are not from the gallery are considered dangerous.
568 if (IsExtensionDownload(&download)) { 568 if (IsExtensionDownload(&download)) {
569 ExtensionService* service = profile_->GetExtensionService(); 569 ExtensionService* service = profile_->GetExtensionService();
570 if (!service || !service->IsDownloadFromGallery(download.GetURL(), 570 if (!service || !service->IsDownloadFromGallery(download.GetURL(),
571 download.referrer_url())) 571 download.GetReferrerUrl()))
572 return true; 572 return true;
573 } 573 }
574 574
575 // Anything the user has marked auto-open is OK if it's user-initiated. 575 // Anything the user has marked auto-open is OK if it's user-initiated.
576 if (ShouldOpenFileBasedOnExtension(state.suggested_path) && 576 if (ShouldOpenFileBasedOnExtension(state.suggested_path) &&
577 state.has_user_gesture) 577 state.has_user_gesture)
578 return false; 578 return false;
579 579
580 // "Allow on user gesture" is OK when we have a user gesture and the hosting 580 // "Allow on user gesture" is OK when we have a user gesture and the hosting
581 // page has been visited before today. 581 // page has been visited before today.
582 download_util::DownloadDangerLevel danger_level = 582 download_util::DownloadDangerLevel danger_level =
583 download_util::GetFileDangerLevel(state.suggested_path.BaseName()); 583 download_util::GetFileDangerLevel(state.suggested_path.BaseName());
584 if (danger_level == download_util::AllowOnUserGesture) 584 if (danger_level == download_util::AllowOnUserGesture)
585 return !state.has_user_gesture || !visited_referrer_before; 585 return !state.has_user_gesture || !visited_referrer_before;
586 586
587 return danger_level == download_util::Dangerous; 587 return danger_level == download_util::Dangerous;
588 } 588 }
589 589
590 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore( 590 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore(
591 int32 download_id, int64 db_handle) { 591 int32 download_id, int64 db_handle) {
592 // It's not immediately obvious, but HistoryBackend::CreateDownload() can 592 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
593 // call this function with an invalid |db_handle|. For instance, this can 593 // call this function with an invalid |db_handle|. For instance, this can
594 // happen when the history database is offline. We cannot have multiple 594 // 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 595 // DownloadItems with the same invalid db_handle, so we need to assign a
596 // unique |db_handle| here. 596 // unique |db_handle| here.
597 if (db_handle == DownloadItem::kUninitializedHandle) 597 if (db_handle == DownloadItem::kUninitializedHandle)
598 db_handle = download_history_->GetNextFakeDbHandle(); 598 db_handle = download_history_->GetNextFakeDbHandle();
599 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); 599 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle);
600 } 600 }
OLDNEW
« no previous file with comments | « chrome/browser/automation/testing_automation_provider.cc ('k') | chrome/browser/download/download_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698