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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 case JavaType::TypeVoid: | 390 case JavaType::TypeVoid: |
391 // Conversion to void must never happen. | 391 // Conversion to void must never happen. |
392 case JavaType::TypeArray: | 392 case JavaType::TypeArray: |
393 case JavaType::TypeObject: | 393 case JavaType::TypeObject: |
394 // Not handled. | 394 // Not handled. |
395 NOTREACHED(); | 395 NOTREACHED(); |
396 } | 396 } |
397 return NULL; | 397 return NULL; |
398 } | 398 } |
399 | 399 |
400 // Note that this only handles primitive types and strings. | 400 // Sets the specified element of the supplied array to the value of the |
401 // supplied jvalue. Requires that the type of the array matches that of the | |
402 // jvalue. Handles only primitive types and strings. Note that in the case of a | |
403 // string, the array takes a new reference to the string object. | |
401 void SetArrayElement(jobject array, | 404 void SetArrayElement(jobject array, |
402 const JavaType& type, | 405 const JavaType& type, |
403 jsize index, | 406 jsize index, |
404 const jvalue& value) { | 407 const jvalue& value) { |
405 JNIEnv* env = AttachCurrentThread(); | 408 JNIEnv* env = AttachCurrentThread(); |
406 switch (type.type) { | 409 switch (type.type) { |
407 case JavaType::TypeBoolean: | 410 case JavaType::TypeBoolean: |
408 env->SetBooleanArrayRegion(static_cast<jbooleanArray>(array), index, 1, | 411 env->SetBooleanArrayRegion(static_cast<jbooleanArray>(array), index, 1, |
409 &value.z); | 412 &value.z); |
410 break; | 413 break; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 // Not handled. | 450 // Not handled. |
448 NOTREACHED(); | 451 NOTREACHED(); |
449 } | 452 } |
450 base::android::CheckException(env); | 453 base::android::CheckException(env); |
451 } | 454 } |
452 | 455 |
453 jvalue CoerceJavaScriptValueToJavaValue(const NPVariant& variant, | 456 jvalue CoerceJavaScriptValueToJavaValue(const NPVariant& variant, |
454 const JavaType& target_type, | 457 const JavaType& target_type, |
455 bool coerce_to_string); | 458 bool coerce_to_string); |
456 | 459 |
460 // Returns a new local reference to a Java array. | |
457 jobject CoerceJavaScriptObjectToArray(const NPVariant& variant, | 461 jobject CoerceJavaScriptObjectToArray(const NPVariant& variant, |
458 const JavaType& target_type) { | 462 const JavaType& target_type) { |
459 DCHECK_EQ(JavaType::TypeArray, target_type.type); | 463 DCHECK_EQ(JavaType::TypeArray, target_type.type); |
460 NPObject* object = NPVARIANT_TO_OBJECT(variant); | 464 NPObject* object = NPVARIANT_TO_OBJECT(variant); |
461 DCHECK_NE(&JavaNPObject::kNPClass, object->_class); | 465 DCHECK_NE(&JavaNPObject::kNPClass, object->_class); |
462 | 466 |
463 const JavaType& target_inner_type = *target_type.inner_type.get(); | 467 const JavaType& target_inner_type = *target_type.inner_type.get(); |
464 // LIVECONNECT_COMPLIANCE: Existing behavior is to return null for | 468 // LIVECONNECT_COMPLIANCE: Existing behavior is to return null for |
465 // multi-dimensional arrays. Spec requires handling multi-demensional arrays. | 469 // multi-dimensional arrays. Spec requires handling multi-demensional arrays. |
466 if (target_inner_type.type == JavaType::TypeArray) { | 470 if (target_inner_type.type == JavaType::TypeArray) { |
(...skipping 24 matching lines...) Expand all Loading... | |
491 } else if (NPVARIANT_IS_DOUBLE(length_variant) | 495 } else if (NPVARIANT_IS_DOUBLE(length_variant) |
492 && NPVARIANT_TO_DOUBLE(length_variant) >= 0.0 | 496 && NPVARIANT_TO_DOUBLE(length_variant) >= 0.0 |
493 && NPVARIANT_TO_DOUBLE(length_variant) <= kint32max) { | 497 && NPVARIANT_TO_DOUBLE(length_variant) <= kint32max) { |
494 length = static_cast<jsize>(NPVARIANT_TO_DOUBLE(length_variant)); | 498 length = static_cast<jsize>(NPVARIANT_TO_DOUBLE(length_variant)); |
495 } | 499 } |
496 WebBindings::releaseVariantValue(&length_variant); | 500 WebBindings::releaseVariantValue(&length_variant); |
497 if (length == -1) { | 501 if (length == -1) { |
498 return NULL; | 502 return NULL; |
499 } | 503 } |
500 | 504 |
501 // Create the Java array. Note that we don't explicitly release the local | 505 // Create the Java array. |
502 // ref to the result or any of its elements. | |
503 // TODO(steveblock): Handle failure to create the array. | 506 // TODO(steveblock): Handle failure to create the array. |
504 jobject result = CreateJavaArray(target_inner_type, length); | 507 jobject result = CreateJavaArray(target_inner_type, length); |
505 NPVariant value_variant; | 508 NPVariant value_variant; |
506 for (jsize i = 0; i < length; ++i) { | 509 for (jsize i = 0; i < length; ++i) { |
507 // It seems that getProperty() will set the variant to type void on failure, | 510 // It seems that getProperty() will set the variant to type void on failure, |
508 // but this doesn't seem to be documented, so do it explicitly here for | 511 // but this doesn't seem to be documented, so do it explicitly here for |
509 // safety. | 512 // safety. |
510 VOID_TO_NPVARIANT(value_variant); | 513 VOID_TO_NPVARIANT(value_variant); |
511 // If this fails, for example due to a missing element, we simply treat the | 514 // If this fails, for example due to a missing element, we simply treat the |
512 // value as JavaScript undefined. | 515 // value as JavaScript undefined. |
513 WebBindings::getProperty(0, object, WebBindings::getIntIdentifier(i), | 516 WebBindings::getProperty(0, object, WebBindings::getIntIdentifier(i), |
514 &value_variant); | 517 &value_variant); |
515 SetArrayElement(result, target_inner_type, i, | 518 jvalue element = CoerceJavaScriptValueToJavaValue(value_variant, |
516 CoerceJavaScriptValueToJavaValue(value_variant, | 519 target_inner_type, |
517 target_inner_type, | 520 false); |
518 false)); | 521 SetArrayElement(result, target_inner_type, i, element); |
522 // CoerceJavaScriptValueToJavaValue() creates new local references to | |
523 // strings, objects and arrays. Of these, only strings can occur here. | |
524 // SetArrayElement() causes the array to take its own reference to the | |
525 // string, so we can now release the local reference. | |
526 DCHECK_NE(JavaType::TypeObject, target_inner_type.type); | |
527 DCHECK_NE(JavaType::TypeArray, target_inner_type.type); | |
528 if (target_inner_type.type == JavaType::TypeString) { | |
529 AttachCurrentThread()->DeleteLocalRef(element.l); | |
joth
2012/03/22 12:42:12
nit: Grab the JNIEnv* outside the loop.
Steve Block
2012/03/22 13:03:23
Done.
| |
530 } | |
519 WebBindings::releaseVariantValue(&value_variant); | 531 WebBindings::releaseVariantValue(&value_variant); |
520 } | 532 } |
521 | 533 |
522 return result; | 534 return result; |
523 } | 535 } |
524 | 536 |
525 jvalue CoerceJavaScriptObjectToJavaValue(const NPVariant& variant, | 537 jvalue CoerceJavaScriptObjectToJavaValue(const NPVariant& variant, |
526 const JavaType& target_type, | 538 const JavaType& target_type, |
527 bool coerce_to_string) { | 539 bool coerce_to_string) { |
528 // This covers both JavaScript objects (including arrays) and Java objects. | 540 // This covers both JavaScript objects (including arrays) and Java objects. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
637 NOTREACHED(); | 649 NOTREACHED(); |
638 break; | 650 break; |
639 } | 651 } |
640 return result; | 652 return result; |
641 } | 653 } |
642 | 654 |
643 // coerce_to_string means that we should try to coerce all JavaScript values to | 655 // coerce_to_string means that we should try to coerce all JavaScript values to |
644 // strings when required, rather than simply converting to NULL. This is used | 656 // strings when required, rather than simply converting to NULL. This is used |
645 // to maintain current behaviour, which differs slightly depending upon whether | 657 // to maintain current behaviour, which differs slightly depending upon whether |
646 // or not the coercion in question is for an array element. | 658 // or not the coercion in question is for an array element. |
659 // | |
660 // Note that the jvalue returned by this method may contain a new local | |
661 // reference to an object (string, object or array). This must be released by | |
662 // the caller. | |
647 jvalue CoerceJavaScriptValueToJavaValue(const NPVariant& variant, | 663 jvalue CoerceJavaScriptValueToJavaValue(const NPVariant& variant, |
648 const JavaType& target_type, | 664 const JavaType& target_type, |
649 bool coerce_to_string) { | 665 bool coerce_to_string) { |
650 // Note that in all these conversions, the relevant field of the jvalue must | 666 // Note that in all these conversions, the relevant field of the jvalue must |
651 // always be explicitly set, as jvalue does not initialize its fields. | 667 // always be explicitly set, as jvalue does not initialize its fields. |
652 | 668 |
653 // Some of these methods create new Java Strings. Note that we don't | |
654 // explicitly release the local ref to these new objects, as there's no simple | |
655 // way to do so. | |
656 switch (variant.type) { | 669 switch (variant.type) { |
657 case NPVariantType_Int32: | 670 case NPVariantType_Int32: |
658 case NPVariantType_Double: | 671 case NPVariantType_Double: |
659 return CoerceJavaScriptNumberToJavaValue(variant, target_type, | 672 return CoerceJavaScriptNumberToJavaValue(variant, target_type, |
660 coerce_to_string); | 673 coerce_to_string); |
661 case NPVariantType_Bool: | 674 case NPVariantType_Bool: |
662 return CoerceJavaScriptBooleanToJavaValue(variant, target_type, | 675 return CoerceJavaScriptBooleanToJavaValue(variant, target_type, |
663 coerce_to_string); | 676 coerce_to_string); |
664 case NPVariantType_String: | 677 case NPVariantType_String: |
665 return CoerceJavaScriptStringToJavaValue(variant, target_type); | 678 return CoerceJavaScriptStringToJavaValue(variant, target_type); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
738 std::vector<jvalue> parameters(arg_count); | 751 std::vector<jvalue> parameters(arg_count); |
739 for (size_t i = 0; i < arg_count; ++i) { | 752 for (size_t i = 0; i < arg_count; ++i) { |
740 parameters[i] = CoerceJavaScriptValueToJavaValue(args[i], | 753 parameters[i] = CoerceJavaScriptValueToJavaValue(args[i], |
741 method->parameter_type(i), | 754 method->parameter_type(i), |
742 true); | 755 true); |
743 } | 756 } |
744 | 757 |
745 // Call | 758 // Call |
746 *result = CallJNIMethod(java_object_.obj(), method->return_type(), | 759 *result = CallJNIMethod(java_object_.obj(), method->return_type(), |
747 method->id(), ¶meters[0]); | 760 method->id(), ¶meters[0]); |
761 | |
762 // Now that we're done with the jvalue, release any local references created | |
763 // by CoerceJavaScriptValueToJavaValue(). | |
764 for (size_t i = 0; i < arg_count; ++i) { | |
765 if (method->parameter_type(i).type == JavaType::TypeString || | |
766 method->parameter_type(i).type == JavaType::TypeObject || | |
767 method->parameter_type(i).type == JavaType::TypeArray) { | |
768 AttachCurrentThread()->DeleteLocalRef(parameters[i].l); | |
769 } | |
joth
2012/03/22 12:42:12
to make it a bit more self documenting, how about
Steve Block
2012/03/22 13:03:23
Done.
| |
770 } | |
771 | |
748 return true; | 772 return true; |
749 } | 773 } |
750 | 774 |
751 void JavaBoundObject::EnsureMethodsAreSetUp() const { | 775 void JavaBoundObject::EnsureMethodsAreSetUp() const { |
752 if (!methods_.empty()) { | 776 if (!methods_.empty()) { |
753 return; | 777 return; |
754 } | 778 } |
755 | 779 |
756 JNIEnv* env = AttachCurrentThread(); | 780 JNIEnv* env = AttachCurrentThread(); |
757 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( | 781 ScopedJavaLocalRef<jclass> clazz(env, static_cast<jclass>( |
(...skipping 11 matching lines...) Expand all Loading... | |
769 size_t num_methods = env->GetArrayLength(methods.obj()); | 793 size_t num_methods = env->GetArrayLength(methods.obj()); |
770 DCHECK(num_methods) << "Java objects always have public methods"; | 794 DCHECK(num_methods) << "Java objects always have public methods"; |
771 for (size_t i = 0; i < num_methods; ++i) { | 795 for (size_t i = 0; i < num_methods; ++i) { |
772 ScopedJavaLocalRef<jobject> java_method( | 796 ScopedJavaLocalRef<jobject> java_method( |
773 env, | 797 env, |
774 env->GetObjectArrayElement(methods.obj(), i)); | 798 env->GetObjectArrayElement(methods.obj(), i)); |
775 JavaMethod* method = new JavaMethod(java_method); | 799 JavaMethod* method = new JavaMethod(java_method); |
776 methods_.insert(std::make_pair(method->name(), method)); | 800 methods_.insert(std::make_pair(method->name(), method)); |
777 } | 801 } |
778 } | 802 } |
OLD | NEW |