blob: eed09f02c751d265988236caa45af2c1bc0a8372 [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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
17#include <android-base/logging.h>
18#include <android/binder_manager.h>
19#include <iface/iface.h>
20
21using ::android::sp;
22using ::android::wp;
23
24const char* IFoo::kSomeInstanceName = "libbinder_ndk-test-IFoo";
25const char* kIFooDescriptor = "my-special-IFoo-class";
26
27struct IFoo_Class_Data {
28 sp<IFoo> foo;
29};
30
31void* IFoo_Class_onCreate(void* args) {
32 IFoo_Class_Data* foo = static_cast<IFoo_Class_Data*>(args);
33 // This is a foo, but we're currently not verifying that. So, the method newLocalBinder is
34 // coupled with this.
35 return static_cast<void*>(foo);
36}
37
38void IFoo_Class_onDestroy(void* userData) {
39 delete static_cast<IFoo_Class_Data*>(userData);
40}
41
42binder_status_t IFoo_Class_onTransact(AIBinder* binder, transaction_code_t code, const AParcel* in,
43 AParcel* out) {
44 binder_status_t stat = EX_UNSUPPORTED_OPERATION;
45
46 sp<IFoo> foo = static_cast<IFoo_Class_Data*>(AIBinder_getUserData(binder))->foo;
47 CHECK(foo != nullptr) << "Transaction made on already deleted object";
48
49 switch (code) {
50 case IFoo::DOFOO: {
51 int32_t valueIn;
52 stat = AParcel_readInt32(in, &valueIn);
53 if (stat != EX_NONE) break;
54 int32_t valueOut = foo->doubleNumber(valueIn);
55 stat = AParcel_writeInt32(out, valueOut);
56 break;
57 }
58 }
59
60 return stat;
61}
62
63AIBinder_Class* IFoo::kClass = AIBinder_Class_define(kIFooDescriptor, IFoo_Class_onCreate,
64 IFoo_Class_onDestroy, IFoo_Class_onTransact);
65
66class BpFoo : public IFoo {
67public:
68 BpFoo(AIBinder* binder) : mBinder(binder) {}
69 virtual ~BpFoo() { AIBinder_decStrong(mBinder); }
70
71 virtual int32_t doubleNumber(int32_t in) {
72 AParcel* parcelIn;
73 CHECK(EX_NONE == AIBinder_prepareTransaction(mBinder, &parcelIn));
74
75 CHECK(EX_NONE == AParcel_writeInt32(parcelIn, in));
76
77 AParcel* parcelOut;
78 CHECK(EX_NONE ==
79 AIBinder_transact(mBinder, IFoo::DOFOO, &parcelIn, &parcelOut, 0 /*flags*/));
80
81 int32_t out;
82 CHECK(EX_NONE == AParcel_readInt32(parcelOut, &out));
83
84 CHECK(EX_NONE == AIBinder_finalizeTransaction(mBinder, &parcelOut));
85 return out;
86 }
87
88private:
89 // Always assumes one refcount
90 AIBinder* mBinder;
91};
92
93IFoo::~IFoo() {
94 AIBinder_Weak_delete(mWeakBinder);
95}
96
97binder_status_t IFoo::addService(const char* instance) {
98 AIBinder* binder = nullptr;
99
100 if (mWeakBinder != nullptr) {
101 // one strong ref count of binder
102 binder = AIBinder_Weak_promote(mWeakBinder);
103 }
104 if (binder == nullptr) {
105 // or one strong refcount here
106 binder = AIBinder_new(IFoo::kClass, static_cast<void*>(new IFoo_Class_Data{this}));
107 if (mWeakBinder != nullptr) {
108 AIBinder_Weak_delete(mWeakBinder);
109 }
110 mWeakBinder = AIBinder_Weak_new(binder);
111 }
112
113 binder_status_t status = AServiceManager_addService(binder, instance);
114 // Strong references we care about kept by remote process
115 AIBinder_decStrong(binder);
116 return status;
117}
118
119sp<IFoo> IFoo::getService(const char* instance) {
120 AIBinder* binder = AServiceManager_getService(instance); // maybe nullptr
121 AIBinder_associateClass(&binder, IFoo::kClass);
122
123 if (binder == nullptr) {
124 return nullptr;
125 }
126
127 if (AIBinder_isRemote(binder)) {
128 sp<IFoo> ret = new BpFoo(binder); // takes ownership of binder
129 return ret;
130 }
131
132 IFoo_Class_Data* data = static_cast<IFoo_Class_Data*>(AIBinder_getUserData(binder));
133
134 CHECK(data != nullptr); // always created with non-null data
135
136 sp<IFoo> ret = data->foo;
137
138 AIBinder* held = AIBinder_Weak_promote(ret->mWeakBinder);
139 CHECK(held == binder);
140 AIBinder_decStrong(held);
141
142 // IFoo only keeps a weak reference to AIBinder, so we can drop this
143 AIBinder_decStrong(binder);
144 return ret;
145}