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

Side by Side Diff: components/cronet/android/url_request_context_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 "components/cronet/android/url_request_context_adapter.h" 5 #include "components/cronet/android/url_request_context_adapter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
10 #include "components/cronet/url_request_context_config.h" 10 #include "components/cronet/url_request_context_config.h"
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 128
129 GetNetworkTaskRunner()->PostTask( 129 GetNetworkTaskRunner()->PostTask(
130 FROM_HERE, 130 FROM_HERE,
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, 131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext,
132 this, 132 this,
133 Passed(&config))); 133 Passed(&config)));
134 } 134 }
135 135
136 void URLRequestContextAdapter::InitializeURLRequestContext( 136 void URLRequestContextAdapter::InitializeURLRequestContext(
137 scoped_ptr<URLRequestContextConfig> config) { 137 scoped_ptr<URLRequestContextConfig> config) {
138 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
138 // TODO(mmenke): Add method to have the builder enable SPDY. 139 // TODO(mmenke): Add method to have the builder enable SPDY.
139 net::URLRequestContextBuilder context_builder; 140 net::URLRequestContextBuilder context_builder;
140 context_builder.set_network_delegate(new BasicNetworkDelegate()); 141 context_builder.set_network_delegate(new BasicNetworkDelegate());
141 context_builder.set_proxy_config_service( 142 context_builder.set_proxy_config_service(
142 new net::ProxyConfigServiceFixed(net::ProxyConfig())); 143 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
143 config->ConfigureURLRequestContextBuilder(&context_builder); 144 config->ConfigureURLRequestContextBuilder(&context_builder);
144 145
145 context_.reset(context_builder.Build()); 146 context_.reset(context_builder.Build());
146 147
147 // Currently (circa M39) enabling QUIC requires setting probability threshold. 148 // Currently (circa M39) enabling QUIC requires setting probability threshold.
(...skipping 23 matching lines...) Expand all
171 } 172 }
172 173
173 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, 174 net::HostPortPair quic_hint_host_port_pair(quic_hint.host,
174 quic_hint.port); 175 quic_hint.port);
175 context_->http_server_properties()->SetAlternateProtocol( 176 context_->http_server_properties()->SetAlternateProtocol(
176 quic_hint_host_port_pair, 177 quic_hint_host_port_pair,
177 static_cast<uint16>(quic_hint.alternate_port), 178 static_cast<uint16>(quic_hint.alternate_port),
178 net::AlternateProtocol::QUIC, 179 net::AlternateProtocol::QUIC,
179 1.0f); 180 1.0f);
180 } 181 }
182 is_context_initialized_ = true;
183 while (!tasks_waiting_for_context_.empty()) {
184 tasks_waiting_for_context_.front().Run();
185 tasks_waiting_for_context_.pop();
186 }
181 } 187 }
182 188
183 if (VLOG_IS_ON(2)) { 189 if (VLOG_IS_ON(2)) {
184 net_log_observer_.reset(new NetLogObserver()); 190 net_log_observer_.reset(new NetLogObserver());
185 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), 191 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
186 net::NetLog::LOG_ALL_BUT_BYTES); 192 net::NetLog::LOG_ALL_BUT_BYTES);
187 } 193 }
188 194
189 delegate_->OnContextInitialized(this); 195 delegate_->OnContextInitialized(this);
190 } 196 }
191 197
198 void URLRequestContextAdapter::RunTaskAfterContextInit(
199 const RunAfterContextInitTask& callback) {
200 GetNetworkTaskRunner()->PostTask(
201 FROM_HERE,
202 base::Bind(
203 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
204 this,
205 callback));
206 }
207
208 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
209 const RunAfterContextInitTask& callback) {
210 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
211 if (is_context_initialized_) {
212 callback.Run();
213 return;
214 }
215 tasks_waiting_for_context_.push(callback);
216 }
217
192 URLRequestContextAdapter::~URLRequestContextAdapter() { 218 URLRequestContextAdapter::~URLRequestContextAdapter() {
mmenke 2014/10/03 20:26:58 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThr
xunjieli 2014/10/03 20:39:52 Done.
193 if (net_log_observer_) { 219 if (net_log_observer_) {
194 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); 220 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
195 net_log_observer_.reset(); 221 net_log_observer_.reset();
196 } 222 }
197 StopNetLog(); 223 StopNetLogHelper();
198 // TODO(mef): Ensure that |network_thread_| is destroyed properly. 224 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
199 } 225 }
200 226
201 const std::string& URLRequestContextAdapter::GetUserAgent( 227 const std::string& URLRequestContextAdapter::GetUserAgent(
202 const GURL& url) const { 228 const GURL& url) const {
203 return user_agent_; 229 return user_agent_;
204 } 230 }
205 231
206 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { 232 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
233 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
207 if (!context_) { 234 if (!context_) {
208 LOG(ERROR) << "URLRequestContext is not set up"; 235 LOG(ERROR) << "URLRequestContext is not set up";
209 } 236 }
210 return context_.get(); 237 return context_.get();
211 } 238 }
212 239
213 scoped_refptr<base::SingleThreadTaskRunner> 240 scoped_refptr<base::SingleThreadTaskRunner>
214 URLRequestContextAdapter::GetNetworkTaskRunner() const { 241 URLRequestContextAdapter::GetNetworkTaskRunner() const {
215 return network_thread_->message_loop_proxy(); 242 return network_thread_->message_loop_proxy();
216 } 243 }
217 244
218 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { 245 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
246 RunTaskAfterContextInit(base::Bind(
247 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name));
248 }
249
250 void URLRequestContextAdapter::StopNetLog() {
251 RunTaskAfterContextInit(
252 base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
253 }
254
255 void URLRequestContextAdapter::StartNetLogToFileHelper(
256 const std::string& file_name) {
257 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
219 // Do nothing if already logging to a file. 258 // Do nothing if already logging to a file.
220 if (net_log_logger_) 259 if (net_log_logger_)
221 return; 260 return;
222 261
223 base::FilePath file_path(file_name); 262 base::FilePath file_path(file_name);
224 FILE* file = base::OpenFile(file_path, "w"); 263 FILE* file = base::OpenFile(file_path, "w");
225 if (!file) 264 if (!file)
226 return; 265 return;
227 266
228 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); 267 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
229 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); 268 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
230 net_log_logger_->StartObserving(context_->net_log()); 269 net_log_logger_->StartObserving(context_->net_log());
231 } 270 }
232 271
233 void URLRequestContextAdapter::StopNetLog() { 272 void URLRequestContextAdapter::StopNetLogHelper() {
273 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
234 if (net_log_logger_) { 274 if (net_log_logger_) {
235 net_log_logger_->StopObserving(); 275 net_log_logger_->StopObserving();
236 net_log_logger_.reset(); 276 net_log_logger_.reset();
237 } 277 }
238 } 278 }
239 279
240 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { 280 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
241 VLOG(2) << "Net log entry: type=" << entry.type() 281 VLOG(2) << "Net log entry: type=" << entry.type()
242 << ", source=" << entry.source().type << ", phase=" << entry.phase(); 282 << ", source=" << entry.source().type << ", phase=" << entry.phase();
243 } 283 }
244 284
245 } // namespace cronet 285 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698