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

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: Rebase. 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"
(...skipping 19 matching lines...) Expand all
43 #include "net/base/load_flags.h" 44 #include "net/base/load_flags.h"
44 #include "net/base/request_priority.h" 45 #include "net/base/request_priority.h"
45 #include "net/base/upload_bytes_element_reader.h" 46 #include "net/base/upload_bytes_element_reader.h"
46 #include "net/url_request/url_request_context.h" 47 #include "net/url_request/url_request_context.h"
47 #include "url/origin.h" 48 #include "url/origin.h"
48 49
49 namespace content { 50 namespace content {
50 namespace { 51 namespace {
51 52
52 void BeginDownload(scoped_ptr<DownloadUrlParameters> params, 53 void BeginDownload(scoped_ptr<DownloadUrlParameters> params,
53 uint32 download_id) { 54 uint32 download_id,
55 base::WeakPtr<DownloadManagerImpl> download_manager,
56 DownloadManagerImpl::Core* core) {
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); 57 DCHECK_CURRENTLY_ON(BrowserThread::IO);
55 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and 58 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
56 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so 59 // 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. 60 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
58 scoped_ptr<net::URLRequest> request( 61 scoped_ptr<net::URLRequest> request(
59 params->resource_context()->GetRequestContext()->CreateRequest( 62 params->resource_context()->GetRequestContext()->CreateRequest(
60 params->url(), net::DEFAULT_PRIORITY, NULL)); 63 params->url(), net::DEFAULT_PRIORITY, NULL));
61 request->set_method(params->method()); 64 request->set_method(params->method());
62 if (!params->post_body().empty()) { 65 if (!params->post_body().empty()) {
63 const std::string& body = params->post_body(); 66 const std::string& body = params->post_body();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 } 116 }
114 117
115 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); 118 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
116 save_info->file_path = params->file_path(); 119 save_info->file_path = params->file_path();
117 save_info->suggested_name = params->suggested_name(); 120 save_info->suggested_name = params->suggested_name();
118 save_info->offset = params->offset(); 121 save_info->offset = params->offset();
119 save_info->hash_state = params->hash_state(); 122 save_info->hash_state = params->hash_state();
120 save_info->prompt_for_save_location = params->prompt(); 123 save_info->prompt_for_save_location = params->prompt();
121 save_info->file = params->GetFile(); 124 save_info->file = params->GetFile();
122 125
123 ResourceDispatcherHost::Get()->BeginDownload( 126 if (params->render_process_host_id() != -1) {
124 request.Pass(), 127 ResourceDispatcherHost::Get()->BeginDownload(
125 params->referrer(), 128 request.Pass(), params->referrer(), params->content_initiated(),
126 params->content_initiated(), 129 params->resource_context(), params->render_process_host_id(),
127 params->resource_context(), 130 params->render_view_host_routing_id(),
128 params->render_process_host_id(), 131 params->render_frame_host_routing_id(), params->prefer_cache(),
129 params->render_view_host_routing_id(), 132 params->do_not_prompt_for_login(), save_info.Pass(), download_id,
130 params->render_frame_host_routing_id(), 133 params->callback());
131 params->prefer_cache(), 134 } else {
132 params->do_not_prompt_for_login(), 135 core->AddUrlDownloader(UrlDownloader::BeginDownload(
133 save_info.Pass(), 136 download_manager, request.Pass(), params->referrer(), false,
134 download_id, 137 params->prefer_cache(), true, save_info.Pass(), download_id,
135 params->callback()); 138 params->callback()));
139 }
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 scoped_ptr<DownloadRequestHandleInterface> request_handle, 203 scoped_ptr<DownloadRequestHandleInterface> request_handle,
200 const net::BoundNetLog& bound_net_log) override { 204 const net::BoundNetLog& bound_net_log) override {
201 return new DownloadItemImpl(delegate, download_id, path, url, 205 return new DownloadItemImpl(delegate, download_id, path, url,
202 mime_type, request_handle.Pass(), 206 mime_type, request_handle.Pass(),
203 bound_net_log); 207 bound_net_log);
204 } 208 }
205 }; 209 };
206 210
207 } // namespace 211 } // namespace
208 212
213 DownloadManagerImpl::Core::Core() {}
214
215 DownloadManagerImpl::Core::~Core() {
216 DCHECK(url_downloaders_.size() == 0);
217 }
218
219 void DownloadManagerImpl::Core::AddUrlDownloader(
220 scoped_ptr<UrlDownloader> downloader) {
221 url_downloaders_.push_back(downloader.Pass());
222 }
223
224 void DownloadManagerImpl::Core::Shutdown() {
225 url_downloaders_.clear();
226 }
227
209 DownloadManagerImpl::DownloadManagerImpl( 228 DownloadManagerImpl::DownloadManagerImpl(
210 net::NetLog* net_log, 229 net::NetLog* net_log,
211 BrowserContext* browser_context) 230 BrowserContext* browser_context)
212 : item_factory_(new DownloadItemFactoryImpl()), 231 : item_factory_(new DownloadItemFactoryImpl()),
213 file_factory_(new DownloadFileFactory()), 232 file_factory_(new DownloadFileFactory()),
214 history_size_(0), 233 history_size_(0),
215 shutdown_needed_(true), 234 shutdown_needed_(true),
216 browser_context_(browser_context), 235 browser_context_(browser_context),
217 delegate_(NULL), 236 delegate_(NULL),
218 net_log_(net_log), 237 net_log_(net_log),
219 weak_factory_(this) { 238 weak_factory_(this) {
220 DCHECK(browser_context); 239 DCHECK(browser_context);
240 core_ = new Core;
asanka 2015/12/08 16:58:32 Move this to member initialization.
svaldez 2015/12/08 20:39:26 Removed Core.
221 } 241 }
222 242
223 DownloadManagerImpl::~DownloadManagerImpl() { 243 DownloadManagerImpl::~DownloadManagerImpl() {
224 DCHECK(!shutdown_needed_); 244 DCHECK(!shutdown_needed_);
225 } 245 }
226 246
227 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( 247 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
228 uint32 id, const DownloadCreateInfo& info) { 248 uint32 id, const DownloadCreateInfo& info) {
229 DCHECK_CURRENTLY_ON(BrowserThread::UI); 249 DCHECK_CURRENTLY_ON(BrowserThread::UI);
230 DCHECK(!ContainsKey(downloads_, id)); 250 DCHECK(!ContainsKey(downloads_, id));
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 download->Cancel(false); 342 download->Cancel(false);
323 } 343 }
324 STLDeleteValues(&downloads_); 344 STLDeleteValues(&downloads_);
325 345
326 // We'll have nothing more to report to the observers after this point. 346 // We'll have nothing more to report to the observers after this point.
327 observers_.Clear(); 347 observers_.Clear();
328 348
329 if (delegate_) 349 if (delegate_)
330 delegate_->Shutdown(); 350 delegate_->Shutdown();
331 delegate_ = NULL; 351 delegate_ = NULL;
352
353 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
354 base::Bind(&Core::Shutdown,
355 base::Unretained(core_)));
332 } 356 }
333 357
334 void DownloadManagerImpl::StartDownload( 358 void DownloadManagerImpl::StartDownload(
335 scoped_ptr<DownloadCreateInfo> info, 359 scoped_ptr<DownloadCreateInfo> info,
336 scoped_ptr<ByteStreamReader> stream, 360 scoped_ptr<ByteStreamReader> stream,
337 const DownloadUrlParameters::OnStartedCallback& on_started) { 361 const DownloadUrlParameters::OnStartedCallback& on_started) {
338 DCHECK_CURRENTLY_ON(BrowserThread::UI); 362 DCHECK_CURRENTLY_ON(BrowserThread::UI);
339 DCHECK(info); 363 DCHECK(info);
340 uint32 download_id = info->download_id; 364 uint32 download_id = info->download_id;
341 const bool new_download = (download_id == content::DownloadItem::kInvalidId); 365 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 OnSavePackageSuccessfullyFinished(this, download_item)); 529 OnSavePackageSuccessfullyFinished(this, download_item));
506 } 530 }
507 531
508 // Resume a download of a specific URL. We send the request to the 532 // Resume a download of a specific URL. We send the request to the
509 // ResourceDispatcherHost, and let it send us responses like a regular 533 // ResourceDispatcherHost, and let it send us responses like a regular
510 // download. 534 // download.
511 void DownloadManagerImpl::ResumeInterruptedDownload( 535 void DownloadManagerImpl::ResumeInterruptedDownload(
512 scoped_ptr<content::DownloadUrlParameters> params, 536 scoped_ptr<content::DownloadUrlParameters> params,
513 uint32 id) { 537 uint32 id) {
514 RecordDownloadSource(INITIATED_BY_RESUMPTION); 538 RecordDownloadSource(INITIATED_BY_RESUMPTION);
515 BrowserThread::PostTask( 539 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
516 BrowserThread::IO, 540 base::Bind(&BeginDownload, base::Passed(&params), id,
517 FROM_HERE, 541 weak_factory_.GetWeakPtr(),
518 base::Bind(&BeginDownload, base::Passed(&params), id)); 542 core_));
519 } 543 }
520 544
521 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( 545 void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
522 scoped_ptr<DownloadItemFactory> item_factory) { 546 scoped_ptr<DownloadItemFactory> item_factory) {
523 item_factory_ = item_factory.Pass(); 547 item_factory_ = item_factory.Pass();
524 } 548 }
525 549
526 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( 550 void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
527 scoped_ptr<DownloadFileFactory> file_factory) { 551 scoped_ptr<DownloadFileFactory> file_factory) {
528 file_factory_ = file_factory.Pass(); 552 file_factory_ = file_factory.Pass();
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 return num_deleted; 629 return num_deleted;
606 } 630 }
607 631
608 void DownloadManagerImpl::DownloadUrl( 632 void DownloadManagerImpl::DownloadUrl(
609 scoped_ptr<DownloadUrlParameters> params) { 633 scoped_ptr<DownloadUrlParameters> params) {
610 if (params->post_id() >= 0) { 634 if (params->post_id() >= 0) {
611 // Check this here so that the traceback is more useful. 635 // Check this here so that the traceback is more useful.
612 DCHECK(params->prefer_cache()); 636 DCHECK(params->prefer_cache());
613 DCHECK_EQ("POST", params->method()); 637 DCHECK_EQ("POST", params->method());
614 } 638 }
615 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 639 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
616 &BeginDownload, base::Passed(&params), 640 base::Bind(&BeginDownload, base::Passed(&params),
617 content::DownloadItem::kInvalidId)); 641 content::DownloadItem::kInvalidId,
642 weak_factory_.GetWeakPtr(),
643 core_));
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