Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/android/jni_android.h" | |
| 6 | |
| 7 #include "base/android/auto_jobject.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
| 10 namespace { | |
| 11 JavaVM* g_jvm = 0; | |
| 12 jobject g_application_context = NULL; | |
| 13 } | |
| 14 | |
| 15 namespace base { | |
| 16 namespace android { | |
| 17 | |
| 18 JNIEnv* AttachCurrentThread() { | |
| 19 if (!g_jvm) | |
|
M-A Ruel
2011/11/11 13:09:07
Having this function silently fail is a recipe for
| |
| 20 return NULL; | |
| 21 | |
| 22 JNIEnv* env = NULL; | |
| 23 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | |
| 24 DCHECK_EQ(ret, JNI_OK); | |
| 25 return env; | |
| 26 } | |
| 27 | |
| 28 void DetachFromVM() { | |
| 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. | |
| 31 if (g_jvm) | |
| 32 g_jvm->DetachCurrentThread(); | |
| 33 } | |
| 34 | |
| 35 void InitVM(JavaVM* vm) { | |
| 36 DCHECK(!g_jvm); | |
| 37 g_jvm = vm; | |
| 38 } | |
| 39 | |
| 40 void InitApplicationContext(jobject context) { | |
| 41 DCHECK(!g_application_context); | |
| 42 g_application_context = context; | |
| 43 } | |
| 44 | |
| 45 jobject GetApplicationContext() { | |
| 46 DCHECK(g_application_context); | |
| 47 return g_application_context; | |
| 48 } | |
| 49 | |
| 50 jmethodID GetMethodID(JNIEnv* env, | |
| 51 jclass clazz, | |
| 52 const char* const method, | |
| 53 const char* const jni_signature) { | |
| 54 jmethodID id = env->GetMethodID(clazz, method, jni_signature); | |
| 55 DCHECK(id) << method; | |
| 56 CheckException(env); | |
| 57 return id; | |
| 58 } | |
| 59 | |
| 60 bool CheckException(JNIEnv* env) { | |
| 61 if (env->ExceptionCheck() == JNI_FALSE) | |
| 62 return false; | |
| 63 env->ExceptionDescribe(); | |
| 64 env->ExceptionClear(); | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 } // namespace android | |
| 69 } // namespace base | |
| OLD | NEW |