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