OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/common/android/resource_request_body_android.h" | 5 #include "content/common/android/resource_request_body_android.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "content/common/page_state_serialization.h" | 14 #include "content/common/page_state_serialization.h" |
15 #include "content/public/common/resource_request_body.h" | 15 #include "content/public/common/resource_request_body.h" |
16 #include "jni/ResourceRequestBody_jni.h" | 16 #include "jni/ResourceRequestBody_jni.h" |
17 | 17 |
18 using base::android::JavaParamRef; | 18 using base::android::JavaParamRef; |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 base::android::ScopedJavaLocalRef<jbyteArray> | 24 base::android::ScopedJavaLocalRef<jbyteArray> |
25 ConvertResourceRequestBodyToJavaArray(JNIEnv* env, | 25 ConvertResourceRequestBodyToJavaArray(JNIEnv* env, |
26 const ResourceRequestBodyImpl& body) { | 26 const ResourceRequestBody& body) { |
27 std::string encoded = EncodeResourceRequestBody(body); | 27 std::string encoded = EncodeResourceRequestBody(body); |
28 return base::android::ToJavaByteArray( | 28 return base::android::ToJavaByteArray( |
29 env, reinterpret_cast<const uint8_t*>(encoded.data()), encoded.size()); | 29 env, reinterpret_cast<const uint8_t*>(encoded.data()), encoded.size()); |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 bool RegisterResourceRequestBody(JNIEnv* env) { | 34 bool RegisterResourceRequestBody(JNIEnv* env) { |
35 return RegisterNativesImpl(env); | 35 return RegisterNativesImpl(env); |
36 } | 36 } |
37 | 37 |
38 base::android::ScopedJavaLocalRef<jbyteArray> | 38 base::android::ScopedJavaLocalRef<jbyteArray> |
39 CreateResourceRequestBodyFromBytes( | 39 CreateResourceRequestBodyFromBytes( |
40 JNIEnv* env, | 40 JNIEnv* env, |
41 const JavaParamRef<jclass>& clazz, | 41 const JavaParamRef<jclass>& clazz, |
42 const JavaParamRef<jbyteArray>& j_post_data) { | 42 const JavaParamRef<jbyteArray>& j_post_data) { |
43 base::android::ScopedJavaLocalRef<jbyteArray> result; | 43 base::android::ScopedJavaLocalRef<jbyteArray> result; |
44 if (!j_post_data) | 44 if (!j_post_data) |
45 return result; | 45 return result; |
46 | 46 |
47 std::vector<uint8_t> post_data; | 47 std::vector<uint8_t> post_data; |
48 base::android::JavaByteArrayToByteVector(env, j_post_data, &post_data); | 48 base::android::JavaByteArrayToByteVector(env, j_post_data, &post_data); |
49 scoped_refptr<ResourceRequestBody> body = | 49 scoped_refptr<ResourceRequestBody> body = |
50 ResourceRequestBody::CreateFromBytes( | 50 ResourceRequestBody::CreateFromBytes( |
51 reinterpret_cast<const char*>(post_data.data()), post_data.size()); | 51 reinterpret_cast<const char*>(post_data.data()), post_data.size()); |
52 | 52 |
53 return ConvertResourceRequestBodyToJavaArray( | 53 return ConvertResourceRequestBodyToJavaArray( |
54 env, static_cast<const ResourceRequestBodyImpl&>(*body)); | 54 env, static_cast<const ResourceRequestBody&>(*body)); |
55 } | 55 } |
56 | 56 |
57 base::android::ScopedJavaLocalRef<jobject> | 57 base::android::ScopedJavaLocalRef<jobject> |
58 ConvertResourceRequestBodyToJavaObject( | 58 ConvertResourceRequestBodyToJavaObject( |
59 JNIEnv* env, | 59 JNIEnv* env, |
60 const scoped_refptr<ResourceRequestBodyImpl>& body) { | 60 const scoped_refptr<ResourceRequestBody>& body) { |
61 if (!body) | 61 if (!body) |
62 return base::android::ScopedJavaLocalRef<jobject>(); | 62 return base::android::ScopedJavaLocalRef<jobject>(); |
63 | 63 |
64 // TODO(lukasza): Avoid repeatedly copying the bytes. | 64 // TODO(lukasza): Avoid repeatedly copying the bytes. |
65 // See also https://goo.gl/ITiLGI. | 65 // See also https://goo.gl/ITiLGI. |
66 base::android::ScopedJavaLocalRef<jbyteArray> j_encoded = | 66 base::android::ScopedJavaLocalRef<jbyteArray> j_encoded = |
67 ConvertResourceRequestBodyToJavaArray(env, *body); | 67 ConvertResourceRequestBodyToJavaArray(env, *body); |
68 | 68 |
69 return Java_ResourceRequestBody_createFromEncodedNativeForm(env, j_encoded); | 69 return Java_ResourceRequestBody_createFromEncodedNativeForm(env, j_encoded); |
70 } | 70 } |
71 | 71 |
72 scoped_refptr<ResourceRequestBodyImpl> ExtractResourceRequestBodyFromJavaObject( | 72 scoped_refptr<ResourceRequestBody> ExtractResourceRequestBodyFromJavaObject( |
73 JNIEnv* env, | 73 JNIEnv* env, |
74 const base::android::JavaParamRef<jobject>& j_body) { | 74 const base::android::JavaParamRef<jobject>& j_body) { |
75 if (!j_body) | 75 if (!j_body) |
76 return nullptr; | 76 return nullptr; |
77 | 77 |
78 base::android::ScopedJavaLocalRef<jbyteArray> j_encoded = | 78 base::android::ScopedJavaLocalRef<jbyteArray> j_encoded = |
79 Java_ResourceRequestBody_getEncodedNativeForm(env, j_body); | 79 Java_ResourceRequestBody_getEncodedNativeForm(env, j_body); |
80 if (j_encoded.is_null()) | 80 if (j_encoded.is_null()) |
81 return nullptr; | 81 return nullptr; |
82 | 82 |
83 std::vector<uint8_t> encoded; | 83 std::vector<uint8_t> encoded; |
84 base::android::JavaByteArrayToByteVector(env, j_encoded.obj(), &encoded); | 84 base::android::JavaByteArrayToByteVector(env, j_encoded.obj(), &encoded); |
85 | 85 |
86 return DecodeResourceRequestBody( | 86 return DecodeResourceRequestBody( |
87 reinterpret_cast<const char*>(encoded.data()), encoded.size()); | 87 reinterpret_cast<const char*>(encoded.data()), encoded.size()); |
88 } | 88 } |
89 | 89 |
90 } // namespace content | 90 } // namespace content |
OLD | NEW |