| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 25 | #include <utils/Flattenable.h> | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 26 | #include <linux/binder.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 |  | 
|  | 28 | // --------------------------------------------------------------------------- | 
|  | 29 | namespace android { | 
|  | 30 |  | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 31 | template <typename T> class Flattenable; | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 32 | template <typename T> class LightFlattenable; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | class IBinder; | 
| Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 34 | class IPCThreadState; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | class ProcessState; | 
|  | 36 | class String8; | 
|  | 37 | class TextOutput; | 
|  | 38 |  | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 39 | class Parcel { | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 40 | friend class IPCThreadState; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | public: | 
| Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 42 | class ReadableBlob; | 
|  | 43 | class WritableBlob; | 
|  | 44 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | Parcel(); | 
|  | 46 | ~Parcel(); | 
|  | 47 |  | 
|  | 48 | const uint8_t*      data() const; | 
|  | 49 | size_t              dataSize() const; | 
|  | 50 | size_t              dataAvail() const; | 
|  | 51 | size_t              dataPosition() const; | 
|  | 52 | size_t              dataCapacity() const; | 
| Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 53 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | status_t            setDataSize(size_t size); | 
|  | 55 | void                setDataPosition(size_t pos) const; | 
|  | 56 | status_t            setDataCapacity(size_t size); | 
|  | 57 |  | 
|  | 58 | status_t            setData(const uint8_t* buffer, size_t len); | 
|  | 59 |  | 
| Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 60 | status_t            appendFrom(const Parcel *parcel, | 
|  | 61 | size_t start, size_t len); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 |  | 
| Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 63 | bool                pushAllowFds(bool allowFds); | 
|  | 64 | void                restoreAllowFds(bool lastValue); | 
| Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 65 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | bool                hasFileDescriptors() const; | 
|  | 67 |  | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 68 | // Writes the RPC header. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | status_t            writeInterfaceToken(const String16& interface); | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 70 |  | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 71 | // Parses the RPC header, returning true if the interface name | 
|  | 72 | // in the header matches the expected interface from the caller. | 
| Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 73 | // | 
|  | 74 | // Additionally, enforceInterface does part of the work of | 
|  | 75 | // propagating the StrictMode policy mask, populating the current | 
|  | 76 | // IPCThreadState, which as an optimization may optionally be | 
|  | 77 | // passed in. | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 78 | bool                enforceInterface(const String16& interface, | 
| Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 79 | IPCThreadState* threadState = NULL) const; | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 80 | bool                checkInterface(IBinder*) const; | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 81 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | void                freeData(); | 
|  | 83 |  | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 84 | private: | 
|  | 85 | const binder_size_t* objects() const; | 
|  | 86 |  | 
|  | 87 | public: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | size_t              objectsCount() const; | 
|  | 89 |  | 
|  | 90 | status_t            errorCheck() const; | 
|  | 91 | void                setError(status_t err); | 
|  | 92 |  | 
|  | 93 | status_t            write(const void* data, size_t len); | 
|  | 94 | void*               writeInplace(size_t len); | 
|  | 95 | status_t            writeUnpadded(const void* data, size_t len); | 
|  | 96 | status_t            writeInt32(int32_t val); | 
|  | 97 | status_t            writeInt64(int64_t val); | 
|  | 98 | status_t            writeFloat(float val); | 
|  | 99 | status_t            writeDouble(double val); | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 100 | status_t            writeIntPtr(intptr_t val); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | status_t            writeCString(const char* str); | 
|  | 102 | status_t            writeString8(const String8& str); | 
|  | 103 | status_t            writeString16(const String16& str); | 
|  | 104 | status_t            writeString16(const char16_t* str, size_t len); | 
|  | 105 | status_t            writeStrongBinder(const sp<IBinder>& val); | 
|  | 106 | status_t            writeWeakBinder(const wp<IBinder>& val); | 
| Marco Nelissen | 708cc79 | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 107 | status_t            writeInt32Array(size_t len, const int32_t *val); | 
| Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 108 | status_t            writeByteArray(size_t len, const uint8_t *val); | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | template<typename T> | 
|  | 111 | status_t            write(const Flattenable<T>& val); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 |  | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 113 | template<typename T> | 
|  | 114 | status_t            write(const LightFlattenable<T>& val); | 
|  | 115 |  | 
|  | 116 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 117 | // Place a native_handle into the parcel (the native_handle's file- | 
|  | 118 | // descriptors are dup'ed, so it is safe to delete the native_handle | 
|  | 119 | // when this function returns). | 
|  | 120 | // Doesn't take ownership of the native_handle. | 
|  | 121 | status_t            writeNativeHandle(const native_handle* handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 |  | 
|  | 123 | // Place a file descriptor into the parcel.  The given fd must remain | 
|  | 124 | // valid for the lifetime of the parcel. | 
| Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 125 | // The Parcel does not take ownership of the given fd unless you ask it to. | 
|  | 126 | status_t            writeFileDescriptor(int fd, bool takeOwnership = false); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 |  | 
|  | 128 | // Place a file descriptor into the parcel.  A dup of the fd is made, which | 
|  | 129 | // will be closed once the parcel is destroyed. | 
|  | 130 | status_t            writeDupFileDescriptor(int fd); | 
| Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | // Writes a blob to the parcel. | 
|  | 133 | // If the blob is small, then it is stored in-place, otherwise it is | 
|  | 134 | // transferred by way of an anonymous shared memory region. | 
|  | 135 | // The caller should call release() on the blob after writing its contents. | 
|  | 136 | status_t            writeBlob(size_t len, WritableBlob* outBlob); | 
|  | 137 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | status_t            writeObject(const flat_binder_object& val, bool nullMetaData); | 
|  | 139 |  | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 140 | // Like Parcel.java's writeNoException().  Just writes a zero int32. | 
|  | 141 | // Currently the native implementation doesn't do any of the StrictMode | 
|  | 142 | // stack gathering and serialization that the Java implementation does. | 
|  | 143 | status_t            writeNoException(); | 
|  | 144 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | void                remove(size_t start, size_t amt); | 
|  | 146 |  | 
|  | 147 | status_t            read(void* outData, size_t len) const; | 
|  | 148 | const void*         readInplace(size_t len) const; | 
|  | 149 | int32_t             readInt32() const; | 
|  | 150 | status_t            readInt32(int32_t *pArg) const; | 
|  | 151 | int64_t             readInt64() const; | 
|  | 152 | status_t            readInt64(int64_t *pArg) const; | 
|  | 153 | float               readFloat() const; | 
|  | 154 | status_t            readFloat(float *pArg) const; | 
|  | 155 | double              readDouble() const; | 
|  | 156 | status_t            readDouble(double *pArg) const; | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 157 | intptr_t            readIntPtr() const; | 
|  | 158 | status_t            readIntPtr(intptr_t *pArg) const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 |  | 
|  | 160 | const char*         readCString() const; | 
|  | 161 | String8             readString8() const; | 
|  | 162 | String16            readString16() const; | 
|  | 163 | const char16_t*     readString16Inplace(size_t* outLen) const; | 
|  | 164 | sp<IBinder>         readStrongBinder() const; | 
|  | 165 | wp<IBinder>         readWeakBinder() const; | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 166 |  | 
|  | 167 | template<typename T> | 
|  | 168 | status_t            read(Flattenable<T>& val) const; | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 169 |  | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 170 | template<typename T> | 
|  | 171 | status_t            read(LightFlattenable<T>& val) const; | 
|  | 172 |  | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 173 | // Like Parcel.java's readExceptionCode().  Reads the first int32 | 
|  | 174 | // off of a Parcel's header, returning 0 or the negative error | 
|  | 175 | // code on exceptions, but also deals with skipping over rich | 
|  | 176 | // response headers.  Callers should use this to read & parse the | 
|  | 177 | // response headers rather than doing it by hand. | 
|  | 178 | int32_t             readExceptionCode() const; | 
|  | 179 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 180 | // Retrieve native_handle from the parcel. This returns a copy of the | 
|  | 181 | // parcel's native_handle (the caller takes ownership). The caller | 
|  | 182 | // must free the native_handle with native_handle_close() and | 
|  | 183 | // native_handle_delete(). | 
|  | 184 | native_handle*     readNativeHandle() const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 |  | 
|  | 186 |  | 
|  | 187 | // Retrieve a file descriptor from the parcel.  This returns the raw fd | 
|  | 188 | // in the parcel, which you do not own -- use dup() to get your own copy. | 
|  | 189 | int                 readFileDescriptor() const; | 
| Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 190 |  | 
|  | 191 | // Reads a blob from the parcel. | 
|  | 192 | // The caller should call release() on the blob after reading its contents. | 
|  | 193 | status_t            readBlob(size_t len, ReadableBlob* outBlob) const; | 
|  | 194 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | const flat_binder_object* readObject(bool nullMetaData) const; | 
|  | 196 |  | 
|  | 197 | // Explicitly close all file descriptors in the parcel. | 
|  | 198 | void                closeFileDescriptors(); | 
|  | 199 |  | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 200 | private: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | typedef void        (*release_func)(Parcel* parcel, | 
|  | 202 | const uint8_t* data, size_t dataSize, | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 203 | const binder_size_t* objects, size_t objectsSize, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | void* cookie); | 
|  | 205 |  | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 206 | uintptr_t           ipcData() const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | size_t              ipcDataSize() const; | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 208 | uintptr_t           ipcObjects() const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | size_t              ipcObjectsCount() const; | 
|  | 210 | void                ipcSetDataReference(const uint8_t* data, size_t dataSize, | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 211 | const binder_size_t* objects, size_t objectsCount, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | release_func relFunc, void* relCookie); | 
|  | 213 |  | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 214 | public: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | void                print(TextOutput& to, uint32_t flags = 0) const; | 
| Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 216 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | private: | 
|  | 218 | Parcel(const Parcel& o); | 
|  | 219 | Parcel&             operator=(const Parcel& o); | 
|  | 220 |  | 
|  | 221 | status_t            finishWrite(size_t len); | 
|  | 222 | void                releaseObjects(); | 
|  | 223 | void                acquireObjects(); | 
|  | 224 | status_t            growData(size_t len); | 
|  | 225 | status_t            restartWrite(size_t desired); | 
|  | 226 | status_t            continueWrite(size_t desired); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 227 | status_t            writePointer(uintptr_t val); | 
|  | 228 | status_t            readPointer(uintptr_t *pArg) const; | 
|  | 229 | uintptr_t           readPointer() const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | void                freeDataNoInit(); | 
|  | 231 | void                initState(); | 
|  | 232 | void                scanForFds() const; | 
|  | 233 |  | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 234 | template<class T> | 
|  | 235 | status_t            readAligned(T *pArg) const; | 
|  | 236 |  | 
|  | 237 | template<class T>   T readAligned() const; | 
|  | 238 |  | 
|  | 239 | template<class T> | 
|  | 240 | status_t            writeAligned(T val); | 
|  | 241 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | status_t            mError; | 
|  | 243 | uint8_t*            mData; | 
|  | 244 | size_t              mDataSize; | 
|  | 245 | size_t              mDataCapacity; | 
|  | 246 | mutable size_t      mDataPos; | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 247 | binder_size_t*      mObjects; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | size_t              mObjectsSize; | 
|  | 249 | size_t              mObjectsCapacity; | 
|  | 250 | mutable size_t      mNextObjectHint; | 
|  | 251 |  | 
|  | 252 | mutable bool        mFdsKnown; | 
|  | 253 | mutable bool        mHasFds; | 
| Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 254 | bool                mAllowFds; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 |  | 
|  | 256 | release_func        mOwner; | 
|  | 257 | void*               mOwnerCookie; | 
| Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 258 |  | 
|  | 259 | class Blob { | 
|  | 260 | public: | 
|  | 261 | Blob(); | 
|  | 262 | ~Blob(); | 
|  | 263 |  | 
|  | 264 | void release(); | 
|  | 265 | inline size_t size() const { return mSize; } | 
|  | 266 |  | 
|  | 267 | protected: | 
|  | 268 | void init(bool mapped, void* data, size_t size); | 
|  | 269 | void clear(); | 
|  | 270 |  | 
|  | 271 | bool mMapped; | 
|  | 272 | void* mData; | 
|  | 273 | size_t mSize; | 
|  | 274 | }; | 
|  | 275 |  | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 276 | class FlattenableHelperInterface { | 
|  | 277 | protected: | 
|  | 278 | ~FlattenableHelperInterface() { } | 
|  | 279 | public: | 
|  | 280 | virtual size_t getFlattenedSize() const = 0; | 
|  | 281 | virtual size_t getFdCount() const = 0; | 
|  | 282 | virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0; | 
|  | 283 | virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0; | 
|  | 284 | }; | 
|  | 285 |  | 
|  | 286 | template<typename T> | 
|  | 287 | class FlattenableHelper : public FlattenableHelperInterface { | 
|  | 288 | friend class Parcel; | 
|  | 289 | const Flattenable<T>& val; | 
|  | 290 | explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { } | 
|  | 291 |  | 
|  | 292 | public: | 
|  | 293 | virtual size_t getFlattenedSize() const { | 
|  | 294 | return val.getFlattenedSize(); | 
|  | 295 | } | 
|  | 296 | virtual size_t getFdCount() const { | 
|  | 297 | return val.getFdCount(); | 
|  | 298 | } | 
|  | 299 | virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const { | 
|  | 300 | return val.flatten(buffer, size, fds, count); | 
|  | 301 | } | 
|  | 302 | virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) { | 
|  | 303 | return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count); | 
|  | 304 | } | 
|  | 305 | }; | 
|  | 306 | status_t write(const FlattenableHelperInterface& val); | 
|  | 307 | status_t read(FlattenableHelperInterface& val) const; | 
|  | 308 |  | 
| Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 309 | public: | 
|  | 310 | class ReadableBlob : public Blob { | 
|  | 311 | friend class Parcel; | 
|  | 312 | public: | 
|  | 313 | inline const void* data() const { return mData; } | 
|  | 314 | }; | 
|  | 315 |  | 
|  | 316 | class WritableBlob : public Blob { | 
|  | 317 | friend class Parcel; | 
|  | 318 | public: | 
|  | 319 | inline void* data() { return mData; } | 
|  | 320 | }; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | }; | 
|  | 322 |  | 
|  | 323 | // --------------------------------------------------------------------------- | 
|  | 324 |  | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 325 | template<typename T> | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 326 | status_t Parcel::write(const Flattenable<T>& val) { | 
|  | 327 | const FlattenableHelper<T> helper(val); | 
|  | 328 | return write(helper); | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | template<typename T> | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 332 | status_t Parcel::write(const LightFlattenable<T>& val) { | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 333 | size_t size(val.getFlattenedSize()); | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 334 | if (!val.isFixedSize()) { | 
|  | 335 | status_t err = writeInt32(size); | 
|  | 336 | if (err != NO_ERROR) { | 
|  | 337 | return err; | 
|  | 338 | } | 
|  | 339 | } | 
| Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 340 | if (size) { | 
|  | 341 | void* buffer = writeInplace(size); | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 342 | if (buffer == NULL) | 
|  | 343 | return NO_MEMORY; | 
|  | 344 | return val.flatten(buffer, size); | 
| Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 345 | } | 
|  | 346 | return NO_ERROR; | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
|  | 349 | template<typename T> | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 350 | status_t Parcel::read(Flattenable<T>& val) const { | 
|  | 351 | FlattenableHelper<T> helper(val); | 
|  | 352 | return read(helper); | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | template<typename T> | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 356 | status_t Parcel::read(LightFlattenable<T>& val) const { | 
|  | 357 | size_t size; | 
|  | 358 | if (val.isFixedSize()) { | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 359 | size = val.getFlattenedSize(); | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 360 | } else { | 
|  | 361 | int32_t s; | 
|  | 362 | status_t err = readInt32(&s); | 
|  | 363 | if (err != NO_ERROR) { | 
|  | 364 | return err; | 
|  | 365 | } | 
|  | 366 | size = s; | 
|  | 367 | } | 
| Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 368 | if (size) { | 
|  | 369 | void const* buffer = readInplace(size); | 
|  | 370 | return buffer == NULL ? NO_MEMORY : | 
|  | 371 | val.unflatten(buffer, size); | 
|  | 372 | } | 
|  | 373 | return NO_ERROR; | 
| Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 374 | } | 
|  | 375 |  | 
|  | 376 | // --------------------------------------------------------------------------- | 
|  | 377 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) | 
|  | 379 | { | 
|  | 380 | parcel.print(to); | 
|  | 381 | return to; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | // --------------------------------------------------------------------------- | 
|  | 385 |  | 
|  | 386 | // Generic acquire and release of objects. | 
|  | 387 | void acquire_object(const sp<ProcessState>& proc, | 
|  | 388 | const flat_binder_object& obj, const void* who); | 
|  | 389 | void release_object(const sp<ProcessState>& proc, | 
|  | 390 | const flat_binder_object& obj, const void* who); | 
|  | 391 |  | 
|  | 392 | void flatten_binder(const sp<ProcessState>& proc, | 
|  | 393 | const sp<IBinder>& binder, flat_binder_object* out); | 
|  | 394 | void flatten_binder(const sp<ProcessState>& proc, | 
|  | 395 | const wp<IBinder>& binder, flat_binder_object* out); | 
|  | 396 | status_t unflatten_binder(const sp<ProcessState>& proc, | 
|  | 397 | const flat_binder_object& flat, sp<IBinder>* out); | 
|  | 398 | status_t unflatten_binder(const sp<ProcessState>& proc, | 
|  | 399 | const flat_binder_object& flat, wp<IBinder>* out); | 
|  | 400 |  | 
|  | 401 | }; // namespace android | 
|  | 402 |  | 
|  | 403 | // --------------------------------------------------------------------------- | 
|  | 404 |  | 
|  | 405 | #endif // ANDROID_PARCEL_H |