blob: d9737852d9fb4da0c36f6a80464bacad482e5afa [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 Brown5707dbf2011-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;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040052
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 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
Dianne Hackborn8938ed22011-09-28 23:19:47 -040062 bool setAllowFds(bool allowFds);
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 bool hasFileDescriptors() const;
65
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070066 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070068
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070069 // Parses the RPC header, returning true if the interface name
70 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070071 //
72 // Additionally, enforceInterface does part of the work of
73 // propagating the StrictMode policy mask, populating the current
74 // IPCThreadState, which as an optimization may optionally be
75 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070077 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070078 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 void freeData();
81
82 const size_t* objects() const;
83 size_t objectsCount() const;
84
85 status_t errorCheck() const;
86 void setError(status_t err);
87
88 status_t write(const void* data, size_t len);
89 void* writeInplace(size_t len);
90 status_t writeUnpadded(const void* data, size_t len);
91 status_t writeInt32(int32_t val);
92 status_t writeInt64(int64_t val);
93 status_t writeFloat(float val);
94 status_t writeDouble(double val);
Andreas Huber84a6d042009-08-17 13:33:27 -070095 status_t writeIntPtr(intptr_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 status_t writeCString(const char* str);
97 status_t writeString8(const String8& str);
98 status_t writeString16(const String16& str);
99 status_t writeString16(const char16_t* str, size_t len);
100 status_t writeStrongBinder(const sp<IBinder>& val);
101 status_t writeWeakBinder(const wp<IBinder>& val);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800102 status_t write(const Flattenable& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700104 // Place a native_handle into the parcel (the native_handle's file-
105 // descriptors are dup'ed, so it is safe to delete the native_handle
106 // when this function returns).
107 // Doesn't take ownership of the native_handle.
108 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 // Place a file descriptor into the parcel. The given fd must remain
111 // valid for the lifetime of the parcel.
112 status_t writeFileDescriptor(int fd);
113
114 // Place a file descriptor into the parcel. A dup of the fd is made, which
115 // will be closed once the parcel is destroyed.
116 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700117
118 // Writes a blob to the parcel.
119 // If the blob is small, then it is stored in-place, otherwise it is
120 // transferred by way of an anonymous shared memory region.
121 // The caller should call release() on the blob after writing its contents.
122 status_t writeBlob(size_t len, WritableBlob* outBlob);
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
125
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700126 // Like Parcel.java's writeNoException(). Just writes a zero int32.
127 // Currently the native implementation doesn't do any of the StrictMode
128 // stack gathering and serialization that the Java implementation does.
129 status_t writeNoException();
130
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 void remove(size_t start, size_t amt);
132
133 status_t read(void* outData, size_t len) const;
134 const void* readInplace(size_t len) const;
135 int32_t readInt32() const;
136 status_t readInt32(int32_t *pArg) const;
137 int64_t readInt64() const;
138 status_t readInt64(int64_t *pArg) const;
139 float readFloat() const;
140 status_t readFloat(float *pArg) const;
141 double readDouble() const;
142 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700143 intptr_t readIntPtr() const;
144 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
146 const char* readCString() const;
147 String8 readString8() const;
148 String16 readString16() const;
149 const char16_t* readString16Inplace(size_t* outLen) const;
150 sp<IBinder> readStrongBinder() const;
151 wp<IBinder> readWeakBinder() const;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800152 status_t read(Flattenable& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700153
154 // Like Parcel.java's readExceptionCode(). Reads the first int32
155 // off of a Parcel's header, returning 0 or the negative error
156 // code on exceptions, but also deals with skipping over rich
157 // response headers. Callers should use this to read & parse the
158 // response headers rather than doing it by hand.
159 int32_t readExceptionCode() const;
160
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700161 // Retrieve native_handle from the parcel. This returns a copy of the
162 // parcel's native_handle (the caller takes ownership). The caller
163 // must free the native_handle with native_handle_close() and
164 // native_handle_delete().
165 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
167
168 // Retrieve a file descriptor from the parcel. This returns the raw fd
169 // in the parcel, which you do not own -- use dup() to get your own copy.
170 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700171
172 // Reads a blob from the parcel.
173 // The caller should call release() on the blob after reading its contents.
174 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 const flat_binder_object* readObject(bool nullMetaData) const;
177
178 // Explicitly close all file descriptors in the parcel.
179 void closeFileDescriptors();
180
181 typedef void (*release_func)(Parcel* parcel,
182 const uint8_t* data, size_t dataSize,
183 const size_t* objects, size_t objectsSize,
184 void* cookie);
185
186 const uint8_t* ipcData() const;
187 size_t ipcDataSize() const;
188 const size_t* ipcObjects() const;
189 size_t ipcObjectsCount() const;
190 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
191 const size_t* objects, size_t objectsCount,
192 release_func relFunc, void* relCookie);
193
194 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196private:
197 Parcel(const Parcel& o);
198 Parcel& operator=(const Parcel& o);
199
200 status_t finishWrite(size_t len);
201 void releaseObjects();
202 void acquireObjects();
203 status_t growData(size_t len);
204 status_t restartWrite(size_t desired);
205 status_t continueWrite(size_t desired);
206 void freeDataNoInit();
207 void initState();
208 void scanForFds() const;
209
Andreas Huber84a6d042009-08-17 13:33:27 -0700210 template<class T>
211 status_t readAligned(T *pArg) const;
212
213 template<class T> T readAligned() const;
214
215 template<class T>
216 status_t writeAligned(T val);
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 status_t mError;
219 uint8_t* mData;
220 size_t mDataSize;
221 size_t mDataCapacity;
222 mutable size_t mDataPos;
223 size_t* mObjects;
224 size_t mObjectsSize;
225 size_t mObjectsCapacity;
226 mutable size_t mNextObjectHint;
227
228 mutable bool mFdsKnown;
229 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400230 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231
232 release_func mOwner;
233 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700234
235 class Blob {
236 public:
237 Blob();
238 ~Blob();
239
240 void release();
241 inline size_t size() const { return mSize; }
242
243 protected:
244 void init(bool mapped, void* data, size_t size);
245 void clear();
246
247 bool mMapped;
248 void* mData;
249 size_t mSize;
250 };
251
252public:
253 class ReadableBlob : public Blob {
254 friend class Parcel;
255 public:
256 inline const void* data() const { return mData; }
257 };
258
259 class WritableBlob : public Blob {
260 friend class Parcel;
261 public:
262 inline void* data() { return mData; }
263 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264};
265
266// ---------------------------------------------------------------------------
267
268inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
269{
270 parcel.print(to);
271 return to;
272}
273
274// ---------------------------------------------------------------------------
275
276// Generic acquire and release of objects.
277void acquire_object(const sp<ProcessState>& proc,
278 const flat_binder_object& obj, const void* who);
279void release_object(const sp<ProcessState>& proc,
280 const flat_binder_object& obj, const void* who);
281
282void flatten_binder(const sp<ProcessState>& proc,
283 const sp<IBinder>& binder, flat_binder_object* out);
284void flatten_binder(const sp<ProcessState>& proc,
285 const wp<IBinder>& binder, flat_binder_object* out);
286status_t unflatten_binder(const sp<ProcessState>& proc,
287 const flat_binder_object& flat, sp<IBinder>* out);
288status_t unflatten_binder(const sp<ProcessState>& proc,
289 const flat_binder_object& flat, wp<IBinder>* out);
290
291}; // namespace android
292
293// ---------------------------------------------------------------------------
294
295#endif // ANDROID_PARCEL_H