blob: 0abf8f34837a0a2fb3bd8fdc6a8b9b98476751f4 [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>
Casey Dahlin06673e32015-11-23 13:24:23 -080023#include <nativehelper/ScopedFd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/Errors.h>
25#include <utils/RefBase.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070028#include <utils/Flattenable.h>
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080029#include <linux/binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Casey Dahlinf0c13772015-10-27 18:33:56 -070031#include <binder/IInterface.h>
Christopher Wiley97f048d2015-11-19 06:49:05 -080032#include <binder/Parcelable.h>
Casey Dahlinf0c13772015-10-27 18:33:56 -070033
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034// ---------------------------------------------------------------------------
35namespace android {
36
Mathias Agopiane1424282013-07-29 21:24:40 -070037template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070038template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070040class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041class ProcessState;
42class String8;
43class TextOutput;
44
Mathias Agopiane1424282013-07-29 21:24:40 -070045class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000046 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070048 class ReadableBlob;
49 class WritableBlob;
50
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 Parcel();
52 ~Parcel();
53
54 const uint8_t* data() const;
55 size_t dataSize() const;
56 size_t dataAvail() const;
57 size_t dataPosition() const;
58 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 status_t setDataSize(size_t size);
61 void setDataPosition(size_t pos) const;
62 status_t setDataCapacity(size_t size);
63
64 status_t setData(const uint8_t* buffer, size_t len);
65
Andreas Huber51faf462011-04-13 10:21:56 -070066 status_t appendFrom(const Parcel *parcel,
67 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Jeff Brown13b16042014-11-11 16:44:25 -080069 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070070 bool pushAllowFds(bool allowFds);
71 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 bool hasFileDescriptors() const;
74
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070075 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070078 // Parses the RPC header, returning true if the interface name
79 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070080 //
81 // Additionally, enforceInterface does part of the work of
82 // propagating the StrictMode policy mask, populating the current
83 // IPCThreadState, which as an optimization may optionally be
84 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070085 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070086 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070087 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 void freeData();
90
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080091private:
92 const binder_size_t* objects() const;
93
94public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 size_t objectsCount() const;
96
97 status_t errorCheck() const;
98 void setError(status_t err);
99
100 status_t write(const void* data, size_t len);
101 void* writeInplace(size_t len);
102 status_t writeUnpadded(const void* data, size_t len);
103 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800104 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700106 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 status_t writeFloat(float val);
108 status_t writeDouble(double val);
109 status_t writeCString(const char* str);
110 status_t writeString8(const String8& str);
111 status_t writeString16(const String16& str);
Casey Dahlinb9872622015-11-25 15:09:45 -0800112 status_t writeString16(const std::unique_ptr<String16>& str);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 status_t writeString16(const char16_t* str, size_t len);
114 status_t writeStrongBinder(const sp<IBinder>& val);
115 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700116 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700117 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700118 status_t writeBool(bool val);
119 status_t writeChar(char16_t val);
120 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700121
Casey Dahlinb9872622015-11-25 15:09:45 -0800122 status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700123 status_t writeByteVector(const std::vector<int8_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800124 status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700125 status_t writeInt32Vector(const std::vector<int32_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800126 status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700127 status_t writeInt64Vector(const std::vector<int64_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800128 status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700129 status_t writeFloatVector(const std::vector<float>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800130 status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700131 status_t writeDoubleVector(const std::vector<double>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800132 status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700133 status_t writeBoolVector(const std::vector<bool>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800134 status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700135 status_t writeCharVector(const std::vector<char16_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800136 status_t writeString16Vector(
137 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700138 status_t writeString16Vector(const std::vector<String16>& val);
139
Casey Dahlinb9872622015-11-25 15:09:45 -0800140 status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800141 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
142
Mathias Agopiane1424282013-07-29 21:24:40 -0700143 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800144 status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val);
145 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800146 status_t writeParcelableVector(const std::vector<T>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800147
148 template<typename T>
149 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable);
150
Christopher Wiley97f048d2015-11-19 06:49:05 -0800151 status_t writeParcelable(const Parcelable& parcelable);
152
153 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700154 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155
Mathias Agopian8683fca2012-08-12 19:37:16 -0700156 template<typename T>
157 status_t write(const LightFlattenable<T>& val);
158
159
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700160 // Place a native_handle into the parcel (the native_handle's file-
161 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700162 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700163 // Doesn't take ownership of the native_handle.
164 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
166 // Place a file descriptor into the parcel. The given fd must remain
167 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700168 // The Parcel does not take ownership of the given fd unless you ask it to.
169 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170
171 // Place a file descriptor into the parcel. A dup of the fd is made, which
172 // will be closed once the parcel is destroyed.
173 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700174
Casey Dahlin06673e32015-11-23 13:24:23 -0800175 // Place a file descriptor into the parcel. This will not affect the
176 // semantics of the smart file descriptor. A new descriptor will be
177 // created, and will be closed when the parcel is destroyed.
178 status_t writeUniqueFileDescriptor(
179 const ScopedFd& fd);
180
181 // Place a vector of file desciptors into the parcel. Each descriptor is
182 // dup'd as in writeDupFileDescriptor
183 status_t writeUniqueFileDescriptorVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800184 const std::unique_ptr<std::vector<ScopedFd>>& val);
185 status_t writeUniqueFileDescriptorVector(
Casey Dahlin06673e32015-11-23 13:24:23 -0800186 const std::vector<ScopedFd>& val);
187
Jeff Brown5707dbf2011-09-23 21:17:56 -0700188 // Writes a blob to the parcel.
189 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800190 // transferred by way of an anonymous shared memory region. Prefer sending
191 // immutable blobs if possible since they may be subsequently transferred between
192 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700193 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800194 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
195
196 // Write an existing immutable blob file descriptor to the parcel.
197 // This allows the client to send the same blob to multiple processes
198 // as long as it keeps a dup of the blob file descriptor handy for later.
199 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
202
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700203 // Like Parcel.java's writeNoException(). Just writes a zero int32.
204 // Currently the native implementation doesn't do any of the StrictMode
205 // stack gathering and serialization that the Java implementation does.
206 status_t writeNoException();
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 void remove(size_t start, size_t amt);
209
210 status_t read(void* outData, size_t len) const;
211 const void* readInplace(size_t len) const;
212 int32_t readInt32() const;
213 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800214 uint32_t readUint32() const;
215 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 int64_t readInt64() const;
217 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700218 uint64_t readUint64() const;
219 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 float readFloat() const;
221 status_t readFloat(float *pArg) const;
222 double readDouble() const;
223 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700224 intptr_t readIntPtr() const;
225 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700226 bool readBool() const;
227 status_t readBool(bool *pArg) const;
228 char16_t readChar() const;
229 status_t readChar(char16_t *pArg) const;
230 int8_t readByte() const;
231 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232
233 const char* readCString() const;
234 String8 readString8() const;
235 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700236 status_t readString16(String16* pArg) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800237 status_t readString16(std::unique_ptr<String16>* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 const char16_t* readString16Inplace(size_t* outLen) const;
239 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700240 status_t readStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700242
Casey Dahlinf0c13772015-10-27 18:33:56 -0700243 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800244 status_t readParcelableVector(
245 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
246 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800247 status_t readParcelableVector(std::vector<T>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800248
Christopher Wiley97f048d2015-11-19 06:49:05 -0800249 status_t readParcelable(Parcelable* parcelable) const;
250
251 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800252 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
253
254 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800255 status_t readStrongBinder(sp<T>* val) const;
256
Casey Dahlinb9872622015-11-25 15:09:45 -0800257 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800258 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700259
Casey Dahlinb9872622015-11-25 15:09:45 -0800260 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700261 status_t readByteVector(std::vector<int8_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800262 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700263 status_t readInt32Vector(std::vector<int32_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800264 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700265 status_t readInt64Vector(std::vector<int64_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800266 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700267 status_t readFloatVector(std::vector<float>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800268 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700269 status_t readDoubleVector(std::vector<double>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800270 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700271 status_t readBoolVector(std::vector<bool>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800272 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700273 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800274 status_t readString16Vector(
275 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700276 status_t readString16Vector(std::vector<String16>* val) const;
277
Mathias Agopiane1424282013-07-29 21:24:40 -0700278 template<typename T>
279 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700280
Mathias Agopian8683fca2012-08-12 19:37:16 -0700281 template<typename T>
282 status_t read(LightFlattenable<T>& val) const;
283
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700284 // Like Parcel.java's readExceptionCode(). Reads the first int32
285 // off of a Parcel's header, returning 0 or the negative error
286 // code on exceptions, but also deals with skipping over rich
287 // response headers. Callers should use this to read & parse the
288 // response headers rather than doing it by hand.
289 int32_t readExceptionCode() const;
290
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700291 // Retrieve native_handle from the parcel. This returns a copy of the
292 // parcel's native_handle (the caller takes ownership). The caller
293 // must free the native_handle with native_handle_close() and
294 // native_handle_delete().
295 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
297
298 // Retrieve a file descriptor from the parcel. This returns the raw fd
299 // in the parcel, which you do not own -- use dup() to get your own copy.
300 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700301
Casey Dahlin06673e32015-11-23 13:24:23 -0800302 // Retrieve a smart file descriptor from the parcel.
303 status_t readUniqueFileDescriptor(
304 ScopedFd* val) const;
305
306
307 // Retrieve a vector of smart file descriptors from the parcel.
308 status_t readUniqueFileDescriptorVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800309 std::unique_ptr<std::vector<ScopedFd>>* val) const;
310 status_t readUniqueFileDescriptorVector(
Casey Dahlin06673e32015-11-23 13:24:23 -0800311 std::vector<ScopedFd>* val) const;
312
Jeff Brown5707dbf2011-09-23 21:17:56 -0700313 // Reads a blob from the parcel.
314 // The caller should call release() on the blob after reading its contents.
315 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 const flat_binder_object* readObject(bool nullMetaData) const;
318
319 // Explicitly close all file descriptors in the parcel.
320 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800321
322 // Debugging: get metrics on current allocations.
323 static size_t getGlobalAllocSize();
324 static size_t getGlobalAllocCount();
325
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800326private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 typedef void (*release_func)(Parcel* parcel,
328 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 void* cookie);
331
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800332 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 size_t ipcObjectsCount() const;
336 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800337 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 release_func relFunc, void* relCookie);
339
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800340public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343private:
344 Parcel(const Parcel& o);
345 Parcel& operator=(const Parcel& o);
346
347 status_t finishWrite(size_t len);
348 void releaseObjects();
349 void acquireObjects();
350 status_t growData(size_t len);
351 status_t restartWrite(size_t desired);
352 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000353 status_t writePointer(uintptr_t val);
354 status_t readPointer(uintptr_t *pArg) const;
355 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 void freeDataNoInit();
357 void initState();
358 void scanForFds() const;
359
Andreas Huber84a6d042009-08-17 13:33:27 -0700360 template<class T>
361 status_t readAligned(T *pArg) const;
362
363 template<class T> T readAligned() const;
364
365 template<class T>
366 status_t writeAligned(T val);
367
Casey Dahlinb9872622015-11-25 15:09:45 -0800368 status_t writeRawNullableParcelable(const Parcelable*
369 parcelable);
370
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800371 template<typename T, typename U>
372 status_t unsafeReadTypedVector(std::vector<T>* val,
373 status_t(Parcel::*read_func)(U*) const) const;
374 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800375 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
376 status_t(Parcel::*read_func)(T*) const) const;
377 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800378 status_t readTypedVector(std::vector<T>* val,
379 status_t(Parcel::*read_func)(T*) const) const;
380 template<typename T, typename U>
381 status_t unsafeWriteTypedVector(const std::vector<T>& val,
382 status_t(Parcel::*write_func)(U));
383 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800384 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
385 status_t(Parcel::*write_func)(const T&));
386 template<typename T>
387 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
388 status_t(Parcel::*write_func)(T));
389 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800390 status_t writeTypedVector(const std::vector<T>& val,
391 status_t(Parcel::*write_func)(const T&));
392 template<typename T>
393 status_t writeTypedVector(const std::vector<T>& val,
394 status_t(Parcel::*write_func)(T));
395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 status_t mError;
397 uint8_t* mData;
398 size_t mDataSize;
399 size_t mDataCapacity;
400 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800401 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 size_t mObjectsSize;
403 size_t mObjectsCapacity;
404 mutable size_t mNextObjectHint;
405
406 mutable bool mFdsKnown;
407 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400408 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409
410 release_func mOwner;
411 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700412
413 class Blob {
414 public:
415 Blob();
416 ~Blob();
417
Jeff Brown13b16042014-11-11 16:44:25 -0800418 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700419 void release();
420 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800421 inline int fd() const { return mFd; };
422 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700423
424 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800425 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700426
Jeff Brown13b16042014-11-11 16:44:25 -0800427 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700428 void* mData;
429 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800430 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700431 };
432
Mathias Agopiane1424282013-07-29 21:24:40 -0700433 class FlattenableHelperInterface {
434 protected:
435 ~FlattenableHelperInterface() { }
436 public:
437 virtual size_t getFlattenedSize() const = 0;
438 virtual size_t getFdCount() const = 0;
439 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
440 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
441 };
442
443 template<typename T>
444 class FlattenableHelper : public FlattenableHelperInterface {
445 friend class Parcel;
446 const Flattenable<T>& val;
447 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
448
449 public:
450 virtual size_t getFlattenedSize() const {
451 return val.getFlattenedSize();
452 }
453 virtual size_t getFdCount() const {
454 return val.getFdCount();
455 }
456 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
457 return val.flatten(buffer, size, fds, count);
458 }
459 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
460 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
461 }
462 };
463 status_t write(const FlattenableHelperInterface& val);
464 status_t read(FlattenableHelperInterface& val) const;
465
Jeff Brown5707dbf2011-09-23 21:17:56 -0700466public:
467 class ReadableBlob : public Blob {
468 friend class Parcel;
469 public:
470 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800471 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700472 };
473
474 class WritableBlob : public Blob {
475 friend class Parcel;
476 public:
477 inline void* data() { return mData; }
478 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400479
480private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700481 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400482
483public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700484 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400485 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700486 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487};
488
489// ---------------------------------------------------------------------------
490
Mathias Agopian8683fca2012-08-12 19:37:16 -0700491template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700492status_t Parcel::write(const Flattenable<T>& val) {
493 const FlattenableHelper<T> helper(val);
494 return write(helper);
495}
496
497template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700498status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700499 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700500 if (!val.isFixedSize()) {
501 status_t err = writeInt32(size);
502 if (err != NO_ERROR) {
503 return err;
504 }
505 }
Mathias Agopian20985172012-08-31 14:25:22 -0700506 if (size) {
507 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700508 if (buffer == NULL)
509 return NO_MEMORY;
510 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700511 }
512 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700513}
514
515template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700516status_t Parcel::read(Flattenable<T>& val) const {
517 FlattenableHelper<T> helper(val);
518 return read(helper);
519}
520
521template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700522status_t Parcel::read(LightFlattenable<T>& val) const {
523 size_t size;
524 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700525 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700526 } else {
527 int32_t s;
528 status_t err = readInt32(&s);
529 if (err != NO_ERROR) {
530 return err;
531 }
532 size = s;
533 }
Mathias Agopian20985172012-08-31 14:25:22 -0700534 if (size) {
535 void const* buffer = readInplace(size);
536 return buffer == NULL ? NO_MEMORY :
537 val.unflatten(buffer, size);
538 }
539 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700540}
541
Casey Dahlinf0c13772015-10-27 18:33:56 -0700542template<typename T>
543status_t Parcel::readStrongBinder(sp<T>* val) const {
544 sp<IBinder> tmp;
545 status_t ret = readStrongBinder(&tmp);
546
547 if (ret == OK) {
548 *val = interface_cast<T>(tmp);
549
550 if (val->get() == nullptr) {
551 return UNKNOWN_ERROR;
552 }
553 }
554
555 return ret;
556}
557
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800558template<typename T, typename U>
559status_t Parcel::unsafeReadTypedVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800560 std::vector<T>* val,
561 status_t(Parcel::*read_func)(U*) const) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800562 int32_t size;
563 status_t status = this->readInt32(&size);
564
565 if (status != OK) {
566 return status;
567 }
568
569 if (size < 0) {
570 return UNEXPECTED_NULL;
571 }
572
573 val->resize(size);
574
575 for (auto& v: *val) {
576 status = (this->*read_func)(&v);
577
578 if (status != OK) {
579 return status;
580 }
581 }
582
583 return OK;
584}
585
586template<typename T>
587status_t Parcel::readTypedVector(std::vector<T>* val,
588 status_t(Parcel::*read_func)(T*) const) const {
589 return unsafeReadTypedVector(val, read_func);
590}
591
Casey Dahlinb9872622015-11-25 15:09:45 -0800592template<typename T>
593status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
594 status_t(Parcel::*read_func)(T*) const) const {
595 const int32_t start = dataPosition();
596 int32_t size;
597 status_t status = readInt32(&size);
598 val->reset();
599
600 if (status != OK || size < 0) {
601 return status;
602 }
603
604 setDataPosition(start);
605 val->reset(new std::vector<T>());
606
607 status = unsafeReadTypedVector(val->get(), read_func);
608
609 if (status != OK) {
610 val->reset();
611 }
612
613 return status;
614}
615
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800616template<typename T, typename U>
617status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
618 status_t(Parcel::*write_func)(U)) {
619 if (val.size() > std::numeric_limits<int32_t>::max()) {
620 return BAD_VALUE;
621 }
622
623 status_t status = this->writeInt32(val.size());
624
625 if (status != OK) {
626 return status;
627 }
628
629 for (const auto& item : val) {
630 status = (this->*write_func)(item);
631
632 if (status != OK) {
633 return status;
634 }
635 }
636
637 return OK;
638}
639
640template<typename T>
641status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800642 status_t(Parcel::*write_func)(const T&)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800643 return unsafeWriteTypedVector(val, write_func);
644}
645
646template<typename T>
647status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800648 status_t(Parcel::*write_func)(T)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800649 return unsafeWriteTypedVector(val, write_func);
650}
651
Christopher Wiley97f048d2015-11-19 06:49:05 -0800652template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800653status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
654 status_t(Parcel::*write_func)(const T&)) {
655 if (val.get() == nullptr) {
656 return this->writeInt32(-1);
657 }
658
659 return unsafeWriteTypedVector(*val, write_func);
660}
661
662template<typename T>
663status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
664 status_t(Parcel::*write_func)(T)) {
665 if (val.get() == nullptr) {
666 return this->writeInt32(-1);
667 }
668
669 return unsafeWriteTypedVector(*val, write_func);
670}
671
672template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800673status_t Parcel::readParcelableVector(std::vector<T>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -0800674 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
675}
676
677template<typename T>
678status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
679 const int32_t start = dataPosition();
680 int32_t size;
681 status_t status = readInt32(&size);
682 val->reset();
683
684 if (status != OK || size < 0) {
685 return status;
686 }
687
688 setDataPosition(start);
689 val->reset(new std::vector<T>());
690
691 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable);
692
693 if (status != OK) {
694 val->reset();
695 }
696
697 return status;
698}
699
700template<typename T>
701status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
702 const int32_t start = dataPosition();
703 int32_t present;
704 status_t status = readInt32(&present);
705 parcelable->reset();
706
707 if (status != OK || !present) {
708 return status;
709 }
710
711 setDataPosition(start);
712 parcelable->reset(new T());
713
714 status = readParcelable(parcelable->get());
715
716 if (status != OK) {
717 parcelable->reset();
718 }
719
720 return status;
721}
722
723template<typename T>
724status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
725 return writeRawNullableParcelable(parcelable.get());
Christopher Wiley97f048d2015-11-19 06:49:05 -0800726}
727
728template<typename T>
729status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -0800730 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
731}
732
733template<typename T>
734status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
735 if (val.get() == nullptr) {
736 return this->writeInt32(-1);
737 }
738
739 return unsafeWriteTypedVector(*val, &Parcel::writeParcelable);
Christopher Wiley97f048d2015-11-19 06:49:05 -0800740}
741
Mathias Agopian8683fca2012-08-12 19:37:16 -0700742// ---------------------------------------------------------------------------
743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
745{
746 parcel.print(to);
747 return to;
748}
749
750// ---------------------------------------------------------------------------
751
752// Generic acquire and release of objects.
753void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000754 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755void release_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000756 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757
758void flatten_binder(const sp<ProcessState>& proc,
759 const sp<IBinder>& binder, flat_binder_object* out);
760void flatten_binder(const sp<ProcessState>& proc,
761 const wp<IBinder>& binder, flat_binder_object* out);
762status_t unflatten_binder(const sp<ProcessState>& proc,
763 const flat_binder_object& flat, sp<IBinder>* out);
764status_t unflatten_binder(const sp<ProcessState>& proc,
765 const flat_binder_object& flat, wp<IBinder>* out);
766
767}; // namespace android
768
769// ---------------------------------------------------------------------------
770
771#endif // ANDROID_PARCEL_H