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