blob: 9087c4465b9626bd241d6caaae7f68cc32a70d5e [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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
The Android Open Source Project5f78a482009-01-20 14:03:58 -080020#include <cutils/native_handle.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021#include <utils/Errors.h>
22#include <utils/RefBase.h>
23#include <utils/String16.h>
24#include <utils/Vector.h>
25
26// ---------------------------------------------------------------------------
27namespace android {
28
29class IBinder;
30class ProcessState;
31class String8;
32class TextOutput;
33
34struct flat_binder_object; // defined in support_p/binder_module.h
35
36class Parcel
37{
38public:
39 Parcel();
40 ~Parcel();
41
42 const uint8_t* data() const;
43 size_t dataSize() const;
44 size_t dataAvail() const;
45 size_t dataPosition() const;
46 size_t dataCapacity() const;
47
48 status_t setDataSize(size_t size);
49 void setDataPosition(size_t pos) const;
50 status_t setDataCapacity(size_t size);
51
52 status_t setData(const uint8_t* buffer, size_t len);
53
54 status_t appendFrom(Parcel *parcel, size_t start, size_t len);
55
56 bool hasFileDescriptors() const;
57
58 status_t writeInterfaceToken(const String16& interface);
59 bool enforceInterface(const String16& interface) const;
60
61 void freeData();
62
63 const size_t* objects() const;
64 size_t objectsCount() const;
65
66 status_t errorCheck() const;
67 void setError(status_t err);
68
69 status_t write(const void* data, size_t len);
70 void* writeInplace(size_t len);
71 status_t writeUnpadded(const void* data, size_t len);
72 status_t writeInt32(int32_t val);
73 status_t writeInt64(int64_t val);
74 status_t writeFloat(float val);
75 status_t writeDouble(double val);
76 status_t writeCString(const char* str);
77 status_t writeString8(const String8& str);
78 status_t writeString16(const String16& str);
79 status_t writeString16(const char16_t* str, size_t len);
80 status_t writeStrongBinder(const sp<IBinder>& val);
81 status_t writeWeakBinder(const wp<IBinder>& val);
The Android Open Source Project5f78a482009-01-20 14:03:58 -080082
83 // doesn't take ownership of the native_handle
84 status_t writeNativeHandle(const native_handle& handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070085
86 // Place a file descriptor into the parcel. The given fd must remain
87 // valid for the lifetime of the parcel.
88 status_t writeFileDescriptor(int fd);
89
90 // Place a file descriptor into the parcel. A dup of the fd is made, which
91 // will be closed once the parcel is destroyed.
92 status_t writeDupFileDescriptor(int fd);
93
94 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
95
96 void remove(size_t start, size_t amt);
97
98 status_t read(void* outData, size_t len) const;
99 const void* readInplace(size_t len) const;
100 int32_t readInt32() const;
101 status_t readInt32(int32_t *pArg) const;
102 int64_t readInt64() const;
103 status_t readInt64(int64_t *pArg) const;
104 float readFloat() const;
105 status_t readFloat(float *pArg) const;
106 double readDouble() const;
107 status_t readDouble(double *pArg) const;
108
109 const char* readCString() const;
110 String8 readString8() const;
111 String16 readString16() const;
112 const char16_t* readString16Inplace(size_t* outLen) const;
113 sp<IBinder> readStrongBinder() const;
114 wp<IBinder> readWeakBinder() const;
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800115
116
117 // if alloc is NULL, native_handle is allocated with malloc(), otherwise
118 // alloc is used. If the function fails, the effects of alloc() must be
119 // reverted by the caller.
120 native_handle* readNativeHandle(
121 native_handle* (*alloc)(void* cookie, int numFds, int ints),
122 void* cookie) const;
123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700124
125 // Retrieve a file descriptor from the parcel. This returns the raw fd
126 // in the parcel, which you do not own -- use dup() to get your own copy.
127 int readFileDescriptor() const;
128
129 const flat_binder_object* readObject(bool nullMetaData) const;
130
131 // Explicitly close all file descriptors in the parcel.
132 void closeFileDescriptors();
133
134 typedef void (*release_func)(Parcel* parcel,
135 const uint8_t* data, size_t dataSize,
136 const size_t* objects, size_t objectsSize,
137 void* cookie);
138
139 const uint8_t* ipcData() const;
140 size_t ipcDataSize() const;
141 const size_t* ipcObjects() const;
142 size_t ipcObjectsCount() const;
143 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
144 const size_t* objects, size_t objectsCount,
145 release_func relFunc, void* relCookie);
146
147 void print(TextOutput& to, uint32_t flags = 0) const;
148
149private:
150 Parcel(const Parcel& o);
151 Parcel& operator=(const Parcel& o);
152
153 status_t finishWrite(size_t len);
154 void releaseObjects();
155 void acquireObjects();
156 status_t growData(size_t len);
157 status_t restartWrite(size_t desired);
158 status_t continueWrite(size_t desired);
159 void freeDataNoInit();
160 void initState();
161 void scanForFds() const;
162
163 status_t mError;
164 uint8_t* mData;
165 size_t mDataSize;
166 size_t mDataCapacity;
167 mutable size_t mDataPos;
168 size_t* mObjects;
169 size_t mObjectsSize;
170 size_t mObjectsCapacity;
171 mutable size_t mNextObjectHint;
172
173 mutable bool mFdsKnown;
174 mutable bool mHasFds;
175
176 release_func mOwner;
177 void* mOwnerCookie;
178};
179
180// ---------------------------------------------------------------------------
181
182inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
183{
184 parcel.print(to);
185 return to;
186}
187
188// ---------------------------------------------------------------------------
189
190// Generic acquire and release of objects.
191void acquire_object(const sp<ProcessState>& proc,
192 const flat_binder_object& obj, const void* who);
193void release_object(const sp<ProcessState>& proc,
194 const flat_binder_object& obj, const void* who);
195
196void flatten_binder(const sp<ProcessState>& proc,
197 const sp<IBinder>& binder, flat_binder_object* out);
198void flatten_binder(const sp<ProcessState>& proc,
199 const wp<IBinder>& binder, flat_binder_object* out);
200status_t unflatten_binder(const sp<ProcessState>& proc,
201 const flat_binder_object& flat, sp<IBinder>* out);
202status_t unflatten_binder(const sp<ProcessState>& proc,
203 const flat_binder_object& flat, wp<IBinder>* out);
204
205}; // namespace android
206
207// ---------------------------------------------------------------------------
208
209#endif // ANDROID_PARCEL_H