blob: 3ff95d2827250d897f977250fc2e4dbdba3831c6 [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 Agopian8683fca2012-08-12 19:37:16 -070030template <typename T> class LightFlattenable;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070031class Flattenable;
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
40class Parcel
41{
42public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070043 class ReadableBlob;
44 class WritableBlob;
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046 Parcel();
47 ~Parcel();
48
49 const uint8_t* data() const;
50 size_t dataSize() const;
51 size_t dataAvail() const;
52 size_t dataPosition() const;
53 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040054
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055 status_t setDataSize(size_t size);
56 void setDataPosition(size_t pos) const;
57 status_t setDataCapacity(size_t size);
58
59 status_t setData(const uint8_t* buffer, size_t len);
60
Andreas Huber51faf462011-04-13 10:21:56 -070061 status_t appendFrom(const Parcel *parcel,
62 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
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
85 const size_t* objects() const;
86 size_t objectsCount() const;
87
88 status_t errorCheck() const;
89 void setError(status_t err);
90
91 status_t write(const void* data, size_t len);
92 void* writeInplace(size_t len);
93 status_t writeUnpadded(const void* data, size_t len);
94 status_t writeInt32(int32_t val);
95 status_t writeInt64(int64_t val);
96 status_t writeFloat(float val);
97 status_t writeDouble(double val);
Andreas Huber84a6d042009-08-17 13:33:27 -070098 status_t writeIntPtr(intptr_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 status_t writeCString(const char* str);
100 status_t writeString8(const String8& str);
101 status_t writeString16(const String16& str);
102 status_t writeString16(const char16_t* str, size_t len);
103 status_t writeStrongBinder(const sp<IBinder>& val);
104 status_t writeWeakBinder(const wp<IBinder>& val);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800105 status_t write(const Flattenable& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
Mathias Agopian8683fca2012-08-12 19:37:16 -0700107 template<typename T>
108 status_t write(const LightFlattenable<T>& val);
109
110
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700111 // Place a native_handle into the parcel (the native_handle's file-
112 // descriptors are dup'ed, so it is safe to delete the native_handle
113 // when this function returns).
114 // Doesn't take ownership of the native_handle.
115 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
117 // Place a file descriptor into the parcel. The given fd must remain
118 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700119 // The Parcel does not take ownership of the given fd unless you ask it to.
120 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121
122 // Place a file descriptor into the parcel. A dup of the fd is made, which
123 // will be closed once the parcel is destroyed.
124 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700125
126 // Writes a blob to the parcel.
127 // If the blob is small, then it is stored in-place, otherwise it is
128 // transferred by way of an anonymous shared memory region.
129 // The caller should call release() on the blob after writing its contents.
130 status_t writeBlob(size_t len, WritableBlob* outBlob);
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
133
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700134 // Like Parcel.java's writeNoException(). Just writes a zero int32.
135 // Currently the native implementation doesn't do any of the StrictMode
136 // stack gathering and serialization that the Java implementation does.
137 status_t writeNoException();
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 void remove(size_t start, size_t amt);
140
141 status_t read(void* outData, size_t len) const;
142 const void* readInplace(size_t len) const;
143 int32_t readInt32() const;
144 status_t readInt32(int32_t *pArg) const;
145 int64_t readInt64() const;
146 status_t readInt64(int64_t *pArg) const;
147 float readFloat() const;
148 status_t readFloat(float *pArg) const;
149 double readDouble() const;
150 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700151 intptr_t readIntPtr() const;
152 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153
154 const char* readCString() const;
155 String8 readString8() const;
156 String16 readString16() const;
157 const char16_t* readString16Inplace(size_t* outLen) const;
158 sp<IBinder> readStrongBinder() const;
159 wp<IBinder> readWeakBinder() const;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800160 status_t read(Flattenable& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700161
Mathias Agopian8683fca2012-08-12 19:37:16 -0700162 template<typename T>
163 status_t read(LightFlattenable<T>& val) const;
164
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700165 // Like Parcel.java's readExceptionCode(). Reads the first int32
166 // off of a Parcel's header, returning 0 or the negative error
167 // code on exceptions, but also deals with skipping over rich
168 // response headers. Callers should use this to read & parse the
169 // response headers rather than doing it by hand.
170 int32_t readExceptionCode() const;
171
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700172 // Retrieve native_handle from the parcel. This returns a copy of the
173 // parcel's native_handle (the caller takes ownership). The caller
174 // must free the native_handle with native_handle_close() and
175 // native_handle_delete().
176 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177
178
179 // Retrieve a file descriptor from the parcel. This returns the raw fd
180 // in the parcel, which you do not own -- use dup() to get your own copy.
181 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700182
183 // Reads a blob from the parcel.
184 // The caller should call release() on the blob after reading its contents.
185 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 const flat_binder_object* readObject(bool nullMetaData) const;
188
189 // Explicitly close all file descriptors in the parcel.
190 void closeFileDescriptors();
191
192 typedef void (*release_func)(Parcel* parcel,
193 const uint8_t* data, size_t dataSize,
194 const size_t* objects, size_t objectsSize,
195 void* cookie);
196
197 const uint8_t* ipcData() const;
198 size_t ipcDataSize() const;
199 const size_t* ipcObjects() const;
200 size_t ipcObjectsCount() const;
201 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
202 const size_t* objects, size_t objectsCount,
203 release_func relFunc, void* relCookie);
204
205 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207private:
208 Parcel(const Parcel& o);
209 Parcel& operator=(const Parcel& o);
210
211 status_t finishWrite(size_t len);
212 void releaseObjects();
213 void acquireObjects();
214 status_t growData(size_t len);
215 status_t restartWrite(size_t desired);
216 status_t continueWrite(size_t desired);
217 void freeDataNoInit();
218 void initState();
219 void scanForFds() const;
220
Andreas Huber84a6d042009-08-17 13:33:27 -0700221 template<class T>
222 status_t readAligned(T *pArg) const;
223
224 template<class T> T readAligned() const;
225
226 template<class T>
227 status_t writeAligned(T val);
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 status_t mError;
230 uint8_t* mData;
231 size_t mDataSize;
232 size_t mDataCapacity;
233 mutable size_t mDataPos;
234 size_t* mObjects;
235 size_t mObjectsSize;
236 size_t mObjectsCapacity;
237 mutable size_t mNextObjectHint;
238
239 mutable bool mFdsKnown;
240 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400241 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242
243 release_func mOwner;
244 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700245
246 class Blob {
247 public:
248 Blob();
249 ~Blob();
250
251 void release();
252 inline size_t size() const { return mSize; }
253
254 protected:
255 void init(bool mapped, void* data, size_t size);
256 void clear();
257
258 bool mMapped;
259 void* mData;
260 size_t mSize;
261 };
262
263public:
264 class ReadableBlob : public Blob {
265 friend class Parcel;
266 public:
267 inline const void* data() const { return mData; }
268 };
269
270 class WritableBlob : public Blob {
271 friend class Parcel;
272 public:
273 inline void* data() { return mData; }
274 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275};
276
277// ---------------------------------------------------------------------------
278
Mathias Agopian8683fca2012-08-12 19:37:16 -0700279template<typename T>
280status_t Parcel::write(const LightFlattenable<T>& val) {
281 size_t size(val.getSize());
282 if (!val.isFixedSize()) {
283 status_t err = writeInt32(size);
284 if (err != NO_ERROR) {
285 return err;
286 }
287 }
Mathias Agopian20985172012-08-31 14:25:22 -0700288 if (size) {
289 void* buffer = writeInplace(size);
290 return buffer == NULL ? NO_MEMORY :
291 val.flatten(buffer);
292 }
293 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700294}
295
296template<typename T>
297status_t Parcel::read(LightFlattenable<T>& val) const {
298 size_t size;
299 if (val.isFixedSize()) {
300 size = val.getSize();
301 } else {
302 int32_t s;
303 status_t err = readInt32(&s);
304 if (err != NO_ERROR) {
305 return err;
306 }
307 size = s;
308 }
Mathias Agopian20985172012-08-31 14:25:22 -0700309 if (size) {
310 void const* buffer = readInplace(size);
311 return buffer == NULL ? NO_MEMORY :
312 val.unflatten(buffer, size);
313 }
314 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700315}
316
317// ---------------------------------------------------------------------------
318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
320{
321 parcel.print(to);
322 return to;
323}
324
325// ---------------------------------------------------------------------------
326
327// Generic acquire and release of objects.
328void acquire_object(const sp<ProcessState>& proc,
329 const flat_binder_object& obj, const void* who);
330void release_object(const sp<ProcessState>& proc,
331 const flat_binder_object& obj, const void* who);
332
333void flatten_binder(const sp<ProcessState>& proc,
334 const sp<IBinder>& binder, flat_binder_object* out);
335void flatten_binder(const sp<ProcessState>& proc,
336 const wp<IBinder>& binder, flat_binder_object* out);
337status_t unflatten_binder(const sp<ProcessState>& proc,
338 const flat_binder_object& flat, sp<IBinder>* out);
339status_t unflatten_binder(const sp<ProcessState>& proc,
340 const flat_binder_object& flat, wp<IBinder>* out);
341
342}; // namespace android
343
344// ---------------------------------------------------------------------------
345
346#endif // ANDROID_PARCEL_H