• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
6 #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
7 
8 #include <jni.h>
9 #include <map>
10 #include <string>
11 
12 #include "base/android/jni_android.h"
13 
14 namespace base {
15 namespace android {
16 
17 // This file is used to:
18 // - document the best practices and guidelines on JNI usage.
19 // - ensure sample_for_tests_jni.h compiles and the functions declared in it
20 // as expected.
21 //
22 // Methods are called directly from Java. More documentation in
23 // SampleForTests.java. See BUILD.gn for the build rules necessary for JNI
24 // to be used in an APK.
25 //
26 // For C++ to access Java methods:
27 // - GN Build must be configured to generate bindings:
28 //  # Add import at top of file:
29 //  if (is_android) {
30 //    import("//build/config/android/rules.gni")  # For generate_jni().
31 //  }
32 //  # ...
33 //  # An example target that will rely on JNI:
34 //  component("foo") {
35 //    # ... normal sources, defines, deps.
36 //    #     For each jni generated .java -> .h header file in jni_headers
37 //    #     target there will be a single .cc file here that includes it.
38 //    #
39 //    # Add a dep for JNI:
40 //    if (is_android) {
41 //      deps += [ ":foo_jni" ]
42 //    }
43 //  }
44 //  # ...
45 //  # Create target for JNI:
46 //  if (is_android) {
47 //    generate_jni("jni_headers") {
48 //      sources = [
49 //        "java/src/org/chromium/example/jni_generator/SampleForTests.java",
50 //      ]
51 //    }
52 //    android_library("java") {
53 //      sources = [
54 //        "java/src/org/chromium/example/jni_generator/SampleForTests.java",
55 //        "java/src/org/chromium/example/jni_generator/NonJniFile.java",
56 //      ]
57 //    }
58 //  }
59 // The build rules above are generally that that's needed when adding new
60 // JNI methods/files. For a full GN example, see
61 // base/android/jni_generator/BUILD.gn
62 //
63 // For C++ methods to be exposed to Java:
64 // - The Java class must be part of an android_apk target that depends on
65 //   a generate_jni_registration target. This generate_jni_registration target
66 //   automatically generates all necessary registration functions. The
67 //   generated header file exposes RegisterNatives() which registers all
68 //   methods.
69 //
70 class CPPClass {
71  public:
72   CPPClass();
73 
74   CPPClass(const CPPClass&) = delete;
75   CPPClass& operator=(const CPPClass&) = delete;
76 
77   ~CPPClass();
78 
79   // Java @CalledByNative methods implicitly available to C++ via the _jni.h
80   // file included in the .cc file.
81 
82   class InnerClass {
83    public:
84     jdouble MethodOtherP0(JNIEnv* env,
85                           const base::android::JavaParamRef<jobject>& caller);
86   };
87 
88   void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& caller);
89 
90   jint Method(JNIEnv* env, const base::android::JavaParamRef<jobject>& caller);
91 
92   void AddStructB(JNIEnv* env,
93                   const base::android::JavaParamRef<jobject>& caller,
94                   const base::android::JavaParamRef<jobject>& structb);
95 
96   void IterateAndDoSomethingWithStructB(
97       JNIEnv* env,
98       const base::android::JavaParamRef<jobject>& caller);
99 
100   base::android::ScopedJavaLocalRef<jstring> ReturnAString(
101       JNIEnv* env,
102       const base::android::JavaParamRef<jobject>& caller);
103 
104  private:
105   std::map<long, std::string> map_;
106 };
107 
108 }  // namespace android
109 }  // namespace base
110 
111 #endif  // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
112