blob: 57f5dd22e34be0f5b97c83ef061a239d9e6e76fc [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;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -040049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 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
Andreas Huber0a47f4f2011-04-13 10:21:56 -070056 status_t appendFrom(const Parcel *parcel,
57 size_t start, size_t len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -040059 bool setAllowFds(bool allowFds);
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 bool hasFileDescriptors() const;
62
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070063 // Writes the RPC header.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatrick727de402010-07-07 16:06:39 -070065
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070066 // Parses the RPC header, returning true if the interface name
67 // in the header matches the expected interface from the caller.
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -070068 //
69 // Additionally, enforceInterface does part of the work of
70 // propagating the StrictMode policy mask, populating the current
71 // IPCThreadState, which as an optimization may optionally be
72 // passed in.
Brad Fitzpatrick727de402010-07-07 16:06:39 -070073 bool enforceInterface(const String16& interface,
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -070074 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070075 bool checkInterface(IBinder*) const;
Mathias Agopianaaf834a2009-05-22 19:00:22 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 void freeData();
78
79 const size_t* objects() const;
80 size_t objectsCount() const;
81
82 status_t errorCheck() const;
83 void setError(status_t err);
84
85 status_t write(const void* data, size_t len);
86 void* writeInplace(size_t len);
87 status_t writeUnpadded(const void* data, size_t len);
88 status_t writeInt32(int32_t val);
89 status_t writeInt64(int64_t val);
90 status_t writeFloat(float val);
91 status_t writeDouble(double val);
Andreas Huber2f10ae02009-08-17 13:33:27 -070092 status_t writeIntPtr(intptr_t val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 status_t writeCString(const char* str);
94 status_t writeString8(const String8& str);
95 status_t writeString16(const String16& str);
96 status_t writeString16(const char16_t* str, size_t len);
97 status_t writeStrongBinder(const sp<IBinder>& val);
98 status_t writeWeakBinder(const wp<IBinder>& val);
Mathias Agopianc86727f2010-02-11 17:30:52 -080099 status_t write(const Flattenable& val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Mathias Agopiandfece802009-05-21 16:29:38 -0700101 // Place a native_handle into the parcel (the native_handle's file-
102 // descriptors are dup'ed, so it is safe to delete the native_handle
103 // when this function returns).
104 // Doesn't take ownership of the native_handle.
105 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 // Place a file descriptor into the parcel. The given fd must remain
108 // valid for the lifetime of the parcel.
109 status_t writeFileDescriptor(int fd);
110
111 // Place a file descriptor into the parcel. A dup of the fd is made, which
112 // will be closed once the parcel is destroyed.
113 status_t writeDupFileDescriptor(int fd);
Bart Searsb6377172011-09-25 14:30:21 -0700114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
116
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -0700117 // Like Parcel.java's writeNoException(). Just writes a zero int32.
118 // Currently the native implementation doesn't do any of the StrictMode
119 // stack gathering and serialization that the Java implementation does.
120 status_t writeNoException();
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 void remove(size_t start, size_t amt);
123
124 status_t read(void* outData, size_t len) const;
125 const void* readInplace(size_t len) const;
126 int32_t readInt32() const;
127 status_t readInt32(int32_t *pArg) const;
128 int64_t readInt64() const;
129 status_t readInt64(int64_t *pArg) const;
130 float readFloat() const;
131 status_t readFloat(float *pArg) const;
132 double readDouble() const;
133 status_t readDouble(double *pArg) const;
Andreas Huber2f10ae02009-08-17 13:33:27 -0700134 intptr_t readIntPtr() const;
135 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137 const char* readCString() const;
138 String8 readString8() const;
139 String16 readString16() const;
140 const char16_t* readString16Inplace(size_t* outLen) const;
141 sp<IBinder> readStrongBinder() const;
142 wp<IBinder> readWeakBinder() const;
Mathias Agopianc86727f2010-02-11 17:30:52 -0800143 status_t read(Flattenable& val) const;
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -0700144
145 // Like Parcel.java's readExceptionCode(). Reads the first int32
146 // off of a Parcel's header, returning 0 or the negative error
147 // code on exceptions, but also deals with skipping over rich
148 // response headers. Callers should use this to read & parse the
149 // response headers rather than doing it by hand.
150 int32_t readExceptionCode() const;
151
Mathias Agopiandfece802009-05-21 16:29:38 -0700152 // Retrieve native_handle from the parcel. This returns a copy of the
153 // parcel's native_handle (the caller takes ownership). The caller
154 // must free the native_handle with native_handle_close() and
155 // native_handle_delete().
156 native_handle* readNativeHandle() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158
159 // Retrieve a file descriptor from the parcel. This returns the raw fd
160 // in the parcel, which you do not own -- use dup() to get your own copy.
161 int readFileDescriptor() const;
Bart Searsb6377172011-09-25 14:30:21 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 const flat_binder_object* readObject(bool nullMetaData) const;
164
165 // Explicitly close all file descriptors in the parcel.
166 void closeFileDescriptors();
167
168 typedef void (*release_func)(Parcel* parcel,
169 const uint8_t* data, size_t dataSize,
170 const size_t* objects, size_t objectsSize,
171 void* cookie);
172
173 const uint8_t* ipcData() const;
174 size_t ipcDataSize() const;
175 const size_t* ipcObjects() const;
176 size_t ipcObjectsCount() const;
177 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
178 const size_t* objects, size_t objectsCount,
179 release_func relFunc, void* relCookie);
180
181 void print(TextOutput& to, uint32_t flags = 0) const;
Bart Searsb6377172011-09-25 14:30:21 -0700182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183private:
184 Parcel(const Parcel& o);
185 Parcel& operator=(const Parcel& o);
186
187 status_t finishWrite(size_t len);
188 void releaseObjects();
189 void acquireObjects();
190 status_t growData(size_t len);
191 status_t restartWrite(size_t desired);
192 status_t continueWrite(size_t desired);
193 void freeDataNoInit();
194 void initState();
195 void scanForFds() const;
196
Andreas Huber2f10ae02009-08-17 13:33:27 -0700197 template<class T>
198 status_t readAligned(T *pArg) const;
199
200 template<class T> T readAligned() const;
201
202 template<class T>
203 status_t writeAligned(T val);
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 status_t mError;
206 uint8_t* mData;
207 size_t mDataSize;
208 size_t mDataCapacity;
209 mutable size_t mDataPos;
210 size_t* mObjects;
211 size_t mObjectsSize;
212 size_t mObjectsCapacity;
213 mutable size_t mNextObjectHint;
214
215 mutable bool mFdsKnown;
216 mutable bool mHasFds;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400217 bool mAllowFds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
219 release_func mOwner;
220 void* mOwnerCookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221};
222
223// ---------------------------------------------------------------------------
224
225inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
226{
227 parcel.print(to);
228 return to;
229}
230
231// ---------------------------------------------------------------------------
232
233// Generic acquire and release of objects.
234void acquire_object(const sp<ProcessState>& proc,
235 const flat_binder_object& obj, const void* who);
236void release_object(const sp<ProcessState>& proc,
237 const flat_binder_object& obj, const void* who);
238
239void flatten_binder(const sp<ProcessState>& proc,
240 const sp<IBinder>& binder, flat_binder_object* out);
241void flatten_binder(const sp<ProcessState>& proc,
242 const wp<IBinder>& binder, flat_binder_object* out);
243status_t unflatten_binder(const sp<ProcessState>& proc,
244 const flat_binder_object& flat, sp<IBinder>* out);
245status_t unflatten_binder(const sp<ProcessState>& proc,
246 const flat_binder_object& flat, wp<IBinder>* out);
247
248}; // namespace android
249
250// ---------------------------------------------------------------------------
251
252#endif // ANDROID_PARCEL_H