Ted Choc | 2db9f6f5 | 2023-07-18 13:35:18 | [diff] [blame] | 1 | # Android Java <-> C++ Ownership Best Practices |
| 2 | |
| 3 | Aims to provide best practices for maintaining the ownership and lifecycle of |
| 4 | logically paired Java / C++ objects (e.g. if you have a Foo object in Java and |
| 5 | one in C++ that are meant to represent the same concept). |
| 6 | |
| 7 | [TOC] |
| 8 | |
| 9 | ## Establish clear ownership |
| 10 | Either the Java or C++ object should own the lifecycle of the corresponding |
| 11 | object in the other language. The initially created object (either in C++ or |
| 12 | Java) should be the object that creates and owns the corresponding other |
| 13 | object. |
| 14 | |
| 15 | It is the responsibility of the initial object to handle destruction / teardown |
| 16 | of the corresponding other object. |
| 17 | |
| 18 | ### [Option #1] Java owns the C++ counterpart |
| 19 | Because Java objects are garbage collected and finalizers are prohibited in |
| 20 | Chromium ([link](/styleguide/java/java.md#finalizers)), an explicit |
| 21 | destroy / teardown method on the Java object is required to prevent leaking the |
| 22 | corresponding C++ object. The destroy / teardown method on the Java object |
| 23 | would call an appropriate function on the C++ object (via JNI) to trigger the |
| 24 | deletion of the C++ object. At this point, the Java object should reset its |
| 25 | pointer reference to the C++ object to prevent any calls to the now destroyed |
| 26 | C++ instance. |
| 27 | |
| 28 | ### [Option #2] C++ owns the Java counterpart |
| 29 | For C++ objects, utilizing the appropriate smart java references |
Sam Maier | e1df6f2 | 2023-08-11 14:20:40 | [diff] [blame] | 30 | ([link](/third_party/jni_zero/README.md#java-objects-and-garbage-collection), |
Ted Choc | 2db9f6f5 | 2023-07-18 13:35:18 | [diff] [blame] | 31 | [code ref](/base/android/scoped_java_ref.h)) will ensure corresponding Java |
| 32 | objects can be garbage collected. But if the Java object requires cleaning up |
| 33 | dependencies, the C++ object should call a corresponding teardown method on the |
| 34 | Java object in its destructor. |
| 35 | |
| 36 | Even in cases where the Java object does not have dependencies requiring clean |
| 37 | up, the C++ object should notify the Java object that is has gone away. Then the |
| 38 | Java object can reset its pointer reference to the C++ object and prevent any |
| 39 | calls to the already destroyed object. |
| 40 | |
| 41 | ## Enforce relationship cardinality |
| 42 | There should be one Java object per native object (and vice versa) to keep the |
| 43 | lifecycle simple and easily understood. |
| 44 | |
| 45 | For example, there is one BookmarkModel per Chrome profile in C++, and |
| 46 | therefore, there should only be one BookmarkModel instance per Profile in Java. |
| 47 | |
| 48 | ## Pick a side for your business logic |
| 49 | Where possible, keep the business logic in either C++ or Java, and have the |
| 50 | other object simply act as a shim to the other. |
| 51 | |
| 52 | To facilitate cross-platform development, C++ is the preferred place for |
| 53 | business logic that could be shared in the future. |
| 54 | |
| 55 | ## Prefer colocation |
| 56 | The code of the Java and C++ object should be colocated to ensure consistent |
| 57 | layering and dependencies.. |
| 58 | |
| 59 | If the C++ object is in //components/[foo], then the corresponding Java object |
| 60 | should also reside in //components/[foo]. |
| 61 | |
| 62 | ## Keep your C++ code close and your Java code closer |
| 63 | The C++ code shared across platforms and the corresponding Java class should be |
| 64 | as close as possible in the code. |
| 65 | |
| 66 | For cases where there are just a few Java <-> C++ calls, try to simply inline |
| 67 | those into the same C++ file to minimize indirection. |
| 68 | |
| 69 | **Example:** |
| 70 | |
| 71 | //components/[foo]/foo_factory.cc |
| 72 | ```c++ |
| 73 | <...> cross platform includes |
| 74 | |
| 75 | #if BUILDFLAG(IS_ANDROID) |
| 76 | #include “base/android/scoped_java_ref.h” |
| 77 | #include “components/[foo]/android/jni_headers/FooFactory_jni.h” |
| 78 | #endif // BUILDFLAG(IS_ANDROID) |
| 79 | |
| 80 | <...> shared functions |
| 81 | |
| 82 | #if BUILDFLAG(IS_ANDROID) |
| 83 | static ScopedJavaLocalRef<jobject> JNI_FooFactory_Get(JNIEnv* env) { |
| 84 | return FooFactory::Get()->GetJavaObject(); |
| 85 | } |
| 86 | #endif // BUILDFLAG(IS_ANDROID) |
| 87 | ``` |
| 88 | |
| 89 | For cases where the Java <-> C++ API surface is substantial (e.g. if you have a |
| 90 | C++ object with a large public API and you want to expose all those functions to |
| 91 | Java), you can split out a JNI methods to a separate class that is owned by the |
| 92 | primary C++ object. This approach is suitable when we want to minimize the JNI |
| 93 | boilerplate in the C++ class. |
| 94 | |
| 95 | **Example:** |
| 96 | |
| 97 | //components/[foo]/foo.h |
| 98 | ```c++ |
| 99 | class Foo { |
| 100 | public: |
| 101 | <...> |
| 102 | |
| 103 | #if BUILDFLAG(IS_ANDROID) |
| 104 | void DoSomething(); |
| 105 | #endif // BUILDFLAG(IS_ANDROID) |
| 106 | |
| 107 | private: |
| 108 | #if BUILDFLAG(IS_ANDROID) |
| 109 | std::unique_ptr<FooAndroid> foo_android_; |
| 110 | #endif // BUILDFLAG(IS_ANDROID) |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | //components/[foo]/foo.cc |
| 115 | ```c++ |
| 116 | <...> |
| 117 | |
| 118 | #if BUILDFLAG(IS_ANDROID) |
| 119 | void Foo::DoSomething() { |
| 120 | if (!foo_android_) { |
| 121 | foo_android_ = std::make_unique<FooAndroid>(this); |
| 122 | } |
| 123 | foo_android_->DoSomething(); |
| 124 | } |
| 125 | #endif // BUILDFLAG(IS_ANDROID) |
| 126 | ``` |
| 127 | |
| 128 | //components/[foo]/android/foo_android.h |
| 129 | ```c++ |
| 130 | class FooAndroid { |
| 131 | public: |
| 132 | void DoAThing(); |
| 133 | |
| 134 | // JNI methods called from Java. |
| 135 | void SomethingElse(JNIEnv* env); |
| 136 | jboolean AndABooleanToo(JNIEnv* env); |
| 137 | <...> |
| 138 | |
| 139 | private: |
| 140 | const raw_ptr<Foo> foo_; |
| 141 | base::android::ScopedJavaGlobalRef<jobject> java_ref_; |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | //components/[foo]/android/foo_android.cc |
| 146 | ```c++ |
| 147 | FooAndroid::FooAndroid(Foo* foo) : foo_(foo) {} |
| 148 | |
| 149 | FooAndroid::DoAThing() { |
| 150 | Java_Foo_DoAThing(base::android::AttachCurrentThread(), java_ref_); |
| 151 | } |
| 152 | |
| 153 | void FooAndroid::SomethingElse(JNIEnv* env) { |
| 154 | foo_->SomethingElse(); |
| 155 | } |
| 156 | |
| 157 | jboolean FooAndroid::AndABooleanToo(JNIEnv* env) { |
| 158 | return foo->AndABooleanToo(); |
| 159 | } |
| 160 | ``` |
| 161 | |
| 162 | ## When Lifetime is Hard |
| 163 | We do not allow the [use of finalizers](/styleguide/java/java.md#Finalizers), |
| 164 | but there are a couple of other tricks that have been used to clean up objects |
| 165 | besides explicit lifetimes: |
| 166 | 1. Destroy and re-create the native object every time you need it |
| 167 | ([GURL does this](/url/android/java/src/org/chromium/url/Parsed.java)). |
| 168 | 2. Use a reference queue that is flushed every once in a while |
| 169 | ([example](https://source.chromium.org/search?q=symbol:TaskRunnerImpl.destroyGarbageCollectedTaskRunners)). |