Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cronet_url_request_adapter.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
| 11 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | |
| 12 #include "net/base/io_buffer.h" | |
| 13 #include "net/base/load_flags.h" | |
| 14 #include "net/http/http_status_code.h" | |
| 15 #include "net/url_request/redirect_info.h" | |
| 16 #include "net/url_request/url_request_context.h" | |
| 17 | |
| 18 namespace cronet { | |
| 19 | |
| 20 static const int kReadBufferSize = 32768; | |
| 21 | |
| 22 CronetURLRequestAdapter::CronetURLRequestAdapter( | |
| 23 CronetURLRequestContextAdapter* context, | |
| 24 scoped_ptr<CronetURLRequestAdapterDelegate> delegate, | |
| 25 const GURL& url, | |
| 26 net::RequestPriority priority) | |
| 27 : context_(context), | |
| 28 delegate_(delegate.Pass()), | |
| 29 initial_url_(url), | |
| 30 initial_priority_(priority), | |
| 31 initial_method_("GET"), | |
| 32 read_buffer_(new net::IOBufferWithSize(kReadBufferSize)) { | |
|
mmenke
2014/11/03 20:21:16
Suggest only initializing this before the first re
mef
2014/11/03 21:23:38
Done.
| |
| 33 | |
|
mmenke
2014/11/03 20:21:16
Remove blank line.
mef
2014/11/03 21:23:38
Done.
| |
| 34 } | |
| 35 | |
| 36 CronetURLRequestAdapter::~CronetURLRequestAdapter() { | |
| 37 } | |
| 38 | |
| 39 void CronetURLRequestAdapter::AddRequestHeader(const std::string& name, | |
| 40 const std::string& value) { | |
| 41 initial_request_headers_.SetHeader(name, value); | |
| 42 } | |
| 43 | |
| 44 net::HttpResponseHeaders* CronetURLRequestAdapter::GetResponseHeaders() const { | |
| 45 DCHECK(IsOnNetworkThread()); | |
| 46 return url_request_->response_headers(); | |
| 47 } | |
| 48 | |
| 49 const std::string& CronetURLRequestAdapter::GetNegotiatedProtocol() const { | |
| 50 DCHECK(IsOnNetworkThread()); | |
| 51 return url_request_->response_info().npn_negotiated_protocol; | |
| 52 } | |
| 53 | |
| 54 bool CronetURLRequestAdapter::GetWasCached() const { | |
| 55 DCHECK(IsOnNetworkThread()); | |
| 56 return url_request_->response_info().was_cached; | |
| 57 } | |
| 58 | |
| 59 int64 CronetURLRequestAdapter::GetTotalReceivedBytes() const { | |
| 60 DCHECK(IsOnNetworkThread()); | |
| 61 return url_request_->GetTotalReceivedBytes(); | |
| 62 } | |
| 63 | |
| 64 void CronetURLRequestAdapter::Start() { | |
| 65 DCHECK(!IsOnNetworkThread()); | |
| 66 context_->GetNetworkTaskRunner()->PostTask( | |
| 67 FROM_HERE, | |
| 68 base::Bind(&CronetURLRequestAdapter::StartOnNetworkThread, | |
| 69 base::Unretained(this))); | |
| 70 } | |
| 71 | |
| 72 void CronetURLRequestAdapter::FollowDeferredRedirect() { | |
| 73 DCHECK(!IsOnNetworkThread()); | |
| 74 context_->GetNetworkTaskRunner()->PostTask( | |
| 75 FROM_HERE, | |
| 76 base::Bind( | |
| 77 &CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread, | |
| 78 base::Unretained(this))); | |
| 79 } | |
| 80 | |
| 81 // Receive more data. | |
| 82 void CronetURLRequestAdapter::ReadData() { | |
| 83 DCHECK(!IsOnNetworkThread()); | |
| 84 context_->GetNetworkTaskRunner()->PostTask( | |
| 85 FROM_HERE, | |
| 86 base::Bind(&CronetURLRequestAdapter::ReadDataOnNetworkThread, | |
| 87 base::Unretained(this))); | |
| 88 } | |
| 89 | |
| 90 void CronetURLRequestAdapter::Destroy() { | |
| 91 DCHECK(!IsOnNetworkThread()); | |
| 92 context_->GetNetworkTaskRunner()->PostTask( | |
| 93 FROM_HERE, | |
| 94 base::Bind(&CronetURLRequestAdapter::DestroyOnNetworkThread, | |
| 95 base::Unretained(this))); | |
| 96 } | |
| 97 | |
| 98 // net::URLRequest::Delegate overrides (called on network thread). | |
| 99 | |
| 100 void CronetURLRequestAdapter::OnReceivedRedirect( | |
| 101 net::URLRequest* request, | |
| 102 const net::RedirectInfo& redirect_info, | |
| 103 bool* defer_redirect) { | |
| 104 DCHECK(IsOnNetworkThread()); | |
| 105 DCHECK(request->status().is_success()); | |
| 106 delegate_->OnRedirect(redirect_info.new_url, redirect_info.status_code); | |
| 107 *defer_redirect = true; | |
| 108 } | |
| 109 | |
| 110 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | |
| 111 DCHECK(IsOnNetworkThread()); | |
| 112 if (!MaybeReportError(request)) | |
| 113 return; | |
| 114 delegate_->OnResponseStarted(request->GetResponseCode()); | |
| 115 } | |
| 116 | |
| 117 void CronetURLRequestAdapter::OnReadCompleted(net::URLRequest* request, | |
| 118 int bytes_read) { | |
| 119 DCHECK(IsOnNetworkThread()); | |
| 120 if (!MaybeReportError(request)) | |
| 121 return; | |
| 122 if (bytes_read != 0) { | |
| 123 delegate_->OnBytesRead(Data(), bytes_read); | |
| 124 } else { | |
| 125 delegate_->OnRequestFinished(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 bool CronetURLRequestAdapter::IsOnNetworkThread() const { | |
| 130 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | |
| 131 } | |
| 132 | |
| 133 void CronetURLRequestAdapter::StartOnNetworkThread() { | |
| 134 DCHECK(IsOnNetworkThread()); | |
| 135 VLOG(1) << "Starting chromium request: " | |
| 136 << initial_url_.possibly_invalid_spec().c_str() | |
| 137 << " priority: " << RequestPriorityToString(initial_priority_); | |
| 138 url_request_ = context_->GetURLRequestContext()->CreateRequest( | |
| 139 initial_url_, net::DEFAULT_PRIORITY, this, NULL); | |
| 140 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | |
| 141 net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 142 net::LOAD_DO_NOT_SEND_COOKIES); | |
| 143 url_request_->set_method(initial_method_); | |
| 144 url_request_->SetExtraRequestHeaders(initial_request_headers_); | |
| 145 url_request_->SetPriority(initial_priority_); | |
| 146 url_request_->Start(); | |
| 147 } | |
| 148 | |
| 149 void CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread() { | |
| 150 DCHECK(IsOnNetworkThread()); | |
| 151 | |
| 152 url_request_->FollowDeferredRedirect(); | |
| 153 } | |
| 154 | |
| 155 // Reads all available data or starts an asynchronous read. | |
| 156 void CronetURLRequestAdapter::ReadDataOnNetworkThread() { | |
| 157 DCHECK(IsOnNetworkThread()); | |
| 158 int bytes_read = 0; | |
| 159 // If read completes synchronously, pass data to delegate. | |
| 160 if (url_request_->Read( | |
| 161 read_buffer_.get(), read_buffer_->size(), &bytes_read)) { | |
| 162 OnReadCompleted(url_request_.get(), bytes_read); | |
| 163 } else if (url_request_->status().status() != | |
| 164 net::URLRequestStatus::IO_PENDING) { | |
| 165 OnReadCompleted(url_request_.get(), -1); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void CronetURLRequestAdapter::DestroyOnNetworkThread() { | |
| 170 DCHECK(IsOnNetworkThread()); | |
| 171 VLOG(1) << "Destroy chromium request: " << | |
| 172 initial_url_.possibly_invalid_spec(); | |
| 173 delete this; | |
| 174 } | |
| 175 | |
| 176 bool CronetURLRequestAdapter::MaybeReportError(net::URLRequest* request) const { | |
| 177 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); | |
| 178 DCHECK_EQ(request, url_request_); | |
| 179 if (url_request_->status().is_success()) | |
| 180 return true; | |
| 181 VLOG(1) << "Error " << url_request_->status().error() | |
| 182 << " on chromium request: " << initial_url_.possibly_invalid_spec(); | |
| 183 delegate_->OnError(url_request_->status().error()); | |
| 184 return false; | |
| 185 } | |
| 186 | |
| 187 unsigned char* CronetURLRequestAdapter::Data() const { | |
| 188 return reinterpret_cast<unsigned char*>(read_buffer_->data()); | |
| 189 } | |
| 190 | |
| 191 } // namespace cronet | |
| OLD | NEW |