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/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/logging.h" |
9 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/values.h" |
10 #include "components/cronet/url_request_context_config.h" | 14 #include "components/cronet/url_request_context_config.h" |
| 15 #include "jni/CronetUrlRequestContext_jni.h" |
11 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
12 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
13 #include "net/base/net_log_logger.h" | 18 #include "net/base/net_log_logger.h" |
14 #include "net/base/network_delegate_impl.h" | 19 #include "net/base/network_delegate_impl.h" |
15 #include "net/cert/cert_verifier.h" | |
16 #include "net/http/http_auth_handler_factory.h" | 20 #include "net/http/http_auth_handler_factory.h" |
17 #include "net/http/http_network_layer.h" | |
18 #include "net/http/http_server_properties.h" | |
19 #include "net/proxy/proxy_config_service_fixed.h" | 21 #include "net/proxy/proxy_config_service_fixed.h" |
20 #include "net/proxy/proxy_service.h" | 22 #include "net/proxy/proxy_service.h" |
21 #include "net/ssl/ssl_config_service_defaults.h" | |
22 #include "net/url_request/static_http_user_agent_settings.h" | |
23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
24 #include "net/url_request/url_request_context_builder.h" | 24 #include "net/url_request/url_request_context_builder.h" |
25 #include "net/url_request/url_request_context_storage.h" | |
26 #include "net/url_request/url_request_job_factory_impl.h" | |
27 | 25 |
28 namespace { | 26 namespace { |
29 | 27 |
30 class BasicNetworkDelegate : public net::NetworkDelegateImpl { | 28 class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
31 public: | 29 public: |
32 BasicNetworkDelegate() {} | 30 BasicNetworkDelegate() {} |
33 ~BasicNetworkDelegate() override {} | 31 ~BasicNetworkDelegate() override {} |
34 | 32 |
35 private: | 33 private: |
36 // net::NetworkDelegate implementation. | 34 // net::NetworkDelegate implementation. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 return false; | 99 return false; |
102 } | 100 } |
103 | 101 |
104 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 102 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
105 }; | 103 }; |
106 | 104 |
107 } // namespace | 105 } // namespace |
108 | 106 |
109 namespace cronet { | 107 namespace cronet { |
110 | 108 |
| 109 // Explicitly register static JNI functions. |
| 110 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { |
| 111 return RegisterNativesImpl(env); |
| 112 } |
| 113 |
111 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( | 114 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
112 scoped_ptr<URLRequestContextConfig> context_config) | 115 scoped_ptr<URLRequestContextConfig> context_config) |
113 : network_thread_(new base::Thread("network")), | 116 : network_thread_(new base::Thread("network")), |
114 context_config_(context_config.Pass()), | 117 context_config_(context_config.Pass()), |
115 is_context_initialized_(false), | 118 is_context_initialized_(false), |
116 default_load_flags_(net::LOAD_NORMAL) { | 119 default_load_flags_(net::LOAD_NORMAL) { |
117 base::Thread::Options options; | 120 base::Thread::Options options; |
118 options.message_loop_type = base::MessageLoop::TYPE_IO; | 121 options.message_loop_type = base::MessageLoop::TYPE_IO; |
119 network_thread_->StartWithOptions(options); | 122 network_thread_->StartWithOptions(options); |
120 } | 123 } |
121 | 124 |
122 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 125 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
123 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 126 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
124 StopNetLogOnNetworkThread(); | 127 StopNetLogOnNetworkThread(); |
125 } | 128 } |
126 | 129 |
127 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 130 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
128 const base::Closure& java_init_network_thread) { | 131 JNIEnv* env, |
| 132 jobject jcaller) { |
| 133 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
| 134 jcaller_ref.Reset(env, jcaller); |
129 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 135 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
130 GetNetworkTaskRunner(), nullptr)); | 136 GetNetworkTaskRunner(), nullptr)); |
131 GetNetworkTaskRunner()->PostTask( | 137 GetNetworkTaskRunner()->PostTask( |
132 FROM_HERE, | 138 FROM_HERE, |
133 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 139 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
134 base::Unretained(this), | 140 base::Unretained(this), Passed(&context_config_), |
135 Passed(&context_config_), | 141 jcaller_ref)); |
136 java_init_network_thread)); | |
137 } | 142 } |
138 | 143 |
139 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 144 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
140 scoped_ptr<URLRequestContextConfig> config, | 145 scoped_ptr<URLRequestContextConfig> config, |
141 const base::Closure& java_init_network_thread) { | 146 const base::android::ScopedJavaGlobalRef<jobject>& |
| 147 jcronet_url_request_context) { |
142 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 148 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
143 DCHECK(!is_context_initialized_); | 149 DCHECK(!is_context_initialized_); |
144 // TODO(mmenke): Add method to have the builder enable SPDY. | 150 // TODO(mmenke): Add method to have the builder enable SPDY. |
145 net::URLRequestContextBuilder context_builder; | 151 net::URLRequestContextBuilder context_builder; |
146 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 152 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
147 context_builder.set_proxy_config_service( | 153 context_builder.set_proxy_config_service( |
148 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 154 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
149 config->ConfigureURLRequestContextBuilder(&context_builder); | 155 config->ConfigureURLRequestContextBuilder(&context_builder); |
150 | 156 |
151 context_.reset(context_builder.Build()); | 157 context_.reset(context_builder.Build()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 net::HostPortPair quic_hint_host_port_pair(canon_host, | 198 net::HostPortPair quic_hint_host_port_pair(canon_host, |
193 quic_hint.port); | 199 quic_hint.port); |
194 context_->http_server_properties()->SetAlternateProtocol( | 200 context_->http_server_properties()->SetAlternateProtocol( |
195 quic_hint_host_port_pair, | 201 quic_hint_host_port_pair, |
196 static_cast<uint16>(quic_hint.alternate_port), | 202 static_cast<uint16>(quic_hint.alternate_port), |
197 net::AlternateProtocol::QUIC, | 203 net::AlternateProtocol::QUIC, |
198 1.0f); | 204 1.0f); |
199 } | 205 } |
200 } | 206 } |
201 | 207 |
202 java_init_network_thread.Run(); | 208 JNIEnv* env = base::android::AttachCurrentThread(); |
| 209 Java_CronetUrlRequestContext_initNetworkThread( |
| 210 env, jcronet_url_request_context.obj()); |
203 | 211 |
204 is_context_initialized_ = true; | 212 is_context_initialized_ = true; |
205 while (!tasks_waiting_for_context_.empty()) { | 213 while (!tasks_waiting_for_context_.empty()) { |
206 tasks_waiting_for_context_.front().Run(); | 214 tasks_waiting_for_context_.front().Run(); |
207 tasks_waiting_for_context_.pop(); | 215 tasks_waiting_for_context_.pop(); |
208 } | 216 } |
209 } | 217 } |
210 | 218 |
211 void CronetURLRequestContextAdapter::Destroy() { | 219 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { |
212 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 220 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
213 // Stick network_thread_ in a local, as |this| may be destroyed from the | 221 // Stick network_thread_ in a local, as |this| may be destroyed from the |
214 // network thread before delete network_thread is called. | 222 // network thread before delete network_thread is called. |
215 base::Thread* network_thread = network_thread_; | 223 base::Thread* network_thread = network_thread_; |
216 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 224 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
217 // Deleting thread stops it after all tasks are completed. | 225 // Deleting thread stops it after all tasks are completed. |
218 delete network_thread; | 226 delete network_thread; |
219 } | 227 } |
220 | 228 |
221 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 229 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
(...skipping 25 matching lines...) Expand all Loading... |
247 | 255 |
248 bool CronetURLRequestContextAdapter::IsOnNetworkThread() const { | 256 bool CronetURLRequestContextAdapter::IsOnNetworkThread() const { |
249 return GetNetworkTaskRunner()->BelongsToCurrentThread(); | 257 return GetNetworkTaskRunner()->BelongsToCurrentThread(); |
250 } | 258 } |
251 | 259 |
252 scoped_refptr<base::SingleThreadTaskRunner> | 260 scoped_refptr<base::SingleThreadTaskRunner> |
253 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { | 261 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
254 return network_thread_->task_runner(); | 262 return network_thread_->task_runner(); |
255 } | 263 } |
256 | 264 |
257 void CronetURLRequestContextAdapter::StartNetLogToFile( | 265 void CronetURLRequestContextAdapter::StartNetLogToFile(JNIEnv* env, |
258 const std::string& file_name) { | 266 jobject jcaller, |
| 267 jstring jfile_name) { |
259 GetNetworkTaskRunner()->PostTask( | 268 GetNetworkTaskRunner()->PostTask( |
260 FROM_HERE, | 269 FROM_HERE, |
261 base::Bind( | 270 base::Bind( |
262 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, | 271 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, |
263 base::Unretained(this), | 272 base::Unretained(this), |
264 file_name)); | 273 base::android::ConvertJavaStringToUTF8(env, jfile_name))); |
265 } | 274 } |
266 | 275 |
267 void CronetURLRequestContextAdapter::StopNetLog() { | 276 void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) { |
268 GetNetworkTaskRunner()->PostTask( | 277 GetNetworkTaskRunner()->PostTask( |
269 FROM_HERE, | 278 FROM_HERE, |
270 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, | 279 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
271 base::Unretained(this))); | 280 base::Unretained(this))); |
272 } | 281 } |
273 | 282 |
274 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( | 283 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
275 const std::string& file_name) { | 284 const std::string& file_name) { |
276 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 285 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
277 // Do nothing if already logging to a file. | 286 // Do nothing if already logging to a file. |
(...skipping 11 matching lines...) Expand all Loading... |
289 } | 298 } |
290 | 299 |
291 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 300 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
292 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 301 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
293 if (net_log_logger_) { | 302 if (net_log_logger_) { |
294 net_log_logger_->StopObserving(); | 303 net_log_logger_->StopObserving(); |
295 net_log_logger_.reset(); | 304 net_log_logger_.reset(); |
296 } | 305 } |
297 } | 306 } |
298 | 307 |
| 308 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
| 309 // returns 0 otherwise. |
| 310 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 311 jclass jcaller, |
| 312 jobject japp_context, |
| 313 jstring jconfig) { |
| 314 std::string config_string = |
| 315 base::android::ConvertJavaStringToUTF8(env, jconfig); |
| 316 scoped_ptr<URLRequestContextConfig> context_config( |
| 317 new URLRequestContextConfig()); |
| 318 if (!context_config->LoadFromJSON(config_string)) |
| 319 return 0; |
| 320 |
| 321 CronetURLRequestContextAdapter* context_adapter = |
| 322 new CronetURLRequestContextAdapter(context_config.Pass()); |
| 323 return reinterpret_cast<jlong>(context_adapter); |
| 324 } |
| 325 |
| 326 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
| 327 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| 328 // MinLogLevel is global, shared by all URLRequestContexts. |
| 329 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 330 return old_log_level; |
| 331 } |
| 332 |
299 } // namespace cronet | 333 } // namespace cronet |
OLD | NEW |