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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 context_config_(context_config.Pass()), |
| 115 is_context_initialized_(false), |
| 116 default_load_flags_(net::LOAD_NORMAL) { |
| 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(), nullptr)); |
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()); |
| 143 DCHECK(!is_context_initialized_); |
140 // TODO(mmenke): Add method to have the builder enable SPDY. | 144 // TODO(mmenke): Add method to have the builder enable SPDY. |
141 net::URLRequestContextBuilder context_builder; | 145 net::URLRequestContextBuilder context_builder; |
142 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 146 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
143 context_builder.set_proxy_config_service( | 147 context_builder.set_proxy_config_service( |
144 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 148 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
145 config->ConfigureURLRequestContextBuilder(&context_builder); | 149 config->ConfigureURLRequestContextBuilder(&context_builder); |
146 | 150 |
147 context_.reset(context_builder.Build()); | 151 context_.reset(context_builder.Build()); |
148 | 152 |
149 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 153 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 quic_hint.port); | 193 quic_hint.port); |
190 context_->http_server_properties()->SetAlternateProtocol( | 194 context_->http_server_properties()->SetAlternateProtocol( |
191 quic_hint_host_port_pair, | 195 quic_hint_host_port_pair, |
192 static_cast<uint16>(quic_hint.alternate_port), | 196 static_cast<uint16>(quic_hint.alternate_port), |
193 net::AlternateProtocol::QUIC, | 197 net::AlternateProtocol::QUIC, |
194 1.0f); | 198 1.0f); |
195 } | 199 } |
196 } | 200 } |
197 | 201 |
198 java_init_network_thread.Run(); | 202 java_init_network_thread.Run(); |
| 203 |
| 204 is_context_initialized_ = true; |
| 205 while (!tasks_waiting_for_context_.empty()) { |
| 206 tasks_waiting_for_context_.front().Run(); |
| 207 tasks_waiting_for_context_.pop(); |
| 208 } |
199 } | 209 } |
200 | 210 |
201 void CronetURLRequestContextAdapter::Destroy() { | 211 void CronetURLRequestContextAdapter::Destroy() { |
202 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 212 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
203 // Stick network_thread_ in a local, as |this| may be destroyed from the | 213 // Stick network_thread_ in a local, as |this| may be destroyed from the |
204 // network thread before delete network_thread is called. | 214 // network thread before delete network_thread is called. |
205 base::Thread* network_thread = network_thread_; | 215 base::Thread* network_thread = network_thread_; |
206 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 216 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
207 // Deleting thread stops it after all tasks are completed. | 217 // Deleting thread stops it after all tasks are completed. |
208 delete network_thread; | 218 delete network_thread; |
209 } | 219 } |
210 | 220 |
211 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 221 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
212 if (!context_) { | 222 if (!context_) { |
213 LOG(ERROR) << "URLRequestContext is not set up"; | 223 LOG(ERROR) << "URLRequestContext is not set up"; |
214 } | 224 } |
215 return context_.get(); | 225 return context_.get(); |
216 } | 226 } |
217 | 227 |
| 228 void CronetURLRequestContextAdapter::PostTaskToNetworkThread( |
| 229 const tracked_objects::Location& posted_from, |
| 230 const base::Closure& callback) { |
| 231 GetNetworkTaskRunner()->PostTask( |
| 232 posted_from, base::Bind(&CronetURLRequestContextAdapter:: |
| 233 RunTaskAfterContextInitOnNetworkThread, |
| 234 base::Unretained(this), callback)); |
| 235 } |
| 236 |
| 237 void CronetURLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( |
| 238 const base::Closure& task_to_run_after_context_init) { |
| 239 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 240 if (is_context_initialized_) { |
| 241 DCHECK(tasks_waiting_for_context_.empty()); |
| 242 task_to_run_after_context_init.Run(); |
| 243 return; |
| 244 } |
| 245 tasks_waiting_for_context_.push(task_to_run_after_context_init); |
| 246 } |
| 247 |
| 248 bool CronetURLRequestContextAdapter::IsOnNetworkThread() const { |
| 249 return GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| 250 } |
| 251 |
218 scoped_refptr<base::SingleThreadTaskRunner> | 252 scoped_refptr<base::SingleThreadTaskRunner> |
219 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { | 253 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
220 return network_thread_->task_runner(); | 254 return network_thread_->task_runner(); |
221 } | 255 } |
222 | 256 |
223 void CronetURLRequestContextAdapter::StartNetLogToFile( | 257 void CronetURLRequestContextAdapter::StartNetLogToFile( |
224 const std::string& file_name) { | 258 const std::string& file_name) { |
225 GetNetworkTaskRunner()->PostTask( | 259 GetNetworkTaskRunner()->PostTask( |
226 FROM_HERE, | 260 FROM_HERE, |
227 base::Bind( | 261 base::Bind( |
(...skipping 28 matching lines...) Expand all Loading... |
256 | 290 |
257 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 291 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
258 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 292 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
259 if (net_log_logger_) { | 293 if (net_log_logger_) { |
260 net_log_logger_->StopObserving(); | 294 net_log_logger_->StopObserving(); |
261 net_log_logger_.reset(); | 295 net_log_logger_.reset(); |
262 } | 296 } |
263 } | 297 } |
264 | 298 |
265 } // namespace cronet | 299 } // namespace cronet |
OLD | NEW |