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 // Sets global user-agent to be used for all subsequent requests. |
| 37 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 38 jobject jcaller, |
| 39 jobject japp_context, |
| 40 jstring jconfig) { |
| 41 std::string config_string = |
| 42 base::android::ConvertJavaStringToUTF8(env, jconfig); |
| 43 scoped_ptr<URLRequestContextConfig> context_config( |
| 44 new URLRequestContextConfig()); |
| 45 if (!context_config->LoadFromJSON(config_string)) |
| 46 return 0; |
| 47 |
| 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 = |
| 56 new CronetURLRequestContextAdapter(); |
| 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); |
| 62 } |
| 63 |
| 64 // Destroys native objects. |
| 65 static void DestroyRequestContextAdapter(JNIEnv* env, |
| 66 jobject jcaller, |
| 67 jlong jurl_request_context_adapter) { |
| 68 DCHECK(jurl_request_context_adapter); |
| 69 CronetURLRequestContextAdapter* context_adapter = |
| 70 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 71 jurl_request_context_adapter); |
| 72 context_adapter->Destroy(); |
| 73 } |
| 74 |
| 75 // Starts recording NetLog into file with |fileName|. |
| 76 static void StartNetLogToFile(JNIEnv* env, |
| 77 jobject jcaller, |
| 78 jlong jurl_request_context_adapter, |
| 79 jstring jfile_name) { |
| 80 if (jurl_request_context_adapter == 0) |
| 81 return; |
| 82 CronetURLRequestContextAdapter* context_adapter = |
| 83 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 84 jurl_request_context_adapter); |
| 85 std::string file_name = |
| 86 base::android::ConvertJavaStringToUTF8(env, jfile_name); |
| 87 context_adapter->StartNetLogToFile(file_name); |
| 88 } |
| 89 |
| 90 // Stops recording NetLog. |
| 91 static void StopNetLog(JNIEnv* env, |
| 92 jobject jcaller, |
| 93 jlong jurl_request_context_adapter) { |
| 94 if (jurl_request_context_adapter == 0) |
| 95 return; |
| 96 CronetURLRequestContextAdapter* context_adapter = |
| 97 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 98 jurl_request_context_adapter); |
| 99 context_adapter->StopNetLog(); |
| 100 } |
| 101 |
| 102 static jint SetMinLogLevel(JNIEnv* env, jobject jcaller, jint jlog_level) { |
| 103 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| 104 // MinLogLevel is global, shared by all URLRequestContexts. |
| 105 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 106 return old_log_level; |
| 107 } |
| 108 |
| 109 } // namespace cronet |
OLD | NEW |