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_bound_object.h" | 5 #include "content/browser/renderer_host/java/java_bound_object.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/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 // The object is created with a ref count of one. | 684 // The object is created with a ref count of one. |
685 NPObject* np_object = WebBindings::createObject(0, const_cast<NPClass*>( | 685 NPObject* np_object = WebBindings::createObject(0, const_cast<NPClass*>( |
686 &JavaNPObject::kNPClass)); | 686 &JavaNPObject::kNPClass)); |
687 // The NPObject takes ownership of the JavaBoundObject. | 687 // The NPObject takes ownership of the JavaBoundObject. |
688 reinterpret_cast<JavaNPObject*>(np_object)->bound_object = | 688 reinterpret_cast<JavaNPObject*>(np_object)->bound_object = |
689 new JavaBoundObject(object); | 689 new JavaBoundObject(object); |
690 return np_object; | 690 return np_object; |
691 } | 691 } |
692 | 692 |
693 JavaBoundObject::JavaBoundObject(const JavaRef<jobject>& object) | 693 JavaBoundObject::JavaBoundObject(const JavaRef<jobject>& object) |
694 : java_object_(object.env()->NewGlobalRef(object.obj())) { | 694 : java_object_(object) { |
695 // We don't do anything with our Java object when first created. We do it all | 695 // We don't do anything with our Java object when first created. We do it all |
696 // lazily when a method is first invoked. | 696 // lazily when a method is first invoked. |
697 } | 697 } |
698 | 698 |
699 JavaBoundObject::~JavaBoundObject() { | 699 JavaBoundObject::~JavaBoundObject() { |
700 // Use the current thread's JNI env to release our global ref to the Java | |
701 // object. | |
702 AttachCurrentThread()->DeleteGlobalRef(java_object_); | |
703 } | 700 } |
704 | 701 |
705 jobject JavaBoundObject::GetJavaObject(NPObject* object) { | 702 jobject JavaBoundObject::GetJavaObject(NPObject* object) { |
706 DCHECK_EQ(&JavaNPObject::kNPClass, object->_class); | 703 DCHECK_EQ(&JavaNPObject::kNPClass, object->_class); |
707 JavaBoundObject* jbo = reinterpret_cast<JavaNPObject*>(object)->bound_object; | 704 JavaBoundObject* jbo = reinterpret_cast<JavaNPObject*>(object)->bound_object; |
708 return jbo->java_object_; | 705 return jbo->java_object_.obj(); |
709 } | 706 } |
710 | 707 |
711 bool JavaBoundObject::HasMethod(const std::string& name) const { | 708 bool JavaBoundObject::HasMethod(const std::string& name) const { |
712 EnsureMethodsAreSetUp(); | 709 EnsureMethodsAreSetUp(); |
713 return methods_.find(name) != methods_.end(); | 710 return methods_.find(name) != methods_.end(); |
714 } | 711 } |
715 | 712 |
716 bool JavaBoundObject::Invoke(const std::string& name, const NPVariant* args, | 713 bool JavaBoundObject::Invoke(const std::string& name, const NPVariant* args, |
717 size_t arg_count, NPVariant* result) { | 714 size_t arg_count, NPVariant* result) { |
718 EnsureMethodsAreSetUp(); | 715 EnsureMethodsAreSetUp(); |
(...skipping 20 matching lines...) Expand all Loading... |
739 | 736 |
740 // Coerce | 737 // Coerce |
741 std::vector<jvalue> parameters(arg_count); | 738 std::vector<jvalue> parameters(arg_count); |
742 for (size_t i = 0; i < arg_count; ++i) { | 739 for (size_t i = 0; i < arg_count; ++i) { |
743 parameters[i] = CoerceJavaScriptValueToJavaValue(args[i], | 740 parameters[i] = CoerceJavaScriptValueToJavaValue(args[i], |
744 method->parameter_type(i), | 741 method->parameter_type(i), |
745 true); | 742 true); |
746 } | 743 } |
747 | 744 |
748 // Call | 745 // Call |
749 *result = CallJNIMethod(java_object_, method->return_type(), method->id(), | 746 *result = CallJNIMethod(java_object_.obj(), method->return_type(), |
750 ¶meters[0]); | 747 method->id(), ¶meters[0]); |
751 return true; | 748 return true; |
752 } | 749 } |
753 | 750 |
754 void JavaBoundObject::EnsureMethodsAreSetUp() const { | 751 void JavaBoundObject::EnsureMethodsAreSetUp() const { |
755 if (!methods_.empty()) { | 752 if (!methods_.empty()) { |
756 return; | 753 return; |
757 } | 754 } |
758 | 755 |
759 JNIEnv* env = AttachCurrentThread(); | 756 JNIEnv* env = AttachCurrentThread(); |
760 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( | 757 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( |
761 env->CallObjectMethod(java_object_, GetMethodIDFromClassName( | 758 env->CallObjectMethod(java_object_.obj(), GetMethodIDFromClassName( |
762 env, | 759 env, |
763 kJavaLangObject, | 760 kJavaLangObject, |
764 kGetClass, | 761 kGetClass, |
765 kReturningJavaLangClass)))); | 762 kReturningJavaLangClass)))); |
766 ScopedJavaLocalRef<jobjectArray> methods(env, static_cast<jobjectArray>( | 763 ScopedJavaLocalRef<jobjectArray> methods(env, static_cast<jobjectArray>( |
767 env->CallObjectMethod(clazz.obj(), GetMethodIDFromClassName( | 764 env->CallObjectMethod(clazz.obj(), GetMethodIDFromClassName( |
768 env, | 765 env, |
769 kJavaLangClass, | 766 kJavaLangClass, |
770 kGetMethods, | 767 kGetMethods, |
771 kReturningJavaLangReflectMethodArray)))); | 768 kReturningJavaLangReflectMethodArray)))); |
772 size_t num_methods = env->GetArrayLength(methods.obj()); | 769 size_t num_methods = env->GetArrayLength(methods.obj()); |
773 DCHECK(num_methods) << "Java objects always have public methods"; | 770 DCHECK(num_methods) << "Java objects always have public methods"; |
774 for (size_t i = 0; i < num_methods; ++i) { | 771 for (size_t i = 0; i < num_methods; ++i) { |
775 ScopedJavaLocalRef<jobject> java_method( | 772 ScopedJavaLocalRef<jobject> java_method( |
776 env, | 773 env, |
777 env->GetObjectArrayElement(methods.obj(), i)); | 774 env->GetObjectArrayElement(methods.obj(), i)); |
778 JavaMethod* method = new JavaMethod(java_method); | 775 JavaMethod* method = new JavaMethod(java_method); |
779 methods_.insert(std::make_pair(method->name(), method)); | 776 methods_.insert(std::make_pair(method->name(), method)); |
780 } | 777 } |
781 } | 778 } |
OLD | NEW |