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