Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: base/android/jni_generator/sample_for_tests.h

Issue 1123343005: Improve jni_generator sample documentation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ 5 #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
6 #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ 6 #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
7 7
8 #include <jni.h> 8 #include <jni.h>
9 #include <map> 9 #include <map>
10 #include <string> 10 #include <string>
11 11
12 #include "base/android/jni_android.h" 12 #include "base/android/jni_android.h"
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 14
15 namespace base { 15 namespace base {
16 namespace android { 16 namespace android {
17 17
18 // This file is used to: 18 // This file is used to:
19 // - document the best practices and guidelines on JNI usage. 19 // - document the best practices and guidelines on JNI usage.
20 // - ensure sample_for_tests_jni.h compiles and the functions declared in it 20 // - ensure sample_for_tests_jni.h compiles and the functions declared in it
21 // as expected. 21 // as expected.
22 // 22 //
23 // All methods are called directly from Java. See more documentation in 23 // Methods are called directly from Java (except RegisterJNI). More
24 // SampleForTests.java. 24 // documentation in SampleForTests.java
25 //
26 // For C++ to access Java methods:
27 // - Build must be configured to generate bindings:
28 //
29 // GYP:
30 // # ...
31 // 'conditions': [
32 // ['OS == "android"', {
33 // 'targets': [
34 // {
35 // 'target_name': 'base_jni_generator_sample_jni_headers',
36 // 'type': 'none',
37 // 'sources': [
38 // 'java/src/org/chromium/example/jni_generator/SampleForTests.java',
39 // ],
40 // 'variables': {
41 // 'jni_gen_package': 'base_jni_generator_sample',
42 // },
43 // 'includes': [ '../../../build/jni_generator.gypi' ],
44 // },
45 // {
46 // 'target_name': 'base_jni_generator_sample_java',
47 // 'type': 'none',
48 // 'dependencies': [
49 // '../../../base/base.gyp:base',
50 // ],
51 // 'variables': {
52 // 'java_in_dir': 'java',
53 // },
54 // 'includes': [ '../../../build/java.gypi' ],
55 // },
56 // ],
57 // }],
58 // ],
59 //
60 // And these two targets must be dependencies of modules using them.
nyquist 2015/05/13 06:47:20 This is a little bit unclear to me (as a fake new
scheib 2015/05/13 21:59:52 Done, I've included the deps added in the build fi
61 //
62 // GN:
63 // if (is_android) {
64 // import("//build/config/android/rules.gni") # For generate_jni().
65 // }
66 // # ...
67 // component("base_jni_generator_sample") {
68 // # ... sources, defines, deps ...
69 // if (is_android) {
70 // deps += [ ":base_jni_generator_sample_jni" ]
71 // }
72 // }
73 //
74 // if (is_android) {
75 // generate_jni("base_jni_generator_sample_jni") {
76 // sources = [
77 // "java/src/org/chromium/example/jni_generator/SampleForTests.java",
78 // ]
79 // jni_package = "base_jni_generator_sample"
80 // }
81 // }
82 //
83 // For C++ methods to be exposed to Java:
84 // - The generated RegisterNativesImpl method must be called, this is typically
85 // done by having a static RegisterJNI method in the C++ class.
86 // - The RegisterJNI method is added to a module's collection of register
87 // methods, such as: example_jni_registrar.h/cc files which call
88 // base::android::RegisterNativeMethods.
89 // An example_jni_registstrar.cc:
90 //
91 // namespace {
92 // const base::android::RegistrationMethod kRegisteredMethods[] = {
93 // // Initial string is for debugging only.
94 // { "ExampleName", base::ExampleNameAndroid::RegisterJNI },
95 // { "ExampleName2", base::ExampleName2Android::RegisterJNI },
96 // };
97 // } // namespace
98 //
99 // bool RegisterModuleNameJni(JNIEnv* env) {
100 // return RegisterNativeMethods(env, kRegisteredMethods,
101 // arraysize(kRegisteredMethods));
102 // }
103 //
104 // - Each module's RegisterModuleNameJni is then called by a larger module,
105 // or application during startup.
106 //
25 class CPPClass { 107 class CPPClass {
26 public: 108 public:
27 CPPClass(); 109 CPPClass();
28 ~CPPClass(); 110 ~CPPClass();
29 111
112 // Register C++ methods exposed to Java using JNI.
nyquist 2015/05/13 06:47:20 What about Java methods that are marked with @Call
scheib 2015/05/13 21:59:52 That doesn't require external C++ setup, it works
113 static bool RegisterJNI(JNIEnv* env);
114
30 class InnerClass { 115 class InnerClass {
31 public: 116 public:
32 jdouble MethodOtherP0(JNIEnv* env, jobject obj); 117 jdouble MethodOtherP0(JNIEnv* env, jobject obj);
33 }; 118 };
34 119
35 void Destroy(JNIEnv* env, jobject obj); 120 void Destroy(JNIEnv* env, jobject obj);
36 121
37 jint Method(JNIEnv* env, jobject obj); 122 jint Method(JNIEnv* env, jobject obj);
38 123
39 void AddStructB(JNIEnv* env, jobject obj, jobject structb); 124 void AddStructB(JNIEnv* env, jobject obj, jobject structb);
40 125
41 void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj); 126 void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj);
42 127
43 base::android::ScopedJavaLocalRef<jstring> ReturnAString( 128 base::android::ScopedJavaLocalRef<jstring> ReturnAString(
44 JNIEnv* env, jobject obj); 129 JNIEnv* env, jobject obj);
45 130
46 private: 131 private:
47 std::map<long, std::string> map_; 132 std::map<long, std::string> map_;
48 133
49 DISALLOW_COPY_AND_ASSIGN(CPPClass); 134 DISALLOW_COPY_AND_ASSIGN(CPPClass);
50 }; 135 };
51 136
52 } // namespace android 137 } // namespace android
53 } // namespace base 138 } // namespace base
54 139
55 #endif // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ 140 #endif // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698