[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / base / android / jni_generator / sample_for_tests.cc
bloba38377cc2f78a34fe4e6bfd66c565b6167789e98
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
3 // found in the LICENSE file.
5 #include "base/android/jni_generator/sample_for_tests.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 // Generated file for JNI bindings from C++ to Java @CalledByNative methods.
11 // Only to be included in one .cc file.
12 // Name is based on the java file name: *.java -> jni/*_jni.h
13 #include "jni/SampleForTests_jni.h" // Generated by JNI.
15 using base::android::AttachCurrentThread;
16 using base::android::ScopedJavaLocalRef;
18 namespace base {
19 namespace android {
21 jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject caller) {
22 return 0.0;
25 CPPClass::CPPClass() {
28 CPPClass::~CPPClass() {
31 // static
32 bool CPPClass::RegisterJNI(JNIEnv* env) {
33 return RegisterNativesImpl(env); // Generated in SampleForTests_jni.h
36 void CPPClass::Destroy(JNIEnv* env, jobject caller) {
37 delete this;
40 jint CPPClass::Method(JNIEnv* env, jobject caller) {
41 return 0;
44 void CPPClass::AddStructB(JNIEnv* env, jobject caller, jobject structb) {
45 long key = Java_InnerStructB_getKey(env, structb);
46 std::string value = ConvertJavaStringToUTF8(
47 env, Java_InnerStructB_getValue(env, structb).obj());
48 map_[key] = value;
51 void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject caller) {
52 // Iterate over the elements and do something with them.
53 for (std::map<long, std::string>::const_iterator it = map_.begin();
54 it != map_.end(); ++it) {
55 long key = it->first;
56 std::string value = it->second;
58 map_.clear();
61 base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString(
62 JNIEnv* env, jobject caller) {
63 base::android::ScopedJavaLocalRef<jstring> ret = ConvertUTF8ToJavaString(
64 env, "test");
65 return ret;
68 // Static free functions declared and called directly from java.
69 static jlong Init(JNIEnv* env,
70 const JavaParamRef<jobject>& caller,
71 const JavaParamRef<jstring>& param) {
72 return 0;
75 static jdouble GetDoubleFunction(JNIEnv*, const JavaParamRef<jobject>&) {
76 return 0;
79 static jfloat GetFloatFunction(JNIEnv*, const JavaParamRef<jclass>&) {
80 return 0;
83 static void SetNonPODDatatype(JNIEnv*,
84 const JavaParamRef<jobject>&,
85 const JavaParamRef<jobject>&) {}
87 static ScopedJavaLocalRef<jobject> GetNonPODDatatype(
88 JNIEnv*,
89 const JavaParamRef<jobject>&) {
90 return ScopedJavaLocalRef<jobject>();
93 static jint InnerFunction(JNIEnv*, jclass) {
94 return 0;
97 } // namespace android
98 } // namespace base
100 int main() {
101 // On a regular application, you'd call AttachCurrentThread(). This sample is
102 // not yet linking with all the libraries.
103 JNIEnv* env = /* AttachCurrentThread() */ NULL;
105 // This is how you call a java static method from C++.
106 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env);
108 // This is how you call a java method from C++. Note that you must have
109 // obtained the jobject somehow.
110 jobject my_java_object = NULL;
111 int bar = base::android::Java_SampleForTests_javaMethod(
112 env, my_java_object, 1, 2);
114 for (int i = 0; i < 10; ++i) {
115 // Creates a "struct" that will then be used by the java side.
116 ScopedJavaLocalRef<jobject> struct_a =
117 base::android::Java_InnerStructA_create(
118 env, 0, 1,
119 base::android::ConvertUTF8ToJavaString(env, "test").obj());
120 base::android::Java_SampleForTests_addStructA(
121 env, my_java_object, struct_a.obj());
123 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object);
124 return 0;