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

Side by Side Diff: components/cronet/android/url_request_adapter.cc

Issue 617393005: Make URLRequestContextAdapter initialization asynchronous (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 6 years, 2 months 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 2014 The Chromium Authors. All rights reserved. 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 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 "url_request_adapter.h" 5 #include "url_request_adapter.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "components/cronet/android/url_request_context_adapter.h" 14 #include "components/cronet/android/url_request_context_adapter.h"
14 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" 15 #include "components/cronet/android/wrapped_channel_upload_element_reader.h"
15 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
16 #include "net/base/upload_bytes_element_reader.h" 17 #include "net/base/upload_bytes_element_reader.h"
17 #include "net/http/http_status_code.h" 18 #include "net/http/http_status_code.h"
18 19
19 namespace cronet { 20 namespace cronet {
20 21
21 static const size_t kBufferSizeIncrement = 8192; 22 static const size_t kBufferSizeIncrement = 8192;
(...skipping 10 matching lines...) Expand all
32 http_status_code_(0), 33 http_status_code_(0),
33 canceled_(false), 34 canceled_(false),
34 expected_size_(0), 35 expected_size_(0),
35 chunked_upload_(false) { 36 chunked_upload_(false) {
36 context_ = context; 37 context_ = context;
37 delegate_ = delegate; 38 delegate_ = delegate;
38 url_ = url; 39 url_ = url;
39 priority_ = priority; 40 priority_ = priority;
40 } 41 }
41 42
42 URLRequestAdapter::~URLRequestAdapter() { 43 URLRequestAdapter::~URLRequestAdapter() {
mmenke 2014/10/03 20:26:58 Could have a DCHECK(OnNetworkThread()); here, too.
xunjieli 2014/10/03 20:39:52 Done.
43 CHECK(url_request_ == NULL); 44 CHECK(url_request_ == NULL);
44 } 45 }
45 46
46 void URLRequestAdapter::SetMethod(const std::string& method) { 47 void URLRequestAdapter::SetMethod(const std::string& method) {
47 method_ = method; 48 method_ = method;
48 } 49 }
49 50
50 void URLRequestAdapter::AddHeader(const std::string& name, 51 void URLRequestAdapter::AddHeader(const std::string& name,
51 const std::string& value) { 52 const std::string& value) {
52 headers_.SetHeader(name, value); 53 headers_.SetHeader(name, value);
(...skipping 16 matching lines...) Expand all
69 70
70 void URLRequestAdapter::EnableChunkedUpload() { 71 void URLRequestAdapter::EnableChunkedUpload() {
71 chunked_upload_ = true; 72 chunked_upload_ = true;
72 } 73 }
73 74
74 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len, 75 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len,
75 bool is_last_chunk) { 76 bool is_last_chunk) {
76 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; 77 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk;
77 scoped_ptr<char[]> buf(new char[bytes_len]); 78 scoped_ptr<char[]> buf(new char[bytes_len]);
78 memcpy(buf.get(), bytes, bytes_len); 79 memcpy(buf.get(), bytes, bytes_len);
79 context_->GetNetworkTaskRunner()->PostTask( 80 context_->RunTaskAfterContextInit(
80 FROM_HERE,
81 base::Bind(&URLRequestAdapter::OnAppendChunk, 81 base::Bind(&URLRequestAdapter::OnAppendChunk,
82 base::Unretained(this), 82 base::Unretained(this),
83 Passed(buf.Pass()), 83 Passed(buf.Pass()),
84 bytes_len, 84 bytes_len,
85 is_last_chunk)); 85 is_last_chunk));
86 } 86 }
87 87
88 std::string URLRequestAdapter::GetHeader(const std::string& name) const { 88 std::string URLRequestAdapter::GetHeader(const std::string& name) const {
89 std::string value; 89 std::string value;
90 if (url_request_ != NULL) { 90 if (url_request_ != NULL) {
91 url_request_->GetResponseHeaderByName(name, &value); 91 url_request_->GetResponseHeaderByName(name, &value);
92 } 92 }
93 return value; 93 return value;
94 } 94 }
95 95
96 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { 96 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const {
97 if (url_request_ == NULL) { 97 if (url_request_ == NULL) {
98 return NULL; 98 return NULL;
99 } 99 }
100 return url_request_->response_headers(); 100 return url_request_->response_headers();
101 } 101 }
102 102
103 std::string URLRequestAdapter::GetNegotiatedProtocol() const { 103 std::string URLRequestAdapter::GetNegotiatedProtocol() const {
104 if (url_request_ == NULL) 104 if (url_request_ == NULL)
105 return std::string(); 105 return std::string();
106 return url_request_->response_info().npn_negotiated_protocol; 106 return url_request_->response_info().npn_negotiated_protocol;
107 } 107 }
108 108
109 void URLRequestAdapter::Start() { 109 void URLRequestAdapter::Start() {
110 context_->GetNetworkTaskRunner()->PostTask( 110 context_->RunTaskAfterContextInit(base::Bind(
111 FROM_HERE, 111 &URLRequestAdapter::OnInitiateConnection, base::Unretained(this)));
112 base::Bind(&URLRequestAdapter::OnInitiateConnection,
113 base::Unretained(this)));
114 } 112 }
115 113
116 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes, 114 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes,
117 int bytes_len, bool is_last_chunk) { 115 int bytes_len, bool is_last_chunk) {
118 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk); 116 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk);
119 } 117 }
120 118
121 void URLRequestAdapter::OnInitiateConnection() { 119 void URLRequestAdapter::OnInitiateConnection() {
122 if (canceled_) { 120 if (canceled_) {
123 return; 121 return;
(...skipping 27 matching lines...) Expand all
151 url_request_->Start(); 149 url_request_->Start();
152 } 150 }
153 151
154 void URLRequestAdapter::Cancel() { 152 void URLRequestAdapter::Cancel() {
155 if (canceled_) { 153 if (canceled_) {
156 return; 154 return;
157 } 155 }
158 156
159 canceled_ = true; 157 canceled_ = true;
160 158
161 context_->GetNetworkTaskRunner()->PostTask( 159 context_->RunTaskAfterContextInit(
162 FROM_HERE,
163 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); 160 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this)));
164 } 161 }
165 162
166 void URLRequestAdapter::OnCancelRequest() { 163 void URLRequestAdapter::OnCancelRequest() {
167 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); 164 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec();
168 165
169 if (url_request_ != NULL) { 166 if (url_request_ != NULL) {
170 url_request_->Cancel(); 167 url_request_->Cancel();
171 } 168 }
172 169
173 OnRequestCanceled(); 170 OnRequestCanceled();
174 } 171 }
175 172
176 void URLRequestAdapter::Destroy() { 173 void URLRequestAdapter::Destroy() {
177 context_->GetNetworkTaskRunner()->PostTask( 174 context_->RunTaskAfterContextInit(
178 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); 175 base::Bind(&URLRequestAdapter::OnDestroyRequest, this));
179 } 176 }
180 177
181 // static 178 // static
182 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { 179 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) {
180 DCHECK(self->OnNetworkThread());
183 VLOG(1) << "Destroying chromium request: " 181 VLOG(1) << "Destroying chromium request: "
184 << self->url_.possibly_invalid_spec(); 182 << self->url_.possibly_invalid_spec();
185 delete self; 183 delete self;
186 } 184 }
187 185
186 // static
188 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { 187 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) {
188 DCHECK(OnNetworkThread());
189 if (request->status().status() != net::URLRequestStatus::SUCCESS) { 189 if (request->status().status() != net::URLRequestStatus::SUCCESS) {
190 OnRequestFailed(); 190 OnRequestFailed();
191 return; 191 return;
192 } 192 }
193 193
194 http_status_code_ = request->GetResponseCode(); 194 http_status_code_ = request->GetResponseCode();
195 VLOG(1) << "Response started with status: " << http_status_code_; 195 VLOG(1) << "Response started with status: " << http_status_code_;
196 196
197 request->GetResponseHeaderByName("Content-Type", &content_type_); 197 request->GetResponseHeaderByName("Content-Type", &content_type_);
198 expected_size_ = request->GetExpectedContentSize(); 198 expected_size_ = request->GetExpectedContentSize();
199 delegate_->OnResponseStarted(this); 199 delegate_->OnResponseStarted(this);
200 200
201 Read(); 201 Read();
202 } 202 }
203 203
204 // Reads all available data or starts an asynchronous read. 204 // Reads all available data or starts an asynchronous read.
205 void URLRequestAdapter::Read() { 205 void URLRequestAdapter::Read() {
206 DCHECK(OnNetworkThread());
206 while (true) { 207 while (true) {
207 if (read_buffer_->RemainingCapacity() == 0) { 208 if (read_buffer_->RemainingCapacity() == 0) {
208 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; 209 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement;
209 read_buffer_->SetCapacity(new_capacity); 210 read_buffer_->SetCapacity(new_capacity);
210 } 211 }
211 212
212 int bytes_read; 213 int bytes_read;
213 if (url_request_->Read( 214 if (url_request_->Read(
214 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { 215 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) {
215 if (bytes_read == 0) { 216 if (bytes_read == 0) {
(...skipping 16 matching lines...) Expand all
232 break; 233 break;
233 } else { 234 } else {
234 OnRequestFailed(); 235 OnRequestFailed();
235 break; 236 break;
236 } 237 }
237 } 238 }
238 } 239 }
239 240
240 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, 241 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request,
241 int bytes_read) { 242 int bytes_read) {
243 DCHECK(OnNetworkThread());
242 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; 244 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes";
243 if (bytes_read < 0) { 245 if (bytes_read < 0) {
244 OnRequestFailed(); 246 OnRequestFailed();
245 return; 247 return;
246 } else if (bytes_read == 0) { 248 } else if (bytes_read == 0) {
247 OnRequestSucceeded(); 249 OnRequestSucceeded();
248 return; 250 return;
249 } 251 }
250 252
251 OnBytesRead(bytes_read); 253 OnBytesRead(bytes_read);
252 Read(); 254 Read();
253 } 255 }
254 256
255 void URLRequestAdapter::OnBytesRead(int bytes_read) { 257 void URLRequestAdapter::OnBytesRead(int bytes_read) {
258 DCHECK(OnNetworkThread());
256 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); 259 read_buffer_->set_offset(read_buffer_->offset() + bytes_read);
257 bytes_read_ += bytes_read; 260 bytes_read_ += bytes_read;
258 total_bytes_read_ += bytes_read; 261 total_bytes_read_ += bytes_read;
259 } 262 }
260 263
261 void URLRequestAdapter::OnRequestSucceeded() { 264 void URLRequestAdapter::OnRequestSucceeded() {
265 DCHECK(OnNetworkThread());
262 if (canceled_) { 266 if (canceled_) {
263 return; 267 return;
264 } 268 }
265 269
266 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ 270 VLOG(1) << "Request completed with HTTP status: " << http_status_code_
267 << ". Total bytes read: " << total_bytes_read_; 271 << ". Total bytes read: " << total_bytes_read_;
268 272
269 OnRequestCompleted(); 273 OnRequestCompleted();
270 } 274 }
271 275
272 void URLRequestAdapter::OnRequestFailed() { 276 void URLRequestAdapter::OnRequestFailed() {
277 DCHECK(OnNetworkThread());
273 if (canceled_) { 278 if (canceled_) {
274 return; 279 return;
275 } 280 }
276 281
277 error_code_ = url_request_->status().error(); 282 error_code_ = url_request_->status().error();
278 VLOG(1) << "Request failed with status: " << url_request_->status().status() 283 VLOG(1) << "Request failed with status: " << url_request_->status().status()
279 << " and error: " << net::ErrorToString(error_code_); 284 << " and error: " << net::ErrorToString(error_code_);
280 OnRequestCompleted(); 285 OnRequestCompleted();
281 } 286 }
282 287
283 void URLRequestAdapter::OnRequestCanceled() { 288 void URLRequestAdapter::OnRequestCanceled() {
289 DCHECK(OnNetworkThread());
284 OnRequestCompleted(); 290 OnRequestCompleted();
285 } 291 }
286 292
287 void URLRequestAdapter::OnRequestCompleted() { 293 void URLRequestAdapter::OnRequestCompleted() {
294 DCHECK(OnNetworkThread());
288 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); 295 VLOG(1) << "Completed: " << url_.possibly_invalid_spec();
289 url_request_.reset(); 296 url_request_.reset();
290 297
291 delegate_->OnBytesRead(this); 298 delegate_->OnBytesRead(this);
292 delegate_->OnRequestFinished(this); 299 delegate_->OnRequestFinished(this);
293 } 300 }
294 301
295 unsigned char* URLRequestAdapter::Data() const { 302 unsigned char* URLRequestAdapter::Data() const {
303 DCHECK(OnNetworkThread());
296 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); 304 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());
297 } 305 }
298 306
307 bool URLRequestAdapter::OnNetworkThread() const {
308 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread();
309 }
310
299 } // namespace cronet 311 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698