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 "content/browser/renderer_host/java/java_method.h" | 5 #include "content/browser/renderer_host/java/java_method.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 NOTREACHED(); | 88 NOTREACHED(); |
89 return EmptyString(); | 89 return EmptyString(); |
90 } | 90 } |
91 | 91 |
92 } // namespace | 92 } // namespace |
93 | 93 |
94 JavaMethod::JavaMethod(const base::android::JavaRef<jobject>& method) | 94 JavaMethod::JavaMethod(const base::android::JavaRef<jobject>& method) |
95 : java_method_(method), | 95 : java_method_(method), |
96 have_calculated_num_parameters_(false), | 96 have_calculated_num_parameters_(false), |
97 id_(NULL) { | 97 id_(NULL) { |
98 JNIEnv* env = java_method_.env(); | 98 JNIEnv* env = AttachCurrentThread(); |
99 // On construction, we do nothing except get the name. Everything else is | 99 // On construction, we do nothing except get the name. Everything else is |
100 // done lazily. | 100 // done lazily. |
101 ScopedJavaLocalRef<jstring> name(env, static_cast<jstring>( | 101 ScopedJavaLocalRef<jstring> name(env, static_cast<jstring>( |
102 env->CallObjectMethod(java_method_.obj(), GetMethodIDFromClassName( | 102 env->CallObjectMethod(java_method_.obj(), GetMethodIDFromClassName( |
103 env, | 103 env, |
104 kJavaLangReflectMethod, | 104 kJavaLangReflectMethod, |
105 kGetName, | 105 kGetName, |
106 kReturningJavaLangString)))); | 106 kReturningJavaLangString)))); |
107 name_ = ConvertJavaStringToUTF8(name); | 107 name_ = ConvertJavaStringToUTF8(name); |
108 } | 108 } |
(...skipping 23 matching lines...) Expand all Loading... |
132 | 132 |
133 void JavaMethod::EnsureNumParametersIsSetUp() const { | 133 void JavaMethod::EnsureNumParametersIsSetUp() const { |
134 if (have_calculated_num_parameters_) { | 134 if (have_calculated_num_parameters_) { |
135 return; | 135 return; |
136 } | 136 } |
137 have_calculated_num_parameters_ = true; | 137 have_calculated_num_parameters_ = true; |
138 | 138 |
139 // The number of parameters will be used frequently when determining | 139 // The number of parameters will be used frequently when determining |
140 // whether to call this method. We don't get the ID etc until actually | 140 // whether to call this method. We don't get the ID etc until actually |
141 // required. | 141 // required. |
142 JNIEnv* env = java_method_.env(); | 142 JNIEnv* env = AttachCurrentThread(); |
143 ScopedJavaLocalRef<jarray> parameters(env, static_cast<jarray>( | 143 ScopedJavaLocalRef<jarray> parameters(env, static_cast<jarray>( |
144 env->CallObjectMethod(java_method_.obj(), GetMethodIDFromClassName( | 144 env->CallObjectMethod(java_method_.obj(), GetMethodIDFromClassName( |
145 env, | 145 env, |
146 kJavaLangReflectMethod, | 146 kJavaLangReflectMethod, |
147 kGetParameterTypes, | 147 kGetParameterTypes, |
148 kReturningJavaLangClassArray)))); | 148 kReturningJavaLangClassArray)))); |
149 num_parameters_ = env->GetArrayLength(parameters.obj()); | 149 num_parameters_ = env->GetArrayLength(parameters.obj()); |
150 } | 150 } |
151 | 151 |
152 void JavaMethod::EnsureTypesAndIDAreSetUp() const { | 152 void JavaMethod::EnsureTypesAndIDAreSetUp() const { |
153 if (id_) { | 153 if (id_) { |
154 return; | 154 return; |
155 } | 155 } |
156 | 156 |
157 // Get the parameters | 157 // Get the parameters |
158 JNIEnv* env = java_method_.env(); | 158 JNIEnv* env = AttachCurrentThread(); |
159 ScopedJavaLocalRef<jobjectArray> parameters(env, static_cast<jobjectArray>( | 159 ScopedJavaLocalRef<jobjectArray> parameters(env, static_cast<jobjectArray>( |
160 env->CallObjectMethod(java_method_.obj(), GetMethodIDFromClassName( | 160 env->CallObjectMethod(java_method_.obj(), GetMethodIDFromClassName( |
161 env, | 161 env, |
162 kJavaLangReflectMethod, | 162 kJavaLangReflectMethod, |
163 kGetParameterTypes, | 163 kGetParameterTypes, |
164 kReturningJavaLangClassArray)))); | 164 kReturningJavaLangClassArray)))); |
165 // Usually, this will already have been called. | 165 // Usually, this will already have been called. |
166 EnsureNumParametersIsSetUp(); | 166 EnsureNumParametersIsSetUp(); |
167 DCHECK_EQ(num_parameters_, | 167 DCHECK_EQ(num_parameters_, |
168 static_cast<size_t>(env->GetArrayLength(parameters.obj()))); | 168 static_cast<size_t>(env->GetArrayLength(parameters.obj()))); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 env, | 225 env, |
226 kJavaLangReflectMethod, | 226 kJavaLangReflectMethod, |
227 kGetDeclaringClass, | 227 kGetDeclaringClass, |
228 kReturningJavaLangClass)))); | 228 kReturningJavaLangClass)))); |
229 id_ = is_static ? | 229 id_ = is_static ? |
230 GetStaticMethodID(env, declaring_class, name_.c_str(), | 230 GetStaticMethodID(env, declaring_class, name_.c_str(), |
231 signature.c_str()) : | 231 signature.c_str()) : |
232 GetMethodID(env, declaring_class, name_.c_str(), signature.c_str()); | 232 GetMethodID(env, declaring_class, name_.c_str(), signature.c_str()); |
233 java_method_.Reset(); | 233 java_method_.Reset(); |
234 } | 234 } |
OLD | NEW |