blob: 53a53c422fc542689af7cd8e48853f51d89fbb76 [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>
25
26// ---------------------------------------------------------------------------
27namespace android {
28
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070029class Flattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070031class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032class ProcessState;
33class String8;
34class TextOutput;
35
36struct flat_binder_object; // defined in support_p/binder_module.h
37
38class Parcel
39{
40public:
Jeff Brownf4c10882011-09-23 21:17:56 -070041 class ReadableBlob;
42 class WritableBlob;
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 Parcel();
45 ~Parcel();
46
47 const uint8_t* data() const;
48 size_t dataSize() const;
49 size_t dataAvail() const;
50 size_t dataPosition() const;
51 size_t dataCapacity() const;
52
53 status_t setDataSize(size_t size);
54 void setDataPosition(size_t pos) const;
55 status_t setDataCapacity(size_t size);
56
57 status_t setData(const uint8_t* buffer, size_t len);
58
Andreas Huber51faf462011-04-13 10:21:56 -070059 status_t appendFrom(const Parcel *parcel,
60 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
62 bool hasFileDescriptors() const;
63
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070064 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070066
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070067 // Parses the RPC header, returning true if the interface name
68 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070069 //
70 // Additionally, enforceInterface does part of the work of
71 // propagating the StrictMode policy mask, populating the current
72 // IPCThreadState, which as an optimization may optionally be
73 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070075 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070076 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 void freeData();
79
80 const size_t* objects() const;
81 size_t objectsCount() const;
82
83 status_t errorCheck() const;
84 void setError(status_t err);
85
86 status_t write(const void* data, size_t len);
87 void* writeInplace(size_t len);
88 status_t writeUnpadded(const void* data, size_t len);
89 status_t writeInt32(int32_t val);
90 status_t writeInt64(int64_t val);
91 status_t writeFloat(float val);
92 status_t writeDouble(double val);
Andreas Huber84a6d042009-08-17 13:33:27 -070093 status_t writeIntPtr(intptr_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 status_t writeCString(const char* str);
95 status_t writeString8(const String8& str);
96 status_t writeString16(const String16& str);
97 status_t writeString16(const char16_t* str, size_t len);
98 status_t writeStrongBinder(const sp<IBinder>& val);
99 status_t writeWeakBinder(const wp<IBinder>& val);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800100 status_t write(const Flattenable& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700102 // Place a native_handle into the parcel (the native_handle's file-
103 // descriptors are dup'ed, so it is safe to delete the native_handle
104 // when this function returns).
105 // Doesn't take ownership of the native_handle.
106 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // Place a file descriptor into the parcel. The given fd must remain
109 // valid for the lifetime of the parcel.
110 status_t writeFileDescriptor(int fd);
111
112 // Place a file descriptor into the parcel. A dup of the fd is made, which
113 // will be closed once the parcel is destroyed.
114 status_t writeDupFileDescriptor(int fd);
Jeff Brownf4c10882011-09-23 21:17:56 -0700115
116 // Writes a blob to the parcel.
117 // If the blob is small, then it is stored in-place, otherwise it is
118 // transferred by way of an anonymous shared memory region.
119 // The caller should call release() on the blob after writing its contents.
120 status_t writeBlob(size_t len, WritableBlob* outBlob);
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
123
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700124 // Like Parcel.java's writeNoException(). Just writes a zero int32.
125 // Currently the native implementation doesn't do any of the StrictMode
126 // stack gathering and serialization that the Java implementation does.
127 status_t writeNoException();
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 void remove(size_t start, size_t amt);
130
131 status_t read(void* outData, size_t len) const;
132 const void* readInplace(size_t len) const;
133 int32_t readInt32() const;
134 status_t readInt32(int32_t *pArg) const;
135 int64_t readInt64() const;
136 status_t readInt64(int64_t *pArg) const;
137 float readFloat() const;
138 status_t readFloat(float *pArg) const;
139 double readDouble() const;
140 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700141 intptr_t readIntPtr() const;
142 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143
144 const char* readCString() const;
145 String8 readString8() const;
146 String16 readString16() const;
147 const char16_t* readString16Inplace(size_t* outLen) const;
148 sp<IBinder> readStrongBinder() const;
149 wp<IBinder> readWeakBinder() const;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800150 status_t read(Flattenable& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700151
152 // Like Parcel.java's readExceptionCode(). Reads the first int32
153 // off of a Parcel's header, returning 0 or the negative error
154 // code on exceptions, but also deals with skipping over rich
155 // response headers. Callers should use this to read & parse the
156 // response headers rather than doing it by hand.
157 int32_t readExceptionCode() const;
158
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700159 // Retrieve native_handle from the parcel. This returns a copy of the
160 // parcel's native_handle (the caller takes ownership). The caller
161 // must free the native_handle with native_handle_close() and
162 // native_handle_delete().
163 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
165
166 // Retrieve a file descriptor from the parcel. This returns the raw fd
167 // in the parcel, which you do not own -- use dup() to get your own copy.
168 int readFileDescriptor() const;
Jeff Brownf4c10882011-09-23 21:17:56 -0700169
170 // Reads a blob from the parcel.
171 // The caller should call release() on the blob after reading its contents.
172 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 const flat_binder_object* readObject(bool nullMetaData) const;
175
176 // Explicitly close all file descriptors in the parcel.
177 void closeFileDescriptors();
178
179 typedef void (*release_func)(Parcel* parcel,
180 const uint8_t* data, size_t dataSize,
181 const size_t* objects, size_t objectsSize,
182 void* cookie);
183
184 const uint8_t* ipcData() const;
185 size_t ipcDataSize() const;
186 const size_t* ipcObjects() const;
187 size_t ipcObjectsCount() const;
188 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
189 const size_t* objects, size_t objectsCount,
190 release_func relFunc, void* relCookie);
191
192 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brownf4c10882011-09-23 21:17:56 -0700193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194private:
195 Parcel(const Parcel& o);
196 Parcel& operator=(const Parcel& o);
197
198 status_t finishWrite(size_t len);
199 void releaseObjects();
200 void acquireObjects();
201 status_t growData(size_t len);
202 status_t restartWrite(size_t desired);
203 status_t continueWrite(size_t desired);
204 void freeDataNoInit();
205 void initState();
206 void scanForFds() const;
207
Andreas Huber84a6d042009-08-17 13:33:27 -0700208 template<class T>
209 status_t readAligned(T *pArg) const;
210
211 template<class T> T readAligned() const;
212
213 template<class T>
214 status_t writeAligned(T val);
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 status_t mError;
217 uint8_t* mData;
218 size_t mDataSize;
219 size_t mDataCapacity;
220 mutable size_t mDataPos;
221 size_t* mObjects;
222 size_t mObjectsSize;
223 size_t mObjectsCapacity;
224 mutable size_t mNextObjectHint;
225
226 mutable bool mFdsKnown;
227 mutable bool mHasFds;
228
229 release_func mOwner;
230 void* mOwnerCookie;
Jeff Brownf4c10882011-09-23 21:17:56 -0700231
232 class Blob {
233 public:
234 Blob();
235 ~Blob();
236
237 void release();
238 inline size_t size() const { return mSize; }
239
240 protected:
241 void init(bool mapped, void* data, size_t size);
242 void clear();
243
244 bool mMapped;
245 void* mData;
246 size_t mSize;
247 };
248
249public:
250 class ReadableBlob : public Blob {
251 friend class Parcel;
252 public:
253 inline const void* data() const { return mData; }
254 };
255
256 class WritableBlob : public Blob {
257 friend class Parcel;
258 public:
259 inline void* data() { return mData; }
260 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261};
262
263// ---------------------------------------------------------------------------
264
265inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
266{
267 parcel.print(to);
268 return to;
269}
270
271// ---------------------------------------------------------------------------
272
273// Generic acquire and release of objects.
274void acquire_object(const sp<ProcessState>& proc,
275 const flat_binder_object& obj, const void* who);
276void release_object(const sp<ProcessState>& proc,
277 const flat_binder_object& obj, const void* who);
278
279void flatten_binder(const sp<ProcessState>& proc,
280 const sp<IBinder>& binder, flat_binder_object* out);
281void flatten_binder(const sp<ProcessState>& proc,
282 const wp<IBinder>& binder, flat_binder_object* out);
283status_t unflatten_binder(const sp<ProcessState>& proc,
284 const flat_binder_object& flat, sp<IBinder>* out);
285status_t unflatten_binder(const sp<ProcessState>& proc,
286 const flat_binder_object& flat, wp<IBinder>* out);
287
288}; // namespace android
289
290// ---------------------------------------------------------------------------
291
292#endif // ANDROID_PARCEL_H