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