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

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 11 matching lines...) Expand all
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() {
44 DCHECK(OnNetworkThread());
43 CHECK(url_request_ == NULL); 45 CHECK(url_request_ == NULL);
44 } 46 }
45 47
46 void URLRequestAdapter::SetMethod(const std::string& method) { 48 void URLRequestAdapter::SetMethod(const std::string& method) {
47 method_ = method; 49 method_ = method;
48 } 50 }
49 51
50 void URLRequestAdapter::AddHeader(const std::string& name, 52 void URLRequestAdapter::AddHeader(const std::string& name,
51 const std::string& value) { 53 const std::string& value) {
52 headers_.SetHeader(name, value); 54 headers_.SetHeader(name, value);
(...skipping 16 matching lines...) Expand all
69 71
70 void URLRequestAdapter::EnableChunkedUpload() { 72 void URLRequestAdapter::EnableChunkedUpload() {
71 chunked_upload_ = true; 73 chunked_upload_ = true;
72 } 74 }
73 75
74 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len, 76 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len,
75 bool is_last_chunk) { 77 bool is_last_chunk) {
76 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; 78 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk;
77 scoped_ptr<char[]> buf(new char[bytes_len]); 79 scoped_ptr<char[]> buf(new char[bytes_len]);
78 memcpy(buf.get(), bytes, bytes_len); 80 memcpy(buf.get(), bytes, bytes_len);
79 context_->GetNetworkTaskRunner()->PostTask( 81 context_->RunTaskAfterContextInit(
80 FROM_HERE,
81 base::Bind(&URLRequestAdapter::OnAppendChunk, 82 base::Bind(&URLRequestAdapter::OnAppendChunk,
82 base::Unretained(this), 83 base::Unretained(this),
83 Passed(buf.Pass()), 84 Passed(buf.Pass()),
84 bytes_len, 85 bytes_len,
85 is_last_chunk)); 86 is_last_chunk));
86 } 87 }
87 88
88 std::string URLRequestAdapter::GetHeader(const std::string& name) const { 89 std::string URLRequestAdapter::GetHeader(const std::string& name) const {
89 std::string value; 90 std::string value;
90 if (url_request_ != NULL) { 91 if (url_request_ != NULL) {
91 url_request_->GetResponseHeaderByName(name, &value); 92 url_request_->GetResponseHeaderByName(name, &value);
92 } 93 }
93 return value; 94 return value;
94 } 95 }
95 96
96 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { 97 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const {
97 if (url_request_ == NULL) { 98 if (url_request_ == NULL) {
98 return NULL; 99 return NULL;
99 } 100 }
100 return url_request_->response_headers(); 101 return url_request_->response_headers();
101 } 102 }
102 103
103 std::string URLRequestAdapter::GetNegotiatedProtocol() const { 104 std::string URLRequestAdapter::GetNegotiatedProtocol() const {
104 if (url_request_ == NULL) 105 if (url_request_ == NULL)
105 return std::string(); 106 return std::string();
106 return url_request_->response_info().npn_negotiated_protocol; 107 return url_request_->response_info().npn_negotiated_protocol;
107 } 108 }
108 109
109 void URLRequestAdapter::Start() { 110 void URLRequestAdapter::Start() {
110 context_->GetNetworkTaskRunner()->PostTask( 111 context_->RunTaskAfterContextInit(base::Bind(
111 FROM_HERE, 112 &URLRequestAdapter::OnInitiateConnection, base::Unretained(this)));
112 base::Bind(&URLRequestAdapter::OnInitiateConnection,
113 base::Unretained(this)));
114 } 113 }
115 114
116 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes, 115 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes,
117 int bytes_len, bool is_last_chunk) { 116 int bytes_len, bool is_last_chunk) {
117 DCHECK(OnNetworkThread());
118 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk); 118 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk);
119 } 119 }
120 120
121 void URLRequestAdapter::OnInitiateConnection() { 121 void URLRequestAdapter::OnInitiateConnection() {
122 DCHECK(OnNetworkThread());
122 if (canceled_) { 123 if (canceled_) {
123 return; 124 return;
124 } 125 }
125 126
126 VLOG(1) << "Starting chromium request: " 127 VLOG(1) << "Starting chromium request: "
127 << url_.possibly_invalid_spec().c_str() 128 << url_.possibly_invalid_spec().c_str()
128 << " priority: " << RequestPriorityToString(priority_); 129 << " priority: " << RequestPriorityToString(priority_);
129 url_request_ = context_->GetURLRequestContext()->CreateRequest( 130 url_request_ = context_->GetURLRequestContext()->CreateRequest(
130 url_, net::DEFAULT_PRIORITY, this, NULL); 131 url_, net::DEFAULT_PRIORITY, this, NULL);
131 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | 132 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE |
(...skipping 19 matching lines...) Expand all
151 url_request_->Start(); 152 url_request_->Start();
152 } 153 }
153 154
154 void URLRequestAdapter::Cancel() { 155 void URLRequestAdapter::Cancel() {
155 if (canceled_) { 156 if (canceled_) {
156 return; 157 return;
157 } 158 }
158 159
159 canceled_ = true; 160 canceled_ = true;
160 161
161 context_->GetNetworkTaskRunner()->PostTask( 162 context_->RunTaskAfterContextInit(
mef 2014/10/10 15:06:27 Does it make sense to make |RunTaskAfterContextIni
xunjieli 2014/10/10 17:25:45 Done. The "run after context init" part is sort of
162 FROM_HERE,
163 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); 163 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this)));
164 } 164 }
165 165
166 void URLRequestAdapter::OnCancelRequest() { 166 void URLRequestAdapter::OnCancelRequest() {
167 DCHECK(OnNetworkThread());
167 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); 168 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec();
168 169
169 if (url_request_ != NULL) { 170 if (url_request_ != NULL) {
170 url_request_->Cancel(); 171 url_request_->Cancel();
171 } 172 }
172 173
173 OnRequestCanceled(); 174 OnRequestCanceled();
174 } 175 }
175 176
176 void URLRequestAdapter::Destroy() { 177 void URLRequestAdapter::Destroy() {
177 context_->GetNetworkTaskRunner()->PostTask( 178 context_->RunTaskAfterContextInit(
178 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); 179 base::Bind(&URLRequestAdapter::OnDestroyRequest, this));
179 } 180 }
180 181
181 // static 182 // static
182 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { 183 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) {
184 DCHECK(self->OnNetworkThread());
183 VLOG(1) << "Destroying chromium request: " 185 VLOG(1) << "Destroying chromium request: "
184 << self->url_.possibly_invalid_spec(); 186 << self->url_.possibly_invalid_spec();
185 delete self; 187 delete self;
186 } 188 }
187 189
190 // static
188 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { 191 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) {
192 DCHECK(OnNetworkThread());
189 if (request->status().status() != net::URLRequestStatus::SUCCESS) { 193 if (request->status().status() != net::URLRequestStatus::SUCCESS) {
190 OnRequestFailed(); 194 OnRequestFailed();
191 return; 195 return;
192 } 196 }
193 197
194 http_status_code_ = request->GetResponseCode(); 198 http_status_code_ = request->GetResponseCode();
195 VLOG(1) << "Response started with status: " << http_status_code_; 199 VLOG(1) << "Response started with status: " << http_status_code_;
196 200
197 request->GetResponseHeaderByName("Content-Type", &content_type_); 201 request->GetResponseHeaderByName("Content-Type", &content_type_);
198 expected_size_ = request->GetExpectedContentSize(); 202 expected_size_ = request->GetExpectedContentSize();
199 delegate_->OnResponseStarted(this); 203 delegate_->OnResponseStarted(this);
200 204
201 Read(); 205 Read();
202 } 206 }
203 207
204 // Reads all available data or starts an asynchronous read. 208 // Reads all available data or starts an asynchronous read.
205 void URLRequestAdapter::Read() { 209 void URLRequestAdapter::Read() {
210 DCHECK(OnNetworkThread());
206 while (true) { 211 while (true) {
207 if (read_buffer_->RemainingCapacity() == 0) { 212 if (read_buffer_->RemainingCapacity() == 0) {
208 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; 213 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement;
209 read_buffer_->SetCapacity(new_capacity); 214 read_buffer_->SetCapacity(new_capacity);
210 } 215 }
211 216
212 int bytes_read; 217 int bytes_read;
213 if (url_request_->Read( 218 if (url_request_->Read(
214 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { 219 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) {
215 if (bytes_read == 0) { 220 if (bytes_read == 0) {
(...skipping 16 matching lines...) Expand all
232 break; 237 break;
233 } else { 238 } else {
234 OnRequestFailed(); 239 OnRequestFailed();
235 break; 240 break;
236 } 241 }
237 } 242 }
238 } 243 }
239 244
240 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, 245 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request,
241 int bytes_read) { 246 int bytes_read) {
247 DCHECK(OnNetworkThread());
242 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; 248 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes";
243 if (bytes_read < 0) { 249 if (bytes_read < 0) {
244 OnRequestFailed(); 250 OnRequestFailed();
245 return; 251 return;
246 } else if (bytes_read == 0) { 252 } else if (bytes_read == 0) {
247 OnRequestSucceeded(); 253 OnRequestSucceeded();
248 return; 254 return;
249 } 255 }
250 256
251 OnBytesRead(bytes_read); 257 OnBytesRead(bytes_read);
252 Read(); 258 Read();
253 } 259 }
254 260
255 void URLRequestAdapter::OnBytesRead(int bytes_read) { 261 void URLRequestAdapter::OnBytesRead(int bytes_read) {
262 DCHECK(OnNetworkThread());
256 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); 263 read_buffer_->set_offset(read_buffer_->offset() + bytes_read);
257 bytes_read_ += bytes_read; 264 bytes_read_ += bytes_read;
258 total_bytes_read_ += bytes_read; 265 total_bytes_read_ += bytes_read;
259 } 266 }
260 267
261 void URLRequestAdapter::OnRequestSucceeded() { 268 void URLRequestAdapter::OnRequestSucceeded() {
269 DCHECK(OnNetworkThread());
262 if (canceled_) { 270 if (canceled_) {
263 return; 271 return;
264 } 272 }
265 273
266 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ 274 VLOG(1) << "Request completed with HTTP status: " << http_status_code_
267 << ". Total bytes read: " << total_bytes_read_; 275 << ". Total bytes read: " << total_bytes_read_;
268 276
269 OnRequestCompleted(); 277 OnRequestCompleted();
270 } 278 }
271 279
272 void URLRequestAdapter::OnRequestFailed() { 280 void URLRequestAdapter::OnRequestFailed() {
281 DCHECK(OnNetworkThread());
273 if (canceled_) { 282 if (canceled_) {
274 return; 283 return;
275 } 284 }
276 285
277 error_code_ = url_request_->status().error(); 286 error_code_ = url_request_->status().error();
278 VLOG(1) << "Request failed with status: " << url_request_->status().status() 287 VLOG(1) << "Request failed with status: " << url_request_->status().status()
279 << " and error: " << net::ErrorToString(error_code_); 288 << " and error: " << net::ErrorToString(error_code_);
280 OnRequestCompleted(); 289 OnRequestCompleted();
281 } 290 }
282 291
283 void URLRequestAdapter::OnRequestCanceled() { 292 void URLRequestAdapter::OnRequestCanceled() {
293 DCHECK(OnNetworkThread());
284 OnRequestCompleted(); 294 OnRequestCompleted();
285 } 295 }
286 296
287 void URLRequestAdapter::OnRequestCompleted() { 297 void URLRequestAdapter::OnRequestCompleted() {
298 DCHECK(OnNetworkThread());
288 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); 299 VLOG(1) << "Completed: " << url_.possibly_invalid_spec();
289 url_request_.reset(); 300 url_request_.reset();
290 301
291 delegate_->OnBytesRead(this); 302 delegate_->OnBytesRead(this);
292 delegate_->OnRequestFinished(this); 303 delegate_->OnRequestFinished(this);
293 } 304 }
294 305
295 unsigned char* URLRequestAdapter::Data() const { 306 unsigned char* URLRequestAdapter::Data() const {
307 DCHECK(OnNetworkThread());
296 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); 308 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());
297 } 309 }
298 310
311 bool URLRequestAdapter::OnNetworkThread() const {
312 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread();
313 }
314
299 } // namespace cronet 315 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698