blob: 939cfbea9aa3e0e50529dd524cb92cd8bc54b9ea [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 Hackborn7746cc32011-10-03 21:09:35 -070062 bool pushAllowFds(bool allowFds);
63 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 bool hasFileDescriptors() const;
66
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070067 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070069
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070070 // Parses the RPC header, returning true if the interface name
71 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070072 //
73 // Additionally, enforceInterface does part of the work of
74 // propagating the StrictMode policy mask, populating the current
75 // IPCThreadState, which as an optimization may optionally be
76 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070078 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070079 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 void freeData();
82
83 const size_t* objects() const;
84 size_t objectsCount() const;
85
86 status_t errorCheck() const;
87 void setError(status_t err);
88
89 status_t write(const void* data, size_t len);
90 void* writeInplace(size_t len);
91 status_t writeUnpadded(const void* data, size_t len);
92 status_t writeInt32(int32_t val);
93 status_t writeInt64(int64_t val);
94 status_t writeFloat(float val);
95 status_t writeDouble(double val);
Andreas Huber84a6d042009-08-17 13:33:27 -070096 status_t writeIntPtr(intptr_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 status_t writeCString(const char* str);
98 status_t writeString8(const String8& str);
99 status_t writeString16(const String16& str);
100 status_t writeString16(const char16_t* str, size_t len);
101 status_t writeStrongBinder(const sp<IBinder>& val);
102 status_t writeWeakBinder(const wp<IBinder>& val);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800103 status_t write(const Flattenable& val);
Marco Nelissen37b44962014-03-13 14:17:40 -0700104 status_t writeByteArray(size_t len, const uint8_t *val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700106 // Place a native_handle into the parcel (the native_handle's file-
107 // descriptors are dup'ed, so it is safe to delete the native_handle
108 // when this function returns).
109 // Doesn't take ownership of the native_handle.
110 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111
112 // Place a file descriptor into the parcel. The given fd must remain
113 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700114 // The Parcel does not take ownership of the given fd unless you ask it to.
115 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
117 // Place a file descriptor into the parcel. A dup of the fd is made, which
118 // will be closed once the parcel is destroyed.
119 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700120
121 // Writes a blob to the parcel.
122 // If the blob is small, then it is stored in-place, otherwise it is
123 // transferred by way of an anonymous shared memory region.
124 // The caller should call release() on the blob after writing its contents.
125 status_t writeBlob(size_t len, WritableBlob* outBlob);
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
128
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700129 // Like Parcel.java's writeNoException(). Just writes a zero int32.
130 // Currently the native implementation doesn't do any of the StrictMode
131 // stack gathering and serialization that the Java implementation does.
132 status_t writeNoException();
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 void remove(size_t start, size_t amt);
135
136 status_t read(void* outData, size_t len) const;
137 const void* readInplace(size_t len) const;
138 int32_t readInt32() const;
139 status_t readInt32(int32_t *pArg) const;
140 int64_t readInt64() const;
141 status_t readInt64(int64_t *pArg) const;
142 float readFloat() const;
143 status_t readFloat(float *pArg) const;
144 double readDouble() const;
145 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700146 intptr_t readIntPtr() const;
147 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148
149 const char* readCString() const;
150 String8 readString8() const;
151 String16 readString16() const;
152 const char16_t* readString16Inplace(size_t* outLen) const;
153 sp<IBinder> readStrongBinder() const;
154 wp<IBinder> readWeakBinder() const;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800155 status_t read(Flattenable& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700156
157 // Like Parcel.java's readExceptionCode(). Reads the first int32
158 // off of a Parcel's header, returning 0 or the negative error
159 // code on exceptions, but also deals with skipping over rich
160 // response headers. Callers should use this to read & parse the
161 // response headers rather than doing it by hand.
162 int32_t readExceptionCode() const;
163
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700164 // Retrieve native_handle from the parcel. This returns a copy of the
165 // parcel's native_handle (the caller takes ownership). The caller
166 // must free the native_handle with native_handle_close() and
167 // native_handle_delete().
168 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
170
171 // Retrieve a file descriptor from the parcel. This returns the raw fd
172 // in the parcel, which you do not own -- use dup() to get your own copy.
173 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700174
175 // Reads a blob from the parcel.
176 // The caller should call release() on the blob after reading its contents.
177 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 const flat_binder_object* readObject(bool nullMetaData) const;
180
181 // Explicitly close all file descriptors in the parcel.
182 void closeFileDescriptors();
183
184 typedef void (*release_func)(Parcel* parcel,
185 const uint8_t* data, size_t dataSize,
186 const size_t* objects, size_t objectsSize,
187 void* cookie);
188
189 const uint8_t* ipcData() const;
190 size_t ipcDataSize() const;
191 const size_t* ipcObjects() const;
192 size_t ipcObjectsCount() const;
193 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
194 const size_t* objects, size_t objectsCount,
195 release_func relFunc, void* relCookie);
196
197 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199private:
200 Parcel(const Parcel& o);
201 Parcel& operator=(const Parcel& o);
202
203 status_t finishWrite(size_t len);
204 void releaseObjects();
205 void acquireObjects();
206 status_t growData(size_t len);
207 status_t restartWrite(size_t desired);
208 status_t continueWrite(size_t desired);
209 void freeDataNoInit();
210 void initState();
211 void scanForFds() const;
212
Andreas Huber84a6d042009-08-17 13:33:27 -0700213 template<class T>
214 status_t readAligned(T *pArg) const;
215
216 template<class T> T readAligned() const;
217
218 template<class T>
219 status_t writeAligned(T val);
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 status_t mError;
222 uint8_t* mData;
223 size_t mDataSize;
224 size_t mDataCapacity;
225 mutable size_t mDataPos;
226 size_t* mObjects;
227 size_t mObjectsSize;
228 size_t mObjectsCapacity;
229 mutable size_t mNextObjectHint;
230
231 mutable bool mFdsKnown;
232 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400233 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
235 release_func mOwner;
236 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700237
238 class Blob {
239 public:
240 Blob();
241 ~Blob();
242
243 void release();
244 inline size_t size() const { return mSize; }
245
246 protected:
247 void init(bool mapped, void* data, size_t size);
248 void clear();
249
250 bool mMapped;
251 void* mData;
252 size_t mSize;
253 };
254
255public:
256 class ReadableBlob : public Blob {
257 friend class Parcel;
258 public:
259 inline const void* data() const { return mData; }
260 };
261
262 class WritableBlob : public Blob {
263 friend class Parcel;
264 public:
265 inline void* data() { return mData; }
266 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267};
268
269// ---------------------------------------------------------------------------
270
271inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
272{
273 parcel.print(to);
274 return to;
275}
276
277// ---------------------------------------------------------------------------
278
279// Generic acquire and release of objects.
280void acquire_object(const sp<ProcessState>& proc,
281 const flat_binder_object& obj, const void* who);
282void release_object(const sp<ProcessState>& proc,
283 const flat_binder_object& obj, const void* who);
284
285void flatten_binder(const sp<ProcessState>& proc,
286 const sp<IBinder>& binder, flat_binder_object* out);
287void flatten_binder(const sp<ProcessState>& proc,
288 const wp<IBinder>& binder, flat_binder_object* out);
289status_t unflatten_binder(const sp<ProcessState>& proc,
290 const flat_binder_object& flat, sp<IBinder>* out);
291status_t unflatten_binder(const sp<ProcessState>& proc,
292 const flat_binder_object& flat, wp<IBinder>* out);
293
294}; // namespace android
295
296// ---------------------------------------------------------------------------
297
298#endif // ANDROID_PARCEL_H