blob: 16cd6cf678fd5752ecfc43bcb2a1c5c70a037671 [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);
Mathias Agopiane1424282013-07-29 21:24:40 -0700111
112 template<typename T>
113 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114
Mathias Agopian8683fca2012-08-12 19:37:16 -0700115 template<typename T>
116 status_t write(const LightFlattenable<T>& val);
117
118
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700119 // Place a native_handle into the parcel (the native_handle's file-
120 // descriptors are dup'ed, so it is safe to delete the native_handle
121 // when this function returns).
122 // Doesn't take ownership of the native_handle.
123 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124
125 // Place a file descriptor into the parcel. The given fd must remain
126 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700127 // The Parcel does not take ownership of the given fd unless you ask it to.
128 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129
130 // Place a file descriptor into the parcel. A dup of the fd is made, which
131 // will be closed once the parcel is destroyed.
132 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700133
134 // Writes a blob to the parcel.
135 // If the blob is small, then it is stored in-place, otherwise it is
Jeff Brown13b16042014-11-11 16:44:25 -0800136 // transferred by way of an anonymous shared memory region. Prefer sending
137 // immutable blobs if possible since they may be subsequently transferred between
138 // processes without further copying whereas mutable blobs always need to be copied.
Jeff Brown5707dbf2011-09-23 21:17:56 -0700139 // The caller should call release() on the blob after writing its contents.
Jeff Brown13b16042014-11-11 16:44:25 -0800140 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
141
142 // Write an existing immutable blob file descriptor to the parcel.
143 // This allows the client to send the same blob to multiple processes
144 // as long as it keeps a dup of the blob file descriptor handy for later.
145 status_t writeDupImmutableBlobFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
148
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700149 // Like Parcel.java's writeNoException(). Just writes a zero int32.
150 // Currently the native implementation doesn't do any of the StrictMode
151 // stack gathering and serialization that the Java implementation does.
152 status_t writeNoException();
153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 void remove(size_t start, size_t amt);
155
156 status_t read(void* outData, size_t len) const;
157 const void* readInplace(size_t len) const;
158 int32_t readInt32() const;
159 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800160 uint32_t readUint32() const;
161 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 int64_t readInt64() const;
163 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700164 uint64_t readUint64() const;
165 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 float readFloat() const;
167 status_t readFloat(float *pArg) const;
168 double readDouble() const;
169 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700170 intptr_t readIntPtr() const;
171 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172
173 const char* readCString() const;
174 String8 readString8() const;
175 String16 readString16() const;
176 const char16_t* readString16Inplace(size_t* outLen) const;
177 sp<IBinder> readStrongBinder() const;
178 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700179
180 template<typename T>
181 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700182
Mathias Agopian8683fca2012-08-12 19:37:16 -0700183 template<typename T>
184 status_t read(LightFlattenable<T>& val) const;
185
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700186 // Like Parcel.java's readExceptionCode(). Reads the first int32
187 // off of a Parcel's header, returning 0 or the negative error
188 // code on exceptions, but also deals with skipping over rich
189 // response headers. Callers should use this to read & parse the
190 // response headers rather than doing it by hand.
191 int32_t readExceptionCode() const;
192
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700193 // Retrieve native_handle from the parcel. This returns a copy of the
194 // parcel's native_handle (the caller takes ownership). The caller
195 // must free the native_handle with native_handle_close() and
196 // native_handle_delete().
197 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198
199
200 // Retrieve a file descriptor from the parcel. This returns the raw fd
201 // in the parcel, which you do not own -- use dup() to get your own copy.
202 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700203
204 // Reads a blob from the parcel.
205 // The caller should call release() on the blob after reading its contents.
206 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 const flat_binder_object* readObject(bool nullMetaData) const;
209
210 // Explicitly close all file descriptors in the parcel.
211 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800212
213 // Debugging: get metrics on current allocations.
214 static size_t getGlobalAllocSize();
215 static size_t getGlobalAllocCount();
216
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800217private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 typedef void (*release_func)(Parcel* parcel,
219 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 void* cookie);
222
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800223 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 size_t ipcObjectsCount() const;
227 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 release_func relFunc, void* relCookie);
230
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234private:
235 Parcel(const Parcel& o);
236 Parcel& operator=(const Parcel& o);
237
238 status_t finishWrite(size_t len);
239 void releaseObjects();
240 void acquireObjects();
241 status_t growData(size_t len);
242 status_t restartWrite(size_t desired);
243 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000244 status_t writePointer(uintptr_t val);
245 status_t readPointer(uintptr_t *pArg) const;
246 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 void freeDataNoInit();
248 void initState();
249 void scanForFds() const;
250
Andreas Huber84a6d042009-08-17 13:33:27 -0700251 template<class T>
252 status_t readAligned(T *pArg) const;
253
254 template<class T> T readAligned() const;
255
256 template<class T>
257 status_t writeAligned(T val);
258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 status_t mError;
260 uint8_t* mData;
261 size_t mDataSize;
262 size_t mDataCapacity;
263 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800264 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 size_t mObjectsSize;
266 size_t mObjectsCapacity;
267 mutable size_t mNextObjectHint;
268
269 mutable bool mFdsKnown;
270 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400271 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272
273 release_func mOwner;
274 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700275
276 class Blob {
277 public:
278 Blob();
279 ~Blob();
280
Jeff Brown13b16042014-11-11 16:44:25 -0800281 void clear();
Jeff Brown5707dbf2011-09-23 21:17:56 -0700282 void release();
283 inline size_t size() const { return mSize; }
Jeff Brown13b16042014-11-11 16:44:25 -0800284 inline int fd() const { return mFd; };
285 inline bool isMutable() const { return mMutable; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700286
287 protected:
Jeff Brown13b16042014-11-11 16:44:25 -0800288 void init(int fd, void* data, size_t size, bool isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700289
Jeff Brown13b16042014-11-11 16:44:25 -0800290 int mFd; // owned by parcel so not closed when released
Jeff Brown5707dbf2011-09-23 21:17:56 -0700291 void* mData;
292 size_t mSize;
Jeff Brown13b16042014-11-11 16:44:25 -0800293 bool mMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700294 };
295
Mathias Agopiane1424282013-07-29 21:24:40 -0700296 class FlattenableHelperInterface {
297 protected:
298 ~FlattenableHelperInterface() { }
299 public:
300 virtual size_t getFlattenedSize() const = 0;
301 virtual size_t getFdCount() const = 0;
302 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
303 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
304 };
305
306 template<typename T>
307 class FlattenableHelper : public FlattenableHelperInterface {
308 friend class Parcel;
309 const Flattenable<T>& val;
310 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
311
312 public:
313 virtual size_t getFlattenedSize() const {
314 return val.getFlattenedSize();
315 }
316 virtual size_t getFdCount() const {
317 return val.getFdCount();
318 }
319 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
320 return val.flatten(buffer, size, fds, count);
321 }
322 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
323 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
324 }
325 };
326 status_t write(const FlattenableHelperInterface& val);
327 status_t read(FlattenableHelperInterface& val) const;
328
Jeff Brown5707dbf2011-09-23 21:17:56 -0700329public:
330 class ReadableBlob : public Blob {
331 friend class Parcel;
332 public:
333 inline const void* data() const { return mData; }
Jeff Brown13b16042014-11-11 16:44:25 -0800334 inline void* mutableData() { return isMutable() ? mData : NULL; }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700335 };
336
337 class WritableBlob : public Blob {
338 friend class Parcel;
339 public:
340 inline void* data() { return mData; }
341 };
Dan Sandleraa5c2342015-04-10 10:08:45 -0400342
343private:
Adrian Rooscbf37262015-10-22 16:12:53 -0700344 size_t mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -0400345
346public:
Adrian Roos6bb31142015-10-22 16:46:12 -0700347 // TODO: Remove once ABI can be changed.
Dan Sandleraa5c2342015-04-10 10:08:45 -0400348 size_t getBlobAshmemSize() const;
Adrian Rooscbf37262015-10-22 16:12:53 -0700349 size_t getOpenAshmemSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350};
351
352// ---------------------------------------------------------------------------
353
Mathias Agopian8683fca2012-08-12 19:37:16 -0700354template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700355status_t Parcel::write(const Flattenable<T>& val) {
356 const FlattenableHelper<T> helper(val);
357 return write(helper);
358}
359
360template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700361status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700362 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700363 if (!val.isFixedSize()) {
364 status_t err = writeInt32(size);
365 if (err != NO_ERROR) {
366 return err;
367 }
368 }
Mathias Agopian20985172012-08-31 14:25:22 -0700369 if (size) {
370 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700371 if (buffer == NULL)
372 return NO_MEMORY;
373 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700374 }
375 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700376}
377
378template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700379status_t Parcel::read(Flattenable<T>& val) const {
380 FlattenableHelper<T> helper(val);
381 return read(helper);
382}
383
384template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700385status_t Parcel::read(LightFlattenable<T>& val) const {
386 size_t size;
387 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700388 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700389 } else {
390 int32_t s;
391 status_t err = readInt32(&s);
392 if (err != NO_ERROR) {
393 return err;
394 }
395 size = s;
396 }
Mathias Agopian20985172012-08-31 14:25:22 -0700397 if (size) {
398 void const* buffer = readInplace(size);
399 return buffer == NULL ? NO_MEMORY :
400 val.unflatten(buffer, size);
401 }
402 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700403}
404
405// ---------------------------------------------------------------------------
406
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
408{
409 parcel.print(to);
410 return to;
411}
412
413// ---------------------------------------------------------------------------
414
415// Generic acquire and release of objects.
416void acquire_object(const sp<ProcessState>& proc,
Adrian Roos6bb31142015-10-22 16:46:12 -0700417 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418void release_object(const sp<ProcessState>& proc,
Adrian Roos6bb31142015-10-22 16:46:12 -0700419 const flat_binder_object& obj, const void* who);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420
421void flatten_binder(const sp<ProcessState>& proc,
422 const sp<IBinder>& binder, flat_binder_object* out);
423void flatten_binder(const sp<ProcessState>& proc,
424 const wp<IBinder>& binder, flat_binder_object* out);
425status_t unflatten_binder(const sp<ProcessState>& proc,
426 const flat_binder_object& flat, sp<IBinder>* out);
427status_t unflatten_binder(const sp<ProcessState>& proc,
428 const flat_binder_object& flat, wp<IBinder>* out);
429
430}; // namespace android
431
432// ---------------------------------------------------------------------------
433
434#endif // ANDROID_PARCEL_H