| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/cronet/android/cronet_url_request_context.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/android/jni_android.h" | |
| 10 #include "base/android/jni_string.h" | |
| 11 #include "base/android/scoped_java_ref.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/metrics/statistics_recorder.h" | |
| 15 #include "base/values.h" | |
| 16 #include "components/cronet/android/cronet_url_request.h" | |
| 17 #include "components/cronet/android/cronet_url_request_adapter.h" | |
| 18 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
| 19 #include "components/cronet/url_request_context_config.h" | |
| 20 #include "jni/CronetUrlRequestContext_jni.h" | |
| 21 | |
| 22 namespace cronet { | |
| 23 | |
| 24 // Init network thread on Java side. | |
| 25 void initJavaNetworkThread( | |
| 26 const base::android::ScopedJavaGlobalRef<jobject>& jowner) { | |
| 27 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 28 Java_CronetUrlRequestContext_initNetworkThread(env, jowner.obj()); | |
| 29 } | |
| 30 | |
| 31 // Explicitly register static JNI functions. | |
| 32 bool CronetUrlRequestContextRegisterJni(JNIEnv* env) { | |
| 33 return RegisterNativesImpl(env); | |
| 34 } | |
| 35 | |
| 36 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | |
| 37 // returns 0 otherwise. | |
| 38 static jlong CreateRequestContextAdapter(JNIEnv* env, | |
| 39 jobject jcaller, | |
| 40 jobject japp_context, | |
| 41 jstring jconfig) { | |
| 42 std::string config_string = | |
| 43 base::android::ConvertJavaStringToUTF8(env, jconfig); | |
| 44 scoped_ptr<URLRequestContextConfig> context_config( | |
| 45 new URLRequestContextConfig()); | |
| 46 if (!context_config->LoadFromJSON(config_string)) | |
| 47 return 0; | |
| 48 | |
| 49 CronetURLRequestContextAdapter* context_adapter = | |
| 50 new CronetURLRequestContextAdapter(context_config.Pass()); | |
| 51 return reinterpret_cast<jlong>(context_adapter); | |
| 52 } | |
| 53 | |
| 54 // Destroys native objects. | |
| 55 static void DestroyRequestContextAdapter(JNIEnv* env, | |
| 56 jobject jcaller, | |
| 57 jlong jurl_request_context_adapter) { | |
| 58 DCHECK(jurl_request_context_adapter); | |
| 59 CronetURLRequestContextAdapter* context_adapter = | |
| 60 reinterpret_cast<CronetURLRequestContextAdapter*>( | |
| 61 jurl_request_context_adapter); | |
| 62 context_adapter->Destroy(); | |
| 63 } | |
| 64 | |
| 65 // Starts recording NetLog into file with |fileName|. | |
| 66 static void StartNetLogToFile(JNIEnv* env, | |
| 67 jobject jcaller, | |
| 68 jlong jurl_request_context_adapter, | |
| 69 jstring jfile_name) { | |
| 70 if (jurl_request_context_adapter == 0) | |
| 71 return; | |
| 72 CronetURLRequestContextAdapter* context_adapter = | |
| 73 reinterpret_cast<CronetURLRequestContextAdapter*>( | |
| 74 jurl_request_context_adapter); | |
| 75 std::string file_name = | |
| 76 base::android::ConvertJavaStringToUTF8(env, jfile_name); | |
| 77 context_adapter->StartNetLogToFile(file_name); | |
| 78 } | |
| 79 | |
| 80 // Stops recording NetLog. | |
| 81 static void StopNetLog(JNIEnv* env, | |
| 82 jobject jcaller, | |
| 83 jlong jurl_request_context_adapter) { | |
| 84 if (jurl_request_context_adapter == 0) | |
| 85 return; | |
| 86 CronetURLRequestContextAdapter* context_adapter = | |
| 87 reinterpret_cast<CronetURLRequestContextAdapter*>( | |
| 88 jurl_request_context_adapter); | |
| 89 context_adapter->StopNetLog(); | |
| 90 } | |
| 91 | |
| 92 static jint SetMinLogLevel(JNIEnv* env, jobject jcaller, jint jlog_level) { | |
| 93 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | |
| 94 // MinLogLevel is global, shared by all URLRequestContexts. | |
| 95 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | |
| 96 return old_log_level; | |
| 97 } | |
| 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 | |
| 117 } // namespace cronet | |
| OLD | NEW |