blob: a12d68d0e9dfb6994d95a17b175933962fa095e0 [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>
Christopher Wiley97f048d2015-11-19 06:49:05 -080031#include <binder/Parcelable.h>
Casey Dahlinf0c13772015-10-27 18:33:56 -070032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033// ---------------------------------------------------------------------------
34namespace android {
35
Mathias Agopiane1424282013-07-29 21:24:40 -070036template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070037template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070039class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040class ProcessState;
41class String8;
42class TextOutput;
43
Mathias Agopiane1424282013-07-29 21:24:40 -070044class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000045 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070047 class ReadableBlob;
48 class WritableBlob;
49
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050 Parcel();
51 ~Parcel();
52
53 const uint8_t* data() const;
54 size_t dataSize() const;
55 size_t dataAvail() const;
56 size_t dataPosition() const;
57 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 status_t setDataSize(size_t size);
60 void setDataPosition(size_t pos) const;
61 status_t setDataCapacity(size_t size);
62
63 status_t setData(const uint8_t* buffer, size_t len);
64
Andreas Huber51faf462011-04-13 10:21:56 -070065 status_t appendFrom(const Parcel *parcel,
66 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Jeff Brown13b16042014-11-11 16:44:25 -080068 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070069 bool pushAllowFds(bool allowFds);
70 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 bool hasFileDescriptors() const;
73
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070074 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070077 // Parses the RPC header, returning true if the interface name
78 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070079 //
80 // Additionally, enforceInterface does part of the work of
81 // propagating the StrictMode policy mask, populating the current
82 // IPCThreadState, which as an optimization may optionally be
83 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070084 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070085 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070086 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 void freeData();
89
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080090private:
91 const binder_size_t* objects() const;
92
93public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 size_t objectsCount() const;
95
96 status_t errorCheck() const;
97 void setError(status_t err);
98
99 status_t write(const void* data, size_t len);
100 void* writeInplace(size_t len);
101 status_t writeUnpadded(const void* data, size_t len);
102 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800103 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700105 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 status_t writeFloat(float val);
107 status_t writeDouble(double val);
108 status_t writeCString(const char* str);
109 status_t writeString8(const String8& str);
110 status_t writeString16(const String16& str);
111 status_t writeString16(const char16_t* str, size_t len);
112 status_t writeStrongBinder(const sp<IBinder>& val);
113 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700114 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700115 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700116 status_t writeBool(bool val);
117 status_t writeChar(char16_t val);
118 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700119
Casey Dahlin451ff582015-10-19 18:12:18 -0700120 status_t writeByteVector(const std::vector<int8_t>& val);
121 status_t writeInt32Vector(const std::vector<int32_t>& val);
122 status_t writeInt64Vector(const std::vector<int64_t>& val);
123 status_t writeFloatVector(const std::vector<float>& val);
124 status_t writeDoubleVector(const std::vector<double>& val);
125 status_t writeBoolVector(const std::vector<bool>& val);
126 status_t writeCharVector(const std::vector<char16_t>& val);
127 status_t writeString16Vector(const std::vector<String16>& val);
128
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800129 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
130
Mathias Agopiane1424282013-07-29 21:24:40 -0700131 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800132 status_t writeParcelableVector(const std::vector<T>& val);
133 status_t writeParcelable(const Parcelable& parcelable);
134
135 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700136 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137
Mathias Agopian8683fca2012-08-12 19:37:16 -0700138 template<typename T>
139 status_t write(const LightFlattenable<T>& val);
140
141
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700142 // Place a native_handle into the parcel (the native_handle's file-
143 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700144 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700145 // Doesn't take ownership of the native_handle.
146 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147
148 // Place a file descriptor into the parcel. The given fd must remain
149 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700150 // The Parcel does not take ownership of the given fd unless you ask it to.
151 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152
153 // Place a file descriptor into the parcel. A dup of the fd is made, which
154 // will be closed once the parcel is destroyed.
155 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700156
157 // Writes a blob to the parcel.
158 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800159 // transferred by way of an anonymous shared memory region. Prefer sending
160 // immutable blobs if possible since they may be subsequently transferred between
161 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700162 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800163 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
164
165 // Write an existing immutable blob file descriptor to the parcel.
166 // This allows the client to send the same blob to multiple processes
167 // as long as it keeps a dup of the blob file descriptor handy for later.
168 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
171
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700172 // Like Parcel.java's writeNoException(). Just writes a zero int32.
173 // Currently the native implementation doesn't do any of the StrictMode
174 // stack gathering and serialization that the Java implementation does.
175 status_t writeNoException();
176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 void remove(size_t start, size_t amt);
178
179 status_t read(void* outData, size_t len) const;
180 const void* readInplace(size_t len) const;
181 int32_t readInt32() const;
182 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800183 uint32_t readUint32() const;
184 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 int64_t readInt64() const;
186 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700187 uint64_t readUint64() const;
188 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 float readFloat() const;
190 status_t readFloat(float *pArg) const;
191 double readDouble() const;
192 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700193 intptr_t readIntPtr() const;
194 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700195 bool readBool() const;
196 status_t readBool(bool *pArg) const;
197 char16_t readChar() const;
198 status_t readChar(char16_t *pArg) const;
199 int8_t readByte() const;
200 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201
202 const char* readCString() const;
203 String8 readString8() const;
204 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700205 status_t readString16(String16* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 const char16_t* readString16Inplace(size_t* outLen) const;
207 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700208 status_t readStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700210
Casey Dahlinf0c13772015-10-27 18:33:56 -0700211 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800212 status_t readParcelableVector(std::vector<T>* val) const;
213 status_t readParcelable(Parcelable* parcelable) const;
214
215 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800216 status_t readStrongBinder(sp<T>* val) const;
217
218 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700219
Casey Dahlin451ff582015-10-19 18:12:18 -0700220 status_t readByteVector(std::vector<int8_t>* val) const;
221 status_t readInt32Vector(std::vector<int32_t>* val) const;
222 status_t readInt64Vector(std::vector<int64_t>* val) const;
223 status_t readFloatVector(std::vector<float>* val) const;
224 status_t readDoubleVector(std::vector<double>* val) const;
225 status_t readBoolVector(std::vector<bool>* val) const;
226 status_t readCharVector(std::vector<char16_t>* val) const;
227 status_t readString16Vector(std::vector<String16>* val) const;
228
Mathias Agopiane1424282013-07-29 21:24:40 -0700229 template<typename T>
230 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700231
Mathias Agopian8683fca2012-08-12 19:37:16 -0700232 template<typename T>
233 status_t read(LightFlattenable<T>& val) const;
234
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700235 // Like Parcel.java's readExceptionCode(). Reads the first int32
236 // off of a Parcel's header, returning 0 or the negative error
237 // code on exceptions, but also deals with skipping over rich
238 // response headers. Callers should use this to read & parse the
239 // response headers rather than doing it by hand.
240 int32_t readExceptionCode() const;
241
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700242 // Retrieve native_handle from the parcel. This returns a copy of the
243 // parcel's native_handle (the caller takes ownership). The caller
244 // must free the native_handle with native_handle_close() and
245 // native_handle_delete().
246 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247
248
249 // Retrieve a file descriptor from the parcel. This returns the raw fd
250 // in the parcel, which you do not own -- use dup() to get your own copy.
251 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700252
253 // Reads a blob from the parcel.
254 // The caller should call release() on the blob after reading its contents.
255 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 const flat_binder_object* readObject(bool nullMetaData) const;
258
259 // Explicitly close all file descriptors in the parcel.
260 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800261
262 // Debugging: get metrics on current allocations.
263 static size_t getGlobalAllocSize();
264 static size_t getGlobalAllocCount();
265
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800266private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 typedef void (*release_func)(Parcel* parcel,
268 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800269 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 void* cookie);
271
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800274 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 size_t ipcObjectsCount() const;
276 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800277 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 release_func relFunc, void* relCookie);
279
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800280public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700282
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283private:
284 Parcel(const Parcel& o);
285 Parcel& operator=(const Parcel& o);
286
287 status_t finishWrite(size_t len);
288 void releaseObjects();
289 void acquireObjects();
290 status_t growData(size_t len);
291 status_t restartWrite(size_t desired);
292 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000293 status_t writePointer(uintptr_t val);
294 status_t readPointer(uintptr_t *pArg) const;
295 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 void freeDataNoInit();
297 void initState();
298 void scanForFds() const;
299
Andreas Huber84a6d042009-08-17 13:33:27 -0700300 template<class T>
301 status_t readAligned(T *pArg) const;
302
303 template<class T> T readAligned() const;
304
305 template<class T>
306 status_t writeAligned(T val);
307
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800308 template<typename T, typename U>
309 status_t unsafeReadTypedVector(std::vector<T>* val,
310 status_t(Parcel::*read_func)(U*) const) const;
311 template<typename T>
312 status_t readTypedVector(std::vector<T>* val,
313 status_t(Parcel::*read_func)(T*) const) const;
314 template<typename T, typename U>
315 status_t unsafeWriteTypedVector(const std::vector<T>& val,
316 status_t(Parcel::*write_func)(U));
317 template<typename T>
318 status_t writeTypedVector(const std::vector<T>& val,
319 status_t(Parcel::*write_func)(const T&));
320 template<typename T>
321 status_t writeTypedVector(const std::vector<T>& val,
322 status_t(Parcel::*write_func)(T));
323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 status_t mError;
325 uint8_t* mData;
326 size_t mDataSize;
327 size_t mDataCapacity;
328 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 size_t mObjectsSize;
331 size_t mObjectsCapacity;
332 mutable size_t mNextObjectHint;
333
334 mutable bool mFdsKnown;
335 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400336 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337
338 release_func mOwner;
339 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700340
341 class Blob {
342 public:
343 Blob();
344 ~Blob();
345
Jeff Brown13b16042014-11-11 16:44:25 -0800346 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700347 void release();
348 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800349 inline int fd() const { return mFd; };
350 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700351
352 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800353 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700354
Jeff Brown13b16042014-11-11 16:44:25 -0800355 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700356 void* mData;
357 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800358 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700359 };
360
Mathias Agopiane1424282013-07-29 21:24:40 -0700361 class FlattenableHelperInterface {
362 protected:
363 ~FlattenableHelperInterface() { }
364 public:
365 virtual size_t getFlattenedSize() const = 0;
366 virtual size_t getFdCount() const = 0;
367 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
368 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
369 };
370
371 template<typename T>
372 class FlattenableHelper : public FlattenableHelperInterface {
373 friend class Parcel;
374 const Flattenable<T>& val;
375 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
376
377 public:
378 virtual size_t getFlattenedSize() const {
379 return val.getFlattenedSize();
380 }
381 virtual size_t getFdCount() const {
382 return val.getFdCount();
383 }
384 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
385 return val.flatten(buffer, size, fds, count);
386 }
387 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
388 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
389 }
390 };
391 status_t write(const FlattenableHelperInterface& val);
392 status_t read(FlattenableHelperInterface& val) const;
393
Jeff Brown5707dbf2011-09-23 21:17:56 -0700394public:
395 class ReadableBlob : public Blob {
396 friend class Parcel;
397 public:
398 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800399 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700400 };
401
402 class WritableBlob : public Blob {
403 friend class Parcel;
404 public:
405 inline void* data() { return mData; }
406 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400407
408private:
409 size_t mBlobAshmemSize;
410
411public:
412 size_t getBlobAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413};
414
415// ---------------------------------------------------------------------------
416
Mathias Agopian8683fca2012-08-12 19:37:16 -0700417template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700418status_t Parcel::write(const Flattenable<T>& val) {
419 const FlattenableHelper<T> helper(val);
420 return write(helper);
421}
422
423template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700424status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700425 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700426 if (!val.isFixedSize()) {
427 status_t err = writeInt32(size);
428 if (err != NO_ERROR) {
429 return err;
430 }
431 }
Mathias Agopian20985172012-08-31 14:25:22 -0700432 if (size) {
433 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700434 if (buffer == NULL)
435 return NO_MEMORY;
436 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700437 }
438 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700439}
440
441template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700442status_t Parcel::read(Flattenable<T>& val) const {
443 FlattenableHelper<T> helper(val);
444 return read(helper);
445}
446
447template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700448status_t Parcel::read(LightFlattenable<T>& val) const {
449 size_t size;
450 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700451 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700452 } else {
453 int32_t s;
454 status_t err = readInt32(&s);
455 if (err != NO_ERROR) {
456 return err;
457 }
458 size = s;
459 }
Mathias Agopian20985172012-08-31 14:25:22 -0700460 if (size) {
461 void const* buffer = readInplace(size);
462 return buffer == NULL ? NO_MEMORY :
463 val.unflatten(buffer, size);
464 }
465 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700466}
467
Casey Dahlinf0c13772015-10-27 18:33:56 -0700468template<typename T>
469status_t Parcel::readStrongBinder(sp<T>* val) const {
470 sp<IBinder> tmp;
471 status_t ret = readStrongBinder(&tmp);
472
473 if (ret == OK) {
474 *val = interface_cast<T>(tmp);
475
476 if (val->get() == nullptr) {
477 return UNKNOWN_ERROR;
478 }
479 }
480
481 return ret;
482}
483
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800484template<typename T, typename U>
485status_t Parcel::unsafeReadTypedVector(
486 std::vector<T>* val, status_t(Parcel::*read_func)(U*) const) const {
487 val->clear();
488
489 int32_t size;
490 status_t status = this->readInt32(&size);
491
492 if (status != OK) {
493 return status;
494 }
495
496 if (size < 0) {
497 return UNEXPECTED_NULL;
498 }
499
500 val->resize(size);
501
502 for (auto& v: *val) {
503 status = (this->*read_func)(&v);
504
505 if (status != OK) {
506 return status;
507 }
508 }
509
510 return OK;
511}
512
513template<typename T>
514status_t Parcel::readTypedVector(std::vector<T>* val,
515 status_t(Parcel::*read_func)(T*) const) const {
516 return unsafeReadTypedVector(val, read_func);
517}
518
519template<typename T, typename U>
520status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
521 status_t(Parcel::*write_func)(U)) {
522 if (val.size() > std::numeric_limits<int32_t>::max()) {
523 return BAD_VALUE;
524 }
525
526 status_t status = this->writeInt32(val.size());
527
528 if (status != OK) {
529 return status;
530 }
531
532 for (const auto& item : val) {
533 status = (this->*write_func)(item);
534
535 if (status != OK) {
536 return status;
537 }
538 }
539
540 return OK;
541}
542
543template<typename T>
544status_t Parcel::writeTypedVector(const std::vector<T>& val,
545 status_t(Parcel::*write_func)(const T&)) {
546 return unsafeWriteTypedVector(val, write_func);
547}
548
549template<typename T>
550status_t Parcel::writeTypedVector(const std::vector<T>& val,
551 status_t(Parcel::*write_func)(T)) {
552 return unsafeWriteTypedVector(val, write_func);
553}
554
Christopher Wiley97f048d2015-11-19 06:49:05 -0800555template<typename T>
556status_t Parcel::readParcelableVector(std::vector<T>* val) const {
557 return unsafeReadTypedVector(val, &Parcel::readParcelable);
558}
559
560template<typename T>
561status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
562 return unsafeWriteTypedVector(val, &Parcel::writeParcelable);
563}
564
Mathias Agopian8683fca2012-08-12 19:37:16 -0700565// ---------------------------------------------------------------------------
566
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
568{
569 parcel.print(to);
570 return to;
571}
572
573// ---------------------------------------------------------------------------
574
575// Generic acquire and release of objects.
576void acquire_object(const sp<ProcessState>& proc,
577 const flat_binder_object& obj, const void* who);
578void release_object(const sp<ProcessState>& proc,
579 const flat_binder_object& obj, const void* who);
580
581void flatten_binder(const sp<ProcessState>& proc,
582 const sp<IBinder>& binder, flat_binder_object* out);
583void flatten_binder(const sp<ProcessState>& proc,
584 const wp<IBinder>& binder, flat_binder_object* out);
585status_t unflatten_binder(const sp<ProcessState>& proc,
586 const flat_binder_object& flat, sp<IBinder>* out);
587status_t unflatten_binder(const sp<ProcessState>& proc,
588 const flat_binder_object& flat, wp<IBinder>* out);
589
590}; // namespace android
591
592// ---------------------------------------------------------------------------
593
594#endif // ANDROID_PARCEL_H