| 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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 browser_context_(browser_context), | 240 browser_context_(browser_context), |
| 241 delegate_(NULL), | 241 delegate_(NULL), |
| 242 net_log_(net_log) { | 242 net_log_(net_log) { |
| 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 void DownloadManagerImpl::CreateActiveItem( | 250 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( |
| 251 DownloadId id, const DownloadCreateInfo& info) { | 251 DownloadId id, const DownloadCreateInfo& info) { |
| 252 net::BoundNetLog bound_net_log = | 252 net::BoundNetLog bound_net_log = |
| 253 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 253 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 254 downloads_[id.local()] = | 254 DownloadItemImpl* download = |
| 255 item_factory_->CreateActiveItem(this, id, info, bound_net_log); | 255 item_factory_->CreateActiveItem(this, id, info, bound_net_log); |
| 256 downloads_[id.local()] = download; |
| 257 return download; |
| 256 } | 258 } |
| 257 | 259 |
| 258 DownloadId DownloadManagerImpl::GetNextId() { | 260 DownloadId DownloadManagerImpl::GetNextId() { |
| 259 DownloadId id; | 261 DownloadId id; |
| 260 if (delegate_) | 262 if (delegate_) |
| 261 id = delegate_->GetNextId(); | 263 id = delegate_->GetNextId(); |
| 262 if (!id.IsValid()) { | 264 if (!id.IsValid()) { |
| 263 static int next_id; | 265 static int next_id; |
| 264 id = DownloadId(browser_context_, ++next_id); | 266 id = DownloadId(browser_context_, ++next_id); |
| 265 } | 267 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 if (delegate_) | 372 if (delegate_) |
| 371 delegate_->Shutdown(); | 373 delegate_->Shutdown(); |
| 372 delegate_ = NULL; | 374 delegate_ = NULL; |
| 373 } | 375 } |
| 374 | 376 |
| 375 DownloadItem* DownloadManagerImpl::StartDownload( | 377 DownloadItem* DownloadManagerImpl::StartDownload( |
| 376 scoped_ptr<DownloadCreateInfo> info, | 378 scoped_ptr<DownloadCreateInfo> info, |
| 377 scoped_ptr<ByteStreamReader> stream) { | 379 scoped_ptr<ByteStreamReader> stream) { |
| 378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 379 | 381 |
| 382 DownloadId id(info->download_id); |
| 383 const bool new_download = !id.IsValid(); |
| 384 DownloadItemImpl* download = NULL; |
| 385 |
| 386 if (new_download) { |
| 387 id = GetNextId(); |
| 388 download = CreateActiveItem(id, *info); |
| 389 } else { |
| 390 DownloadMap::iterator item_iterator = downloads_.find(id.local()); |
| 391 // Trying to resume an interrupted download. |
| 392 if (item_iterator == downloads_.end()) { |
| 393 // If the download is no longer known to the DownloadManager, then it was |
| 394 // removed after it was resumed. Ignore. |
| 395 info->request_handle.CancelRequest(); |
| 396 return NULL; |
| 397 } |
| 398 download = item_iterator->second; |
| 399 DCHECK(download->IsInterrupted()); |
| 400 } |
| 401 |
| 380 base::FilePath default_download_directory; | 402 base::FilePath default_download_directory; |
| 381 if (delegate_) { | 403 if (delegate_) { |
| 382 base::FilePath website_save_directory; // Unused | 404 base::FilePath website_save_directory; // Unused |
| 383 bool skip_dir_check = false; // Unused | 405 bool skip_dir_check = false; // Unused |
| 384 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 406 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
| 385 &default_download_directory, &skip_dir_check); | 407 &default_download_directory, &skip_dir_check); |
| 386 } | 408 } |
| 387 | 409 |
| 388 // If we don't have a valid id, that's a signal to generate one. | |
| 389 DownloadId id(info->download_id); | |
| 390 if (!id.IsValid()) | |
| 391 id = GetNextId(); | |
| 392 | |
| 393 // Create a new download item if this isn't a resumption. | |
| 394 bool new_download(!ContainsKey(downloads_, id.local())); | |
| 395 if (new_download) | |
| 396 CreateActiveItem(id, *info); | |
| 397 | |
| 398 DownloadItemImpl* download(downloads_[id.local()]); | |
| 399 DCHECK(download); | |
| 400 DCHECK(new_download || download->IsInterrupted()); | |
| 401 | |
| 402 // Create the download file and start the download. | 410 // Create the download file and start the download. |
| 403 scoped_ptr<DownloadFile> download_file( | 411 scoped_ptr<DownloadFile> download_file( |
| 404 file_factory_->CreateFile( | 412 file_factory_->CreateFile( |
| 405 info->save_info.Pass(), default_download_directory, | 413 info->save_info.Pass(), default_download_directory, |
| 406 info->url(), info->referrer_url, | 414 info->url(), info->referrer_url, |
| 407 delegate_->GenerateFileHash(), | 415 delegate_->GenerateFileHash(), |
| 408 stream.Pass(), download->GetBoundNetLog(), | 416 stream.Pass(), download->GetBoundNetLog(), |
| 409 download->DestinationObserverAsWeakPtr())); | 417 download->DestinationObserverAsWeakPtr())); |
| 410 scoped_ptr<DownloadRequestHandleInterface> req_handle( | 418 scoped_ptr<DownloadRequestHandleInterface> req_handle( |
| 411 new DownloadRequestHandle(info->request_handle)); | 419 new DownloadRequestHandle(info->request_handle)); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 if (delegate_) | 663 if (delegate_) |
| 656 delegate_->OpenDownload(download); | 664 delegate_->OpenDownload(download); |
| 657 } | 665 } |
| 658 | 666 |
| 659 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 667 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 660 if (delegate_) | 668 if (delegate_) |
| 661 delegate_->ShowDownloadInShell(download); | 669 delegate_->ShowDownloadInShell(download); |
| 662 } | 670 } |
| 663 | 671 |
| 664 } // namespace content | 672 } // namespace content |
| OLD | NEW |