blob: 32c9a1d5a9b6e7d9574e9016dc86e4f80aadb8e4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Fitzpatrick7bcad8a32010-07-27 09:49:11 -070029class Flattenable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030class IBinder;
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -070031class IPCThreadState;
The Android Open Source Project9066cfe2009-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:
41 Parcel();
42 ~Parcel();
43
44 const uint8_t* data() const;
45 size_t dataSize() const;
46 size_t dataAvail() const;
47 size_t dataPosition() const;
48 size_t dataCapacity() const;
49
50 status_t setDataSize(size_t size);
51 void setDataPosition(size_t pos) const;
52 status_t setDataCapacity(size_t size);
53
54 status_t setData(const uint8_t* buffer, size_t len);
55
56 status_t appendFrom(Parcel *parcel, size_t start, size_t len);
57
58 bool hasFileDescriptors() const;
59
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070060 // Writes the RPC header.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatrick727de402010-07-07 16:06:39 -070062
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070063 // Parses the RPC header, returning true if the interface name
64 // in the header matches the expected interface from the caller.
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -070065 //
66 // Additionally, enforceInterface does part of the work of
67 // propagating the StrictMode policy mask, populating the current
68 // IPCThreadState, which as an optimization may optionally be
69 // passed in.
Brad Fitzpatrick727de402010-07-07 16:06:39 -070070 bool enforceInterface(const String16& interface,
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -070071 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070072 bool checkInterface(IBinder*) const;
Mathias Agopianaaf834a2009-05-22 19:00:22 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 void freeData();
75
76 const size_t* objects() const;
77 size_t objectsCount() const;
78
79 status_t errorCheck() const;
80 void setError(status_t err);
81
82 status_t write(const void* data, size_t len);
83 void* writeInplace(size_t len);
84 status_t writeUnpadded(const void* data, size_t len);
85 status_t writeInt32(int32_t val);
86 status_t writeInt64(int64_t val);
87 status_t writeFloat(float val);
88 status_t writeDouble(double val);
Andreas Huber2f10ae02009-08-17 13:33:27 -070089 status_t writeIntPtr(intptr_t val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 status_t writeCString(const char* str);
91 status_t writeString8(const String8& str);
92 status_t writeString16(const String16& str);
93 status_t writeString16(const char16_t* str, size_t len);
94 status_t writeStrongBinder(const sp<IBinder>& val);
95 status_t writeWeakBinder(const wp<IBinder>& val);
Mathias Agopianc86727f2010-02-11 17:30:52 -080096 status_t write(const Flattenable& val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Mathias Agopiandfece802009-05-21 16:29:38 -070098 // Place a native_handle into the parcel (the native_handle's file-
99 // descriptors are dup'ed, so it is safe to delete the native_handle
100 // when this function returns).
101 // Doesn't take ownership of the native_handle.
102 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 // Place a file descriptor into the parcel. The given fd must remain
105 // valid for the lifetime of the parcel.
106 status_t writeFileDescriptor(int fd);
107
108 // Place a file descriptor into the parcel. A dup of the fd is made, which
109 // will be closed once the parcel is destroyed.
110 status_t writeDupFileDescriptor(int fd);
111
112 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
113
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -0700114 // Like Parcel.java's writeNoException(). Just writes a zero int32.
115 // Currently the native implementation doesn't do any of the StrictMode
116 // stack gathering and serialization that the Java implementation does.
117 status_t writeNoException();
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 void remove(size_t start, size_t amt);
120
121 status_t read(void* outData, size_t len) const;
122 const void* readInplace(size_t len) const;
123 int32_t readInt32() const;
124 status_t readInt32(int32_t *pArg) const;
125 int64_t readInt64() const;
126 status_t readInt64(int64_t *pArg) const;
127 float readFloat() const;
128 status_t readFloat(float *pArg) const;
129 double readDouble() const;
130 status_t readDouble(double *pArg) const;
Andreas Huber2f10ae02009-08-17 13:33:27 -0700131 intptr_t readIntPtr() const;
132 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134 const char* readCString() const;
135 String8 readString8() const;
136 String16 readString16() const;
137 const char16_t* readString16Inplace(size_t* outLen) const;
138 sp<IBinder> readStrongBinder() const;
139 wp<IBinder> readWeakBinder() const;
Mathias Agopianc86727f2010-02-11 17:30:52 -0800140 status_t read(Flattenable& val) const;
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -0700141
142 // Like Parcel.java's readExceptionCode(). Reads the first int32
143 // off of a Parcel's header, returning 0 or the negative error
144 // code on exceptions, but also deals with skipping over rich
145 // response headers. Callers should use this to read & parse the
146 // response headers rather than doing it by hand.
147 int32_t readExceptionCode() const;
148
Mathias Agopiandfece802009-05-21 16:29:38 -0700149 // Retrieve native_handle from the parcel. This returns a copy of the
150 // parcel's native_handle (the caller takes ownership). The caller
151 // must free the native_handle with native_handle_close() and
152 // native_handle_delete().
153 native_handle* readNativeHandle() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155
156 // Retrieve a file descriptor from the parcel. This returns the raw fd
157 // in the parcel, which you do not own -- use dup() to get your own copy.
158 int readFileDescriptor() const;
159
160 const flat_binder_object* readObject(bool nullMetaData) const;
161
162 // Explicitly close all file descriptors in the parcel.
163 void closeFileDescriptors();
164
165 typedef void (*release_func)(Parcel* parcel,
166 const uint8_t* data, size_t dataSize,
167 const size_t* objects, size_t objectsSize,
168 void* cookie);
169
170 const uint8_t* ipcData() const;
171 size_t ipcDataSize() const;
172 const size_t* ipcObjects() const;
173 size_t ipcObjectsCount() const;
174 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
175 const size_t* objects, size_t objectsCount,
176 release_func relFunc, void* relCookie);
177
178 void print(TextOutput& to, uint32_t flags = 0) const;
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180private:
181 Parcel(const Parcel& o);
182 Parcel& operator=(const Parcel& o);
183
184 status_t finishWrite(size_t len);
185 void releaseObjects();
186 void acquireObjects();
187 status_t growData(size_t len);
188 status_t restartWrite(size_t desired);
189 status_t continueWrite(size_t desired);
190 void freeDataNoInit();
191 void initState();
192 void scanForFds() const;
193
Andreas Huber2f10ae02009-08-17 13:33:27 -0700194 template<class T>
195 status_t readAligned(T *pArg) const;
196
197 template<class T> T readAligned() const;
198
199 template<class T>
200 status_t writeAligned(T val);
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 status_t mError;
203 uint8_t* mData;
204 size_t mDataSize;
205 size_t mDataCapacity;
206 mutable size_t mDataPos;
207 size_t* mObjects;
208 size_t mObjectsSize;
209 size_t mObjectsCapacity;
210 mutable size_t mNextObjectHint;
211
212 mutable bool mFdsKnown;
213 mutable bool mHasFds;
214
215 release_func mOwner;
216 void* mOwnerCookie;
217};
218
219// ---------------------------------------------------------------------------
220
221inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
222{
223 parcel.print(to);
224 return to;
225}
226
227// ---------------------------------------------------------------------------
228
229// Generic acquire and release of objects.
230void acquire_object(const sp<ProcessState>& proc,
231 const flat_binder_object& obj, const void* who);
232void release_object(const sp<ProcessState>& proc,
233 const flat_binder_object& obj, const void* who);
234
235void flatten_binder(const sp<ProcessState>& proc,
236 const sp<IBinder>& binder, flat_binder_object* out);
237void flatten_binder(const sp<ProcessState>& proc,
238 const wp<IBinder>& binder, flat_binder_object* out);
239status_t unflatten_binder(const sp<ProcessState>& proc,
240 const flat_binder_object& flat, sp<IBinder>* out);
241status_t unflatten_binder(const sp<ProcessState>& proc,
242 const flat_binder_object& flat, wp<IBinder>* out);
243
244}; // namespace android
245
246// ---------------------------------------------------------------------------
247
248#endif // ANDROID_PARCEL_H