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 { | 484 jmethodID GetObjectGetClassID() { |
485 public: | 485 static jmethodID id = GetMethodIDFromClassName(AttachCurrentThread(), |
M-A Ruel
2011/12/01 14:36:03
I'm really not a fan of static locals. Especially
| |
486 static ObjectGetClassID* GetInstance() { | 486 "java/lang/Object", |
487 return Singleton<ObjectGetClassID>::get(); | 487 "getClass", |
M-A Ruel
2011/12/01 14:36:03
If you had used LazyInstance in the first place, y
joth
2011/12/02 11:39:02
I don't quite follow this bit-
- both Singleton<>
| |
488 } | 488 "()Ljava/lang/Class;"); |
489 private: | 489 return id; |
490 friend struct DefaultSingletonTraits<ObjectGetClassID>; | 490 } |
491 ObjectGetClassID() | |
492 : MethodID(AttachCurrentThread(), "java/lang/Object", "getClass", | |
493 "()Ljava/lang/Class;") { | |
494 } | |
495 DISALLOW_COPY_AND_ASSIGN(ObjectGetClassID); | |
496 }; | |
497 | 491 |
498 class ClassGetMethodsID : public MethodID { | 492 jmethodID GetClassGetMethodsID() { |
499 public: | 493 static jmethodID id = GetMethodIDFromClassName( |
500 static ClassGetMethodsID* GetInstance() { | 494 AttachCurrentThread(), |
501 return Singleton<ClassGetMethodsID>::get(); | 495 "java/lang/Class", |
502 } | 496 "getMethods", |
503 private: | 497 "()[Ljava/lang/reflect/Method;"); |
504 friend struct DefaultSingletonTraits<ClassGetMethodsID>; | 498 return id; |
505 ClassGetMethodsID() | 499 } |
506 : MethodID(AttachCurrentThread(), "java/lang/Class", "getMethods", | |
507 "()[Ljava/lang/reflect/Method;") { | |
508 } | |
509 DISALLOW_COPY_AND_ASSIGN(ClassGetMethodsID); | |
510 }; | |
511 | 500 |
512 } // namespace | 501 } // namespace |
513 | 502 |
514 | 503 |
515 NPObject* JavaBoundObject::Create(const JavaRef<jobject>& object) { | 504 NPObject* JavaBoundObject::Create(const JavaRef<jobject>& object) { |
516 // The first argument (a plugin's instance handle) is passed through to the | 505 // 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. | 506 // 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. | 507 // The object is created with a ref count of one. |
519 NPObject* np_object = WebBindings::createObject(0, const_cast<NPClass*>( | 508 NPObject* np_object = WebBindings::createObject(0, const_cast<NPClass*>( |
520 &JavaNPObject::kNPClass)); | 509 &JavaNPObject::kNPClass)); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 return true; | 573 return true; |
585 } | 574 } |
586 | 575 |
587 void JavaBoundObject::EnsureMethodsAreSetUp() const { | 576 void JavaBoundObject::EnsureMethodsAreSetUp() const { |
588 if (!methods_.empty()) { | 577 if (!methods_.empty()) { |
589 return; | 578 return; |
590 } | 579 } |
591 | 580 |
592 JNIEnv* env = AttachCurrentThread(); | 581 JNIEnv* env = AttachCurrentThread(); |
593 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( | 582 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( |
594 env->CallObjectMethod(java_object_, | 583 env->CallObjectMethod(java_object_, GetObjectGetClassID()))); |
595 ObjectGetClassID::GetInstance()->id()))); | |
596 ScopedJavaLocalRef<jobjectArray> methods(env, static_cast<jobjectArray>( | 584 ScopedJavaLocalRef<jobjectArray> methods(env, static_cast<jobjectArray>( |
597 env->CallObjectMethod(clazz.obj(), | 585 env->CallObjectMethod(clazz.obj(), GetClassGetMethodsID()))); |
598 ClassGetMethodsID::GetInstance()->id()))); | |
599 size_t num_methods = env->GetArrayLength(methods.obj()); | 586 size_t num_methods = env->GetArrayLength(methods.obj()); |
600 DCHECK(num_methods) << "Java objects always have public methods"; | 587 DCHECK(num_methods) << "Java objects always have public methods"; |
601 for (size_t i = 0; i < num_methods; ++i) { | 588 for (size_t i = 0; i < num_methods; ++i) { |
602 ScopedJavaLocalRef<jobject> java_method( | 589 ScopedJavaLocalRef<jobject> java_method( |
603 env, | 590 env, |
604 env->GetObjectArrayElement(methods.obj(), i)); | 591 env->GetObjectArrayElement(methods.obj(), i)); |
605 JavaMethod* method = new JavaMethod(java_method); | 592 JavaMethod* method = new JavaMethod(java_method); |
606 methods_.insert(std::make_pair(method->name(), method)); | 593 methods_.insert(std::make_pair(method->name(), method)); |
607 } | 594 } |
608 } | 595 } |
OLD | NEW |