| 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/chromium_url_request_context.h" | 5 #include "components/cronet/android/chromium_url_request_context.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 namespace cronet { | 56 namespace cronet { |
| 57 | 57 |
| 58 // Explicitly register static JNI functions. | 58 // Explicitly register static JNI functions. |
| 59 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { | 59 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { |
| 60 return RegisterNativesImpl(env); | 60 return RegisterNativesImpl(env); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Sets global user-agent to be used for all subsequent requests. | 63 // Sets global user-agent to be used for all subsequent requests. |
| 64 static jlong CreateRequestContextAdapter(JNIEnv* env, | 64 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 65 jobject jcaller, | 65 jobject jcaller, |
| 66 jobject japp_context, | |
| 67 jstring juser_agent, | 66 jstring juser_agent, |
| 68 jint jlog_level, | 67 jint jlog_level, |
| 69 jstring jconfig) { | 68 jstring jconfig) { |
| 70 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent); | 69 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent); |
| 71 | 70 |
| 72 std::string config = ConvertJavaStringToUTF8(env, jconfig); | 71 std::string config = ConvertJavaStringToUTF8(env, jconfig); |
| 73 | 72 |
| 74 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config)); | 73 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config)); |
| 75 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { | 74 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 76 DLOG(ERROR) << "Bad JSON: " << config; | 75 DLOG(ERROR) << "Bad JSON: " << config; |
| 77 return 0; | 76 return 0; |
| 78 } | 77 } |
| 79 | 78 |
| 80 scoped_ptr<URLRequestContextConfig> context_config( | 79 scoped_ptr<URLRequestContextConfig> context_config( |
| 81 new URLRequestContextConfig()); | 80 new URLRequestContextConfig()); |
| 82 base::JSONValueConverter<URLRequestContextConfig> converter; | 81 base::JSONValueConverter<URLRequestContextConfig> converter; |
| 83 if (!converter.Convert(*config_value, context_config.get())) { | 82 if (!converter.Convert(*config_value, context_config.get())) { |
| 84 DLOG(ERROR) << "Bad Config: " << config_value; | 83 DLOG(ERROR) << "Bad Config: " << config_value; |
| 85 return 0; | 84 return 0; |
| 86 } | 85 } |
| 87 | 86 |
| 88 // Set application context. | |
| 89 base::android::ScopedJavaLocalRef<jobject> scoped_app_context(env, | |
| 90 japp_context); | |
| 91 base::android::InitApplicationContext(env, scoped_app_context); | |
| 92 | |
| 93 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. | 87 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. |
| 94 // Revisit this if each URLRequestContext would need an individual log level. | 88 // Revisit this if each URLRequestContext would need an individual log level. |
| 95 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 89 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 96 | 90 |
| 97 // TODO(dplotnikov): set application context. | 91 // TODO(dplotnikov): set application context. |
| 98 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter( | 92 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter( |
| 99 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent); | 93 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent); |
| 100 context_adapter->AddRef(); // Hold onto this ref-counted object. | 94 context_adapter->AddRef(); // Hold onto this ref-counted object. |
| 101 context_adapter->Initialize(context_config.Pass()); | 95 context_adapter->Initialize(context_config.Pass()); |
| 102 return reinterpret_cast<jlong>(context_adapter); | 96 return reinterpret_cast<jlong>(context_adapter); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 // Called on application's main Java thread. | 148 // Called on application's main Java thread. |
| 155 static void InitRequestContextOnMainThread(JNIEnv* env, | 149 static void InitRequestContextOnMainThread(JNIEnv* env, |
| 156 jobject jcaller, | 150 jobject jcaller, |
| 157 jlong jurl_request_context_adapter) { | 151 jlong jurl_request_context_adapter) { |
| 158 URLRequestContextAdapter* context_adapter = | 152 URLRequestContextAdapter* context_adapter = |
| 159 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | 153 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); |
| 160 context_adapter->InitRequestContextOnMainThread(); | 154 context_adapter->InitRequestContextOnMainThread(); |
| 161 } | 155 } |
| 162 | 156 |
| 163 } // namespace cronet | 157 } // namespace cronet |
| OLD | NEW |