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 "base/android/base_jni_registrar.h" | 5 #include "components/cronet/android/org_chromium_net_UrlRequestContext.h" |
| 6 |
6 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
7 #include "base/android/jni_registrar.h" | |
8 #include "base/android/jni_string.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" | 9 #include "base/metrics/statistics_recorder.h" |
12 #include "components/cronet/android/org_chromium_net_UrlRequest.h" | 10 #include "components/cronet/android/org_chromium_net_UrlRequest.h" |
13 #include "components/cronet/android/url_request_context_peer.h" | 11 #include "components/cronet/android/url_request_context_peer.h" |
14 #include "components/cronet/android/url_request_peer.h" | 12 #include "components/cronet/android/url_request_peer.h" |
15 #include "jni/UrlRequestContext_jni.h" | 13 #include "jni/UrlRequestContext_jni.h" |
16 #include "net/android/net_jni_registrar.h" | |
17 | 14 |
18 // Version of this build of Chromium NET. | 15 // Version of this build of Chromium NET. |
19 #define CHROMIUM_NET_VERSION "1" | 16 #define CHROMIUM_NET_VERSION "1" |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION; | 20 const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION; |
24 | 21 |
25 const base::android::RegistrationMethod kCronetRegisteredMethods[] = { | |
26 {"BaseAndroid", base::android::RegisterJni}, | |
27 {"NetAndroid", net::android::RegisterJni}, | |
28 {"UrlRequest", cronet::UrlRequestRegisterJni}, | |
29 {"UrlRequestContext", cronet::RegisterNativesImpl}, | |
30 }; | |
31 | |
32 base::AtExitManager* g_at_exit_manager = NULL; | |
33 | |
34 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer. | 22 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer. |
35 class JniURLRequestContextPeerDelegate | 23 class JniURLRequestContextPeerDelegate |
36 : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate { | 24 : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate { |
37 public: | 25 public: |
38 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) | 26 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) |
39 : owner_(env->NewGlobalRef(owner)) { | 27 : owner_(env->NewGlobalRef(owner)) { |
40 } | 28 } |
41 | 29 |
42 virtual void OnContextInitialized( | 30 virtual void OnContextInitialized( |
43 cronet::URLRequestContextPeer* context) OVERRIDE { | 31 cronet::URLRequestContextPeer* context) OVERRIDE { |
44 JNIEnv* env = base::android::AttachCurrentThread(); | 32 JNIEnv* env = base::android::AttachCurrentThread(); |
45 cronet::Java_UrlRequestContext_initNetworkThread(env, owner_); | 33 cronet::Java_UrlRequestContext_initNetworkThread(env, owner_); |
46 // TODO(dplotnikov): figure out if we need to detach from the thread. | 34 // TODO(dplotnikov): figure out if we need to detach from the thread. |
47 // The documentation says we should detach just before the thread exits. | 35 // The documentation says we should detach just before the thread exits. |
48 } | 36 } |
49 | 37 |
50 protected: | 38 protected: |
51 virtual ~JniURLRequestContextPeerDelegate() { | 39 virtual ~JniURLRequestContextPeerDelegate() { |
52 JNIEnv* env = base::android::AttachCurrentThread(); | 40 JNIEnv* env = base::android::AttachCurrentThread(); |
53 env->DeleteGlobalRef(owner_); | 41 env->DeleteGlobalRef(owner_); |
54 } | 42 } |
55 | 43 |
56 private: | 44 private: |
57 jobject owner_; | 45 jobject owner_; |
58 }; | 46 }; |
59 | 47 |
60 } // namespace | 48 } // namespace |
61 | 49 |
62 // Checks the available version of JNI. Also, caches Java reflection artifacts. | 50 namespace cronet { |
63 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
64 JNIEnv* env; | |
65 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { | |
66 return -1; | |
67 } | |
68 | 51 |
69 base::android::InitVM(vm); | 52 // Explicitly register static JNI functions. |
70 | 53 bool UrlRequestContextRegisterJni(JNIEnv* env) { |
71 if (!base::android::RegisterNativeMethods( | 54 return RegisterNativesImpl(env); |
72 env, kCronetRegisteredMethods, arraysize(kCronetRegisteredMethods))) { | |
73 return -1; | |
74 } | |
75 | |
76 g_at_exit_manager = new base::AtExitManager(); | |
77 | |
78 base::i18n::InitializeICU(); | |
79 | |
80 return JNI_VERSION_1_6; | |
81 } | 55 } |
82 | 56 |
83 extern "C" void JNIEXPORT JNICALL JNI_OnUnLoad(JavaVM* jvm, void* reserved) { | |
84 if (g_at_exit_manager) { | |
85 delete g_at_exit_manager; | |
86 g_at_exit_manager = NULL; | |
87 } | |
88 } | |
89 | |
90 namespace cronet { | |
91 | |
92 static jstring GetVersion(JNIEnv* env, jclass unused) { | 57 static jstring GetVersion(JNIEnv* env, jclass unused) { |
93 return env->NewStringUTF(kVersion); | 58 return env->NewStringUTF(kVersion); |
94 } | 59 } |
95 | 60 |
96 // Sets global user-agent to be used for all subsequent requests. | 61 // Sets global user-agent to be used for all subsequent requests. |
97 static jlong CreateRequestContextPeer(JNIEnv* env, | 62 static jlong CreateRequestContextPeer(JNIEnv* env, |
98 jobject object, | 63 jobject object, |
99 jobject context, | 64 jobject context, |
100 jstring user_agent, | 65 jstring user_agent, |
101 jint log_level) { | 66 jint log_level) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // Stops recording NetLog. | 125 // Stops recording NetLog. |
161 static void StopNetLog(JNIEnv* env, | 126 static void StopNetLog(JNIEnv* env, |
162 jobject jcaller, | 127 jobject jcaller, |
163 jlong urlRequestContextPeer) { | 128 jlong urlRequestContextPeer) { |
164 URLRequestContextPeer* peer = | 129 URLRequestContextPeer* peer = |
165 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | 130 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); |
166 peer->StopNetLog(); | 131 peer->StopNetLog(); |
167 } | 132 } |
168 | 133 |
169 } // namespace cronet | 134 } // namespace cronet |
OLD | NEW |