OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/android/java/gin_java_script_to_java_types_coercion.h" | 5 #include "content/browser/android/java/gin_java_script_to_java_types_coercion.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "content/common/android/gin_java_bridge_value.h" | 14 #include "content/common/android/gin_java_bridge_value.h" |
15 | 15 #include "third_party/WebKit/public/platform/WebString.h" |
16 using base::android::ConvertUTF8ToJavaString; | |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 const char kJavaLangString[] = "java/lang/String"; | 21 const char kJavaLangString[] = "java/lang/String"; |
23 const char kUndefined[] = "undefined"; | 22 const char kUndefined[] = "undefined"; |
24 | 23 |
| 24 // This is an intermediate solution until we fix http://crbug.com/391492. |
| 25 jstring ConvertUTF8ToJString(JNIEnv* env, const std::string& string) { |
| 26 base::string16 utf16( |
| 27 blink::WebString::fromUTF8(string.c_str(), string.size())); |
| 28 return env->NewString(utf16.data(), utf16.length()); |
| 29 } |
| 30 |
25 double RoundDoubleTowardsZero(const double& x) { | 31 double RoundDoubleTowardsZero(const double& x) { |
26 if (std::isnan(x)) { | 32 if (std::isnan(x)) { |
27 return 0.0; | 33 return 0.0; |
28 } | 34 } |
29 return x > 0.0 ? floor(x) : ceil(x); | 35 return x > 0.0 ? floor(x) : ceil(x); |
30 } | 36 } |
31 | 37 |
32 // Rounds to jlong using Java's type conversion rules. | 38 // Rounds to jlong using Java's type conversion rules. |
33 jlong RoundDoubleToLong(const double& x) { | 39 jlong RoundDoubleToLong(const double& x) { |
34 double intermediate = RoundDoubleTowardsZero(x); | 40 double intermediate = RoundDoubleTowardsZero(x); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 case JavaType::TypeDouble: | 99 case JavaType::TypeDouble: |
94 result.d = int_value; | 100 result.d = int_value; |
95 break; | 101 break; |
96 case JavaType::TypeObject: | 102 case JavaType::TypeObject: |
97 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec | 103 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec |
98 // requires handling object equivalents of primitive types. | 104 // requires handling object equivalents of primitive types. |
99 result.l = NULL; | 105 result.l = NULL; |
100 break; | 106 break; |
101 case JavaType::TypeString: | 107 case JavaType::TypeString: |
102 result.l = coerce_to_string | 108 result.l = coerce_to_string |
103 ? ConvertUTF8ToJavaString( | 109 ? ConvertUTF8ToJString(env, base::Int64ToString(int_value)) |
104 env, base::Int64ToString(int_value)).Release() | |
105 : NULL; | 110 : NULL; |
106 break; | 111 break; |
107 case JavaType::TypeBoolean: | 112 case JavaType::TypeBoolean: |
108 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to false. Spec | 113 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to false. Spec |
109 // requires converting to false for 0 or NaN, true otherwise. | 114 // requires converting to false for 0 or NaN, true otherwise. |
110 result.z = JNI_FALSE; | 115 result.z = JNI_FALSE; |
111 break; | 116 break; |
112 case JavaType::TypeArray: | 117 case JavaType::TypeArray: |
113 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec | 118 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec |
114 // requires raising a JavaScript exception. | 119 // requires raising a JavaScript exception. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 break; | 159 break; |
155 case JavaType::TypeDouble: | 160 case JavaType::TypeDouble: |
156 result.d = double_value; | 161 result.d = double_value; |
157 break; | 162 break; |
158 case JavaType::TypeObject: | 163 case JavaType::TypeObject: |
159 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec | 164 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec |
160 // requires handling object equivalents of primitive types. | 165 // requires handling object equivalents of primitive types. |
161 result.l = NULL; | 166 result.l = NULL; |
162 break; | 167 break; |
163 case JavaType::TypeString: | 168 case JavaType::TypeString: |
164 result.l = | 169 result.l = coerce_to_string |
165 coerce_to_string | 170 ? ConvertUTF8ToJString( |
166 ? ConvertUTF8ToJavaString( | 171 env, base::StringPrintf("%.6lg", double_value)) |
167 env, base::StringPrintf("%.6lg", double_value)).Release() | 172 : NULL; |
168 : NULL; | |
169 break; | 173 break; |
170 case JavaType::TypeBoolean: | 174 case JavaType::TypeBoolean: |
171 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to false. Spec | 175 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to false. Spec |
172 // requires converting to false for 0 or NaN, true otherwise. | 176 // requires converting to false for 0 or NaN, true otherwise. |
173 result.z = JNI_FALSE; | 177 result.z = JNI_FALSE; |
174 break; | 178 break; |
175 case JavaType::TypeArray: | 179 case JavaType::TypeArray: |
176 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec | 180 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to null. Spec |
177 // requires raising a JavaScript exception. | 181 // requires raising a JavaScript exception. |
178 result.l = NULL; | 182 result.l = NULL; |
(...skipping 17 matching lines...) Expand all Loading... |
196 switch (target_type.type) { | 200 switch (target_type.type) { |
197 case JavaType::TypeBoolean: | 201 case JavaType::TypeBoolean: |
198 result.z = boolean_value ? JNI_TRUE : JNI_FALSE; | 202 result.z = boolean_value ? JNI_TRUE : JNI_FALSE; |
199 break; | 203 break; |
200 case JavaType::TypeObject: | 204 case JavaType::TypeObject: |
201 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to NULL. Spec | 205 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to NULL. Spec |
202 // requires handling java.lang.Boolean and java.lang.Object. | 206 // requires handling java.lang.Boolean and java.lang.Object. |
203 result.l = NULL; | 207 result.l = NULL; |
204 break; | 208 break; |
205 case JavaType::TypeString: | 209 case JavaType::TypeString: |
206 result.l = coerce_to_string | 210 result.l = coerce_to_string ? ConvertUTF8ToJString( |
207 ? ConvertUTF8ToJavaString( | 211 env, boolean_value ? "true" : "false") |
208 env, boolean_value ? "true" : "false").Release() | 212 : NULL; |
209 : NULL; | |
210 break; | 213 break; |
211 case JavaType::TypeByte: | 214 case JavaType::TypeByte: |
212 case JavaType::TypeChar: | 215 case JavaType::TypeChar: |
213 case JavaType::TypeShort: | 216 case JavaType::TypeShort: |
214 case JavaType::TypeInt: | 217 case JavaType::TypeInt: |
215 case JavaType::TypeLong: | 218 case JavaType::TypeLong: |
216 case JavaType::TypeFloat: | 219 case JavaType::TypeFloat: |
217 case JavaType::TypeDouble: { | 220 case JavaType::TypeDouble: { |
218 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to 0. Spec | 221 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to 0. Spec |
219 // requires converting to 0 or 1. | 222 // requires converting to 0 or 1. |
(...skipping 16 matching lines...) Expand all Loading... |
236 | 239 |
237 jvalue CoerceJavaScriptStringToJavaValue(JNIEnv* env, | 240 jvalue CoerceJavaScriptStringToJavaValue(JNIEnv* env, |
238 const base::Value* value, | 241 const base::Value* value, |
239 const JavaType& target_type) { | 242 const JavaType& target_type) { |
240 // See http://jdk6.java.net/plugin2/liveconnect/#JS_STRING_VALUES. | 243 // See http://jdk6.java.net/plugin2/liveconnect/#JS_STRING_VALUES. |
241 jvalue result; | 244 jvalue result; |
242 switch (target_type.type) { | 245 switch (target_type.type) { |
243 case JavaType::TypeString: { | 246 case JavaType::TypeString: { |
244 std::string string_result; | 247 std::string string_result; |
245 value->GetAsString(&string_result); | 248 value->GetAsString(&string_result); |
246 result.l = ConvertUTF8ToJavaString(env, string_result).Release(); | 249 result.l = ConvertUTF8ToJString(env, string_result); |
247 break; | 250 break; |
248 } | 251 } |
249 case JavaType::TypeObject: | 252 case JavaType::TypeObject: |
250 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to NULL. Spec | 253 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to NULL. Spec |
251 // requires handling java.lang.Object. | 254 // requires handling java.lang.Object. |
252 result.l = NULL; | 255 result.l = NULL; |
253 break; | 256 break; |
254 case JavaType::TypeByte: | 257 case JavaType::TypeByte: |
255 case JavaType::TypeShort: | 258 case JavaType::TypeShort: |
256 case JavaType::TypeInt: | 259 case JavaType::TypeInt: |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 } | 393 } |
391 jvalue result; | 394 jvalue result; |
392 switch (target_type.type) { | 395 switch (target_type.type) { |
393 case JavaType::TypeObject: | 396 case JavaType::TypeObject: |
394 result.l = NULL; | 397 result.l = NULL; |
395 break; | 398 break; |
396 case JavaType::TypeString: | 399 case JavaType::TypeString: |
397 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert undefined to | 400 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert undefined to |
398 // "undefined". Spec requires converting undefined to NULL. | 401 // "undefined". Spec requires converting undefined to NULL. |
399 result.l = (coerce_to_string && is_undefined) | 402 result.l = (coerce_to_string && is_undefined) |
400 ? ConvertUTF8ToJavaString(env, kUndefined).Release() | 403 ? ConvertUTF8ToJString(env, kUndefined) |
401 : NULL; | 404 : NULL; |
402 break; | 405 break; |
403 case JavaType::TypeByte: | 406 case JavaType::TypeByte: |
404 case JavaType::TypeChar: | 407 case JavaType::TypeChar: |
405 case JavaType::TypeShort: | 408 case JavaType::TypeShort: |
406 case JavaType::TypeInt: | 409 case JavaType::TypeInt: |
407 case JavaType::TypeLong: | 410 case JavaType::TypeLong: |
408 case JavaType::TypeFloat: | 411 case JavaType::TypeFloat: |
409 case JavaType::TypeDouble: { | 412 case JavaType::TypeDouble: { |
410 jvalue null_value = {0}; | 413 jvalue null_value = {0}; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 // requires converting if the target type is | 579 // requires converting if the target type is |
577 // netscape.javascript.JSObject, otherwise raising a JavaScript | 580 // netscape.javascript.JSObject, otherwise raising a JavaScript |
578 // exception. | 581 // exception. |
579 result.l = NULL; | 582 result.l = NULL; |
580 } | 583 } |
581 break; | 584 break; |
582 } | 585 } |
583 case JavaType::TypeString: | 586 case JavaType::TypeString: |
584 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to | 587 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to |
585 // "undefined". Spec requires calling toString() on the Java object. | 588 // "undefined". Spec requires calling toString() on the Java object. |
586 result.l = coerce_to_string | 589 result.l = |
587 ? ConvertUTF8ToJavaString(env, kUndefined).Release() | 590 coerce_to_string ? ConvertUTF8ToJString(env, kUndefined) : NULL; |
588 : NULL; | |
589 break; | 591 break; |
590 case JavaType::TypeByte: | 592 case JavaType::TypeByte: |
591 case JavaType::TypeShort: | 593 case JavaType::TypeShort: |
592 case JavaType::TypeInt: | 594 case JavaType::TypeInt: |
593 case JavaType::TypeLong: | 595 case JavaType::TypeLong: |
594 case JavaType::TypeFloat: | 596 case JavaType::TypeFloat: |
595 case JavaType::TypeDouble: | 597 case JavaType::TypeDouble: |
596 case JavaType::TypeChar: { | 598 case JavaType::TypeChar: { |
597 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to 0. Spec | 599 // LIVECONNECT_COMPLIANCE: Existing behavior is to convert to 0. Spec |
598 // requires raising a JavaScript exception. | 600 // requires raising a JavaScript exception. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 env, value, target_type, coerce_to_string); | 698 env, value, target_type, coerce_to_string); |
697 case base::Value::TYPE_BINARY: | 699 case base::Value::TYPE_BINARY: |
698 return CoerceGinJavaBridgeValueToJavaValue( | 700 return CoerceGinJavaBridgeValueToJavaValue( |
699 env, value, target_type, coerce_to_string, object_refs); | 701 env, value, target_type, coerce_to_string, object_refs); |
700 } | 702 } |
701 NOTREACHED(); | 703 NOTREACHED(); |
702 return jvalue(); | 704 return jvalue(); |
703 } | 705 } |
704 | 706 |
705 } // namespace content | 707 } // namespace content |
OLD | NEW |