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 size_t kBufferSizeIncrement = 32768; |
| 21 |
| 22 CronetURLRequestAdapter::CronetURLRequestAdapter( |
| 23 CronetURLRequestContextAdapter* context, |
| 24 CronetURLRequestAdapterDelegate* delegate, |
| 25 const GURL& url, |
| 26 net::RequestPriority priority) |
| 27 : initial_method_("GET"), |
| 28 read_buffer_(new net::IOBufferWithSize(kBufferSizeIncrement)), |
| 29 called_to_delegate_(false), |
| 30 destroy_soon_(false) { |
| 31 context_ = context; |
| 32 delegate_ = delegate; |
| 33 initial_url_ = url; |
| 34 initial_priority_ = priority; |
| 35 } |
| 36 |
| 37 CronetURLRequestAdapter::~CronetURLRequestAdapter() { |
| 38 url_request_.reset(); |
| 39 } |
| 40 |
| 41 void CronetURLRequestAdapter::SetMethod(const std::string& method) { |
| 42 initial_method_ = method; |
| 43 } |
| 44 |
| 45 void CronetURLRequestAdapter::AddRequestHeader(const std::string& name, |
| 46 const std::string& value) { |
| 47 initial_request_headers_.SetHeader(name, value); |
| 48 } |
| 49 |
| 50 net::HttpResponseHeaders* CronetURLRequestAdapter::GetResponseHeaders() const { |
| 51 DCHECK(IsOnNetworkThread()); |
| 52 if (url_request_ == NULL) |
| 53 return NULL; |
| 54 return url_request_->response_headers(); |
| 55 } |
| 56 |
| 57 std::string CronetURLRequestAdapter::GetNegotiatedProtocol() const { |
| 58 DCHECK(IsOnNetworkThread()); |
| 59 if (url_request_ == NULL) |
| 60 return std::string(); |
| 61 return url_request_->response_info().npn_negotiated_protocol; |
| 62 } |
| 63 |
| 64 bool CronetURLRequestAdapter::GetWasCached() const { |
| 65 DCHECK(IsOnNetworkThread()); |
| 66 if (url_request_ == NULL) |
| 67 return false; |
| 68 return url_request_->response_info().was_cached; |
| 69 } |
| 70 |
| 71 int64 CronetURLRequestAdapter::GetTotalReceivedBytes() const { |
| 72 DCHECK(IsOnNetworkThread()); |
| 73 if (url_request_ == NULL) |
| 74 return 0; |
| 75 return url_request_->GetTotalReceivedBytes(); |
| 76 } |
| 77 |
| 78 void CronetURLRequestAdapter::Start() { |
| 79 DCHECK(!IsOnNetworkThread()); |
| 80 context_->GetNetworkTaskRunner()->PostTask( |
| 81 FROM_HERE, |
| 82 base::Bind(&CronetURLRequestAdapter::StartOnNetworkThread, |
| 83 base::Unretained(this))); |
| 84 } |
| 85 |
| 86 void CronetURLRequestAdapter::FollowDeferredRedirect() { |
| 87 DCHECK(!IsOnNetworkThread()); |
| 88 context_->GetNetworkTaskRunner()->PostTask( |
| 89 FROM_HERE, |
| 90 base::Bind( |
| 91 &CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread, |
| 92 base::Unretained(this))); |
| 93 } |
| 94 |
| 95 // Receive more data. |
| 96 void CronetURLRequestAdapter::ReadData() { |
| 97 DCHECK(!IsOnNetworkThread()); |
| 98 context_->GetNetworkTaskRunner()->PostTask( |
| 99 FROM_HERE, |
| 100 base::Bind(&CronetURLRequestAdapter::ReadDataOnNetworkThread, |
| 101 base::Unretained(this))); |
| 102 } |
| 103 |
| 104 void CronetURLRequestAdapter::Destroy() { |
| 105 DCHECK(!IsOnNetworkThread()); |
| 106 context_->GetNetworkTaskRunner()->PostTask( |
| 107 FROM_HERE, |
| 108 base::Bind(&CronetURLRequestAdapter::DestroyOnNetworkThread, |
| 109 base::Unretained(this))); |
| 110 } |
| 111 |
| 112 // net::URLRequest::Delegate overrides (called on network thread). |
| 113 void CronetURLRequestAdapter::OnReceivedRedirect( |
| 114 net::URLRequest* request, |
| 115 const net::RedirectInfo& redirect_info, |
| 116 bool* defer_redirect) { |
| 117 DCHECK(IsOnNetworkThread()); |
| 118 DCHECK(request->status().is_success()); |
| 119 called_to_delegate_ = true; |
| 120 delegate_->OnRedirect(redirect_info.new_url, redirect_info.status_code); |
| 121 *defer_redirect = true; |
| 122 } |
| 123 |
| 124 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { |
| 125 DCHECK(IsOnNetworkThread()); |
| 126 if (!CheckStatus(request)) |
| 127 return; |
| 128 called_to_delegate_ = true; |
| 129 delegate_->OnResponseStarted(request->GetResponseCode()); |
| 130 } |
| 131 |
| 132 void CronetURLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| 133 int bytes_read) { |
| 134 DCHECK(IsOnNetworkThread()); |
| 135 if (!CheckStatus(request)) |
| 136 return; |
| 137 called_to_delegate_ = true; |
| 138 if (bytes_read != 0) { |
| 139 delegate_->OnBytesRead(Data(), bytes_read); |
| 140 } else { |
| 141 delegate_->OnRequestFinished(); |
| 142 } |
| 143 } |
| 144 |
| 145 bool CronetURLRequestAdapter::IsOnNetworkThread() const { |
| 146 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| 147 } |
| 148 |
| 149 void CronetURLRequestAdapter::StartOnNetworkThread() { |
| 150 DCHECK(IsOnNetworkThread()); |
| 151 if (destroy_soon_) |
| 152 return; |
| 153 |
| 154 VLOG(1) << "Starting chromium request: " |
| 155 << initial_url_.possibly_invalid_spec().c_str() |
| 156 << " priority: " << RequestPriorityToString(initial_priority_); |
| 157 url_request_ = context_->GetURLRequestContext()->CreateRequest( |
| 158 initial_url_, net::DEFAULT_PRIORITY, this, NULL); |
| 159 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| 160 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 161 net::LOAD_DO_NOT_SEND_COOKIES); |
| 162 url_request_->set_method(initial_method_); |
| 163 url_request_->SetExtraRequestHeaders(initial_request_headers_); |
| 164 url_request_->SetPriority(initial_priority_); |
| 165 url_request_->Start(); |
| 166 } |
| 167 |
| 168 void CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread() { |
| 169 DCHECK(IsOnNetworkThread()); |
| 170 if (WasDestroyedWhileCalledToDelegate()) |
| 171 return; |
| 172 |
| 173 url_request_->FollowDeferredRedirect(); |
| 174 } |
| 175 |
| 176 // Reads all available data or starts an asynchronous read. |
| 177 void CronetURLRequestAdapter::ReadDataOnNetworkThread() { |
| 178 DCHECK(IsOnNetworkThread()); |
| 179 if (WasDestroyedWhileCalledToDelegate()) |
| 180 return; |
| 181 int bytes_read = 0; |
| 182 // If read completes synchronously, pass data to delegate. |
| 183 if (url_request_->Read( |
| 184 read_buffer_.get(), read_buffer_->size(), &bytes_read)) { |
| 185 OnReadCompleted(url_request_.get(), bytes_read); |
| 186 } else if (url_request_->status().status() != |
| 187 net::URLRequestStatus::IO_PENDING) { |
| 188 OnReadCompleted(url_request_.get(), -1); |
| 189 } |
| 190 } |
| 191 |
| 192 void CronetURLRequestAdapter::DestroyOnNetworkThread() { |
| 193 DCHECK(IsOnNetworkThread()); |
| 194 VLOG(1) << "Destroy chromium request: " << |
| 195 initial_url_.possibly_invalid_spec(); |
| 196 |
| 197 if (called_to_delegate_) { |
| 198 destroy_soon_ = true; |
| 199 return; |
| 200 } |
| 201 |
| 202 if (url_request_ != NULL) |
| 203 url_request_->Cancel(); |
| 204 |
| 205 delete this; |
| 206 } |
| 207 |
| 208 bool CronetURLRequestAdapter::WasDestroyedWhileCalledToDelegate() { |
| 209 DCHECK(IsOnNetworkThread()); |
| 210 DCHECK(called_to_delegate_); |
| 211 called_to_delegate_ = false; |
| 212 if (destroy_soon_) { |
| 213 DestroyOnNetworkThread(); |
| 214 return true; |
| 215 } |
| 216 return false; |
| 217 } |
| 218 |
| 219 bool CronetURLRequestAdapter::CheckStatus(net::URLRequest* request) { |
| 220 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); |
| 221 DCHECK_EQ(request, url_request_); |
| 222 if (url_request_->status().is_success()) |
| 223 return true; |
| 224 VLOG(1) << "Error " << url_request_->status().error() |
| 225 << " on chromium request: " << initial_url_.possibly_invalid_spec(); |
| 226 if (!called_to_delegate_) { |
| 227 called_to_delegate_ = true; |
| 228 delegate_->OnError(url_request_->status().error()); |
| 229 } |
| 230 return false; |
| 231 } |
| 232 |
| 233 unsigned char* CronetURLRequestAdapter::Data() const { |
| 234 return reinterpret_cast<unsigned char*>(read_buffer_->data()); |
| 235 } |
| 236 |
| 237 } // namespace cronet |
OLD | NEW |