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 "base/android/jni_generator/sample_for_tests.h" | 5 #include "base/android/jni_generator/sample_for_tests.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/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
10 // Generated file for JNI bindings from C++ to Java @CalledByNative methods. | 10 // Generated file for JNI bindings from C++ to Java @CalledByNative methods. |
11 // Only to be included in one .cc file. | 11 // Only to be included in one .cc file. |
12 // Name is based on the java file name: *.java -> jni/*_jni.h | 12 // Name is based on the java file name: *.java -> jni/*_jni.h |
13 #include "jni/SampleForTests_jni.h" // Generated by JNI. | 13 #include "jni/SampleForTests_jni.h" // Generated by JNI. |
14 | 14 |
15 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
| 16 using base::android::ConvertJavaStringToUTF8; |
| 17 using base::android::ConvertUTF8ToJavaString; |
16 using base::android::ScopedJavaLocalRef; | 18 using base::android::ScopedJavaLocalRef; |
17 | 19 |
18 namespace base { | 20 namespace base { |
19 namespace android { | 21 namespace android { |
20 | 22 |
21 jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject caller) { | 23 jdouble CPPClass::InnerClass::MethodOtherP0( |
| 24 JNIEnv* env, |
| 25 const JavaParamRef<jobject>& caller) { |
22 return 0.0; | 26 return 0.0; |
23 } | 27 } |
24 | 28 |
25 CPPClass::CPPClass() { | 29 CPPClass::CPPClass() { |
26 } | 30 } |
27 | 31 |
28 CPPClass::~CPPClass() { | 32 CPPClass::~CPPClass() { |
29 } | 33 } |
30 | 34 |
31 // static | 35 // static |
32 bool CPPClass::RegisterJNI(JNIEnv* env) { | 36 bool CPPClass::RegisterJNI(JNIEnv* env) { |
33 return RegisterNativesImpl(env); // Generated in SampleForTests_jni.h | 37 return RegisterNativesImpl(env); // Generated in SampleForTests_jni.h |
34 } | 38 } |
35 | 39 |
36 void CPPClass::Destroy(JNIEnv* env, jobject caller) { | 40 void CPPClass::Destroy(JNIEnv* env, const JavaParamRef<jobject>& caller) { |
37 delete this; | 41 delete this; |
38 } | 42 } |
39 | 43 |
40 jint CPPClass::Method(JNIEnv* env, jobject caller) { | 44 jint CPPClass::Method(JNIEnv* env, const JavaParamRef<jobject>& caller) { |
41 return 0; | 45 return 0; |
42 } | 46 } |
43 | 47 |
44 void CPPClass::AddStructB(JNIEnv* env, jobject caller, jobject structb) { | 48 void CPPClass::AddStructB(JNIEnv* env, |
| 49 const JavaParamRef<jobject>& caller, |
| 50 const JavaParamRef<jobject>& structb) { |
45 long key = Java_InnerStructB_getKey(env, structb); | 51 long key = Java_InnerStructB_getKey(env, structb); |
46 std::string value = ConvertJavaStringToUTF8( | 52 std::string value = ConvertJavaStringToUTF8( |
47 env, Java_InnerStructB_getValue(env, structb).obj()); | 53 env, Java_InnerStructB_getValue(env, structb).obj()); |
48 map_[key] = value; | 54 map_[key] = value; |
49 } | 55 } |
50 | 56 |
51 void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject caller) { | 57 void CPPClass::IterateAndDoSomethingWithStructB( |
| 58 JNIEnv* env, |
| 59 const JavaParamRef<jobject>& caller) { |
52 // Iterate over the elements and do something with them. | 60 // Iterate over the elements and do something with them. |
53 for (std::map<long, std::string>::const_iterator it = map_.begin(); | 61 for (std::map<long, std::string>::const_iterator it = map_.begin(); |
54 it != map_.end(); ++it) { | 62 it != map_.end(); ++it) { |
55 long key = it->first; | 63 long key = it->first; |
56 std::string value = it->second; | 64 std::string value = it->second; |
57 } | 65 } |
58 map_.clear(); | 66 map_.clear(); |
59 } | 67 } |
60 | 68 |
61 base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( | 69 ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( |
62 JNIEnv* env, jobject caller) { | 70 JNIEnv* env, |
63 base::android::ScopedJavaLocalRef<jstring> ret = ConvertUTF8ToJavaString( | 71 const JavaParamRef<jobject>& caller) { |
64 env, "test"); | 72 return ConvertUTF8ToJavaString(env, "test"); |
65 return ret; | |
66 } | 73 } |
67 | 74 |
68 // Static free functions declared and called directly from java. | 75 // Static free functions declared and called directly from java. |
69 static jlong Init(JNIEnv* env, | 76 static jlong Init(JNIEnv* env, |
70 const JavaParamRef<jobject>& caller, | 77 const JavaParamRef<jobject>& caller, |
71 const JavaParamRef<jstring>& param) { | 78 const JavaParamRef<jstring>& param) { |
72 return 0; | 79 return 0; |
73 } | 80 } |
74 | 81 |
75 static jdouble GetDoubleFunction(JNIEnv*, const JavaParamRef<jobject>&) { | 82 static jdouble GetDoubleFunction(JNIEnv*, const JavaParamRef<jobject>&) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // This is how you call a java method from C++. Note that you must have | 115 // This is how you call a java method from C++. Note that you must have |
109 // obtained the jobject somehow. | 116 // obtained the jobject somehow. |
110 jobject my_java_object = NULL; | 117 jobject my_java_object = NULL; |
111 int bar = base::android::Java_SampleForTests_javaMethod( | 118 int bar = base::android::Java_SampleForTests_javaMethod( |
112 env, my_java_object, 1, 2); | 119 env, my_java_object, 1, 2); |
113 | 120 |
114 for (int i = 0; i < 10; ++i) { | 121 for (int i = 0; i < 10; ++i) { |
115 // Creates a "struct" that will then be used by the java side. | 122 // Creates a "struct" that will then be used by the java side. |
116 ScopedJavaLocalRef<jobject> struct_a = | 123 ScopedJavaLocalRef<jobject> struct_a = |
117 base::android::Java_InnerStructA_create( | 124 base::android::Java_InnerStructA_create( |
118 env, 0, 1, | 125 env, 0, 1, ConvertUTF8ToJavaString(env, "test").obj()); |
119 base::android::ConvertUTF8ToJavaString(env, "test").obj()); | |
120 base::android::Java_SampleForTests_addStructA( | 126 base::android::Java_SampleForTests_addStructA( |
121 env, my_java_object, struct_a.obj()); | 127 env, my_java_object, struct_a.obj()); |
122 } | 128 } |
123 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); | 129 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); |
124 return 0; | 130 return 0; |
125 } | 131 } |
OLD | NEW |