| 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 <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/android/scoped_java_ref.h" | |
| 10 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
| 11 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 JavaVM* g_jvm = 0; | 15 JavaVM* g_jvm = 0; |
| 17 jobject g_application_context = NULL; | 16 jobject g_application_context = NULL; |
| 18 | 17 |
| 19 struct MethodIdentifier { | 18 struct MethodIdentifier { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 48 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked; | 47 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked; |
| 49 | 48 |
| 50 } | 49 } |
| 51 | 50 |
| 52 namespace base { | 51 namespace base { |
| 53 namespace android { | 52 namespace android { |
| 54 | 53 |
| 55 JNIEnv* AttachCurrentThread() { | 54 JNIEnv* AttachCurrentThread() { |
| 56 if (!g_jvm) | 55 if (!g_jvm) |
| 57 return NULL; | 56 return NULL; |
| 58 | |
| 59 JNIEnv* env = NULL; | 57 JNIEnv* env = NULL; |
| 60 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 58 jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
| 61 DCHECK_EQ(ret, JNI_OK); | 59 DCHECK_EQ(ret, JNI_OK); |
| 62 return env; | 60 return env; |
| 63 } | 61 } |
| 64 | 62 |
| 65 void DetachFromVM() { | 63 void DetachFromVM() { |
| 66 // Ignore the return value, if the thread is not attached, DetachCurrentThread | 64 // Ignore the return value, if the thread is not attached, DetachCurrentThread |
| 67 // will fail. But it is ok as the native thread may never be attached. | 65 // will fail. But it is ok as the native thread may never be attached. |
| 68 if (g_jvm) | 66 if (g_jvm) |
| 69 g_jvm->DetachCurrentThread(); | 67 g_jvm->DetachCurrentThread(); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void InitVM(JavaVM* vm) { | 70 void InitVM(JavaVM* vm) { |
| 73 DCHECK(!g_jvm); | 71 DCHECK(!g_jvm); |
| 74 g_jvm = vm; | 72 g_jvm = vm; |
| 75 } | 73 } |
| 76 | 74 |
| 77 void InitApplicationContext(jobject context) { | 75 void InitApplicationContext(const JavaRef<jobject>& context) { |
| 78 DCHECK(!g_application_context); | 76 DCHECK(!g_application_context); |
| 79 g_application_context = context; | 77 g_application_context = context.env()->NewGlobalRef(context.obj()); |
| 80 } | 78 } |
| 81 | 79 |
| 82 jobject GetApplicationContext() { | 80 jobject GetApplicationContext() { |
| 83 DCHECK(g_application_context); | 81 DCHECK(g_application_context); |
| 84 return g_application_context; | 82 return g_application_context; |
| 85 } | 83 } |
| 86 | 84 |
| 87 jmethodID GetMethodIDFromClassName(JNIEnv* env, | 85 jmethodID GetMethodIDFromClassName(JNIEnv* env, |
| 88 const char* class_name, | 86 const char* class_name, |
| 89 const char* method, | 87 const char* method, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 bool CheckException(JNIEnv* env) { | 160 bool CheckException(JNIEnv* env) { |
| 163 if (env->ExceptionCheck() == JNI_FALSE) | 161 if (env->ExceptionCheck() == JNI_FALSE) |
| 164 return false; | 162 return false; |
| 165 env->ExceptionDescribe(); | 163 env->ExceptionDescribe(); |
| 166 env->ExceptionClear(); | 164 env->ExceptionClear(); |
| 167 return true; | 165 return true; |
| 168 } | 166 } |
| 169 | 167 |
| 170 } // namespace android | 168 } // namespace android |
| 171 } // namespace base | 169 } // namespace base |
| OLD | NEW |