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