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