blob: cf2fa47df47e9f68227ad7a3424959dd24b86505 [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
Christopher Wiley83e6b982016-04-19 10:27:00 -070023#include <android-base/unique_fd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <cutils/native_handle.h>
25#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>
Christopher Ferris0170cd02016-07-18 16:57:34 -070030#include <linux/android/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>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080034#include <binder/Map.h>
Casey Dahlinf0c13772015-10-27 18:33:56 -070035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036// ---------------------------------------------------------------------------
37namespace android {
38
Mathias Agopiane1424282013-07-29 21:24:40 -070039template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070040template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070042class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043class ProcessState;
44class String8;
45class TextOutput;
46
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080047namespace binder {
48class Value;
49};
50
Mathias Agopiane1424282013-07-29 21:24:40 -070051class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000052 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070054 class ReadableBlob;
55 class WritableBlob;
56
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 Parcel();
58 ~Parcel();
59
60 const uint8_t* data() const;
61 size_t dataSize() const;
62 size_t dataAvail() const;
63 size_t dataPosition() const;
64 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 status_t setDataSize(size_t size);
67 void setDataPosition(size_t pos) const;
68 status_t setDataCapacity(size_t size);
69
70 status_t setData(const uint8_t* buffer, size_t len);
71
Andreas Huber51faf462011-04-13 10:21:56 -070072 status_t appendFrom(const Parcel *parcel,
73 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Jeff Brown13b16042014-11-11 16:44:25 -080075 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070076 bool pushAllowFds(bool allowFds);
77 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 bool hasFileDescriptors() const;
80
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070081 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070083
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070084 // Parses the RPC header, returning true if the interface name
85 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070086 //
87 // Additionally, enforceInterface does part of the work of
88 // propagating the StrictMode policy mask, populating the current
89 // IPCThreadState, which as an optimization may optionally be
90 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070091 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070092 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070093 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070094
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 void freeData();
96
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080097private:
98 const binder_size_t* objects() const;
99
100public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 size_t objectsCount() const;
102
103 status_t errorCheck() const;
104 void setError(status_t err);
105
106 status_t write(const void* data, size_t len);
107 void* writeInplace(size_t len);
108 status_t writeUnpadded(const void* data, size_t len);
109 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800110 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700112 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 status_t writeFloat(float val);
114 status_t writeDouble(double val);
115 status_t writeCString(const char* str);
116 status_t writeString8(const String8& str);
117 status_t writeString16(const String16& str);
Casey Dahlinb9872622015-11-25 15:09:45 -0800118 status_t writeString16(const std::unique_ptr<String16>& str);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 status_t writeString16(const char16_t* str, size_t len);
120 status_t writeStrongBinder(const sp<IBinder>& val);
121 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700122 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700123 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700124 status_t writeBool(bool val);
125 status_t writeChar(char16_t val);
126 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700127
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800128 // Take a UTF8 encoded string, convert to UTF16, write it to the parcel.
129 status_t writeUtf8AsUtf16(const std::string& str);
130 status_t writeUtf8AsUtf16(const std::unique_ptr<std::string>& str);
131
Casey Dahlinb9872622015-11-25 15:09:45 -0800132 status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700133 status_t writeByteVector(const std::vector<int8_t>& val);
Casey Dahlin185d3442016-02-09 11:08:35 -0800134 status_t writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val);
135 status_t writeByteVector(const std::vector<uint8_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800136 status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700137 status_t writeInt32Vector(const std::vector<int32_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800138 status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700139 status_t writeInt64Vector(const std::vector<int64_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800140 status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700141 status_t writeFloatVector(const std::vector<float>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800142 status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700143 status_t writeDoubleVector(const std::vector<double>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800144 status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700145 status_t writeBoolVector(const std::vector<bool>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800146 status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700147 status_t writeCharVector(const std::vector<char16_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800148 status_t writeString16Vector(
149 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700150 status_t writeString16Vector(const std::vector<String16>& val);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800151 status_t writeUtf8VectorAsUtf16Vector(
152 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val);
153 status_t writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700154
Casey Dahlinb9872622015-11-25 15:09:45 -0800155 status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800156 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
157
Mathias Agopiane1424282013-07-29 21:24:40 -0700158 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800159 status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val);
160 template<typename T>
Janis Danisevskis8d747092016-08-11 13:52:12 +0100161 status_t writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val);
162 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800163 status_t writeParcelableVector(const std::vector<T>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800164
165 template<typename T>
166 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable);
167
Christopher Wiley97f048d2015-11-19 06:49:05 -0800168 status_t writeParcelable(const Parcelable& parcelable);
169
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -0800170 status_t writeValue(const binder::Value& value);
171
Christopher Wiley97f048d2015-11-19 06:49:05 -0800172 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700173 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174
Mathias Agopian8683fca2012-08-12 19:37:16 -0700175 template<typename T>
176 status_t write(const LightFlattenable<T>& val);
177
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700178 template<typename T>
179 status_t writeVectorSize(const std::vector<T>& val);
180 template<typename T>
181 status_t writeVectorSize(const std::unique_ptr<std::vector<T>>& val);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700182
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -0800183 status_t writeMap(const binder::Map& map);
184 status_t writeNullableMap(const std::unique_ptr<binder::Map>& map);
185
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700186 // Place a native_handle into the parcel (the native_handle's file-
187 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700188 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700189 // Doesn't take ownership of the native_handle.
190 status_t writeNativeHandle(const native_handle* handle);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 // Place a file descriptor into the parcel. The given fd must remain
193 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700194 // The Parcel does not take ownership of the given fd unless you ask it to.
195 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 // Place a file descriptor into the parcel. A dup of the fd is made, which
198 // will be closed once the parcel is destroyed.
199 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700200
Dianne Hackborn1941a402016-08-29 12:30:43 -0700201 // Place a Java "parcel file descriptor" into the parcel. The given fd must remain
202 // valid for the lifetime of the parcel.
203 // The Parcel does not take ownership of the given fd unless you ask it to.
204 status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false);
205
Casey Dahlin06673e32015-11-23 13:24:23 -0800206 // Place a file descriptor into the parcel. This will not affect the
207 // semantics of the smart file descriptor. A new descriptor will be
208 // created, and will be closed when the parcel is destroyed.
209 status_t writeUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700210 const base::unique_fd& fd);
Casey Dahlin06673e32015-11-23 13:24:23 -0800211
212 // Place a vector of file desciptors into the parcel. Each descriptor is
213 // dup'd as in writeDupFileDescriptor
214 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700215 const std::unique_ptr<std::vector<base::unique_fd>>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800216 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700217 const std::vector<base::unique_fd>& val);
Casey Dahlin06673e32015-11-23 13:24:23 -0800218
Jeff Brown5707dbf2011-09-23 21:17:56 -0700219 // Writes a blob to the parcel.
220 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800221 // transferred by way of an anonymous shared memory region. Prefer sending
222 // immutable blobs if possible since they may be subsequently transferred between
223 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700224 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800225 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
226
227 // Write an existing immutable blob file descriptor to the parcel.
228 // This allows the client to send the same blob to multiple processes
229 // as long as it keeps a dup of the blob file descriptor handy for later.
230 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
233
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700234 // Like Parcel.java's writeNoException(). Just writes a zero int32.
235 // Currently the native implementation doesn't do any of the StrictMode
236 // stack gathering and serialization that the Java implementation does.
237 status_t writeNoException();
238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 void remove(size_t start, size_t amt);
240
241 status_t read(void* outData, size_t len) const;
242 const void* readInplace(size_t len) const;
243 int32_t readInt32() const;
244 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800245 uint32_t readUint32() const;
246 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 int64_t readInt64() const;
248 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700249 uint64_t readUint64() const;
250 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 float readFloat() const;
252 status_t readFloat(float *pArg) const;
253 double readDouble() const;
254 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700255 intptr_t readIntPtr() const;
256 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700257 bool readBool() const;
258 status_t readBool(bool *pArg) const;
259 char16_t readChar() const;
260 status_t readChar(char16_t *pArg) const;
261 int8_t readByte() const;
262 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800264 // Read a UTF16 encoded string, convert to UTF8
265 status_t readUtf8FromUtf16(std::string* str) const;
266 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 const char* readCString() const;
269 String8 readString8() const;
Roshan Pius87b64d22016-07-18 12:51:02 -0700270 status_t readString8(String8* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700272 status_t readString16(String16* pArg) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800273 status_t readString16(std::unique_ptr<String16>* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 const char16_t* readString16Inplace(size_t* outLen) const;
275 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700276 status_t readStrongBinder(sp<IBinder>* val) const;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800277 status_t readNullableStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700279
Casey Dahlinf0c13772015-10-27 18:33:56 -0700280 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800281 status_t readParcelableVector(
282 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
283 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800284 status_t readParcelableVector(std::vector<T>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800285
Christopher Wiley97f048d2015-11-19 06:49:05 -0800286 status_t readParcelable(Parcelable* parcelable) const;
287
288 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800289 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
290
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -0800291 status_t readValue(binder::Value* value) const;
292
Casey Dahlinb9872622015-11-25 15:09:45 -0800293 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800294 status_t readStrongBinder(sp<T>* val) const;
295
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800296 template<typename T>
297 status_t readNullableStrongBinder(sp<T>* val) const;
298
Casey Dahlinb9872622015-11-25 15:09:45 -0800299 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800300 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700301
Casey Dahlinb9872622015-11-25 15:09:45 -0800302 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700303 status_t readByteVector(std::vector<int8_t>* val) const;
Casey Dahlin185d3442016-02-09 11:08:35 -0800304 status_t readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const;
305 status_t readByteVector(std::vector<uint8_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800306 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700307 status_t readInt32Vector(std::vector<int32_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800308 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700309 status_t readInt64Vector(std::vector<int64_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800310 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700311 status_t readFloatVector(std::vector<float>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800312 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700313 status_t readDoubleVector(std::vector<double>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800314 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700315 status_t readBoolVector(std::vector<bool>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800316 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700317 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800318 status_t readString16Vector(
319 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700320 status_t readString16Vector(std::vector<String16>* val) const;
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800321 status_t readUtf8VectorFromUtf16Vector(
322 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
323 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700324
Mathias Agopiane1424282013-07-29 21:24:40 -0700325 template<typename T>
326 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700327
Mathias Agopian8683fca2012-08-12 19:37:16 -0700328 template<typename T>
329 status_t read(LightFlattenable<T>& val) const;
330
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700331 template<typename T>
332 status_t resizeOutVector(std::vector<T>* val) const;
333 template<typename T>
334 status_t resizeOutVector(std::unique_ptr<std::vector<T>>* val) const;
335
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -0800336 status_t readMap(binder::Map* map)const;
337 status_t readNullableMap(std::unique_ptr<binder::Map>* map) const;
338
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700339 // Like Parcel.java's readExceptionCode(). Reads the first int32
340 // off of a Parcel's header, returning 0 or the negative error
341 // code on exceptions, but also deals with skipping over rich
342 // response headers. Callers should use this to read & parse the
343 // response headers rather than doing it by hand.
344 int32_t readExceptionCode() const;
345
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700346 // Retrieve native_handle from the parcel. This returns a copy of the
347 // parcel's native_handle (the caller takes ownership). The caller
348 // must free the native_handle with native_handle_close() and
349 // native_handle_delete().
350 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351
352
353 // Retrieve a file descriptor from the parcel. This returns the raw fd
354 // in the parcel, which you do not own -- use dup() to get your own copy.
355 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700356
Dianne Hackborn1941a402016-08-29 12:30:43 -0700357 // Retrieve a Java "parcel file descriptor" from the parcel. This returns the raw fd
358 // in the parcel, which you do not own -- use dup() to get your own copy.
359 int readParcelFileDescriptor() const;
360
Casey Dahlin06673e32015-11-23 13:24:23 -0800361 // Retrieve a smart file descriptor from the parcel.
362 status_t readUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700363 base::unique_fd* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800364
365
366 // Retrieve a vector of smart file descriptors from the parcel.
367 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700368 std::unique_ptr<std::vector<base::unique_fd>>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800369 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700370 std::vector<base::unique_fd>* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800371
Jeff Brown5707dbf2011-09-23 21:17:56 -0700372 // Reads a blob from the parcel.
373 // The caller should call release() on the blob after reading its contents.
374 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 const flat_binder_object* readObject(bool nullMetaData) const;
377
378 // Explicitly close all file descriptors in the parcel.
379 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800380
381 // Debugging: get metrics on current allocations.
382 static size_t getGlobalAllocSize();
383 static size_t getGlobalAllocCount();
384
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800385private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 typedef void (*release_func)(Parcel* parcel,
387 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800388 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 void* cookie);
390
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800391 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800393 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 size_t ipcObjectsCount() const;
395 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800396 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 release_func relFunc, void* relCookie);
398
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800399public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402private:
403 Parcel(const Parcel& o);
404 Parcel& operator=(const Parcel& o);
405
406 status_t finishWrite(size_t len);
407 void releaseObjects();
408 void acquireObjects();
409 status_t growData(size_t len);
410 status_t restartWrite(size_t desired);
411 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000412 status_t writePointer(uintptr_t val);
413 status_t readPointer(uintptr_t *pArg) const;
414 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 void freeDataNoInit();
416 void initState();
417 void scanForFds() const;
418
Andreas Huber84a6d042009-08-17 13:33:27 -0700419 template<class T>
420 status_t readAligned(T *pArg) const;
421
422 template<class T> T readAligned() const;
423
424 template<class T>
425 status_t writeAligned(T val);
426
Casey Dahlinb9872622015-11-25 15:09:45 -0800427 status_t writeRawNullableParcelable(const Parcelable*
428 parcelable);
429
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800430 template<typename T, typename U>
431 status_t unsafeReadTypedVector(std::vector<T>* val,
432 status_t(Parcel::*read_func)(U*) const) const;
433 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800434 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
435 status_t(Parcel::*read_func)(T*) const) const;
436 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800437 status_t readTypedVector(std::vector<T>* val,
438 status_t(Parcel::*read_func)(T*) const) const;
439 template<typename T, typename U>
440 status_t unsafeWriteTypedVector(const std::vector<T>& val,
441 status_t(Parcel::*write_func)(U));
442 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800443 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
444 status_t(Parcel::*write_func)(const T&));
445 template<typename T>
446 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
447 status_t(Parcel::*write_func)(T));
448 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800449 status_t writeTypedVector(const std::vector<T>& val,
450 status_t(Parcel::*write_func)(const T&));
451 template<typename T>
452 status_t writeTypedVector(const std::vector<T>& val,
453 status_t(Parcel::*write_func)(T));
454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 status_t mError;
456 uint8_t* mData;
457 size_t mDataSize;
458 size_t mDataCapacity;
459 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800460 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 size_t mObjectsSize;
462 size_t mObjectsCapacity;
463 mutable size_t mNextObjectHint;
464
465 mutable bool mFdsKnown;
466 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400467 bool mAllowFds;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 release_func mOwner;
470 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700471
472 class Blob {
473 public:
474 Blob();
475 ~Blob();
476
Jeff Brown13b16042014-11-11 16:44:25 -0800477 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700478 void release();
479 inline size_t size() const { return mSize; }
Colin Cross17576de2016-09-26 13:07:06 -0700480 inline int fd() const { return mFd; }
Jeff Brown13b16042014-11-11 16:44:25 -0800481 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700482
483 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800484 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700485
Jeff Brown13b16042014-11-11 16:44:25 -0800486 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700487 void* mData;
488 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800489 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700490 };
491
Colin Cross97b64db2016-09-26 13:48:02 -0700492 #if defined(__clang__)
493 #pragma clang diagnostic push
494 #pragma clang diagnostic ignored "-Wweak-vtables"
495 #endif
496
Fabien Sanglardc38992f2016-10-27 19:05:29 -0700497 // FlattenableHelperInterface and FlattenableHelper avoid generating a vtable entry in objects
498 // following Flattenable template/protocol.
Mathias Agopiane1424282013-07-29 21:24:40 -0700499 class FlattenableHelperInterface {
500 protected:
501 ~FlattenableHelperInterface() { }
502 public:
503 virtual size_t getFlattenedSize() const = 0;
504 virtual size_t getFdCount() const = 0;
505 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
506 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
507 };
508
Colin Cross97b64db2016-09-26 13:48:02 -0700509 #if defined(__clang__)
510 #pragma clang diagnostic pop
511 #endif
512
Fabien Sanglardc38992f2016-10-27 19:05:29 -0700513 // Concrete implementation of FlattenableHelperInterface that delegates virtual calls to the
514 // specified class T implementing the Flattenable protocol. It "virtualizes" a compile-time
515 // protocol.
Mathias Agopiane1424282013-07-29 21:24:40 -0700516 template<typename T>
517 class FlattenableHelper : public FlattenableHelperInterface {
518 friend class Parcel;
519 const Flattenable<T>& val;
Colin Cross382ecd32016-09-26 13:33:59 -0700520 explicit FlattenableHelper(const Flattenable<T>& _val) : val(_val) { }
Mathias Agopiane1424282013-07-29 21:24:40 -0700521
Colin Cross97b64db2016-09-26 13:48:02 -0700522 protected:
523 ~FlattenableHelper() = default;
Mathias Agopiane1424282013-07-29 21:24:40 -0700524 public:
525 virtual size_t getFlattenedSize() const {
526 return val.getFlattenedSize();
527 }
528 virtual size_t getFdCount() const {
529 return val.getFdCount();
530 }
531 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
532 return val.flatten(buffer, size, fds, count);
533 }
534 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
535 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
536 }
537 };
538 status_t write(const FlattenableHelperInterface& val);
539 status_t read(FlattenableHelperInterface& val) const;
540
Jeff Brown5707dbf2011-09-23 21:17:56 -0700541public:
542 class ReadableBlob : public Blob {
543 friend class Parcel;
544 public:
545 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800546 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700547 };
548
549 class WritableBlob : public Blob {
550 friend class Parcel;
551 public:
552 inline void* data() { return mData; }
553 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400554
555private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700556 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400557
558public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700559 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400560 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700561 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562};
563
564// ---------------------------------------------------------------------------
565
Mathias Agopian8683fca2012-08-12 19:37:16 -0700566template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700567status_t Parcel::write(const Flattenable<T>& val) {
568 const FlattenableHelper<T> helper(val);
569 return write(helper);
570}
571
572template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700573status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700574 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700575 if (!val.isFixedSize()) {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700576 if (size > INT32_MAX) {
577 return BAD_VALUE;
578 }
579 status_t err = writeInt32(static_cast<int32_t>(size));
Mathias Agopian8683fca2012-08-12 19:37:16 -0700580 if (err != NO_ERROR) {
581 return err;
582 }
583 }
Mathias Agopian20985172012-08-31 14:25:22 -0700584 if (size) {
585 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700586 if (buffer == NULL)
587 return NO_MEMORY;
588 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700589 }
590 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700591}
592
593template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700594status_t Parcel::read(Flattenable<T>& val) const {
595 FlattenableHelper<T> helper(val);
596 return read(helper);
597}
598
599template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700600status_t Parcel::read(LightFlattenable<T>& val) const {
601 size_t size;
602 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700603 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700604 } else {
605 int32_t s;
606 status_t err = readInt32(&s);
607 if (err != NO_ERROR) {
608 return err;
609 }
Colin Cross4c62b4f2016-09-27 13:58:30 -0700610 size = static_cast<size_t>(s);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700611 }
Mathias Agopian20985172012-08-31 14:25:22 -0700612 if (size) {
613 void const* buffer = readInplace(size);
614 return buffer == NULL ? NO_MEMORY :
615 val.unflatten(buffer, size);
616 }
617 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700618}
619
Casey Dahlinf0c13772015-10-27 18:33:56 -0700620template<typename T>
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700621status_t Parcel::writeVectorSize(const std::vector<T>& val) {
622 if (val.size() > INT32_MAX) {
623 return BAD_VALUE;
624 }
Colin Cross4c62b4f2016-09-27 13:58:30 -0700625 return writeInt32(static_cast<int32_t>(val.size()));
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700626}
627
628template<typename T>
629status_t Parcel::writeVectorSize(const std::unique_ptr<std::vector<T>>& val) {
630 if (!val) {
631 return writeInt32(-1);
632 }
633
634 return writeVectorSize(*val);
635}
636
637template<typename T>
638status_t Parcel::resizeOutVector(std::vector<T>* val) const {
639 int32_t size;
640 status_t err = readInt32(&size);
641 if (err != NO_ERROR) {
642 return err;
643 }
644
645 if (size < 0) {
646 return UNEXPECTED_NULL;
647 }
648 val->resize(size_t(size));
649 return OK;
650}
651
652template<typename T>
653status_t Parcel::resizeOutVector(std::unique_ptr<std::vector<T>>* val) const {
654 int32_t size;
655 status_t err = readInt32(&size);
656 if (err != NO_ERROR) {
657 return err;
658 }
659
660 val->reset();
661 if (size >= 0) {
662 val->reset(new std::vector<T>(size_t(size)));
663 }
664
665 return OK;
666}
667
668template<typename T>
Casey Dahlinf0c13772015-10-27 18:33:56 -0700669status_t Parcel::readStrongBinder(sp<T>* val) const {
670 sp<IBinder> tmp;
671 status_t ret = readStrongBinder(&tmp);
672
673 if (ret == OK) {
674 *val = interface_cast<T>(tmp);
675
676 if (val->get() == nullptr) {
677 return UNKNOWN_ERROR;
678 }
679 }
680
681 return ret;
682}
683
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800684template<typename T>
685status_t Parcel::readNullableStrongBinder(sp<T>* val) const {
686 sp<IBinder> tmp;
687 status_t ret = readNullableStrongBinder(&tmp);
688
689 if (ret == OK) {
690 *val = interface_cast<T>(tmp);
691
Christopher Wiley447b00f2016-07-19 09:23:25 -0700692 if (val->get() == nullptr && tmp.get() != nullptr) {
693 ret = UNKNOWN_ERROR;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800694 }
695 }
Christopher Wiley447b00f2016-07-19 09:23:25 -0700696
697 return ret;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800698}
699
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800700template<typename T, typename U>
701status_t Parcel::unsafeReadTypedVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800702 std::vector<T>* val,
703 status_t(Parcel::*read_func)(U*) const) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800704 int32_t size;
705 status_t status = this->readInt32(&size);
706
707 if (status != OK) {
708 return status;
709 }
710
711 if (size < 0) {
712 return UNEXPECTED_NULL;
713 }
714
Stephen Hines803ed292016-11-16 11:34:09 -0800715 if (val->max_size() < static_cast<size_t>(size)) {
Casey Dahlin65a8f072016-10-26 17:18:25 -0700716 return NO_MEMORY;
717 }
718
Colin Cross4c62b4f2016-09-27 13:58:30 -0700719 val->resize(static_cast<size_t>(size));
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800720
Stephen Hines803ed292016-11-16 11:34:09 -0800721 if (val->size() < static_cast<size_t>(size)) {
Casey Dahlin65a8f072016-10-26 17:18:25 -0700722 return NO_MEMORY;
723 }
724
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800725 for (auto& v: *val) {
726 status = (this->*read_func)(&v);
727
728 if (status != OK) {
729 return status;
730 }
731 }
732
733 return OK;
734}
735
736template<typename T>
737status_t Parcel::readTypedVector(std::vector<T>* val,
738 status_t(Parcel::*read_func)(T*) const) const {
739 return unsafeReadTypedVector(val, read_func);
740}
741
Casey Dahlinb9872622015-11-25 15:09:45 -0800742template<typename T>
743status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
744 status_t(Parcel::*read_func)(T*) const) const {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700745 const size_t start = dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -0800746 int32_t size;
747 status_t status = readInt32(&size);
748 val->reset();
749
750 if (status != OK || size < 0) {
751 return status;
752 }
753
754 setDataPosition(start);
755 val->reset(new std::vector<T>());
756
757 status = unsafeReadTypedVector(val->get(), read_func);
758
759 if (status != OK) {
760 val->reset();
761 }
762
763 return status;
764}
765
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800766template<typename T, typename U>
767status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
768 status_t(Parcel::*write_func)(U)) {
769 if (val.size() > std::numeric_limits<int32_t>::max()) {
770 return BAD_VALUE;
771 }
772
Colin Cross4c62b4f2016-09-27 13:58:30 -0700773 status_t status = this->writeInt32(static_cast<int32_t>(val.size()));
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800774
775 if (status != OK) {
776 return status;
777 }
778
779 for (const auto& item : val) {
780 status = (this->*write_func)(item);
781
782 if (status != OK) {
783 return status;
784 }
785 }
786
787 return OK;
788}
789
790template<typename T>
791status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800792 status_t(Parcel::*write_func)(const T&)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800793 return unsafeWriteTypedVector(val, write_func);
794}
795
796template<typename T>
797status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800798 status_t(Parcel::*write_func)(T)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800799 return unsafeWriteTypedVector(val, write_func);
800}
801
Christopher Wiley97f048d2015-11-19 06:49:05 -0800802template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800803status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
804 status_t(Parcel::*write_func)(const T&)) {
805 if (val.get() == nullptr) {
806 return this->writeInt32(-1);
807 }
808
809 return unsafeWriteTypedVector(*val, write_func);
810}
811
812template<typename T>
813status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
814 status_t(Parcel::*write_func)(T)) {
815 if (val.get() == nullptr) {
816 return this->writeInt32(-1);
817 }
818
819 return unsafeWriteTypedVector(*val, write_func);
820}
821
822template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800823status_t Parcel::readParcelableVector(std::vector<T>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -0800824 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
825}
826
827template<typename T>
828status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700829 const size_t start = dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -0800830 int32_t size;
831 status_t status = readInt32(&size);
832 val->reset();
833
834 if (status != OK || size < 0) {
835 return status;
836 }
837
838 setDataPosition(start);
Janis Danisevskisf26a3ab2016-05-20 18:10:04 +0100839 val->reset(new std::vector<std::unique_ptr<T>>());
Casey Dahlinb9872622015-11-25 15:09:45 -0800840
Janis Danisevskisf26a3ab2016-05-20 18:10:04 +0100841 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable<T>);
Casey Dahlinb9872622015-11-25 15:09:45 -0800842
843 if (status != OK) {
844 val->reset();
845 }
846
847 return status;
848}
849
850template<typename T>
851status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700852 const size_t start = dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -0800853 int32_t present;
854 status_t status = readInt32(&present);
855 parcelable->reset();
856
857 if (status != OK || !present) {
858 return status;
859 }
860
861 setDataPosition(start);
862 parcelable->reset(new T());
863
864 status = readParcelable(parcelable->get());
865
866 if (status != OK) {
867 parcelable->reset();
868 }
869
870 return status;
871}
872
873template<typename T>
874status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
875 return writeRawNullableParcelable(parcelable.get());
Christopher Wiley97f048d2015-11-19 06:49:05 -0800876}
877
878template<typename T>
879status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -0800880 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
881}
882
883template<typename T>
884status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
885 if (val.get() == nullptr) {
886 return this->writeInt32(-1);
887 }
888
Janis Danisevskis8d747092016-08-11 13:52:12 +0100889 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
890}
891
892template<typename T>
893status_t Parcel::writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val) {
894 if (val.get() == nullptr) {
895 return this->writeInt32(-1);
896 }
897
898 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
Christopher Wiley97f048d2015-11-19 06:49:05 -0800899}
900
Mathias Agopian8683fca2012-08-12 19:37:16 -0700901// ---------------------------------------------------------------------------
902
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
904{
905 parcel.print(to);
906 return to;
907}
908
909// ---------------------------------------------------------------------------
910
911// Generic acquire and release of objects.
912void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000913 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914void release_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000915 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916
917void flatten_binder(const sp<ProcessState>& proc,
918 const sp<IBinder>& binder, flat_binder_object* out);
919void flatten_binder(const sp<ProcessState>& proc,
920 const wp<IBinder>& binder, flat_binder_object* out);
921status_t unflatten_binder(const sp<ProcessState>& proc,
922 const flat_binder_object& flat, sp<IBinder>* out);
923status_t unflatten_binder(const sp<ProcessState>& proc,
924 const flat_binder_object& flat, wp<IBinder>* out);
925
926}; // namespace android
927
928// ---------------------------------------------------------------------------
929
930#endif // ANDROID_PARCEL_H