Chromium Code Reviews| Index: components/cronet/android/cronet_url_request_context_adapter.cc |
| diff --git a/components/cronet/android/cronet_url_request_context_adapter.cc b/components/cronet/android/cronet_url_request_context_adapter.cc |
| index 3c2cfa8634dd997f49252b18cbce148c235e541c..4985a7607d11faee89e7e94461536082310c0c00 100644 |
| --- a/components/cronet/android/cronet_url_request_context_adapter.cc |
| +++ b/components/cronet/android/cronet_url_request_context_adapter.cc |
| @@ -4,26 +4,24 @@ |
| #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/android/scoped_java_ref.h" |
| #include "base/bind.h" |
| #include "base/files/file_util.h" |
| #include "base/single_thread_task_runner.h" |
| +#include "base/values.h" |
| #include "components/cronet/url_request_context_config.h" |
| +#include "jni/CronetUrlRequestContext_jni.h" |
|
mmenke
2015/02/17 16:30:17
While you're fixing these, should include base/log
xunjieli
2015/02/17 18:29:01
Done.
|
| #include "net/base/load_flags.h" |
| #include "net/base/net_errors.h" |
| #include "net/base/net_log_logger.h" |
| #include "net/base/network_delegate_impl.h" |
| -#include "net/cert/cert_verifier.h" |
| #include "net/http/http_auth_handler_factory.h" |
| -#include "net/http/http_network_layer.h" |
| -#include "net/http/http_server_properties.h" |
| -#include "net/proxy/proxy_config_service_fixed.h" |
| #include "net/proxy/proxy_service.h" |
| -#include "net/ssl/ssl_config_service_defaults.h" |
| -#include "net/url_request/static_http_user_agent_settings.h" |
| #include "net/url_request/url_request_context.h" |
| #include "net/url_request/url_request_context_builder.h" |
|
mmenke
2015/02/17 16:30:17
Thanks for cleaning these up!
xunjieli
2015/02/17 18:29:01
Sure thing!
|
| -#include "net/url_request/url_request_context_storage.h" |
| -#include "net/url_request/url_request_job_factory_impl.h" |
| + |
| namespace { |
| @@ -108,6 +106,18 @@ class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
| namespace cronet { |
| +// Explicitly register static JNI functions. |
| +bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +// Init network thread on Java side. |
| +void initJavaNetworkThread( |
|
mmenke
2015/02/17 16:30:17
nit: This should be called InitJavaNetworkThread
xunjieli
2015/02/17 18:29:01
Done.
|
| + const base::android::ScopedJavaGlobalRef<jobject>& jowner) { |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + Java_CronetUrlRequestContext_initNetworkThread(env, jowner.obj()); |
| +} |
|
mmenke
2015/02/17 16:30:17
nit: This should go in the anonymous namespace up
xunjieli
2015/02/17 18:29:01
Done.
|
| + |
| CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
| scoped_ptr<URLRequestContextConfig> context_config) |
| : network_thread_(new base::Thread("network")), |
| @@ -125,7 +135,11 @@ CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
| } |
| void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
| - const base::Closure& java_init_network_thread) { |
| + JNIEnv* env, jobject jcaller) { |
| + base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
| + jcaller_ref.Reset(env, jcaller); |
| + base::Closure init_java_network_thread = base::Bind(&initJavaNetworkThread, |
| + jcaller_ref); |
|
mmenke
2015/02/17 16:30:17
Suggest just inlining this.
xunjieli
2015/02/17 18:29:01
Done. I inlined the entire method. Let me know if
|
| proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
| GetNetworkTaskRunner(), nullptr)); |
| GetNetworkTaskRunner()->PostTask( |
| @@ -133,7 +147,7 @@ void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
| base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| base::Unretained(this), |
| Passed(&context_config_), |
| - java_init_network_thread)); |
| + init_java_network_thread)); |
| } |
| void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| @@ -141,11 +155,11 @@ void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| const base::Closure& java_init_network_thread) { |
| DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| DCHECK(!is_context_initialized_); |
| + DCHECK(proxy_config_service_); |
| // TODO(mmenke): Add method to have the builder enable SPDY. |
| net::URLRequestContextBuilder context_builder; |
| context_builder.set_network_delegate(new BasicNetworkDelegate()); |
| - context_builder.set_proxy_config_service( |
| - new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
| + context_builder.set_proxy_config_service(proxy_config_service_.release()); |
|
mmenke
2015/02/17 16:30:17
Was the old code here a bug? If so, seems like it
xunjieli
2015/02/17 18:29:01
Done. Reverted. Will do it in a separate CL.
|
| config->ConfigureURLRequestContextBuilder(&context_builder); |
| context_.reset(context_builder.Build()); |
| @@ -208,7 +222,7 @@ void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| } |
| } |
| -void CronetURLRequestContextAdapter::Destroy() { |
| +void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { |
| DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| // Stick network_thread_ in a local, as |this| may be destroyed from the |
| // network thread before delete network_thread is called. |
| @@ -255,7 +269,9 @@ CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
| } |
| void CronetURLRequestContextAdapter::StartNetLogToFile( |
| - const std::string& file_name) { |
| + JNIEnv* env, jobject jcaller, jstring jfile_name) { |
| + std::string file_name = |
| + base::android::ConvertJavaStringToUTF8(env, jfile_name); |
|
mmenke
2015/02/17 16:30:17
Suggest just inlining this.
xunjieli
2015/02/17 18:29:01
Done.
|
| GetNetworkTaskRunner()->PostTask( |
| FROM_HERE, |
| base::Bind( |
| @@ -264,7 +280,7 @@ void CronetURLRequestContextAdapter::StartNetLogToFile( |
| file_name)); |
| } |
| -void CronetURLRequestContextAdapter::StopNetLog() { |
| +void CronetURLRequestContextAdapter::StopNetLog(JNIEnv* env, jobject jcaller) { |
| GetNetworkTaskRunner()->PostTask( |
| FROM_HERE, |
| base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
| @@ -296,4 +312,29 @@ void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
| } |
| } |
| +// Creates RequestContextAdater if config is valid URLRequestContextConfig, |
| +// returns 0 otherwise. |
| +static jlong CreateRequestContextAdapter(JNIEnv* env, |
| + jclass jcaller, |
| + jobject japp_context, |
| + jstring jconfig) { |
| + std::string config_string = |
| + base::android::ConvertJavaStringToUTF8(env, jconfig); |
| + scoped_ptr<URLRequestContextConfig> context_config( |
| + new URLRequestContextConfig()); |
| + if (!context_config->LoadFromJSON(config_string)) |
| + return 0; |
| + |
| + CronetURLRequestContextAdapter* context_adapter = |
| + new CronetURLRequestContextAdapter(context_config.Pass()); |
| + return reinterpret_cast<jlong>(context_adapter); |
| +} |
| + |
| +static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
| + jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| + // MinLogLevel is global, shared by all URLRequestContexts. |
| + logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| + return old_log_level; |
| +} |
| + |
| } // namespace cronet |