OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/jni_android.h" | 5 #include "base/android/jni_android.h" |
6 | 6 |
| 7 #include "base/android/scoped_java_ref.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 | 9 |
9 namespace { | 10 namespace { |
10 JavaVM* g_jvm = 0; | 11 JavaVM* g_jvm = 0; |
11 jobject g_application_context = NULL; | 12 jobject g_application_context = NULL; |
12 } | 13 } |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace android { | 16 namespace android { |
16 | 17 |
17 JNIEnv* AttachCurrentThread() { | 18 JNIEnv* AttachCurrentThread() { |
18 if (!g_jvm) | 19 if (!g_jvm) |
19 return NULL; | 20 return NULL; |
20 | |
21 JNIEnv* env = NULL; | 21 JNIEnv* env = NULL; |
22 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 22 jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
23 DCHECK_EQ(ret, JNI_OK); | 23 DCHECK_EQ(ret, JNI_OK); |
24 return env; | 24 return env; |
25 } | 25 } |
26 | 26 |
27 void DetachFromVM() { | 27 void DetachFromVM() { |
28 // Ignore the return value, if the thread is not attached, DetachCurrentThread | 28 // Ignore the return value, if the thread is not attached, DetachCurrentThread |
29 // will fail. But it is ok as the native thread may never be attached. | 29 // will fail. But it is ok as the native thread may never be attached. |
30 if (g_jvm) | 30 if (g_jvm) |
31 g_jvm->DetachCurrentThread(); | 31 g_jvm->DetachCurrentThread(); |
32 } | 32 } |
33 | 33 |
34 void InitVM(JavaVM* vm) { | 34 void InitVM(JavaVM* vm) { |
35 DCHECK(!g_jvm); | 35 DCHECK(!g_jvm); |
36 g_jvm = vm; | 36 g_jvm = vm; |
37 } | 37 } |
38 | 38 |
39 void InitApplicationContext(jobject context) { | 39 void InitApplicationContext(jobject context) { |
40 DCHECK(!g_application_context); | 40 DCHECK(!g_application_context); |
41 g_application_context = context; | 41 g_application_context = context; |
42 } | 42 } |
43 | 43 |
44 jobject GetApplicationContext() { | 44 jobject GetApplicationContext() { |
45 DCHECK(g_application_context); | 45 DCHECK(g_application_context); |
46 return g_application_context; | 46 return g_application_context; |
47 } | 47 } |
48 | 48 |
| 49 MethodID::MethodID(JNIEnv* env, const char* class_name, const char* method, |
| 50 const char* jni_signature) { |
| 51 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); |
| 52 id_ = GetMethodID(env, clazz.obj(), method, jni_signature); |
| 53 } |
| 54 |
49 jmethodID GetMethodID(JNIEnv* env, | 55 jmethodID GetMethodID(JNIEnv* env, |
50 jclass clazz, | 56 jclass clazz, |
51 const char* const method, | 57 const char* const method, |
52 const char* const jni_signature) { | 58 const char* const jni_signature) { |
53 jmethodID id = env->GetMethodID(clazz, method, jni_signature); | 59 jmethodID id = env->GetMethodID(clazz, method, jni_signature); |
54 DCHECK(id) << method; | 60 DCHECK(id) << method; |
55 CheckException(env); | 61 CheckException(env); |
56 return id; | 62 return id; |
57 } | 63 } |
58 | 64 |
59 jmethodID GetStaticMethodID(JNIEnv* env, | 65 jmethodID GetStaticMethodID(JNIEnv* env, |
60 jclass clazz, | 66 jclass clazz, |
61 const char* const method, | 67 const char* const method, |
62 const char* const jni_signature) { | 68 const char* const jni_signature) { |
63 jmethodID id = env->GetStaticMethodID(clazz, method, jni_signature); | 69 jmethodID id = env->GetStaticMethodID(clazz, method, jni_signature); |
64 DCHECK(id) << method; | 70 DCHECK(id) << method; |
65 CheckException(env); | 71 CheckException(env); |
66 return id; | 72 return id; |
67 } | 73 } |
68 | 74 |
| 75 jfieldID GetFieldID(JNIEnv* env, |
| 76 jclass clazz, |
| 77 const char* field, |
| 78 const char* jni_signature) { |
| 79 jfieldID id = env->GetFieldID(clazz, field, jni_signature); |
| 80 DCHECK(id) << field; |
| 81 CheckException(env); |
| 82 return id; |
| 83 } |
| 84 |
69 bool CheckException(JNIEnv* env) { | 85 bool CheckException(JNIEnv* env) { |
70 if (env->ExceptionCheck() == JNI_FALSE) | 86 if (env->ExceptionCheck() == JNI_FALSE) |
71 return false; | 87 return false; |
72 env->ExceptionDescribe(); | 88 env->ExceptionDescribe(); |
73 env->ExceptionClear(); | 89 env->ExceptionClear(); |
74 return true; | 90 return true; |
75 } | 91 } |
76 | 92 |
77 } // namespace android | 93 } // namespace android |
78 } // namespace base | 94 } // namespace base |
OLD | NEW |