blob: e770e6fcc29f74e55e2ceaaff1c2cace48b2c194 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27// ---------------------------------------------------------------------------
28namespace android {
29
Mathias Agopiane1424282013-07-29 21:24:40 -070030template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070031template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070033class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034class ProcessState;
35class String8;
36class TextOutput;
37
38struct flat_binder_object; // defined in support_p/binder_module.h
39
Mathias Agopiane1424282013-07-29 21:24:40 -070040class Parcel {
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
Dianne Hackborn7746cc32011-10-03 21:09:35 -070063 bool pushAllowFds(bool allowFds);
64 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 bool hasFileDescriptors() const;
67
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070068 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070071 // Parses the RPC header, returning true if the interface name
72 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070073 //
74 // Additionally, enforceInterface does part of the work of
75 // propagating the StrictMode policy mask, populating the current
76 // IPCThreadState, which as an optimization may optionally be
77 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070079 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070080 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 void freeData();
83
84 const size_t* objects() const;
85 size_t objectsCount() const;
86
87 status_t errorCheck() const;
88 void setError(status_t err);
89
90 status_t write(const void* data, size_t len);
91 void* writeInplace(size_t len);
92 status_t writeUnpadded(const void* data, size_t len);
93 status_t writeInt32(int32_t val);
94 status_t writeInt64(int64_t val);
95 status_t writeFloat(float val);
96 status_t writeDouble(double val);
Andreas Huber84a6d042009-08-17 13:33:27 -070097 status_t writeIntPtr(intptr_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 status_t writeCString(const char* str);
99 status_t writeString8(const String8& str);
100 status_t writeString16(const String16& str);
101 status_t writeString16(const char16_t* str, size_t len);
102 status_t writeStrongBinder(const sp<IBinder>& val);
103 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700104 status_t writeInt32Array(size_t len, const int32_t *val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700105
106 template<typename T>
107 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
Mathias Agopian8683fca2012-08-12 19:37:16 -0700109 template<typename T>
110 status_t write(const LightFlattenable<T>& val);
111
112
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700113 // Place a native_handle into the parcel (the native_handle's file-
114 // descriptors are dup'ed, so it is safe to delete the native_handle
115 // when this function returns).
116 // Doesn't take ownership of the native_handle.
117 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 // Place a file descriptor into the parcel. The given fd must remain
120 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700121 // The Parcel does not take ownership of the given fd unless you ask it to.
122 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
124 // Place a file descriptor into the parcel. A dup of the fd is made, which
125 // will be closed once the parcel is destroyed.
126 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700127
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700128 // Writes a raw fd and optional comm channel fd to the parcel as a ParcelFileDescriptor.
129 // A dup's of the fds are made, which will be closed once the parcel is destroyed.
130 // Null values are passed as -1.
131 status_t writeParcelFileDescriptor(int fd, int commChannel = -1);
132
Jeff Brown5707dbf2011-09-23 21:17:56 -0700133 // Writes a blob to the parcel.
134 // If the blob is small, then it is stored in-place, otherwise it is
135 // transferred by way of an anonymous shared memory region.
136 // The caller should call release() on the blob after writing its contents.
137 status_t writeBlob(size_t len, WritableBlob* outBlob);
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
140
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700141 // Like Parcel.java's writeNoException(). Just writes a zero int32.
142 // Currently the native implementation doesn't do any of the StrictMode
143 // stack gathering and serialization that the Java implementation does.
144 status_t writeNoException();
145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 void remove(size_t start, size_t amt);
147
148 status_t read(void* outData, size_t len) const;
149 const void* readInplace(size_t len) const;
150 int32_t readInt32() const;
151 status_t readInt32(int32_t *pArg) const;
152 int64_t readInt64() const;
153 status_t readInt64(int64_t *pArg) const;
154 float readFloat() const;
155 status_t readFloat(float *pArg) const;
156 double readDouble() const;
157 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700158 intptr_t readIntPtr() const;
159 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
161 const char* readCString() const;
162 String8 readString8() const;
163 String16 readString16() const;
164 const char16_t* readString16Inplace(size_t* outLen) const;
165 sp<IBinder> readStrongBinder() const;
166 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700167
168 template<typename T>
169 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700170
Mathias Agopian8683fca2012-08-12 19:37:16 -0700171 template<typename T>
172 status_t read(LightFlattenable<T>& val) const;
173
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700174 // Like Parcel.java's readExceptionCode(). Reads the first int32
175 // off of a Parcel's header, returning 0 or the negative error
176 // code on exceptions, but also deals with skipping over rich
177 // response headers. Callers should use this to read & parse the
178 // response headers rather than doing it by hand.
179 int32_t readExceptionCode() const;
180
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700181 // Retrieve native_handle from the parcel. This returns a copy of the
182 // parcel's native_handle (the caller takes ownership). The caller
183 // must free the native_handle with native_handle_close() and
184 // native_handle_delete().
185 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186
187
188 // Retrieve a file descriptor from the parcel. This returns the raw fd
189 // in the parcel, which you do not own -- use dup() to get your own copy.
190 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700191
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700192 // Reads a ParcelFileDescriptor from the parcel. Returns the raw fd as
193 // the result, and the optional comm channel fd in outCommChannel.
194 // Null values are returned as -1.
195 int readParcelFileDescriptor(int& outCommChannel) const;
196
Jeff Brown5707dbf2011-09-23 21:17:56 -0700197 // Reads a blob from the parcel.
198 // The caller should call release() on the blob after reading its contents.
199 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 const flat_binder_object* readObject(bool nullMetaData) const;
202
203 // Explicitly close all file descriptors in the parcel.
204 void closeFileDescriptors();
205
206 typedef void (*release_func)(Parcel* parcel,
207 const uint8_t* data, size_t dataSize,
208 const size_t* objects, size_t objectsSize,
209 void* cookie);
210
211 const uint8_t* ipcData() const;
212 size_t ipcDataSize() const;
213 const size_t* ipcObjects() const;
214 size_t ipcObjectsCount() const;
215 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
216 const size_t* objects, size_t objectsCount,
217 release_func relFunc, void* relCookie);
218
219 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221private:
222 Parcel(const Parcel& o);
223 Parcel& operator=(const Parcel& o);
224
225 status_t finishWrite(size_t len);
226 void releaseObjects();
227 void acquireObjects();
228 status_t growData(size_t len);
229 status_t restartWrite(size_t desired);
230 status_t continueWrite(size_t desired);
231 void freeDataNoInit();
232 void initState();
233 void scanForFds() const;
234
Andreas Huber84a6d042009-08-17 13:33:27 -0700235 template<class T>
236 status_t readAligned(T *pArg) const;
237
238 template<class T> T readAligned() const;
239
240 template<class T>
241 status_t writeAligned(T val);
242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 status_t mError;
244 uint8_t* mData;
245 size_t mDataSize;
246 size_t mDataCapacity;
247 mutable size_t mDataPos;
248 size_t* mObjects;
249 size_t mObjectsSize;
250 size_t mObjectsCapacity;
251 mutable size_t mNextObjectHint;
252
253 mutable bool mFdsKnown;
254 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400255 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256
257 release_func mOwner;
258 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700259
260 class Blob {
261 public:
262 Blob();
263 ~Blob();
264
265 void release();
266 inline size_t size() const { return mSize; }
267
268 protected:
269 void init(bool mapped, void* data, size_t size);
270 void clear();
271
272 bool mMapped;
273 void* mData;
274 size_t mSize;
275 };
276
Mathias Agopiane1424282013-07-29 21:24:40 -0700277 class FlattenableHelperInterface {
278 protected:
279 ~FlattenableHelperInterface() { }
280 public:
281 virtual size_t getFlattenedSize() const = 0;
282 virtual size_t getFdCount() const = 0;
283 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
284 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
285 };
286
287 template<typename T>
288 class FlattenableHelper : public FlattenableHelperInterface {
289 friend class Parcel;
290 const Flattenable<T>& val;
291 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
292
293 public:
294 virtual size_t getFlattenedSize() const {
295 return val.getFlattenedSize();
296 }
297 virtual size_t getFdCount() const {
298 return val.getFdCount();
299 }
300 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
301 return val.flatten(buffer, size, fds, count);
302 }
303 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
304 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
305 }
306 };
307 status_t write(const FlattenableHelperInterface& val);
308 status_t read(FlattenableHelperInterface& val) const;
309
Jeff Brown5707dbf2011-09-23 21:17:56 -0700310public:
311 class ReadableBlob : public Blob {
312 friend class Parcel;
313 public:
314 inline const void* data() const { return mData; }
315 };
316
317 class WritableBlob : public Blob {
318 friend class Parcel;
319 public:
320 inline void* data() { return mData; }
321 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322};
323
324// ---------------------------------------------------------------------------
325
Mathias Agopian8683fca2012-08-12 19:37:16 -0700326template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700327status_t Parcel::write(const Flattenable<T>& val) {
328 const FlattenableHelper<T> helper(val);
329 return write(helper);
330}
331
332template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700333status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700334 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700335 if (!val.isFixedSize()) {
336 status_t err = writeInt32(size);
337 if (err != NO_ERROR) {
338 return err;
339 }
340 }
Mathias Agopian20985172012-08-31 14:25:22 -0700341 if (size) {
342 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700343 if (buffer == NULL)
344 return NO_MEMORY;
345 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700346 }
347 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700348}
349
350template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700351status_t Parcel::read(Flattenable<T>& val) const {
352 FlattenableHelper<T> helper(val);
353 return read(helper);
354}
355
356template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700357status_t Parcel::read(LightFlattenable<T>& val) const {
358 size_t size;
359 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700360 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700361 } else {
362 int32_t s;
363 status_t err = readInt32(&s);
364 if (err != NO_ERROR) {
365 return err;
366 }
367 size = s;
368 }
Mathias Agopian20985172012-08-31 14:25:22 -0700369 if (size) {
370 void const* buffer = readInplace(size);
371 return buffer == NULL ? NO_MEMORY :
372 val.unflatten(buffer, size);
373 }
374 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700375}
376
377// ---------------------------------------------------------------------------
378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
380{
381 parcel.print(to);
382 return to;
383}
384
385// ---------------------------------------------------------------------------
386
387// Generic acquire and release of objects.
388void acquire_object(const sp<ProcessState>& proc,
389 const flat_binder_object& obj, const void* who);
390void release_object(const sp<ProcessState>& proc,
391 const flat_binder_object& obj, const void* who);
392
393void flatten_binder(const sp<ProcessState>& proc,
394 const sp<IBinder>& binder, flat_binder_object* out);
395void flatten_binder(const sp<ProcessState>& proc,
396 const wp<IBinder>& binder, flat_binder_object* out);
397status_t unflatten_binder(const sp<ProcessState>& proc,
398 const flat_binder_object& flat, sp<IBinder>* out);
399status_t unflatten_binder(const sp<ProcessState>& proc,
400 const flat_binder_object& flat, wp<IBinder>* out);
401
402}; // namespace android
403
404// ---------------------------------------------------------------------------
405
406#endif // ANDROID_PARCEL_H