| OLD | NEW |
| 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 "content/browser/download/download_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 129 |
| 130 void EnsureNoPendingDownloadJobsOnIO(bool* result) { | 130 void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 131 scoped_refptr<DownloadFileManager> download_file_manager = | 131 scoped_refptr<DownloadFileManager> download_file_manager = |
| 132 ResourceDispatcherHostImpl::Get()->download_file_manager(); | 132 ResourceDispatcherHostImpl::Get()->download_file_manager(); |
| 133 BrowserThread::PostTask( | 133 BrowserThread::PostTask( |
| 134 BrowserThread::FILE, FROM_HERE, | 134 BrowserThread::FILE, FROM_HERE, |
| 135 base::Bind(&EnsureNoPendingDownloadsOnFile, | 135 base::Bind(&EnsureNoPendingDownloadsOnFile, |
| 136 download_file_manager, result)); | 136 download_file_manager, result)); |
| 137 } | 137 } |
| 138 | 138 |
| 139 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
| 140 public: |
| 141 DownloadItemFactoryImpl() {} |
| 142 virtual ~DownloadItemFactoryImpl() {} |
| 143 |
| 144 virtual content::DownloadItem* CreatePersistedItem( |
| 145 DownloadItemImpl::Delegate* delegate, |
| 146 content::DownloadId download_id, |
| 147 const content::DownloadPersistentStoreInfo& info, |
| 148 const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 149 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
| 150 } |
| 151 |
| 152 virtual content::DownloadItem* CreateActiveItem( |
| 153 DownloadItemImpl::Delegate* delegate, |
| 154 const DownloadCreateInfo& info, |
| 155 DownloadRequestHandleInterface* request_handle, |
| 156 bool is_otr, |
| 157 const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 158 return new DownloadItemImpl(delegate, info, request_handle, is_otr, |
| 159 bound_net_log); |
| 160 } |
| 161 |
| 162 virtual content::DownloadItem* CreateSavePageItem( |
| 163 DownloadItemImpl::Delegate* delegate, |
| 164 const FilePath& path, |
| 165 const GURL& url, |
| 166 bool is_otr, |
| 167 content::DownloadId download_id, |
| 168 const std::string& mime_type, |
| 169 const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 170 return new DownloadItemImpl(delegate, path, url, is_otr, download_id, |
| 171 mime_type, bound_net_log); |
| 172 } |
| 173 }; |
| 174 |
| 139 } // namespace | 175 } // namespace |
| 140 | 176 |
| 141 namespace content { | 177 namespace content { |
| 142 | 178 |
| 143 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 179 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 145 bool result = true; | 181 bool result = true; |
| 146 BrowserThread::PostTask( | 182 BrowserThread::PostTask( |
| 147 BrowserThread::IO, FROM_HERE, | 183 BrowserThread::IO, FROM_HERE, |
| 148 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 184 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
| 149 MessageLoop::current()->Run(); | 185 MessageLoop::current()->Run(); |
| 150 return result; | 186 return result; |
| 151 } | 187 } |
| 152 | 188 |
| 153 } // namespace content | 189 } // namespace content |
| 154 | 190 |
| 155 DownloadManagerImpl::DownloadManagerImpl(net::NetLog* net_log) | 191 DownloadManagerImpl::DownloadManagerImpl( |
| 156 : shutdown_needed_(false), | 192 DownloadFileManager* file_manager, |
| 193 scoped_ptr<content::DownloadItemFactory> factory, |
| 194 net::NetLog* net_log) |
| 195 : factory_(factory.Pass()), |
| 196 shutdown_needed_(false), |
| 157 browser_context_(NULL), | 197 browser_context_(NULL), |
| 158 file_manager_(NULL), | 198 file_manager_(file_manager), |
| 159 delegate_(NULL), | |
| 160 net_log_(net_log) { | 199 net_log_(net_log) { |
| 200 DCHECK(file_manager); |
| 201 DCHECK(factory_.get()); |
| 202 // A NULL net_log is allowed. |
| 203 } |
| 204 |
| 205 // static |
| 206 DownloadManagerImpl* DownloadManagerImpl::Create( |
| 207 net::NetLog* net_log) { |
| 208 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 209 DCHECK(rdh); |
| 210 DownloadFileManager* file_manager = rdh->download_file_manager(); |
| 211 DCHECK(file_manager); |
| 212 |
| 213 return new DownloadManagerImpl( |
| 214 file_manager, scoped_ptr<content::DownloadItemFactory>( |
| 215 new DownloadItemFactoryImpl()).Pass(), net_log); |
| 161 } | 216 } |
| 162 | 217 |
| 163 DownloadManagerImpl::~DownloadManagerImpl() { | 218 DownloadManagerImpl::~DownloadManagerImpl() { |
| 164 DCHECK(!shutdown_needed_); | 219 DCHECK(!shutdown_needed_); |
| 165 } | 220 } |
| 166 | 221 |
| 167 DownloadId DownloadManagerImpl::GetNextId() { | 222 DownloadId DownloadManagerImpl::GetNextId() { |
| 168 DownloadId id; | 223 DownloadId id; |
| 169 if (delegate_) | 224 if (delegate_) |
| 170 id = delegate_->GetNextId(); | 225 id = delegate_->GetNextId(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 void DownloadManagerImpl::Shutdown() { | 257 void DownloadManagerImpl::Shutdown() { |
| 203 VLOG(20) << __FUNCTION__ << "()" | 258 VLOG(20) << __FUNCTION__ << "()" |
| 204 << " shutdown_needed_ = " << shutdown_needed_; | 259 << " shutdown_needed_ = " << shutdown_needed_; |
| 205 if (!shutdown_needed_) | 260 if (!shutdown_needed_) |
| 206 return; | 261 return; |
| 207 shutdown_needed_ = false; | 262 shutdown_needed_ = false; |
| 208 | 263 |
| 209 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 264 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
| 210 // TODO(benjhayden): Consider clearing observers_. | 265 // TODO(benjhayden): Consider clearing observers_. |
| 211 | 266 |
| 212 if (file_manager_) { | 267 DCHECK(file_manager_); |
| 213 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 268 BrowserThread::PostTask( |
| 214 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | 269 BrowserThread::FILE, FROM_HERE, |
| 215 file_manager_, make_scoped_refptr(this))); | 270 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, |
| 216 } | 271 file_manager_, make_scoped_refptr(this))); |
| 217 | 272 |
| 218 AssertContainersConsistent(); | 273 AssertContainersConsistent(); |
| 219 | 274 |
| 220 // Go through all downloads in downloads_. Dangerous ones we need to | 275 // Go through all downloads in downloads_. Dangerous ones we need to |
| 221 // remove on disk, and in progress ones we need to cancel. | 276 // remove on disk, and in progress ones we need to cancel. |
| 222 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { | 277 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { |
| 223 DownloadItem* download = *it; | 278 DownloadItem* download = *it; |
| 224 | 279 |
| 225 // Save iterator from potential erases in this set done by called code. | 280 // Save iterator from potential erases in this set done by called code. |
| 226 // Iterators after an erasure point are still valid for lists and | 281 // Iterators after an erasure point are still valid for lists and |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 // The Incognito Downloads page will get the list of non-Incognito downloads | 364 // The Incognito Downloads page will get the list of non-Incognito downloads |
| 310 // from its parent profile. | 365 // from its parent profile. |
| 311 if (browser_context_->IsOffTheRecord() != download_item->IsOtr()) | 366 if (browser_context_->IsOffTheRecord() != download_item->IsOtr()) |
| 312 continue; | 367 continue; |
| 313 | 368 |
| 314 if (download_item->MatchesQuery(query_lower)) | 369 if (download_item->MatchesQuery(query_lower)) |
| 315 result->push_back(download_item); | 370 result->push_back(download_item); |
| 316 } | 371 } |
| 317 } | 372 } |
| 318 | 373 |
| 319 // Query the history service for information about all persisted downloads. | |
| 320 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 374 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
| 321 DCHECK(browser_context); | 375 DCHECK(browser_context); |
| 322 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 376 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 323 shutdown_needed_ = true; | 377 shutdown_needed_ = true; |
| 324 | 378 |
| 325 browser_context_ = browser_context; | 379 browser_context_ = browser_context; |
| 326 | 380 |
| 327 // In test mode, there may be no ResourceDispatcherHostImpl. In this case | |
| 328 // it's safe to avoid setting |file_manager_| because we only call a small | |
| 329 // set of functions, none of which need it. | |
| 330 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); | |
| 331 if (rdh) { | |
| 332 file_manager_ = rdh->download_file_manager(); | |
| 333 DCHECK(file_manager_); | |
| 334 } | |
| 335 | |
| 336 return true; | 381 return true; |
| 337 } | 382 } |
| 338 | 383 |
| 339 // We have received a message from DownloadFileManager about a new download. | 384 // We have received a message from DownloadFileManager about a new download. |
| 340 void DownloadManagerImpl::StartDownload(int32 download_id) { | 385 void DownloadManagerImpl::StartDownload(int32 download_id) { |
| 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 342 | 387 |
| 343 if (!delegate_ || delegate_->ShouldStartDownload(download_id)) | 388 if (!delegate_ || delegate_->ShouldStartDownload(download_id)) |
| 344 RestartDownload(download_id); | 389 RestartDownload(download_id); |
| 345 } | 390 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 FilePath DownloadManagerImpl::LastDownloadPath() { | 466 FilePath DownloadManagerImpl::LastDownloadPath() { |
| 422 return last_download_path_; | 467 return last_download_path_; |
| 423 } | 468 } |
| 424 | 469 |
| 425 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 470 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( |
| 426 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { | 471 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { |
| 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 428 | 473 |
| 429 net::BoundNetLog bound_net_log = | 474 net::BoundNetLog bound_net_log = |
| 430 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 475 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 431 info->download_id = GetNextId(); | 476 if (!info->download_id.IsValid()) |
| 432 DownloadItem* download = new DownloadItemImpl( | 477 info->download_id = GetNextId(); |
| 478 DownloadItem* download = factory_->CreateActiveItem( |
| 433 this, *info, new DownloadRequestHandle(request_handle), | 479 this, *info, new DownloadRequestHandle(request_handle), |
| 434 browser_context_->IsOffTheRecord(), bound_net_log); | 480 browser_context_->IsOffTheRecord(), bound_net_log); |
| 435 int32 download_id = info->download_id.local(); | 481 int32 download_id = info->download_id.local(); |
| 436 | 482 |
| 437 DCHECK(!ContainsKey(active_downloads_, download_id)); | 483 DCHECK(!ContainsKey(active_downloads_, download_id)); |
| 438 downloads_.insert(download); | 484 downloads_.insert(download); |
| 439 active_downloads_[download_id] = download; | 485 active_downloads_[download_id] = download; |
| 440 | 486 |
| 441 return bound_net_log; | 487 return bound_net_log; |
| 442 } | 488 } |
| 443 | 489 |
| 444 DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem( | 490 DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem( |
| 445 const FilePath& main_file_path, | 491 const FilePath& main_file_path, |
| 446 const GURL& page_url, | 492 const GURL& page_url, |
| 447 bool is_otr, | 493 bool is_otr, |
| 448 const std::string& mime_type, | 494 const std::string& mime_type, |
| 449 DownloadItem::Observer* observer) { | 495 DownloadItem::Observer* observer) { |
| 450 net::BoundNetLog bound_net_log = | 496 net::BoundNetLog bound_net_log = |
| 451 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 497 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 452 DownloadItem* download = new DownloadItemImpl( | 498 DownloadItem* download = factory_->CreateSavePageItem( |
| 453 this, | 499 this, |
| 454 main_file_path, | 500 main_file_path, |
| 455 page_url, | 501 page_url, |
| 456 is_otr, | 502 is_otr, |
| 457 GetNextId(), | 503 GetNextId(), |
| 458 mime_type, | 504 mime_type, |
| 459 bound_net_log); | 505 bound_net_log); |
| 460 | 506 |
| 461 download->AddObserver(observer); | 507 download->AddObserver(observer); |
| 462 | 508 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 676 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 722 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 677 | 723 |
| 678 VLOG(20) << __FUNCTION__ << "()" | 724 VLOG(20) << __FUNCTION__ << "()" |
| 679 << " download = " << download->DebugString(true); | 725 << " download = " << download->DebugString(true); |
| 680 | 726 |
| 681 RemoveFromActiveList(download); | 727 RemoveFromActiveList(download); |
| 682 // This function is called from the DownloadItem, so DI state | 728 // This function is called from the DownloadItem, so DI state |
| 683 // should already have been updated. | 729 // should already have been updated. |
| 684 AssertStateConsistent(download); | 730 AssertStateConsistent(download); |
| 685 | 731 |
| 686 if (file_manager_) | 732 DCHECK(file_manager_); |
| 687 download->OffThreadCancel(file_manager_); | 733 download->OffThreadCancel(file_manager_); |
| 688 } | 734 } |
| 689 | 735 |
| 690 void DownloadManagerImpl::OnDownloadInterrupted( | 736 void DownloadManagerImpl::OnDownloadInterrupted( |
| 691 int32 download_id, | 737 int32 download_id, |
| 692 int64 size, | 738 int64 size, |
| 693 const std::string& hash_state, | 739 const std::string& hash_state, |
| 694 content::DownloadInterruptReason reason) { | 740 content::DownloadInterruptReason reason) { |
| 695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 741 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 696 | 742 |
| 697 DownloadItem* download = GetActiveDownload(download_id); | 743 DownloadItem* download = GetActiveDownload(download_id); |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 922 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
| 877 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 923 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
| 878 std::vector<DownloadPersistentStoreInfo>* entries) { | 924 std::vector<DownloadPersistentStoreInfo>* entries) { |
| 879 for (size_t i = 0; i < entries->size(); ++i) { | 925 for (size_t i = 0; i < entries->size(); ++i) { |
| 880 int64 db_handle = entries->at(i).db_handle; | 926 int64 db_handle = entries->at(i).db_handle; |
| 881 base::debug::Alias(&db_handle); | 927 base::debug::Alias(&db_handle); |
| 882 DCHECK(!ContainsKey(history_downloads_, db_handle)); | 928 DCHECK(!ContainsKey(history_downloads_, db_handle)); |
| 883 | 929 |
| 884 net::BoundNetLog bound_net_log = | 930 net::BoundNetLog bound_net_log = |
| 885 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 931 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 886 DownloadItem* download = new DownloadItemImpl( | 932 DownloadItem* download = factory_->CreatePersistedItem( |
| 887 this, GetNextId(), entries->at(i), bound_net_log); | 933 this, GetNextId(), entries->at(i), bound_net_log); |
| 888 downloads_.insert(download); | 934 downloads_.insert(download); |
| 889 history_downloads_[download->GetDbHandle()] = download; | 935 history_downloads_[download->GetDbHandle()] = download; |
| 890 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 936 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 891 << " download = " << download->DebugString(true); | 937 << " download = " << download->DebugString(true); |
| 892 } | 938 } |
| 893 NotifyModelChanged(); | 939 NotifyModelChanged(); |
| 894 CheckForHistoryFilesRemoval(); | 940 CheckForHistoryFilesRemoval(); |
| 895 } | 941 } |
| 896 | 942 |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 void DownloadManagerImpl::DownloadRenamedToFinalName( | 1192 void DownloadManagerImpl::DownloadRenamedToFinalName( |
| 1147 DownloadItem* download) { | 1193 DownloadItem* download) { |
| 1148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1149 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 1195 // If the rename failed, we receive an OnDownloadInterrupted() call before we |
| 1150 // receive the DownloadRenamedToFinalName() call. | 1196 // receive the DownloadRenamedToFinalName() call. |
| 1151 if (delegate_) { | 1197 if (delegate_) { |
| 1152 delegate_->UpdatePathForItemInPersistentStore( | 1198 delegate_->UpdatePathForItemInPersistentStore( |
| 1153 download, download->GetFullPath()); | 1199 download, download->GetFullPath()); |
| 1154 } | 1200 } |
| 1155 } | 1201 } |
| 1156 | |
| 1157 void DownloadManagerImpl::SetFileManagerForTesting( | |
| 1158 DownloadFileManager* file_manager) { | |
| 1159 file_manager_ = file_manager; | |
| 1160 } | |
| OLD | NEW |