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 "content/browser/download/download_manager.h" | 5 #include "content/browser/download/download_manager.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/callback.h" | 10 #include "base/callback.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/stringprintf.h" |
| 16 #include "base/synchronization/lock.h" |
| 17 #include "base/sys_string_conversions.h" |
14 #include "base/task.h" | 18 #include "base/task.h" |
15 #include "build/build_config.h" | 19 #include "build/build_config.h" |
16 #include "content/browser/browser_context.h" | 20 #include "content/browser/browser_context.h" |
17 #include "content/browser/browser_thread.h" | 21 #include "content/browser/browser_thread.h" |
18 #include "content/browser/content_browser_client.h" | 22 #include "content/browser/content_browser_client.h" |
19 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
20 #include "content/browser/download/download_file_manager.h" | 24 #include "content/browser/download/download_file_manager.h" |
21 #include "content/browser/download/download_item.h" | 25 #include "content/browser/download/download_item.h" |
22 #include "content/browser/download/download_manager_delegate.h" | 26 #include "content/browser/download/download_manager_delegate.h" |
23 #include "content/browser/download/download_persistent_store_info.h" | 27 #include "content/browser/download/download_persistent_store_info.h" |
(...skipping 26 matching lines...) Expand all Loading... |
50 render_view_id, | 54 render_view_id, |
51 *context); | 55 *context); |
52 } | 56 } |
53 | 57 |
54 } // namespace | 58 } // namespace |
55 | 59 |
56 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 60 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
57 DownloadStatusUpdater* status_updater) | 61 DownloadStatusUpdater* status_updater) |
58 : shutdown_needed_(false), | 62 : shutdown_needed_(false), |
59 browser_context_(NULL), | 63 browser_context_(NULL), |
| 64 next_id_(0), |
60 file_manager_(NULL), | 65 file_manager_(NULL), |
61 status_updater_(status_updater->AsWeakPtr()), | 66 status_updater_((status_updater != NULL) |
| 67 ? status_updater->AsWeakPtr() |
| 68 : base::WeakPtr<DownloadStatusUpdater>()), |
62 delegate_(delegate), | 69 delegate_(delegate), |
63 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 70 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
64 if (status_updater_) | 71 // NOTE(benjhayden): status_updater may be NULL when using |
| 72 // TestingBrowserProcess. |
| 73 if (status_updater_.get() != NULL) |
65 status_updater_->AddDelegate(this); | 74 status_updater_->AddDelegate(this); |
66 } | 75 } |
67 | 76 |
68 DownloadManager::~DownloadManager() { | 77 DownloadManager::~DownloadManager() { |
69 DCHECK(!shutdown_needed_); | 78 DCHECK(!shutdown_needed_); |
70 if (status_updater_) | 79 if (status_updater_.get() != NULL) |
71 status_updater_->RemoveDelegate(this); | 80 status_updater_->RemoveDelegate(this); |
72 } | 81 } |
73 | 82 |
74 void DownloadManager::Shutdown() { | 83 void DownloadManager::Shutdown() { |
75 VLOG(20) << __FUNCTION__ << "()" | 84 VLOG(20) << __FUNCTION__ << "()" |
76 << " shutdown_needed_ = " << shutdown_needed_; | 85 << " shutdown_needed_ = " << shutdown_needed_; |
77 if (!shutdown_needed_) | 86 if (!shutdown_needed_) |
78 return; | 87 return; |
79 shutdown_needed_ = false; | 88 shutdown_needed_ = false; |
80 | 89 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 // The Incognito Downloads page will get the list of non-Incognito downloads | 187 // The Incognito Downloads page will get the list of non-Incognito downloads |
179 // from its parent profile. | 188 // from its parent profile. |
180 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 189 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
181 continue; | 190 continue; |
182 | 191 |
183 if (download_item->MatchesQuery(query_lower)) | 192 if (download_item->MatchesQuery(query_lower)) |
184 result->push_back(download_item); | 193 result->push_back(download_item); |
185 } | 194 } |
186 } | 195 } |
187 | 196 |
| 197 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { |
| 198 DVLOG(1) << __FUNCTION__ << " " << next_id; |
| 199 base::AutoLock lock(next_id_lock_); |
| 200 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ |
| 201 // = next_id. The '+=' works for now because these ids are not yet persisted |
| 202 // to the database. GetNextId() can allocate zero or more ids starting from 0, |
| 203 // then this callback can increment next_id_, and the items with lower ids |
| 204 // won't clash with any other items even though there may be items loaded from |
| 205 // the history because items from the history don't have valid ids. |
| 206 next_id_ += next_id; |
| 207 } |
| 208 |
| 209 DownloadId DownloadManager::GetNextId() { |
| 210 // May be called on any thread via the GetNextIdThunk. |
| 211 // TODO(benjhayden) If otr, forward to parent DM. |
| 212 base::AutoLock lock(next_id_lock_); |
| 213 return DownloadId(this, next_id_++); |
| 214 } |
| 215 |
| 216 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { |
| 217 // TODO(benjhayden) If otr, forward to parent DM. |
| 218 return base::Bind(&DownloadManager::GetNextId, this); |
| 219 } |
| 220 |
188 // Query the history service for information about all persisted downloads. | 221 // Query the history service for information about all persisted downloads. |
189 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 222 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
190 DCHECK(browser_context); | 223 DCHECK(browser_context); |
191 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 224 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
192 shutdown_needed_ = true; | 225 shutdown_needed_ = true; |
193 | 226 |
194 browser_context_ = browser_context; | 227 browser_context_ = browser_context; |
195 | 228 |
196 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 229 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
197 // safe to avoid setting |file_manager_| because we only call a small set of | 230 // safe to avoid setting |file_manager_| because we only call a small set of |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 362 |
330 // Rename to intermediate name. | 363 // Rename to intermediate name. |
331 FilePath download_path; | 364 FilePath download_path; |
332 if (!delegate_->OverrideIntermediatePath(download, &download_path)) | 365 if (!delegate_->OverrideIntermediatePath(download, &download_path)) |
333 download_path = download->full_path(); | 366 download_path = download->full_path(); |
334 | 367 |
335 BrowserThread::PostTask( | 368 BrowserThread::PostTask( |
336 BrowserThread::FILE, FROM_HERE, | 369 BrowserThread::FILE, FROM_HERE, |
337 NewRunnableMethod( | 370 NewRunnableMethod( |
338 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 371 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
339 download->id(), download_path)); | 372 download->global_id(), download_path)); |
340 | 373 |
341 download->Rename(download_path); | 374 download->Rename(download_path); |
342 | 375 |
343 delegate_->AddItemToPersistentStore(download); | 376 delegate_->AddItemToPersistentStore(download); |
344 } | 377 } |
345 | 378 |
346 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 379 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
348 DownloadMap::iterator it = active_downloads_.find(download_id); | 381 DownloadMap::iterator it = active_downloads_.find(download_id); |
349 if (it != active_downloads_.end()) { | 382 if (it != active_downloads_.end()) { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
487 | 520 |
488 DownloadItem* item = GetDownloadItem(download_id); | 521 DownloadItem* item = GetDownloadItem(download_id); |
489 if (!item) | 522 if (!item) |
490 return; | 523 return; |
491 | 524 |
492 if (item->safety_state() == DownloadItem::SAFE) { | 525 if (item->safety_state() == DownloadItem::SAFE) { |
493 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 526 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
494 } | 527 } |
495 | 528 |
496 BrowserThread::PostTask( | 529 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
497 BrowserThread::FILE, FROM_HERE, | 530 file_manager_, |
498 NewRunnableMethod( | 531 &DownloadFileManager::CompleteDownload, |
499 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); | 532 item->global_id())); |
500 | 533 |
501 if (uniquifier) | 534 if (uniquifier) |
502 item->set_path_uniquifier(uniquifier); | 535 item->set_path_uniquifier(uniquifier); |
503 | 536 |
504 item->OnDownloadRenamedToFinalName(full_path); | 537 item->OnDownloadRenamedToFinalName(full_path); |
505 delegate_->UpdatePathForItemInPersistentStore(item, full_path); | 538 delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
506 } | 539 } |
507 | 540 |
508 void DownloadManager::CancelDownload(int32 download_id) { | 541 void DownloadManager::CancelDownload(int32 download_id) { |
509 DownloadItem* download = GetActiveDownload(download_id); | 542 DownloadItem* download = GetActiveDownload(download_id); |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 DCHECK(ContainsKey(save_page_downloads_, download->id())); | 1057 DCHECK(ContainsKey(save_page_downloads_, download->id())); |
1025 save_page_downloads_.erase(download->id()); | 1058 save_page_downloads_.erase(download->id()); |
1026 | 1059 |
1027 if (download->IsComplete()) | 1060 if (download->IsComplete()) |
1028 NotificationService::current()->Notify( | 1061 NotificationService::current()->Notify( |
1029 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 1062 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
1030 Source<DownloadManager>(this), | 1063 Source<DownloadManager>(this), |
1031 Details<DownloadItem>(download)); | 1064 Details<DownloadItem>(download)); |
1032 } | 1065 } |
1033 } | 1066 } |
OLD | NEW |