blob: 2685bcc8071c80b2514c7ec28f3b2b8993b59f87 [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
Christopher Wiley9a5e32f2016-01-28 16:56:53 -080020#include <string>
Casey Dahlin451ff582015-10-19 18:12:18 -070021#include <vector>
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <cutils/native_handle.h>
Casey Dahlin06673e32015-11-23 13:24:23 -080024#include <nativehelper/ScopedFd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Errors.h>
26#include <utils/RefBase.h>
27#include <utils/String16.h>
28#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070029#include <utils/Flattenable.h>
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080030#include <linux/binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Casey Dahlinf0c13772015-10-27 18:33:56 -070032#include <binder/IInterface.h>
Christopher Wiley97f048d2015-11-19 06:49:05 -080033#include <binder/Parcelable.h>
Casey Dahlinf0c13772015-10-27 18:33:56 -070034
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035// ---------------------------------------------------------------------------
36namespace android {
37
Mathias Agopiane1424282013-07-29 21:24:40 -070038template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070039template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070041class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042class ProcessState;
43class String8;
44class TextOutput;
45
Mathias Agopiane1424282013-07-29 21:24:40 -070046class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000047 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070049 class ReadableBlob;
50 class WritableBlob;
51
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 Parcel();
53 ~Parcel();
54
55 const uint8_t* data() const;
56 size_t dataSize() const;
57 size_t dataAvail() const;
58 size_t dataPosition() const;
59 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 status_t setDataSize(size_t size);
62 void setDataPosition(size_t pos) const;
63 status_t setDataCapacity(size_t size);
64
65 status_t setData(const uint8_t* buffer, size_t len);
66
Andreas Huber51faf462011-04-13 10:21:56 -070067 status_t appendFrom(const Parcel *parcel,
68 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Jeff Brown13b16042014-11-11 16:44:25 -080070 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070071 bool pushAllowFds(bool allowFds);
72 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 bool hasFileDescriptors() const;
75
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070076 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070079 // Parses the RPC header, returning true if the interface name
80 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070081 //
82 // Additionally, enforceInterface does part of the work of
83 // propagating the StrictMode policy mask, populating the current
84 // IPCThreadState, which as an optimization may optionally be
85 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070086 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070087 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070088 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 void freeData();
91
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080092private:
93 const binder_size_t* objects() const;
94
95public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 size_t objectsCount() const;
97
98 status_t errorCheck() const;
99 void setError(status_t err);
100
101 status_t write(const void* data, size_t len);
102 void* writeInplace(size_t len);
103 status_t writeUnpadded(const void* data, size_t len);
104 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800105 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700107 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 status_t writeFloat(float val);
109 status_t writeDouble(double val);
110 status_t writeCString(const char* str);
111 status_t writeString8(const String8& str);
112 status_t writeString16(const String16& str);
Casey Dahlinb9872622015-11-25 15:09:45 -0800113 status_t writeString16(const std::unique_ptr<String16>& str);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 status_t writeString16(const char16_t* str, size_t len);
115 status_t writeStrongBinder(const sp<IBinder>& val);
116 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700117 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700118 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700119 status_t writeBool(bool val);
120 status_t writeChar(char16_t val);
121 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700122
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800123 // Take a UTF8 encoded string, convert to UTF16, write it to the parcel.
124 status_t writeUtf8AsUtf16(const std::string& str);
125 status_t writeUtf8AsUtf16(const std::unique_ptr<std::string>& str);
126
Casey Dahlinb9872622015-11-25 15:09:45 -0800127 status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700128 status_t writeByteVector(const std::vector<int8_t>& val);
Casey Dahlin0b1cf332016-02-09 11:08:35 -0800129 status_t writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val);
130 status_t writeByteVector(const std::vector<uint8_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800131 status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700132 status_t writeInt32Vector(const std::vector<int32_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800133 status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700134 status_t writeInt64Vector(const std::vector<int64_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800135 status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700136 status_t writeFloatVector(const std::vector<float>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800137 status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700138 status_t writeDoubleVector(const std::vector<double>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800139 status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700140 status_t writeBoolVector(const std::vector<bool>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800141 status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700142 status_t writeCharVector(const std::vector<char16_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800143 status_t writeString16Vector(
144 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700145 status_t writeString16Vector(const std::vector<String16>& val);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800146 status_t writeUtf8VectorAsUtf16Vector(
147 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val);
148 status_t writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700149
Casey Dahlinb9872622015-11-25 15:09:45 -0800150 status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800151 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
152
Mathias Agopiane1424282013-07-29 21:24:40 -0700153 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800154 status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val);
155 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800156 status_t writeParcelableVector(const std::vector<T>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800157
158 template<typename T>
159 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable);
160
Christopher Wiley97f048d2015-11-19 06:49:05 -0800161 status_t writeParcelable(const Parcelable& parcelable);
162
163 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700164 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
Mathias Agopian8683fca2012-08-12 19:37:16 -0700166 template<typename T>
167 status_t write(const LightFlattenable<T>& val);
168
169
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700170 // Place a native_handle into the parcel (the native_handle's file-
171 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700172 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700173 // Doesn't take ownership of the native_handle.
174 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175
176 // Place a file descriptor into the parcel. The given fd must remain
177 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700178 // The Parcel does not take ownership of the given fd unless you ask it to.
179 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180
181 // Place a file descriptor into the parcel. A dup of the fd is made, which
182 // will be closed once the parcel is destroyed.
183 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700184
Casey Dahlin06673e32015-11-23 13:24:23 -0800185 // Place a file descriptor into the parcel. This will not affect the
186 // semantics of the smart file descriptor. A new descriptor will be
187 // created, and will be closed when the parcel is destroyed.
188 status_t writeUniqueFileDescriptor(
189 const ScopedFd& fd);
190
191 // Place a vector of file desciptors into the parcel. Each descriptor is
192 // dup'd as in writeDupFileDescriptor
193 status_t writeUniqueFileDescriptorVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800194 const std::unique_ptr<std::vector<ScopedFd>>& val);
195 status_t writeUniqueFileDescriptorVector(
Casey Dahlin06673e32015-11-23 13:24:23 -0800196 const std::vector<ScopedFd>& val);
197
Jeff Brown5707dbf2011-09-23 21:17:56 -0700198 // Writes a blob to the parcel.
199 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800200 // transferred by way of an anonymous shared memory region. Prefer sending
201 // immutable blobs if possible since they may be subsequently transferred between
202 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700203 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800204 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
205
206 // Write an existing immutable blob file descriptor to the parcel.
207 // This allows the client to send the same blob to multiple processes
208 // as long as it keeps a dup of the blob file descriptor handy for later.
209 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
212
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700213 // Like Parcel.java's writeNoException(). Just writes a zero int32.
214 // Currently the native implementation doesn't do any of the StrictMode
215 // stack gathering and serialization that the Java implementation does.
216 status_t writeNoException();
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 void remove(size_t start, size_t amt);
219
220 status_t read(void* outData, size_t len) const;
221 const void* readInplace(size_t len) const;
222 int32_t readInt32() const;
223 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800224 uint32_t readUint32() const;
225 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 int64_t readInt64() const;
227 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700228 uint64_t readUint64() const;
229 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 float readFloat() const;
231 status_t readFloat(float *pArg) const;
232 double readDouble() const;
233 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700234 intptr_t readIntPtr() const;
235 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700236 bool readBool() const;
237 status_t readBool(bool *pArg) const;
238 char16_t readChar() const;
239 status_t readChar(char16_t *pArg) const;
240 int8_t readByte() const;
241 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800243 // Read a UTF16 encoded string, convert to UTF8
244 status_t readUtf8FromUtf16(std::string* str) const;
245 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 const char* readCString() const;
248 String8 readString8() const;
249 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700250 status_t readString16(String16* pArg) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800251 status_t readString16(std::unique_ptr<String16>* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 const char16_t* readString16Inplace(size_t* outLen) const;
253 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700254 status_t readStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700256
Casey Dahlinf0c13772015-10-27 18:33:56 -0700257 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800258 status_t readParcelableVector(
259 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
260 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800261 status_t readParcelableVector(std::vector<T>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800262
Christopher Wiley97f048d2015-11-19 06:49:05 -0800263 status_t readParcelable(Parcelable* parcelable) const;
264
265 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800266 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
267
268 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800269 status_t readStrongBinder(sp<T>* val) const;
270
Casey Dahlinb9872622015-11-25 15:09:45 -0800271 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800272 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700273
Casey Dahlinb9872622015-11-25 15:09:45 -0800274 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700275 status_t readByteVector(std::vector<int8_t>* val) const;
Casey Dahlin0b1cf332016-02-09 11:08:35 -0800276 status_t readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const;
277 status_t readByteVector(std::vector<uint8_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800278 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700279 status_t readInt32Vector(std::vector<int32_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800280 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700281 status_t readInt64Vector(std::vector<int64_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800282 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700283 status_t readFloatVector(std::vector<float>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800284 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700285 status_t readDoubleVector(std::vector<double>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800286 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700287 status_t readBoolVector(std::vector<bool>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800288 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700289 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800290 status_t readString16Vector(
291 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700292 status_t readString16Vector(std::vector<String16>* val) const;
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800293 status_t readUtf8VectorFromUtf16Vector(
294 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
295 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700296
Mathias Agopiane1424282013-07-29 21:24:40 -0700297 template<typename T>
298 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700299
Mathias Agopian8683fca2012-08-12 19:37:16 -0700300 template<typename T>
301 status_t read(LightFlattenable<T>& val) const;
302
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700303 // Like Parcel.java's readExceptionCode(). Reads the first int32
304 // off of a Parcel's header, returning 0 or the negative error
305 // code on exceptions, but also deals with skipping over rich
306 // response headers. Callers should use this to read & parse the
307 // response headers rather than doing it by hand.
308 int32_t readExceptionCode() const;
309
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700310 // Retrieve native_handle from the parcel. This returns a copy of the
311 // parcel's native_handle (the caller takes ownership). The caller
312 // must free the native_handle with native_handle_close() and
313 // native_handle_delete().
314 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316
317 // Retrieve a file descriptor from the parcel. This returns the raw fd
318 // in the parcel, which you do not own -- use dup() to get your own copy.
319 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700320
Casey Dahlin06673e32015-11-23 13:24:23 -0800321 // Retrieve a smart file descriptor from the parcel.
322 status_t readUniqueFileDescriptor(
323 ScopedFd* val) const;
324
325
326 // Retrieve a vector of smart file descriptors from the parcel.
327 status_t readUniqueFileDescriptorVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800328 std::unique_ptr<std::vector<ScopedFd>>* val) const;
329 status_t readUniqueFileDescriptorVector(
Casey Dahlin06673e32015-11-23 13:24:23 -0800330 std::vector<ScopedFd>* val) const;
331
Jeff Brown5707dbf2011-09-23 21:17:56 -0700332 // Reads a blob from the parcel.
333 // The caller should call release() on the blob after reading its contents.
334 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
335
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 const flat_binder_object* readObject(bool nullMetaData) const;
337
338 // Explicitly close all file descriptors in the parcel.
339 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800340
341 // Debugging: get metrics on current allocations.
342 static size_t getGlobalAllocSize();
343 static size_t getGlobalAllocCount();
344
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800345private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 typedef void (*release_func)(Parcel* parcel,
347 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800348 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 void* cookie);
350
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800351 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800353 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 size_t ipcObjectsCount() const;
355 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800356 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 release_func relFunc, void* relCookie);
358
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800359public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362private:
363 Parcel(const Parcel& o);
364 Parcel& operator=(const Parcel& o);
365
366 status_t finishWrite(size_t len);
367 void releaseObjects();
368 void acquireObjects();
369 status_t growData(size_t len);
370 status_t restartWrite(size_t desired);
371 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000372 status_t writePointer(uintptr_t val);
373 status_t readPointer(uintptr_t *pArg) const;
374 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 void freeDataNoInit();
376 void initState();
377 void scanForFds() const;
378
Andreas Huber84a6d042009-08-17 13:33:27 -0700379 template<class T>
380 status_t readAligned(T *pArg) const;
381
382 template<class T> T readAligned() const;
383
384 template<class T>
385 status_t writeAligned(T val);
386
Casey Dahlinb9872622015-11-25 15:09:45 -0800387 status_t writeRawNullableParcelable(const Parcelable*
388 parcelable);
389
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800390 template<typename T, typename U>
391 status_t unsafeReadTypedVector(std::vector<T>* val,
392 status_t(Parcel::*read_func)(U*) const) const;
393 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800394 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
395 status_t(Parcel::*read_func)(T*) const) const;
396 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800397 status_t readTypedVector(std::vector<T>* val,
398 status_t(Parcel::*read_func)(T*) const) const;
399 template<typename T, typename U>
400 status_t unsafeWriteTypedVector(const std::vector<T>& val,
401 status_t(Parcel::*write_func)(U));
402 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800403 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
404 status_t(Parcel::*write_func)(const T&));
405 template<typename T>
406 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
407 status_t(Parcel::*write_func)(T));
408 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800409 status_t writeTypedVector(const std::vector<T>& val,
410 status_t(Parcel::*write_func)(const T&));
411 template<typename T>
412 status_t writeTypedVector(const std::vector<T>& val,
413 status_t(Parcel::*write_func)(T));
414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 status_t mError;
416 uint8_t* mData;
417 size_t mDataSize;
418 size_t mDataCapacity;
419 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800420 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 size_t mObjectsSize;
422 size_t mObjectsCapacity;
423 mutable size_t mNextObjectHint;
424
425 mutable bool mFdsKnown;
426 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400427 bool mAllowFds;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 release_func mOwner;
430 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700431
432 class Blob {
433 public:
434 Blob();
435 ~Blob();
436
Jeff Brown13b16042014-11-11 16:44:25 -0800437 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700438 void release();
439 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800440 inline int fd() const { return mFd; };
441 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700442
443 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800444 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700445
Jeff Brown13b16042014-11-11 16:44:25 -0800446 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700447 void* mData;
448 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800449 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700450 };
451
Mathias Agopiane1424282013-07-29 21:24:40 -0700452 class FlattenableHelperInterface {
453 protected:
454 ~FlattenableHelperInterface() { }
455 public:
456 virtual size_t getFlattenedSize() const = 0;
457 virtual size_t getFdCount() const = 0;
458 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
459 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
460 };
461
462 template<typename T>
463 class FlattenableHelper : public FlattenableHelperInterface {
464 friend class Parcel;
465 const Flattenable<T>& val;
466 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
467
468 public:
469 virtual size_t getFlattenedSize() const {
470 return val.getFlattenedSize();
471 }
472 virtual size_t getFdCount() const {
473 return val.getFdCount();
474 }
475 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
476 return val.flatten(buffer, size, fds, count);
477 }
478 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
479 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
480 }
481 };
482 status_t write(const FlattenableHelperInterface& val);
483 status_t read(FlattenableHelperInterface& val) const;
484
Jeff Brown5707dbf2011-09-23 21:17:56 -0700485public:
486 class ReadableBlob : public Blob {
487 friend class Parcel;
488 public:
489 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800490 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700491 };
492
493 class WritableBlob : public Blob {
494 friend class Parcel;
495 public:
496 inline void* data() { return mData; }
497 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400498
499private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700500 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400501
502public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700503 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400504 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700505 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506};
507
508// ---------------------------------------------------------------------------
509
Mathias Agopian8683fca2012-08-12 19:37:16 -0700510template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700511status_t Parcel::write(const Flattenable<T>& val) {
512 const FlattenableHelper<T> helper(val);
513 return write(helper);
514}
515
516template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700517status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700518 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700519 if (!val.isFixedSize()) {
520 status_t err = writeInt32(size);
521 if (err != NO_ERROR) {
522 return err;
523 }
524 }
Mathias Agopian20985172012-08-31 14:25:22 -0700525 if (size) {
526 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700527 if (buffer == NULL)
528 return NO_MEMORY;
529 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700530 }
531 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700532}
533
534template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700535status_t Parcel::read(Flattenable<T>& val) const {
536 FlattenableHelper<T> helper(val);
537 return read(helper);
538}
539
540template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700541status_t Parcel::read(LightFlattenable<T>& val) const {
542 size_t size;
543 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700544 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700545 } else {
546 int32_t s;
547 status_t err = readInt32(&s);
548 if (err != NO_ERROR) {
549 return err;
550 }
551 size = s;
552 }
Mathias Agopian20985172012-08-31 14:25:22 -0700553 if (size) {
554 void const* buffer = readInplace(size);
555 return buffer == NULL ? NO_MEMORY :
556 val.unflatten(buffer, size);
557 }
558 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700559}
560
Casey Dahlinf0c13772015-10-27 18:33:56 -0700561template<typename T>
562status_t Parcel::readStrongBinder(sp<T>* val) const {
563 sp<IBinder> tmp;
564 status_t ret = readStrongBinder(&tmp);
565
566 if (ret == OK) {
567 *val = interface_cast<T>(tmp);
568
569 if (val->get() == nullptr) {
570 return UNKNOWN_ERROR;
571 }
572 }
573
574 return ret;
575}
576
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800577template<typename T, typename U>
578status_t Parcel::unsafeReadTypedVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800579 std::vector<T>* val,
580 status_t(Parcel::*read_func)(U*) const) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800581 int32_t size;
582 status_t status = this->readInt32(&size);
583
584 if (status != OK) {
585 return status;
586 }
587
588 if (size < 0) {
589 return UNEXPECTED_NULL;
590 }
591
592 val->resize(size);
593
594 for (auto& v: *val) {
595 status = (this->*read_func)(&v);
596
597 if (status != OK) {
598 return status;
599 }
600 }
601
602 return OK;
603}
604
605template<typename T>
606status_t Parcel::readTypedVector(std::vector<T>* val,
607 status_t(Parcel::*read_func)(T*) const) const {
608 return unsafeReadTypedVector(val, read_func);
609}
610
Casey Dahlinb9872622015-11-25 15:09:45 -0800611template<typename T>
612status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
613 status_t(Parcel::*read_func)(T*) const) const {
614 const int32_t start = dataPosition();
615 int32_t size;
616 status_t status = readInt32(&size);
617 val->reset();
618
619 if (status != OK || size < 0) {
620 return status;
621 }
622
623 setDataPosition(start);
624 val->reset(new std::vector<T>());
625
626 status = unsafeReadTypedVector(val->get(), read_func);
627
628 if (status != OK) {
629 val->reset();
630 }
631
632 return status;
633}
634
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800635template<typename T, typename U>
636status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
637 status_t(Parcel::*write_func)(U)) {
638 if (val.size() > std::numeric_limits<int32_t>::max()) {
639 return BAD_VALUE;
640 }
641
642 status_t status = this->writeInt32(val.size());
643
644 if (status != OK) {
645 return status;
646 }
647
648 for (const auto& item : val) {
649 status = (this->*write_func)(item);
650
651 if (status != OK) {
652 return status;
653 }
654 }
655
656 return OK;
657}
658
659template<typename T>
660status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800661 status_t(Parcel::*write_func)(const T&)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800662 return unsafeWriteTypedVector(val, write_func);
663}
664
665template<typename T>
666status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800667 status_t(Parcel::*write_func)(T)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800668 return unsafeWriteTypedVector(val, write_func);
669}
670
Christopher Wiley97f048d2015-11-19 06:49:05 -0800671template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800672status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
673 status_t(Parcel::*write_func)(const T&)) {
674 if (val.get() == nullptr) {
675 return this->writeInt32(-1);
676 }
677
678 return unsafeWriteTypedVector(*val, write_func);
679}
680
681template<typename T>
682status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
683 status_t(Parcel::*write_func)(T)) {
684 if (val.get() == nullptr) {
685 return this->writeInt32(-1);
686 }
687
688 return unsafeWriteTypedVector(*val, write_func);
689}
690
691template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800692status_t Parcel::readParcelableVector(std::vector<T>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -0800693 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
694}
695
696template<typename T>
697status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
698 const int32_t start = dataPosition();
699 int32_t size;
700 status_t status = readInt32(&size);
701 val->reset();
702
703 if (status != OK || size < 0) {
704 return status;
705 }
706
707 setDataPosition(start);
708 val->reset(new std::vector<T>());
709
710 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable);
711
712 if (status != OK) {
713 val->reset();
714 }
715
716 return status;
717}
718
719template<typename T>
720status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
721 const int32_t start = dataPosition();
722 int32_t present;
723 status_t status = readInt32(&present);
724 parcelable->reset();
725
726 if (status != OK || !present) {
727 return status;
728 }
729
730 setDataPosition(start);
731 parcelable->reset(new T());
732
733 status = readParcelable(parcelable->get());
734
735 if (status != OK) {
736 parcelable->reset();
737 }
738
739 return status;
740}
741
742template<typename T>
743status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
744 return writeRawNullableParcelable(parcelable.get());
Christopher Wiley97f048d2015-11-19 06:49:05 -0800745}
746
747template<typename T>
748status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -0800749 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
750}
751
752template<typename T>
753status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
754 if (val.get() == nullptr) {
755 return this->writeInt32(-1);
756 }
757
758 return unsafeWriteTypedVector(*val, &Parcel::writeParcelable);
Christopher Wiley97f048d2015-11-19 06:49:05 -0800759}
760
Mathias Agopian8683fca2012-08-12 19:37:16 -0700761// ---------------------------------------------------------------------------
762
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
764{
765 parcel.print(to);
766 return to;
767}
768
769// ---------------------------------------------------------------------------
770
771// Generic acquire and release of objects.
772void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000773 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774void release_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000775 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776
777void flatten_binder(const sp<ProcessState>& proc,
778 const sp<IBinder>& binder, flat_binder_object* out);
779void flatten_binder(const sp<ProcessState>& proc,
780 const wp<IBinder>& binder, flat_binder_object* out);
781status_t unflatten_binder(const sp<ProcessState>& proc,
782 const flat_binder_object& flat, sp<IBinder>* out);
783status_t unflatten_binder(const sp<ProcessState>& proc,
784 const flat_binder_object& flat, wp<IBinder>* out);
785
786}; // namespace android
787
788// ---------------------------------------------------------------------------
789
790#endif // ANDROID_PARCEL_H