summaryrefslogtreecommitdiffstats
path: root/android
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2011-02-23 14:28:02 +0000
committerBen Murdoch <benm@google.com>2011-02-28 14:09:05 +0000
commita0a75b28bb1264f9c1fe914ca7c9ef1d37198e48 (patch)
treef448c0907031060c6e50e1c36e15818977a87fe2 /android
parent50bb8b7f64c84a88670da4ed4e47cf9a1a374814 (diff)
downloadexternal_chromium-a0a75b28bb1264f9c1fe914ca7c9ef1d37198e48.zip
external_chromium-a0a75b28bb1264f9c1fe914ca7c9ef1d37198e48.tar.gz
external_chromium-a0a75b28bb1264f9c1fe914ca7c9ef1d37198e48.tar.bz2
Do not cache the JNIEnv pointer.
JNIEnv pointers are per thread and now that we want to read localised strings from Java, we may call this code from different threads. Change-Id: Ifc3596487334a3c8c52687dd1929f3a0300fbccd
Diffstat (limited to 'android')
-rw-r--r--android/app/l10n_util.cc39
1 files changed, 20 insertions, 19 deletions
diff --git a/android/app/l10n_util.cc b/android/app/l10n_util.cc
index 9ea87e8..1787313 100644
--- a/android/app/l10n_util.cc
+++ b/android/app/l10n_util.cc
@@ -29,6 +29,8 @@
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
+#include <utils/threads.h>
+
namespace l10n_util {
class JNIHelper {
@@ -38,46 +40,45 @@ class JNIHelper {
string16 getLocalisedString(int message_id);
private:
- JNIEnv* mEnv;
+ bool mInited;
jclass mClassRef;
string16 mMessageCache[ANDROID_L10N_IDS_MESSAGE_COUNT];
+ android::Mutex mGetStringLock;
};
JNIHelper jniHelper;
JNIHelper::JNIHelper()
- : mEnv(0)
+ : mInited(false)
{
}
JNIHelper::~JNIHelper()
{
- if (mEnv) {
- // Because the stored mEnv could be bad during c++ destruction phase
- // since it's thread local.
- JNIEnv* currentEnv = android::GetJNIEnv();
- if (currentEnv)
- currentEnv->DeleteGlobalRef(mClassRef);
- }
+ JNIEnv* currentEnv = android::GetJNIEnv();
+ if (currentEnv)
+ currentEnv->DeleteGlobalRef(mClassRef);
}
string16 JNIHelper::getLocalisedString(int message_id)
{
- if (!mEnv) {
- mEnv = android::GetJNIEnv();
- jclass localClass = mEnv->FindClass("android/webkit/L10nUtils");
- mClassRef = static_cast<jclass>(mEnv->NewGlobalRef(localClass));
- mEnv->DeleteLocalRef(localClass);
+ android::Mutex::Autolock lock(mGetStringLock);
+ JNIEnv* env = android::GetJNIEnv();
+ if (!mInited) {
+ jclass localClass = env->FindClass("android/webkit/L10nUtils");
+ mClassRef = static_cast<jclass>(env->NewGlobalRef(localClass));
+ env->DeleteLocalRef(localClass);
+ mInited = true;
}
if (!mMessageCache[message_id].empty())
return mMessageCache[message_id];
- DCHECK(mEnv);
- static jmethodID getLocalisedString = mEnv->GetStaticMethodID(mClassRef, "getLocalisedString", "(I)Ljava/lang/String;");
- jstring result = static_cast<jstring>(mEnv->CallStaticObjectMethod(mClassRef, getLocalisedString, message_id));
- string16 str = android::JstringToString16(mEnv, result);
- mEnv->DeleteLocalRef(result);
+
+ static jmethodID getLocalisedString = env->GetStaticMethodID(mClassRef, "getLocalisedString", "(I)Ljava/lang/String;");
+ jstring result = static_cast<jstring>(env->CallStaticObjectMethod(mClassRef, getLocalisedString, message_id));
+ string16 str = android::JstringToString16(env, result);
+ env->DeleteLocalRef(result);
mMessageCache[message_id] = str;
return str;
}