Parcel.h revision a877cd85b5a026384542e3271fc310d6a8fe24c6
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
26// ---------------------------------------------------------------------------
27namespace android {
28
29class IBinder;
30class ProcessState;
31class String8;
32class TextOutput;
33class Flattenable;
34
35struct flat_binder_object;  // defined in support_p/binder_module.h
36
37class Parcel
38{
39public:
40                        Parcel();
41                        ~Parcel();
42
43    const uint8_t*      data() const;
44    size_t              dataSize() const;
45    size_t              dataAvail() const;
46    size_t              dataPosition() const;
47    size_t              dataCapacity() const;
48
49    status_t            setDataSize(size_t size);
50    void                setDataPosition(size_t pos) const;
51    status_t            setDataCapacity(size_t size);
52
53    status_t            setData(const uint8_t* buffer, size_t len);
54
55    status_t            appendFrom(Parcel *parcel, size_t start, size_t len);
56
57    bool                hasFileDescriptors() const;
58
59    // Writes the RPC header.
60    status_t            writeInterfaceToken(const String16& interface);
61
62    // Parses the RPC header, returning true if the interface name
63    // in the header matches the expected interface from the caller.
64    // If strict_policy_out is non-NULL, the RPC header's StrictMode policy
65    // mask is returned.
66    bool                enforceInterface(const String16& interface,
67                                         int32_t* strict_policy_out = NULL) const;
68    bool                checkInterface(IBinder*) const;
69
70    void                freeData();
71
72    const size_t*       objects() const;
73    size_t              objectsCount() const;
74
75    status_t            errorCheck() const;
76    void                setError(status_t err);
77
78    status_t            write(const void* data, size_t len);
79    void*               writeInplace(size_t len);
80    status_t            writeUnpadded(const void* data, size_t len);
81    status_t            writeInt32(int32_t val);
82    status_t            writeInt64(int64_t val);
83    status_t            writeFloat(float val);
84    status_t            writeDouble(double val);
85    status_t            writeIntPtr(intptr_t val);
86    status_t            writeCString(const char* str);
87    status_t            writeString8(const String8& str);
88    status_t            writeString16(const String16& str);
89    status_t            writeString16(const char16_t* str, size_t len);
90    status_t            writeStrongBinder(const sp<IBinder>& val);
91    status_t            writeWeakBinder(const wp<IBinder>& val);
92    status_t            write(const Flattenable& val);
93
94    // Place a native_handle into the parcel (the native_handle's file-
95    // descriptors are dup'ed, so it is safe to delete the native_handle
96    // when this function returns).
97    // Doesn't take ownership of the native_handle.
98    status_t            writeNativeHandle(const native_handle* handle);
99
100    // Place a file descriptor into the parcel.  The given fd must remain
101    // valid for the lifetime of the parcel.
102    status_t            writeFileDescriptor(int fd);
103
104    // Place a file descriptor into the parcel.  A dup of the fd is made, which
105    // will be closed once the parcel is destroyed.
106    status_t            writeDupFileDescriptor(int fd);
107
108    status_t            writeObject(const flat_binder_object& val, bool nullMetaData);
109
110    // Like Parcel.java's writeNoException().  Just writes a zero int32.
111    // Currently the native implementation doesn't do any of the StrictMode
112    // stack gathering and serialization that the Java implementation does.
113    status_t            writeNoException();
114
115    void                remove(size_t start, size_t amt);
116
117    status_t            read(void* outData, size_t len) const;
118    const void*         readInplace(size_t len) const;
119    int32_t             readInt32() const;
120    status_t            readInt32(int32_t *pArg) const;
121    int64_t             readInt64() const;
122    status_t            readInt64(int64_t *pArg) const;
123    float               readFloat() const;
124    status_t            readFloat(float *pArg) const;
125    double              readDouble() const;
126    status_t            readDouble(double *pArg) const;
127    intptr_t            readIntPtr() const;
128    status_t            readIntPtr(intptr_t *pArg) const;
129
130    const char*         readCString() const;
131    String8             readString8() const;
132    String16            readString16() const;
133    const char16_t*     readString16Inplace(size_t* outLen) const;
134    sp<IBinder>         readStrongBinder() const;
135    wp<IBinder>         readWeakBinder() const;
136    status_t            read(Flattenable& val) const;
137
138    // Like Parcel.java's readExceptionCode().  Reads the first int32
139    // off of a Parcel's header, returning 0 or the negative error
140    // code on exceptions, but also deals with skipping over rich
141    // response headers.  Callers should use this to read & parse the
142    // response headers rather than doing it by hand.
143    int32_t             readExceptionCode() const;
144
145    // Retrieve native_handle from the parcel. This returns a copy of the
146    // parcel's native_handle (the caller takes ownership). The caller
147    // must free the native_handle with native_handle_close() and
148    // native_handle_delete().
149    native_handle*     readNativeHandle() const;
150
151
152    // Retrieve a file descriptor from the parcel.  This returns the raw fd
153    // in the parcel, which you do not own -- use dup() to get your own copy.
154    int                 readFileDescriptor() const;
155
156    const flat_binder_object* readObject(bool nullMetaData) const;
157
158    // Explicitly close all file descriptors in the parcel.
159    void                closeFileDescriptors();
160
161    typedef void        (*release_func)(Parcel* parcel,
162                                        const uint8_t* data, size_t dataSize,
163                                        const size_t* objects, size_t objectsSize,
164                                        void* cookie);
165
166    const uint8_t*      ipcData() const;
167    size_t              ipcDataSize() const;
168    const size_t*       ipcObjects() const;
169    size_t              ipcObjectsCount() const;
170    void                ipcSetDataReference(const uint8_t* data, size_t dataSize,
171                                            const size_t* objects, size_t objectsCount,
172                                            release_func relFunc, void* relCookie);
173
174    void                print(TextOutput& to, uint32_t flags = 0) const;
175
176private:
177                        Parcel(const Parcel& o);
178    Parcel&             operator=(const Parcel& o);
179
180    status_t            finishWrite(size_t len);
181    void                releaseObjects();
182    void                acquireObjects();
183    status_t            growData(size_t len);
184    status_t            restartWrite(size_t desired);
185    status_t            continueWrite(size_t desired);
186    void                freeDataNoInit();
187    void                initState();
188    void                scanForFds() const;
189
190    template<class T>
191    status_t            readAligned(T *pArg) const;
192
193    template<class T>   T readAligned() const;
194
195    template<class T>
196    status_t            writeAligned(T val);
197
198    status_t            mError;
199    uint8_t*            mData;
200    size_t              mDataSize;
201    size_t              mDataCapacity;
202    mutable size_t      mDataPos;
203    size_t*             mObjects;
204    size_t              mObjectsSize;
205    size_t              mObjectsCapacity;
206    mutable size_t      mNextObjectHint;
207
208    mutable bool        mFdsKnown;
209    mutable bool        mHasFds;
210
211    release_func        mOwner;
212    void*               mOwnerCookie;
213};
214
215// ---------------------------------------------------------------------------
216
217inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
218{
219    parcel.print(to);
220    return to;
221}
222
223// ---------------------------------------------------------------------------
224
225// Generic acquire and release of objects.
226void acquire_object(const sp<ProcessState>& proc,
227                    const flat_binder_object& obj, const void* who);
228void release_object(const sp<ProcessState>& proc,
229                    const flat_binder_object& obj, const void* who);
230
231void flatten_binder(const sp<ProcessState>& proc,
232                    const sp<IBinder>& binder, flat_binder_object* out);
233void flatten_binder(const sp<ProcessState>& proc,
234                    const wp<IBinder>& binder, flat_binder_object* out);
235status_t unflatten_binder(const sp<ProcessState>& proc,
236                          const flat_binder_object& flat, sp<IBinder>* out);
237status_t unflatten_binder(const sp<ProcessState>& proc,
238                          const flat_binder_object& flat, wp<IBinder>* out);
239
240}; // namespace android
241
242// ---------------------------------------------------------------------------
243
244#endif // ANDROID_PARCEL_H
245