Chromium Code Reviews| 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 | |
| 140 : public DownloadManagerImpl::DownloadItemFactory { | |
| 141 public: | |
| 142 DownloadItemFactoryImpl() {} | |
| 143 virtual ~DownloadItemFactoryImpl() {} | |
| 144 | |
| 145 virtual content::DownloadItem* CreatePersistedItem( | |
| 146 DownloadItemImpl::Delegate* delegate, | |
| 147 content::DownloadId download_id, | |
| 148 const content::DownloadPersistentStoreInfo& info, | |
| 149 const net::BoundNetLog& bound_net_log) OVERRIDE; | |
| 150 | |
| 151 virtual content::DownloadItem* CreateActiveItem( | |
| 152 DownloadItemImpl::Delegate* delegate, | |
| 153 const DownloadCreateInfo& info, | |
| 154 DownloadRequestHandleInterface* request_handle, | |
| 155 bool is_otr, | |
| 156 const net::BoundNetLog& bound_net_log) OVERRIDE; | |
| 157 | |
| 158 virtual content::DownloadItem* CreateSavePageItem( | |
| 159 DownloadItemImpl::Delegate* delegate, | |
| 160 const FilePath& path, | |
| 161 const GURL& url, | |
| 162 bool is_otr, | |
| 163 content::DownloadId download_id, | |
| 164 const std::string& mime_type, | |
| 165 const net::BoundNetLog& bound_net_log) OVERRIDE; | |
| 166 }; | |
| 167 | |
| 168 | |
| 169 content::DownloadItem* DownloadItemFactoryImpl::CreatePersistedItem( | |
| 170 DownloadItemImpl::Delegate* delegate, | |
| 171 content::DownloadId download_id, | |
| 172 const content::DownloadPersistentStoreInfo& info, | |
| 173 const net::BoundNetLog& bound_net_log) { | |
| 174 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); | |
| 175 } | |
| 176 | |
| 177 content::DownloadItem* DownloadItemFactoryImpl::CreateActiveItem( | |
| 178 DownloadItemImpl::Delegate* delegate, | |
| 179 const DownloadCreateInfo& info, | |
| 180 DownloadRequestHandleInterface* request_handle, | |
| 181 bool is_otr, | |
| 182 const net::BoundNetLog& bound_net_log) { | |
| 183 return new DownloadItemImpl(delegate, info, request_handle, is_otr, | |
| 184 bound_net_log); | |
| 185 } | |
| 186 | |
| 187 content::DownloadItem* DownloadItemFactoryImpl::CreateSavePageItem( | |
| 188 DownloadItemImpl::Delegate* delegate, | |
| 189 const FilePath& path, | |
| 190 const GURL& url, | |
| 191 bool is_otr, | |
| 192 content::DownloadId download_id, | |
| 193 const std::string& mime_type, | |
| 194 const net::BoundNetLog& bound_net_log) { | |
| 195 return new DownloadItemImpl(delegate, path, url, is_otr, download_id, | |
| 196 mime_type, bound_net_log); | |
| 197 } | |
| 198 | |
| 139 } // namespace | 199 } // namespace |
| 140 | 200 |
| 141 namespace content { | 201 namespace content { |
| 142 | 202 |
| 143 // static | 203 // static |
| 144 DownloadManager* DownloadManager::Create( | 204 DownloadManager* DownloadManager::Create( |
| 145 content::DownloadManagerDelegate* delegate, | 205 content::DownloadManagerDelegate* delegate, |
| 146 net::NetLog* net_log) { | 206 net::NetLog* net_log) { |
| 147 return new DownloadManagerImpl(delegate, net_log); | 207 // Find the DownloadFileManager for use when creating the DownloadManager. |
| 208 // Note that unit tests should construct based on the underlying | |
| 209 // constructor, so ResourceDispatcherHostImpl & DownloadFileManager | |
| 210 // should always be present when using this code. | |
| 211 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); | |
| 212 DownloadFileManager* file_manager = NULL; | |
| 213 DCHECK(rdh); | |
| 214 file_manager = rdh->download_file_manager(); | |
| 215 DCHECK(file_manager); | |
| 216 | |
| 217 return new DownloadManagerImpl(delegate, file_manager, NULL, net_log); | |
|
asanka
2012/06/07 17:04:53
Why not create the DownloadItemFactoryImpl instanc
Randy Smith (Not in Mondays)
2012/06/07 23:27:21
Fair question, and I'll try to implement the spirt
| |
| 148 } | 218 } |
| 149 | 219 |
| 150 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 220 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 152 bool result = true; | 222 bool result = true; |
| 153 BrowserThread::PostTask( | 223 BrowserThread::PostTask( |
| 154 BrowserThread::IO, FROM_HERE, | 224 BrowserThread::IO, FROM_HERE, |
| 155 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 225 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
| 156 MessageLoop::current()->Run(); | 226 MessageLoop::current()->Run(); |
| 157 return result; | 227 return result; |
| 158 } | 228 } |
| 159 | 229 |
| 160 } // namespace content | 230 } // namespace content |
| 161 | 231 |
| 162 DownloadManagerImpl::DownloadManagerImpl( | 232 DownloadManagerImpl::DownloadManagerImpl( |
| 163 content::DownloadManagerDelegate* delegate, | 233 content::DownloadManagerDelegate* delegate, |
| 234 DownloadFileManager* file_manager, | |
| 235 DownloadManagerImpl::DownloadItemFactory *factory, | |
| 164 net::NetLog* net_log) | 236 net::NetLog* net_log) |
| 165 : shutdown_needed_(false), | 237 : factory_(factory), |
| 166 browser_context_(NULL), | 238 shutdown_needed_(false), |
| 167 file_manager_(NULL), | 239 browser_context_(NULL), |
| 168 delegate_(delegate), | 240 file_manager_(file_manager), |
| 169 net_log_(net_log) { | 241 delegate_(delegate), |
| 242 net_log_(net_log) { | |
| 243 if (!factory_.get()) | |
| 244 factory_.reset(new DownloadItemFactoryImpl()); | |
| 170 } | 245 } |
| 171 | 246 |
| 172 DownloadManagerImpl::~DownloadManagerImpl() { | 247 DownloadManagerImpl::~DownloadManagerImpl() { |
| 173 DCHECK(!shutdown_needed_); | 248 DCHECK(!shutdown_needed_); |
| 174 } | 249 } |
| 175 | 250 |
| 176 DownloadId DownloadManagerImpl::GetNextId() { | 251 DownloadId DownloadManagerImpl::GetNextId() { |
| 177 return delegate_->GetNextId(); | 252 return delegate_->GetNextId(); |
| 178 } | 253 } |
| 179 | 254 |
| 180 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) { | 255 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) { |
| 181 return delegate_->ShouldOpenDownload(item); | 256 return delegate_->ShouldOpenDownload(item); |
| 182 } | 257 } |
| 183 | 258 |
| 184 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 259 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
| 185 return delegate_->ShouldOpenFileBasedOnExtension(path); | 260 return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 186 } | 261 } |
| 187 | 262 |
| 188 void DownloadManagerImpl::Shutdown() { | 263 void DownloadManagerImpl::Shutdown() { |
| 189 VLOG(20) << __FUNCTION__ << "()" | 264 VLOG(20) << __FUNCTION__ << "()" |
| 190 << " shutdown_needed_ = " << shutdown_needed_; | 265 << " shutdown_needed_ = " << shutdown_needed_; |
| 191 if (!shutdown_needed_) | 266 if (!shutdown_needed_) |
| 192 return; | 267 return; |
| 193 shutdown_needed_ = false; | 268 shutdown_needed_ = false; |
| 194 | 269 |
| 195 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 270 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
| 196 // TODO(benjhayden): Consider clearing observers_. | 271 // TODO(benjhayden): Consider clearing observers_. |
| 197 | 272 |
| 198 if (file_manager_) { | 273 DCHECK(file_manager_); |
| 199 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 274 BrowserThread::PostTask( |
| 200 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | 275 BrowserThread::FILE, FROM_HERE, |
| 201 file_manager_, make_scoped_refptr(this))); | 276 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, |
| 202 } | 277 file_manager_, make_scoped_refptr(this))); |
| 203 | 278 |
| 204 AssertContainersConsistent(); | 279 AssertContainersConsistent(); |
| 205 | 280 |
| 206 // Go through all downloads in downloads_. Dangerous ones we need to | 281 // Go through all downloads in downloads_. Dangerous ones we need to |
| 207 // remove on disk, and in progress ones we need to cancel. | 282 // remove on disk, and in progress ones we need to cancel. |
| 208 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { | 283 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { |
| 209 DownloadItem* download = *it; | 284 DownloadItem* download = *it; |
| 210 | 285 |
| 211 // Save iterator from potential erases in this set done by called code. | 286 // Save iterator from potential erases in this set done by called code. |
| 212 // Iterators after an erasure point are still valid for lists and | 287 // Iterators after an erasure point are still valid for lists and |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 // The Incognito Downloads page will get the list of non-Incognito downloads | 368 // The Incognito Downloads page will get the list of non-Incognito downloads |
| 294 // from its parent profile. | 369 // from its parent profile. |
| 295 if (browser_context_->IsOffTheRecord() != download_item->IsOtr()) | 370 if (browser_context_->IsOffTheRecord() != download_item->IsOtr()) |
| 296 continue; | 371 continue; |
| 297 | 372 |
| 298 if (download_item->MatchesQuery(query_lower)) | 373 if (download_item->MatchesQuery(query_lower)) |
| 299 result->push_back(download_item); | 374 result->push_back(download_item); |
| 300 } | 375 } |
| 301 } | 376 } |
| 302 | 377 |
| 303 // Query the history service for information about all persisted downloads. | |
| 304 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 378 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
| 305 DCHECK(browser_context); | 379 DCHECK(browser_context); |
| 306 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 380 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 307 shutdown_needed_ = true; | 381 shutdown_needed_ = true; |
| 308 | 382 |
| 309 browser_context_ = browser_context; | 383 browser_context_ = browser_context; |
| 310 | 384 |
| 311 // In test mode, there may be no ResourceDispatcherHostImpl. In this case | |
| 312 // it's safe to avoid setting |file_manager_| because we only call a small | |
| 313 // set of functions, none of which need it. | |
| 314 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); | |
| 315 if (rdh) { | |
| 316 file_manager_ = rdh->download_file_manager(); | |
| 317 DCHECK(file_manager_); | |
| 318 } | |
| 319 | |
| 320 return true; | 385 return true; |
| 321 } | 386 } |
| 322 | 387 |
| 323 // We have received a message from DownloadFileManager about a new download. | 388 // We have received a message from DownloadFileManager about a new download. |
| 324 void DownloadManagerImpl::StartDownload(int32 download_id) { | 389 void DownloadManagerImpl::StartDownload(int32 download_id) { |
| 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 326 | 391 |
| 327 if (delegate_->ShouldStartDownload(download_id)) | 392 if (delegate_->ShouldStartDownload(download_id)) |
| 328 RestartDownload(download_id); | 393 RestartDownload(download_id); |
| 329 } | 394 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 FilePath DownloadManagerImpl::LastDownloadPath() { | 466 FilePath DownloadManagerImpl::LastDownloadPath() { |
| 402 return last_download_path_; | 467 return last_download_path_; |
| 403 } | 468 } |
| 404 | 469 |
| 405 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 470 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( |
| 406 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { | 471 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { |
| 407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 408 | 473 |
| 409 net::BoundNetLog bound_net_log = | 474 net::BoundNetLog bound_net_log = |
| 410 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 475 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 411 DownloadItem* download = new DownloadItemImpl( | 476 DownloadItem* download = factory_->CreateActiveItem( |
| 412 this, *info, new DownloadRequestHandle(request_handle), | 477 this, *info, new DownloadRequestHandle(request_handle), |
| 413 browser_context_->IsOffTheRecord(), bound_net_log); | 478 browser_context_->IsOffTheRecord(), bound_net_log); |
| 414 int32 download_id = info->download_id.local(); | 479 int32 download_id = info->download_id.local(); |
| 415 | 480 |
| 416 DCHECK(!ContainsKey(active_downloads_, download_id)); | 481 DCHECK(!ContainsKey(active_downloads_, download_id)); |
| 417 downloads_.insert(download); | 482 downloads_.insert(download); |
| 418 active_downloads_[download_id] = download; | 483 active_downloads_[download_id] = download; |
| 419 | 484 |
| 420 return bound_net_log; | 485 return bound_net_log; |
| 421 } | 486 } |
| 422 | 487 |
| 423 DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem( | 488 DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem( |
| 424 const FilePath& main_file_path, | 489 const FilePath& main_file_path, |
| 425 const GURL& page_url, | 490 const GURL& page_url, |
| 426 bool is_otr, | 491 bool is_otr, |
| 427 const std::string& mime_type, | 492 const std::string& mime_type, |
| 428 DownloadItem::Observer* observer) { | 493 DownloadItem::Observer* observer) { |
| 429 net::BoundNetLog bound_net_log = | 494 net::BoundNetLog bound_net_log = |
| 430 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 495 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 431 DownloadItem* download = new DownloadItemImpl( | 496 DownloadItem* download = factory_->CreateSavePageItem( |
| 432 this, | 497 this, |
| 433 main_file_path, | 498 main_file_path, |
| 434 page_url, | 499 page_url, |
| 435 is_otr, | 500 is_otr, |
| 436 GetNextId(), | 501 GetNextId(), |
| 437 mime_type, | 502 mime_type, |
| 438 bound_net_log); | 503 bound_net_log); |
| 439 | 504 |
| 440 download->AddObserver(observer); | 505 download->AddObserver(observer); |
| 441 | 506 |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 647 | 712 |
| 648 VLOG(20) << __FUNCTION__ << "()" | 713 VLOG(20) << __FUNCTION__ << "()" |
| 649 << " download = " << download->DebugString(true); | 714 << " download = " << download->DebugString(true); |
| 650 | 715 |
| 651 RemoveFromActiveList(download); | 716 RemoveFromActiveList(download); |
| 652 // This function is called from the DownloadItem, so DI state | 717 // This function is called from the DownloadItem, so DI state |
| 653 // should already have been updated. | 718 // should already have been updated. |
| 654 AssertStateConsistent(download); | 719 AssertStateConsistent(download); |
| 655 | 720 |
| 656 if (file_manager_) | 721 DCHECK(file_manager_); |
| 657 download->OffThreadCancel(file_manager_); | 722 download->OffThreadCancel(file_manager_); |
| 658 } | 723 } |
| 659 | 724 |
| 660 void DownloadManagerImpl::OnDownloadInterrupted( | 725 void DownloadManagerImpl::OnDownloadInterrupted( |
| 661 int32 download_id, | 726 int32 download_id, |
| 662 int64 size, | 727 int64 size, |
| 663 const std::string& hash_state, | 728 const std::string& hash_state, |
| 664 content::DownloadInterruptReason reason) { | 729 content::DownloadInterruptReason reason) { |
| 665 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 730 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 666 | 731 |
| 667 DownloadItem* download = GetActiveDownload(download_id); | 732 DownloadItem* download = GetActiveDownload(download_id); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 852 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 917 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
| 853 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 918 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
| 854 std::vector<DownloadPersistentStoreInfo>* entries) { | 919 std::vector<DownloadPersistentStoreInfo>* entries) { |
| 855 for (size_t i = 0; i < entries->size(); ++i) { | 920 for (size_t i = 0; i < entries->size(); ++i) { |
| 856 int64 db_handle = entries->at(i).db_handle; | 921 int64 db_handle = entries->at(i).db_handle; |
| 857 base::debug::Alias(&db_handle); | 922 base::debug::Alias(&db_handle); |
| 858 DCHECK(!ContainsKey(history_downloads_, db_handle)); | 923 DCHECK(!ContainsKey(history_downloads_, db_handle)); |
| 859 | 924 |
| 860 net::BoundNetLog bound_net_log = | 925 net::BoundNetLog bound_net_log = |
| 861 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 926 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 862 DownloadItem* download = new DownloadItemImpl( | 927 DownloadItem* download = factory_->CreatePersistedItem( |
| 863 this, GetNextId(), entries->at(i), bound_net_log); | 928 this, GetNextId(), entries->at(i), bound_net_log); |
| 864 downloads_.insert(download); | 929 downloads_.insert(download); |
| 865 history_downloads_[download->GetDbHandle()] = download; | 930 history_downloads_[download->GetDbHandle()] = download; |
| 866 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 931 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 867 << " download = " << download->DebugString(true); | 932 << " download = " << download->DebugString(true); |
| 868 } | 933 } |
| 869 NotifyModelChanged(); | 934 NotifyModelChanged(); |
| 870 CheckForHistoryFilesRemoval(); | 935 CheckForHistoryFilesRemoval(); |
| 871 } | 936 } |
| 872 | 937 |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1116 } | 1181 } |
| 1117 | 1182 |
| 1118 void DownloadManagerImpl::DownloadRenamedToFinalName( | 1183 void DownloadManagerImpl::DownloadRenamedToFinalName( |
| 1119 DownloadItem* download) { | 1184 DownloadItem* download) { |
| 1120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1121 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 1186 // If the rename failed, we receive an OnDownloadInterrupted() call before we |
| 1122 // receive the DownloadRenamedToFinalName() call. | 1187 // receive the DownloadRenamedToFinalName() call. |
| 1123 delegate_->UpdatePathForItemInPersistentStore(download, | 1188 delegate_->UpdatePathForItemInPersistentStore(download, |
| 1124 download->GetFullPath()); | 1189 download->GetFullPath()); |
| 1125 } | 1190 } |
| 1126 | |
| 1127 void DownloadManagerImpl::SetFileManagerForTesting( | |
| 1128 DownloadFileManager* file_manager) { | |
| 1129 file_manager_ = file_manager; | |
| 1130 } | |
| OLD | NEW |