Chromium Code Reviews| Index: components/cronet/android/cronet_url_request_adapter.cc |
| diff --git a/components/cronet/android/cronet_url_request_adapter.cc b/components/cronet/android/cronet_url_request_adapter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ea03addf9061ca1bf94b11f1c1423f903c8983ad |
| --- /dev/null |
| +++ b/components/cronet/android/cronet_url_request_adapter.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cronet_url_request_adapter.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "components/cronet/android/cronet_url_request_context_adapter.h" |
| +#include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/redirect_info.h" |
| +#include "net/url_request/url_request_context.h" |
| + |
| +namespace cronet { |
| + |
| +static const int kReadBufferSize = 32768; |
| + |
| +CronetURLRequestAdapter::CronetURLRequestAdapter( |
| + CronetURLRequestContextAdapter* context, |
| + scoped_ptr<CronetURLRequestAdapterDelegate> delegate, |
| + const GURL& url, |
| + net::RequestPriority priority) |
| + : context_(context), |
|
xunjieli
2014/11/06 17:02:36
a totally optional nit. Use "context_adapter" inst
mef
2014/11/06 22:51:45
Acknowledged.
|
| + delegate_(delegate.Pass()), |
| + initial_url_(url), |
| + initial_priority_(priority), |
| + initial_method_("GET") { |
| +} |
| + |
| +CronetURLRequestAdapter::~CronetURLRequestAdapter() { |
| + DCHECK(IsOnNetworkThread()); |
| +} |
| + |
| +void CronetURLRequestAdapter::AddRequestHeader(const std::string& name, |
| + const std::string& value) { |
| + DCHECK(!IsOnNetworkThread()); |
| + initial_request_headers_.SetHeader(name, value); |
| +} |
| + |
| +net::HttpResponseHeaders* CronetURLRequestAdapter::GetResponseHeaders() const { |
| + DCHECK(IsOnNetworkThread()); |
| + return url_request_->response_headers(); |
| +} |
| + |
| +const std::string& CronetURLRequestAdapter::GetNegotiatedProtocol() const { |
| + DCHECK(IsOnNetworkThread()); |
| + return url_request_->response_info().npn_negotiated_protocol; |
| +} |
| + |
| +bool CronetURLRequestAdapter::GetWasCached() const { |
| + DCHECK(IsOnNetworkThread()); |
| + return url_request_->response_info().was_cached; |
| +} |
| + |
| +int64 CronetURLRequestAdapter::GetTotalReceivedBytes() const { |
| + DCHECK(IsOnNetworkThread()); |
| + return url_request_->GetTotalReceivedBytes(); |
| +} |
| + |
| +void CronetURLRequestAdapter::Start() { |
| + DCHECK(!IsOnNetworkThread()); |
| + context_->GetNetworkTaskRunner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CronetURLRequestAdapter::StartOnNetworkThread, |
| + base::Unretained(this))); |
| +} |
| + |
| +void CronetURLRequestAdapter::FollowDeferredRedirect() { |
| + DCHECK(!IsOnNetworkThread()); |
| + context_->GetNetworkTaskRunner()->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread, |
| + base::Unretained(this))); |
| +} |
| + |
| +void CronetURLRequestAdapter::ReadData() { |
| + DCHECK(!IsOnNetworkThread()); |
| + context_->GetNetworkTaskRunner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CronetURLRequestAdapter::ReadDataOnNetworkThread, |
| + base::Unretained(this))); |
| +} |
| + |
| +void CronetURLRequestAdapter::Destroy() { |
| + DCHECK(!IsOnNetworkThread()); |
| + context_->GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| +// net::URLRequest::Delegate overrides (called on network thread). |
| + |
| +void CronetURLRequestAdapter::OnReceivedRedirect( |
| + net::URLRequest* request, |
| + const net::RedirectInfo& redirect_info, |
| + bool* defer_redirect) { |
| + DCHECK(IsOnNetworkThread()); |
| + DCHECK(request->status().is_success()); |
| + delegate_->OnRedirect(redirect_info.new_url, redirect_info.status_code); |
| + *defer_redirect = true; |
| +} |
| + |
| +void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { |
| + DCHECK(IsOnNetworkThread()); |
| + if (!MaybeReportError(request)) |
| + return; |
| + delegate_->OnResponseStarted(request->GetResponseCode()); |
| +} |
| + |
| +void CronetURLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| + int bytes_read) { |
| + DCHECK(IsOnNetworkThread()); |
| + if (!MaybeReportError(request)) |
| + return; |
| + if (bytes_read != 0) { |
| + delegate_->OnBytesRead( |
| + reinterpret_cast<unsigned char*>(read_buffer_->data()), bytes_read); |
| + } else { |
| + delegate_->OnRequestFinished(); |
| + } |
| +} |
| + |
| +bool CronetURLRequestAdapter::IsOnNetworkThread() const { |
| + return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| +} |
| + |
| +void CronetURLRequestAdapter::StartOnNetworkThread() { |
| + DCHECK(IsOnNetworkThread()); |
| + VLOG(1) << "Starting chromium request: " |
| + << initial_url_.possibly_invalid_spec().c_str() |
| + << " priority: " << RequestPriorityToString(initial_priority_); |
| + url_request_ = context_->GetURLRequestContext()->CreateRequest( |
| + initial_url_, net::DEFAULT_PRIORITY, this, NULL); |
| + url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| + net::LOAD_DO_NOT_SAVE_COOKIES | |
| + net::LOAD_DO_NOT_SEND_COOKIES); |
| + url_request_->set_method(initial_method_); |
| + url_request_->SetExtraRequestHeaders(initial_request_headers_); |
| + url_request_->SetPriority(initial_priority_); |
| + url_request_->Start(); |
| +} |
| + |
| +void CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread() { |
| + DCHECK(IsOnNetworkThread()); |
| + |
| + url_request_->FollowDeferredRedirect(); |
| +} |
| + |
| +void CronetURLRequestAdapter::ReadDataOnNetworkThread() { |
| + DCHECK(IsOnNetworkThread()); |
| + if (!read_buffer_.get()) |
| + read_buffer_ = new net::IOBufferWithSize(kReadBufferSize); |
| + |
| + int bytes_read = 0; |
| + url_request_->Read(read_buffer_.get(), read_buffer_->size(), &bytes_read); |
| + // If IO is pending, wait for the URLRequest to call OnReadCompleted. |
| + if (url_request_->status().is_io_pending()) |
| + return; |
| + |
| + if (bytes_read <= 0) { |
| + OnReadCompleted(url_request_.get(), bytes_read); |
| + } else { |
| + // Else, trigger OnReadCompleted asynchronously to avoid starving the other |
| + // thread in case the URLRequest can provide data synchronously. |
|
mmenke
2014/11/06 17:31:28
I don't think this gets us anything - we always ha
mef
2014/11/06 22:51:45
Done.
|
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CronetURLRequestAdapter::OnReadCompleted, |
| + base::Unretained(this), |
|
mmenke
2014/11/06 17:31:28
This also isn't threadsafe - if we get a cancel me
mef
2014/11/06 22:51:45
Done.
|
| + url_request_.get(), |
| + bytes_read)); |
| + } |
| +} |
| + |
| +bool CronetURLRequestAdapter::MaybeReportError(net::URLRequest* request) const { |
| + DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); |
| + DCHECK_EQ(request, url_request_); |
| + if (url_request_->status().is_success()) |
| + return true; |
| + VLOG(1) << "Error " << url_request_->status().error() |
| + << " on chromium request: " << initial_url_.possibly_invalid_spec(); |
| + delegate_->OnError(url_request_->status().error()); |
| + return false; |
| +} |
| + |
| +} // namespace cronet |