1 /*
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 #include <utils/Flattenable.h>
26
27 // ---------------------------------------------------------------------------
28 namespace android {
29
30 template <typename T> class LightFlattenable;
31 class Flattenable;
32 class IBinder;
33 class IPCThreadState;
34 class ProcessState;
35 class String8;
36 class TextOutput;
37
38 struct flat_binder_object; // defined in support_p/binder_module.h
39
40 class Parcel
41 {
42 public:
43 class ReadableBlob;
44 class WritableBlob;
45
46 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;
54
55 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
61 status_t appendFrom(const Parcel *parcel,
62 size_t start, size_t len);
63
64 bool pushAllowFds(bool allowFds);
65 void restoreAllowFds(bool lastValue);
66
67 bool hasFileDescriptors() const;
68
69 // Writes the RPC header.
70 status_t writeInterfaceToken(const String16& interface);
71
72 // Parses the RPC header, returning true if the interface name
73 // in the header matches the expected interface from the caller.
74 //
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.
79 bool enforceInterface(const String16& interface,
80 IPCThreadState* threadState = NULL) const;
81 bool checkInterface(IBinder*) const;
82
83 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);
98 status_t writeIntPtr(intptr_t val);
99 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);
105 status_t write(const Flattenable& val);
106
107 template<typename T>
108 status_t write(const LightFlattenable<T>& val);
109
110
111 // 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);
116
117 // Place a file descriptor into the parcel. The given fd must remain
118 // valid for the lifetime of the parcel.
119 // The Parcel does not take ownership of the given fd unless you ask it to.
120 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
121
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);
125
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
132 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
133
134 // 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
139 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;
151 intptr_t readIntPtr() const;
152 status_t readIntPtr(intptr_t *pArg) const;
153
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;
160 status_t read(Flattenable& val) const;
161
162 template<typename T>
163 status_t read(LightFlattenable<T>& val) const;
164
165 // 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
172 // 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;
177
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;
182
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
187 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;
206
207 private:
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
221 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
229 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;
241 bool mAllowFds;
242
243 release_func mOwner;
244 void* mOwnerCookie;
245
246 class Blob {
247 public:
248 Blob();
249 ~Blob();
250
251 void release();
size()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
263 public:
264 class ReadableBlob : public Blob {
265 friend class Parcel;
266 public:
data()267 inline const void* data() const { return mData; }
268 };
269
270 class WritableBlob : public Blob {
271 friend class Parcel;
272 public:
data()273 inline void* data() { return mData; }
274 };
275 };
276
277 // ---------------------------------------------------------------------------
278
279 template<typename T>
write(const LightFlattenable<T> & val)280 status_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 }
288 if (size) {
289 void* buffer = writeInplace(size);
290 return buffer == NULL ? NO_MEMORY :
291 val.flatten(buffer);
292 }
293 return NO_ERROR;
294 }
295
296 template<typename T>
read(LightFlattenable<T> & val)297 status_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 }
309 if (size) {
310 void const* buffer = readInplace(size);
311 return buffer == NULL ? NO_MEMORY :
312 val.unflatten(buffer, size);
313 }
314 return NO_ERROR;
315 }
316
317 // ---------------------------------------------------------------------------
318
319 inline 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.
328 void acquire_object(const sp<ProcessState>& proc,
329 const flat_binder_object& obj, const void* who);
330 void release_object(const sp<ProcessState>& proc,
331 const flat_binder_object& obj, const void* who);
332
333 void flatten_binder(const sp<ProcessState>& proc,
334 const sp<IBinder>& binder, flat_binder_object* out);
335 void flatten_binder(const sp<ProcessState>& proc,
336 const wp<IBinder>& binder, flat_binder_object* out);
337 status_t unflatten_binder(const sp<ProcessState>& proc,
338 const flat_binder_object& flat, sp<IBinder>* out);
339 status_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
347