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()); |
mmenke
2015/02/02 19:30:26
DCHECK(!is_context_initialized_);?
mef
2015/02/03 01:28:57
Done.
| |
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()); |
143 context_builder.set_proxy_config_service( | 146 context_builder.set_proxy_config_service( |
144 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 147 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
145 config->ConfigureURLRequestContextBuilder(&context_builder); | 148 config->ConfigureURLRequestContextBuilder(&context_builder); |
146 | 149 |
147 context_.reset(context_builder.Build()); | 150 context_.reset(context_builder.Build()); |
148 | 151 |
149 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 152 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); | 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 base::Closure& 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 base::Closure& callback) { | |
238 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
239 if (is_context_initialized_) { | |
mmenke
2015/02/02 19:30:26
Maybe DCHECK(tasks_waiting_for_context_.empty());?
mef
2015/02/03 01:28:57
Done.
| |
240 callback.Run(); | |
241 return; | |
242 } | |
243 tasks_waiting_for_context_.push(callback); | |
244 } | |
245 | |
246 bool CronetURLRequestContextAdapter::IsOnNetworkThread() const { | |
247 return GetNetworkTaskRunner()->BelongsToCurrentThread(); | |
248 } | |
249 | |
218 scoped_refptr<base::SingleThreadTaskRunner> | 250 scoped_refptr<base::SingleThreadTaskRunner> |
219 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { | 251 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
220 return network_thread_->task_runner(); | 252 return network_thread_->task_runner(); |
221 } | 253 } |
222 | 254 |
223 void CronetURLRequestContextAdapter::StartNetLogToFile( | 255 void CronetURLRequestContextAdapter::StartNetLogToFile( |
224 const std::string& file_name) { | 256 const std::string& file_name) { |
225 GetNetworkTaskRunner()->PostTask( | 257 GetNetworkTaskRunner()->PostTask( |
226 FROM_HERE, | 258 FROM_HERE, |
227 base::Bind( | 259 base::Bind( |
(...skipping 28 matching lines...) Expand all Loading... | |
256 | 288 |
257 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 289 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
258 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 290 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
259 if (net_log_logger_) { | 291 if (net_log_logger_) { |
260 net_log_logger_->StopObserving(); | 292 net_log_logger_->StopObserving(); |
261 net_log_logger_.reset(); | 293 net_log_logger_.reset(); |
262 } | 294 } |
263 } | 295 } |
264 | 296 |
265 } // namespace cronet | 297 } // namespace cronet |
OLD | NEW |