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 21 matching lines...) Expand all Loading... | |
32 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/content_browser_client.h" | 33 #include "content/public/browser/content_browser_client.h" |
34 #include "content/public/browser/download_interrupt_reasons.h" | 34 #include "content/public/browser/download_interrupt_reasons.h" |
35 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
36 #include "content/public/browser/download_url_parameters.h" | 36 #include "content/public/browser/download_url_parameters.h" |
37 #include "content/public/browser/notification_service.h" | 37 #include "content/public/browser/notification_service.h" |
38 #include "content/public/browser/notification_types.h" | 38 #include "content/public/browser/notification_types.h" |
39 #include "content/public/browser/render_process_host.h" | 39 #include "content/public/browser/render_process_host.h" |
40 #include "content/public/browser/resource_context.h" | 40 #include "content/public/browser/resource_context.h" |
41 #include "content/public/browser/web_contents_delegate.h" | 41 #include "content/public/browser/web_contents_delegate.h" |
42 #include "content/public/common/referrer.h" | |
42 #include "net/base/load_flags.h" | 43 #include "net/base/load_flags.h" |
43 #include "net/base/upload_bytes_element_reader.h" | 44 #include "net/base/upload_bytes_element_reader.h" |
44 #include "net/base/upload_data_stream.h" | 45 #include "net/base/upload_data_stream.h" |
45 #include "net/url_request/url_request_context.h" | 46 #include "net/url_request/url_request_context.h" |
46 #include "webkit/glue/webkit_glue.h" | 47 #include "webkit/glue/webkit_glue.h" |
47 | 48 |
48 namespace content { | 49 namespace content { |
49 namespace { | 50 namespace { |
50 | 51 |
51 void BeginDownload(scoped_ptr<DownloadUrlParameters> params) { | 52 void BeginDownload(scoped_ptr<DownloadUrlParameters> params, |
53 DownloadId download_id) { | |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
53 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and | 55 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
54 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so | 56 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
55 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. | 57 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
56 scoped_ptr<net::URLRequest> request( | 58 scoped_ptr<net::URLRequest> request( |
57 params->resource_context()->GetRequestContext()->CreateRequest( | 59 params->resource_context()->GetRequestContext()->CreateRequest( |
58 params->url(), NULL)); | 60 params->url(), NULL)); |
59 request->set_referrer(params->referrer().url.spec()); | 61 request->set_referrer(params->referrer().url.spec()); |
60 webkit_glue::ConfigureURLRequestForReferrerPolicy( | 62 webkit_glue::ConfigureURLRequestForReferrerPolicy( |
61 request.get(), params->referrer().policy); | 63 request.get(), params->referrer().policy); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 save_info->file_stream = params->GetFileStream(); | 98 save_info->file_stream = params->GetFileStream(); |
97 | 99 |
98 params->resource_dispatcher_host()->BeginDownload( | 100 params->resource_dispatcher_host()->BeginDownload( |
99 request.Pass(), | 101 request.Pass(), |
100 params->content_initiated(), | 102 params->content_initiated(), |
101 params->resource_context(), | 103 params->resource_context(), |
102 params->render_process_host_id(), | 104 params->render_process_host_id(), |
103 params->render_view_host_routing_id(), | 105 params->render_view_host_routing_id(), |
104 params->prefer_cache(), | 106 params->prefer_cache(), |
105 save_info.Pass(), | 107 save_info.Pass(), |
108 params->last_modified(), | |
109 params->etag(), | |
110 download_id, | |
106 params->callback()); | 111 params->callback()); |
107 } | 112 } |
108 | 113 |
109 class MapValueIteratorAdapter { | 114 class MapValueIteratorAdapter { |
110 public: | 115 public: |
111 explicit MapValueIteratorAdapter( | 116 explicit MapValueIteratorAdapter( |
112 base::hash_map<int64, DownloadItem*>::const_iterator iter) | 117 base::hash_map<int64, DownloadItem*>::const_iterator iter) |
113 : iter_(iter) { | 118 : iter_(iter) { |
114 } | 119 } |
115 ~MapValueIteratorAdapter() {} | 120 ~MapValueIteratorAdapter() {} |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 received_bytes, | 171 received_bytes, |
167 total_bytes, | 172 total_bytes, |
168 state, | 173 state, |
169 opened, | 174 opened, |
170 bound_net_log); | 175 bound_net_log); |
171 } | 176 } |
172 | 177 |
173 virtual DownloadItemImpl* CreateActiveItem( | 178 virtual DownloadItemImpl* CreateActiveItem( |
174 DownloadItemImplDelegate* delegate, | 179 DownloadItemImplDelegate* delegate, |
175 const DownloadCreateInfo& info, | 180 const DownloadCreateInfo& info, |
176 scoped_ptr<DownloadRequestHandleInterface> request_handle, | |
177 const net::BoundNetLog& bound_net_log) OVERRIDE { | 181 const net::BoundNetLog& bound_net_log) OVERRIDE { |
178 return new DownloadItemImpl(delegate, info, request_handle.Pass(), | 182 return new DownloadItemImpl(delegate, info, bound_net_log); |
179 bound_net_log); | |
180 } | 183 } |
181 | 184 |
182 virtual DownloadItemImpl* CreateSavePageItem( | 185 virtual DownloadItemImpl* CreateSavePageItem( |
183 DownloadItemImplDelegate* delegate, | 186 DownloadItemImplDelegate* delegate, |
184 const FilePath& path, | 187 const FilePath& path, |
185 const GURL& url, | 188 const GURL& url, |
186 DownloadId download_id, | 189 DownloadId download_id, |
187 const std::string& mime_type, | 190 const std::string& mime_type, |
188 const net::BoundNetLog& bound_net_log) OVERRIDE { | 191 const net::BoundNetLog& bound_net_log) OVERRIDE { |
189 return new DownloadItemImpl(delegate, path, url, download_id, | 192 return new DownloadItemImpl(delegate, path, url, download_id, |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 browser_context_ = browser_context; | 335 browser_context_ = browser_context; |
333 | 336 |
334 return true; | 337 return true; |
335 } | 338 } |
336 | 339 |
337 DownloadItem* DownloadManagerImpl::StartDownload( | 340 DownloadItem* DownloadManagerImpl::StartDownload( |
338 scoped_ptr<DownloadCreateInfo> info, | 341 scoped_ptr<DownloadCreateInfo> info, |
339 scoped_ptr<ByteStreamReader> stream) { | 342 scoped_ptr<ByteStreamReader> stream) { |
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
341 | 344 |
342 net::BoundNetLog bound_net_log = | |
343 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
344 | |
345 FilePath default_download_directory; | 345 FilePath default_download_directory; |
346 if (delegate_) { | 346 if (delegate_) { |
347 FilePath website_save_directory; // Unused | 347 FilePath website_save_directory; // Unused |
348 bool skip_dir_check = false; // Unused | 348 bool skip_dir_check = false; // Unused |
349 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 349 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
350 &default_download_directory, &skip_dir_check); | 350 &default_download_directory, &skip_dir_check); |
351 } | 351 } |
352 | 352 |
353 // We create the DownloadItem before the DownloadFile because the | 353 // We create the DownloadItem before the DownloadFile because the |
354 // DownloadItem already needs to handle a state in which there is | 354 // DownloadItem already needs to handle a state in which there is |
355 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) | 355 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
356 DownloadItemImpl* download = | 356 DownloadItemImpl* download = GetOrCreateDownloadItem(info.get()); |
357 CreateDownloadItem(info.get(), bound_net_log); | |
358 scoped_ptr<DownloadFile> download_file( | 357 scoped_ptr<DownloadFile> download_file( |
359 file_factory_->CreateFile( | 358 file_factory_->CreateFile( |
360 info->save_info.Pass(), default_download_directory, | 359 info->save_info.Pass(), default_download_directory, |
361 info->url(), info->referrer_url, | 360 info->url(), info->referrer_url, |
362 delegate_->GenerateFileHash(), | 361 delegate_->GenerateFileHash(), |
363 stream.Pass(), bound_net_log, | 362 stream.Pass(), download->GetBoundNetLog(), |
364 download->DestinationObserverAsWeakPtr())); | 363 download->DestinationObserverAsWeakPtr())); |
365 download->Start(download_file.Pass()); | 364 scoped_ptr<DownloadRequestHandleInterface> req_handle( |
365 new DownloadRequestHandle(info->request_handle)); | |
366 download->Start(download_file.Pass(), req_handle.Pass()); | |
366 | 367 |
367 // Delay notification until after Start() so that download_file is bound | 368 // Delay notification until after Start() so that download_file is bound |
368 // to download and all the usual setters (e.g. Cancel) work. | 369 // to download and all the usual setters (e.g. Cancel) work. |
369 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 370 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
370 | 371 |
371 return download; | 372 return download; |
372 } | 373 } |
373 | 374 |
374 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 375 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 376 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 408 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
408 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
409 if (ContainsKey(downloads_, download_id)) | 410 if (ContainsKey(downloads_, download_id)) |
410 downloads_[download_id]->OnDownloadedFileRemoved(); | 411 downloads_[download_id]->OnDownloadedFileRemoved(); |
411 } | 412 } |
412 | 413 |
413 BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 414 BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
414 return browser_context_; | 415 return browser_context_; |
415 } | 416 } |
416 | 417 |
417 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( | 418 DownloadItemImpl* DownloadManagerImpl::GetOrCreateDownloadItem( |
418 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { | 419 DownloadCreateInfo* info) { |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
420 | 421 |
421 if (!info->download_id.IsValid()) | 422 if (!info->download_id.IsValid()) |
asanka
2012/12/28 22:01:42
Under what circumstances would download_id.IsValid
Randy Smith (Not in Mondays)
2012/12/29 17:16:16
Possibly I shouldn't allow for the possibility in
| |
422 info->download_id = GetNextId(); | 423 info->download_id = GetNextId(); |
423 DownloadItemImpl* download = item_factory_->CreateActiveItem( | |
424 this, *info, | |
425 scoped_ptr<DownloadRequestHandleInterface>( | |
426 new DownloadRequestHandle(info->request_handle)).Pass(), | |
427 bound_net_log); | |
428 | 424 |
429 DCHECK(!ContainsKey(downloads_, download->GetId())); | 425 DownloadItemImpl* download = NULL; |
430 downloads_[download->GetId()] = download; | 426 if (ContainsKey(downloads_, info->download_id.local())) { |
427 // Resuming an existing download. | |
428 download = downloads_[info->download_id.local()]; | |
429 DCHECK(download->IsInterrupted()); | |
430 } else { | |
431 // New download | |
432 net::BoundNetLog bound_net_log = | |
433 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
434 download = item_factory_->CreateActiveItem(this, *info, bound_net_log); | |
435 downloads_[download->GetId()] = download; | |
436 } | |
437 | |
431 return download; | 438 return download; |
432 } | 439 } |
433 | 440 |
434 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 441 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
435 const FilePath& main_file_path, | 442 const FilePath& main_file_path, |
436 const GURL& page_url, | 443 const GURL& page_url, |
437 const std::string& mime_type, | 444 const std::string& mime_type, |
438 DownloadItem::Observer* observer) { | 445 DownloadItem::Observer* observer) { |
439 net::BoundNetLog bound_net_log = | 446 net::BoundNetLog bound_net_log = |
440 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 447 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
(...skipping 17 matching lines...) Expand all Loading... | |
458 return download_item; | 465 return download_item; |
459 } | 466 } |
460 | 467 |
461 void DownloadManagerImpl::CancelDownload(int32 download_id) { | 468 void DownloadManagerImpl::CancelDownload(int32 download_id) { |
462 DownloadItem* download = GetDownload(download_id); | 469 DownloadItem* download = GetDownload(download_id); |
463 if (!download || !download->IsInProgress()) | 470 if (!download || !download->IsInProgress()) |
464 return; | 471 return; |
465 download->Cancel(true); | 472 download->Cancel(true); |
466 } | 473 } |
467 | 474 |
475 // Resume a download of a specific URL. We send the request to the | |
476 // ResourceDispatcherHost, and let it send us responses like a regular | |
477 // download. | |
478 void DownloadManagerImpl::ResumeInterruptedDownload( | |
479 scoped_ptr<content::DownloadUrlParameters> params, | |
480 content::DownloadId id) { | |
481 BrowserThread::PostTask( | |
482 BrowserThread::IO, | |
483 FROM_HERE, | |
484 base::Bind(&BeginDownload, base::Passed(params.Pass()), id)); | |
485 } | |
486 | |
468 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( | 487 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
469 scoped_ptr<DownloadItemFactory> item_factory) { | 488 scoped_ptr<DownloadItemFactory> item_factory) { |
470 item_factory_ = item_factory.Pass(); | 489 item_factory_ = item_factory.Pass(); |
471 } | 490 } |
472 | 491 |
473 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( | 492 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
474 scoped_ptr<DownloadFileFactory> file_factory) { | 493 scoped_ptr<DownloadFileFactory> file_factory) { |
475 file_factory_ = file_factory.Pass(); | 494 file_factory_ = file_factory.Pass(); |
476 } | 495 } |
477 | 496 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 } | 556 } |
538 | 557 |
539 void DownloadManagerImpl::DownloadUrl( | 558 void DownloadManagerImpl::DownloadUrl( |
540 scoped_ptr<DownloadUrlParameters> params) { | 559 scoped_ptr<DownloadUrlParameters> params) { |
541 if (params->post_id() >= 0) { | 560 if (params->post_id() >= 0) { |
542 // Check this here so that the traceback is more useful. | 561 // Check this here so that the traceback is more useful. |
543 DCHECK(params->prefer_cache()); | 562 DCHECK(params->prefer_cache()); |
544 DCHECK(params->method() == "POST"); | 563 DCHECK(params->method() == "POST"); |
545 } | 564 } |
546 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 565 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
547 &BeginDownload, base::Passed(¶ms))); | 566 &BeginDownload, base::Passed(¶ms), DownloadId())); |
548 } | 567 } |
549 | 568 |
550 void DownloadManagerImpl::AddObserver(Observer* observer) { | 569 void DownloadManagerImpl::AddObserver(Observer* observer) { |
551 observers_.AddObserver(observer); | 570 observers_.AddObserver(observer); |
552 } | 571 } |
553 | 572 |
554 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 573 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
555 observers_.RemoveObserver(observer); | 574 observers_.RemoveObserver(observer); |
556 } | 575 } |
557 | 576 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 it != downloads_.end(); ++it) { | 646 it != downloads_.end(); ++it) { |
628 DownloadItemImpl* item = it->second; | 647 DownloadItemImpl* item = it->second; |
629 if (item->IsComplete() && | 648 if (item->IsComplete() && |
630 !item->GetOpened()) | 649 !item->GetOpened()) |
631 ++num_unopened; | 650 ++num_unopened; |
632 } | 651 } |
633 RecordOpensOutstanding(num_unopened); | 652 RecordOpensOutstanding(num_unopened); |
634 } | 653 } |
635 | 654 |
636 } // namespace content | 655 } // namespace content |
OLD | NEW |