Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 1418663010: Adding WebContent-free Download (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removing Core. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/i18n/case_conversion.h" 12 #include "base/i18n/case_conversion.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
15 #include "base/stl_util.h" 16 #include "base/stl_util.h"
16 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
17 #include "base/strings/sys_string_conversions.h" 18 #include "base/strings/sys_string_conversions.h"
18 #include "base/supports_user_data.h" 19 #include "base/supports_user_data.h"
19 #include "base/synchronization/lock.h" 20 #include "base/synchronization/lock.h"
20 #include "build/build_config.h" 21 #include "build/build_config.h"
21 #include "content/browser/byte_stream.h" 22 #include "content/browser/byte_stream.h"
22 #include "content/browser/download/download_create_info.h" 23 #include "content/browser/download/download_create_info.h"
23 #include "content/browser/download/download_file_factory.h" 24 #include "content/browser/download/download_file_factory.h"
24 #include "content/browser/download/download_item_factory.h" 25 #include "content/browser/download/download_item_factory.h"
25 #include "content/browser/download/download_item_impl.h" 26 #include "content/browser/download/download_item_impl.h"
26 #include "content/browser/download/download_stats.h" 27 #include "content/browser/download/download_stats.h"
27 #include "content/browser/loader/resource_dispatcher_host_impl.h" 28 #include "content/browser/loader/resource_dispatcher_host_impl.h"
28 #include "content/browser/renderer_host/render_view_host_impl.h" 29 #include "content/browser/renderer_host/render_view_host_impl.h"
29 #include "content/browser/web_contents/web_contents_impl.h" 30 #include "content/browser/web_contents/web_contents_impl.h"
30 #include "content/public/browser/browser_context.h" 31 #include "content/public/browser/browser_context.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"
35 #include "content/public/browser/download_url_parameters.h" 35 #include "content/public/browser/download_url_parameters.h"
36 #include "content/public/browser/notification_service.h" 36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/notification_types.h" 37 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/render_process_host.h" 38 #include "content/public/browser/render_process_host.h"
39 #include "content/public/browser/resource_context.h" 39 #include "content/public/browser/resource_context.h"
40 #include "content/public/browser/web_contents_delegate.h" 40 #include "content/public/browser/web_contents_delegate.h"
41 #include "content/public/common/referrer.h" 41 #include "content/public/common/referrer.h"
42 #include "net/base/elements_upload_data_stream.h" 42 #include "net/base/elements_upload_data_stream.h"
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 #include "url/origin.h" 47 #include "url/origin.h"
48 48
49 namespace content { 49 namespace content {
50 namespace { 50 namespace {
51 51
52 void BeginDownload(scoped_ptr<DownloadUrlParameters> params, 52 UrlDownloader* BeginDownload(
asanka 2015/12/08 21:53:56 You can return a scoped_ptr<UrlDownloader, DeleteO
svaldez 2015/12/09 17:20:41 We can't since using PostTask... with a scoped_ptr
asanka 2015/12/10 16:27:55 PostTaskAndReplyWithResult() correctly forwards mo
53 uint32 download_id) { 53 scoped_ptr<DownloadUrlParameters> params,
54 uint32 download_id,
55 base::WeakPtr<DownloadManagerImpl> download_manager) {
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); 56 DCHECK_CURRENTLY_ON(BrowserThread::IO);
55 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and 57 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
56 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so 58 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
57 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. 59 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
58 scoped_ptr<net::URLRequest> request( 60 scoped_ptr<net::URLRequest> request(
59 params->resource_context()->GetRequestContext()->CreateRequest( 61 params->resource_context()->GetRequestContext()->CreateRequest(
60 params->url(), net::DEFAULT_PRIORITY, NULL)); 62 params->url(), net::DEFAULT_PRIORITY, NULL));
61 request->set_method(params->method()); 63 request->set_method(params->method());
62 if (!params->post_body().empty()) { 64 if (!params->post_body().empty()) {
63 const std::string& body = params->post_body(); 65 const std::string& body = params->post_body();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 } 115 }
114 116
115 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); 117 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
116 save_info->file_path = params->file_path(); 118 save_info->file_path = params->file_path();
117 save_info->suggested_name = params->suggested_name(); 119 save_info->suggested_name = params->suggested_name();
118 save_info->offset = params->offset(); 120 save_info->offset = params->offset();
119 save_info->hash_state = params->hash_state(); 121 save_info->hash_state = params->hash_state();
120 save_info->prompt_for_save_location = params->prompt(); 122 save_info->prompt_for_save_location = params->prompt();
121 save_info->file = params->GetFile(); 123 save_info->file = params->GetFile();
122 124
123 ResourceDispatcherHost::Get()->BeginDownload( 125 if (params->render_process_host_id() != -1) {
124 request.Pass(), 126 ResourceDispatcherHost::Get()->BeginDownload(
125 params->referrer(), 127 request.Pass(), params->referrer(), params->content_initiated(),
126 params->content_initiated(), 128 params->resource_context(), params->render_process_host_id(),
127 params->resource_context(), 129 params->render_view_host_routing_id(),
128 params->render_process_host_id(), 130 params->render_frame_host_routing_id(), params->prefer_cache(),
129 params->render_view_host_routing_id(), 131 params->do_not_prompt_for_login(), save_info.Pass(), download_id,
130 params->render_frame_host_routing_id(), 132 params->callback());
131 params->prefer_cache(), 133 return nullptr;
132 params->do_not_prompt_for_login(), 134 }
133 save_info.Pass(), 135 return UrlDownloader::BeginDownload(
134 download_id, 136 download_manager, request.Pass(), params->referrer(), false,
135 params->callback()); 137 params->prefer_cache(), true, save_info.Pass(), download_id,
138 params->callback())
139 .release();
136 } 140 }
137 141
138 class DownloadItemFactoryImpl : public DownloadItemFactory { 142 class DownloadItemFactoryImpl : public DownloadItemFactory {
139 public: 143 public:
140 DownloadItemFactoryImpl() {} 144 DownloadItemFactoryImpl() {}
141 ~DownloadItemFactoryImpl() override {} 145 ~DownloadItemFactoryImpl() override {}
142 146
143 DownloadItemImpl* CreatePersistedItem( 147 DownloadItemImpl* CreatePersistedItem(
144 DownloadItemImplDelegate* delegate, 148 DownloadItemImplDelegate* delegate,
145 uint32 download_id, 149 uint32 download_id,
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 OnSavePackageSuccessfullyFinished(this, download_item)); 509 OnSavePackageSuccessfullyFinished(this, download_item));
506 } 510 }
507 511
508 // Resume a download of a specific URL. We send the request to the 512 // Resume a download of a specific URL. We send the request to the
509 // ResourceDispatcherHost, and let it send us responses like a regular 513 // ResourceDispatcherHost, and let it send us responses like a regular
510 // download. 514 // download.
511 void DownloadManagerImpl::ResumeInterruptedDownload( 515 void DownloadManagerImpl::ResumeInterruptedDownload(
512 scoped_ptr<content::DownloadUrlParameters> params, 516 scoped_ptr<content::DownloadUrlParameters> params,
513 uint32 id) { 517 uint32 id) {
514 RecordDownloadSource(INITIATED_BY_RESUMPTION); 518 RecordDownloadSource(INITIATED_BY_RESUMPTION);
515 BrowserThread::PostTask( 519 BrowserThread::PostTaskAndReplyWithResult(
516 BrowserThread::IO, 520 BrowserThread::IO, FROM_HERE,
517 FROM_HERE, 521 base::Bind(&BeginDownload, base::Passed(&params), id,
518 base::Bind(&BeginDownload, base::Passed(&params), id)); 522 weak_factory_.GetWeakPtr()),
523 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
524 weak_factory_.GetWeakPtr()));
519 } 525 }
520 526
521 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( 527 void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
522 scoped_ptr<DownloadItemFactory> item_factory) { 528 scoped_ptr<DownloadItemFactory> item_factory) {
523 item_factory_ = item_factory.Pass(); 529 item_factory_ = item_factory.Pass();
524 } 530 }
525 531
526 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( 532 void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
527 scoped_ptr<DownloadFileFactory> file_factory) { 533 scoped_ptr<DownloadFileFactory> file_factory) {
528 file_factory_ = file_factory.Pass(); 534 file_factory_ = file_factory.Pass();
529 } 535 }
530 536
531 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { 537 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
532 return file_factory_.get(); 538 return file_factory_.get();
533 } 539 }
534 540
535 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { 541 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
536 if (!download) 542 if (!download)
537 return; 543 return;
538 544
539 uint32 download_id = download->GetId(); 545 uint32 download_id = download->GetId();
540 if (downloads_.erase(download_id) == 0) 546 if (downloads_.erase(download_id) == 0)
541 return; 547 return;
542 delete download; 548 delete download;
543 } 549 }
544 550
551 void DownloadManagerImpl::AddUrlDownloader(UrlDownloader* downloader) {
552 if (downloader) {
553 url_downloaders_.push_back(
554 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(downloader));
555 }
556 }
557
558 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) {
559 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
560 ++ptr) {
561 if (ptr->get() == downloader) {
562 url_downloaders_.erase(ptr);
563 return;
564 }
565 }
566 }
567
545 namespace { 568 namespace {
546 569
547 bool RemoveDownloadBetween(base::Time remove_begin, 570 bool RemoveDownloadBetween(base::Time remove_begin,
548 base::Time remove_end, 571 base::Time remove_end,
549 const DownloadItemImpl* download_item) { 572 const DownloadItemImpl* download_item) {
550 return download_item->GetStartTime() >= remove_begin && 573 return download_item->GetStartTime() >= remove_begin &&
551 (remove_end.is_null() || download_item->GetStartTime() < remove_end); 574 (remove_end.is_null() || download_item->GetStartTime() < remove_end);
552 } 575 }
553 576
554 bool RemoveDownloadByOriginAndTime(const url::Origin& origin, 577 bool RemoveDownloadByOriginAndTime(const url::Origin& origin,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 return num_deleted; 628 return num_deleted;
606 } 629 }
607 630
608 void DownloadManagerImpl::DownloadUrl( 631 void DownloadManagerImpl::DownloadUrl(
609 scoped_ptr<DownloadUrlParameters> params) { 632 scoped_ptr<DownloadUrlParameters> params) {
610 if (params->post_id() >= 0) { 633 if (params->post_id() >= 0) {
611 // Check this here so that the traceback is more useful. 634 // Check this here so that the traceback is more useful.
612 DCHECK(params->prefer_cache()); 635 DCHECK(params->prefer_cache());
613 DCHECK_EQ("POST", params->method()); 636 DCHECK_EQ("POST", params->method());
614 } 637 }
615 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 638 BrowserThread::PostTaskAndReplyWithResult(
616 &BeginDownload, base::Passed(&params), 639 BrowserThread::IO, FROM_HERE,
617 content::DownloadItem::kInvalidId)); 640 base::Bind(&BeginDownload, base::Passed(&params),
641 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
642 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
643 weak_factory_.GetWeakPtr()));
618 } 644 }
619 645
620 void DownloadManagerImpl::AddObserver(Observer* observer) { 646 void DownloadManagerImpl::AddObserver(Observer* observer) {
621 observers_.AddObserver(observer); 647 observers_.AddObserver(observer);
622 } 648 }
623 649
624 void DownloadManagerImpl::RemoveObserver(Observer* observer) { 650 void DownloadManagerImpl::RemoveObserver(Observer* observer) {
625 observers_.RemoveObserver(observer); 651 observers_.RemoveObserver(observer);
626 } 652 }
627 653
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 if (delegate_) 750 if (delegate_)
725 delegate_->OpenDownload(download); 751 delegate_->OpenDownload(download);
726 } 752 }
727 753
728 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { 754 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
729 if (delegate_) 755 if (delegate_)
730 delegate_->ShowDownloadInShell(download); 756 delegate_->ShowDownloadInShell(download);
731 } 757 }
732 758
733 } // namespace content 759 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698