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

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"
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 69
70 void URLRequestAdapter::EnableChunkedUpload() { 70 void URLRequestAdapter::EnableChunkedUpload() {
71 chunked_upload_ = true; 71 chunked_upload_ = true;
72 } 72 }
73 73
74 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len, 74 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len,
75 bool is_last_chunk) { 75 bool is_last_chunk) {
76 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; 76 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk;
77 scoped_ptr<char[]> buf(new char[bytes_len]); 77 scoped_ptr<char[]> buf(new char[bytes_len]);
78 memcpy(buf.get(), bytes, bytes_len); 78 memcpy(buf.get(), bytes, bytes_len);
79 context_->GetNetworkTaskRunner()->PostTask( 79 context_->RunTaskAfterContextInit(
80 FROM_HERE,
81 base::Bind(&URLRequestAdapter::OnAppendChunk, 80 base::Bind(&URLRequestAdapter::OnAppendChunk,
82 base::Unretained(this), 81 base::Unretained(this),
83 Passed(buf.Pass()), 82 Passed(buf.Pass()),
84 bytes_len, 83 bytes_len,
85 is_last_chunk)); 84 is_last_chunk));
86 } 85 }
87 86
88 std::string URLRequestAdapter::GetHeader(const std::string& name) const { 87 std::string URLRequestAdapter::GetHeader(const std::string& name) const {
89 std::string value; 88 std::string value;
90 if (url_request_ != NULL) { 89 if (url_request_ != NULL) {
91 url_request_->GetResponseHeaderByName(name, &value); 90 url_request_->GetResponseHeaderByName(name, &value);
92 } 91 }
93 return value; 92 return value;
94 } 93 }
95 94
96 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { 95 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const {
97 if (url_request_ == NULL) { 96 if (url_request_ == NULL) {
98 return NULL; 97 return NULL;
99 } 98 }
100 return url_request_->response_headers(); 99 return url_request_->response_headers();
101 } 100 }
102 101
103 std::string URLRequestAdapter::GetNegotiatedProtocol() const { 102 std::string URLRequestAdapter::GetNegotiatedProtocol() const {
104 if (url_request_ == NULL) 103 if (url_request_ == NULL)
105 return std::string(); 104 return std::string();
106 return url_request_->response_info().npn_negotiated_protocol; 105 return url_request_->response_info().npn_negotiated_protocol;
107 } 106 }
108 107
109 void URLRequestAdapter::Start() { 108 void URLRequestAdapter::Start() {
110 context_->GetNetworkTaskRunner()->PostTask( 109 context_->RunTaskAfterContextInit(base::Bind(
111 FROM_HERE, 110 &URLRequestAdapter::OnInitiateConnection, base::Unretained(this)));
112 base::Bind(&URLRequestAdapter::OnInitiateConnection,
113 base::Unretained(this)));
114 } 111 }
115 112
116 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes, 113 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes,
117 int bytes_len, bool is_last_chunk) { 114 int bytes_len, bool is_last_chunk) {
118 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk); 115 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk);
119 } 116 }
120 117
121 void URLRequestAdapter::OnInitiateConnection() { 118 void URLRequestAdapter::OnInitiateConnection() {
122 if (canceled_) { 119 if (canceled_) {
123 return; 120 return;
(...skipping 27 matching lines...) Expand all
151 url_request_->Start(); 148 url_request_->Start();
152 } 149 }
153 150
154 void URLRequestAdapter::Cancel() { 151 void URLRequestAdapter::Cancel() {
155 if (canceled_) { 152 if (canceled_) {
156 return; 153 return;
157 } 154 }
158 155
159 canceled_ = true; 156 canceled_ = true;
160 157
161 context_->GetNetworkTaskRunner()->PostTask( 158 context_->RunTaskAfterContextInit(
162 FROM_HERE,
163 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); 159 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this)));
164 } 160 }
165 161
166 void URLRequestAdapter::OnCancelRequest() { 162 void URLRequestAdapter::OnCancelRequest() {
167 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); 163 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec();
168 164
169 if (url_request_ != NULL) { 165 if (url_request_ != NULL) {
170 url_request_->Cancel(); 166 url_request_->Cancel();
171 } 167 }
172 168
173 OnRequestCanceled(); 169 OnRequestCanceled();
174 } 170 }
175 171
176 void URLRequestAdapter::Destroy() { 172 void URLRequestAdapter::Destroy() {
177 context_->GetNetworkTaskRunner()->PostTask( 173 context_->RunTaskAfterContextInit(
178 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); 174 base::Bind(&URLRequestAdapter::OnDestroyRequest, this));
179 } 175 }
180 176
181 // static 177 // static
182 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { 178 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) {
mmenke 2014/10/03 19:36:14 Suggest adding a: DCHECK(context_->GetNetworkTask
xunjieli 2014/10/03 20:09:54 Done.
183 VLOG(1) << "Destroying chromium request: " 179 VLOG(1) << "Destroying chromium request: "
184 << self->url_.possibly_invalid_spec(); 180 << self->url_.possibly_invalid_spec();
185 delete self; 181 delete self;
186 } 182 }
187 183
188 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { 184 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) {
189 if (request->status().status() != net::URLRequestStatus::SUCCESS) { 185 if (request->status().status() != net::URLRequestStatus::SUCCESS) {
190 OnRequestFailed(); 186 OnRequestFailed();
191 return; 187 return;
192 } 188 }
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 286
291 delegate_->OnBytesRead(this); 287 delegate_->OnBytesRead(this);
292 delegate_->OnRequestFinished(this); 288 delegate_->OnRequestFinished(this);
293 } 289 }
294 290
295 unsigned char* URLRequestAdapter::Data() const { 291 unsigned char* URLRequestAdapter::Data() const {
296 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); 292 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());
297 } 293 }
298 294
299 } // namespace cronet 295 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698