OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 DCHECK(g_application_context.Get().is_null()); | 79 DCHECK(g_application_context.Get().is_null()); |
80 g_application_context.Get().Reset(context); | 80 g_application_context.Get().Reset(context); |
81 } | 81 } |
82 | 82 |
83 const jobject GetApplicationContext() { | 83 const jobject GetApplicationContext() { |
84 DCHECK(!g_application_context.Get().is_null()); | 84 DCHECK(!g_application_context.Get().is_null()); |
85 return g_application_context.Get().obj(); | 85 return g_application_context.Get().obj(); |
86 } | 86 } |
87 | 87 |
88 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { | 88 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { |
| 89 return ScopedJavaLocalRef<jclass>(env, GetUnscopedClass(env, class_name)); |
| 90 } |
| 91 |
| 92 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) { |
89 jclass clazz = env->FindClass(class_name); | 93 jclass clazz = env->FindClass(class_name); |
90 CHECK(clazz && !ClearException(env)) << "Failed to find class " << class_name; | 94 CHECK(clazz && !ClearException(env)) << "Failed to find class " << class_name; |
91 return ScopedJavaLocalRef<jclass>(env, clazz); | 95 return clazz; |
92 } | 96 } |
93 | 97 |
94 bool HasClass(JNIEnv* env, const char* class_name) { | 98 bool HasClass(JNIEnv* env, const char* class_name) { |
95 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); | 99 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); |
96 if (!clazz.obj()) { | 100 if (!clazz.obj()) { |
97 ClearException(env); | 101 ClearException(env); |
98 return false; | 102 return false; |
99 } | 103 } |
100 bool error = ClearException(env); | 104 bool error = ClearException(env); |
101 DCHECK(!error); | 105 DCHECK(!error); |
102 return true; | 106 return true; |
103 } | 107 } |
104 | 108 |
105 jmethodID GetMethodID(JNIEnv* env, | 109 jmethodID GetMethodID(JNIEnv* env, |
106 const JavaRef<jclass>& clazz, | 110 const JavaRef<jclass>& clazz, |
107 const char* method_name, | 111 const char* method_name, |
108 const char* jni_signature) { | 112 const char* jni_signature) { |
| 113 // We can't use clazz.env() as that may be from a different thread. |
| 114 return GetMethodID(env, clazz.obj(), method_name, jni_signature); |
| 115 } |
| 116 |
| 117 jmethodID GetMethodID(JNIEnv* env, |
| 118 jclass clazz, |
| 119 const char* method_name, |
| 120 const char* jni_signature) { |
109 jmethodID method_id = | 121 jmethodID method_id = |
110 env->GetMethodID(clazz.obj(), method_name, jni_signature); | 122 env->GetMethodID(clazz, method_name, jni_signature); |
111 CHECK(method_id && !ClearException(env)) << "Failed to find method " << | 123 CHECK(method_id && !ClearException(env)) << "Failed to find method " << |
112 method_name << " " << jni_signature; | 124 method_name << " " << jni_signature; |
113 return method_id; | 125 return method_id; |
114 } | 126 } |
115 | 127 |
116 jmethodID GetStaticMethodID(JNIEnv* env, | 128 jmethodID GetStaticMethodID(JNIEnv* env, |
117 const JavaRef<jclass>& clazz, | 129 const JavaRef<jclass>& clazz, |
118 const char* method_name, | 130 const char* method_name, |
119 const char* jni_signature) { | 131 const char* jni_signature) { |
| 132 return GetStaticMethodID(env, clazz.obj(), method_name, |
| 133 jni_signature); |
| 134 } |
| 135 |
| 136 jmethodID GetStaticMethodID(JNIEnv* env, |
| 137 jclass clazz, |
| 138 const char* method_name, |
| 139 const char* jni_signature) { |
120 jmethodID method_id = | 140 jmethodID method_id = |
121 env->GetStaticMethodID(clazz.obj(), method_name, jni_signature); | 141 env->GetStaticMethodID(clazz, method_name, jni_signature); |
122 CHECK(method_id && !ClearException(env)) << "Failed to find static method " << | 142 CHECK(method_id && !ClearException(env)) << "Failed to find static method " << |
123 method_name << " " << jni_signature; | 143 method_name << " " << jni_signature; |
124 return method_id; | 144 return method_id; |
125 } | 145 } |
126 | 146 |
127 bool HasMethod(JNIEnv* env, | 147 bool HasMethod(JNIEnv* env, |
128 const JavaRef<jclass>& clazz, | 148 const JavaRef<jclass>& clazz, |
129 const char* method_name, | 149 const char* method_name, |
130 const char* jni_signature) { | 150 const char* jni_signature) { |
131 jmethodID method_id = | 151 jmethodID method_id = |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 256 |
237 void CheckException(JNIEnv* env) { | 257 void CheckException(JNIEnv* env) { |
238 if (HasException(env)) { | 258 if (HasException(env)) { |
239 env->ExceptionDescribe(); | 259 env->ExceptionDescribe(); |
240 CHECK(false); | 260 CHECK(false); |
241 } | 261 } |
242 } | 262 } |
243 | 263 |
244 } // namespace android | 264 } // namespace android |
245 } // namespace base | 265 } // namespace base |
OLD | NEW |