OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "content/browser/renderer_host/java/java_type.h" | 12 #include "content/browser/renderer_host/java/java_type.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
14 | 14 |
15 using base::StringPrintf; | 15 using base::StringPrintf; |
16 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
17 using base::android::ConvertUTF8ToJavaString; | 17 using base::android::ConvertUTF8ToJavaString; |
| 18 using base::android::GetMethodIDFromClassName; |
18 using base::android::JavaRef; | 19 using base::android::JavaRef; |
19 using base::android::MethodID; | |
20 using base::android::ScopedJavaGlobalRef; | 20 using base::android::ScopedJavaGlobalRef; |
21 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
22 using WebKit::WebBindings; | 22 using WebKit::WebBindings; |
23 | 23 |
24 // The conversion between JavaScript and Java types is based on the Live | 24 // The conversion between JavaScript and Java types is based on the Live |
25 // Connect 2 spec. See | 25 // Connect 2 spec. See |
26 // http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. | 26 // http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. |
27 | 27 |
28 // Note that in some cases, we differ from from the spec in order to maintain | 28 // Note that in some cases, we differ from from the spec in order to maintain |
29 // existing behavior. These areas are marked LIVECONNECT_COMPLIANCE. We may | 29 // existing behavior. These areas are marked LIVECONNECT_COMPLIANCE. We may |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 case NPVariantType_Object: | 474 case NPVariantType_Object: |
475 return CoerceJavaScriptObjectToJavaValue(variant, target_type); | 475 return CoerceJavaScriptObjectToJavaValue(variant, target_type); |
476 case NPVariantType_Null: | 476 case NPVariantType_Null: |
477 case NPVariantType_Void: | 477 case NPVariantType_Void: |
478 return CoerceJavaScriptNullOrUndefinedToJavaValue(variant, target_type); | 478 return CoerceJavaScriptNullOrUndefinedToJavaValue(variant, target_type); |
479 } | 479 } |
480 NOTREACHED(); | 480 NOTREACHED(); |
481 return jvalue(); | 481 return jvalue(); |
482 } | 482 } |
483 | 483 |
484 class ObjectGetClassID : public MethodID { | |
485 public: | |
486 static ObjectGetClassID* GetInstance() { | |
487 return Singleton<ObjectGetClassID>::get(); | |
488 } | |
489 private: | |
490 friend struct DefaultSingletonTraits<ObjectGetClassID>; | |
491 ObjectGetClassID() | |
492 : MethodID(AttachCurrentThread(), "java/lang/Object", "getClass", | |
493 "()Ljava/lang/Class;") { | |
494 } | |
495 DISALLOW_COPY_AND_ASSIGN(ObjectGetClassID); | |
496 }; | |
497 | |
498 class ClassGetMethodsID : public MethodID { | |
499 public: | |
500 static ClassGetMethodsID* GetInstance() { | |
501 return Singleton<ClassGetMethodsID>::get(); | |
502 } | |
503 private: | |
504 friend struct DefaultSingletonTraits<ClassGetMethodsID>; | |
505 ClassGetMethodsID() | |
506 : MethodID(AttachCurrentThread(), "java/lang/Class", "getMethods", | |
507 "()[Ljava/lang/reflect/Method;") { | |
508 } | |
509 DISALLOW_COPY_AND_ASSIGN(ClassGetMethodsID); | |
510 }; | |
511 | |
512 } // namespace | 484 } // namespace |
513 | 485 |
514 | 486 |
515 NPObject* JavaBoundObject::Create(const JavaRef<jobject>& object) { | 487 NPObject* JavaBoundObject::Create(const JavaRef<jobject>& object) { |
516 // The first argument (a plugin's instance handle) is passed through to the | 488 // The first argument (a plugin's instance handle) is passed through to the |
517 // allocate function directly, and we don't use it, so it's ok to be 0. | 489 // allocate function directly, and we don't use it, so it's ok to be 0. |
518 // The object is created with a ref count of one. | 490 // The object is created with a ref count of one. |
519 NPObject* np_object = WebBindings::createObject(0, const_cast<NPClass*>( | 491 NPObject* np_object = WebBindings::createObject(0, const_cast<NPClass*>( |
520 &JavaNPObject::kNPClass)); | 492 &JavaNPObject::kNPClass)); |
521 // The NPObject takes ownership of the JavaBoundObject. | 493 // The NPObject takes ownership of the JavaBoundObject. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 return true; | 556 return true; |
585 } | 557 } |
586 | 558 |
587 void JavaBoundObject::EnsureMethodsAreSetUp() const { | 559 void JavaBoundObject::EnsureMethodsAreSetUp() const { |
588 if (!methods_.empty()) { | 560 if (!methods_.empty()) { |
589 return; | 561 return; |
590 } | 562 } |
591 | 563 |
592 JNIEnv* env = AttachCurrentThread(); | 564 JNIEnv* env = AttachCurrentThread(); |
593 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( | 565 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( |
594 env->CallObjectMethod(java_object_, | 566 env->CallObjectMethod(java_object_, GetMethodIDFromClassName( |
595 ObjectGetClassID::GetInstance()->id()))); | 567 env, |
| 568 "java/lang/Object", |
| 569 "getClass", |
| 570 "()Ljava/lang/Class;")))); |
596 ScopedJavaLocalRef<jobjectArray> methods(env, static_cast<jobjectArray>( | 571 ScopedJavaLocalRef<jobjectArray> methods(env, static_cast<jobjectArray>( |
597 env->CallObjectMethod(clazz.obj(), | 572 env->CallObjectMethod(clazz.obj(), GetMethodIDFromClassName( |
598 ClassGetMethodsID::GetInstance()->id()))); | 573 env, |
| 574 "java/lang/Class", |
| 575 "getMethods", |
| 576 "()[Ljava/lang/reflect/Method;")))); |
599 size_t num_methods = env->GetArrayLength(methods.obj()); | 577 size_t num_methods = env->GetArrayLength(methods.obj()); |
600 DCHECK(num_methods) << "Java objects always have public methods"; | 578 DCHECK(num_methods) << "Java objects always have public methods"; |
601 for (size_t i = 0; i < num_methods; ++i) { | 579 for (size_t i = 0; i < num_methods; ++i) { |
602 ScopedJavaLocalRef<jobject> java_method( | 580 ScopedJavaLocalRef<jobject> java_method( |
603 env, | 581 env, |
604 env->GetObjectArrayElement(methods.obj(), i)); | 582 env->GetObjectArrayElement(methods.obj(), i)); |
605 JavaMethod* method = new JavaMethod(java_method); | 583 JavaMethod* method = new JavaMethod(java_method); |
606 methods_.insert(std::make_pair(method->name(), method)); | 584 methods_.insert(std::make_pair(method->name(), method)); |
607 } | 585 } |
608 } | 586 } |
OLD | NEW |