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" | |
10 #include "base/callback.h" | 9 #include "base/callback.h" |
11 #include "base/file_util.h" | 10 #include "base/file_util.h" |
12 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
14 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
15 #include "base/stringprintf.h" | |
16 #include "base/synchronization/lock.h" | |
17 #include "base/sys_string_conversions.h" | |
18 #include "base/task.h" | 14 #include "base/task.h" |
19 #include "build/build_config.h" | 15 #include "build/build_config.h" |
20 #include "content/browser/browser_context.h" | 16 #include "content/browser/browser_context.h" |
21 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
22 #include "content/browser/content_browser_client.h" | 18 #include "content/browser/content_browser_client.h" |
23 #include "content/browser/download/download_create_info.h" | 19 #include "content/browser/download/download_create_info.h" |
24 #include "content/browser/download/download_file_manager.h" | 20 #include "content/browser/download/download_file_manager.h" |
25 #include "content/browser/download/download_item.h" | 21 #include "content/browser/download/download_item.h" |
26 #include "content/browser/download/download_manager_delegate.h" | 22 #include "content/browser/download/download_manager_delegate.h" |
27 #include "content/browser/download/download_persistent_store_info.h" | 23 #include "content/browser/download/download_persistent_store_info.h" |
(...skipping 19 matching lines...) Expand all Loading... |
47 url, referrer, save_info, true, render_process_id, render_view_id, | 43 url, referrer, save_info, true, render_process_id, render_view_id, |
48 *context); | 44 *context); |
49 } | 45 } |
50 | 46 |
51 } // namespace | 47 } // namespace |
52 | 48 |
53 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 49 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
54 DownloadStatusUpdater* status_updater) | 50 DownloadStatusUpdater* status_updater) |
55 : shutdown_needed_(false), | 51 : shutdown_needed_(false), |
56 browser_context_(NULL), | 52 browser_context_(NULL), |
57 next_id_(0), | |
58 file_manager_(NULL), | 53 file_manager_(NULL), |
59 status_updater_(status_updater->AsWeakPtr()), | 54 status_updater_(status_updater->AsWeakPtr()), |
60 delegate_(delegate), | 55 delegate_(delegate), |
61 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 56 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
62 if (status_updater_) | 57 if (status_updater_) |
63 status_updater_->AddDelegate(this); | 58 status_updater_->AddDelegate(this); |
64 } | 59 } |
65 | 60 |
66 DownloadManager::~DownloadManager() { | 61 DownloadManager::~DownloadManager() { |
67 DCHECK(!shutdown_needed_); | 62 DCHECK(!shutdown_needed_); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // The Incognito Downloads page will get the list of non-Incognito downloads | 170 // The Incognito Downloads page will get the list of non-Incognito downloads |
176 // from its parent profile. | 171 // from its parent profile. |
177 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 172 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
178 continue; | 173 continue; |
179 | 174 |
180 if (download_item->MatchesQuery(query_lower)) | 175 if (download_item->MatchesQuery(query_lower)) |
181 result->push_back(download_item); | 176 result->push_back(download_item); |
182 } | 177 } |
183 } | 178 } |
184 | 179 |
185 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { | |
186 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
187 base::AutoLock lock(next_id_lock_); | |
188 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
189 // = next_id. The '+=' works for now because these ids are not yet persisted | |
190 // to the database. GetNextId() can allocate zero or more ids starting from 0, | |
191 // then this callback can increment next_id_, and the items with lower ids | |
192 // won't clash with any other items even though there may be items loaded from | |
193 // the history because items from the history don't have valid ids. | |
194 next_id_ += next_id; | |
195 } | |
196 | |
197 DownloadId DownloadManager::GetNextId() { | |
198 // May be called on any thread via the GetNextIdThunk. | |
199 // TODO(benjhayden) If otr, forward to parent DM. | |
200 base::AutoLock lock(next_id_lock_); | |
201 return DownloadId(this, next_id_++); | |
202 } | |
203 | |
204 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
205 // TODO(benjhayden) If otr, forward to parent DM. | |
206 return base::Bind(&DownloadManager::GetNextId, this); | |
207 } | |
208 | |
209 // Query the history service for information about all persisted downloads. | 180 // Query the history service for information about all persisted downloads. |
210 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 181 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
211 DCHECK(browser_context); | 182 DCHECK(browser_context); |
212 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 183 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
213 shutdown_needed_ = true; | 184 shutdown_needed_ = true; |
214 | 185 |
215 browser_context_ = browser_context; | 186 browser_context_ = browser_context; |
216 | 187 |
217 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 188 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
218 // safe to avoid setting |file_manager_| because we only call a small set of | 189 // 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... |
350 | 321 |
351 // Rename to intermediate name. | 322 // Rename to intermediate name. |
352 FilePath download_path; | 323 FilePath download_path; |
353 if (!delegate_->OverrideIntermediatePath(download, &download_path)) | 324 if (!delegate_->OverrideIntermediatePath(download, &download_path)) |
354 download_path = download->full_path(); | 325 download_path = download->full_path(); |
355 | 326 |
356 BrowserThread::PostTask( | 327 BrowserThread::PostTask( |
357 BrowserThread::FILE, FROM_HERE, | 328 BrowserThread::FILE, FROM_HERE, |
358 NewRunnableMethod( | 329 NewRunnableMethod( |
359 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 330 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
360 download->global_id(), download_path)); | 331 download->id(), download_path)); |
361 | 332 |
362 download->Rename(download_path); | 333 download->Rename(download_path); |
363 | 334 |
364 delegate_->AddItemToPersistentStore(download); | 335 delegate_->AddItemToPersistentStore(download); |
365 } | 336 } |
366 | 337 |
367 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 338 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
369 DownloadMap::iterator it = active_downloads_.find(download_id); | 340 DownloadMap::iterator it = active_downloads_.find(download_id); |
370 if (it != active_downloads_.end()) { | 341 if (it != active_downloads_.end()) { |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
522 | 493 |
523 DownloadItem* item = GetDownloadItem(download_id); | 494 DownloadItem* item = GetDownloadItem(download_id); |
524 if (!item) | 495 if (!item) |
525 return; | 496 return; |
526 | 497 |
527 if (item->safety_state() == DownloadItem::SAFE) { | 498 if (item->safety_state() == DownloadItem::SAFE) { |
528 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 499 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
529 } | 500 } |
530 | 501 |
531 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( | 502 BrowserThread::PostTask( |
532 file_manager_, | 503 BrowserThread::FILE, FROM_HERE, |
533 &DownloadFileManager::CompleteDownload, | 504 NewRunnableMethod( |
534 item->global_id())); | 505 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); |
535 | 506 |
536 if (uniquifier) | 507 if (uniquifier) |
537 item->set_path_uniquifier(uniquifier); | 508 item->set_path_uniquifier(uniquifier); |
538 | 509 |
539 item->OnDownloadRenamedToFinalName(full_path); | 510 item->OnDownloadRenamedToFinalName(full_path); |
540 delegate_->UpdatePathForItemInPersistentStore(item, full_path); | 511 delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
541 } | 512 } |
542 | 513 |
543 void DownloadManager::CancelDownload(int32 download_id) { | 514 void DownloadManager::CancelDownload(int32 download_id) { |
544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 // | 566 // |
596 // Clean up will happen when the history system create callback runs if we | 567 // Clean up will happen when the history system create callback runs if we |
597 // don't have a valid db_handle yet. | 568 // don't have a valid db_handle yet. |
598 if (download->db_handle() != DownloadItem::kUninitializedHandle) { | 569 if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
599 in_progress_.erase(download_id); | 570 in_progress_.erase(download_id); |
600 active_downloads_.erase(download_id); | 571 active_downloads_.erase(download_id); |
601 UpdateDownloadProgress(); // Reflect removal from in_progress_. | 572 UpdateDownloadProgress(); // Reflect removal from in_progress_. |
602 delegate_->UpdateItemInPersistentStore(download); | 573 delegate_->UpdateItemInPersistentStore(download); |
603 } | 574 } |
604 | 575 |
605 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( | 576 BrowserThread::PostTask( |
606 file_manager_, | 577 BrowserThread::FILE, FROM_HERE, |
607 &DownloadFileManager::CancelDownload, | 578 NewRunnableMethod( |
608 download->global_id())); | 579 file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
609 } | 580 } |
610 | 581 |
611 void DownloadManager::UpdateDownloadProgress() { | 582 void DownloadManager::UpdateDownloadProgress() { |
612 delegate_->DownloadProgressUpdated(); | 583 delegate_->DownloadProgressUpdated(); |
613 } | 584 } |
614 | 585 |
615 int DownloadManager::RemoveDownloadItems( | 586 int DownloadManager::RemoveDownloadItems( |
616 const DownloadVector& pending_deletes) { | 587 const DownloadVector& pending_deletes) { |
617 if (pending_deletes.empty()) | 588 if (pending_deletes.empty()) |
618 return 0; | 589 return 0; |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 DCHECK(ContainsKey(save_page_downloads_, download->id())); | 1017 DCHECK(ContainsKey(save_page_downloads_, download->id())); |
1047 save_page_downloads_.erase(download->id()); | 1018 save_page_downloads_.erase(download->id()); |
1048 | 1019 |
1049 if (download->IsComplete()) | 1020 if (download->IsComplete()) |
1050 NotificationService::current()->Notify( | 1021 NotificationService::current()->Notify( |
1051 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 1022 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
1052 Source<DownloadManager>(this), | 1023 Source<DownloadManager>(this), |
1053 Details<DownloadItem>(download)); | 1024 Details<DownloadItem>(download)); |
1054 } | 1025 } |
1055 } | 1026 } |
OLD | NEW |