blob: 9bf62a3a8edfbcee41e2928bd77e652a980732fb [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
20#include <cutils/native_handle.h>
21#include <utils/Errors.h>
22#include <utils/RefBase.h>
23#include <utils/String16.h>
24#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070025#include <utils/Flattenable.h>
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080026#include <linux/binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28// ---------------------------------------------------------------------------
29namespace android {
30
Mathias Agopiane1424282013-07-29 21:24:40 -070031template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070032template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070034class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035class ProcessState;
36class String8;
37class TextOutput;
38
Mathias Agopiane1424282013-07-29 21:24:40 -070039class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000040 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070042 class ReadableBlob;
43 class WritableBlob;
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 Parcel();
46 ~Parcel();
47
48 const uint8_t* data() const;
49 size_t dataSize() const;
50 size_t dataAvail() const;
51 size_t dataPosition() const;
52 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 status_t setDataSize(size_t size);
55 void setDataPosition(size_t pos) const;
56 status_t setDataCapacity(size_t size);
57
58 status_t setData(const uint8_t* buffer, size_t len);
59
Andreas Huber51faf462011-04-13 10:21:56 -070060 status_t appendFrom(const Parcel *parcel,
61 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Jeff Brown13b16042014-11-11 16:44:25 -080063 bool allowFds() const;
Dianne Hackborn7746cc32011-10-03 21:09:35 -070064 bool pushAllowFds(bool allowFds);
65 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 bool hasFileDescriptors() const;
68
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070069 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070072 // Parses the RPC header, returning true if the interface name
73 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070074 //
75 // Additionally, enforceInterface does part of the work of
76 // propagating the StrictMode policy mask, populating the current
77 // IPCThreadState, which as an optimization may optionally be
78 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070079 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070080 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070081 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 void freeData();
84
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080085private:
86 const binder_size_t* objects() const;
87
88public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 size_t objectsCount() const;
90
91 status_t errorCheck() const;
92 void setError(status_t err);
93
94 status_t write(const void* data, size_t len);
95 void* writeInplace(size_t len);
96 status_t writeUnpadded(const void* data, size_t len);
97 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -080098 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700100 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 status_t writeFloat(float val);
102 status_t writeDouble(double val);
103 status_t writeCString(const char* str);
104 status_t writeString8(const String8& str);
105 status_t writeString16(const String16& str);
106 status_t writeString16(const char16_t* str, size_t len);
107 status_t writeStrongBinder(const sp<IBinder>& val);
108 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700109 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700110 status_t writeByteArray(size_t len, const uint8_t *val);
Casey Dahlind6848f52015-10-15 15:44:59 -0700111 status_t writeBool(bool val);
112 status_t writeChar(char16_t val);
113 status_t writeByte(int8_t val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700114
115 template<typename T>
116 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopian8683fca2012-08-12 19:37:16 -0700118 template<typename T>
119 status_t write(const LightFlattenable<T>& val);
120
121
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700122 // Place a native_handle into the parcel (the native_handle's file-
123 // descriptors are dup'ed, so it is safe to delete the native_handle
124 // when this function returns).
125 // Doesn't take ownership of the native_handle.
126 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
128 // Place a file descriptor into the parcel. The given fd must remain
129 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700130 // The Parcel does not take ownership of the given fd unless you ask it to.
131 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132
133 // Place a file descriptor into the parcel. A dup of the fd is made, which
134 // will be closed once the parcel is destroyed.
135 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700136
137 // Writes a blob to the parcel.
138 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800139 // transferred by way of an anonymous shared memory region. Prefer sending
140 // immutable blobs if possible since they may be subsequently transferred between
141 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700142 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800143 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
144
145 // Write an existing immutable blob file descriptor to the parcel.
146 // This allows the client to send the same blob to multiple processes
147 // as long as it keeps a dup of the blob file descriptor handy for later.
148 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
151
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700152 // Like Parcel.java's writeNoException(). Just writes a zero int32.
153 // Currently the native implementation doesn't do any of the StrictMode
154 // stack gathering and serialization that the Java implementation does.
155 status_t writeNoException();
156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 void remove(size_t start, size_t amt);
158
159 status_t read(void* outData, size_t len) const;
160 const void* readInplace(size_t len) const;
161 int32_t readInt32() const;
162 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800163 uint32_t readUint32() const;
164 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 int64_t readInt64() const;
166 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700167 uint64_t readUint64() const;
168 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 float readFloat() const;
170 status_t readFloat(float *pArg) const;
171 double readDouble() const;
172 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700173 intptr_t readIntPtr() const;
174 status_t readIntPtr(intptr_t *pArg) const;
Casey Dahlind6848f52015-10-15 15:44:59 -0700175 bool readBool() const;
176 status_t readBool(bool *pArg) const;
177 char16_t readChar() const;
178 status_t readChar(char16_t *pArg) const;
179 int8_t readByte() const;
180 status_t readByte(int8_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181
182 const char* readCString() const;
183 String8 readString8() const;
184 String16 readString16() const;
185 const char16_t* readString16Inplace(size_t* outLen) const;
186 sp<IBinder> readStrongBinder() const;
187 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700188
189 template<typename T>
190 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700191
Mathias Agopian8683fca2012-08-12 19:37:16 -0700192 template<typename T>
193 status_t read(LightFlattenable<T>& val) const;
194
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700195 // Like Parcel.java's readExceptionCode(). Reads the first int32
196 // off of a Parcel's header, returning 0 or the negative error
197 // code on exceptions, but also deals with skipping over rich
198 // response headers. Callers should use this to read & parse the
199 // response headers rather than doing it by hand.
200 int32_t readExceptionCode() const;
201
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700202 // Retrieve native_handle from the parcel. This returns a copy of the
203 // parcel's native_handle (the caller takes ownership). The caller
204 // must free the native_handle with native_handle_close() and
205 // native_handle_delete().
206 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
208
209 // Retrieve a file descriptor from the parcel. This returns the raw fd
210 // in the parcel, which you do not own -- use dup() to get your own copy.
211 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700212
213 // Reads a blob from the parcel.
214 // The caller should call release() on the blob after reading its contents.
215 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 const flat_binder_object* readObject(bool nullMetaData) const;
218
219 // Explicitly close all file descriptors in the parcel.
220 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800221
222 // Debugging: get metrics on current allocations.
223 static size_t getGlobalAllocSize();
224 static size_t getGlobalAllocCount();
225
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 typedef void (*release_func)(Parcel* parcel,
228 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 void* cookie);
231
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800232 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800234 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 size_t ipcObjectsCount() const;
236 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800237 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 release_func relFunc, void* relCookie);
239
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800240public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243private:
244 Parcel(const Parcel& o);
245 Parcel& operator=(const Parcel& o);
246
247 status_t finishWrite(size_t len);
248 void releaseObjects();
249 void acquireObjects();
250 status_t growData(size_t len);
251 status_t restartWrite(size_t desired);
252 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000253 status_t writePointer(uintptr_t val);
254 status_t readPointer(uintptr_t *pArg) const;
255 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 void freeDataNoInit();
257 void initState();
258 void scanForFds() const;
259
Andreas Huber84a6d042009-08-17 13:33:27 -0700260 template<class T>
261 status_t readAligned(T *pArg) const;
262
263 template<class T> T readAligned() const;
264
265 template<class T>
266 status_t writeAligned(T val);
267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 status_t mError;
269 uint8_t* mData;
270 size_t mDataSize;
271 size_t mDataCapacity;
272 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 size_t mObjectsSize;
275 size_t mObjectsCapacity;
276 mutable size_t mNextObjectHint;
277
278 mutable bool mFdsKnown;
279 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400280 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
282 release_func mOwner;
283 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700284
285 class Blob {
286 public:
287 Blob();
288 ~Blob();
289
Jeff Brown13b16042014-11-11 16:44:25 -0800290 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700291 void release();
292 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800293 inline int fd() const { return mFd; };
294 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700295
296 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800297 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700298
Jeff Brown13b16042014-11-11 16:44:25 -0800299 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700300 void* mData;
301 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800302 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700303 };
304
Mathias Agopiane1424282013-07-29 21:24:40 -0700305 class FlattenableHelperInterface {
306 protected:
307 ~FlattenableHelperInterface() { }
308 public:
309 virtual size_t getFlattenedSize() const = 0;
310 virtual size_t getFdCount() const = 0;
311 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
312 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
313 };
314
315 template<typename T>
316 class FlattenableHelper : public FlattenableHelperInterface {
317 friend class Parcel;
318 const Flattenable<T>& val;
319 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
320
321 public:
322 virtual size_t getFlattenedSize() const {
323 return val.getFlattenedSize();
324 }
325 virtual size_t getFdCount() const {
326 return val.getFdCount();
327 }
328 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
329 return val.flatten(buffer, size, fds, count);
330 }
331 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
332 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
333 }
334 };
335 status_t write(const FlattenableHelperInterface& val);
336 status_t read(FlattenableHelperInterface& val) const;
337
Jeff Brown5707dbf2011-09-23 21:17:56 -0700338public:
339 class ReadableBlob : public Blob {
340 friend class Parcel;
341 public:
342 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800343 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700344 };
345
346 class WritableBlob : public Blob {
347 friend class Parcel;
348 public:
349 inline void* data() { return mData; }
350 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400351
352private:
353 size_t mBlobAshmemSize;
354
355public:
356 size_t getBlobAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357};
358
359// ---------------------------------------------------------------------------
360
Mathias Agopian8683fca2012-08-12 19:37:16 -0700361template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700362status_t Parcel::write(const Flattenable<T>& val) {
363 const FlattenableHelper<T> helper(val);
364 return write(helper);
365}
366
367template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700368status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700369 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700370 if (!val.isFixedSize()) {
371 status_t err = writeInt32(size);
372 if (err != NO_ERROR) {
373 return err;
374 }
375 }
Mathias Agopian20985172012-08-31 14:25:22 -0700376 if (size) {
377 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700378 if (buffer == NULL)
379 return NO_MEMORY;
380 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700381 }
382 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700383}
384
385template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700386status_t Parcel::read(Flattenable<T>& val) const {
387 FlattenableHelper<T> helper(val);
388 return read(helper);
389}
390
391template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700392status_t Parcel::read(LightFlattenable<T>& val) const {
393 size_t size;
394 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700395 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700396 } else {
397 int32_t s;
398 status_t err = readInt32(&s);
399 if (err != NO_ERROR) {
400 return err;
401 }
402 size = s;
403 }
Mathias Agopian20985172012-08-31 14:25:22 -0700404 if (size) {
405 void const* buffer = readInplace(size);
406 return buffer == NULL ? NO_MEMORY :
407 val.unflatten(buffer, size);
408 }
409 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700410}
411
412// ---------------------------------------------------------------------------
413
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
415{
416 parcel.print(to);
417 return to;
418}
419
420// ---------------------------------------------------------------------------
421
422// Generic acquire and release of objects.
423void acquire_object(const sp<ProcessState>& proc,
424 const flat_binder_object& obj, const void* who);
425void release_object(const sp<ProcessState>& proc,
426 const flat_binder_object& obj, const void* who);
427
428void flatten_binder(const sp<ProcessState>& proc,
429 const sp<IBinder>& binder, flat_binder_object* out);
430void flatten_binder(const sp<ProcessState>& proc,
431 const wp<IBinder>& binder, flat_binder_object* out);
432status_t unflatten_binder(const sp<ProcessState>& proc,
433 const flat_binder_object& flat, sp<IBinder>* out);
434status_t unflatten_binder(const sp<ProcessState>& proc,
435 const flat_binder_object& flat, wp<IBinder>* out);
436
437}; // namespace android
438
439// ---------------------------------------------------------------------------
440
441#endif // ANDROID_PARCEL_H