Binder.cpp revision f683e0163a84d93448b9388126902242367cd961
1/*
2 * Copyright (C) 2005 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 <binder/Binder.h>
18
19#include <utils/Atomic.h>
20#include <utils/misc.h>
21#include <binder/BpBinder.h>
22#include <binder/IInterface.h>
23#include <binder/Parcel.h>
24
25#include <stdio.h>
26
27namespace android {
28
29// ---------------------------------------------------------------------------
30
31IBinder::IBinder()
32    : RefBase()
33{
34}
35
36IBinder::~IBinder()
37{
38}
39
40// ---------------------------------------------------------------------------
41
42sp<IInterface>  IBinder::queryLocalInterface(const String16& descriptor)
43{
44    return NULL;
45}
46
47BBinder* IBinder::localBinder()
48{
49    return NULL;
50}
51
52BpBinder* IBinder::remoteBinder()
53{
54    return NULL;
55}
56
57bool IBinder::checkSubclass(const void* /*subclassID*/) const
58{
59    return false;
60}
61
62// ---------------------------------------------------------------------------
63
64class BBinder::Extras
65{
66public:
67    Mutex mLock;
68    BpBinder::ObjectManager mObjects;
69};
70
71// ---------------------------------------------------------------------------
72
73BBinder::BBinder()
74    : mExtras(NULL)
75{
76}
77
78bool BBinder::isBinderAlive() const
79{
80    return true;
81}
82
83status_t BBinder::pingBinder()
84{
85    return NO_ERROR;
86}
87
88const String16& BBinder::getInterfaceDescriptor() const
89{
90    // This is a local static rather than a global static,
91    // to avoid static initializer ordering issues.
92    static String16 sEmptyDescriptor;
93    ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
94    return sEmptyDescriptor;
95}
96
97status_t BBinder::transact(
98    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
99{
100    data.setDataPosition(0);
101
102    status_t err = NO_ERROR;
103    switch (code) {
104        case PING_TRANSACTION:
105            reply->writeInt32(pingBinder());
106            break;
107        default:
108            err = onTransact(code, data, reply, flags);
109            break;
110    }
111
112    if (reply != NULL) {
113        reply->setDataPosition(0);
114    }
115
116    return err;
117}
118
119status_t BBinder::linkToDeath(
120    const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
121{
122    return INVALID_OPERATION;
123}
124
125status_t BBinder::unlinkToDeath(
126    const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
127    wp<DeathRecipient>* outRecipient)
128{
129    return INVALID_OPERATION;
130}
131
132status_t BBinder::dump(int fd, const Vector<String16>& args)
133{
134    return NO_ERROR;
135}
136
137void BBinder::attachObject(
138    const void* objectID, void* object, void* cleanupCookie,
139    object_cleanup_func func)
140{
141    Extras* e = mExtras;
142
143    if (!e) {
144        e = new Extras;
145#ifdef __LP64__
146        if (android_atomic_release_cas64(0, reinterpret_cast<int64_t>(e),
147                reinterpret_cast<volatile int64_t*>(&mExtras)) != 0) {
148#else
149        if (android_atomic_cmpxchg(0, reinterpret_cast<int32_t>(e),
150                reinterpret_cast<volatile int32_t*>(&mExtras)) != 0) {
151#endif
152            delete e;
153            e = mExtras;
154        }
155        if (e == 0) return; // out of memory
156    }
157
158    AutoMutex _l(e->mLock);
159    e->mObjects.attach(objectID, object, cleanupCookie, func);
160}
161
162void* BBinder::findObject(const void* objectID) const
163{
164    Extras* e = mExtras;
165    if (!e) return NULL;
166
167    AutoMutex _l(e->mLock);
168    return e->mObjects.find(objectID);
169}
170
171void BBinder::detachObject(const void* objectID)
172{
173    Extras* e = mExtras;
174    if (!e) return;
175
176    AutoMutex _l(e->mLock);
177    e->mObjects.detach(objectID);
178}
179
180BBinder* BBinder::localBinder()
181{
182    return this;
183}
184
185BBinder::~BBinder()
186{
187    if (mExtras) delete mExtras;
188}
189
190
191status_t BBinder::onTransact(
192    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
193{
194    switch (code) {
195        case INTERFACE_TRANSACTION:
196            reply->writeString16(getInterfaceDescriptor());
197            return NO_ERROR;
198
199        case DUMP_TRANSACTION: {
200            int fd = data.readFileDescriptor();
201            int argc = data.readInt32();
202            Vector<String16> args;
203            for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
204               args.add(data.readString16());
205            }
206            return dump(fd, args);
207        }
208
209        case SYSPROPS_TRANSACTION: {
210            report_sysprop_change();
211            return NO_ERROR;
212        }
213
214        default:
215            return UNKNOWN_TRANSACTION;
216    }
217}
218
219// ---------------------------------------------------------------------------
220
221enum {
222    // This is used to transfer ownership of the remote binder from
223    // the BpRefBase object holding it (when it is constructed), to the
224    // owner of the BpRefBase object when it first acquires that BpRefBase.
225    kRemoteAcquired = 0x00000001
226};
227
228BpRefBase::BpRefBase(const sp<IBinder>& o)
229    : mRemote(o.get()), mRefs(NULL), mState(0)
230{
231    extendObjectLifetime(OBJECT_LIFETIME_WEAK);
232
233    if (mRemote) {
234        mRemote->incStrong(this);           // Removed on first IncStrong().
235        mRefs = mRemote->createWeak(this);  // Held for our entire lifetime.
236    }
237}
238
239BpRefBase::~BpRefBase()
240{
241    if (mRemote) {
242        if (!(mState&kRemoteAcquired)) {
243            mRemote->decStrong(this);
244        }
245        mRefs->decWeak(this);
246    }
247}
248
249void BpRefBase::onFirstRef()
250{
251    android_atomic_or(kRemoteAcquired, &mState);
252}
253
254void BpRefBase::onLastStrongRef(const void* id)
255{
256    if (mRemote) {
257        mRemote->decStrong(this);
258    }
259}
260
261bool BpRefBase::onIncStrongAttempted(uint32_t flags, const void* id)
262{
263    return mRemote ? mRefs->attemptIncStrong(this) : false;
264}
265
266// ---------------------------------------------------------------------------
267
268}; // namespace android
269