Chromium Code Reviews| Index: base/android/jni_generator/jni_generator.py |
| diff --git a/base/android/jni_generator/jni_generator.py b/base/android/jni_generator/jni_generator.py |
| index 88332fafd04bb8afc0526de8a3cf58e2fa5d7879..824bc59a763df0eba94b815d9cd210a0ed764a1c 100755 |
| --- a/base/android/jni_generator/jni_generator.py |
| +++ b/base/android/jni_generator/jni_generator.py |
| @@ -731,7 +731,7 @@ class InlHeaderFileGenerator(object): |
| def GetContent(self): |
| """Returns the content of the JNI binding file.""" |
| template = Template("""\ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -947,11 +947,11 @@ ${CALLED_BY_NATIVES} |
| template = Template("""\ |
| const int kMethods${JAVA_CLASS}Size = arraysize(kMethods${JAVA_CLASS}); |
| - if (env->RegisterNatives(g_${JAVA_CLASS}_clazz, |
| + if (env->RegisterNatives(${JAVA_CLASS}_clazz(env), |
| kMethods${JAVA_CLASS}, |
| kMethods${JAVA_CLASS}Size) < 0) { |
| jni_generator::HandleRegistrationError( |
| - env, g_${JAVA_CLASS}_clazz, __FILE__); |
| + env, ${JAVA_CLASS}_clazz(env), __FILE__); |
| return false; |
| } |
| """) |
| @@ -1122,11 +1122,10 @@ static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {""" |
| def GetCalledByNativeValues(self, called_by_native): |
| """Fills in necessary values for the CalledByNative methods.""" |
| + java_class = called_by_native.java_class_name or self.class_name |
| if called_by_native.static or called_by_native.is_constructor: |
| first_param_in_declaration = '' |
| - first_param_in_call = ('g_%s_clazz' % |
| - (called_by_native.java_class_name or |
| - self.class_name)) |
| + first_param_in_call = ('%s_clazz(env)' % (java_class,)) |
|
rmcilroy
2014/08/18 15:16:41
nit - remove unnecessary ","
Torne
2014/08/18 15:27:21
The , is what makes it a 1-element tuple instead o
rmcilroy
2014/08/18 15:32:17
That makes sense, thanks. I still think it doesn'
mkosiba (inactive)
2014/08/19 09:50:15
Done.
|
| else: |
| first_param_in_declaration = ', jobject obj' |
| first_param_in_call = 'obj' |
| @@ -1160,7 +1159,7 @@ static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {""" |
| else: |
| return_clause = 'return ret;' |
| return { |
| - 'JAVA_CLASS': called_by_native.java_class_name or self.class_name, |
| + 'JAVA_CLASS': java_class, |
| 'RETURN_TYPE': return_type, |
| 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 'RETURN_DECLARATION': return_declaration, |
| @@ -1204,7 +1203,7 @@ static base::subtle::AtomicWord g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = 0; |
| ${FUNCTION_HEADER} |
| /* Must call RegisterNativesImpl() */ |
| CHECK_CLAZZ(env, ${FIRST_PARAM_IN_CALL}, |
| - g_${JAVA_CLASS}_clazz${OPTIONAL_ERROR_RETURN}); |
| + ${JAVA_CLASS}_clazz(env)${OPTIONAL_ERROR_RETURN}); |
| jmethodID method_id = |
| ${GET_METHOD_ID_IMPL} |
| ${RETURN_DECLARATION} |
| @@ -1263,10 +1262,22 @@ const char k${JAVA_CLASS}ClassPath[] = "${JNI_CLASS_PATH}";""") |
| } |
| ret += [template.substitute(values)] |
| ret += '' |
| - for clazz in called_by_native_classes: |
| + |
| + if self.options.native_exports: |
| + template = Template("""\ |
| +// Leaking this jclass as we cannot use LazyInstance from some threads. |
| +base::subtle::AtomicWord g_${JAVA_CLASS}_clazz = 0; |
| +jclass ${JAVA_CLASS}_clazz(JNIEnv* env) { |
| + return base::android::LazyGetClass(env, k${JAVA_CLASS}ClassPath, \ |
| + &g_${JAVA_CLASS}_clazz); |
| +}""") |
| + else: |
| template = Template("""\ |
| // Leaking this jclass as we cannot use LazyInstance from some threads. |
| -jclass g_${JAVA_CLASS}_clazz = NULL;""") |
| +jclass g_${JAVA_CLASS}_clazz = NULL; |
| +jclass ${JAVA_CLASS}_clazz(JNIEnv*) { return g_${JAVA_CLASS}_clazz; }""") |
| + |
| + for clazz in called_by_native_classes: |
| values = { |
| 'JAVA_CLASS': clazz, |
| } |
| @@ -1276,9 +1287,16 @@ jclass g_${JAVA_CLASS}_clazz = NULL;""") |
| def GetFindClasses(self): |
| """Returns the imlementation of FindClass for all known classes.""" |
| if self.init_native: |
| - template = Template("""\ |
| + if self.options.native_exports: |
| + template = Template("""\ |
| + base::subtle::Release_Store(&g_${JAVA_CLASS}_clazz, |
| + static_cast<base::subtle::AtomicWord>(env->NewWeakGlobalRef(clazz));""") |
| + else: |
| + template = Template("""\ |
| g_${JAVA_CLASS}_clazz = static_cast<jclass>(env->NewWeakGlobalRef(clazz));""") |
| else: |
| + if self.options.native_exports: |
| + return '\n' |
| template = Template("""\ |
| g_${JAVA_CLASS}_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( |
| base::android::GetClass(env, k${JAVA_CLASS}ClassPath).obj()));""") |
| @@ -1293,13 +1311,13 @@ jclass g_${JAVA_CLASS}_clazz = NULL;""") |
| if self.options.eager_called_by_natives: |
| template = Template("""\ |
| env->Get${STATIC_METHOD_PART}MethodID( |
| - g_${JAVA_CLASS}_clazz, |
| + ${JAVA_CLASS}_clazz(env), |
| "${JNI_NAME}", ${JNI_SIGNATURE});""") |
| else: |
| template = Template("""\ |
| base::android::MethodID::LazyGet< |
| base::android::MethodID::TYPE_${STATIC}>( |
| - env, g_${JAVA_CLASS}_clazz, |
| + env, ${JAVA_CLASS}_clazz(env), |
| "${JNI_NAME}", |
| ${JNI_SIGNATURE}, |
| &g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME}); |