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

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

Issue 10665049: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 months 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/rand_util.h" 13 #include "base/rand_util.h"
14 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
15 #include "base/time.h" 15 #include "base/time.h"
16 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/download/download_completion_blocker.h" 18 #include "chrome/browser/download/download_completion_blocker.h"
19 #include "chrome/browser/download/download_crx_util.h" 19 #include "chrome/browser/download/download_crx_util.h"
20 #include "chrome/browser/download/download_extensions.h" 20 #include "chrome/browser/download/download_extensions.h"
21 #include "chrome/browser/download/download_file_picker.h" 21 #include "chrome/browser/download/download_file_picker.h"
22 #include "chrome/browser/download/download_history.h" 22 #include "chrome/browser/download/download_history.h"
23 #include "chrome/browser/download/download_path_reservation_tracker.h"
23 #include "chrome/browser/download/download_prefs.h" 24 #include "chrome/browser/download/download_prefs.h"
24 #include "chrome/browser/download/download_path_reservation_tracker.h"
25 #include "chrome/browser/download/download_status_updater.h" 25 #include "chrome/browser/download/download_status_updater.h"
26 #include "chrome/browser/download/download_util.h" 26 #include "chrome/browser/download/download_util.h"
27 #include "chrome/browser/download/save_package_file_picker.h" 27 #include "chrome/browser/download/save_package_file_picker.h"
28 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 28 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
29 #include "chrome/browser/extensions/crx_installer.h" 29 #include "chrome/browser/extensions/crx_installer.h"
30 #include "chrome/browser/extensions/extension_service.h" 30 #include "chrome/browser/extensions/extension_service.h"
31 #include "chrome/browser/history/history_service_factory.h"
31 #include "chrome/browser/prefs/pref_member.h" 32 #include "chrome/browser/prefs/pref_member.h"
32 #include "chrome/browser/prefs/pref_service.h" 33 #include "chrome/browser/prefs/pref_service.h"
33 #include "chrome/browser/profiles/profile.h" 34 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
35 #include "chrome/browser/ui/browser_tabstrip.h" 36 #include "chrome/browser/ui/browser_tabstrip.h"
36 #include "chrome/common/chrome_notification_types.h" 37 #include "chrome/common/chrome_notification_types.h"
37 #include "chrome/common/extensions/extension_switch_utils.h" 38 #include "chrome/common/extensions/extension_switch_utils.h"
38 #include "chrome/common/extensions/user_script.h" 39 #include "chrome/common/extensions/user_script.h"
39 #include "chrome/common/pref_names.h" 40 #include "chrome/common/pref_names.h"
40 #include "content/public/browser/download_item.h" 41 #include "content/public/browser/download_item.h"
41 #include "content/public/browser/download_manager.h" 42 #include "content/public/browser/download_manager.h"
43 #include "content/public/browser/download_persistent_store_info.h"
42 #include "content/public/browser/notification_source.h" 44 #include "content/public/browser/notification_source.h"
43 #include "content/public/browser/web_contents.h" 45 #include "content/public/browser/web_contents.h"
44 #include "content/public/browser/web_contents_delegate.h" 46 #include "content/public/browser/web_contents_delegate.h"
45 #include "content/public/browser/web_intents_dispatcher.h" 47 #include "content/public/browser/web_intents_dispatcher.h"
46 #include "grit/generated_resources.h" 48 #include "grit/generated_resources.h"
47 #include "net/base/net_util.h" 49 #include "net/base/net_util.h"
48 #include "ui/base/l10n/l10n_util.h" 50 #include "ui/base/l10n/l10n_util.h"
49 #include "webkit/glue/web_intent_data.h" 51 #include "webkit/glue/web_intent_data.h"
50 52
51 #if !defined(OS_ANDROID) 53 #if !defined(OS_ANDROID)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); 113 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME));
112 114
113 *generated_name = net::GenerateFileName(download_item.GetURL(), 115 *generated_name = net::GenerateFileName(download_item.GetURL(),
114 download_item.GetContentDisposition(), 116 download_item.GetContentDisposition(),
115 download_item.GetReferrerCharset(), 117 download_item.GetReferrerCharset(),
116 download_item.GetSuggestedFilename(), 118 download_item.GetSuggestedFilename(),
117 download_item.GetMimeType(), 119 download_item.GetMimeType(),
118 default_file_name); 120 default_file_name);
119 } 121 }
120 122
123 class HistoryServiceDownloadAdapter : public HistoryServiceDownloadInterface {
124 public:
125 explicit HistoryServiceDownloadAdapter(HistoryService* history_service)
126 : history_service_(history_service) {
127 }
128 virtual ~HistoryServiceDownloadAdapter() {}
129 virtual void QueryDownloads(
130 const HistoryService::DownloadQueryCallback& callback) OVERRIDE {
131 history_service_->QueryDownloads(&history_consumer_, callback);
132 history_service_->CleanUpInProgressEntries();
133 }
134 virtual HistoryService::Handle GetVisibleVisitCountToHost(
135 const GURL& referrer_url,
136 const HistoryService::GetVisibleVisitCountToHostCallback&
137 callback) OVERRIDE {
138 return history_service_->GetVisibleVisitCountToHost(
139 referrer_url, &history_consumer_, callback);
140 }
141 virtual void CreateDownload(
142 int32 id,
143 const content::DownloadPersistentStoreInfo& info,
144 const HistoryService::DownloadCreateCallback& callback) OVERRIDE {
145 history_service_->CreateDownload(id, info, &history_consumer_, callback);
146 }
147 virtual void UpdateDownload(
148 const content::DownloadPersistentStoreInfo& info) OVERRIDE {
149 history_service_->UpdateDownload(info);
150 }
151 virtual void RemoveDownloads(const std::set<int64>& handles) OVERRIDE {
152 history_service_->RemoveDownloads(handles);
153 }
154 virtual void OnDownloadHistoryDestroyed() OVERRIDE {
155 delete this;
156 }
157
158 private:
159 CancelableRequestConsumer history_consumer_;
160 HistoryService* history_service_;
161
162 DISALLOW_COPY_AND_ASSIGN(HistoryServiceDownloadAdapter);
163 };
164
121 } // namespace 165 } // namespace
122 166
123 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) 167 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile)
124 : profile_(profile), 168 : profile_(profile),
125 next_download_id_(0), 169 next_download_id_(0),
126 download_prefs_(new DownloadPrefs(profile)) { 170 download_prefs_(new DownloadPrefs(profile)) {
127 } 171 }
128 172
129 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 173 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
130 } 174 }
131 175
132 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { 176 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) {
133 download_manager_ = dm; 177 download_manager_ = dm;
134 download_history_.reset(new DownloadHistory(profile_)); 178
135 download_history_->Load(
136 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete,
137 base::Unretained(dm)));
138 #if !defined(OS_ANDROID) 179 #if !defined(OS_ANDROID)
139 extension_event_router_.reset(new ExtensionDownloadsEventRouter( 180 extension_event_router_.reset(new ExtensionDownloadsEventRouter(
140 profile_, download_manager_)); 181 profile_, download_manager_));
141 #endif 182 #endif
183
184 if (profile_ == profile_->GetOriginalProfile()) {
185 HistoryService* hs = HistoryServiceFactory::GetForProfile(
186 profile_, Profile::EXPLICIT_ACCESS);
187 if (hs) {
188 download_history_.reset(new DownloadHistory(
189 download_manager_, new HistoryServiceDownloadAdapter(hs)));
190 download_history_->Load(base::Bind(
191 &DownloadManager::OnPersistentStoreQueryComplete,
192 download_manager_));
193 }
194 }
142 } 195 }
143 196
144 void ChromeDownloadManagerDelegate::Shutdown() { 197 void ChromeDownloadManagerDelegate::Shutdown() {
145 download_history_.reset();
146 download_prefs_.reset(); 198 download_prefs_.reset();
147 #if !defined(OS_ANDROID) 199 #if !defined(OS_ANDROID)
148 extension_event_router_.reset(); 200 extension_event_router_.reset();
149 #endif 201 #endif
150 } 202 }
151 203
152 DownloadId ChromeDownloadManagerDelegate::GetNextId() { 204 DownloadId ChromeDownloadManagerDelegate::GetNextId() {
153 if (!profile_->IsOffTheRecord()) 205 if (!profile_->IsOffTheRecord())
154 return DownloadId(this, next_download_id_++); 206 return DownloadId(this, next_download_id_++);
155 207
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 return false; 297 return false;
246 DCHECK(extension[0] == FilePath::kExtensionSeparator); 298 DCHECK(extension[0] == FilePath::kExtensionSeparator);
247 extension.erase(0, 1); 299 extension.erase(0, 1);
248 return download_prefs_->IsAutoOpenEnabledForExtension(extension); 300 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
249 } 301 }
250 302
251 // static 303 // static
252 void ChromeDownloadManagerDelegate::DisableSafeBrowsing(DownloadItem* item) { 304 void ChromeDownloadManagerDelegate::DisableSafeBrowsing(DownloadItem* item) {
253 #if defined(ENABLE_SAFE_BROWSING) 305 #if defined(ENABLE_SAFE_BROWSING)
254 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( 306 SafeBrowsingState* state = static_cast<SafeBrowsingState*>(
255 item->GetExternalData(&safe_browsing_id)); 307 item->GetUserData(&safe_browsing_id));
256 DCHECK(!state); 308 DCHECK(!state);
257 if (!state) 309 if (!state)
258 state = new SafeBrowsingState(); 310 state = new SafeBrowsingState();
259 state->SetVerdict(DownloadProtectionService::SAFE); 311 state->SetVerdict(DownloadProtectionService::SAFE);
260 item->SetExternalData(&safe_browsing_id, state); 312 item->SetUserData(&safe_browsing_id, state);
261 #endif 313 #endif
262 } 314 }
263 315
264 bool ChromeDownloadManagerDelegate::IsDownloadReadyForCompletion( 316 bool ChromeDownloadManagerDelegate::IsDownloadReadyForCompletion(
265 DownloadItem* item, 317 DownloadItem* item,
266 const base::Closure& internal_complete_callback) { 318 const base::Closure& internal_complete_callback) {
267 #if defined(ENABLE_SAFE_BROWSING) 319 #if defined(ENABLE_SAFE_BROWSING)
268 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( 320 SafeBrowsingState* state = static_cast<SafeBrowsingState*>(
269 item->GetExternalData(&safe_browsing_id)); 321 item->GetUserData(&safe_browsing_id));
270 if (!state) { 322 if (!state) {
271 // Begin the safe browsing download protection check. 323 // Begin the safe browsing download protection check.
272 DownloadProtectionService* service = GetDownloadProtectionService(); 324 DownloadProtectionService* service = GetDownloadProtectionService();
273 if (service) { 325 if (service) {
274 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = " 326 VLOG(2) << __FUNCTION__ << "() Start SB download check for download = "
275 << item->DebugString(false); 327 << item->DebugString(false);
276 state = new SafeBrowsingState(); 328 state = new SafeBrowsingState();
277 state->set_callback(internal_complete_callback); 329 state->set_callback(internal_complete_callback);
278 item->SetExternalData(&safe_browsing_id, state); 330 item->SetUserData(&safe_browsing_id, state);
279 service->CheckClientDownload( 331 service->CheckClientDownload(
280 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item), 332 DownloadProtectionService::DownloadInfo::FromDownloadItem(*item),
281 base::Bind( 333 base::Bind(
282 &ChromeDownloadManagerDelegate::CheckClientDownloadDone, 334 &ChromeDownloadManagerDelegate::CheckClientDownloadDone,
283 this, 335 this,
284 item->GetId())); 336 item->GetId()));
285 return false; 337 return false;
286 } 338 }
287 } else if (!state->is_complete()) { 339 } else if (!state->is_complete()) {
288 // Don't complete the download until we have an answer. 340 // Don't complete the download until we have an answer.
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 467
416 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 468 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
417 #if defined(ENABLE_SAFE_BROWSING) 469 #if defined(ENABLE_SAFE_BROWSING)
418 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 470 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
419 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); 471 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded();
420 #else 472 #else
421 return false; 473 return false;
422 #endif 474 #endif
423 } 475 }
424 476
425 void ChromeDownloadManagerDelegate::AddItemToPersistentStore(
426 DownloadItem* item) {
427 download_history_->AddEntry(item,
428 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore,
429 base::Unretained(this)));
430 }
431
432 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore(
433 DownloadItem* item) {
434 download_history_->UpdateEntry(item);
435 }
436
437 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore(
438 DownloadItem* item,
439 const FilePath& new_path) {
440 download_history_->UpdateDownloadPath(item, new_path);
441 }
442
443 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore(
444 DownloadItem* item) {
445 download_history_->RemoveEntry(item);
446 }
447
448 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween(
449 base::Time remove_begin,
450 base::Time remove_end) {
451 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
452 }
453
454 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents, 477 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents,
455 FilePath* website_save_dir, 478 FilePath* website_save_dir,
456 FilePath* download_save_dir, 479 FilePath* download_save_dir,
457 bool* skip_dir_check) { 480 bool* skip_dir_check) {
458 Profile* profile = 481 Profile* profile =
459 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 482 Profile::FromBrowserContext(web_contents->GetBrowserContext());
460 PrefService* prefs = profile->GetPrefs(); 483 PrefService* prefs = profile->GetPrefs();
461 484
462 // Check whether the preference has the preferred directory for saving file. 485 // Check whether the preference has the preferred directory for saving file.
463 // If not, initialize it with default directory. 486 // If not, initialize it with default directory.
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT); 625 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT);
603 break; 626 break;
604 case DownloadProtectionService::UNCOMMON: 627 case DownloadProtectionService::UNCOMMON:
605 item->OnContentCheckCompleted( 628 item->OnContentCheckCompleted(
606 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); 629 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT);
607 break; 630 break;
608 } 631 }
609 } 632 }
610 633
611 SafeBrowsingState* state = static_cast<SafeBrowsingState*>( 634 SafeBrowsingState* state = static_cast<SafeBrowsingState*>(
612 item->GetExternalData(&safe_browsing_id)); 635 item->GetUserData(&safe_browsing_id));
613 state->SetVerdict(result); 636 state->SetVerdict(result);
614 } 637 }
615 638
616 // content::NotificationObserver implementation. 639 // content::NotificationObserver implementation.
617 void ChromeDownloadManagerDelegate::Observe( 640 void ChromeDownloadManagerDelegate::Observe(
618 int type, 641 int type,
619 const content::NotificationSource& source, 642 const content::NotificationSource& source,
620 const content::NotificationDetails& details) { 643 const content::NotificationDetails& details) {
621 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE); 644 DCHECK(type == chrome::NOTIFICATION_CRX_INSTALLER_DONE);
622 645
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a 831 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a
809 // directory to persist. Or perhaps, if the GData path 832 // directory to persist. Or perhaps, if the GData path
810 // substitution logic is moved here, then we would have a 833 // substitution logic is moved here, then we would have a
811 // persistable path after the DownloadFilePicker is done. 834 // persistable path after the DownloadFilePicker is done.
812 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && 835 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT &&
813 !download->IsTemporary()) 836 !download->IsTemporary())
814 last_download_path_ = target_path.DirName(); 837 last_download_path_ = target_path.DirName();
815 } 838 }
816 callback.Run(target_path, disposition, danger_type, intermediate_path); 839 callback.Run(target_path, disposition, danger_type, intermediate_path);
817 } 840 }
818
819 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore(
820 int32 download_id, int64 db_handle) {
821 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
822 // call this function with an invalid |db_handle|. For instance, this can
823 // happen when the history database is offline. We cannot have multiple
824 // DownloadItems with the same invalid db_handle, so we need to assign a
825 // unique |db_handle| here.
826 if (db_handle == DownloadItem::kUninitializedHandle)
827 db_handle = download_history_->GetNextFakeDbHandle();
828 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle);
829 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698