| 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/org_chromium_net_UrlRequestContext.h" | 5 #include "components/cronet/android/org_chromium_net_UrlRequestContext.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/org_chromium_net_UrlRequest.h" | 16 #include "components/cronet/android/org_chromium_net_UrlRequest.h" |
| 17 #include "components/cronet/android/url_request_context_peer.h" | 17 #include "components/cronet/android/url_request_adapter.h" |
| 18 #include "components/cronet/android/url_request_peer.h" | 18 #include "components/cronet/android/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/UrlRequestContext_jni.h" | 20 #include "jni/UrlRequestContext_jni.h" |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer. | 24 // Delegate of URLRequestContextAdapter that delivers callbacks to the Java |
| 25 class JniURLRequestContextPeerDelegate | 25 // layer. |
| 26 : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate { | 26 class JniURLRequestContextAdapterDelegate |
| 27 : public cronet::URLRequestContextAdapter:: |
| 28 URLRequestContextAdapterDelegate { |
| 27 public: | 29 public: |
| 28 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) | 30 JniURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner) |
| 29 : owner_(env->NewGlobalRef(owner)) { | 31 : owner_(env->NewGlobalRef(owner)) {} |
| 30 } | |
| 31 | 32 |
| 32 virtual void OnContextInitialized( | 33 virtual void OnContextInitialized( |
| 33 cronet::URLRequestContextPeer* context) OVERRIDE { | 34 cronet::URLRequestContextAdapter* context) OVERRIDE { |
| 34 JNIEnv* env = base::android::AttachCurrentThread(); | 35 JNIEnv* env = base::android::AttachCurrentThread(); |
| 35 cronet::Java_UrlRequestContext_initNetworkThread(env, owner_); | 36 cronet::Java_UrlRequestContext_initNetworkThread(env, owner_); |
| 36 // TODO(dplotnikov): figure out if we need to detach from the thread. | 37 // TODO(dplotnikov): figure out if we need to detach from the thread. |
| 37 // The documentation says we should detach just before the thread exits. | 38 // The documentation says we should detach just before the thread exits. |
| 38 } | 39 } |
| 39 | 40 |
| 40 protected: | 41 protected: |
| 41 virtual ~JniURLRequestContextPeerDelegate() { | 42 virtual ~JniURLRequestContextAdapterDelegate() { |
| 42 JNIEnv* env = base::android::AttachCurrentThread(); | 43 JNIEnv* env = base::android::AttachCurrentThread(); |
| 43 env->DeleteGlobalRef(owner_); | 44 env->DeleteGlobalRef(owner_); |
| 44 } | 45 } |
| 45 | 46 |
| 46 private: | 47 private: |
| 47 jobject owner_; | 48 jobject owner_; |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 } // namespace | 51 } // namespace |
| 51 | 52 |
| 52 namespace cronet { | 53 namespace cronet { |
| 53 | 54 |
| 54 // Explicitly register static JNI functions. | 55 // Explicitly register static JNI functions. |
| 55 bool UrlRequestContextRegisterJni(JNIEnv* env) { | 56 bool UrlRequestContextRegisterJni(JNIEnv* env) { |
| 56 return RegisterNativesImpl(env); | 57 return RegisterNativesImpl(env); |
| 57 } | 58 } |
| 58 | 59 |
| 59 // Sets global user-agent to be used for all subsequent requests. | 60 // Sets global user-agent to be used for all subsequent requests. |
| 60 static jlong CreateRequestContextPeer(JNIEnv* env, | 61 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 61 jobject object, | 62 jobject object, |
| 62 jobject context, | 63 jobject context, |
| 63 jstring user_agent, | 64 jstring user_agent, |
| 64 jint log_level, | 65 jint log_level, |
| 65 jstring config) { | 66 jstring config) { |
| 66 std::string user_agent_string = | 67 std::string user_agent_string = |
| 67 base::android::ConvertJavaStringToUTF8(env, user_agent); | 68 base::android::ConvertJavaStringToUTF8(env, user_agent); |
| 68 | 69 |
| 69 std::string config_string = | 70 std::string config_string = |
| 70 base::android::ConvertJavaStringToUTF8(env, config); | 71 base::android::ConvertJavaStringToUTF8(env, config); |
| 71 | 72 |
| 72 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string)); | 73 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string)); |
| 73 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { | 74 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 74 DLOG(ERROR) << "Bad JSON: " << config_string; | 75 DLOG(ERROR) << "Bad JSON: " << config_string; |
| 75 return 0; | 76 return 0; |
| 76 } | 77 } |
| 77 | 78 |
| 78 scoped_ptr<URLRequestContextConfig> context_config( | 79 scoped_ptr<URLRequestContextConfig> context_config( |
| 79 new URLRequestContextConfig()); | 80 new URLRequestContextConfig()); |
| 80 base::JSONValueConverter<URLRequestContextConfig> converter; | 81 base::JSONValueConverter<URLRequestContextConfig> converter; |
| 81 if (!converter.Convert(*config_value, context_config.get())) { | 82 if (!converter.Convert(*config_value, context_config.get())) { |
| 82 DLOG(ERROR) << "Bad Config: " << config_value; | 83 DLOG(ERROR) << "Bad Config: " << config_value; |
| 83 return 0; | 84 return 0; |
| 84 } | 85 } |
| 85 | 86 |
| 86 // Set application context. | 87 // Set application context. |
| 87 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | 88 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); |
| 88 base::android::InitApplicationContext(env, scoped_context); | 89 base::android::InitApplicationContext(env, scoped_context); |
| 89 | 90 |
| 90 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. | 91 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. |
| 91 // Revisit this if each URLRequestContext would need an individual log level. | 92 // Revisit this if each URLRequestContext would need an individual log level. |
| 92 logging::SetMinLogLevel(static_cast<int>(log_level)); | 93 logging::SetMinLogLevel(static_cast<int>(log_level)); |
| 93 | 94 |
| 94 // TODO(dplotnikov): set application context. | 95 // TODO(dplotnikov): set application context. |
| 95 URLRequestContextPeer* peer = new URLRequestContextPeer( | 96 URLRequestContextAdapter* adapter = new URLRequestContextAdapter( |
| 96 new JniURLRequestContextPeerDelegate(env, object), user_agent_string); | 97 new JniURLRequestContextAdapterDelegate(env, object), user_agent_string); |
| 97 peer->AddRef(); // Hold onto this ref-counted object. | 98 adapter->AddRef(); // Hold onto this ref-counted object. |
| 98 peer->Initialize(context_config.Pass()); | 99 adapter->Initialize(context_config.Pass()); |
| 99 return reinterpret_cast<jlong>(peer); | 100 return reinterpret_cast<jlong>(adapter); |
| 100 } | 101 } |
| 101 | 102 |
| 102 // Releases native objects. | 103 // Releases native objects. |
| 103 static void ReleaseRequestContextPeer(JNIEnv* env, | 104 static void ReleaseRequestContextAdapter(JNIEnv* env, |
| 104 jobject object, | 105 jobject object, |
| 105 jlong urlRequestContextPeer) { | 106 jlong urlRequestContextAdapter) { |
| 106 URLRequestContextPeer* peer = | 107 URLRequestContextAdapter* adapter = |
| 107 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | 108 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); |
| 108 // TODO(mef): Revisit this from thread safety point of view: Can we delete a | 109 // TODO(mef): Revisit this from thread safety point of view: Can we delete a |
| 109 // thread while running on that thread? | 110 // thread while running on that thread? |
| 110 // URLRequestContextPeer is a ref-counted object, and may have pending tasks, | 111 // URLRequestContextAdapter is a ref-counted object, and may have pending |
| 112 // tasks, |
| 111 // so we need to release it instead of deleting here. | 113 // so we need to release it instead of deleting here. |
| 112 peer->Release(); | 114 adapter->Release(); |
| 113 } | 115 } |
| 114 | 116 |
| 115 // Starts recording statistics. | 117 // Starts recording statistics. |
| 116 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { | 118 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { |
| 117 base::StatisticsRecorder::Initialize(); | 119 base::StatisticsRecorder::Initialize(); |
| 118 } | 120 } |
| 119 | 121 |
| 120 // Gets current statistics with |filter| as a substring as JSON text (an empty | 122 // Gets current statistics with |filter| as a substring as JSON text (an empty |
| 121 // |filter| will include all registered histograms). | 123 // |filter| will include all registered histograms). |
| 122 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) { | 124 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) { |
| 123 std::string query = base::android::ConvertJavaStringToUTF8(env, filter); | 125 std::string query = base::android::ConvertJavaStringToUTF8(env, filter); |
| 124 std::string json = base::StatisticsRecorder::ToJSON(query); | 126 std::string json = base::StatisticsRecorder::ToJSON(query); |
| 125 return base::android::ConvertUTF8ToJavaString(env, json).Release(); | 127 return base::android::ConvertUTF8ToJavaString(env, json).Release(); |
| 126 } | 128 } |
| 127 | 129 |
| 128 // Starts recording NetLog into file with |fileName|. | 130 // Starts recording NetLog into file with |fileName|. |
| 129 static void StartNetLogToFile(JNIEnv* env, | 131 static void StartNetLogToFile(JNIEnv* env, |
| 130 jobject jcaller, | 132 jobject jcaller, |
| 131 jlong urlRequestContextPeer, | 133 jlong urlRequestContextAdapter, |
| 132 jstring fileName) { | 134 jstring fileName) { |
| 133 URLRequestContextPeer* peer = | 135 URLRequestContextAdapter* adapter = |
| 134 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | 136 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); |
| 135 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName); | 137 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName); |
| 136 peer->StartNetLogToFile(file_name); | 138 adapter->StartNetLogToFile(file_name); |
| 137 } | 139 } |
| 138 | 140 |
| 139 // Stops recording NetLog. | 141 // Stops recording NetLog. |
| 140 static void StopNetLog(JNIEnv* env, | 142 static void StopNetLog(JNIEnv* env, |
| 141 jobject jcaller, | 143 jobject jcaller, |
| 142 jlong urlRequestContextPeer) { | 144 jlong urlRequestContextAdapter) { |
| 143 URLRequestContextPeer* peer = | 145 URLRequestContextAdapter* adapter = |
| 144 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | 146 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); |
| 145 peer->StopNetLog(); | 147 adapter->StopNetLog(); |
| 146 } | 148 } |
| 147 | 149 |
| 148 } // namespace cronet | 150 } // namespace cronet |
| OLD | NEW |