blob: 5956e133b0ba8cb358d30e6e5ba2369f003099ad [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_PARCEL_H
18#define ANDROID_PARCEL_H
19
Christopher Wiley9a5e32f2016-01-28 16:56:53 -080020#include <string>
Casey Dahlin451ff582015-10-19 18:12:18 -070021#include <vector>
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <cutils/native_handle.h>
Casey Dahlin06673e32015-11-23 13:24:23 -080024#include <nativehelper/ScopedFd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Errors.h>
26#include <utils/RefBase.h>
27#include <utils/String16.h>
28#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070029#include <utils/Flattenable.h>
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080030#include <linux/binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Casey Dahlinf0c13772015-10-27 18:33:56 -070032#include <binder/IInterface.h>
Christopher Wiley97f048d2015-11-19 06:49:05 -080033#include <binder/Parcelable.h>
Casey Dahlinf0c13772015-10-27 18:33:56 -070034
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035// ---------------------------------------------------------------------------
36namespace android {
37
Mathias Agopiane1424282013-07-29 21:24:40 -070038template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070039template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070041class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042class ProcessState;
43class String8;
44class TextOutput;
45
Mathias Agopiane1424282013-07-29 21:24:40 -070046class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000047 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070049 class ReadableBlob;
50 class WritableBlob;
51
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 Parcel();
53 ~Parcel();
54
55 const uint8_t* data() const;
56 size_t dataSize() const;
57 size_t dataAvail() const;
58 size_t dataPosition() const;
59 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 status_t setDataSize(size_t size);
62 void setDataPosition(size_t pos) const;
63 status_t setDataCapacity(size_t size);
64
65 status_t setData(const uint8_t* buffer, size_t len);
66
Andreas Huber51faf462011-04-13 10:21:56 -070067 status_t appendFrom(const Parcel *parcel,
68 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Jeff Brown13b16042014-11-11 16:44:25 -080070 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070071 bool pushAllowFds(bool allowFds);
72 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 bool hasFileDescriptors() const;
75
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070076 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070079 // Parses the RPC header, returning true if the interface name
80 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070081 //
82 // Additionally, enforceInterface does part of the work of
83 // propagating the StrictMode policy mask, populating the current
84 // IPCThreadState, which as an optimization may optionally be
85 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070086 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070087 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070088 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 void freeData();
91
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080092private:
93 const binder_size_t* objects() const;
94
95public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 size_t objectsCount() const;
97
98 status_t errorCheck() const;
99 void setError(status_t err);
100
101 status_t write(const void* data, size_t len);
102 void* writeInplace(size_t len);
103 status_t writeUnpadded(const void* data, size_t len);
104 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800105 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700107 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 status_t writeFloat(float val);
109 status_t writeDouble(double val);
110 status_t writeCString(const char* str);
111 status_t writeString8(const String8& str);
112 status_t writeString16(const String16& str);
Casey Dahlinb9872622015-11-25 15:09:45 -0800113 status_t writeString16(const std::unique_ptr<String16>& str);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 status_t writeString16(const char16_t* str, size_t len);
115 status_t writeStrongBinder(const sp<IBinder>& val);
116 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700117 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700118 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700119 status_t writeBool(bool val);
120 status_t writeChar(char16_t val);
121 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700122
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800123 // Take a UTF8 encoded string, convert to UTF16, write it to the parcel.
124 status_t writeUtf8AsUtf16(const std::string& str);
125 status_t writeUtf8AsUtf16(const std::unique_ptr<std::string>& str);
126
Casey Dahlinb9872622015-11-25 15:09:45 -0800127 status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700128 status_t writeByteVector(const std::vector<int8_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800129 status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700130 status_t writeInt32Vector(const std::vector<int32_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800131 status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700132 status_t writeInt64Vector(const std::vector<int64_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800133 status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700134 status_t writeFloatVector(const std::vector<float>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800135 status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700136 status_t writeDoubleVector(const std::vector<double>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800137 status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700138 status_t writeBoolVector(const std::vector<bool>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800139 status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700140 status_t writeCharVector(const std::vector<char16_t>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800141 status_t writeString16Vector(
142 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700143 status_t writeString16Vector(const std::vector<String16>& val);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800144 status_t writeUtf8VectorAsUtf16Vector(
145 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val);
146 status_t writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val);
Casey Dahlin451ff582015-10-19 18:12:18 -0700147
Casey Dahlinb9872622015-11-25 15:09:45 -0800148 status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800149 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
150
Mathias Agopiane1424282013-07-29 21:24:40 -0700151 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800152 status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val);
153 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800154 status_t writeParcelableVector(const std::vector<T>& val);
Casey Dahlinb9872622015-11-25 15:09:45 -0800155
156 template<typename T>
157 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable);
158
Christopher Wiley97f048d2015-11-19 06:49:05 -0800159 status_t writeParcelable(const Parcelable& parcelable);
160
161 template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700162 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
Mathias Agopian8683fca2012-08-12 19:37:16 -0700164 template<typename T>
165 status_t write(const LightFlattenable<T>& val);
166
167
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700168 // Place a native_handle into the parcel (the native_handle's file-
169 // descriptors are dup'ed, so it is safe to delete the native_handle
Casey Dahlin451ff582015-10-19 18:12:18 -0700170 // when this function returns).
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700171 // Doesn't take ownership of the native_handle.
172 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
174 // Place a file descriptor into the parcel. The given fd must remain
175 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700176 // The Parcel does not take ownership of the given fd unless you ask it to.
177 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178
179 // Place a file descriptor into the parcel. A dup of the fd is made, which
180 // will be closed once the parcel is destroyed.
181 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700182
Casey Dahlin06673e32015-11-23 13:24:23 -0800183 // Place a file descriptor into the parcel. This will not affect the
184 // semantics of the smart file descriptor. A new descriptor will be
185 // created, and will be closed when the parcel is destroyed.
186 status_t writeUniqueFileDescriptor(
187 const ScopedFd& fd);
188
189 // Place a vector of file desciptors into the parcel. Each descriptor is
190 // dup'd as in writeDupFileDescriptor
191 status_t writeUniqueFileDescriptorVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800192 const std::unique_ptr<std::vector<ScopedFd>>& val);
193 status_t writeUniqueFileDescriptorVector(
Casey Dahlin06673e32015-11-23 13:24:23 -0800194 const std::vector<ScopedFd>& val);
195
Jeff Brown5707dbf2011-09-23 21:17:56 -0700196 // Writes a blob to the parcel.
197 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800198 // transferred by way of an anonymous shared memory region. Prefer sending
199 // immutable blobs if possible since they may be subsequently transferred between
200 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700201 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800202 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
203
204 // Write an existing immutable blob file descriptor to the parcel.
205 // This allows the client to send the same blob to multiple processes
206 // as long as it keeps a dup of the blob file descriptor handy for later.
207 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
210
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700211 // Like Parcel.java's writeNoException(). Just writes a zero int32.
212 // Currently the native implementation doesn't do any of the StrictMode
213 // stack gathering and serialization that the Java implementation does.
214 status_t writeNoException();
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 void remove(size_t start, size_t amt);
217
218 status_t read(void* outData, size_t len) const;
219 const void* readInplace(size_t len) const;
220 int32_t readInt32() const;
221 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800222 uint32_t readUint32() const;
223 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 int64_t readInt64() const;
225 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700226 uint64_t readUint64() const;
227 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 float readFloat() const;
229 status_t readFloat(float *pArg) const;
230 double readDouble() const;
231 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700232 intptr_t readIntPtr() const;
233 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700234 bool readBool() const;
235 status_t readBool(bool *pArg) const;
236 char16_t readChar() const;
237 status_t readChar(char16_t *pArg) const;
238 int8_t readByte() const;
239 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800241 // Read a UTF16 encoded string, convert to UTF8
242 status_t readUtf8FromUtf16(std::string* str) const;
243 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 const char* readCString() const;
246 String8 readString8() const;
247 String16 readString16() const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700248 status_t readString16(String16* pArg) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800249 status_t readString16(std::unique_ptr<String16>* pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 const char16_t* readString16Inplace(size_t* outLen) const;
251 sp<IBinder> readStrongBinder() const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700252 status_t readStrongBinder(sp<IBinder>* val) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700254
Casey Dahlinf0c13772015-10-27 18:33:56 -0700255 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800256 status_t readParcelableVector(
257 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
258 template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800259 status_t readParcelableVector(std::vector<T>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800260
Christopher Wiley97f048d2015-11-19 06:49:05 -0800261 status_t readParcelable(Parcelable* parcelable) const;
262
263 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800264 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
265
266 template<typename T>
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800267 status_t readStrongBinder(sp<T>* val) const;
268
Casey Dahlinb9872622015-11-25 15:09:45 -0800269 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800270 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
Casey Dahlinf0c13772015-10-27 18:33:56 -0700271
Casey Dahlinb9872622015-11-25 15:09:45 -0800272 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700273 status_t readByteVector(std::vector<int8_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800274 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700275 status_t readInt32Vector(std::vector<int32_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800276 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700277 status_t readInt64Vector(std::vector<int64_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800278 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700279 status_t readFloatVector(std::vector<float>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800280 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700281 status_t readDoubleVector(std::vector<double>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800282 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700283 status_t readBoolVector(std::vector<bool>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800284 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700285 status_t readCharVector(std::vector<char16_t>* val) const;
Casey Dahlinb9872622015-11-25 15:09:45 -0800286 status_t readString16Vector(
287 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700288 status_t readString16Vector(std::vector<String16>* val) const;
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800289 status_t readUtf8VectorFromUtf16Vector(
290 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
291 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
Casey Dahlin451ff582015-10-19 18:12:18 -0700292
Mathias Agopiane1424282013-07-29 21:24:40 -0700293 template<typename T>
294 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700295
Mathias Agopian8683fca2012-08-12 19:37:16 -0700296 template<typename T>
297 status_t read(LightFlattenable<T>& val) const;
298
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700299 // Like Parcel.java's readExceptionCode(). Reads the first int32
300 // off of a Parcel's header, returning 0 or the negative error
301 // code on exceptions, but also deals with skipping over rich
302 // response headers. Callers should use this to read & parse the
303 // response headers rather than doing it by hand.
304 int32_t readExceptionCode() const;
305
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700306 // Retrieve native_handle from the parcel. This returns a copy of the
307 // parcel's native_handle (the caller takes ownership). The caller
308 // must free the native_handle with native_handle_close() and
309 // native_handle_delete().
310 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311
312
313 // Retrieve a file descriptor from the parcel. This returns the raw fd
314 // in the parcel, which you do not own -- use dup() to get your own copy.
315 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700316
Casey Dahlin06673e32015-11-23 13:24:23 -0800317 // Retrieve a smart file descriptor from the parcel.
318 status_t readUniqueFileDescriptor(
319 ScopedFd* val) const;
320
321
322 // Retrieve a vector of smart file descriptors from the parcel.
323 status_t readUniqueFileDescriptorVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800324 std::unique_ptr<std::vector<ScopedFd>>* val) const;
325 status_t readUniqueFileDescriptorVector(
Casey Dahlin06673e32015-11-23 13:24:23 -0800326 std::vector<ScopedFd>* val) const;
327
Jeff Brown5707dbf2011-09-23 21:17:56 -0700328 // Reads a blob from the parcel.
329 // The caller should call release() on the blob after reading its contents.
330 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 const flat_binder_object* readObject(bool nullMetaData) const;
333
334 // Explicitly close all file descriptors in the parcel.
335 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800336
337 // Debugging: get metrics on current allocations.
338 static size_t getGlobalAllocSize();
339 static size_t getGlobalAllocCount();
340
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800341private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 typedef void (*release_func)(Parcel* parcel,
343 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800344 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 void* cookie);
346
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800347 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800349 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 size_t ipcObjectsCount() const;
351 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800352 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 release_func relFunc, void* relCookie);
354
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800355public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358private:
359 Parcel(const Parcel& o);
360 Parcel& operator=(const Parcel& o);
361
362 status_t finishWrite(size_t len);
363 void releaseObjects();
364 void acquireObjects();
365 status_t growData(size_t len);
366 status_t restartWrite(size_t desired);
367 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000368 status_t writePointer(uintptr_t val);
369 status_t readPointer(uintptr_t *pArg) const;
370 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 void freeDataNoInit();
372 void initState();
373 void scanForFds() const;
374
Andreas Huber84a6d042009-08-17 13:33:27 -0700375 template<class T>
376 status_t readAligned(T *pArg) const;
377
378 template<class T> T readAligned() const;
379
380 template<class T>
381 status_t writeAligned(T val);
382
Casey Dahlinb9872622015-11-25 15:09:45 -0800383 status_t writeRawNullableParcelable(const Parcelable*
384 parcelable);
385
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800386 template<typename T, typename U>
387 status_t unsafeReadTypedVector(std::vector<T>* val,
388 status_t(Parcel::*read_func)(U*) const) const;
389 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800390 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
391 status_t(Parcel::*read_func)(T*) const) const;
392 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800393 status_t readTypedVector(std::vector<T>* val,
394 status_t(Parcel::*read_func)(T*) const) const;
395 template<typename T, typename U>
396 status_t unsafeWriteTypedVector(const std::vector<T>& val,
397 status_t(Parcel::*write_func)(U));
398 template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800399 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
400 status_t(Parcel::*write_func)(const T&));
401 template<typename T>
402 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
403 status_t(Parcel::*write_func)(T));
404 template<typename T>
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800405 status_t writeTypedVector(const std::vector<T>& val,
406 status_t(Parcel::*write_func)(const T&));
407 template<typename T>
408 status_t writeTypedVector(const std::vector<T>& val,
409 status_t(Parcel::*write_func)(T));
410
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411 status_t mError;
412 uint8_t* mData;
413 size_t mDataSize;
414 size_t mDataCapacity;
415 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800416 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 size_t mObjectsSize;
418 size_t mObjectsCapacity;
419 mutable size_t mNextObjectHint;
420
421 mutable bool mFdsKnown;
422 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400423 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424
425 release_func mOwner;
426 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700427
428 class Blob {
429 public:
430 Blob();
431 ~Blob();
432
Jeff Brown13b16042014-11-11 16:44:25 -0800433 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700434 void release();
435 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800436 inline int fd() const { return mFd; };
437 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700438
439 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800440 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700441
Jeff Brown13b16042014-11-11 16:44:25 -0800442 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700443 void* mData;
444 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800445 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700446 };
447
Mathias Agopiane1424282013-07-29 21:24:40 -0700448 class FlattenableHelperInterface {
449 protected:
450 ~FlattenableHelperInterface() { }
451 public:
452 virtual size_t getFlattenedSize() const = 0;
453 virtual size_t getFdCount() const = 0;
454 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
455 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
456 };
457
458 template<typename T>
459 class FlattenableHelper : public FlattenableHelperInterface {
460 friend class Parcel;
461 const Flattenable<T>& val;
462 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
463
464 public:
465 virtual size_t getFlattenedSize() const {
466 return val.getFlattenedSize();
467 }
468 virtual size_t getFdCount() const {
469 return val.getFdCount();
470 }
471 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
472 return val.flatten(buffer, size, fds, count);
473 }
474 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
475 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
476 }
477 };
478 status_t write(const FlattenableHelperInterface& val);
479 status_t read(FlattenableHelperInterface& val) const;
480
Jeff Brown5707dbf2011-09-23 21:17:56 -0700481public:
482 class ReadableBlob : public Blob {
483 friend class Parcel;
484 public:
485 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800486 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700487 };
488
489 class WritableBlob : public Blob {
490 friend class Parcel;
491 public:
492 inline void* data() { return mData; }
493 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400494
495private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700496 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400497
498public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700499 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400500 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700501 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502};
503
504// ---------------------------------------------------------------------------
505
Mathias Agopian8683fca2012-08-12 19:37:16 -0700506template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700507status_t Parcel::write(const Flattenable<T>& val) {
508 const FlattenableHelper<T> helper(val);
509 return write(helper);
510}
511
512template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700513status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700514 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700515 if (!val.isFixedSize()) {
516 status_t err = writeInt32(size);
517 if (err != NO_ERROR) {
518 return err;
519 }
520 }
Mathias Agopian20985172012-08-31 14:25:22 -0700521 if (size) {
522 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700523 if (buffer == NULL)
524 return NO_MEMORY;
525 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700526 }
527 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700528}
529
530template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700531status_t Parcel::read(Flattenable<T>& val) const {
532 FlattenableHelper<T> helper(val);
533 return read(helper);
534}
535
536template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700537status_t Parcel::read(LightFlattenable<T>& val) const {
538 size_t size;
539 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700540 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700541 } else {
542 int32_t s;
543 status_t err = readInt32(&s);
544 if (err != NO_ERROR) {
545 return err;
546 }
547 size = s;
548 }
Mathias Agopian20985172012-08-31 14:25:22 -0700549 if (size) {
550 void const* buffer = readInplace(size);
551 return buffer == NULL ? NO_MEMORY :
552 val.unflatten(buffer, size);
553 }
554 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700555}
556
Casey Dahlinf0c13772015-10-27 18:33:56 -0700557template<typename T>
558status_t Parcel::readStrongBinder(sp<T>* val) const {
559 sp<IBinder> tmp;
560 status_t ret = readStrongBinder(&tmp);
561
562 if (ret == OK) {
563 *val = interface_cast<T>(tmp);
564
565 if (val->get() == nullptr) {
566 return UNKNOWN_ERROR;
567 }
568 }
569
570 return ret;
571}
572
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800573template<typename T, typename U>
574status_t Parcel::unsafeReadTypedVector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800575 std::vector<T>* val,
576 status_t(Parcel::*read_func)(U*) const) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800577 int32_t size;
578 status_t status = this->readInt32(&size);
579
580 if (status != OK) {
581 return status;
582 }
583
584 if (size < 0) {
585 return UNEXPECTED_NULL;
586 }
587
588 val->resize(size);
589
590 for (auto& v: *val) {
591 status = (this->*read_func)(&v);
592
593 if (status != OK) {
594 return status;
595 }
596 }
597
598 return OK;
599}
600
601template<typename T>
602status_t Parcel::readTypedVector(std::vector<T>* val,
603 status_t(Parcel::*read_func)(T*) const) const {
604 return unsafeReadTypedVector(val, read_func);
605}
606
Casey Dahlinb9872622015-11-25 15:09:45 -0800607template<typename T>
608status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
609 status_t(Parcel::*read_func)(T*) const) const {
610 const int32_t start = dataPosition();
611 int32_t size;
612 status_t status = readInt32(&size);
613 val->reset();
614
615 if (status != OK || size < 0) {
616 return status;
617 }
618
619 setDataPosition(start);
620 val->reset(new std::vector<T>());
621
622 status = unsafeReadTypedVector(val->get(), read_func);
623
624 if (status != OK) {
625 val->reset();
626 }
627
628 return status;
629}
630
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800631template<typename T, typename U>
632status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
633 status_t(Parcel::*write_func)(U)) {
634 if (val.size() > std::numeric_limits<int32_t>::max()) {
635 return BAD_VALUE;
636 }
637
638 status_t status = this->writeInt32(val.size());
639
640 if (status != OK) {
641 return status;
642 }
643
644 for (const auto& item : val) {
645 status = (this->*write_func)(item);
646
647 if (status != OK) {
648 return status;
649 }
650 }
651
652 return OK;
653}
654
655template<typename T>
656status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800657 status_t(Parcel::*write_func)(const T&)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800658 return unsafeWriteTypedVector(val, write_func);
659}
660
661template<typename T>
662status_t Parcel::writeTypedVector(const std::vector<T>& val,
Casey Dahlinb9872622015-11-25 15:09:45 -0800663 status_t(Parcel::*write_func)(T)) {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800664 return unsafeWriteTypedVector(val, write_func);
665}
666
Christopher Wiley97f048d2015-11-19 06:49:05 -0800667template<typename T>
Casey Dahlinb9872622015-11-25 15:09:45 -0800668status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
669 status_t(Parcel::*write_func)(const T&)) {
670 if (val.get() == nullptr) {
671 return this->writeInt32(-1);
672 }
673
674 return unsafeWriteTypedVector(*val, write_func);
675}
676
677template<typename T>
678status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
679 status_t(Parcel::*write_func)(T)) {
680 if (val.get() == nullptr) {
681 return this->writeInt32(-1);
682 }
683
684 return unsafeWriteTypedVector(*val, write_func);
685}
686
687template<typename T>
Christopher Wiley97f048d2015-11-19 06:49:05 -0800688status_t Parcel::readParcelableVector(std::vector<T>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -0800689 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
690}
691
692template<typename T>
693status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
694 const int32_t start = dataPosition();
695 int32_t size;
696 status_t status = readInt32(&size);
697 val->reset();
698
699 if (status != OK || size < 0) {
700 return status;
701 }
702
703 setDataPosition(start);
704 val->reset(new std::vector<T>());
705
706 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable);
707
708 if (status != OK) {
709 val->reset();
710 }
711
712 return status;
713}
714
715template<typename T>
716status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
717 const int32_t start = dataPosition();
718 int32_t present;
719 status_t status = readInt32(&present);
720 parcelable->reset();
721
722 if (status != OK || !present) {
723 return status;
724 }
725
726 setDataPosition(start);
727 parcelable->reset(new T());
728
729 status = readParcelable(parcelable->get());
730
731 if (status != OK) {
732 parcelable->reset();
733 }
734
735 return status;
736}
737
738template<typename T>
739status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
740 return writeRawNullableParcelable(parcelable.get());
Christopher Wiley97f048d2015-11-19 06:49:05 -0800741}
742
743template<typename T>
744status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -0800745 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
746}
747
748template<typename T>
749status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
750 if (val.get() == nullptr) {
751 return this->writeInt32(-1);
752 }
753
754 return unsafeWriteTypedVector(*val, &Parcel::writeParcelable);
Christopher Wiley97f048d2015-11-19 06:49:05 -0800755}
756
Mathias Agopian8683fca2012-08-12 19:37:16 -0700757// ---------------------------------------------------------------------------
758
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
760{
761 parcel.print(to);
762 return to;
763}
764
765// ---------------------------------------------------------------------------
766
767// Generic acquire and release of objects.
768void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000769 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770void release_object(const sp<ProcessState>& proc,
Ian Pedowitz68803072015-10-22 22:08:10 +0000771 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772
773void flatten_binder(const sp<ProcessState>& proc,
774 const sp<IBinder>& binder, flat_binder_object* out);
775void flatten_binder(const sp<ProcessState>& proc,
776 const wp<IBinder>& binder, flat_binder_object* out);
777status_t unflatten_binder(const sp<ProcessState>& proc,
778 const flat_binder_object& flat, sp<IBinder>* out);
779status_t unflatten_binder(const sp<ProcessState>& proc,
780 const flat_binder_object& flat, wp<IBinder>* out);
781
782}; // namespace android
783
784// ---------------------------------------------------------------------------
785
786#endif // ANDROID_PARCEL_H