| 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/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" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/metrics/statistics_recorder.h" | 14 #include "base/metrics/statistics_recorder.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "components/cronet/android/chromium_url_request.h" | 16 #include "components/cronet/android/cronet_url_request.h" |
| 17 #include "components/cronet/android/url_request_adapter.h" | 17 #include "components/cronet/android/cronet_url_request_adapter.h" |
| 18 #include "components/cronet/android/url_request_context_adapter.h" | 18 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 19 #include "components/cronet/url_request_context_config.h" | 19 #include "components/cronet/url_request_context_config.h" |
| 20 #include "jni/ChromiumUrlRequestContext_jni.h" | 20 #include "jni/CronetUrlRequestContext_jni.h" |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Delegate of URLRequestContextAdapter that delivers callbacks to the Java | 24 // Delegate of CronetURLRequestContextAdapter that delivers callbacks to the |
| 25 // Java |
| 25 // layer. | 26 // layer. |
| 26 class JniURLRequestContextAdapterDelegate | 27 class JniCronetURLRequestContextAdapterDelegate |
| 27 : public cronet::URLRequestContextAdapter:: | 28 : public cronet::CronetURLRequestContextAdapter:: |
| 28 URLRequestContextAdapterDelegate { | 29 CronetURLRequestContextAdapterDelegate { |
| 29 public: | 30 public: |
| 30 JniURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner) | 31 JniCronetURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner) |
| 31 : owner_(env->NewGlobalRef(owner)) {} | 32 : owner_(env->NewGlobalRef(owner)) {} |
| 32 | 33 |
| 33 virtual void OnContextInitialized( | 34 virtual void OnContextInitialized( |
| 34 cronet::URLRequestContextAdapter* context) OVERRIDE { | 35 cronet::CronetURLRequestContextAdapter* context) OVERRIDE { |
| 35 JNIEnv* env = base::android::AttachCurrentThread(); | 36 JNIEnv* env = base::android::AttachCurrentThread(); |
| 36 cronet::Java_ChromiumUrlRequestContext_initNetworkThread(env, owner_); | 37 cronet::Java_CronetUrlRequestContext_initNetworkThread(env, owner_); |
| 37 // TODO(dplotnikov): figure out if we need to detach from the thread. | 38 // TODO(dplotnikov): figure out if we need to detach from the thread. |
| 38 // The documentation says we should detach just before the thread exits. | 39 // The documentation says we should detach just before the thread exits. |
| 39 } | 40 } |
| 40 | 41 |
| 41 protected: | 42 protected: |
| 42 virtual ~JniURLRequestContextAdapterDelegate() { | 43 virtual ~JniCronetURLRequestContextAdapterDelegate() { |
| 43 JNIEnv* env = base::android::AttachCurrentThread(); | 44 JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 env->DeleteGlobalRef(owner_); | 45 env->DeleteGlobalRef(owner_); |
| 45 } | 46 } |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 jobject owner_; | 49 jobject owner_; |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 } // namespace | 52 } // namespace |
| 52 | 53 |
| 53 namespace cronet { | 54 namespace cronet { |
| 54 | 55 |
| 55 // Explicitly register static JNI functions. | 56 // Explicitly register static JNI functions. |
| 56 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { | 57 bool CronetUrlRequestContextRegisterJni(JNIEnv* env) { |
| 57 return RegisterNativesImpl(env); | 58 return RegisterNativesImpl(env); |
| 58 } | 59 } |
| 59 | 60 |
| 60 // Sets global user-agent to be used for all subsequent requests. | 61 // Sets global user-agent to be used for all subsequent requests. |
| 61 static jlong CreateRequestContextAdapter(JNIEnv* env, | 62 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 62 jobject object, | 63 jobject object, |
| 63 jobject context, | 64 jobject context, |
| 64 jstring user_agent, | 65 jstring user_agent, |
| 65 jint log_level, | 66 jint log_level, |
| 66 jstring config) { | 67 jstring config) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 86 | 87 |
| 87 // Set application context. | 88 // Set application context. |
| 88 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | 89 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); |
| 89 base::android::InitApplicationContext(env, scoped_context); | 90 base::android::InitApplicationContext(env, scoped_context); |
| 90 | 91 |
| 91 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. | 92 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. |
| 92 // Revisit this if each URLRequestContext would need an individual log level. | 93 // Revisit this if each URLRequestContext would need an individual log level. |
| 93 logging::SetMinLogLevel(static_cast<int>(log_level)); | 94 logging::SetMinLogLevel(static_cast<int>(log_level)); |
| 94 | 95 |
| 95 // TODO(dplotnikov): set application context. | 96 // TODO(dplotnikov): set application context. |
| 96 URLRequestContextAdapter* adapter = new URLRequestContextAdapter( | 97 CronetURLRequestContextAdapter* adapter = new CronetURLRequestContextAdapter( |
| 97 new JniURLRequestContextAdapterDelegate(env, object), user_agent_string); | 98 new JniCronetURLRequestContextAdapterDelegate(env, object), |
| 99 user_agent_string); |
| 98 adapter->AddRef(); // Hold onto this ref-counted object. | 100 adapter->AddRef(); // Hold onto this ref-counted object. |
| 99 adapter->Initialize(context_config.Pass()); | 101 adapter->Initialize(context_config.Pass()); |
| 100 return reinterpret_cast<jlong>(adapter); | 102 return reinterpret_cast<jlong>(adapter); |
| 101 } | 103 } |
| 102 | 104 |
| 103 // Releases native objects. | 105 // Releases native objects. |
| 104 static void ReleaseRequestContextAdapter(JNIEnv* env, | 106 static void ReleaseRequestContextAdapter(JNIEnv* env, |
| 105 jobject object, | 107 jobject object, |
| 106 jlong urlRequestContextAdapter) { | 108 jlong urlRequestContextAdapter) { |
| 107 URLRequestContextAdapter* adapter = | 109 CronetURLRequestContextAdapter* adapter = |
| 108 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); | 110 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 111 urlRequestContextAdapter); |
| 109 // TODO(mef): Revisit this from thread safety point of view: Can we delete a | 112 // TODO(mef): Revisit this from thread safety point of view: Can we delete a |
| 110 // thread while running on that thread? | 113 // thread while running on that thread? |
| 111 // URLRequestContextAdapter is a ref-counted object, and may have pending | 114 // CronetURLRequestContextAdapter is a ref-counted object, and may have |
| 115 // pending |
| 112 // tasks, | 116 // tasks, |
| 113 // so we need to release it instead of deleting here. | 117 // so we need to release it instead of deleting here. |
| 114 adapter->Release(); | 118 adapter->Release(); |
| 115 } | 119 } |
| 116 | 120 |
| 117 // Starts recording statistics. | 121 // Starts recording statistics. |
| 118 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { | 122 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { |
| 119 base::StatisticsRecorder::Initialize(); | 123 base::StatisticsRecorder::Initialize(); |
| 120 } | 124 } |
| 121 | 125 |
| 122 // Gets current statistics with |filter| as a substring as JSON text (an empty | 126 // Gets current statistics with |filter| as a substring as JSON text (an empty |
| 123 // |filter| will include all registered histograms). | 127 // |filter| will include all registered histograms). |
| 124 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) { | 128 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) { |
| 125 std::string query = base::android::ConvertJavaStringToUTF8(env, filter); | 129 std::string query = base::android::ConvertJavaStringToUTF8(env, filter); |
| 126 std::string json = base::StatisticsRecorder::ToJSON(query); | 130 std::string json = base::StatisticsRecorder::ToJSON(query); |
| 127 return base::android::ConvertUTF8ToJavaString(env, json).Release(); | 131 return base::android::ConvertUTF8ToJavaString(env, json).Release(); |
| 128 } | 132 } |
| 129 | 133 |
| 130 // Starts recording NetLog into file with |fileName|. | 134 // Starts recording NetLog into file with |fileName|. |
| 131 static void StartNetLogToFile(JNIEnv* env, | 135 static void StartNetLogToFile(JNIEnv* env, |
| 132 jobject jcaller, | 136 jobject jcaller, |
| 133 jlong urlRequestContextAdapter, | 137 jlong urlRequestContextAdapter, |
| 134 jstring fileName) { | 138 jstring fileName) { |
| 135 URLRequestContextAdapter* adapter = | 139 CronetURLRequestContextAdapter* adapter = |
| 136 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); | 140 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 141 urlRequestContextAdapter); |
| 137 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName); | 142 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName); |
| 138 adapter->StartNetLogToFile(file_name); | 143 adapter->StartNetLogToFile(file_name); |
| 139 } | 144 } |
| 140 | 145 |
| 141 // Stops recording NetLog. | 146 // Stops recording NetLog. |
| 142 static void StopNetLog(JNIEnv* env, | 147 static void StopNetLog(JNIEnv* env, |
| 143 jobject jcaller, | 148 jobject jcaller, |
| 144 jlong urlRequestContextAdapter) { | 149 jlong urlRequestContextAdapter) { |
| 145 URLRequestContextAdapter* adapter = | 150 CronetURLRequestContextAdapter* adapter = |
| 146 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); | 151 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 152 urlRequestContextAdapter); |
| 147 adapter->StopNetLog(); | 153 adapter->StopNetLog(); |
| 148 } | 154 } |
| 149 | 155 |
| 150 } // namespace cronet | 156 } // namespace cronet |
| OLD | NEW |