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" | |
9 #include "base/android/scoped_java_ref.h" | |
7 #include "base/bind.h" | 10 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 11 #include "base/files/file_util.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" | |
20 #include "net/proxy/proxy_service.h" | 21 #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" | 22 #include "net/url_request/url_request_context.h" |
24 #include "net/url_request/url_request_context_builder.h" | 23 #include "net/url_request/url_request_context_builder.h" |
25 #include "net/url_request/url_request_context_storage.h" | 24 |
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 | |
114 // Init network thread on Java side. | |
115 void initJavaNetworkThread( | |
116 const base::android::ScopedJavaGlobalRef<jobject>& jowner) { | |
117 JNIEnv* env = base::android::AttachCurrentThread(); | |
118 Java_CronetUrlRequestContext_initNetworkThread(env, jowner.obj()); | |
119 } | |
120 | |
111 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( | 121 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
112 scoped_ptr<URLRequestContextConfig> context_config) | 122 scoped_ptr<URLRequestContextConfig> context_config) |
113 : network_thread_(new base::Thread("network")), | 123 : network_thread_(new base::Thread("network")), |
114 context_config_(context_config.Pass()), | 124 context_config_(context_config.Pass()), |
115 is_context_initialized_(false), | 125 is_context_initialized_(false), |
116 default_load_flags_(net::LOAD_NORMAL) { | 126 default_load_flags_(net::LOAD_NORMAL) { |
117 base::Thread::Options options; | 127 base::Thread::Options options; |
118 options.message_loop_type = base::MessageLoop::TYPE_IO; | 128 options.message_loop_type = base::MessageLoop::TYPE_IO; |
119 network_thread_->StartWithOptions(options); | 129 network_thread_->StartWithOptions(options); |
120 } | 130 } |
121 | 131 |
122 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 132 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
123 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 133 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
124 StopNetLogOnNetworkThread(); | 134 StopNetLogOnNetworkThread(); |
125 } | 135 } |
126 | 136 |
127 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 137 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
128 const base::Closure& java_init_network_thread) { | 138 JNIEnv* env, jobject jcaller) { |
139 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | |
140 jcaller_ref.Reset(env, jcaller); | |
141 base::Closure init_java_network_thread = base::Bind(&initJavaNetworkThread, | |
142 jcaller_ref); | |
129 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 143 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
130 GetNetworkTaskRunner(), nullptr)); | 144 GetNetworkTaskRunner(), nullptr)); |
131 GetNetworkTaskRunner()->PostTask( | 145 GetNetworkTaskRunner()->PostTask( |
132 FROM_HERE, | 146 FROM_HERE, |
133 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 147 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
134 base::Unretained(this), | 148 base::Unretained(this), |
135 Passed(&context_config_), | 149 Passed(&context_config_), |
136 java_init_network_thread)); | 150 init_java_network_thread)); |
137 } | 151 } |
138 | 152 |
139 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 153 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
140 scoped_ptr<URLRequestContextConfig> config, | 154 scoped_ptr<URLRequestContextConfig> config, |
141 const base::Closure& java_init_network_thread) { | 155 const base::Closure& java_init_network_thread) { |
142 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 156 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
143 DCHECK(!is_context_initialized_); | 157 DCHECK(!is_context_initialized_); |
158 DCHECK(proxy_config_service_); | |
144 // TODO(mmenke): Add method to have the builder enable SPDY. | 159 // TODO(mmenke): Add method to have the builder enable SPDY. |
145 net::URLRequestContextBuilder context_builder; | 160 net::URLRequestContextBuilder context_builder; |
146 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 161 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
147 context_builder.set_proxy_config_service( | 162 context_builder.set_proxy_config_service(proxy_config_service_.release()); |
148 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | |
149 config->ConfigureURLRequestContextBuilder(&context_builder); | 163 config->ConfigureURLRequestContextBuilder(&context_builder); |
150 | 164 |
151 context_.reset(context_builder.Build()); | 165 context_.reset(context_builder.Build()); |
152 | 166 |
153 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 167 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
154 net::LOAD_DO_NOT_SEND_COOKIES; | 168 net::LOAD_DO_NOT_SEND_COOKIES; |
155 if (config->load_disable_cache) | 169 if (config->load_disable_cache) |
156 default_load_flags_ |= net::LOAD_DISABLE_CACHE; | 170 default_load_flags_ |= net::LOAD_DISABLE_CACHE; |
157 | 171 |
158 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 172 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 | 215 |
202 java_init_network_thread.Run(); | 216 java_init_network_thread.Run(); |
203 | 217 |
204 is_context_initialized_ = true; | 218 is_context_initialized_ = true; |
205 while (!tasks_waiting_for_context_.empty()) { | 219 while (!tasks_waiting_for_context_.empty()) { |
206 tasks_waiting_for_context_.front().Run(); | 220 tasks_waiting_for_context_.front().Run(); |
207 tasks_waiting_for_context_.pop(); | 221 tasks_waiting_for_context_.pop(); |
208 } | 222 } |
209 } | 223 } |
210 | 224 |
211 void CronetURLRequestContextAdapter::Destroy() { | 225 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { |
212 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 226 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
213 // Stick network_thread_ in a local, as |this| may be destroyed from the | 227 // Stick network_thread_ in a local, as |this| may be destroyed from the |
214 // network thread before delete network_thread is called. | 228 // network thread before delete network_thread is called. |
215 base::Thread* network_thread = network_thread_; | 229 base::Thread* network_thread = network_thread_; |
216 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 230 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
217 // Deleting thread stops it after all tasks are completed. | 231 // Deleting thread stops it after all tasks are completed. |
218 delete network_thread; | 232 delete network_thread; |
219 } | 233 } |
220 | 234 |
221 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 235 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
(...skipping 26 matching lines...) Expand all Loading... | |
248 bool CronetURLRequestContextAdapter::IsOnNetworkThread() const { | 262 bool CronetURLRequestContextAdapter::IsOnNetworkThread() const { |
249 return GetNetworkTaskRunner()->BelongsToCurrentThread(); | 263 return GetNetworkTaskRunner()->BelongsToCurrentThread(); |
250 } | 264 } |
251 | 265 |
252 scoped_refptr<base::SingleThreadTaskRunner> | 266 scoped_refptr<base::SingleThreadTaskRunner> |
253 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { | 267 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
254 return network_thread_->task_runner(); | 268 return network_thread_->task_runner(); |
255 } | 269 } |
256 | 270 |
257 void CronetURLRequestContextAdapter::StartNetLogToFile( | 271 void CronetURLRequestContextAdapter::StartNetLogToFile( |
258 const std::string& file_name) { | 272 JNIEnv* env, jobject jcaller, jstring jfile_name) { |
273 std::string file_name = | |
274 base::android::ConvertJavaStringToUTF8(env, jfile_name); | |
259 GetNetworkTaskRunner()->PostTask( | 275 GetNetworkTaskRunner()->PostTask( |
260 FROM_HERE, | 276 FROM_HERE, |
261 base::Bind( | 277 base::Bind( |
262 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, | 278 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, |
263 base::Unretained(this), | 279 base::Unretained(this), |
264 file_name)); | 280 file_name)); |
265 } | 281 } |
266 | 282 |
267 void CronetURLRequestContextAdapter::StopNetLog() { | 283 void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) { |
268 GetNetworkTaskRunner()->PostTask( | 284 GetNetworkTaskRunner()->PostTask( |
269 FROM_HERE, | 285 FROM_HERE, |
270 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, | 286 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
271 base::Unretained(this))); | 287 base::Unretained(this))); |
272 } | 288 } |
273 | 289 |
274 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( | 290 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
275 const std::string& file_name) { | 291 const std::string& file_name) { |
276 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 292 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
277 // Do nothing if already logging to a file. | 293 // Do nothing if already logging to a file. |
(...skipping 11 matching lines...) Expand all Loading... | |
289 } | 305 } |
290 | 306 |
291 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 307 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
292 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 308 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
293 if (net_log_logger_) { | 309 if (net_log_logger_) { |
294 net_log_logger_->StopObserving(); | 310 net_log_logger_->StopObserving(); |
295 net_log_logger_.reset(); | 311 net_log_logger_.reset(); |
296 } | 312 } |
297 } | 313 } |
298 | 314 |
315 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | |
316 // returns 0 otherwise. | |
317 static jlong CreateRequestContextAdapter(JNIEnv* env, | |
318 jobject jcaller, | |
mef
2015/02/17 10:02:43
jcaller doesn't seem to be used here and below. Do
xunjieli
2015/02/17 15:54:09
Done. Good catch! We should probably make them sta
| |
319 jobject japp_context, | |
320 jstring jconfig) { | |
321 std::string config_string = | |
322 base::android::ConvertJavaStringToUTF8(env, jconfig); | |
323 scoped_ptr<URLRequestContextConfig> context_config( | |
324 new URLRequestContextConfig()); | |
325 if (!context_config->LoadFromJSON(config_string)) | |
326 return 0; | |
327 | |
328 CronetURLRequestContextAdapter* context_adapter = | |
329 new CronetURLRequestContextAdapter(context_config.Pass()); | |
330 return reinterpret_cast<jlong>(context_adapter); | |
331 } | |
332 | |
333 static jint SetMinLogLevel(JNIEnv* env, jobject jcaller, jint jlog_level) { | |
334 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | |
335 // MinLogLevel is global, shared by all URLRequestContexts. | |
336 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | |
337 return old_log_level; | |
338 } | |
339 | |
299 } // namespace cronet | 340 } // namespace cronet |
OLD | NEW |