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

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

Issue 8536041: This CL integrates the new SafeBrowsing download service class (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: fix unit-tests 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"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/rand_util.h" 12 #include "base/rand_util.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/time.h"
14 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/download/download_crx_util.h" 16 #include "chrome/browser/download/download_crx_util.h"
16 #include "chrome/browser/download/download_extensions.h" 17 #include "chrome/browser/download/download_extensions.h"
17 #include "chrome/browser/download/download_file_picker.h" 18 #include "chrome/browser/download/download_file_picker.h"
18 #include "chrome/browser/download/download_history.h" 19 #include "chrome/browser/download/download_history.h"
19 #include "chrome/browser/download/download_prefs.h" 20 #include "chrome/browser/download/download_prefs.h"
20 #include "chrome/browser/download/download_safe_browsing_client.h"
21 #include "chrome/browser/download/download_util.h" 21 #include "chrome/browser/download/download_util.h"
22 #include "chrome/browser/download/save_package_file_picker.h" 22 #include "chrome/browser/download/save_package_file_picker.h"
23 #include "chrome/browser/extensions/crx_installer.h" 23 #include "chrome/browser/extensions/crx_installer.h"
24 #include "chrome/browser/extensions/extension_service.h" 24 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/prefs/pref_member.h" 25 #include "chrome/browser/prefs/pref_member.h"
26 #include "chrome/browser/prefs/pref_service.h" 26 #include "chrome/browser/prefs/pref_service.h"
27 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 28 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
29 #include "chrome/browser/ui/browser.h" 29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_list.h" 30 #include "chrome/browser/ui/browser_list.h"
31 #include "chrome/common/chrome_paths.h" 31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/extensions/user_script.h" 32 #include "chrome/common/extensions/user_script.h"
33 #include "chrome/common/pref_names.h" 33 #include "chrome/common/pref_names.h"
34 #include "content/browser/download/download_file.h" 34 #include "content/browser/download/download_file.h"
35 #include "content/browser/download/download_item.h" 35 #include "content/browser/download/download_item.h"
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 45
45 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) 46 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile)
46 : profile_(profile), 47 : profile_(profile),
47 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { 48 download_prefs_(new DownloadPrefs(profile->GetPrefs())) {
48 } 49 }
49 50
50 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 51 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
51 } 52 }
52 53
53 bool ChromeDownloadManagerDelegate::IsExtensionDownload( 54 bool ChromeDownloadManagerDelegate::IsExtensionDownload(
(...skipping 24 matching lines...) Expand all
78 // history service thread is still reading the persistent state, we do not 79 // history service thread is still reading the persistent state, we do not
79 // insert the new DownloadItem into 'history_downloads_' or inform our 80 // insert the new DownloadItem into 'history_downloads_' or inform our
80 // observers at this point. OnCreateDownloadEntryComplete() handles that 81 // observers at this point. OnCreateDownloadEntryComplete() handles that
81 // finalization of the the download creation as a callback from the history 82 // finalization of the the download creation as a callback from the history
82 // thread. 83 // thread.
83 DownloadItem* download = 84 DownloadItem* download =
84 download_manager_->GetActiveDownloadItem(download_id); 85 download_manager_->GetActiveDownloadItem(download_id);
85 if (!download) 86 if (!download)
86 return false; 87 return false;
87 88
88 #if defined(ENABLE_SAFE_BROWSING) 89 #if defined(ENABLE_SAFE_BROWSING)
Randy Smith (Not in Mondays) 2011/11/14 19:43:00 Remind me what the context around this define is?
noelutz 2011/11/14 20:30:42 SafeBrowsing is currently disabled (and compiled o
89 // Create a client to verify download URL with safebrowsing. 90 SafeBrowsingService* sb_service =
90 // It deletes itself after the callback. 91 g_browser_process->safe_browsing_service();
91 scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient( 92 if (sb_service && sb_service->download_protection_service() &&
92 download_id, download->url_chain(), download->referrer_url(), 93 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) {
93 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)); 94 VLOG(2) << __FUNCTION__ << "() Start SB URL check for download = "
94 sb_client->CheckDownloadUrl( 95 << download->DebugString(false);
95 base::Bind(&ChromeDownloadManagerDelegate::CheckDownloadUrlDone, 96 sb_service->download_protection_service()->CheckDownloadUrl(
96 base::Unretained(this))); 97 DownloadProtectionService::DownloadInfo::FromDownloadItem(*download),
97 #else 98 base::Bind(
98 CheckDownloadUrlDone(download_id, false); 99 &ChromeDownloadManagerDelegate::CheckDownloadUrlDone,
100 this,
101 download->id()));
102 return false;
103 }
99 #endif 104 #endif
105 CheckDownloadUrlDone(download_id, DownloadProtectionService::SAFE);
100 return false; 106 return false;
101 } 107 }
102 108
103 void ChromeDownloadManagerDelegate::ChooseDownloadPath( 109 void ChromeDownloadManagerDelegate::ChooseDownloadPath(
104 TabContents* tab_contents, 110 TabContents* tab_contents,
105 const FilePath& suggested_path, 111 const FilePath& suggested_path,
106 void* data) { 112 void* data) {
107 // Deletes itself. 113 // Deletes itself.
108 new DownloadFilePicker( 114 new DownloadFilePicker(
109 download_manager_, tab_contents, suggested_path, data); 115 download_manager_, tab_contents, suggested_path, data);
(...skipping 30 matching lines...) Expand all
140 if (extension.empty()) 146 if (extension.empty())
141 return false; 147 return false;
142 if (Extension::IsExtension(path)) 148 if (Extension::IsExtension(path))
143 return false; 149 return false;
144 DCHECK(extension[0] == FilePath::kExtensionSeparator); 150 DCHECK(extension[0] == FilePath::kExtensionSeparator);
145 extension.erase(0, 1); 151 extension.erase(0, 1);
146 return download_prefs_->IsAutoOpenEnabledForExtension(extension); 152 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
147 } 153 }
148 154
149 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { 155 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) {
156 #if defined(ENABLE_SAFE_BROWSING)
157 // See if there is already a pending SafeBrowsing check for that download.
158 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->id());
159 if (it != safe_browsing_state_.end()) {
160 SafeBrowsingState state = it->second;
161 if (!state.pending) {
162 safe_browsing_state_.erase(it);
163 }
164 return !state.pending;
165 }
166 // Begin the safe browsing download protection check.
167 SafeBrowsingService* sb_service =
168 g_browser_process->safe_browsing_service();
169 if (sb_service && sb_service->download_protection_service() &&
170 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) {
171 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = "
172 << item->DebugString(false);
173 sb_service->download_protection_service()->CheckClientDownload(
174 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item),
175 base::Bind(
176 &ChromeDownloadManagerDelegate::CheckClientDownloadDone,
177 this,
178 item->id()));
179 SafeBrowsingState state;
180 state.pending = true;
181 safe_browsing_state_[item->id()] = state;
182 return false;
183 }
184 #endif
150 return true; 185 return true;
151 } 186 }
152 187
153 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { 188 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) {
154 if (!IsExtensionDownload(item)) { 189 if (!IsExtensionDownload(item)) {
155 #if defined(ENABLE_SAFE_BROWSING)
156 // Begin the safe browsing download protection check.
157 SafeBrowsingService* sb_service =
158 g_browser_process->safe_browsing_service();
159 if (sb_service && sb_service->download_protection_service() &&
160 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) {
161 using safe_browsing::DownloadProtectionService;
162 sb_service->download_protection_service()->CheckClientDownload(
163 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item),
164 base::Bind(
165 &ChromeDownloadManagerDelegate::CheckClientDownloadDone,
166 this, item->id()));
167 // For now, we won't delay the download for this.
168 }
169 #else
170 // Assume safe.
171 #endif
172
173 return true; 190 return true;
174 } 191 }
175 192
176 scoped_refptr<CrxInstaller> crx_installer = 193 scoped_refptr<CrxInstaller> crx_installer =
177 download_crx_util::OpenChromeExtension(profile_, *item); 194 download_crx_util::OpenChromeExtension(profile_, *item);
178 195
179 // CRX_INSTALLER_DONE will fire when the install completes. Observe() 196 // CRX_INSTALLER_DONE will fire when the install completes. Observe()
180 // will call DelayedDownloadOpened() on this item. If this DownloadItem is 197 // will call DelayedDownloadOpened() on this item. If this DownloadItem is
181 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called. 198 // not around when CRX_INSTALLER_DONE fires, Complete() will not be called.
182 registrar_.Add(this, 199 registrar_.Add(this,
(...skipping 11 matching lines...) Expand all
194 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 211 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
195 #if defined(ENABLE_SAFE_BROWSING) 212 #if defined(ENABLE_SAFE_BROWSING)
196 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 213 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
197 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); 214 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded();
198 #else 215 #else
199 return false; 216 return false;
200 #endif 217 #endif
201 } 218 }
202 219
203 void ChromeDownloadManagerDelegate::OnResponseCompleted(DownloadItem* item) { 220 void ChromeDownloadManagerDelegate::OnResponseCompleted(DownloadItem* item) {
204 #if defined(ENABLE_SAFE_BROWSING) 221 // TODO(noelutz): remove this method from the delegate API.
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
207 // check in that case.
208 if (item->hash().empty())
209 return;
210
211 scoped_refptr<DownloadSBClient> sb_client =
212 new DownloadSBClient(item->id(),
213 item->url_chain(),
214 item->referrer_url(),
215 profile_->GetPrefs()->GetBoolean(
216 prefs::kSafeBrowsingEnabled));
217 sb_client->CheckDownloadHash(
218 item->hash(),
219 base::Bind(&ChromeDownloadManagerDelegate::CheckDownloadHashDone,
220 base::Unretained(this)));
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)));
229 } 229 }
230 230
231 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore( 231 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore(
232 DownloadItem* item) { 232 DownloadItem* item) {
233 download_history_->UpdateEntry(item); 233 download_history_->UpdateEntry(item);
234 } 234 }
235 235
236 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore( 236 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore(
237 DownloadItem* item, 237 DownloadItem* item,
238 const FilePath& new_path) { 238 const FilePath& new_path) {
239 download_history_->UpdateDownloadPath(item, new_path); 239 download_history_->UpdateDownloadPath(item, new_path);
240 } 240 }
241 241
242 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
243 int32 download_id,
244 safe_browsing::DownloadProtectionService::DownloadCheckResult result) {
245 // TODO(bryner): notify the user based on this result
246 }
247
248 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore( 242 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore(
249 DownloadItem* item) { 243 DownloadItem* item) {
250 download_history_->RemoveEntry(item); 244 download_history_->RemoveEntry(item);
251 } 245 }
252 246
253 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( 247 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween(
254 const base::Time remove_begin, 248 const base::Time remove_begin,
255 const base::Time remove_end) { 249 const base::Time remove_end) {
256 download_history_->RemoveEntriesBetween(remove_begin, remove_end); 250 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
257 } 251 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 int download_count = 0; 293 int download_count = 0;
300 bool progress_known = 294 bool progress_known =
301 g_browser_process->download_status_updater()->GetProgress( 295 g_browser_process->download_status_updater()->GetProgress(
302 &progress, &download_count); 296 &progress, &download_count);
303 download_util::UpdateAppIconDownloadProgress( 297 download_util::UpdateAppIconDownloadProgress(
304 download_count, progress_known, progress); 298 download_count, progress_known, progress);
305 } 299 }
306 300
307 void ChromeDownloadManagerDelegate::CheckDownloadUrlDone( 301 void ChromeDownloadManagerDelegate::CheckDownloadUrlDone(
308 int32 download_id, 302 int32 download_id,
309 bool is_dangerous_url) { 303 DownloadProtectionService::DownloadCheckResult result) {
310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
311
312 DownloadItem* download = 305 DownloadItem* download =
313 download_manager_->GetActiveDownloadItem(download_id); 306 download_manager_->GetActiveDownloadItem(download_id);
314 if (!download) 307 if (!download)
315 return; 308 return;
316 309
317 if (is_dangerous_url) 310 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false)
311 << " verdict = " << result;
312 if (result == DownloadProtectionService::DANGEROUS)
318 download->MarkUrlDangerous(); 313 download->MarkUrlDangerous();
319 314
320 download_history_->CheckVisitedReferrerBefore( 315 download_history_->CheckVisitedReferrerBefore(
321 download_id, download->referrer_url(), 316 download_id, download->referrer_url(),
322 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, 317 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone,
323 base::Unretained(this))); 318 base::Unretained(this)));
324 } 319 }
325 320
321 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
322 int32 download_id,
323 DownloadProtectionService::DownloadCheckResult result) {
324 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id);
325 if (!item)
Randy Smith (Not in Mondays) 2011/11/14 22:24:22 There's a (minor) leak here if the DownloadItem's
noelutz 2011/11/14 22:26:35 I think we're OK. There aren't any pointers in the
noelutz 2011/11/15 00:14:10 Fixed.
326 return;
327
328 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
329 << " verdict = " << result;
330 // TODO(noelutz):
331 // 1) display a warning if the result is DANGEROUS.
332 // 2) make sure we haven't already displayed a warning for the URL.
333 // 3) disable the existing dangerous file warning for executables.
334
335 SafeBrowsingStateMap::iterator it = safe_browsing_state_.find(item->id());
336 DCHECK(it != safe_browsing_state_.end() && it->second.pending);
337 if (it != safe_browsing_state_.end()) {
338 it->second.pending = false;
339 it->second.verdict = result;
340 }
341 download_manager_->MaybeCompleteDownload(item);
342 }
343
326 // content::NotificationObserver implementation. 344 // content::NotificationObserver implementation.
327 void ChromeDownloadManagerDelegate::Observe( 345 void ChromeDownloadManagerDelegate::Observe(
328 int type, 346 int type,
329 const content::NotificationSource& source, 347 const content::NotificationSource& source,
330 const content::NotificationDetails& details) { 348 const content::NotificationDetails& details) {
331 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); 349 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE);
332 350
333 registrar_.Remove(this, 351 registrar_.Remove(this,
334 chrome::NOTIFICATION_CRX_INSTALLER_DONE, 352 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
335 source); 353 source);
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 int32 download_id, int64 db_handle) { 565 int32 download_id, int64 db_handle) {
548 // It's not immediately obvious, but HistoryBackend::CreateDownload() can 566 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
549 // call this function with an invalid |db_handle|. For instance, this can 567 // call this function with an invalid |db_handle|. For instance, this can
550 // happen when the history database is offline. We cannot have multiple 568 // happen when the history database is offline. We cannot have multiple
551 // DownloadItems with the same invalid db_handle, so we need to assign a 569 // DownloadItems with the same invalid db_handle, so we need to assign a
552 // unique |db_handle| here. 570 // unique |db_handle| here.
553 if (db_handle == DownloadItem::kUninitializedHandle) 571 if (db_handle == DownloadItem::kUninitializedHandle)
554 db_handle = download_history_->GetNextFakeDbHandle(); 572 db_handle = download_history_->GetNextFakeDbHandle();
555 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); 573 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle);
556 } 574 }
557
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
560 // MaybeCompleteDownload in OnAllDataSaved to this function.
561 void ChromeDownloadManagerDelegate::CheckDownloadHashDone(
562 int32 download_id,
563 bool is_dangerous_hash) {
564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
565 DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id
566 << " is dangerous_hash: " << is_dangerous_hash;
567 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698