• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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_BINDER_BOX_H_
6 #define BASE_ANDROID_BINDER_BOX_H_
7 
8 #include <jni.h>
9 
10 #include <vector>
11 
12 #include "base/android/binder.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/base_export.h"
15 
16 namespace base::android {
17 
18 // Creates a new binder box containing `binders` and returns a Java reference to
19 // it. The Java reference (which itself is an android.os.IBinder) may be passed
20 // to another process and unpacked there by UnpackBinderBox().
21 //
22 // The point of this thing is to conveniently pass native binders through Java
23 // code (e.g. across Java AIDL) without actually taking Java references to them.
24 // This is desirable because by design AIBinder_toJavaBinder actually leaks
25 // IBinder references for an indeterminate period of time, which is unacceptable
26 // for native binder users who want deterministic control of their binder's
27 // refcounts.
28 BASE_EXPORT ScopedJavaLocalRef<jobject> PackBinderBox(
29     JNIEnv* env,
30     std::vector<BinderRef> binders);
31 
32 // Retrieves a collection of binders stashed in a binder box.
33 BASE_EXPORT BinderStatusOr<std::vector<BinderRef>> UnpackBinderBox(
34     JNIEnv* env,
35     const JavaRef<jobject>& box);
36 
37 }  // namespace base::android
38 
39 #endif  // BASE_ANDROID_BINDER_BOX_H_
40