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 "base/android/base_jni_registrar.h" | |
6 #include "base/android/jni_android.h" | |
7 #include "base/android/jni_registrar.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/at_exit.h" | |
10 #include "base/i18n/icu_util.h" | |
11 #include "base/metrics/statistics_recorder.h" | |
12 #include "jni/UrlRequestContext_jni.h" | |
13 #include "net/android/net_jni_registrar.h" | |
14 #include "net/cronet/android/org_chromium_net_UrlRequest.h" | |
15 #include "net/cronet/android/url_request_context_peer.h" | |
16 #include "net/cronet/android/url_request_peer.h" | |
17 | |
18 // Version of this build of Chromium NET. | |
19 #define CHROMIUM_NET_VERSION "1" | |
20 | |
21 namespace { | |
22 | |
23 const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION; | |
24 | |
25 const base::android::RegistrationMethod kCronetRegisteredMethods[] = { | |
26 {"BaseAndroid", base::android::RegisterJni}, | |
27 {"NetAndroid", net::android::RegisterJni}, | |
28 {"UrlRequest", net::UrlRequestRegisterJni}, | |
29 {"UrlRequestContext", net::RegisterNativesImpl}, | |
30 }; | |
31 | |
32 base::AtExitManager* g_at_exit_manager = NULL; | |
33 | |
34 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer. | |
35 class JniURLRequestContextPeerDelegate | |
36 : public URLRequestContextPeer::URLRequestContextPeerDelegate { | |
37 public: | |
38 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) | |
39 : owner_(env->NewGlobalRef(owner)) { | |
40 } | |
41 | |
42 virtual void OnContextInitialized(URLRequestContextPeer* context) OVERRIDE { | |
43 JNIEnv* env = base::android::AttachCurrentThread(); | |
44 net::Java_UrlRequestContext_initNetworkThread(env, owner_); | |
45 // TODO(dplotnikov): figure out if we need to detach from the thread. | |
46 // The documentation says we should detach just before the thread exits. | |
47 } | |
48 | |
49 protected: | |
50 virtual ~JniURLRequestContextPeerDelegate() { | |
51 JNIEnv* env = base::android::AttachCurrentThread(); | |
52 env->DeleteGlobalRef(owner_); | |
53 } | |
54 | |
55 private: | |
56 jobject owner_; | |
57 }; | |
58 | |
59 } // namespace | |
60 | |
61 // Checks the available version of JNI. Also, caches Java reflection artifacts. | |
62 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
63 JNIEnv* env; | |
64 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { | |
65 return -1; | |
66 } | |
67 | |
68 base::android::InitVM(vm); | |
69 | |
70 if (!base::android::RegisterNativeMethods( | |
71 env, kCronetRegisteredMethods, arraysize(kCronetRegisteredMethods))) { | |
72 return -1; | |
73 } | |
74 | |
75 g_at_exit_manager = new base::AtExitManager(); | |
76 | |
77 base::i18n::InitializeICU(); | |
78 | |
79 return JNI_VERSION_1_6; | |
80 } | |
81 | |
82 extern "C" void JNIEXPORT JNICALL JNI_OnUnLoad(JavaVM* jvm, void* reserved) { | |
83 if (g_at_exit_manager) { | |
84 delete g_at_exit_manager; | |
85 g_at_exit_manager = NULL; | |
86 } | |
87 } | |
88 | |
89 namespace net { | |
90 | |
91 static jstring GetVersion(JNIEnv* env, jclass unused) { | |
92 return env->NewStringUTF(kVersion); | |
93 } | |
94 | |
95 // Sets global user-agent to be used for all subsequent requests. | |
96 static jlong CreateRequestContextPeer(JNIEnv* env, | |
97 jobject object, | |
98 jobject context, | |
99 jstring user_agent, | |
100 jint log_level) { | |
101 const char* user_agent_utf8 = env->GetStringUTFChars(user_agent, NULL); | |
102 std::string user_agent_string(user_agent_utf8); | |
103 env->ReleaseStringUTFChars(user_agent, user_agent_utf8); | |
104 | |
105 // Set application context. | |
106 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | |
107 base::android::InitApplicationContext(env, scoped_context); | |
108 | |
109 int logging_level = log_level; | |
110 | |
111 // TODO(dplotnikov): set application context. | |
112 URLRequestContextPeer* peer = new URLRequestContextPeer( | |
113 new JniURLRequestContextPeerDelegate(env, object), | |
114 user_agent_string, | |
115 logging_level, | |
116 kVersion); | |
117 peer->AddRef(); // Hold onto this ref-counted object. | |
118 peer->Initialize(); | |
119 return reinterpret_cast<jlong>(peer); | |
120 } | |
121 | |
122 // Releases native objects. | |
123 static void ReleaseRequestContextPeer(JNIEnv* env, | |
124 jobject object, | |
125 jlong urlRequestContextPeer) { | |
126 URLRequestContextPeer* peer = | |
127 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | |
128 // TODO(mef): Revisit this from thread safety point of view: Can we delete a | |
129 // thread while running on that thread? | |
130 // URLRequestContextPeer is a ref-counted object, and may have pending tasks, | |
131 // so we need to release it instead of deleting here. | |
132 peer->Release(); | |
133 } | |
134 | |
135 // Starts recording statistics. | |
136 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { | |
137 base::StatisticsRecorder::Initialize(); | |
138 } | |
139 | |
140 // Gets current statistics with |filter| as a substring as JSON text (an empty | |
141 // |filter| will include all registered histograms). | |
142 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) { | |
143 std::string query = base::android::ConvertJavaStringToUTF8(env, filter); | |
144 std::string json = base::StatisticsRecorder::ToJSON(query); | |
145 return base::android::ConvertUTF8ToJavaString(env, json).Release(); | |
146 } | |
147 | |
148 // Starts recording NetLog into file with |fileName|. | |
149 static void StartNetLogToFile(JNIEnv* env, | |
150 jobject jcaller, | |
151 jlong urlRequestContextPeer, | |
152 jstring fileName) { | |
153 URLRequestContextPeer* peer = | |
154 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | |
155 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName); | |
156 peer->StartNetLogToFile(file_name); | |
157 } | |
158 | |
159 // Stops recording NetLog. | |
160 static void StopNetLog(JNIEnv* env, | |
161 jobject jcaller, | |
162 jlong urlRequestContextPeer) { | |
163 URLRequestContextPeer* peer = | |
164 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | |
165 peer->StopNetLog(); | |
166 } | |
167 | |
168 } // namespace net | |
OLD | NEW |