2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <binder/Binder.h>
19 #include <utils/Atomic.h>
20 #include <binder/BpBinder.h>
21 #include <binder/IInterface.h>
22 #include <binder/Parcel.h>
28 // ---------------------------------------------------------------------------
39 // ---------------------------------------------------------------------------
41 sp<IInterface> IBinder::queryLocalInterface(const String16& descriptor)
46 BBinder* IBinder::localBinder()
51 BpBinder* IBinder::remoteBinder()
56 bool IBinder::checkSubclass(const void* /*subclassID*/) const
61 // ---------------------------------------------------------------------------
67 BpBinder::ObjectManager mObjects;
70 // ---------------------------------------------------------------------------
77 bool BBinder::isBinderAlive() const
82 status_t BBinder::pingBinder()
87 const String16& BBinder::getInterfaceDescriptor() const
89 // This is a local static rather than a global static,
90 // to avoid static initializer ordering issues.
91 static String16 sEmptyDescriptor;
92 LOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
93 return sEmptyDescriptor;
96 status_t BBinder::transact(
97 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
99 data.setDataPosition(0);
101 status_t err = NO_ERROR;
103 case PING_TRANSACTION:
104 reply->writeInt32(pingBinder());
107 err = onTransact(code, data, reply, flags);
112 reply->setDataPosition(0);
118 status_t BBinder::linkToDeath(
119 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
121 return INVALID_OPERATION;
124 status_t BBinder::unlinkToDeath(
125 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
126 wp<DeathRecipient>* outRecipient)
128 return INVALID_OPERATION;
131 status_t BBinder::dump(int fd, const Vector<String16>& args)
136 void BBinder::attachObject(
137 const void* objectID, void* object, void* cleanupCookie,
138 object_cleanup_func func)
144 if (android_atomic_cmpxchg(0, reinterpret_cast<int32_t>(e),
145 reinterpret_cast<volatile int32_t*>(&mExtras)) != 0) {
149 if (e == 0) return; // out of memory
152 AutoMutex _l(e->mLock);
153 e->mObjects.attach(objectID, object, cleanupCookie, func);
156 void* BBinder::findObject(const void* objectID) const
161 AutoMutex _l(e->mLock);
162 return e->mObjects.find(objectID);
165 void BBinder::detachObject(const void* objectID)
170 AutoMutex _l(e->mLock);
171 e->mObjects.detach(objectID);
174 BBinder* BBinder::localBinder()
181 if (mExtras) delete mExtras;
185 status_t BBinder::onTransact(
186 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
189 case INTERFACE_TRANSACTION:
190 reply->writeString16(getInterfaceDescriptor());
193 case DUMP_TRANSACTION: {
194 int fd = data.readFileDescriptor();
195 int argc = data.readInt32();
196 Vector<String16> args;
197 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
198 args.add(data.readString16());
200 return dump(fd, args);
203 return UNKNOWN_TRANSACTION;
207 // ---------------------------------------------------------------------------
210 // This is used to transfer ownership of the remote binder from
211 // the BpRefBase object holding it (when it is constructed), to the
212 // owner of the BpRefBase object when it first acquires that BpRefBase.
213 kRemoteAcquired = 0x00000001
216 BpRefBase::BpRefBase(const sp<IBinder>& o)
217 : mRemote(o.get()), mRefs(NULL), mState(0)
219 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
222 mRemote->incStrong(this); // Removed on first IncStrong().
223 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
227 BpRefBase::~BpRefBase()
230 if (!(mState&kRemoteAcquired)) {
231 mRemote->decStrong(this);
233 mRefs->decWeak(this);
237 void BpRefBase::onFirstRef()
239 android_atomic_or(kRemoteAcquired, &mState);
242 void BpRefBase::onLastStrongRef(const void* id)
245 mRemote->decStrong(this);
249 bool BpRefBase::onIncStrongAttempted(uint32_t flags, const void* id)
251 return mRemote ? mRefs->attemptIncStrong(this) : false;
254 // ---------------------------------------------------------------------------
256 }; // namespace android