xref: /aosp_15_r20/cts/tests/tests/binder_ndk/libbinder_ndk_test/android_binder_cts_NativeService.cpp (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define LOG_TAG "Cts-NdkBinderTest"
17 
18 #include <android/binder_ibinder_jni.h>
19 #include <android/log.h>
20 
21 #include "itest_impl.h"
22 
23 using ::aidl::test_package::ITest;
24 using ::ndk::SharedRefBase;
25 using ::ndk::SpAIBinder;
26 
27 extern "C" JNIEXPORT jobject JNICALL
Java_android_binder_cts_NativeService_getBinder_1native(JNIEnv * env)28 Java_android_binder_cts_NativeService_getBinder_1native(JNIEnv* env) {
29   // The ref owns the MyTest, and the binder owns the ref.
30   SpAIBinder binder = SharedRefBase::make<MyTest>()->asBinder();
31 
32   // adding an arbitrary class as the extension
33   std::shared_ptr<MyTest> ext = SharedRefBase::make<MyTest>();
34   SpAIBinder extBinder = ext->asBinder();
35 
36   binder_status_t ret = AIBinder_setExtension(binder.get(), extBinder.get());
37   if (ret != STATUS_OK) {
38     __android_log_write(ANDROID_LOG_ERROR, LOG_TAG, "Could not set local extension");
39   }
40 
41   // And the Java object owns the binder
42   return AIBinder_toJavaBinder(env, binder.get());
43 }
44