| 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" |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/supports_user_data.h" | 18 #include "base/supports_user_data.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/sys_string_conversions.h" | 20 #include "base/sys_string_conversions.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "content/browser/download/byte_stream.h" | 22 #include "content/browser/download/byte_stream.h" |
| 23 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
| 24 #include "content/browser/download/download_file_manager.h" | 24 #include "content/browser/download/download_file_factory.h" |
| 25 #include "content/browser/download/download_item_impl.h" | 25 #include "content/browser/download/download_item_impl.h" |
| 26 #include "content/browser/download/download_stats.h" | 26 #include "content/browser/download/download_stats.h" |
| 27 #include "content/browser/renderer_host/render_view_host_impl.h" | 27 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
| 29 #include "content/browser/web_contents/web_contents_impl.h" | 29 #include "content/browser/web_contents/web_contents_impl.h" |
| 30 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/content_browser_client.h" | 32 #include "content/public/browser/content_browser_client.h" |
| 33 #include "content/public/browser/download_interrupt_reasons.h" | 33 #include "content/public/browser/download_interrupt_reasons.h" |
| 34 #include "content/public/browser/download_manager_delegate.h" | 34 #include "content/public/browser/download_manager_delegate.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 137 |
| 138 bool operator!=(const MapValueIteratorAdapter& that) const { | 138 bool operator!=(const MapValueIteratorAdapter& that) const { |
| 139 return iter_ != that.iter_; | 139 return iter_ != that.iter_; |
| 140 } | 140 } |
| 141 | 141 |
| 142 private: | 142 private: |
| 143 base::hash_map<int64, DownloadItem*>::const_iterator iter_; | 143 base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
| 144 // Allow copy and assign. | 144 // Allow copy and assign. |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, | 147 void EnsureNoPendingDownloadJobsOnFile(bool* result) { |
| 148 bool* result) { | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 if (dfm->NumberOfActiveDownloads()) | 149 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0); |
| 150 *result = false; | |
| 151 BrowserThread::PostTask( | 150 BrowserThread::PostTask( |
| 152 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 151 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 153 } | 152 } |
| 154 | 153 |
| 155 void EnsureNoPendingDownloadJobsOnIO(bool* result) { | |
| 156 scoped_refptr<DownloadFileManager> download_file_manager = | |
| 157 ResourceDispatcherHostImpl::Get()->download_file_manager(); | |
| 158 BrowserThread::PostTask( | |
| 159 BrowserThread::FILE, FROM_HERE, | |
| 160 base::Bind(&EnsureNoPendingDownloadsOnFile, | |
| 161 download_file_manager, result)); | |
| 162 } | |
| 163 | |
| 164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 154 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
| 165 public: | 155 public: |
| 166 DownloadItemFactoryImpl() {} | 156 DownloadItemFactoryImpl() {} |
| 167 virtual ~DownloadItemFactoryImpl() {} | 157 virtual ~DownloadItemFactoryImpl() {} |
| 168 | 158 |
| 169 virtual DownloadItemImpl* CreatePersistedItem( | 159 virtual DownloadItemImpl* CreatePersistedItem( |
| 170 DownloadItemImplDelegate* delegate, | 160 DownloadItemImplDelegate* delegate, |
| 171 content::DownloadId download_id, | 161 content::DownloadId download_id, |
| 172 const content::DownloadPersistentStoreInfo& info, | 162 const content::DownloadPersistentStoreInfo& info, |
| 173 const net::BoundNetLog& bound_net_log) OVERRIDE { | 163 const net::BoundNetLog& bound_net_log) OVERRIDE { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 196 }; | 186 }; |
| 197 | 187 |
| 198 } // namespace | 188 } // namespace |
| 199 | 189 |
| 200 namespace content { | 190 namespace content { |
| 201 | 191 |
| 202 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 192 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
| 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 204 bool result = true; | 194 bool result = true; |
| 205 BrowserThread::PostTask( | 195 BrowserThread::PostTask( |
| 206 BrowserThread::IO, FROM_HERE, | 196 BrowserThread::FILE, FROM_HERE, |
| 207 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 197 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result)); |
| 208 MessageLoop::current()->Run(); | 198 MessageLoop::current()->Run(); |
| 209 return result; | 199 return result; |
| 210 } | 200 } |
| 211 | 201 |
| 212 } // namespace content | 202 } // namespace content |
| 213 | 203 |
| 214 DownloadManagerImpl::DownloadManagerImpl( | 204 DownloadManagerImpl::DownloadManagerImpl( |
| 215 DownloadFileManager* file_manager, | 205 scoped_ptr<content::DownloadItemFactory> item_factory, |
| 216 scoped_ptr<content::DownloadItemFactory> factory, | |
| 217 net::NetLog* net_log) | 206 net::NetLog* net_log) |
| 218 : factory_(factory.Pass()), | 207 : item_factory_(item_factory.Pass()), |
| 219 history_size_(0), | 208 history_size_(0), |
| 220 shutdown_needed_(false), | 209 shutdown_needed_(false), |
| 221 browser_context_(NULL), | 210 browser_context_(NULL), |
| 222 file_manager_(file_manager), | |
| 223 delegate_(NULL), | 211 delegate_(NULL), |
| 224 net_log_(net_log) { | 212 net_log_(net_log) { |
| 225 DCHECK(file_manager); | 213 if (!item_factory_.get()) |
| 226 if (!factory_.get()) | 214 item_factory_.reset(new DownloadItemFactoryImpl()); |
| 227 factory_.reset(new DownloadItemFactoryImpl()); | 215 file_factory_.reset(new content::DownloadFileFactory()); |
| 228 } | 216 } |
| 229 | 217 |
| 230 DownloadManagerImpl::~DownloadManagerImpl() { | 218 DownloadManagerImpl::~DownloadManagerImpl() { |
| 231 DCHECK(!shutdown_needed_); | 219 DCHECK(!shutdown_needed_); |
| 232 } | 220 } |
| 233 | 221 |
| 234 DownloadId DownloadManagerImpl::GetNextId() { | 222 DownloadId DownloadManagerImpl::GetNextId() { |
| 235 DownloadId id; | 223 DownloadId id; |
| 236 if (delegate_) | 224 if (delegate_) |
| 237 id = delegate_->GetNextId(); | 225 id = delegate_->GetNextId(); |
| 238 if (!id.IsValid()) { | 226 if (!id.IsValid()) { |
| 239 static int next_id; | 227 static int next_id; |
| 240 id = DownloadId(browser_context_, ++next_id); | 228 id = DownloadId(browser_context_, ++next_id); |
| 241 } | 229 } |
| 242 | 230 |
| 243 return id; | 231 return id; |
| 244 } | 232 } |
| 245 | 233 |
| 246 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { | 234 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) { |
| 247 return file_manager_; | 235 content::DownloadTargetCallback callback = |
| 236 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, |
| 237 this, item->GetId()); |
| 238 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
| 239 FilePath target_path = item->GetForcedFilePath(); |
| 240 // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 241 callback.Run(target_path, |
| 242 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 243 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 244 target_path); |
| 245 } |
| 248 } | 246 } |
| 249 | 247 |
| 250 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { | 248 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
| 251 if (!delegate_) | 249 if (!delegate_) |
| 252 return true; | 250 return true; |
| 253 | 251 |
| 254 return delegate_->ShouldOpenDownload(item); | 252 return delegate_->ShouldOpenDownload(item); |
| 255 } | 253 } |
| 256 | 254 |
| 257 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 255 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 273 void DownloadManagerImpl::Shutdown() { | 271 void DownloadManagerImpl::Shutdown() { |
| 274 VLOG(20) << __FUNCTION__ << "()" | 272 VLOG(20) << __FUNCTION__ << "()" |
| 275 << " shutdown_needed_ = " << shutdown_needed_; | 273 << " shutdown_needed_ = " << shutdown_needed_; |
| 276 if (!shutdown_needed_) | 274 if (!shutdown_needed_) |
| 277 return; | 275 return; |
| 278 shutdown_needed_ = false; | 276 shutdown_needed_ = false; |
| 279 | 277 |
| 280 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 278 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
| 281 // TODO(benjhayden): Consider clearing observers_. | 279 // TODO(benjhayden): Consider clearing observers_. |
| 282 | 280 |
| 283 DCHECK(file_manager_); | 281 // The DownloadFiles will be canceled and deleted by their DownloadItems. |
| 284 BrowserThread::PostTask( | |
| 285 BrowserThread::FILE, FROM_HERE, | |
| 286 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | |
| 287 file_manager_, make_scoped_refptr(this))); | |
| 288 | 282 |
| 289 AssertContainersConsistent(); | 283 AssertContainersConsistent(); |
| 290 | 284 |
| 291 // Go through all downloads in downloads_. Dangerous ones we need to | 285 // Go through all downloads in downloads_. Dangerous ones we need to |
| 292 // remove on disk, and in progress ones we need to cancel. | 286 // remove on disk, and in progress ones we need to cancel. |
| 293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { | 287 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
| 294 DownloadItemImpl* download = it->second; | 288 DownloadItemImpl* download = it->second; |
| 295 | 289 |
| 296 // Save iterator from potential erases in this set done by called code. | 290 // Save iterator from potential erases in this set done by called code. |
| 297 // Iterators after an erasure point are still valid for lists and | 291 // Iterators after an erasure point are still valid for lists and |
| (...skipping 22 matching lines...) Expand all Loading... |
| 320 // and all in progress downloads have been cancelled. We can now delete | 314 // and all in progress downloads have been cancelled. We can now delete |
| 321 // anything left. | 315 // anything left. |
| 322 | 316 |
| 323 active_downloads_.clear(); | 317 active_downloads_.clear(); |
| 324 STLDeleteValues(&downloads_); | 318 STLDeleteValues(&downloads_); |
| 325 downloads_.clear(); | 319 downloads_.clear(); |
| 326 | 320 |
| 327 // We'll have nothing more to report to the observers after this point. | 321 // We'll have nothing more to report to the observers after this point. |
| 328 observers_.Clear(); | 322 observers_.Clear(); |
| 329 | 323 |
| 330 file_manager_ = NULL; | |
| 331 if (delegate_) | 324 if (delegate_) |
| 332 delegate_->Shutdown(); | 325 delegate_->Shutdown(); |
| 333 delegate_ = NULL; | 326 delegate_ = NULL; |
| 334 } | 327 } |
| 335 | 328 |
| 336 void DownloadManagerImpl::GetTemporaryDownloads( | 329 void DownloadManagerImpl::GetTemporaryDownloads( |
| 337 const FilePath& dir_path, DownloadVector* result) { | 330 const FilePath& dir_path, DownloadVector* result) { |
| 338 DCHECK(result); | 331 DCHECK(result); |
| 339 | 332 |
| 340 for (DownloadMap::iterator it = downloads_.begin(); | 333 for (DownloadMap::iterator it = downloads_.begin(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 377 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
| 385 DCHECK(browser_context); | 378 DCHECK(browser_context); |
| 386 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 379 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 387 shutdown_needed_ = true; | 380 shutdown_needed_ = true; |
| 388 | 381 |
| 389 browser_context_ = browser_context; | 382 browser_context_ = browser_context; |
| 390 | 383 |
| 391 return true; | 384 return true; |
| 392 } | 385 } |
| 393 | 386 |
| 394 // We have received a message from DownloadFileManager about a new download. | |
| 395 content::DownloadId DownloadManagerImpl::StartDownload( | 387 content::DownloadId DownloadManagerImpl::StartDownload( |
| 396 scoped_ptr<DownloadCreateInfo> info, | 388 scoped_ptr<DownloadCreateInfo> info, |
| 397 scoped_ptr<content::ByteStreamReader> stream) { | 389 scoped_ptr<content::ByteStreamReader> stream) { |
| 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 399 | 391 |
| 400 // |bound_net_log| will be used for logging both the download item's and | 392 net::BoundNetLog bound_net_log = |
| 401 // the download file's events. | 393 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 402 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | |
| 403 | 394 |
| 404 // If info->download_id was unknown on entry to this function, it was | 395 // We create the DownloadItem before the DownloadFile because the |
| 405 // assigned in CreateDownloadItem. | 396 // DownloadItem already needs to handle a state in which there is |
| 406 DownloadId download_id = info->download_id; | 397 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
| 398 DownloadItemImpl* download = |
| 399 CreateDownloadItem(info.get(), bound_net_log); |
| 400 scoped_ptr<content::DownloadFile> download_file( |
| 401 file_factory_->CreateFile( |
| 402 info->save_info, info->url(), info->referrer_url, |
| 403 info->received_bytes, GenerateFileHash(), |
| 404 stream.Pass(), bound_net_log, |
| 405 download->DestinationObserverAsWeakPtr())); |
| 406 download->Start(download_file.Pass()); |
| 407 | 407 |
| 408 DownloadFileManager::CreateDownloadFileCallback callback( | 408 return download->GetGlobalId(); |
| 409 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | |
| 410 this, download_id.local())); | |
| 411 | |
| 412 BrowserThread::PostTask( | |
| 413 BrowserThread::FILE, FROM_HERE, | |
| 414 base::Bind(&DownloadFileManager::CreateDownloadFile, | |
| 415 file_manager_, base::Passed(info.Pass()), | |
| 416 base::Passed(stream.Pass()), | |
| 417 make_scoped_refptr(this), | |
| 418 GenerateFileHash(), bound_net_log, | |
| 419 callback)); | |
| 420 | |
| 421 return download_id; | |
| 422 } | |
| 423 | |
| 424 void DownloadManagerImpl::OnDownloadFileCreated( | |
| 425 int32 download_id, content::DownloadInterruptReason reason) { | |
| 426 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 427 OnDownloadInterrupted(download_id, reason); | |
| 428 // TODO(rdsmith): It makes no sense to continue along the | |
| 429 // regular download path after we've gotten an error. But it's | |
| 430 // the way the code has historically worked, and this allows us | |
| 431 // to get the download persisted and observers of the download manager | |
| 432 // notified, so tests work. When we execute all side effects of cancel | |
| 433 // (including queue removal) immedately rather than waiting for | |
| 434 // persistence we should replace this comment with a "return;". | |
| 435 } | |
| 436 | |
| 437 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | |
| 438 if (download_iter == active_downloads_.end()) | |
| 439 return; | |
| 440 | |
| 441 DownloadItemImpl* download = download_iter->second; | |
| 442 content::DownloadTargetCallback callback = | |
| 443 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, | |
| 444 this, download_id); | |
| 445 if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) { | |
| 446 FilePath target_path = download->GetForcedFilePath(); | |
| 447 // TODO(asanka): Determine a useful path if |target_path| is empty. | |
| 448 callback.Run(target_path, | |
| 449 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 450 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 451 target_path); | |
| 452 } | |
| 453 } | 409 } |
| 454 | 410 |
| 455 void DownloadManagerImpl::OnDownloadTargetDetermined( | 411 void DownloadManagerImpl::OnDownloadTargetDetermined( |
| 456 int32 download_id, | 412 int32 download_id, |
| 457 const FilePath& target_path, | 413 const FilePath& target_path, |
| 458 DownloadItem::TargetDisposition disposition, | 414 DownloadItem::TargetDisposition disposition, |
| 459 content::DownloadDangerType danger_type, | 415 content::DownloadDangerType danger_type, |
| 460 const FilePath& intermediate_path) { | 416 const FilePath& intermediate_path) { |
| 461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 462 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | 418 DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 462 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
| 507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 508 if (ContainsKey(downloads_, download_id)) | 464 if (ContainsKey(downloads_, download_id)) |
| 509 downloads_[download_id]->OnDownloadedFileRemoved(); | 465 downloads_[download_id]->OnDownloadedFileRemoved(); |
| 510 } | 466 } |
| 511 | 467 |
| 512 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 468 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
| 513 return browser_context_; | 469 return browser_context_; |
| 514 } | 470 } |
| 515 | 471 |
| 516 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 472 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
| 517 DownloadCreateInfo* info) { | 473 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
| 518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 519 | 475 |
| 520 net::BoundNetLog bound_net_log = | |
| 521 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
| 522 if (!info->download_id.IsValid()) | 476 if (!info->download_id.IsValid()) |
| 523 info->download_id = GetNextId(); | 477 info->download_id = GetNextId(); |
| 524 DownloadItemImpl* download = factory_->CreateActiveItem( | 478 DownloadItemImpl* download = item_factory_->CreateActiveItem( |
| 525 this, *info, | 479 this, *info, |
| 526 scoped_ptr<DownloadRequestHandleInterface>( | 480 scoped_ptr<DownloadRequestHandleInterface>( |
| 527 new DownloadRequestHandle(info->request_handle)).Pass(), | 481 new DownloadRequestHandle(info->request_handle)).Pass(), |
| 528 bound_net_log); | 482 bound_net_log); |
| 529 | 483 |
| 530 DCHECK(!ContainsKey(downloads_, download->GetId())); | 484 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 531 downloads_[download->GetId()] = download; | 485 downloads_[download->GetId()] = download; |
| 532 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 486 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 533 active_downloads_[download->GetId()] = download; | 487 active_downloads_[download->GetId()] = download; |
| 534 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 488 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 535 | 489 |
| 536 return bound_net_log; | 490 return download; |
| 537 } | 491 } |
| 538 | 492 |
| 539 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 493 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
| 540 const FilePath& main_file_path, | 494 const FilePath& main_file_path, |
| 541 const GURL& page_url, | 495 const GURL& page_url, |
| 542 const std::string& mime_type, | 496 const std::string& mime_type, |
| 543 DownloadItem::Observer* observer) { | 497 DownloadItem::Observer* observer) { |
| 544 net::BoundNetLog bound_net_log = | 498 net::BoundNetLog bound_net_log = |
| 545 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 499 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 546 DownloadItemImpl* download = factory_->CreateSavePageItem( | 500 DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
| 547 this, | 501 this, |
| 548 main_file_path, | 502 main_file_path, |
| 549 page_url, | 503 page_url, |
| 550 GetNextId(), | 504 GetNextId(), |
| 551 mime_type, | 505 mime_type, |
| 552 bound_net_log); | 506 bound_net_log); |
| 553 | 507 |
| 554 download->AddObserver(observer); | 508 download->AddObserver(observer); |
| 555 | 509 |
| 556 DCHECK(!ContainsKey(downloads_, download->GetId())); | 510 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 557 downloads_[download->GetId()] = download; | 511 downloads_[download->GetId()] = download; |
| 558 DCHECK(!SavePageData::Get(download)); | 512 DCHECK(!SavePageData::Get(download)); |
| 559 new SavePageData(download); | 513 new SavePageData(download); |
| 560 DCHECK(SavePageData::Get(download)); | 514 DCHECK(SavePageData::Get(download)); |
| 561 | 515 |
| 562 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when | 516 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when |
| 563 // we're comfortable with the user interacting with them. | 517 // we're comfortable with the user interacting with them. |
| 564 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 518 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 565 | 519 |
| 566 // Will notify the observer in the callback. | 520 // Will notify the observer in the callback. |
| 567 if (delegate_) | 521 if (delegate_) |
| 568 delegate_->AddItemToPersistentStore(download); | 522 delegate_->AddItemToPersistentStore(download); |
| 569 | 523 |
| 570 return download; | 524 return download; |
| 571 } | 525 } |
| 572 | 526 |
| 573 void DownloadManagerImpl::UpdateDownload(int32 download_id, | |
| 574 int64 bytes_so_far, | |
| 575 int64 bytes_per_sec, | |
| 576 const std::string& hash_state) { | |
| 577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 578 DownloadMap::iterator it = active_downloads_.find(download_id); | |
| 579 if (it != active_downloads_.end()) { | |
| 580 DownloadItemImpl* download = it->second; | |
| 581 if (download->IsInProgress()) { | |
| 582 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | |
| 583 if (delegate_) | |
| 584 delegate_->UpdateItemInPersistentStore(download); | |
| 585 } | |
| 586 } | |
| 587 } | |
| 588 | |
| 589 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | |
| 590 int64 size, | |
| 591 const std::string& hash) { | |
| 592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 593 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | |
| 594 << " size = " << size; | |
| 595 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 596 | |
| 597 // If it's not in active_downloads_, that means it was cancelled; just | |
| 598 // ignore the notification. | |
| 599 if (active_downloads_.count(download_id) == 0) | |
| 600 return; | |
| 601 | |
| 602 DownloadItemImpl* download = active_downloads_[download_id]; | |
| 603 download->OnAllDataSaved(size, hash); | |
| 604 MaybeCompleteDownload(download); | |
| 605 } | |
| 606 | |
| 607 void DownloadManagerImpl::AssertStateConsistent( | 527 void DownloadManagerImpl::AssertStateConsistent( |
| 608 DownloadItemImpl* download) const { | 528 DownloadItemImpl* download) const { |
| 609 CHECK(ContainsKey(downloads_, download->GetId())); | 529 CHECK(ContainsKey(downloads_, download->GetId())); |
| 610 | 530 |
| 611 int64 state = download->GetState(); | 531 int64 state = download->GetState(); |
| 612 base::debug::Alias(&state); | 532 base::debug::Alias(&state); |
| 613 if (ContainsKey(active_downloads_, download->GetId())) { | 533 if (ContainsKey(active_downloads_, download->GetId())) { |
| 614 if (download->IsPersisted()) | 534 if (download->IsPersisted()) |
| 615 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 535 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 616 if (DownloadItem::IN_PROGRESS != download->GetState()) | 536 if (DownloadItem::IN_PROGRESS != download->GetState()) |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 718 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 638 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
| 719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 639 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 720 | 640 |
| 721 VLOG(20) << __FUNCTION__ << "()" | 641 VLOG(20) << __FUNCTION__ << "()" |
| 722 << " download = " << download->DebugString(true); | 642 << " download = " << download->DebugString(true); |
| 723 | 643 |
| 724 RemoveFromActiveList(download); | 644 RemoveFromActiveList(download); |
| 725 // This function is called from the DownloadItem, so DI state | 645 // This function is called from the DownloadItem, so DI state |
| 726 // should already have been updated. | 646 // should already have been updated. |
| 727 AssertStateConsistent(download); | 647 AssertStateConsistent(download); |
| 728 | |
| 729 DCHECK(file_manager_); | |
| 730 download->OffThreadCancel(); | |
| 731 } | |
| 732 | |
| 733 void DownloadManagerImpl::OnDownloadInterrupted( | |
| 734 int32 download_id, | |
| 735 content::DownloadInterruptReason reason) { | |
| 736 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 737 | |
| 738 if (!ContainsKey(active_downloads_, download_id)) | |
| 739 return; | |
| 740 active_downloads_[download_id]->Interrupt(reason); | |
| 741 } | 648 } |
| 742 | 649 |
| 743 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 650 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
| 744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 745 DCHECK(download); | 652 DCHECK(download); |
| 746 | 653 |
| 747 // Clean up will happen when the history system create callback runs if we | 654 // Clean up will happen when the history system create callback runs if we |
| 748 // don't have a valid db_handle yet. | 655 // don't have a valid db_handle yet. |
| 749 if (download->IsPersisted()) { | 656 if (download->IsPersisted()) { |
| 750 active_downloads_.erase(download->GetId()); | 657 active_downloads_.erase(download->GetId()); |
| 751 if (delegate_) | 658 if (delegate_) |
| 752 delegate_->UpdateItemInPersistentStore(download); | 659 delegate_->UpdateItemInPersistentStore(download); |
| 753 } | 660 } |
| 754 } | 661 } |
| 755 | 662 |
| 756 bool DownloadManagerImpl::GenerateFileHash() { | 663 bool DownloadManagerImpl::GenerateFileHash() { |
| 757 return delegate_ && delegate_->GenerateFileHash(); | 664 return delegate_ && delegate_->GenerateFileHash(); |
| 758 } | 665 } |
| 759 | 666 |
| 667 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 668 scoped_ptr<content::DownloadFileFactory> file_factory) { |
| 669 file_factory_ = file_factory.Pass(); |
| 670 } |
| 671 |
| 672 content::DownloadFileFactory* |
| 673 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
| 674 return file_factory_.get(); |
| 675 } |
| 676 |
| 760 int DownloadManagerImpl::RemoveDownloadItems( | 677 int DownloadManagerImpl::RemoveDownloadItems( |
| 761 const DownloadItemImplVector& pending_deletes) { | 678 const DownloadItemImplVector& pending_deletes) { |
| 762 if (pending_deletes.empty()) | 679 if (pending_deletes.empty()) |
| 763 return 0; | 680 return 0; |
| 764 | 681 |
| 765 // Delete from internal maps. | 682 // Delete from internal maps. |
| 766 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 683 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
| 767 it != pending_deletes.end(); | 684 it != pending_deletes.end(); |
| 768 ++it) { | 685 ++it) { |
| 769 DownloadItemImpl* download = *it; | 686 DownloadItemImpl* download = *it; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 774 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
| 858 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 775 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
| 859 std::vector<DownloadPersistentStoreInfo>* entries) { | 776 std::vector<DownloadPersistentStoreInfo>* entries) { |
| 860 history_size_ = entries->size(); | 777 history_size_ = entries->size(); |
| 861 for (size_t i = 0; i < entries->size(); ++i) { | 778 for (size_t i = 0; i < entries->size(); ++i) { |
| 862 int64 db_handle = entries->at(i).db_handle; | 779 int64 db_handle = entries->at(i).db_handle; |
| 863 base::debug::Alias(&db_handle); | 780 base::debug::Alias(&db_handle); |
| 864 | 781 |
| 865 net::BoundNetLog bound_net_log = | 782 net::BoundNetLog bound_net_log = |
| 866 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 783 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 867 DownloadItemImpl* download = factory_->CreatePersistedItem( | 784 DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
| 868 this, GetNextId(), entries->at(i), bound_net_log); | 785 this, GetNextId(), entries->at(i), bound_net_log); |
| 869 DCHECK(!ContainsKey(downloads_, download->GetId())); | 786 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 870 downloads_[download->GetId()] = download; | 787 downloads_[download->GetId()] = download; |
| 871 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 788 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 872 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 789 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 873 << " download = " << download->DebugString(true); | 790 << " download = " << download->DebugString(true); |
| 874 } | 791 } |
| 875 NotifyModelChanged(); | 792 NotifyModelChanged(); |
| 876 CheckForHistoryFilesRemoval(); | 793 CheckForHistoryFilesRemoval(); |
| 877 } | 794 } |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1072 ++num_unopened; | 989 ++num_unopened; |
| 1073 } | 990 } |
| 1074 download_stats::RecordOpensOutstanding(num_unopened); | 991 download_stats::RecordOpensOutstanding(num_unopened); |
| 1075 } | 992 } |
| 1076 | 993 |
| 1077 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 994 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
| 1078 DownloadItemImpl* download) { | 995 DownloadItemImpl* download) { |
| 1079 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 996 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1080 // download->GetFullPath() is only expected to be meaningful after this | 997 // download->GetFullPath() is only expected to be meaningful after this |
| 1081 // callback is received. Therefore we can now add the download to a persistent | 998 // callback is received. Therefore we can now add the download to a persistent |
| 1082 // store. If the rename failed, we receive an OnDownloadInterrupted() call | 999 // store. If the rename failed, we processed an interrupt |
| 1083 // before we receive the DownloadRenamedToIntermediateName() call. | 1000 // before we receive the DownloadRenamedToIntermediateName() call. |
| 1084 if (delegate_) { | 1001 if (delegate_) { |
| 1085 delegate_->AddItemToPersistentStore(download); | 1002 delegate_->AddItemToPersistentStore(download); |
| 1086 } else { | 1003 } else { |
| 1087 OnItemAddedToPersistentStore(download->GetId(), | 1004 OnItemAddedToPersistentStore(download->GetId(), |
| 1088 DownloadItem::kUninitializedHandle); | 1005 DownloadItem::kUninitializedHandle); |
| 1089 } | 1006 } |
| 1090 } | 1007 } |
| 1091 | 1008 |
| 1092 void DownloadManagerImpl::DownloadRenamedToFinalName( | 1009 void DownloadManagerImpl::DownloadRenamedToFinalName( |
| 1093 DownloadItemImpl* download) { | 1010 DownloadItemImpl* download) { |
| 1094 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1011 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1095 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 1012 // If the rename failed, we processed an interrupt before we get here. |
| 1096 // receive the DownloadRenamedToFinalName() call. | |
| 1097 if (delegate_) { | 1013 if (delegate_) { |
| 1098 delegate_->UpdatePathForItemInPersistentStore( | 1014 delegate_->UpdatePathForItemInPersistentStore( |
| 1099 download, download->GetFullPath()); | 1015 download, download->GetFullPath()); |
| 1100 } | 1016 } |
| 1101 } | 1017 } |
| OLD | NEW |