| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include "net/base/load_flags.h" | 43 #include "net/base/load_flags.h" |
| 44 #include "net/base/request_priority.h" | 44 #include "net/base/request_priority.h" |
| 45 #include "net/base/upload_bytes_element_reader.h" | 45 #include "net/base/upload_bytes_element_reader.h" |
| 46 #include "net/url_request/url_request_context.h" | 46 #include "net/url_request/url_request_context.h" |
| 47 | 47 |
| 48 namespace content { | 48 namespace content { |
| 49 namespace { | 49 namespace { |
| 50 | 50 |
| 51 void BeginDownload(scoped_ptr<DownloadUrlParameters> params, | 51 void BeginDownload(scoped_ptr<DownloadUrlParameters> params, |
| 52 uint32 download_id) { | 52 uint32 download_id) { |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 53 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 54 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and | 54 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
| 55 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so | 55 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
| 56 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. | 56 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
| 57 scoped_ptr<net::URLRequest> request( | 57 scoped_ptr<net::URLRequest> request( |
| 58 params->resource_context()->GetRequestContext()->CreateRequest( | 58 params->resource_context()->GetRequestContext()->CreateRequest( |
| 59 params->url(), net::DEFAULT_PRIORITY, NULL, NULL)); | 59 params->url(), net::DEFAULT_PRIORITY, NULL, NULL)); |
| 60 request->set_method(params->method()); | 60 request->set_method(params->method()); |
| 61 if (!params->post_body().empty()) { | 61 if (!params->post_body().empty()) { |
| 62 const std::string& body = params->post_body(); | 62 const std::string& body = params->post_body(); |
| 63 scoped_ptr<net::UploadElementReader> reader( | 63 scoped_ptr<net::UploadElementReader> reader( |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 weak_factory_(this) { | 242 weak_factory_(this) { |
| 243 DCHECK(browser_context); | 243 DCHECK(browser_context); |
| 244 } | 244 } |
| 245 | 245 |
| 246 DownloadManagerImpl::~DownloadManagerImpl() { | 246 DownloadManagerImpl::~DownloadManagerImpl() { |
| 247 DCHECK(!shutdown_needed_); | 247 DCHECK(!shutdown_needed_); |
| 248 } | 248 } |
| 249 | 249 |
| 250 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( | 250 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( |
| 251 uint32 id, const DownloadCreateInfo& info) { | 251 uint32 id, const DownloadCreateInfo& info) { |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 253 DCHECK(!ContainsKey(downloads_, id)); | 253 DCHECK(!ContainsKey(downloads_, id)); |
| 254 net::BoundNetLog bound_net_log = | 254 net::BoundNetLog bound_net_log = |
| 255 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 255 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 256 DownloadItemImpl* download = | 256 DownloadItemImpl* download = |
| 257 item_factory_->CreateActiveItem(this, id, info, bound_net_log); | 257 item_factory_->CreateActiveItem(this, id, info, bound_net_log); |
| 258 downloads_[id] = download; | 258 downloads_[id] = download; |
| 259 return download; | 259 return download; |
| 260 } | 260 } |
| 261 | 261 |
| 262 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { | 262 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { |
| 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 263 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 264 if (delegate_) { | 264 if (delegate_) { |
| 265 delegate_->GetNextId(callback); | 265 delegate_->GetNextId(callback); |
| 266 return; | 266 return; |
| 267 } | 267 } |
| 268 static uint32 next_id = content::DownloadItem::kInvalidId + 1; | 268 static uint32 next_id = content::DownloadItem::kInvalidId + 1; |
| 269 callback.Run(next_id++); | 269 callback.Run(next_id++); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void DownloadManagerImpl::DetermineDownloadTarget( | 272 void DownloadManagerImpl::DetermineDownloadTarget( |
| 273 DownloadItemImpl* item, const DownloadTargetCallback& callback) { | 273 DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 351 |
| 352 if (delegate_) | 352 if (delegate_) |
| 353 delegate_->Shutdown(); | 353 delegate_->Shutdown(); |
| 354 delegate_ = NULL; | 354 delegate_ = NULL; |
| 355 } | 355 } |
| 356 | 356 |
| 357 void DownloadManagerImpl::StartDownload( | 357 void DownloadManagerImpl::StartDownload( |
| 358 scoped_ptr<DownloadCreateInfo> info, | 358 scoped_ptr<DownloadCreateInfo> info, |
| 359 scoped_ptr<ByteStreamReader> stream, | 359 scoped_ptr<ByteStreamReader> stream, |
| 360 const DownloadUrlParameters::OnStartedCallback& on_started) { | 360 const DownloadUrlParameters::OnStartedCallback& on_started) { |
| 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 361 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 362 DCHECK(info); | 362 DCHECK(info); |
| 363 uint32 download_id = info->download_id; | 363 uint32 download_id = info->download_id; |
| 364 const bool new_download = (download_id == content::DownloadItem::kInvalidId); | 364 const bool new_download = (download_id == content::DownloadItem::kInvalidId); |
| 365 base::Callback<void(uint32)> got_id(base::Bind( | 365 base::Callback<void(uint32)> got_id(base::Bind( |
| 366 &DownloadManagerImpl::StartDownloadWithId, | 366 &DownloadManagerImpl::StartDownloadWithId, |
| 367 weak_factory_.GetWeakPtr(), | 367 weak_factory_.GetWeakPtr(), |
| 368 base::Passed(info.Pass()), | 368 base::Passed(info.Pass()), |
| 369 base::Passed(stream.Pass()), | 369 base::Passed(stream.Pass()), |
| 370 on_started, | 370 on_started, |
| 371 new_download)); | 371 new_download)); |
| 372 if (new_download) { | 372 if (new_download) { |
| 373 GetNextId(got_id); | 373 GetNextId(got_id); |
| 374 } else { | 374 } else { |
| 375 got_id.Run(download_id); | 375 got_id.Run(download_id); |
| 376 } | 376 } |
| 377 } | 377 } |
| 378 | 378 |
| 379 void DownloadManagerImpl::StartDownloadWithId( | 379 void DownloadManagerImpl::StartDownloadWithId( |
| 380 scoped_ptr<DownloadCreateInfo> info, | 380 scoped_ptr<DownloadCreateInfo> info, |
| 381 scoped_ptr<ByteStreamReader> stream, | 381 scoped_ptr<ByteStreamReader> stream, |
| 382 const DownloadUrlParameters::OnStartedCallback& on_started, | 382 const DownloadUrlParameters::OnStartedCallback& on_started, |
| 383 bool new_download, | 383 bool new_download, |
| 384 uint32 id) { | 384 uint32 id) { |
| 385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 385 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 386 DCHECK_NE(content::DownloadItem::kInvalidId, id); | 386 DCHECK_NE(content::DownloadItem::kInvalidId, id); |
| 387 DownloadItemImpl* download = NULL; | 387 DownloadItemImpl* download = NULL; |
| 388 if (new_download) { | 388 if (new_download) { |
| 389 download = CreateActiveItem(id, *info); | 389 download = CreateActiveItem(id, *info); |
| 390 } else { | 390 } else { |
| 391 DownloadMap::iterator item_iterator = downloads_.find(id); | 391 DownloadMap::iterator item_iterator = downloads_.find(id); |
| 392 // Trying to resume an interrupted download. | 392 // Trying to resume an interrupted download. |
| 393 if (item_iterator == downloads_.end() || | 393 if (item_iterator == downloads_.end() || |
| 394 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { | 394 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { |
| 395 // If the download is no longer known to the DownloadManager, then it was | 395 // If the download is no longer known to the DownloadManager, then it was |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 // the download_file is bound to download and all the usual | 438 // the download_file is bound to download and all the usual |
| 439 // setters (e.g. Cancel) work. | 439 // setters (e.g. Cancel) work. |
| 440 if (new_download) | 440 if (new_download) |
| 441 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 441 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 442 | 442 |
| 443 if (!on_started.is_null()) | 443 if (!on_started.is_null()) |
| 444 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE); | 444 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE); |
| 445 } | 445 } |
| 446 | 446 |
| 447 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 447 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
| 448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 448 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 449 for (DownloadMap::iterator it = downloads_.begin(); | 449 for (DownloadMap::iterator it = downloads_.begin(); |
| 450 it != downloads_.end(); ++it) { | 450 it != downloads_.end(); ++it) { |
| 451 DownloadItemImpl* item = it->second; | 451 DownloadItemImpl* item = it->second; |
| 452 CheckForFileRemoval(item); | 452 CheckForFileRemoval(item); |
| 453 } | 453 } |
| 454 } | 454 } |
| 455 | 455 |
| 456 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { | 456 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
| 457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 457 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 458 if ((download_item->GetState() == DownloadItem::COMPLETE) && | 458 if ((download_item->GetState() == DownloadItem::COMPLETE) && |
| 459 !download_item->GetFileExternallyRemoved() && | 459 !download_item->GetFileExternallyRemoved() && |
| 460 delegate_) { | 460 delegate_) { |
| 461 delegate_->CheckForFileExistence( | 461 delegate_->CheckForFileExistence( |
| 462 download_item, | 462 download_item, |
| 463 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked, | 463 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked, |
| 464 weak_factory_.GetWeakPtr(), download_item->GetId())); | 464 weak_factory_.GetWeakPtr(), download_item->GetId())); |
| 465 } | 465 } |
| 466 } | 466 } |
| 467 | 467 |
| 468 void DownloadManagerImpl::OnFileExistenceChecked(uint32 download_id, | 468 void DownloadManagerImpl::OnFileExistenceChecked(uint32 download_id, |
| 469 bool result) { | 469 bool result) { |
| 470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 470 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 471 if (!result) { // File does not exist. | 471 if (!result) { // File does not exist. |
| 472 if (ContainsKey(downloads_, download_id)) | 472 if (ContainsKey(downloads_, download_id)) |
| 473 downloads_[download_id]->OnDownloadedFileRemoved(); | 473 downloads_[download_id]->OnDownloadedFileRemoved(); |
| 474 } | 474 } |
| 475 } | 475 } |
| 476 | 476 |
| 477 BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 477 BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
| 478 return browser_context_; | 478 return browser_context_; |
| 479 } | 479 } |
| 480 | 480 |
| 481 void DownloadManagerImpl::CreateSavePackageDownloadItem( | 481 void DownloadManagerImpl::CreateSavePackageDownloadItem( |
| 482 const base::FilePath& main_file_path, | 482 const base::FilePath& main_file_path, |
| 483 const GURL& page_url, | 483 const GURL& page_url, |
| 484 const std::string& mime_type, | 484 const std::string& mime_type, |
| 485 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 485 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 486 const DownloadItemImplCreated& item_created) { | 486 const DownloadItemImplCreated& item_created) { |
| 487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 487 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 488 GetNextId(base::Bind( | 488 GetNextId(base::Bind( |
| 489 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId, | 489 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId, |
| 490 weak_factory_.GetWeakPtr(), | 490 weak_factory_.GetWeakPtr(), |
| 491 main_file_path, | 491 main_file_path, |
| 492 page_url, | 492 page_url, |
| 493 mime_type, | 493 mime_type, |
| 494 base::Passed(request_handle.Pass()), | 494 base::Passed(request_handle.Pass()), |
| 495 item_created)); | 495 item_created)); |
| 496 } | 496 } |
| 497 | 497 |
| 498 void DownloadManagerImpl::CreateSavePackageDownloadItemWithId( | 498 void DownloadManagerImpl::CreateSavePackageDownloadItemWithId( |
| 499 const base::FilePath& main_file_path, | 499 const base::FilePath& main_file_path, |
| 500 const GURL& page_url, | 500 const GURL& page_url, |
| 501 const std::string& mime_type, | 501 const std::string& mime_type, |
| 502 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 502 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 503 const DownloadItemImplCreated& item_created, | 503 const DownloadItemImplCreated& item_created, |
| 504 uint32 id) { | 504 uint32 id) { |
| 505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 505 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 506 DCHECK_NE(content::DownloadItem::kInvalidId, id); | 506 DCHECK_NE(content::DownloadItem::kInvalidId, id); |
| 507 DCHECK(!ContainsKey(downloads_, id)); | 507 DCHECK(!ContainsKey(downloads_, id)); |
| 508 net::BoundNetLog bound_net_log = | 508 net::BoundNetLog bound_net_log = |
| 509 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 509 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 510 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( | 510 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
| 511 this, | 511 this, |
| 512 id, | 512 id, |
| 513 main_file_path, | 513 main_file_path, |
| 514 page_url, | 514 page_url, |
| 515 mime_type, | 515 mime_type, |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 if (delegate_) | 716 if (delegate_) |
| 717 delegate_->OpenDownload(download); | 717 delegate_->OpenDownload(download); |
| 718 } | 718 } |
| 719 | 719 |
| 720 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 720 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 721 if (delegate_) | 721 if (delegate_) |
| 722 delegate_->ShowDownloadInShell(download); | 722 delegate_->ShowDownloadInShell(download); |
| 723 } | 723 } |
| 724 | 724 |
| 725 } // namespace content | 725 } // namespace content |
| OLD | NEW |