diff options
author | bulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-26 17:00:54 +0000 |
---|---|---|
committer | bulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-26 17:00:54 +0000 |
commit | 8e7415e446a2379aecead2903cbc96a34e02fe98 (patch) | |
tree | 95523928701276fdc2acfe4c0611daa4170bd3f1 /base | |
parent | d29242852b7ed8e2009fd1641a53ef213c48065b (diff) | |
download | chromium_src-8e7415e446a2379aecead2903cbc96a34e02fe98.zip chromium_src-8e7415e446a2379aecead2903cbc96a34e02fe98.tar.gz chromium_src-8e7415e446a2379aecead2903cbc96a34e02fe98.tar.bz2 |
Android: cleanup jni_android to minimize external dependencies.
Other projects will be using the JNI bindings.
- Move GetMethodIDFromClassName down to content/browser/renderer_host/java,
the only place where it's used.
- Remove a few methods that are no longer in use.
- Trim down dependencies.
BUG=
Review URL: https://codereview.chromium.org/23835020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@225471 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/android/jni_android.cc | 123 | ||||
-rw-r--r-- | base/android/jni_android.h | 36 | ||||
-rw-r--r-- | base/android/jni_android_unittest.cc | 80 |
3 files changed, 0 insertions, 239 deletions
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc index 0b13699..6dc6a8d 100644 --- a/base/android/jni_android.cc +++ b/base/android/jni_android.cc @@ -10,48 +10,17 @@ #include "base/android/jni_string.h" #include "base/lazy_instance.h" #include "base/logging.h" -#include "base/threading/platform_thread.h" namespace { using base::android::GetClass; using base::android::MethodID; using base::android::ScopedJavaLocalRef; -struct MethodIdentifier { - const char* class_name; - const char* method; - const char* jni_signature; - - bool operator<(const MethodIdentifier& other) const { - int r = strcmp(class_name, other.class_name); - if (r < 0) { - return true; - } else if (r > 0) { - return false; - } - - r = strcmp(method, other.method); - if (r < 0) { - return true; - } else if (r > 0) { - return false; - } - - return strcmp(jni_signature, other.jni_signature) < 0; - } -}; - -typedef std::map<MethodIdentifier, jmethodID> MethodIDMap; - -const base::subtle::AtomicWord kUnlocked = 0; -const base::subtle::AtomicWord kLocked = 1; -base::subtle::AtomicWord g_method_id_map_lock = kUnlocked; JavaVM* g_jvm = NULL; // Leak the global app context, as it is used from a non-joinable worker thread // that may still be running at shutdown. There is no harm in doing this. base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky g_application_context = LAZY_INSTANCE_INITIALIZER; -base::LazyInstance<MethodIDMap> g_method_id_map = LAZY_INSTANCE_INITIALIZER; std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { ScopedJavaLocalRef<jclass> throwable_clazz = @@ -144,17 +113,6 @@ ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { return ScopedJavaLocalRef<jclass>(env, clazz); } -bool HasClass(JNIEnv* env, const char* class_name) { - ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); - if (!clazz.obj()) { - ClearException(env); - return false; - } - bool error = ClearException(env); - DCHECK(!error); - return true; -} - template<MethodID::Type type> jmethodID MethodID::Get(JNIEnv* env, jclass clazz, @@ -207,87 +165,6 @@ template jmethodID MethodID::LazyGet<MethodID::TYPE_INSTANCE>( JNIEnv* env, jclass clazz, const char* method_name, const char* jni_signature, base::subtle::AtomicWord* atomic_method_id); -jfieldID GetFieldID(JNIEnv* env, - const JavaRef<jclass>& clazz, - const char* field_name, - const char* jni_signature) { - jfieldID field_id = env->GetFieldID(clazz.obj(), field_name, jni_signature); - CHECK(!ClearException(env) && field_id) << "Failed to find field " << - field_name << " " << jni_signature; - return field_id; -} - -bool HasField(JNIEnv* env, - const JavaRef<jclass>& clazz, - const char* field_name, - const char* jni_signature) { - jfieldID field_id = env->GetFieldID(clazz.obj(), field_name, jni_signature); - if (!field_id) { - ClearException(env); - return false; - } - bool error = ClearException(env); - DCHECK(!error); - return true; -} - -jfieldID GetStaticFieldID(JNIEnv* env, - const JavaRef<jclass>& clazz, - const char* field_name, - const char* jni_signature) { - jfieldID field_id = - env->GetStaticFieldID(clazz.obj(), field_name, jni_signature); - CHECK(!ClearException(env) && field_id) << "Failed to find static field " << - field_name << " " << jni_signature; - return field_id; -} - -jmethodID GetMethodIDFromClassName(JNIEnv* env, - const char* class_name, - const char* method, - const char* jni_signature) { - MethodIdentifier key; - key.class_name = class_name; - key.method = method; - key.jni_signature = jni_signature; - - MethodIDMap* map = g_method_id_map.Pointer(); - bool found = false; - - while (base::subtle::Acquire_CompareAndSwap(&g_method_id_map_lock, - kUnlocked, - kLocked) != kUnlocked) { - base::PlatformThread::YieldCurrentThread(); - } - MethodIDMap::const_iterator iter = map->find(key); - if (iter != map->end()) { - found = true; - } - base::subtle::Release_Store(&g_method_id_map_lock, kUnlocked); - - // Addition to the map does not invalidate this iterator. - if (found) { - return iter->second; - } - - ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); - jmethodID id = MethodID::Get<MethodID::TYPE_INSTANCE>( - env, clazz.obj(), method, jni_signature); - - while (base::subtle::Acquire_CompareAndSwap(&g_method_id_map_lock, - kUnlocked, - kLocked) != kUnlocked) { - base::PlatformThread::YieldCurrentThread(); - } - // Another thread may have populated the map already. - std::pair<MethodIDMap::const_iterator, bool> result = - map->insert(std::make_pair(key, id)); - DCHECK_EQ(id, result.first->second); - base::subtle::Release_Store(&g_method_id_map_lock, kUnlocked); - - return id; -} - bool HasException(JNIEnv* env) { return env->ExceptionCheck() != JNI_FALSE; } diff --git a/base/android/jni_android.h b/base/android/jni_android.h index 83c9bc8..81ce3fd 100644 --- a/base/android/jni_android.h +++ b/base/android/jni_android.h @@ -56,9 +56,6 @@ const BASE_EXPORT jobject GetApplicationContext(); BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); -// Returns true iff the class |class_name| could be found. -BASE_EXPORT bool HasClass(JNIEnv* env, const char* class_name); - // This class is a wrapper for JNIEnv Get(Static)MethodID. class BASE_EXPORT MethodID { public: @@ -86,39 +83,6 @@ class BASE_EXPORT MethodID { base::subtle::AtomicWord* atomic_method_id); }; -// Gets the method ID from the class name. Clears the pending Java exception -// and returns NULL if the method is not found. Caches results. Note that -// MethodID::Get() above avoids a class lookup, but does not cache results. -// Strings passed to this function are held in the cache and MUST remain valid -// beyond the duration of all future calls to this function, across all -// threads. In practice, this means that the function should only be used with -// string constants. -BASE_EXPORT jmethodID GetMethodIDFromClassName(JNIEnv* env, - const char* class_name, - const char* method, - const char* jni_signature); - -// Gets the field ID for a class field. -// This method triggers a fatal assertion if the field could not be found. -BASE_EXPORT jfieldID GetFieldID(JNIEnv* env, - const JavaRef<jclass>& clazz, - const char* field_name, - const char* jni_signature); - -// Returns true if |clazz| as a field with the given name and signature. -// TODO(jcivelli): Determine whether we explicitly have to pass the environment. -BASE_EXPORT bool HasField(JNIEnv* env, - const JavaRef<jclass>& clazz, - const char* field_name, - const char* jni_signature); - -// Gets the field ID for a static class field. -// This method triggers a fatal assertion if the field could not be found. -BASE_EXPORT jfieldID GetStaticFieldID(JNIEnv* env, - const JavaRef<jclass>& clazz, - const char* field_name, - const char* jni_signature); - // Returns true if an exception is pending in the provided JNIEnv*. BASE_EXPORT bool HasException(JNIEnv* env); diff --git a/base/android/jni_android_unittest.cc b/base/android/jni_android_unittest.cc index 920b395..dabd480 100644 --- a/base/android/jni_android_unittest.cc +++ b/base/android/jni_android_unittest.cc @@ -13,86 +13,6 @@ namespace android { namespace { -const char kJavaLangObject[] = "java/lang/Object"; -const char kGetClass[] = "getClass"; -const char kToString[] = "toString"; -const char kReturningJavaLangClass[] = "()Ljava/lang/Class;"; -const char kReturningJavaLangString[] = "()Ljava/lang/String;"; - -const char* g_last_method; -const char* g_last_jni_signature; -jmethodID g_last_method_id; - -const JNINativeInterface* g_previous_functions; - -jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method, - const char* jni_signature) { - g_last_method = method; - g_last_jni_signature = jni_signature; - g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method, - jni_signature); - return g_last_method_id; -} - -} // namespace - -class JNIAndroidTest : public testing::Test { - protected: - virtual void SetUp() { - JNIEnv* env = AttachCurrentThread(); - g_previous_functions = env->functions; - hooked_functions = *g_previous_functions; - env->functions = &hooked_functions; - hooked_functions.GetMethodID = &GetMethodIDWrapper; - } - - virtual void TearDown() { - JNIEnv* env = AttachCurrentThread(); - env->functions = g_previous_functions; - Reset(); - } - - void Reset() { - g_last_method = 0; - g_last_jni_signature = 0; - g_last_method_id = NULL; - } - // Needed to cleanup the cached method map in the implementation between - // runs (e.g. if using --gtest_repeat) - base::ShadowingAtExitManager exit_manager; - // From JellyBean release, the instance of this struct provided in JNIEnv is - // read-only, so we deep copy it to allow individual functions to be hooked. - JNINativeInterface hooked_functions; -}; - -TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) { - JNIEnv* env = AttachCurrentThread(); - - Reset(); - jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, - kReturningJavaLangClass); - EXPECT_STREQ(kGetClass, g_last_method); - EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature); - EXPECT_EQ(g_last_method_id, id1); - - Reset(); - jmethodID id2 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, - kReturningJavaLangClass); - EXPECT_STREQ(0, g_last_method); - EXPECT_STREQ(0, g_last_jni_signature); - EXPECT_EQ(NULL, g_last_method_id); - EXPECT_EQ(id1, id2); - - Reset(); - jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, - kReturningJavaLangString); - EXPECT_STREQ(kToString, g_last_method); - EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); - EXPECT_EQ(g_last_method_id, id3); -} - -namespace { - base::subtle::AtomicWord g_atomic_id = 0; int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) { jmethodID id = base::android::MethodID::LazyGet< |