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

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

Issue 726013002: [Cronet] Hook up library loader, system proxy and network change notifier to async api. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Better handling of context init and shutdown races. Created 5 years, 11 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/cronet_url_request_context_adapter.h" 5 #include "components/cronet/android/cronet_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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 return false; 101 return false;
102 } 102 }
103 103
104 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 104 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
105 }; 105 };
106 106
107 } // namespace 107 } // namespace
108 108
109 namespace cronet { 109 namespace cronet {
110 110
111 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter() 111 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter(
112 : default_load_flags_(0) { 112 scoped_ptr<URLRequestContextConfig> context_config)
113 : network_thread_(new base::Thread("network")),
114 is_context_initialized_(false),
115 default_load_flags_(0) {
mmenke 2015/01/23 20:41:41 LOAD_NORMAL
mef 2015/01/28 21:32:28 Done.
116 context_config_.reset(context_config.release());
mmenke 2015/01/23 20:41:41 Should be in initializer list, and should use Pass
mef 2015/01/28 21:32:27 Done.
117 base::Thread::Options options;
118 options.message_loop_type = base::MessageLoop::TYPE_IO;
119 network_thread_->StartWithOptions(options);
113 } 120 }
114 121
115 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { 122 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() {
116 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); 123 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
117 StopNetLogOnNetworkThread(); 124 StopNetLogOnNetworkThread();
118 } 125 }
119 126
120 void CronetURLRequestContextAdapter::Initialize( 127 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread(
121 scoped_ptr<URLRequestContextConfig> config,
122 const base::Closure& java_init_network_thread) { 128 const base::Closure& java_init_network_thread) {
123 network_thread_ = new base::Thread("network"); 129 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
124 base::Thread::Options options; 130 GetNetworkTaskRunner(), NULL));
mmenke 2015/01/23 20:41:41 nullptr is preferred now, I believe.
mef 2015/01/28 21:32:28 Done.
125 options.message_loop_type = base::MessageLoop::TYPE_IO;
126 network_thread_->StartWithOptions(options);
127
128 GetNetworkTaskRunner()->PostTask( 131 GetNetworkTaskRunner()->PostTask(
129 FROM_HERE, 132 FROM_HERE,
130 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, 133 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread,
131 base::Unretained(this), 134 base::Unretained(this),
132 Passed(&config), 135 Passed(&context_config_),
133 java_init_network_thread)); 136 java_init_network_thread));
134 } 137 }
135 138
136 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( 139 void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
137 scoped_ptr<URLRequestContextConfig> config, 140 scoped_ptr<URLRequestContextConfig> config,
138 const base::Closure& java_init_network_thread) { 141 const base::Closure& java_init_network_thread) {
139 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); 142 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
140 // TODO(mmenke): Add method to have the builder enable SPDY. 143 // TODO(mmenke): Add method to have the builder enable SPDY.
141 net::URLRequestContextBuilder context_builder; 144 net::URLRequestContextBuilder context_builder;
142 context_builder.set_network_delegate(new BasicNetworkDelegate()); 145 context_builder.set_network_delegate(new BasicNetworkDelegate());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 quic_hint.port); 192 quic_hint.port);
190 context_->http_server_properties()->SetAlternateProtocol( 193 context_->http_server_properties()->SetAlternateProtocol(
191 quic_hint_host_port_pair, 194 quic_hint_host_port_pair,
192 static_cast<uint16>(quic_hint.alternate_port), 195 static_cast<uint16>(quic_hint.alternate_port),
193 net::AlternateProtocol::QUIC, 196 net::AlternateProtocol::QUIC,
194 1.0f); 197 1.0f);
195 } 198 }
196 } 199 }
197 200
198 java_init_network_thread.Run(); 201 java_init_network_thread.Run();
202
203 is_context_initialized_ = true;
204 while (!tasks_waiting_for_context_.empty()) {
205 tasks_waiting_for_context_.front().Run();
206 tasks_waiting_for_context_.pop();
207 }
199 } 208 }
200 209
201 void CronetURLRequestContextAdapter::Destroy() { 210 void CronetURLRequestContextAdapter::Destroy() {
202 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); 211 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread());
203 // Stick network_thread_ in a local, as |this| may be destroyed from the 212 // Stick network_thread_ in a local, as |this| may be destroyed from the
204 // network thread before delete network_thread is called. 213 // network thread before delete network_thread is called.
205 base::Thread* network_thread = network_thread_; 214 base::Thread* network_thread = network_thread_;
206 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); 215 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this);
207 // Deleting thread stops it after all tasks are completed. 216 // Deleting thread stops it after all tasks are completed.
208 delete network_thread; 217 delete network_thread;
209 } 218 }
210 219
211 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { 220 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() {
212 if (!context_) { 221 if (!context_) {
213 LOG(ERROR) << "URLRequestContext is not set up"; 222 LOG(ERROR) << "URLRequestContext is not set up";
214 } 223 }
215 return context_.get(); 224 return context_.get();
216 } 225 }
217 226
227 void CronetURLRequestContextAdapter::PostTaskToNetworkThread(
228 const tracked_objects::Location& posted_from,
229 const RunAfterContextInitTask& callback) {
230 GetNetworkTaskRunner()->PostTask(
231 posted_from, base::Bind(&CronetURLRequestContextAdapter::
232 RunTaskAfterContextInitOnNetworkThread,
233 base::Unretained(this), callback));
234 }
235
236 void CronetURLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
237 const RunAfterContextInitTask& callback) {
238 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
239 if (is_context_initialized_) {
240 callback.Run();
241 return;
242 }
243 tasks_waiting_for_context_.push(callback);
244 }
245
218 scoped_refptr<base::SingleThreadTaskRunner> 246 scoped_refptr<base::SingleThreadTaskRunner>
219 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { 247 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const {
220 return network_thread_->task_runner(); 248 return network_thread_->task_runner();
221 } 249 }
222 250
223 void CronetURLRequestContextAdapter::StartNetLogToFile( 251 void CronetURLRequestContextAdapter::StartNetLogToFile(
224 const std::string& file_name) { 252 const std::string& file_name) {
225 GetNetworkTaskRunner()->PostTask( 253 GetNetworkTaskRunner()->PostTask(
226 FROM_HERE, 254 FROM_HERE,
227 base::Bind( 255 base::Bind(
(...skipping 28 matching lines...) Expand all
256 284
257 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { 285 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() {
258 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); 286 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
259 if (net_log_logger_) { 287 if (net_log_logger_) {
260 net_log_logger_->StopObserving(); 288 net_log_logger_->StopObserving();
261 net_log_logger_.reset(); 289 net_log_logger_.reset();
262 } 290 }
263 } 291 }
264 292
265 } // namespace cronet 293 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698