OLD | NEW |
---|---|
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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 return false; | 100 return false; |
101 } | 101 } |
102 | 102 |
103 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 103 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
104 }; | 104 }; |
105 | 105 |
106 } // namespace | 106 } // namespace |
107 | 107 |
108 namespace cronet { | 108 namespace cronet { |
109 | 109 |
110 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter() { | 110 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter() |
111 : is_context_initialized_(false) { | |
111 } | 112 } |
112 | 113 |
113 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 114 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
114 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 115 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
115 StopNetLogOnNetworkThread(); | 116 StopNetLogOnNetworkThread(); |
116 } | 117 } |
117 | 118 |
118 void CronetURLRequestContextAdapter::Initialize( | 119 void CronetURLRequestContextAdapter::Initialize( |
119 scoped_ptr<URLRequestContextConfig> config, | 120 scoped_ptr<URLRequestContextConfig> config, |
120 const base::Closure& java_init_network_thread) { | 121 const base::Closure& java_init_network_thread) { |
121 network_thread_ = new base::Thread("network"); | 122 network_thread_ = new base::Thread("network"); |
122 base::Thread::Options options; | 123 base::Thread::Options options; |
123 options.message_loop_type = base::MessageLoop::TYPE_IO; | 124 options.message_loop_type = base::MessageLoop::TYPE_IO; |
124 network_thread_->StartWithOptions(options); | 125 network_thread_->StartWithOptions(options); |
125 | 126 |
126 GetNetworkTaskRunner()->PostTask( | 127 GetNetworkTaskRunner()->PostTask( |
127 FROM_HERE, | 128 FROM_HERE, |
128 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 129 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
129 base::Unretained(this), | 130 base::Unretained(this), |
130 Passed(&config), | 131 Passed(&config), |
131 java_init_network_thread)); | 132 java_init_network_thread)); |
132 } | 133 } |
133 | 134 |
135 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread() { | |
136 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | |
xunjieli
2015/01/05 14:48:03
I wonder whether it makes a difference for each co
mef
2015/01/05 16:59:21
CreateSystemProxyConfigService takes io_task_runne
mmenke
2015/01/05 18:20:39
I'm not really a big fan of globals, unless they'r
| |
137 GetNetworkTaskRunner(), NULL)); | |
xunjieli
2015/01/05 14:48:03
I think we moved the task of InitializeOnNetworkTh
mef
2015/01/05 16:59:21
Definitely, thanks for spotting that. Done.
| |
138 } | |
139 | |
134 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 140 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
135 scoped_ptr<URLRequestContextConfig> config, | 141 scoped_ptr<URLRequestContextConfig> config, |
136 const base::Closure& java_init_network_thread) { | 142 const base::Closure& java_init_network_thread) { |
137 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 143 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
138 // TODO(mmenke): Add method to have the builder enable SPDY. | 144 // TODO(mmenke): Add method to have the builder enable SPDY. |
139 net::URLRequestContextBuilder context_builder; | 145 net::URLRequestContextBuilder context_builder; |
140 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 146 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
141 context_builder.set_proxy_config_service( | 147 context_builder.set_proxy_config_service( |
142 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 148 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
143 config->ConfigureURLRequestContextBuilder(&context_builder); | 149 config->ConfigureURLRequestContextBuilder(&context_builder); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 quic_hint.port); | 188 quic_hint.port); |
183 context_->http_server_properties()->SetAlternateProtocol( | 189 context_->http_server_properties()->SetAlternateProtocol( |
184 quic_hint_host_port_pair, | 190 quic_hint_host_port_pair, |
185 static_cast<uint16>(quic_hint.alternate_port), | 191 static_cast<uint16>(quic_hint.alternate_port), |
186 net::AlternateProtocol::QUIC, | 192 net::AlternateProtocol::QUIC, |
187 1.0f); | 193 1.0f); |
188 } | 194 } |
189 } | 195 } |
190 | 196 |
191 java_init_network_thread.Run(); | 197 java_init_network_thread.Run(); |
198 | |
199 is_context_initialized_ = true; | |
200 while (!tasks_waiting_for_context_.empty()) { | |
201 tasks_waiting_for_context_.front().Run(); | |
202 tasks_waiting_for_context_.pop(); | |
203 } | |
192 } | 204 } |
193 | 205 |
194 void CronetURLRequestContextAdapter::Destroy() { | 206 void CronetURLRequestContextAdapter::Destroy() { |
195 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 207 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
196 // Stick network_thread_ in a local, as |this| may be destroyed from the | 208 // Stick network_thread_ in a local, as |this| may be destroyed from the |
197 // network thread before delete network_thread is called. | 209 // network thread before delete network_thread is called. |
198 base::Thread* network_thread = network_thread_; | 210 base::Thread* network_thread = network_thread_; |
199 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 211 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
200 // Deleting thread stops it after all tasks are completed. | 212 // Deleting thread stops it after all tasks are completed. |
201 delete network_thread; | 213 delete network_thread; |
202 } | 214 } |
203 | 215 |
204 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 216 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
205 if (!context_) { | 217 if (!context_) { |
206 LOG(ERROR) << "URLRequestContext is not set up"; | 218 LOG(ERROR) << "URLRequestContext is not set up"; |
207 } | 219 } |
208 return context_.get(); | 220 return context_.get(); |
209 } | 221 } |
210 | 222 |
223 void CronetURLRequestContextAdapter::PostTaskToNetworkThread( | |
224 const tracked_objects::Location& posted_from, | |
225 const RunAfterContextInitTask& callback) { | |
226 GetNetworkTaskRunner()->PostTask( | |
227 posted_from, base::Bind(&CronetURLRequestContextAdapter:: | |
228 RunTaskAfterContextInitOnNetworkThread, | |
229 base::Unretained(this), callback)); | |
230 } | |
231 | |
232 void CronetURLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( | |
233 const RunAfterContextInitTask& callback) { | |
234 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
235 if (is_context_initialized_) { | |
236 callback.Run(); | |
237 return; | |
238 } | |
239 tasks_waiting_for_context_.push(callback); | |
240 } | |
241 | |
211 scoped_refptr<base::SingleThreadTaskRunner> | 242 scoped_refptr<base::SingleThreadTaskRunner> |
212 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { | 243 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
213 return network_thread_->task_runner(); | 244 return network_thread_->task_runner(); |
214 } | 245 } |
215 | 246 |
216 void CronetURLRequestContextAdapter::StartNetLogToFile( | 247 void CronetURLRequestContextAdapter::StartNetLogToFile( |
217 const std::string& file_name) { | 248 const std::string& file_name) { |
218 GetNetworkTaskRunner()->PostTask( | 249 GetNetworkTaskRunner()->PostTask( |
219 FROM_HERE, | 250 FROM_HERE, |
220 base::Bind( | 251 base::Bind( |
(...skipping 28 matching lines...) Expand all Loading... | |
249 | 280 |
250 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 281 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
251 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 282 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
252 if (net_log_logger_) { | 283 if (net_log_logger_) { |
253 net_log_logger_->StopObserving(); | 284 net_log_logger_->StopObserving(); |
254 net_log_logger_.reset(); | 285 net_log_logger_.reset(); |
255 } | 286 } |
256 } | 287 } |
257 | 288 |
258 } // namespace cronet | 289 } // namespace cronet |
OLD | NEW |