blob: af1490a02973ecb0e9db8ec743a725409bd55aab [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
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);
82
Mathias Agopian53f6f3c2009-05-20 14:33:23 -070083 // Place a native_handle into the parcel (the native_handle's file-
84 // descriptors are dup'ed, so it is safe to delete the native_handle
85 // when this function returns).
86 // Doesn't take ownership of the native_handle.
87 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
89 // Place a file descriptor into the parcel. The given fd must remain
90 // valid for the lifetime of the parcel.
91 status_t writeFileDescriptor(int fd);
92
93 // Place a file descriptor into the parcel. A dup of the fd is made, which
94 // will be closed once the parcel is destroyed.
95 status_t writeDupFileDescriptor(int fd);
96
97 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
98
99 void remove(size_t start, size_t amt);
100
101 status_t read(void* outData, size_t len) const;
102 const void* readInplace(size_t len) const;
103 int32_t readInt32() const;
104 status_t readInt32(int32_t *pArg) const;
105 int64_t readInt64() const;
106 status_t readInt64(int64_t *pArg) const;
107 float readFloat() const;
108 status_t readFloat(float *pArg) const;
109 double readDouble() const;
110 status_t readDouble(double *pArg) const;
111
112 const char* readCString() const;
113 String8 readString8() const;
114 String16 readString16() const;
115 const char16_t* readString16Inplace(size_t* outLen) const;
116 sp<IBinder> readStrongBinder() const;
117 wp<IBinder> readWeakBinder() const;
118
119
Mathias Agopian53f6f3c2009-05-20 14:33:23 -0700120 // Retrieve native_handle from the parcel. This returns a copy of the
121 // parcel's native_handle (the caller takes ownership). The caller
122 // must free the native_handle with native_handle_close() and
123 // native_handle_delete().
124 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125
126
127 // Retrieve a file descriptor from the parcel. This returns the raw fd
128 // in the parcel, which you do not own -- use dup() to get your own copy.
129 int readFileDescriptor() const;
130
131 const flat_binder_object* readObject(bool nullMetaData) const;
132
133 // Explicitly close all file descriptors in the parcel.
134 void closeFileDescriptors();
135
136 typedef void (*release_func)(Parcel* parcel,
137 const uint8_t* data, size_t dataSize,
138 const size_t* objects, size_t objectsSize,
139 void* cookie);
140
141 const uint8_t* ipcData() const;
142 size_t ipcDataSize() const;
143 const size_t* ipcObjects() const;
144 size_t ipcObjectsCount() const;
145 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
146 const size_t* objects, size_t objectsCount,
147 release_func relFunc, void* relCookie);
148
149 void print(TextOutput& to, uint32_t flags = 0) const;
150
151private:
152 Parcel(const Parcel& o);
153 Parcel& operator=(const Parcel& o);
154
155 status_t finishWrite(size_t len);
156 void releaseObjects();
157 void acquireObjects();
158 status_t growData(size_t len);
159 status_t restartWrite(size_t desired);
160 status_t continueWrite(size_t desired);
161 void freeDataNoInit();
162 void initState();
163 void scanForFds() const;
164
165 status_t mError;
166 uint8_t* mData;
167 size_t mDataSize;
168 size_t mDataCapacity;
169 mutable size_t mDataPos;
170 size_t* mObjects;
171 size_t mObjectsSize;
172 size_t mObjectsCapacity;
173 mutable size_t mNextObjectHint;
174
175 mutable bool mFdsKnown;
176 mutable bool mHasFds;
177
178 release_func mOwner;
179 void* mOwnerCookie;
180};
181
182// ---------------------------------------------------------------------------
183
184inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
185{
186 parcel.print(to);
187 return to;
188}
189
190// ---------------------------------------------------------------------------
191
192// Generic acquire and release of objects.
193void acquire_object(const sp<ProcessState>& proc,
194 const flat_binder_object& obj, const void* who);
195void release_object(const sp<ProcessState>& proc,
196 const flat_binder_object& obj, const void* who);
197
198void flatten_binder(const sp<ProcessState>& proc,
199 const sp<IBinder>& binder, flat_binder_object* out);
200void flatten_binder(const sp<ProcessState>& proc,
201 const wp<IBinder>& binder, flat_binder_object* out);
202status_t unflatten_binder(const sp<ProcessState>& proc,
203 const flat_binder_object& flat, sp<IBinder>* out);
204status_t unflatten_binder(const sp<ProcessState>& proc,
205 const flat_binder_object& flat, wp<IBinder>* out);
206
207}; // namespace android
208
209// ---------------------------------------------------------------------------
210
211#endif // ANDROID_PARCEL_H