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 scoped_ptr<URLRequestContextConfig> context_config) |
| 112 : network_thread_(new base::Thread("network")), |
| 113 is_context_initialized_(false) { |
| 114 context_config_.reset(context_config.release()); |
| 115 |
| 116 base::Thread::Options options; |
| 117 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 118 network_thread_->StartWithOptions(options); |
111 } | 119 } |
112 | 120 |
113 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 121 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
114 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 122 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
115 StopNetLogOnNetworkThread(); | 123 StopNetLogOnNetworkThread(); |
116 } | 124 } |
117 | 125 |
118 void CronetURLRequestContextAdapter::Initialize( | 126 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
119 scoped_ptr<URLRequestContextConfig> config, | |
120 const base::Closure& java_init_network_thread) { | 127 const base::Closure& java_init_network_thread) { |
121 network_thread_ = new base::Thread("network"); | 128 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
122 base::Thread::Options options; | 129 GetNetworkTaskRunner(), NULL)); |
123 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
124 network_thread_->StartWithOptions(options); | |
125 | |
126 GetNetworkTaskRunner()->PostTask( | 130 GetNetworkTaskRunner()->PostTask( |
127 FROM_HERE, | 131 FROM_HERE, |
128 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 132 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
129 base::Unretained(this), | 133 base::Unretained(this), |
130 Passed(&config), | 134 Passed(&context_config_), |
131 java_init_network_thread)); | 135 java_init_network_thread)); |
132 } | 136 } |
133 | 137 |
134 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 138 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
135 scoped_ptr<URLRequestContextConfig> config, | 139 scoped_ptr<URLRequestContextConfig> config, |
136 const base::Closure& java_init_network_thread) { | 140 const base::Closure& java_init_network_thread) { |
137 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 141 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
138 // TODO(mmenke): Add method to have the builder enable SPDY. | 142 // TODO(mmenke): Add method to have the builder enable SPDY. |
139 net::URLRequestContextBuilder context_builder; | 143 net::URLRequestContextBuilder context_builder; |
140 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 144 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 quic_hint.port); | 186 quic_hint.port); |
183 context_->http_server_properties()->SetAlternateProtocol( | 187 context_->http_server_properties()->SetAlternateProtocol( |
184 quic_hint_host_port_pair, | 188 quic_hint_host_port_pair, |
185 static_cast<uint16>(quic_hint.alternate_port), | 189 static_cast<uint16>(quic_hint.alternate_port), |
186 net::AlternateProtocol::QUIC, | 190 net::AlternateProtocol::QUIC, |
187 1.0f); | 191 1.0f); |
188 } | 192 } |
189 } | 193 } |
190 | 194 |
191 java_init_network_thread.Run(); | 195 java_init_network_thread.Run(); |
| 196 |
| 197 is_context_initialized_ = true; |
| 198 while (!tasks_waiting_for_context_.empty()) { |
| 199 tasks_waiting_for_context_.front().Run(); |
| 200 tasks_waiting_for_context_.pop(); |
| 201 } |
192 } | 202 } |
193 | 203 |
194 void CronetURLRequestContextAdapter::Destroy() { | 204 void CronetURLRequestContextAdapter::Destroy() { |
195 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 205 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
196 // Stick network_thread_ in a local, as |this| may be destroyed from the | 206 // Stick network_thread_ in a local, as |this| may be destroyed from the |
197 // network thread before delete network_thread is called. | 207 // network thread before delete network_thread is called. |
198 base::Thread* network_thread = network_thread_; | 208 base::Thread* network_thread = network_thread_; |
199 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 209 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
200 // Deleting thread stops it after all tasks are completed. | 210 // Deleting thread stops it after all tasks are completed. |
201 delete network_thread; | 211 delete network_thread; |
202 } | 212 } |
203 | 213 |
204 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 214 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
205 if (!context_) { | 215 if (!context_) { |
206 LOG(ERROR) << "URLRequestContext is not set up"; | 216 LOG(ERROR) << "URLRequestContext is not set up"; |
207 } | 217 } |
208 return context_.get(); | 218 return context_.get(); |
209 } | 219 } |
210 | 220 |
| 221 void CronetURLRequestContextAdapter::PostTaskToNetworkThread( |
| 222 const tracked_objects::Location& posted_from, |
| 223 const RunAfterContextInitTask& callback) { |
| 224 GetNetworkTaskRunner()->PostTask( |
| 225 posted_from, base::Bind(&CronetURLRequestContextAdapter:: |
| 226 RunTaskAfterContextInitOnNetworkThread, |
| 227 base::Unretained(this), callback)); |
| 228 } |
| 229 |
| 230 void CronetURLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( |
| 231 const RunAfterContextInitTask& callback) { |
| 232 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 233 if (is_context_initialized_) { |
| 234 callback.Run(); |
| 235 return; |
| 236 } |
| 237 tasks_waiting_for_context_.push(callback); |
| 238 } |
| 239 |
211 scoped_refptr<base::SingleThreadTaskRunner> | 240 scoped_refptr<base::SingleThreadTaskRunner> |
212 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { | 241 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
213 return network_thread_->task_runner(); | 242 return network_thread_->task_runner(); |
214 } | 243 } |
215 | 244 |
216 void CronetURLRequestContextAdapter::StartNetLogToFile( | 245 void CronetURLRequestContextAdapter::StartNetLogToFile( |
217 const std::string& file_name) { | 246 const std::string& file_name) { |
218 GetNetworkTaskRunner()->PostTask( | 247 GetNetworkTaskRunner()->PostTask( |
219 FROM_HERE, | 248 FROM_HERE, |
220 base::Bind( | 249 base::Bind( |
(...skipping 28 matching lines...) Expand all Loading... |
249 | 278 |
250 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 279 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
251 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 280 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
252 if (net_log_logger_) { | 281 if (net_log_logger_) { |
253 net_log_logger_->StopObserving(); | 282 net_log_logger_->StopObserving(); |
254 net_log_logger_.reset(); | 283 net_log_logger_.reset(); |
255 } | 284 } |
256 } | 285 } |
257 | 286 |
258 } // namespace cronet | 287 } // namespace cronet |
OLD | NEW |