Chromium Code Reviews| 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/android/scoped_java_ref.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 JavaVM* g_jvm = 0; | 11 JavaVM* g_jvm = 0; |
| 12 jobject g_application_context = NULL; | 12 jobject g_application_context = NULL; |
| 13 } | 13 } |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 namespace android { | 16 namespace android { |
| 17 | 17 |
| 18 JNIEnv* AttachCurrentThread() { | 18 JNIEnv* AttachCurrentThread() { |
| 19 if (!g_jvm) | 19 if (!g_jvm) |
| 20 return NULL; | 20 return NULL; |
| 21 | 21 |
| 22 JNIEnv* env = NULL; | 22 JNIEnv* env = NULL; |
| 23 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 23 jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
|
M-A Ruel
2011/12/01 14:36:03
I feel like this should be TLS cached. Isn't this
Steve Block
2011/12/01 17:48:47
AttachCurrentThread() is intended to be used as a
| |
| 24 DCHECK_EQ(ret, JNI_OK); | 24 DCHECK_EQ(ret, JNI_OK); |
| 25 return env; | 25 return env; |
| 26 } | 26 } |
| 27 | 27 |
| 28 void DetachFromVM() { | 28 void DetachFromVM() { |
| 29 // Ignore the return value, if the thread is not attached, DetachCurrentThread | 29 // Ignore the return value, if the thread is not attached, DetachCurrentThread |
| 30 // will fail. But it is ok as the native thread may never be attached. | 30 // will fail. But it is ok as the native thread may never be attached. |
| 31 if (g_jvm) | 31 if (g_jvm) |
| 32 g_jvm->DetachCurrentThread(); | 32 g_jvm->DetachCurrentThread(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void InitVM(JavaVM* vm) { | 35 void InitVM(JavaVM* vm) { |
| 36 DCHECK(!g_jvm); | 36 DCHECK(!g_jvm); |
| 37 g_jvm = vm; | 37 g_jvm = vm; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void InitApplicationContext(jobject context) { | 40 void InitApplicationContext(jobject context) { |
| 41 DCHECK(!g_application_context); | 41 DCHECK(!g_application_context); |
| 42 g_application_context = context; | 42 g_application_context = context; |
| 43 } | 43 } |
| 44 | 44 |
| 45 jobject GetApplicationContext() { | 45 jobject GetApplicationContext() { |
| 46 DCHECK(g_application_context); | 46 DCHECK(g_application_context); |
| 47 return g_application_context; | 47 return g_application_context; |
| 48 } | 48 } |
| 49 | 49 |
| 50 MethodID::MethodID(JNIEnv* env, const char* class_name, const char* method, | 50 jmethodID GetMethodIDFromClassName(JNIEnv* env, |
| 51 const char* jni_signature) { | 51 const char* class_name, |
| 52 const char* method, | |
| 53 const char* jni_signature) { | |
| 52 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); | 54 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); |
|
M-A Ruel
2011/12/01 14:36:03
If you implement caching at this layer, you'll hav
Steve Block
2011/12/01 17:48:47
There's no need to cache JNIEnv (see above) and me
| |
| 53 id_ = GetMethodID(env, clazz.obj(), method, jni_signature); | 55 return GetMethodID(env, clazz.obj(), method, jni_signature); |
| 54 } | 56 } |
| 55 | 57 |
| 56 jmethodID GetMethodID(JNIEnv* env, | 58 jmethodID GetMethodID(JNIEnv* env, |
| 57 jclass clazz, | 59 jclass clazz, |
| 58 const char* const method, | 60 const char* const method, |
| 59 const char* const jni_signature) { | 61 const char* const jni_signature) { |
| 60 jmethodID id = env->GetMethodID(clazz, method, jni_signature); | 62 jmethodID id = env->GetMethodID(clazz, method, jni_signature); |
| 61 DCHECK(id) << method; | 63 DCHECK(id) << method; |
| 62 CheckException(env); | 64 CheckException(env); |
| 63 return id; | 65 return id; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 86 bool CheckException(JNIEnv* env) { | 88 bool CheckException(JNIEnv* env) { |
| 87 if (env->ExceptionCheck() == JNI_FALSE) | 89 if (env->ExceptionCheck() == JNI_FALSE) |
| 88 return false; | 90 return false; |
| 89 env->ExceptionDescribe(); | 91 env->ExceptionDescribe(); |
| 90 env->ExceptionClear(); | 92 env->ExceptionClear(); |
| 91 return true; | 93 return true; |
| 92 } | 94 } |
| 93 | 95 |
| 94 } // namespace android | 96 } // namespace android |
| 95 } // namespace base | 97 } // namespace base |
| OLD | NEW |