OSDN Git Service

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