blob: 0eb2ea7d12b806ddd03fea4cc2b708e85687a5e0 [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
171
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700172 // Place a native_handle into the parcel (the native_handle's file-
173 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700174 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700175 // Doesn't take ownership of the native_handle.
176 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177
178 // Place a file descriptor into the parcel. The given fd must remain
179 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700180 // The Parcel does not take ownership of the given fd unless you ask it to.
181 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182
183 // Place a file descriptor into the parcel. A dup of the fd is made, which
184 // will be closed once the parcel is destroyed.
185 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700186
Casey Dahlin06673e32015-11-23 13:24:23 -0800187 // Place a file descriptor into the parcel. This will not affect the
188 // semantics of the smart file descriptor. A new descriptor will be
189 // created, and will be closed when the parcel is destroyed.
190 status_t writeUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700191 const base::unique_fd& fd);
Casey Dahlin06673e32015-11-23 13:24:23 -0800192
193 // Place a vector of file desciptors into the parcel. Each descriptor is
194 // dup'd as in writeDupFileDescriptor
195 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700196 const std::unique_ptr<std::vector<base::unique_fd>>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800197 status_t writeUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700198 const std::vector<base::unique_fd>& val);
Casey Dahlin06673e32015-11-23 13:24:23 -0800199
Jeff Brown5707dbf2011-09-23 21:17:56 -0700200 // Writes a blob to the parcel.
201 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800202 // transferred by way of an anonymous shared memory region. Prefer sending
203 // immutable blobs if possible since they may be subsequently transferred between
204 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700205 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800206 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
207
208 // Write an existing immutable blob file descriptor to the parcel.
209 // This allows the client to send the same blob to multiple processes
210 // as long as it keeps a dup of the blob file descriptor handy for later.
211 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
214
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700215 // Like Parcel.java's writeNoException(). Just writes a zero int32.
216 // Currently the native implementation doesn't do any of the StrictMode
217 // stack gathering and serialization that the Java implementation does.
218 status_t writeNoException();
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 void remove(size_t start, size_t amt);
221
222 status_t read(void* outData, size_t len) const;
223 const void* readInplace(size_t len) const;
224 int32_t readInt32() const;
225 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800226 uint32_t readUint32() const;
227 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 int64_t readInt64() const;
229 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700230 uint64_t readUint64() const;
231 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 float readFloat() const;
233 status_t readFloat(float *pArg) const;
234 double readDouble() const;
235 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700236 intptr_t readIntPtr() const;
237 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700238 bool readBool() const;
239 status_t readBool(bool *pArg) const;
240 char16_t readChar() const;
241 status_t readChar(char16_t *pArg) const;
242 int8_t readByte() const;
243 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800245 // Read a UTF16 encoded string, convert to UTF8
246 status_t readUtf8FromUtf16(std::string* str) const;
247 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 const char* readCString() const;
250 String8 readString8() const;
Roshan Pius87b64d22016-07-18 12:51:02 -0700251 status_t readString8(String8* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700253 status_t readString16(String16* pArg) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800254 status_t readString16(std::unique_ptr<String16>* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 const char16_t* readString16Inplace(size_t* outLen) const;
256 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700257 status_t readStrongBinder(sp<IBinder>* val) const;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800258 status_t readNullableStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700260
Casey Dahlinf0c13772015-10-27 18:33:56 -0700261 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800262 status_t readParcelableVector(
263 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
264 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800265 status_t readParcelableVector(std::vector<T>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800266
Christopher Wiley97f048d2015-11-19 06:49:05 -0800267 status_t readParcelable(Parcelable* parcelable) const;
268
269 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800270 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
271
272 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800273 status_t readStrongBinder(sp<T>* val) const;
274
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800275 template<typename T>
276 status_t readNullableStrongBinder(sp<T>* val) const;
277
Casey Dahlinb9872622015-11-25 15:09:45 -0800278 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800279 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700280
Casey Dahlinb9872622015-11-25 15:09:45 -0800281 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700282 status_t readByteVector(std::vector<int8_t>* val) const;
Casey Dahlin185d3442016-02-09 11:08:35 -0800283 status_t readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const;
284 status_t readByteVector(std::vector<uint8_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800285 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700286 status_t readInt32Vector(std::vector<int32_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800287 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700288 status_t readInt64Vector(std::vector<int64_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800289 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700290 status_t readFloatVector(std::vector<float>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800291 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700292 status_t readDoubleVector(std::vector<double>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800293 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700294 status_t readBoolVector(std::vector<bool>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800295 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700296 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800297 status_t readString16Vector(
298 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700299 status_t readString16Vector(std::vector<String16>* val) const;
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800300 status_t readUtf8VectorFromUtf16Vector(
301 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
302 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700303
Mathias Agopiane1424282013-07-29 21:24:40 -0700304 template<typename T>
305 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700306
Mathias Agopian8683fca2012-08-12 19:37:16 -0700307 template<typename T>
308 status_t read(LightFlattenable<T>& val) const;
309
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700310 // Like Parcel.java's readExceptionCode(). Reads the first int32
311 // off of a Parcel's header, returning 0 or the negative error
312 // code on exceptions, but also deals with skipping over rich
313 // response headers. Callers should use this to read & parse the
314 // response headers rather than doing it by hand.
315 int32_t readExceptionCode() const;
316
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700317 // Retrieve native_handle from the parcel. This returns a copy of the
318 // parcel's native_handle (the caller takes ownership). The caller
319 // must free the native_handle with native_handle_close() and
320 // native_handle_delete().
321 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
323
324 // Retrieve a file descriptor from the parcel. This returns the raw fd
325 // in the parcel, which you do not own -- use dup() to get your own copy.
326 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700327
Casey Dahlin06673e32015-11-23 13:24:23 -0800328 // Retrieve a smart file descriptor from the parcel.
329 status_t readUniqueFileDescriptor(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700330 base::unique_fd* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800331
332
333 // Retrieve a vector of smart file descriptors from the parcel.
334 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700335 std::unique_ptr<std::vector<base::unique_fd>>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800336 status_t readUniqueFileDescriptorVector(
Christopher Wiley2cf19952016-04-11 11:09:37 -0700337 std::vector<base::unique_fd>* val) const;
Casey Dahlin06673e32015-11-23 13:24:23 -0800338
Jeff Brown5707dbf2011-09-23 21:17:56 -0700339 // Reads a blob from the parcel.
340 // The caller should call release() on the blob after reading its contents.
341 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 const flat_binder_object* readObject(bool nullMetaData) const;
344
345 // Explicitly close all file descriptors in the parcel.
346 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800347
348 // Debugging: get metrics on current allocations.
349 static size_t getGlobalAllocSize();
350 static size_t getGlobalAllocCount();
351
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800352private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 typedef void (*release_func)(Parcel* parcel,
354 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800355 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 void* cookie);
357
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800358 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800360 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 size_t ipcObjectsCount() const;
362 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800363 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 release_func relFunc, void* relCookie);
365
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800366public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700368
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369private:
370 Parcel(const Parcel& o);
371 Parcel& operator=(const Parcel& o);
372
373 status_t finishWrite(size_t len);
374 void releaseObjects();
375 void acquireObjects();
376 status_t growData(size_t len);
377 status_t restartWrite(size_t desired);
378 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000379 status_t writePointer(uintptr_t val);
380 status_t readPointer(uintptr_t *pArg) const;
381 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 void freeDataNoInit();
383 void initState();
384 void scanForFds() const;
385
Andreas Huber84a6d042009-08-17 13:33:27 -0700386 template<class T>
387 status_t readAligned(T *pArg) const;
388
389 template<class T> T readAligned() const;
390
391 template<class T>
392 status_t writeAligned(T val);
393
Casey Dahlinb9872622015-11-25 15:09:45 -0800394 status_t writeRawNullableParcelable(const Parcelable*
395 parcelable);
396
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800397 template<typename T, typename U>
398 status_t unsafeReadTypedVector(std::vector<T>* val,
399 status_t(Parcel::*read_func)(U*) const) const;
400 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800401 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
402 status_t(Parcel::*read_func)(T*) const) const;
403 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800404 status_t readTypedVector(std::vector<T>* val,
405 status_t(Parcel::*read_func)(T*) const) const;
406 template<typename T, typename U>
407 status_t unsafeWriteTypedVector(const std::vector<T>& val,
408 status_t(Parcel::*write_func)(U));
409 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800410 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
411 status_t(Parcel::*write_func)(const T&));
412 template<typename T>
413 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
414 status_t(Parcel::*write_func)(T));
415 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800416 status_t writeTypedVector(const std::vector<T>& val,
417 status_t(Parcel::*write_func)(const T&));
418 template<typename T>
419 status_t writeTypedVector(const std::vector<T>& val,
420 status_t(Parcel::*write_func)(T));
421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 status_t mError;
423 uint8_t* mData;
424 size_t mDataSize;
425 size_t mDataCapacity;
426 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800427 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 size_t mObjectsSize;
429 size_t mObjectsCapacity;
430 mutable size_t mNextObjectHint;
431
432 mutable bool mFdsKnown;
433 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400434 bool mAllowFds;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 release_func mOwner;
437 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700438
439 class Blob {
440 public:
441 Blob();
442 ~Blob();
443
Jeff Brown13b16042014-11-11 16:44:25 -0800444 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700445 void release();
446 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800447 inline int fd() const { return mFd; };
448 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700449
450 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800451 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700452
Jeff Brown13b16042014-11-11 16:44:25 -0800453 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700454 void* mData;
455 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800456 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700457 };
458
Mathias Agopiane1424282013-07-29 21:24:40 -0700459 class FlattenableHelperInterface {
460 protected:
461 ~FlattenableHelperInterface() { }
462 public:
463 virtual size_t getFlattenedSize() const = 0;
464 virtual size_t getFdCount() const = 0;
465 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
466 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
467 };
468
469 template<typename T>
470 class FlattenableHelper : public FlattenableHelperInterface {
471 friend class Parcel;
472 const Flattenable<T>& val;
473 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
474
475 public:
476 virtual size_t getFlattenedSize() const {
477 return val.getFlattenedSize();
478 }
479 virtual size_t getFdCount() const {
480 return val.getFdCount();
481 }
482 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
483 return val.flatten(buffer, size, fds, count);
484 }
485 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
486 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
487 }
488 };
489 status_t write(const FlattenableHelperInterface& val);
490 status_t read(FlattenableHelperInterface& val) const;
491
Jeff Brown5707dbf2011-09-23 21:17:56 -0700492public:
493 class ReadableBlob : public Blob {
494 friend class Parcel;
495 public:
496 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800497 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700498 };
499
500 class WritableBlob : public Blob {
501 friend class Parcel;
502 public:
503 inline void* data() { return mData; }
504 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400505
506private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700507 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400508
509public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700510 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400511 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700512 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513};
514
515// ---------------------------------------------------------------------------
516
Mathias Agopian8683fca2012-08-12 19:37:16 -0700517template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700518status_t Parcel::write(const Flattenable<T>& val) {
519 const FlattenableHelper<T> helper(val);
520 return write(helper);
521}
522
523template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700524status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700525 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700526 if (!val.isFixedSize()) {
527 status_t err = writeInt32(size);
528 if (err != NO_ERROR) {
529 return err;
530 }
531 }
Mathias Agopian20985172012-08-31 14:25:22 -0700532 if (size) {
533 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700534 if (buffer == NULL)
535 return NO_MEMORY;
536 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700537 }
538 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700539}
540
541template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700542status_t Parcel::read(Flattenable<T>& val) const {
543 FlattenableHelper<T> helper(val);
544 return read(helper);
545}
546
547template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700548status_t Parcel::read(LightFlattenable<T>& val) const {
549 size_t size;
550 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700551 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700552 } else {
553 int32_t s;
554 status_t err = readInt32(&s);
555 if (err != NO_ERROR) {
556 return err;
557 }
558 size = s;
559 }
Mathias Agopian20985172012-08-31 14:25:22 -0700560 if (size) {
561 void const* buffer = readInplace(size);
562 return buffer == NULL ? NO_MEMORY :
563 val.unflatten(buffer, size);
564 }
565 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700566}
567
Casey Dahlinf0c13772015-10-27 18:33:56 -0700568template<typename T>
569status_t Parcel::readStrongBinder(sp<T>* val) const {
570 sp<IBinder> tmp;
571 status_t ret = readStrongBinder(&tmp);
572
573 if (ret == OK) {
574 *val = interface_cast<T>(tmp);
575
576 if (val->get() == nullptr) {
577 return UNKNOWN_ERROR;
578 }
579 }
580
581 return ret;
582}
583
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800584template<typename T>
585status_t Parcel::readNullableStrongBinder(sp<T>* val) const {
586 sp<IBinder> tmp;
587 status_t ret = readNullableStrongBinder(&tmp);
588
589 if (ret == OK) {
590 *val = interface_cast<T>(tmp);
591
Christopher Wiley447b00f2016-07-19 09:23:25 -0700592 if (val->get() == nullptr && tmp.get() != nullptr) {
593 ret = UNKNOWN_ERROR;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800594 }
595 }
Christopher Wiley447b00f2016-07-19 09:23:25 -0700596
597 return ret;
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800598}
599
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800600template<typename T, typename U>
601status_t Parcel::unsafeReadTypedVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800602 std::vector<T>* val,
603 status_t(Parcel::*read_func)(U*) const) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800604 int32_t size;
605 status_t status = this->readInt32(&size);
606
607 if (status != OK) {
608 return status;
609 }
610
611 if (size < 0) {
612 return UNEXPECTED_NULL;
613 }
614
615 val->resize(size);
616
617 for (auto& v: *val) {
618 status = (this->*read_func)(&v);
619
620 if (status != OK) {
621 return status;
622 }
623 }
624
625 return OK;
626}
627
628template<typename T>
629status_t Parcel::readTypedVector(std::vector<T>* val,
630 status_t(Parcel::*read_func)(T*) const) const {
631 return unsafeReadTypedVector(val, read_func);
632}
633
Casey Dahlinb9872622015-11-25 15:09:45 -0800634template<typename T>
635status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
636 status_t(Parcel::*read_func)(T*) const) const {
637 const int32_t start = dataPosition();
638 int32_t size;
639 status_t status = readInt32(&size);
640 val->reset();
641
642 if (status != OK || size < 0) {
643 return status;
644 }
645
646 setDataPosition(start);
647 val->reset(new std::vector<T>());
648
649 status = unsafeReadTypedVector(val->get(), read_func);
650
651 if (status != OK) {
652 val->reset();
653 }
654
655 return status;
656}
657
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800658template<typename T, typename U>
659status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
660 status_t(Parcel::*write_func)(U)) {
661 if (val.size() > std::numeric_limits<int32_t>::max()) {
662 return BAD_VALUE;
663 }
664
665 status_t status = this->writeInt32(val.size());
666
667 if (status != OK) {
668 return status;
669 }
670
671 for (const auto& item : val) {
672 status = (this->*write_func)(item);
673
674 if (status != OK) {
675 return status;
676 }
677 }
678
679 return OK;
680}
681
682template<typename T>
683status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800684 status_t(Parcel::*write_func)(const T&)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800685 return unsafeWriteTypedVector(val, write_func);
686}
687
688template<typename T>
689status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800690 status_t(Parcel::*write_func)(T)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800691 return unsafeWriteTypedVector(val, write_func);
692}
693
Christopher Wiley97f048d2015-11-19 06:49:05 -0800694template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800695status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
696 status_t(Parcel::*write_func)(const T&)) {
697 if (val.get() == nullptr) {
698 return this->writeInt32(-1);
699 }
700
701 return unsafeWriteTypedVector(*val, write_func);
702}
703
704template<typename T>
705status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
706 status_t(Parcel::*write_func)(T)) {
707 if (val.get() == nullptr) {
708 return this->writeInt32(-1);
709 }
710
711 return unsafeWriteTypedVector(*val, write_func);
712}
713
714template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800715status_t Parcel::readParcelableVector(std::vector<T>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -0800716 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
717}
718
719template<typename T>
720status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
721 const int32_t start = dataPosition();
722 int32_t size;
723 status_t status = readInt32(&size);
724 val->reset();
725
726 if (status != OK || size < 0) {
727 return status;
728 }
729
730 setDataPosition(start);
Janis Danisevskisf26a3ab2016-05-20 18:10:04 +0100731 val->reset(new std::vector<std::unique_ptr<T>>());
Casey Dahlinb9872622015-11-25 15:09:45 -0800732
Janis Danisevskisf26a3ab2016-05-20 18:10:04 +0100733 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable<T>);
Casey Dahlinb9872622015-11-25 15:09:45 -0800734
735 if (status != OK) {
736 val->reset();
737 }
738
739 return status;
740}
741
742template<typename T>
743status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
744 const int32_t start = dataPosition();
745 int32_t present;
746 status_t status = readInt32(&present);
747 parcelable->reset();
748
749 if (status != OK || !present) {
750 return status;
751 }
752
753 setDataPosition(start);
754 parcelable->reset(new T());
755
756 status = readParcelable(parcelable->get());
757
758 if (status != OK) {
759 parcelable->reset();
760 }
761
762 return status;
763}
764
765template<typename T>
766status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
767 return writeRawNullableParcelable(parcelable.get());
Christopher Wiley97f048d2015-11-19 06:49:05 -0800768}
769
770template<typename T>
771status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -0800772 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
773}
774
775template<typename T>
776status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
777 if (val.get() == nullptr) {
778 return this->writeInt32(-1);
779 }
780
Janis Danisevskis8d747092016-08-11 13:52:12 +0100781 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
782}
783
784template<typename T>
785status_t Parcel::writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val) {
786 if (val.get() == nullptr) {
787 return this->writeInt32(-1);
788 }
789
790 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
Christopher Wiley97f048d2015-11-19 06:49:05 -0800791}
792
Mathias Agopian8683fca2012-08-12 19:37:16 -0700793// ---------------------------------------------------------------------------
794
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
796{
797 parcel.print(to);
798 return to;
799}
800
801// ---------------------------------------------------------------------------
802
803// Generic acquire and release of objects.
804void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000805 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806void release_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000807 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808
809void flatten_binder(const sp<ProcessState>& proc,
810 const sp<IBinder>& binder, flat_binder_object* out);
811void flatten_binder(const sp<ProcessState>& proc,
812 const wp<IBinder>& binder, flat_binder_object* out);
813status_t unflatten_binder(const sp<ProcessState>& proc,
814 const flat_binder_object& flat, sp<IBinder>* out);
815status_t unflatten_binder(const sp<ProcessState>& proc,
816 const flat_binder_object& flat, wp<IBinder>* out);
817
818}; // namespace android
819
820// ---------------------------------------------------------------------------
821
822#endif // ANDROID_PARCEL_H