| 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/chromium_url_request_context.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/android/jni_android.h" | |
| 12 #include "base/android/jni_string.h" | |
| 13 #include "base/json/json_reader.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/metrics/statistics_recorder.h" | |
| 16 #include "base/values.h" | |
| 17 #include "components/cronet/android/chromium_url_request.h" | |
| 18 #include "components/cronet/android/url_request_adapter.h" | |
| 19 #include "components/cronet/android/url_request_context_adapter.h" | |
| 20 #include "components/cronet/url_request_context_config.h" | |
| 21 #include "jni/ChromiumUrlRequestContext_jni.h" | |
| 22 | |
| 23 using base::android::ConvertUTF8ToJavaString; | |
| 24 using base::android::ConvertJavaStringToUTF8; | |
| 25 using base::android::JavaParamRef; | |
| 26 using base::android::ScopedJavaLocalRef; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // Delegate of URLRequestContextAdapter that delivers callbacks to the Java | |
| 31 // layer. | |
| 32 class JniURLRequestContextAdapterDelegate | |
| 33 : public cronet::URLRequestContextAdapter:: | |
| 34 URLRequestContextAdapterDelegate { | |
| 35 public: | |
| 36 JniURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner) | |
| 37 : owner_(env->NewGlobalRef(owner)) {} | |
| 38 | |
| 39 void OnContextInitialized( | |
| 40 cronet::URLRequestContextAdapter* context_adapter) override { | |
| 41 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 42 cronet::Java_ChromiumUrlRequestContext_initNetworkThread(env, owner_); | |
| 43 // TODO(dplotnikov): figure out if we need to detach from the thread. | |
| 44 // The documentation says we should detach just before the thread exits. | |
| 45 } | |
| 46 | |
| 47 protected: | |
| 48 ~JniURLRequestContextAdapterDelegate() override { | |
| 49 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 50 env->DeleteGlobalRef(owner_); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 jobject owner_; | |
| 55 }; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 namespace cronet { | |
| 60 | |
| 61 // Explicitly register static JNI functions. | |
| 62 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { | |
| 63 return RegisterNativesImpl(env); | |
| 64 } | |
| 65 | |
| 66 // Sets global user-agent to be used for all subsequent requests. | |
| 67 static jlong CreateRequestContextAdapter( | |
| 68 JNIEnv* env, | |
| 69 const JavaParamRef<jobject>& jcaller, | |
| 70 const JavaParamRef<jstring>& juser_agent, | |
| 71 jint jlog_level, | |
| 72 jlong jconfig) { | |
| 73 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent); | |
| 74 | |
| 75 std::unique_ptr<URLRequestContextConfig> context_config( | |
| 76 reinterpret_cast<URLRequestContextConfig*>(jconfig)); | |
| 77 | |
| 78 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. | |
| 79 // Revisit this if each URLRequestContext would need an individual log level. | |
| 80 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | |
| 81 | |
| 82 // TODO(dplotnikov): set application context. | |
| 83 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter( | |
| 84 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent); | |
| 85 context_adapter->AddRef(); // Hold onto this ref-counted object. | |
| 86 context_adapter->Initialize(std::move(context_config)); | |
| 87 return reinterpret_cast<jlong>(context_adapter); | |
| 88 } | |
| 89 | |
| 90 // Releases native objects. | |
| 91 static void ReleaseRequestContextAdapter(JNIEnv* env, | |
| 92 const JavaParamRef<jobject>& jcaller, | |
| 93 jlong jurl_request_context_adapter) { | |
| 94 URLRequestContextAdapter* context_adapter = | |
| 95 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | |
| 96 // TODO(mef): Revisit this from thread safety point of view: Can we delete a | |
| 97 // thread while running on that thread? | |
| 98 // URLRequestContextAdapter is a ref-counted object, and may have pending | |
| 99 // tasks, | |
| 100 // so we need to release it instead of deleting here. | |
| 101 context_adapter->Release(); | |
| 102 } | |
| 103 | |
| 104 // Starts recording statistics. | |
| 105 static void InitializeStatistics(JNIEnv* env, | |
| 106 const JavaParamRef<jobject>& jcaller) { | |
| 107 base::StatisticsRecorder::Initialize(); | |
| 108 } | |
| 109 | |
| 110 // Gets current statistics with |jfilter| as a substring as JSON text (an empty | |
| 111 // |jfilter| will include all registered histograms). | |
| 112 static ScopedJavaLocalRef<jstring> GetStatisticsJSON( | |
| 113 JNIEnv* env, | |
| 114 const JavaParamRef<jobject>& jcaller, | |
| 115 const JavaParamRef<jstring>& jfilter) { | |
| 116 std::string query = ConvertJavaStringToUTF8(env, jfilter); | |
| 117 std::string json = base::StatisticsRecorder::ToJSON(query); | |
| 118 return ConvertUTF8ToJavaString(env, json); | |
| 119 } | |
| 120 | |
| 121 // Starts recording NetLog into file with |jfilename|. | |
| 122 static void StartNetLogToFile(JNIEnv* env, | |
| 123 const JavaParamRef<jobject>& jcaller, | |
| 124 jlong jurl_request_context_adapter, | |
| 125 const JavaParamRef<jstring>& jfilename, | |
| 126 jboolean jlog_all) { | |
| 127 URLRequestContextAdapter* context_adapter = | |
| 128 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | |
| 129 std::string filename = ConvertJavaStringToUTF8(env, jfilename); | |
| 130 context_adapter->StartNetLogToFile(filename, jlog_all); | |
| 131 } | |
| 132 | |
| 133 // Stops recording NetLog. | |
| 134 static void StopNetLog(JNIEnv* env, | |
| 135 const JavaParamRef<jobject>& jcaller, | |
| 136 jlong jurl_request_context_adapter) { | |
| 137 URLRequestContextAdapter* context_adapter = | |
| 138 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | |
| 139 context_adapter->StopNetLog(); | |
| 140 } | |
| 141 | |
| 142 // Called on application's main Java thread. | |
| 143 static void InitRequestContextOnMainThread(JNIEnv* env, | |
| 144 const JavaParamRef<jobject>& jcaller, | |
| 145 jlong jurl_request_context_adapter) { | |
| 146 URLRequestContextAdapter* context_adapter = | |
| 147 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | |
| 148 context_adapter->InitRequestContextOnMainThread(); | |
| 149 } | |
| 150 | |
| 151 } // namespace cronet | |
| OLD | NEW |