blob: 277f1566009775936228d5e998553547f55aefd3 [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
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800128 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
129
Mathias Agopiane1424282013-07-29 21:24:40 -0700130 template<typename T>
131 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132
Mathias Agopian8683fca2012-08-12 19:37:16 -0700133 template<typename T>
134 status_t write(const LightFlattenable<T>& val);
135
136
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700137 // Place a native_handle into the parcel (the native_handle's file-
138 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700139 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700140 // Doesn't take ownership of the native_handle.
141 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142
143 // Place a file descriptor into the parcel. The given fd must remain
144 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700145 // The Parcel does not take ownership of the given fd unless you ask it to.
146 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147
148 // Place a file descriptor into the parcel. A dup of the fd is made, which
149 // will be closed once the parcel is destroyed.
150 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700151
152 // Writes a blob to the parcel.
153 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800154 // transferred by way of an anonymous shared memory region. Prefer sending
155 // immutable blobs if possible since they may be subsequently transferred between
156 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700157 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800158 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
159
160 // Write an existing immutable blob file descriptor to the parcel.
161 // This allows the client to send the same blob to multiple processes
162 // as long as it keeps a dup of the blob file descriptor handy for later.
163 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
166
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700167 // Like Parcel.java's writeNoException(). Just writes a zero int32.
168 // Currently the native implementation doesn't do any of the StrictMode
169 // stack gathering and serialization that the Java implementation does.
170 status_t writeNoException();
171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 void remove(size_t start, size_t amt);
173
174 status_t read(void* outData, size_t len) const;
175 const void* readInplace(size_t len) const;
176 int32_t readInt32() const;
177 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800178 uint32_t readUint32() const;
179 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 int64_t readInt64() const;
181 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700182 uint64_t readUint64() const;
183 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 float readFloat() const;
185 status_t readFloat(float *pArg) const;
186 double readDouble() const;
187 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700188 intptr_t readIntPtr() const;
189 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700190 bool readBool() const;
191 status_t readBool(bool *pArg) const;
192 char16_t readChar() const;
193 status_t readChar(char16_t *pArg) const;
194 int8_t readByte() const;
195 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196
197 const char* readCString() const;
198 String8 readString8() const;
199 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700200 status_t readString16(String16* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 const char16_t* readString16Inplace(size_t* outLen) const;
202 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700203 status_t readStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700205
Casey Dahlinf0c13772015-10-27 18:33:56 -0700206 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800207 status_t readStrongBinder(sp<T>* val) const;
208
209 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700210
Casey Dahlin451ff582015-10-19 18:12:18 -0700211 status_t readByteVector(std::vector<int8_t>* val) const;
212 status_t readInt32Vector(std::vector<int32_t>* val) const;
213 status_t readInt64Vector(std::vector<int64_t>* val) const;
214 status_t readFloatVector(std::vector<float>* val) const;
215 status_t readDoubleVector(std::vector<double>* val) const;
216 status_t readBoolVector(std::vector<bool>* val) const;
217 status_t readCharVector(std::vector<char16_t>* val) const;
218 status_t readString16Vector(std::vector<String16>* val) const;
219
Mathias Agopiane1424282013-07-29 21:24:40 -0700220 template<typename T>
221 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700222
Mathias Agopian8683fca2012-08-12 19:37:16 -0700223 template<typename T>
224 status_t read(LightFlattenable<T>& val) const;
225
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700226 // Like Parcel.java's readExceptionCode(). Reads the first int32
227 // off of a Parcel's header, returning 0 or the negative error
228 // code on exceptions, but also deals with skipping over rich
229 // response headers. Callers should use this to read & parse the
230 // response headers rather than doing it by hand.
231 int32_t readExceptionCode() const;
232
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700233 // Retrieve native_handle from the parcel. This returns a copy of the
234 // parcel's native_handle (the caller takes ownership). The caller
235 // must free the native_handle with native_handle_close() and
236 // native_handle_delete().
237 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238
239
240 // Retrieve a file descriptor from the parcel. This returns the raw fd
241 // in the parcel, which you do not own -- use dup() to get your own copy.
242 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700243
244 // Reads a blob from the parcel.
245 // The caller should call release() on the blob after reading its contents.
246 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
247
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 const flat_binder_object* readObject(bool nullMetaData) const;
249
250 // Explicitly close all file descriptors in the parcel.
251 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800252
253 // Debugging: get metrics on current allocations.
254 static size_t getGlobalAllocSize();
255 static size_t getGlobalAllocCount();
256
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800257private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 typedef void (*release_func)(Parcel* parcel,
259 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800260 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 void* cookie);
262
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800263 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 size_t ipcObjectsCount() const;
267 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800268 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 release_func relFunc, void* relCookie);
270
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274private:
275 Parcel(const Parcel& o);
276 Parcel& operator=(const Parcel& o);
277
278 status_t finishWrite(size_t len);
279 void releaseObjects();
280 void acquireObjects();
281 status_t growData(size_t len);
282 status_t restartWrite(size_t desired);
283 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000284 status_t writePointer(uintptr_t val);
285 status_t readPointer(uintptr_t *pArg) const;
286 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 void freeDataNoInit();
288 void initState();
289 void scanForFds() const;
290
Andreas Huber84a6d042009-08-17 13:33:27 -0700291 template<class T>
292 status_t readAligned(T *pArg) const;
293
294 template<class T> T readAligned() const;
295
296 template<class T>
297 status_t writeAligned(T val);
298
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800299 template<typename T, typename U>
300 status_t unsafeReadTypedVector(std::vector<T>* val,
301 status_t(Parcel::*read_func)(U*) const) const;
302 template<typename T>
303 status_t readTypedVector(std::vector<T>* val,
304 status_t(Parcel::*read_func)(T*) const) const;
305 template<typename T, typename U>
306 status_t unsafeWriteTypedVector(const std::vector<T>& val,
307 status_t(Parcel::*write_func)(U));
308 template<typename T>
309 status_t writeTypedVector(const std::vector<T>& val,
310 status_t(Parcel::*write_func)(const T&));
311 template<typename T>
312 status_t writeTypedVector(const std::vector<T>& val,
313 status_t(Parcel::*write_func)(T));
314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 status_t mError;
316 uint8_t* mData;
317 size_t mDataSize;
318 size_t mDataCapacity;
319 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800320 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 size_t mObjectsSize;
322 size_t mObjectsCapacity;
323 mutable size_t mNextObjectHint;
324
325 mutable bool mFdsKnown;
326 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400327 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328
329 release_func mOwner;
330 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700331
332 class Blob {
333 public:
334 Blob();
335 ~Blob();
336
Jeff Brown13b16042014-11-11 16:44:25 -0800337 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700338 void release();
339 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800340 inline int fd() const { return mFd; };
341 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700342
343 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800344 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700345
Jeff Brown13b16042014-11-11 16:44:25 -0800346 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700347 void* mData;
348 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800349 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700350 };
351
Mathias Agopiane1424282013-07-29 21:24:40 -0700352 class FlattenableHelperInterface {
353 protected:
354 ~FlattenableHelperInterface() { }
355 public:
356 virtual size_t getFlattenedSize() const = 0;
357 virtual size_t getFdCount() const = 0;
358 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
359 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
360 };
361
362 template<typename T>
363 class FlattenableHelper : public FlattenableHelperInterface {
364 friend class Parcel;
365 const Flattenable<T>& val;
366 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
367
368 public:
369 virtual size_t getFlattenedSize() const {
370 return val.getFlattenedSize();
371 }
372 virtual size_t getFdCount() const {
373 return val.getFdCount();
374 }
375 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
376 return val.flatten(buffer, size, fds, count);
377 }
378 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
379 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
380 }
381 };
382 status_t write(const FlattenableHelperInterface& val);
383 status_t read(FlattenableHelperInterface& val) const;
384
Jeff Brown5707dbf2011-09-23 21:17:56 -0700385public:
386 class ReadableBlob : public Blob {
387 friend class Parcel;
388 public:
389 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800390 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700391 };
392
393 class WritableBlob : public Blob {
394 friend class Parcel;
395 public:
396 inline void* data() { return mData; }
397 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400398
399private:
400 size_t mBlobAshmemSize;
401
402public:
403 size_t getBlobAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404};
405
406// ---------------------------------------------------------------------------
407
Mathias Agopian8683fca2012-08-12 19:37:16 -0700408template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700409status_t Parcel::write(const Flattenable<T>& val) {
410 const FlattenableHelper<T> helper(val);
411 return write(helper);
412}
413
414template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700415status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700416 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700417 if (!val.isFixedSize()) {
418 status_t err = writeInt32(size);
419 if (err != NO_ERROR) {
420 return err;
421 }
422 }
Mathias Agopian20985172012-08-31 14:25:22 -0700423 if (size) {
424 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700425 if (buffer == NULL)
426 return NO_MEMORY;
427 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700428 }
429 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700430}
431
432template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700433status_t Parcel::read(Flattenable<T>& val) const {
434 FlattenableHelper<T> helper(val);
435 return read(helper);
436}
437
438template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700439status_t Parcel::read(LightFlattenable<T>& val) const {
440 size_t size;
441 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700442 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700443 } else {
444 int32_t s;
445 status_t err = readInt32(&s);
446 if (err != NO_ERROR) {
447 return err;
448 }
449 size = s;
450 }
Mathias Agopian20985172012-08-31 14:25:22 -0700451 if (size) {
452 void const* buffer = readInplace(size);
453 return buffer == NULL ? NO_MEMORY :
454 val.unflatten(buffer, size);
455 }
456 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700457}
458
Casey Dahlinf0c13772015-10-27 18:33:56 -0700459template<typename T>
460status_t Parcel::readStrongBinder(sp<T>* val) const {
461 sp<IBinder> tmp;
462 status_t ret = readStrongBinder(&tmp);
463
464 if (ret == OK) {
465 *val = interface_cast<T>(tmp);
466
467 if (val->get() == nullptr) {
468 return UNKNOWN_ERROR;
469 }
470 }
471
472 return ret;
473}
474
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800475template<typename T, typename U>
476status_t Parcel::unsafeReadTypedVector(
477 std::vector<T>* val, status_t(Parcel::*read_func)(U*) const) const {
478 val->clear();
479
480 int32_t size;
481 status_t status = this->readInt32(&size);
482
483 if (status != OK) {
484 return status;
485 }
486
487 if (size < 0) {
488 return UNEXPECTED_NULL;
489 }
490
491 val->resize(size);
492
493 for (auto& v: *val) {
494 status = (this->*read_func)(&v);
495
496 if (status != OK) {
497 return status;
498 }
499 }
500
501 return OK;
502}
503
504template<typename T>
505status_t Parcel::readTypedVector(std::vector<T>* val,
506 status_t(Parcel::*read_func)(T*) const) const {
507 return unsafeReadTypedVector(val, read_func);
508}
509
510template<typename T, typename U>
511status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
512 status_t(Parcel::*write_func)(U)) {
513 if (val.size() > std::numeric_limits<int32_t>::max()) {
514 return BAD_VALUE;
515 }
516
517 status_t status = this->writeInt32(val.size());
518
519 if (status != OK) {
520 return status;
521 }
522
523 for (const auto& item : val) {
524 status = (this->*write_func)(item);
525
526 if (status != OK) {
527 return status;
528 }
529 }
530
531 return OK;
532}
533
534template<typename T>
535status_t Parcel::writeTypedVector(const std::vector<T>& val,
536 status_t(Parcel::*write_func)(const T&)) {
537 return unsafeWriteTypedVector(val, write_func);
538}
539
540template<typename T>
541status_t Parcel::writeTypedVector(const std::vector<T>& val,
542 status_t(Parcel::*write_func)(T)) {
543 return unsafeWriteTypedVector(val, write_func);
544}
545
Mathias Agopian8683fca2012-08-12 19:37:16 -0700546// ---------------------------------------------------------------------------
547
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
549{
550 parcel.print(to);
551 return to;
552}
553
554// ---------------------------------------------------------------------------
555
556// Generic acquire and release of objects.
557void acquire_object(const sp<ProcessState>& proc,
558 const flat_binder_object& obj, const void* who);
559void release_object(const sp<ProcessState>& proc,
560 const flat_binder_object& obj, const void* who);
561
562void flatten_binder(const sp<ProcessState>& proc,
563 const sp<IBinder>& binder, flat_binder_object* out);
564void flatten_binder(const sp<ProcessState>& proc,
565 const wp<IBinder>& binder, flat_binder_object* out);
566status_t unflatten_binder(const sp<ProcessState>& proc,
567 const flat_binder_object& flat, sp<IBinder>* out);
568status_t unflatten_binder(const sp<ProcessState>& proc,
569 const flat_binder_object& flat, wp<IBinder>* out);
570
571}; // namespace android
572
573// ---------------------------------------------------------------------------
574
575#endif // ANDROID_PARCEL_H