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