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
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/test/test_support_android.h"
12 #include "jni/ValidationTestUtil_jni.h"
13 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
14
15 using base::android::JavaParamRef;
16 using base::android::ScopedJavaLocalRef;
17
18 namespace mojo {
19 namespace android {
20
JNI_ValidationTestUtil_ParseData(JNIEnv * env,const JavaParamRef<jclass> & jcaller,const JavaParamRef<jstring> & data_as_string)21 ScopedJavaLocalRef<jobject> JNI_ValidationTestUtil_ParseData(
22 JNIEnv* env,
23 const JavaParamRef<jclass>& jcaller,
24 const JavaParamRef<jstring>& data_as_string) {
25 std::string input =
26 base::android::ConvertJavaStringToUTF8(env, data_as_string);
27 std::vector<uint8_t> data;
28 size_t num_handles;
29 std::string error_message;
30 if (!test::ParseValidationTestInput(input, &data, &num_handles,
31 &error_message)) {
32 ScopedJavaLocalRef<jstring> j_error_message =
33 base::android::ConvertUTF8ToJavaString(env, error_message);
34 return Java_ValidationTestUtil_buildData(env, nullptr, 0, j_error_message);
35 }
36 void* data_ptr = data.data();
37 if (!data_ptr) {
38 DCHECK(!data.size());
39 data_ptr = &data;
40 }
41 ScopedJavaLocalRef<jobject> byte_buffer(
42 env, env->NewDirectByteBuffer(data_ptr, data.size()));
43 return Java_ValidationTestUtil_buildData(env, byte_buffer, num_handles,
44 nullptr);
45 }
46
47 } // namespace android
48 } // namespace mojo
49