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.h" | 5 #include "components/cronet/android/cronet_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 15 matching lines...) Expand all Loading... |
26 const base::android::ScopedJavaGlobalRef<jobject>& jowner) { | 26 const base::android::ScopedJavaGlobalRef<jobject>& jowner) { |
27 JNIEnv* env = base::android::AttachCurrentThread(); | 27 JNIEnv* env = base::android::AttachCurrentThread(); |
28 Java_CronetUrlRequestContext_initNetworkThread(env, jowner.obj()); | 28 Java_CronetUrlRequestContext_initNetworkThread(env, jowner.obj()); |
29 } | 29 } |
30 | 30 |
31 // Explicitly register static JNI functions. | 31 // Explicitly register static JNI functions. |
32 bool CronetUrlRequestContextRegisterJni(JNIEnv* env) { | 32 bool CronetUrlRequestContextRegisterJni(JNIEnv* env) { |
33 return RegisterNativesImpl(env); | 33 return RegisterNativesImpl(env); |
34 } | 34 } |
35 | 35 |
36 // Sets global user-agent to be used for all subsequent requests. | 36 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
| 37 // returns 0 otherwise. |
37 static jlong CreateRequestContextAdapter(JNIEnv* env, | 38 static jlong CreateRequestContextAdapter(JNIEnv* env, |
38 jobject jcaller, | 39 jobject jcaller, |
39 jobject japp_context, | 40 jobject japp_context, |
40 jstring jconfig) { | 41 jstring jconfig) { |
41 std::string config_string = | 42 std::string config_string = |
42 base::android::ConvertJavaStringToUTF8(env, jconfig); | 43 base::android::ConvertJavaStringToUTF8(env, jconfig); |
43 scoped_ptr<URLRequestContextConfig> context_config( | 44 scoped_ptr<URLRequestContextConfig> context_config( |
44 new URLRequestContextConfig()); | 45 new URLRequestContextConfig()); |
45 if (!context_config->LoadFromJSON(config_string)) | 46 if (!context_config->LoadFromJSON(config_string)) |
46 return 0; | 47 return 0; |
47 | 48 |
48 // Set application context. | |
49 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, japp_context); | |
50 base::android::InitApplicationContext(env, scoped_context); | |
51 | |
52 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | |
53 jcaller_ref.Reset(env, jcaller); | |
54 | |
55 CronetURLRequestContextAdapter* context_adapter = | 49 CronetURLRequestContextAdapter* context_adapter = |
56 new CronetURLRequestContextAdapter(); | 50 new CronetURLRequestContextAdapter(context_config.Pass()); |
57 base::Closure init_java_network_thread = base::Bind(&initJavaNetworkThread, | |
58 jcaller_ref); | |
59 context_adapter->Initialize(context_config.Pass(), init_java_network_thread); | |
60 | |
61 return reinterpret_cast<jlong>(context_adapter); | 51 return reinterpret_cast<jlong>(context_adapter); |
62 } | 52 } |
63 | 53 |
64 // Destroys native objects. | 54 // Destroys native objects. |
65 static void DestroyRequestContextAdapter(JNIEnv* env, | 55 static void DestroyRequestContextAdapter(JNIEnv* env, |
66 jobject jcaller, | 56 jobject jcaller, |
67 jlong jurl_request_context_adapter) { | 57 jlong jurl_request_context_adapter) { |
68 DCHECK(jurl_request_context_adapter); | 58 DCHECK(jurl_request_context_adapter); |
69 CronetURLRequestContextAdapter* context_adapter = | 59 CronetURLRequestContextAdapter* context_adapter = |
70 reinterpret_cast<CronetURLRequestContextAdapter*>( | 60 reinterpret_cast<CronetURLRequestContextAdapter*>( |
(...skipping 28 matching lines...) Expand all Loading... |
99 context_adapter->StopNetLog(); | 89 context_adapter->StopNetLog(); |
100 } | 90 } |
101 | 91 |
102 static jint SetMinLogLevel(JNIEnv* env, jobject jcaller, jint jlog_level) { | 92 static jint SetMinLogLevel(JNIEnv* env, jobject jcaller, jint jlog_level) { |
103 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 93 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
104 // MinLogLevel is global, shared by all URLRequestContexts. | 94 // MinLogLevel is global, shared by all URLRequestContexts. |
105 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 95 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
106 return old_log_level; | 96 return old_log_level; |
107 } | 97 } |
108 | 98 |
| 99 // Called on application's main Java thread. |
| 100 static void InitRequestContextOnMainThread(JNIEnv* env, |
| 101 jobject jcaller, |
| 102 jlong jurl_request_context_adapter) { |
| 103 if (jurl_request_context_adapter == 0) |
| 104 return; |
| 105 |
| 106 CronetURLRequestContextAdapter* context_adapter = |
| 107 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 108 jurl_request_context_adapter); |
| 109 |
| 110 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
| 111 jcaller_ref.Reset(env, jcaller); |
| 112 base::Closure init_java_network_thread = base::Bind(&initJavaNetworkThread, |
| 113 jcaller_ref); |
| 114 context_adapter->InitRequestContextOnMainThread(init_java_network_thread); |
| 115 } |
| 116 |
109 } // namespace cronet | 117 } // namespace cronet |
OLD | NEW |