Parcel.h revision 702ea9d42f52fc145090c0f0bfbe64993e4b8b33
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    // Parses the RPC header, returning true if the interface name
62    // in the header matches the expected interface from the caller.
63    bool                enforceInterface(const String16& interface) const;
64    bool                checkInterface(IBinder*) const;
65
66    void                freeData();
67
68    const size_t*       objects() const;
69    size_t              objectsCount() const;
70
71    status_t            errorCheck() const;
72    void                setError(status_t err);
73
74    status_t            write(const void* data, size_t len);
75    void*               writeInplace(size_t len);
76    status_t            writeUnpadded(const void* data, size_t len);
77    status_t            writeInt32(int32_t val);
78    status_t            writeInt64(int64_t val);
79    status_t            writeFloat(float val);
80    status_t            writeDouble(double val);
81    status_t            writeIntPtr(intptr_t val);
82    status_t            writeCString(const char* str);
83    status_t            writeString8(const String8& str);
84    status_t            writeString16(const String16& str);
85    status_t            writeString16(const char16_t* str, size_t len);
86    status_t            writeStrongBinder(const sp<IBinder>& val);
87    status_t            writeWeakBinder(const wp<IBinder>& val);
88    status_t            write(const Flattenable& val);
89
90    // Place a native_handle into the parcel (the native_handle's file-
91    // descriptors are dup'ed, so it is safe to delete the native_handle
92    // when this function returns).
93    // Doesn't take ownership of the native_handle.
94    status_t            writeNativeHandle(const native_handle* handle);
95
96    // Place a file descriptor into the parcel.  The given fd must remain
97    // valid for the lifetime of the parcel.
98    status_t            writeFileDescriptor(int fd);
99
100    // Place a file descriptor into the parcel.  A dup of the fd is made, which
101    // will be closed once the parcel is destroyed.
102    status_t            writeDupFileDescriptor(int fd);
103
104    status_t            writeObject(const flat_binder_object& val, bool nullMetaData);
105
106    void                remove(size_t start, size_t amt);
107
108    status_t            read(void* outData, size_t len) const;
109    const void*         readInplace(size_t len) const;
110    int32_t             readInt32() const;
111    status_t            readInt32(int32_t *pArg) const;
112    int64_t             readInt64() const;
113    status_t            readInt64(int64_t *pArg) const;
114    float               readFloat() const;
115    status_t            readFloat(float *pArg) const;
116    double              readDouble() const;
117    status_t            readDouble(double *pArg) const;
118    intptr_t            readIntPtr() const;
119    status_t            readIntPtr(intptr_t *pArg) const;
120
121    const char*         readCString() const;
122    String8             readString8() const;
123    String16            readString16() const;
124    const char16_t*     readString16Inplace(size_t* outLen) const;
125    sp<IBinder>         readStrongBinder() const;
126    wp<IBinder>         readWeakBinder() const;
127    status_t            read(Flattenable& val) const;
128
129    // Retrieve native_handle from the parcel. This returns a copy of the
130    // parcel's native_handle (the caller takes ownership). The caller
131    // must free the native_handle with native_handle_close() and
132    // native_handle_delete().
133    native_handle*     readNativeHandle() const;
134
135
136    // Retrieve a file descriptor from the parcel.  This returns the raw fd
137    // in the parcel, which you do not own -- use dup() to get your own copy.
138    int                 readFileDescriptor() const;
139
140    const flat_binder_object* readObject(bool nullMetaData) const;
141
142    // Explicitly close all file descriptors in the parcel.
143    void                closeFileDescriptors();
144
145    typedef void        (*release_func)(Parcel* parcel,
146                                        const uint8_t* data, size_t dataSize,
147                                        const size_t* objects, size_t objectsSize,
148                                        void* cookie);
149
150    const uint8_t*      ipcData() const;
151    size_t              ipcDataSize() const;
152    const size_t*       ipcObjects() const;
153    size_t              ipcObjectsCount() const;
154    void                ipcSetDataReference(const uint8_t* data, size_t dataSize,
155                                            const size_t* objects, size_t objectsCount,
156                                            release_func relFunc, void* relCookie);
157
158    void                print(TextOutput& to, uint32_t flags = 0) const;
159
160private:
161                        Parcel(const Parcel& o);
162    Parcel&             operator=(const Parcel& o);
163
164    status_t            finishWrite(size_t len);
165    void                releaseObjects();
166    void                acquireObjects();
167    status_t            growData(size_t len);
168    status_t            restartWrite(size_t desired);
169    status_t            continueWrite(size_t desired);
170    void                freeDataNoInit();
171    void                initState();
172    void                scanForFds() const;
173
174    template<class T>
175    status_t            readAligned(T *pArg) const;
176
177    template<class T>   T readAligned() const;
178
179    template<class T>
180    status_t            writeAligned(T val);
181
182    status_t            mError;
183    uint8_t*            mData;
184    size_t              mDataSize;
185    size_t              mDataCapacity;
186    mutable size_t      mDataPos;
187    size_t*             mObjects;
188    size_t              mObjectsSize;
189    size_t              mObjectsCapacity;
190    mutable size_t      mNextObjectHint;
191
192    mutable bool        mFdsKnown;
193    mutable bool        mHasFds;
194
195    release_func        mOwner;
196    void*               mOwnerCookie;
197};
198
199// ---------------------------------------------------------------------------
200
201inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
202{
203    parcel.print(to);
204    return to;
205}
206
207// ---------------------------------------------------------------------------
208
209// Generic acquire and release of objects.
210void acquire_object(const sp<ProcessState>& proc,
211                    const flat_binder_object& obj, const void* who);
212void release_object(const sp<ProcessState>& proc,
213                    const flat_binder_object& obj, const void* who);
214
215void flatten_binder(const sp<ProcessState>& proc,
216                    const sp<IBinder>& binder, flat_binder_object* out);
217void flatten_binder(const sp<ProcessState>& proc,
218                    const wp<IBinder>& binder, flat_binder_object* out);
219status_t unflatten_binder(const sp<ProcessState>& proc,
220                          const flat_binder_object& flat, sp<IBinder>* out);
221status_t unflatten_binder(const sp<ProcessState>& proc,
222                          const flat_binder_object& flat, wp<IBinder>* out);
223
224}; // namespace android
225
226// ---------------------------------------------------------------------------
227
228#endif // ANDROID_PARCEL_H
229