blob: 637a1e9b644c91c189feee4211b026662a8bb17f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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#ifndef ANDROID_PARCEL_H
18#define ANDROID_PARCEL_H
19
Casey Dahlin451ff582015-10-19 18:12:18 -070020#include <vector>
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <cutils/native_handle.h>
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/String16.h>
26#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070027#include <utils/Flattenable.h>
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080028#include <linux/binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Casey Dahlinf0c13772015-10-27 18:33:56 -070030#include <binder/IInterface.h>
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032// ---------------------------------------------------------------------------
33namespace android {
34
Mathias Agopiane1424282013-07-29 21:24:40 -070035template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070036template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070038class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039class ProcessState;
40class String8;
41class TextOutput;
42
Mathias Agopiane1424282013-07-29 21:24:40 -070043class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000044 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070046 class ReadableBlob;
47 class WritableBlob;
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 Parcel();
50 ~Parcel();
51
52 const uint8_t* data() const;
53 size_t dataSize() const;
54 size_t dataAvail() const;
55 size_t dataPosition() const;
56 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 status_t setDataSize(size_t size);
59 void setDataPosition(size_t pos) const;
60 status_t setDataCapacity(size_t size);
61
62 status_t setData(const uint8_t* buffer, size_t len);
63
Andreas Huber51faf462011-04-13 10:21:56 -070064 status_t appendFrom(const Parcel *parcel,
65 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Jeff Brown13b16042014-11-11 16:44:25 -080067 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070068 bool pushAllowFds(bool allowFds);
69 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 bool hasFileDescriptors() const;
72
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070073 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070076 // Parses the RPC header, returning true if the interface name
77 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070078 //
79 // Additionally, enforceInterface does part of the work of
80 // propagating the StrictMode policy mask, populating the current
81 // IPCThreadState, which as an optimization may optionally be
82 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070083 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070084 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070085 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070086
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 void freeData();
88
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080089private:
90 const binder_size_t* objects() const;
91
92public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 size_t objectsCount() const;
94
95 status_t errorCheck() const;
96 void setError(status_t err);
97
98 status_t write(const void* data, size_t len);
99 void* writeInplace(size_t len);
100 status_t writeUnpadded(const void* data, size_t len);
101 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800102 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700104 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 status_t writeFloat(float val);
106 status_t writeDouble(double val);
107 status_t writeCString(const char* str);
108 status_t writeString8(const String8& str);
109 status_t writeString16(const String16& str);
110 status_t writeString16(const char16_t* str, size_t len);
111 status_t writeStrongBinder(const sp<IBinder>& val);
112 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700113 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700114 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700115 status_t writeBool(bool val);
116 status_t writeChar(char16_t val);
117 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700118
Casey Dahlin451ff582015-10-19 18:12:18 -0700119 status_t writeByteVector(const std::vector<int8_t>& val);
120 status_t writeInt32Vector(const std::vector<int32_t>& val);
121 status_t writeInt64Vector(const std::vector<int64_t>& val);
122 status_t writeFloatVector(const std::vector<float>& val);
123 status_t writeDoubleVector(const std::vector<double>& val);
124 status_t writeBoolVector(const std::vector<bool>& val);
125 status_t writeCharVector(const std::vector<char16_t>& val);
126 status_t writeString16Vector(const std::vector<String16>& val);
127
Mathias Agopiane1424282013-07-29 21:24:40 -0700128 template<typename T>
129 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130
Mathias Agopian8683fca2012-08-12 19:37:16 -0700131 template<typename T>
132 status_t write(const LightFlattenable<T>& val);
133
134
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700135 // Place a native_handle into the parcel (the native_handle's file-
136 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700137 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700138 // Doesn't take ownership of the native_handle.
139 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140
141 // Place a file descriptor into the parcel. The given fd must remain
142 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700143 // The Parcel does not take ownership of the given fd unless you ask it to.
144 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
146 // Place a file descriptor into the parcel. A dup of the fd is made, which
147 // will be closed once the parcel is destroyed.
148 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700149
150 // Writes a blob to the parcel.
151 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800152 // transferred by way of an anonymous shared memory region. Prefer sending
153 // immutable blobs if possible since they may be subsequently transferred between
154 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700155 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800156 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
157
158 // Write an existing immutable blob file descriptor to the parcel.
159 // This allows the client to send the same blob to multiple processes
160 // as long as it keeps a dup of the blob file descriptor handy for later.
161 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
164
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700165 // Like Parcel.java's writeNoException(). Just writes a zero int32.
166 // Currently the native implementation doesn't do any of the StrictMode
167 // stack gathering and serialization that the Java implementation does.
168 status_t writeNoException();
169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 void remove(size_t start, size_t amt);
171
172 status_t read(void* outData, size_t len) const;
173 const void* readInplace(size_t len) const;
174 int32_t readInt32() const;
175 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800176 uint32_t readUint32() const;
177 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 int64_t readInt64() const;
179 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700180 uint64_t readUint64() const;
181 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 float readFloat() const;
183 status_t readFloat(float *pArg) const;
184 double readDouble() const;
185 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700186 intptr_t readIntPtr() const;
187 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700188 bool readBool() const;
189 status_t readBool(bool *pArg) const;
190 char16_t readChar() const;
191 status_t readChar(char16_t *pArg) const;
192 int8_t readByte() const;
193 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194
195 const char* readCString() const;
196 String8 readString8() const;
197 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700198 status_t readString16(String16* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 const char16_t* readString16Inplace(size_t* outLen) const;
200 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700201 status_t readStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700203
Casey Dahlinf0c13772015-10-27 18:33:56 -0700204 template<typename T>
205 status_t readStrongBinder(sp<T>* val) const;
206
Casey Dahlin451ff582015-10-19 18:12:18 -0700207 status_t readByteVector(std::vector<int8_t>* val) const;
208 status_t readInt32Vector(std::vector<int32_t>* val) const;
209 status_t readInt64Vector(std::vector<int64_t>* val) const;
210 status_t readFloatVector(std::vector<float>* val) const;
211 status_t readDoubleVector(std::vector<double>* val) const;
212 status_t readBoolVector(std::vector<bool>* val) const;
213 status_t readCharVector(std::vector<char16_t>* val) const;
214 status_t readString16Vector(std::vector<String16>* val) const;
215
Mathias Agopiane1424282013-07-29 21:24:40 -0700216 template<typename T>
217 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700218
Mathias Agopian8683fca2012-08-12 19:37:16 -0700219 template<typename T>
220 status_t read(LightFlattenable<T>& val) const;
221
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700222 // Like Parcel.java's readExceptionCode(). Reads the first int32
223 // off of a Parcel's header, returning 0 or the negative error
224 // code on exceptions, but also deals with skipping over rich
225 // response headers. Callers should use this to read & parse the
226 // response headers rather than doing it by hand.
227 int32_t readExceptionCode() const;
228
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700229 // Retrieve native_handle from the parcel. This returns a copy of the
230 // parcel's native_handle (the caller takes ownership). The caller
231 // must free the native_handle with native_handle_close() and
232 // native_handle_delete().
233 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
235
236 // Retrieve a file descriptor from the parcel. This returns the raw fd
237 // in the parcel, which you do not own -- use dup() to get your own copy.
238 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700239
240 // Reads a blob from the parcel.
241 // The caller should call release() on the blob after reading its contents.
242 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 const flat_binder_object* readObject(bool nullMetaData) const;
245
246 // Explicitly close all file descriptors in the parcel.
247 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800248
249 // Debugging: get metrics on current allocations.
250 static size_t getGlobalAllocSize();
251 static size_t getGlobalAllocCount();
252
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 typedef void (*release_func)(Parcel* parcel,
255 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 void* cookie);
258
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800261 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 size_t ipcObjectsCount() const;
263 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800264 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 release_func relFunc, void* relCookie);
266
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270private:
271 Parcel(const Parcel& o);
272 Parcel& operator=(const Parcel& o);
273
274 status_t finishWrite(size_t len);
275 void releaseObjects();
276 void acquireObjects();
277 status_t growData(size_t len);
278 status_t restartWrite(size_t desired);
279 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000280 status_t writePointer(uintptr_t val);
281 status_t readPointer(uintptr_t *pArg) const;
282 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 void freeDataNoInit();
284 void initState();
285 void scanForFds() const;
286
Andreas Huber84a6d042009-08-17 13:33:27 -0700287 template<class T>
288 status_t readAligned(T *pArg) const;
289
290 template<class T> T readAligned() const;
291
292 template<class T>
293 status_t writeAligned(T val);
294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 status_t mError;
296 uint8_t* mData;
297 size_t mDataSize;
298 size_t mDataCapacity;
299 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800300 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 size_t mObjectsSize;
302 size_t mObjectsCapacity;
303 mutable size_t mNextObjectHint;
304
305 mutable bool mFdsKnown;
306 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400307 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
309 release_func mOwner;
310 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700311
312 class Blob {
313 public:
314 Blob();
315 ~Blob();
316
Jeff Brown13b16042014-11-11 16:44:25 -0800317 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700318 void release();
319 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800320 inline int fd() const { return mFd; };
321 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700322
323 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800324 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700325
Jeff Brown13b16042014-11-11 16:44:25 -0800326 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700327 void* mData;
328 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800329 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700330 };
331
Mathias Agopiane1424282013-07-29 21:24:40 -0700332 class FlattenableHelperInterface {
333 protected:
334 ~FlattenableHelperInterface() { }
335 public:
336 virtual size_t getFlattenedSize() const = 0;
337 virtual size_t getFdCount() const = 0;
338 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
339 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
340 };
341
342 template<typename T>
343 class FlattenableHelper : public FlattenableHelperInterface {
344 friend class Parcel;
345 const Flattenable<T>& val;
346 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
347
348 public:
349 virtual size_t getFlattenedSize() const {
350 return val.getFlattenedSize();
351 }
352 virtual size_t getFdCount() const {
353 return val.getFdCount();
354 }
355 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
356 return val.flatten(buffer, size, fds, count);
357 }
358 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
359 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
360 }
361 };
362 status_t write(const FlattenableHelperInterface& val);
363 status_t read(FlattenableHelperInterface& val) const;
364
Jeff Brown5707dbf2011-09-23 21:17:56 -0700365public:
366 class ReadableBlob : public Blob {
367 friend class Parcel;
368 public:
369 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800370 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700371 };
372
373 class WritableBlob : public Blob {
374 friend class Parcel;
375 public:
376 inline void* data() { return mData; }
377 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400378
379private:
380 size_t mBlobAshmemSize;
381
382public:
383 size_t getBlobAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384};
385
386// ---------------------------------------------------------------------------
387
Mathias Agopian8683fca2012-08-12 19:37:16 -0700388template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700389status_t Parcel::write(const Flattenable<T>& val) {
390 const FlattenableHelper<T> helper(val);
391 return write(helper);
392}
393
394template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700395status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700396 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700397 if (!val.isFixedSize()) {
398 status_t err = writeInt32(size);
399 if (err != NO_ERROR) {
400 return err;
401 }
402 }
Mathias Agopian20985172012-08-31 14:25:22 -0700403 if (size) {
404 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700405 if (buffer == NULL)
406 return NO_MEMORY;
407 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700408 }
409 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700410}
411
412template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700413status_t Parcel::read(Flattenable<T>& val) const {
414 FlattenableHelper<T> helper(val);
415 return read(helper);
416}
417
418template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700419status_t Parcel::read(LightFlattenable<T>& val) const {
420 size_t size;
421 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700422 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700423 } else {
424 int32_t s;
425 status_t err = readInt32(&s);
426 if (err != NO_ERROR) {
427 return err;
428 }
429 size = s;
430 }
Mathias Agopian20985172012-08-31 14:25:22 -0700431 if (size) {
432 void const* buffer = readInplace(size);
433 return buffer == NULL ? NO_MEMORY :
434 val.unflatten(buffer, size);
435 }
436 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700437}
438
Casey Dahlinf0c13772015-10-27 18:33:56 -0700439template<typename T>
440status_t Parcel::readStrongBinder(sp<T>* val) const {
441 sp<IBinder> tmp;
442 status_t ret = readStrongBinder(&tmp);
443
444 if (ret == OK) {
445 *val = interface_cast<T>(tmp);
446
447 if (val->get() == nullptr) {
448 return UNKNOWN_ERROR;
449 }
450 }
451
452 return ret;
453}
454
Mathias Agopian8683fca2012-08-12 19:37:16 -0700455// ---------------------------------------------------------------------------
456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
458{
459 parcel.print(to);
460 return to;
461}
462
463// ---------------------------------------------------------------------------
464
465// Generic acquire and release of objects.
466void acquire_object(const sp<ProcessState>& proc,
467 const flat_binder_object& obj, const void* who);
468void release_object(const sp<ProcessState>& proc,
469 const flat_binder_object& obj, const void* who);
470
471void flatten_binder(const sp<ProcessState>& proc,
472 const sp<IBinder>& binder, flat_binder_object* out);
473void flatten_binder(const sp<ProcessState>& proc,
474 const wp<IBinder>& binder, flat_binder_object* out);
475status_t unflatten_binder(const sp<ProcessState>& proc,
476 const flat_binder_object& flat, sp<IBinder>* out);
477status_t unflatten_binder(const sp<ProcessState>& proc,
478 const flat_binder_object& flat, wp<IBinder>* out);
479
480}; // namespace android
481
482// ---------------------------------------------------------------------------
483
484#endif // ANDROID_PARCEL_H