blob: 954b976cd61a80148800c1c0ebaac83793783211 [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>
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 Dahlin185d3442016-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>
Janis Danisevskis8d747092016-08-11 13:52:12 +0100156 status_t writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val);
157 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800158 status_t writeParcelableVector(const std::vector<T>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800159
160 template<typename T>
161 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable);
162
Christopher Wiley97f048d2015-11-19 06:49:05 -0800163 status_t writeParcelable(const Parcelable& parcelable);
164
165 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700166 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
Mathias Agopian8683fca2012-08-12 19:37:16 -0700168 template<typename T>
169 status_t write(const LightFlattenable<T>& val);
170
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700171 template<typename T>
172 status_t writeVectorSize(const std::vector<T>& val);
173 template<typename T>
174 status_t writeVectorSize(const std::unique_ptr<std::vector<T>>& val);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700175
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700176 // Place a native_handle into the parcel (the native_handle's file-
177 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700178 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700179 // Doesn't take ownership of the native_handle.
180 status_t writeNativeHandle(const native_handle* handle);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 // Place a file descriptor into the parcel. The given fd must remain
183 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700184 // The Parcel does not take ownership of the given fd unless you ask it to.
185 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 // Place a file descriptor into the parcel. A dup of the fd is made, which
188 // will be closed once the parcel is destroyed.
189 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700190
Dianne Hackborn1941a402016-08-29 12:30:43 -0700191 // Place a Java "parcel file descriptor" into the parcel. The given fd must remain
192 // valid for the lifetime of the parcel.
193 // The Parcel does not take ownership of the given fd unless you ask it to.
194 status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false);
195
Casey Dahlin06673e32015-11-23 13:24:23 -0800196 // Place a file descriptor into the parcel. This will not affect the
197 // semantics of the smart file descriptor. A new descriptor will be
198 // created, and will be closed when the parcel is destroyed.
199 status_t writeUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700200 const base::unique_fd& fd);
Casey Dahlin06673e32015-11-23 13:24:23 -0800201
202 // Place a vector of file desciptors into the parcel. Each descriptor is
203 // dup'd as in writeDupFileDescriptor
204 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700205 const std::unique_ptr<std::vector<base::unique_fd>>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800206 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700207 const std::vector<base::unique_fd>& val);
Casey Dahlin06673e32015-11-23 13:24:23 -0800208
Jeff Brown5707dbf2011-09-23 21:17:56 -0700209 // Writes a blob to the parcel.
210 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800211 // transferred by way of an anonymous shared memory region. Prefer sending
212 // immutable blobs if possible since they may be subsequently transferred between
213 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700214 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800215 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
216
217 // Write an existing immutable blob file descriptor to the parcel.
218 // This allows the client to send the same blob to multiple processes
219 // as long as it keeps a dup of the blob file descriptor handy for later.
220 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
223
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700224 // Like Parcel.java's writeNoException(). Just writes a zero int32.
225 // Currently the native implementation doesn't do any of the StrictMode
226 // stack gathering and serialization that the Java implementation does.
227 status_t writeNoException();
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 void remove(size_t start, size_t amt);
230
231 status_t read(void* outData, size_t len) const;
232 const void* readInplace(size_t len) const;
233 int32_t readInt32() const;
234 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800235 uint32_t readUint32() const;
236 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 int64_t readInt64() const;
238 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700239 uint64_t readUint64() const;
240 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 float readFloat() const;
242 status_t readFloat(float *pArg) const;
243 double readDouble() const;
244 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700245 intptr_t readIntPtr() const;
246 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700247 bool readBool() const;
248 status_t readBool(bool *pArg) const;
249 char16_t readChar() const;
250 status_t readChar(char16_t *pArg) const;
251 int8_t readByte() const;
252 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800254 // Read a UTF16 encoded string, convert to UTF8
255 status_t readUtf8FromUtf16(std::string* str) const;
256 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 const char* readCString() const;
259 String8 readString8() const;
Roshan Pius87b64d22016-07-18 12:51:02 -0700260 status_t readString8(String8* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700262 status_t readString16(String16* pArg) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800263 status_t readString16(std::unique_ptr<String16>* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 const char16_t* readString16Inplace(size_t* outLen) const;
265 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700266 status_t readStrongBinder(sp<IBinder>* val) const;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800267 status_t readNullableStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700269
Casey Dahlinf0c13772015-10-27 18:33:56 -0700270 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800271 status_t readParcelableVector(
272 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
273 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800274 status_t readParcelableVector(std::vector<T>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800275
Christopher Wiley97f048d2015-11-19 06:49:05 -0800276 status_t readParcelable(Parcelable* parcelable) const;
277
278 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800279 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
280
281 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800282 status_t readStrongBinder(sp<T>* val) const;
283
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800284 template<typename T>
285 status_t readNullableStrongBinder(sp<T>* val) const;
286
Casey Dahlinb9872622015-11-25 15:09:45 -0800287 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800288 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700289
Casey Dahlinb9872622015-11-25 15:09:45 -0800290 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700291 status_t readByteVector(std::vector<int8_t>* val) const;
Casey Dahlin185d3442016-02-09 11:08:35 -0800292 status_t readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const;
293 status_t readByteVector(std::vector<uint8_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800294 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700295 status_t readInt32Vector(std::vector<int32_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800296 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700297 status_t readInt64Vector(std::vector<int64_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800298 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700299 status_t readFloatVector(std::vector<float>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800300 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700301 status_t readDoubleVector(std::vector<double>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800302 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700303 status_t readBoolVector(std::vector<bool>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800304 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700305 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800306 status_t readString16Vector(
307 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700308 status_t readString16Vector(std::vector<String16>* val) const;
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800309 status_t readUtf8VectorFromUtf16Vector(
310 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
311 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700312
Mathias Agopiane1424282013-07-29 21:24:40 -0700313 template<typename T>
314 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700315
Mathias Agopian8683fca2012-08-12 19:37:16 -0700316 template<typename T>
317 status_t read(LightFlattenable<T>& val) const;
318
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700319 template<typename T>
320 status_t resizeOutVector(std::vector<T>* val) const;
321 template<typename T>
322 status_t resizeOutVector(std::unique_ptr<std::vector<T>>* val) const;
323
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700324 // Like Parcel.java's readExceptionCode(). Reads the first int32
325 // off of a Parcel's header, returning 0 or the negative error
326 // code on exceptions, but also deals with skipping over rich
327 // response headers. Callers should use this to read & parse the
328 // response headers rather than doing it by hand.
329 int32_t readExceptionCode() const;
330
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700331 // Retrieve native_handle from the parcel. This returns a copy of the
332 // parcel's native_handle (the caller takes ownership). The caller
333 // must free the native_handle with native_handle_close() and
334 // native_handle_delete().
335 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336
337
338 // Retrieve a file descriptor from the parcel. This returns the raw fd
339 // in the parcel, which you do not own -- use dup() to get your own copy.
340 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700341
Dianne Hackborn1941a402016-08-29 12:30:43 -0700342 // Retrieve a Java "parcel file descriptor" from the parcel. This returns the raw fd
343 // in the parcel, which you do not own -- use dup() to get your own copy.
344 int readParcelFileDescriptor() const;
345
Casey Dahlin06673e32015-11-23 13:24:23 -0800346 // Retrieve a smart file descriptor from the parcel.
347 status_t readUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700348 base::unique_fd* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800349
350
351 // Retrieve a vector of smart file descriptors from the parcel.
352 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700353 std::unique_ptr<std::vector<base::unique_fd>>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800354 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700355 std::vector<base::unique_fd>* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800356
Jeff Brown5707dbf2011-09-23 21:17:56 -0700357 // Reads a blob from the parcel.
358 // The caller should call release() on the blob after reading its contents.
359 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
360
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 const flat_binder_object* readObject(bool nullMetaData) const;
362
363 // Explicitly close all file descriptors in the parcel.
364 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800365
366 // Debugging: get metrics on current allocations.
367 static size_t getGlobalAllocSize();
368 static size_t getGlobalAllocCount();
369
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800370private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 typedef void (*release_func)(Parcel* parcel,
372 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800373 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 void* cookie);
375
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800376 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800378 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 size_t ipcObjectsCount() const;
380 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800381 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 release_func relFunc, void* relCookie);
383
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800384public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700386
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387private:
388 Parcel(const Parcel& o);
389 Parcel& operator=(const Parcel& o);
390
391 status_t finishWrite(size_t len);
392 void releaseObjects();
393 void acquireObjects();
394 status_t growData(size_t len);
395 status_t restartWrite(size_t desired);
396 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000397 status_t writePointer(uintptr_t val);
398 status_t readPointer(uintptr_t *pArg) const;
399 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 void freeDataNoInit();
401 void initState();
402 void scanForFds() const;
403
Andreas Huber84a6d042009-08-17 13:33:27 -0700404 template<class T>
405 status_t readAligned(T *pArg) const;
406
407 template<class T> T readAligned() const;
408
409 template<class T>
410 status_t writeAligned(T val);
411
Casey Dahlinb9872622015-11-25 15:09:45 -0800412 status_t writeRawNullableParcelable(const Parcelable*
413 parcelable);
414
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800415 template<typename T, typename U>
416 status_t unsafeReadTypedVector(std::vector<T>* val,
417 status_t(Parcel::*read_func)(U*) const) const;
418 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800419 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
420 status_t(Parcel::*read_func)(T*) const) const;
421 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800422 status_t readTypedVector(std::vector<T>* val,
423 status_t(Parcel::*read_func)(T*) const) const;
424 template<typename T, typename U>
425 status_t unsafeWriteTypedVector(const std::vector<T>& val,
426 status_t(Parcel::*write_func)(U));
427 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800428 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
429 status_t(Parcel::*write_func)(const T&));
430 template<typename T>
431 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
432 status_t(Parcel::*write_func)(T));
433 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800434 status_t writeTypedVector(const std::vector<T>& val,
435 status_t(Parcel::*write_func)(const T&));
436 template<typename T>
437 status_t writeTypedVector(const std::vector<T>& val,
438 status_t(Parcel::*write_func)(T));
439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 status_t mError;
441 uint8_t* mData;
442 size_t mDataSize;
443 size_t mDataCapacity;
444 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800445 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 size_t mObjectsSize;
447 size_t mObjectsCapacity;
448 mutable size_t mNextObjectHint;
449
450 mutable bool mFdsKnown;
451 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400452 bool mAllowFds;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 release_func mOwner;
455 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700456
457 class Blob {
458 public:
459 Blob();
460 ~Blob();
461
Jeff Brown13b16042014-11-11 16:44:25 -0800462 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700463 void release();
464 inline size_t size() const { return mSize; }
Colin Cross17576de2016-09-26 13:07:06 -0700465 inline int fd() const { return mFd; }
Jeff Brown13b16042014-11-11 16:44:25 -0800466 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700467
468 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800469 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700470
Jeff Brown13b16042014-11-11 16:44:25 -0800471 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700472 void* mData;
473 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800474 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700475 };
476
Colin Cross97b64db2016-09-26 13:48:02 -0700477 #if defined(__clang__)
478 #pragma clang diagnostic push
479 #pragma clang diagnostic ignored "-Wweak-vtables"
480 #endif
481
Mathias Agopiane1424282013-07-29 21:24:40 -0700482 class FlattenableHelperInterface {
483 protected:
484 ~FlattenableHelperInterface() { }
485 public:
486 virtual size_t getFlattenedSize() const = 0;
487 virtual size_t getFdCount() const = 0;
488 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
489 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
490 };
491
Colin Cross97b64db2016-09-26 13:48:02 -0700492 #if defined(__clang__)
493 #pragma clang diagnostic pop
494 #endif
495
Mathias Agopiane1424282013-07-29 21:24:40 -0700496 template<typename T>
497 class FlattenableHelper : public FlattenableHelperInterface {
498 friend class Parcel;
499 const Flattenable<T>& val;
Colin Cross382ecd32016-09-26 13:33:59 -0700500 explicit FlattenableHelper(const Flattenable<T>& _val) : val(_val) { }
Mathias Agopiane1424282013-07-29 21:24:40 -0700501
Colin Cross97b64db2016-09-26 13:48:02 -0700502 protected:
503 ~FlattenableHelper() = default;
Mathias Agopiane1424282013-07-29 21:24:40 -0700504 public:
505 virtual size_t getFlattenedSize() const {
506 return val.getFlattenedSize();
507 }
508 virtual size_t getFdCount() const {
509 return val.getFdCount();
510 }
511 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
512 return val.flatten(buffer, size, fds, count);
513 }
514 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
515 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
516 }
517 };
518 status_t write(const FlattenableHelperInterface& val);
519 status_t read(FlattenableHelperInterface& val) const;
520
Jeff Brown5707dbf2011-09-23 21:17:56 -0700521public:
522 class ReadableBlob : public Blob {
523 friend class Parcel;
524 public:
525 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800526 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700527 };
528
529 class WritableBlob : public Blob {
530 friend class Parcel;
531 public:
532 inline void* data() { return mData; }
533 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400534
535private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700536 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400537
538public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700539 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400540 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700541 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542};
543
544// ---------------------------------------------------------------------------
545
Mathias Agopian8683fca2012-08-12 19:37:16 -0700546template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700547status_t Parcel::write(const Flattenable<T>& val) {
548 const FlattenableHelper<T> helper(val);
549 return write(helper);
550}
551
552template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700553status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700554 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700555 if (!val.isFixedSize()) {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700556 if (size > INT32_MAX) {
557 return BAD_VALUE;
558 }
559 status_t err = writeInt32(static_cast<int32_t>(size));
Mathias Agopian8683fca2012-08-12 19:37:16 -0700560 if (err != NO_ERROR) {
561 return err;
562 }
563 }
Mathias Agopian20985172012-08-31 14:25:22 -0700564 if (size) {
565 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700566 if (buffer == NULL)
567 return NO_MEMORY;
568 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700569 }
570 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700571}
572
573template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700574status_t Parcel::read(Flattenable<T>& val) const {
575 FlattenableHelper<T> helper(val);
576 return read(helper);
577}
578
579template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700580status_t Parcel::read(LightFlattenable<T>& val) const {
581 size_t size;
582 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700583 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700584 } else {
585 int32_t s;
586 status_t err = readInt32(&s);
587 if (err != NO_ERROR) {
588 return err;
589 }
Colin Cross4c62b4f2016-09-27 13:58:30 -0700590 size = static_cast<size_t>(s);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700591 }
Mathias Agopian20985172012-08-31 14:25:22 -0700592 if (size) {
593 void const* buffer = readInplace(size);
594 return buffer == NULL ? NO_MEMORY :
595 val.unflatten(buffer, size);
596 }
597 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700598}
599
Casey Dahlinf0c13772015-10-27 18:33:56 -0700600template<typename T>
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700601status_t Parcel::writeVectorSize(const std::vector<T>& val) {
602 if (val.size() > INT32_MAX) {
603 return BAD_VALUE;
604 }
Colin Cross4c62b4f2016-09-27 13:58:30 -0700605 return writeInt32(static_cast<int32_t>(val.size()));
Christopher Wiley31c1beb2016-08-19 11:43:54 -0700606}
607
608template<typename T>
609status_t Parcel::writeVectorSize(const std::unique_ptr<std::vector<T>>& val) {
610 if (!val) {
611 return writeInt32(-1);
612 }
613
614 return writeVectorSize(*val);
615}
616
617template<typename T>
618status_t Parcel::resizeOutVector(std::vector<T>* val) const {
619 int32_t size;
620 status_t err = readInt32(&size);
621 if (err != NO_ERROR) {
622 return err;
623 }
624
625 if (size < 0) {
626 return UNEXPECTED_NULL;
627 }
628 val->resize(size_t(size));
629 return OK;
630}
631
632template<typename T>
633status_t Parcel::resizeOutVector(std::unique_ptr<std::vector<T>>* val) const {
634 int32_t size;
635 status_t err = readInt32(&size);
636 if (err != NO_ERROR) {
637 return err;
638 }
639
640 val->reset();
641 if (size >= 0) {
642 val->reset(new std::vector<T>(size_t(size)));
643 }
644
645 return OK;
646}
647
648template<typename T>
Casey Dahlinf0c13772015-10-27 18:33:56 -0700649status_t Parcel::readStrongBinder(sp<T>* val) const {
650 sp<IBinder> tmp;
651 status_t ret = readStrongBinder(&tmp);
652
653 if (ret == OK) {
654 *val = interface_cast<T>(tmp);
655
656 if (val->get() == nullptr) {
657 return UNKNOWN_ERROR;
658 }
659 }
660
661 return ret;
662}
663
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800664template<typename T>
665status_t Parcel::readNullableStrongBinder(sp<T>* val) const {
666 sp<IBinder> tmp;
667 status_t ret = readNullableStrongBinder(&tmp);
668
669 if (ret == OK) {
670 *val = interface_cast<T>(tmp);
671
Christopher Wiley447b00f2016-07-19 09:23:25 -0700672 if (val->get() == nullptr && tmp.get() != nullptr) {
673 ret = UNKNOWN_ERROR;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800674 }
675 }
Christopher Wiley447b00f2016-07-19 09:23:25 -0700676
677 return ret;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800678}
679
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800680template<typename T, typename U>
681status_t Parcel::unsafeReadTypedVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800682 std::vector<T>* val,
683 status_t(Parcel::*read_func)(U*) const) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800684 int32_t size;
685 status_t status = this->readInt32(&size);
686
687 if (status != OK) {
688 return status;
689 }
690
691 if (size < 0) {
692 return UNEXPECTED_NULL;
693 }
694
Colin Cross4c62b4f2016-09-27 13:58:30 -0700695 val->resize(static_cast<size_t>(size));
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800696
697 for (auto& v: *val) {
698 status = (this->*read_func)(&v);
699
700 if (status != OK) {
701 return status;
702 }
703 }
704
705 return OK;
706}
707
708template<typename T>
709status_t Parcel::readTypedVector(std::vector<T>* val,
710 status_t(Parcel::*read_func)(T*) const) const {
711 return unsafeReadTypedVector(val, read_func);
712}
713
Casey Dahlinb9872622015-11-25 15:09:45 -0800714template<typename T>
715status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
716 status_t(Parcel::*read_func)(T*) const) const {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700717 const size_t start = dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -0800718 int32_t size;
719 status_t status = readInt32(&size);
720 val->reset();
721
722 if (status != OK || size < 0) {
723 return status;
724 }
725
726 setDataPosition(start);
727 val->reset(new std::vector<T>());
728
729 status = unsafeReadTypedVector(val->get(), read_func);
730
731 if (status != OK) {
732 val->reset();
733 }
734
735 return status;
736}
737
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800738template<typename T, typename U>
739status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
740 status_t(Parcel::*write_func)(U)) {
741 if (val.size() > std::numeric_limits<int32_t>::max()) {
742 return BAD_VALUE;
743 }
744
Colin Cross4c62b4f2016-09-27 13:58:30 -0700745 status_t status = this->writeInt32(static_cast<int32_t>(val.size()));
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800746
747 if (status != OK) {
748 return status;
749 }
750
751 for (const auto& item : val) {
752 status = (this->*write_func)(item);
753
754 if (status != OK) {
755 return status;
756 }
757 }
758
759 return OK;
760}
761
762template<typename T>
763status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800764 status_t(Parcel::*write_func)(const T&)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800765 return unsafeWriteTypedVector(val, write_func);
766}
767
768template<typename T>
769status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800770 status_t(Parcel::*write_func)(T)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800771 return unsafeWriteTypedVector(val, write_func);
772}
773
Christopher Wiley97f048d2015-11-19 06:49:05 -0800774template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800775status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
776 status_t(Parcel::*write_func)(const T&)) {
777 if (val.get() == nullptr) {
778 return this->writeInt32(-1);
779 }
780
781 return unsafeWriteTypedVector(*val, write_func);
782}
783
784template<typename T>
785status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
786 status_t(Parcel::*write_func)(T)) {
787 if (val.get() == nullptr) {
788 return this->writeInt32(-1);
789 }
790
791 return unsafeWriteTypedVector(*val, write_func);
792}
793
794template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800795status_t Parcel::readParcelableVector(std::vector<T>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -0800796 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
797}
798
799template<typename T>
800status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700801 const size_t start = dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -0800802 int32_t size;
803 status_t status = readInt32(&size);
804 val->reset();
805
806 if (status != OK || size < 0) {
807 return status;
808 }
809
810 setDataPosition(start);
Janis Danisevskisf26a3ab2016-05-20 18:10:04 +0100811 val->reset(new std::vector<std::unique_ptr<T>>());
Casey Dahlinb9872622015-11-25 15:09:45 -0800812
Janis Danisevskisf26a3ab2016-05-20 18:10:04 +0100813 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable<T>);
Casey Dahlinb9872622015-11-25 15:09:45 -0800814
815 if (status != OK) {
816 val->reset();
817 }
818
819 return status;
820}
821
822template<typename T>
823status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
Colin Cross4c62b4f2016-09-27 13:58:30 -0700824 const size_t start = dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -0800825 int32_t present;
826 status_t status = readInt32(&present);
827 parcelable->reset();
828
829 if (status != OK || !present) {
830 return status;
831 }
832
833 setDataPosition(start);
834 parcelable->reset(new T());
835
836 status = readParcelable(parcelable->get());
837
838 if (status != OK) {
839 parcelable->reset();
840 }
841
842 return status;
843}
844
845template<typename T>
846status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
847 return writeRawNullableParcelable(parcelable.get());
Christopher Wiley97f048d2015-11-19 06:49:05 -0800848}
849
850template<typename T>
851status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -0800852 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
853}
854
855template<typename T>
856status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
857 if (val.get() == nullptr) {
858 return this->writeInt32(-1);
859 }
860
Janis Danisevskis8d747092016-08-11 13:52:12 +0100861 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
862}
863
864template<typename T>
865status_t Parcel::writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val) {
866 if (val.get() == nullptr) {
867 return this->writeInt32(-1);
868 }
869
870 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
Christopher Wiley97f048d2015-11-19 06:49:05 -0800871}
872
Mathias Agopian8683fca2012-08-12 19:37:16 -0700873// ---------------------------------------------------------------------------
874
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
876{
877 parcel.print(to);
878 return to;
879}
880
881// ---------------------------------------------------------------------------
882
883// Generic acquire and release of objects.
884void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000885 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886void release_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000887 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888
889void flatten_binder(const sp<ProcessState>& proc,
890 const sp<IBinder>& binder, flat_binder_object* out);
891void flatten_binder(const sp<ProcessState>& proc,
892 const wp<IBinder>& binder, flat_binder_object* out);
893status_t unflatten_binder(const sp<ProcessState>& proc,
894 const flat_binder_object& flat, sp<IBinder>* out);
895status_t unflatten_binder(const sp<ProcessState>& proc,
896 const flat_binder_object& flat, wp<IBinder>* out);
897
898}; // namespace android
899
900// ---------------------------------------------------------------------------
901
902#endif // ANDROID_PARCEL_H