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); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 112 | status_t writeString16(const std::unique_ptr<String16>& str); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | status_t writeString16(const char16_t* str, size_t len); |
| 114 | status_t writeStrongBinder(const sp<IBinder>& val); |
| 115 | status_t writeWeakBinder(const wp<IBinder>& val); |
Marco Nelissen | 5c0106e | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 116 | status_t writeInt32Array(size_t len, const int32_t *val); |
Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 117 | status_t writeByteArray(size_t len, const uint8_t *val); |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 118 | status_t writeBool(bool val); |
| 119 | status_t writeChar(char16_t val); |
| 120 | status_t writeByte(int8_t val); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 121 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 122 | status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 123 | status_t writeByteVector(const std::vector<int8_t>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 124 | status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 125 | status_t writeInt32Vector(const std::vector<int32_t>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 126 | status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 127 | status_t writeInt64Vector(const std::vector<int64_t>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 128 | status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 129 | status_t writeFloatVector(const std::vector<float>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 130 | status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 131 | status_t writeDoubleVector(const std::vector<double>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 132 | status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 133 | status_t writeBoolVector(const std::vector<bool>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 134 | status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 135 | status_t writeCharVector(const std::vector<char16_t>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 136 | status_t writeString16Vector( |
| 137 | const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 138 | status_t writeString16Vector(const std::vector<String16>& val); |
| 139 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 140 | status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val); |
Casey Dahlin | eb8e15f | 2015-11-03 13:50:37 -0800 | [diff] [blame] | 141 | status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val); |
| 142 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 143 | template<typename T> |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 144 | status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val); |
| 145 | template<typename T> |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 146 | status_t writeParcelableVector(const std::vector<T>& val); |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 147 | |
| 148 | template<typename T> |
| 149 | status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable); |
| 150 | |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 151 | status_t writeParcelable(const Parcelable& parcelable); |
| 152 | |
| 153 | template<typename T> |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 154 | status_t write(const Flattenable<T>& val); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 156 | template<typename T> |
| 157 | status_t write(const LightFlattenable<T>& val); |
| 158 | |
| 159 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 160 | // Place a native_handle into the parcel (the native_handle's file- |
| 161 | // 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] | 162 | // when this function returns). |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 163 | // Doesn't take ownership of the native_handle. |
| 164 | status_t writeNativeHandle(const native_handle* handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
| 166 | // Place a file descriptor into the parcel. The given fd must remain |
| 167 | // valid for the lifetime of the parcel. |
Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 168 | // The Parcel does not take ownership of the given fd unless you ask it to. |
| 169 | status_t writeFileDescriptor(int fd, bool takeOwnership = false); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | |
| 171 | // Place a file descriptor into the parcel. A dup of the fd is made, which |
| 172 | // will be closed once the parcel is destroyed. |
| 173 | status_t writeDupFileDescriptor(int fd); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 174 | |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 175 | // Place a file descriptor into the parcel. This will not affect the |
| 176 | // semantics of the smart file descriptor. A new descriptor will be |
| 177 | // created, and will be closed when the parcel is destroyed. |
| 178 | status_t writeUniqueFileDescriptor( |
| 179 | const ScopedFd& fd); |
| 180 | |
| 181 | // Place a vector of file desciptors into the parcel. Each descriptor is |
| 182 | // dup'd as in writeDupFileDescriptor |
| 183 | status_t writeUniqueFileDescriptorVector( |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 184 | const std::unique_ptr<std::vector<ScopedFd>>& val); |
| 185 | status_t writeUniqueFileDescriptorVector( |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 186 | const std::vector<ScopedFd>& val); |
| 187 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 188 | // Writes a blob to the parcel. |
| 189 | // 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] | 190 | // transferred by way of an anonymous shared memory region. Prefer sending |
| 191 | // immutable blobs if possible since they may be subsequently transferred between |
| 192 | // processes without further copying whereas mutable blobs always need to be copied. |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 193 | // The caller should call release() on the blob after writing its contents. |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 194 | status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob); |
| 195 | |
| 196 | // Write an existing immutable blob file descriptor to the parcel. |
| 197 | // This allows the client to send the same blob to multiple processes |
| 198 | // as long as it keeps a dup of the blob file descriptor handy for later. |
| 199 | status_t writeDupImmutableBlobFileDescriptor(int fd); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 200 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | status_t writeObject(const flat_binder_object& val, bool nullMetaData); |
| 202 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 203 | // Like Parcel.java's writeNoException(). Just writes a zero int32. |
| 204 | // Currently the native implementation doesn't do any of the StrictMode |
| 205 | // stack gathering and serialization that the Java implementation does. |
| 206 | status_t writeNoException(); |
| 207 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | void remove(size_t start, size_t amt); |
| 209 | |
| 210 | status_t read(void* outData, size_t len) const; |
| 211 | const void* readInplace(size_t len) const; |
| 212 | int32_t readInt32() const; |
| 213 | status_t readInt32(int32_t *pArg) const; |
Dan Stoza | 41a0f2f | 2014-12-01 10:01:10 -0800 | [diff] [blame] | 214 | uint32_t readUint32() const; |
| 215 | status_t readUint32(uint32_t *pArg) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | int64_t readInt64() const; |
| 217 | status_t readInt64(int64_t *pArg) const; |
Ronghua Wu | 2d13afd | 2015-03-16 11:11:07 -0700 | [diff] [blame] | 218 | uint64_t readUint64() const; |
| 219 | status_t readUint64(uint64_t *pArg) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | float readFloat() const; |
| 221 | status_t readFloat(float *pArg) const; |
| 222 | double readDouble() const; |
| 223 | status_t readDouble(double *pArg) const; |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 224 | intptr_t readIntPtr() const; |
| 225 | status_t readIntPtr(intptr_t *pArg) const; |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 226 | bool readBool() const; |
| 227 | status_t readBool(bool *pArg) const; |
| 228 | char16_t readChar() const; |
| 229 | status_t readChar(char16_t *pArg) const; |
| 230 | int8_t readByte() const; |
| 231 | status_t readByte(int8_t *pArg) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
| 233 | const char* readCString() const; |
| 234 | String8 readString8() const; |
| 235 | String16 readString16() const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 236 | status_t readString16(String16* pArg) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 237 | status_t readString16(std::unique_ptr<String16>* pArg) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | const char16_t* readString16Inplace(size_t* outLen) const; |
| 239 | sp<IBinder> readStrongBinder() const; |
Casey Dahlin | f0c1377 | 2015-10-27 18:33:56 -0700 | [diff] [blame] | 240 | status_t readStrongBinder(sp<IBinder>* val) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | wp<IBinder> readWeakBinder() const; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 242 | |
Casey Dahlin | f0c1377 | 2015-10-27 18:33:56 -0700 | [diff] [blame] | 243 | template<typename T> |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 244 | status_t readParcelableVector( |
| 245 | std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const; |
| 246 | template<typename T> |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 247 | status_t readParcelableVector(std::vector<T>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 248 | |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 249 | status_t readParcelable(Parcelable* parcelable) const; |
| 250 | |
| 251 | template<typename T> |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 252 | status_t readParcelable(std::unique_ptr<T>* parcelable) const; |
| 253 | |
| 254 | template<typename T> |
Casey Dahlin | eb8e15f | 2015-11-03 13:50:37 -0800 | [diff] [blame] | 255 | status_t readStrongBinder(sp<T>* val) const; |
| 256 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 257 | status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const; |
Casey Dahlin | eb8e15f | 2015-11-03 13:50:37 -0800 | [diff] [blame] | 258 | status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const; |
Casey Dahlin | f0c1377 | 2015-10-27 18:33:56 -0700 | [diff] [blame] | 259 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 260 | status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 261 | status_t readByteVector(std::vector<int8_t>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 262 | status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 263 | status_t readInt32Vector(std::vector<int32_t>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 264 | status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 265 | status_t readInt64Vector(std::vector<int64_t>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 266 | status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 267 | status_t readFloatVector(std::vector<float>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 268 | status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 269 | status_t readDoubleVector(std::vector<double>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 270 | status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 271 | status_t readBoolVector(std::vector<bool>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 272 | status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 273 | status_t readCharVector(std::vector<char16_t>* val) const; |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 274 | status_t readString16Vector( |
| 275 | std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 276 | status_t readString16Vector(std::vector<String16>* val) const; |
| 277 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 278 | template<typename T> |
| 279 | status_t read(Flattenable<T>& val) const; |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 280 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 281 | template<typename T> |
| 282 | status_t read(LightFlattenable<T>& val) const; |
| 283 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 284 | // Like Parcel.java's readExceptionCode(). Reads the first int32 |
| 285 | // off of a Parcel's header, returning 0 or the negative error |
| 286 | // code on exceptions, but also deals with skipping over rich |
| 287 | // response headers. Callers should use this to read & parse the |
| 288 | // response headers rather than doing it by hand. |
| 289 | int32_t readExceptionCode() const; |
| 290 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 291 | // Retrieve native_handle from the parcel. This returns a copy of the |
| 292 | // parcel's native_handle (the caller takes ownership). The caller |
| 293 | // must free the native_handle with native_handle_close() and |
| 294 | // native_handle_delete(). |
| 295 | native_handle* readNativeHandle() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | |
| 297 | |
| 298 | // Retrieve a file descriptor from the parcel. This returns the raw fd |
| 299 | // in the parcel, which you do not own -- use dup() to get your own copy. |
| 300 | int readFileDescriptor() const; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 301 | |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 302 | // Retrieve a smart file descriptor from the parcel. |
| 303 | status_t readUniqueFileDescriptor( |
| 304 | ScopedFd* val) const; |
| 305 | |
| 306 | |
| 307 | // Retrieve a vector of smart file descriptors from the parcel. |
| 308 | status_t readUniqueFileDescriptorVector( |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 309 | std::unique_ptr<std::vector<ScopedFd>>* val) const; |
| 310 | status_t readUniqueFileDescriptorVector( |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 311 | std::vector<ScopedFd>* val) const; |
| 312 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 313 | // Reads a blob from the parcel. |
| 314 | // The caller should call release() on the blob after reading its contents. |
| 315 | status_t readBlob(size_t len, ReadableBlob* outBlob) const; |
| 316 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | const flat_binder_object* readObject(bool nullMetaData) const; |
| 318 | |
| 319 | // Explicitly close all file descriptors in the parcel. |
| 320 | void closeFileDescriptors(); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 321 | |
| 322 | // Debugging: get metrics on current allocations. |
| 323 | static size_t getGlobalAllocSize(); |
| 324 | static size_t getGlobalAllocCount(); |
| 325 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 326 | private: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | typedef void (*release_func)(Parcel* parcel, |
| 328 | const uint8_t* data, size_t dataSize, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 329 | const binder_size_t* objects, size_t objectsSize, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | void* cookie); |
| 331 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 332 | uintptr_t ipcData() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | size_t ipcDataSize() const; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 334 | uintptr_t ipcObjects() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | size_t ipcObjectsCount() const; |
| 336 | void ipcSetDataReference(const uint8_t* data, size_t dataSize, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 337 | const binder_size_t* objects, size_t objectsCount, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | release_func relFunc, void* relCookie); |
| 339 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 340 | public: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | void print(TextOutput& to, uint32_t flags = 0) const; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 342 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | private: |
| 344 | Parcel(const Parcel& o); |
| 345 | Parcel& operator=(const Parcel& o); |
| 346 | |
| 347 | status_t finishWrite(size_t len); |
| 348 | void releaseObjects(); |
| 349 | void acquireObjects(); |
| 350 | status_t growData(size_t len); |
| 351 | status_t restartWrite(size_t desired); |
| 352 | status_t continueWrite(size_t desired); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 353 | status_t writePointer(uintptr_t val); |
| 354 | status_t readPointer(uintptr_t *pArg) const; |
| 355 | uintptr_t readPointer() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | void freeDataNoInit(); |
| 357 | void initState(); |
| 358 | void scanForFds() const; |
| 359 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 360 | template<class T> |
| 361 | status_t readAligned(T *pArg) const; |
| 362 | |
| 363 | template<class T> T readAligned() const; |
| 364 | |
| 365 | template<class T> |
| 366 | status_t writeAligned(T val); |
| 367 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 368 | status_t writeRawNullableParcelable(const Parcelable* |
| 369 | parcelable); |
| 370 | |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 371 | template<typename T, typename U> |
| 372 | status_t unsafeReadTypedVector(std::vector<T>* val, |
| 373 | status_t(Parcel::*read_func)(U*) const) const; |
| 374 | template<typename T> |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 375 | status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val, |
| 376 | status_t(Parcel::*read_func)(T*) const) const; |
| 377 | template<typename T> |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 378 | status_t readTypedVector(std::vector<T>* val, |
| 379 | status_t(Parcel::*read_func)(T*) const) const; |
| 380 | template<typename T, typename U> |
| 381 | status_t unsafeWriteTypedVector(const std::vector<T>& val, |
| 382 | status_t(Parcel::*write_func)(U)); |
| 383 | template<typename T> |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 384 | status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val, |
| 385 | status_t(Parcel::*write_func)(const T&)); |
| 386 | template<typename T> |
| 387 | status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val, |
| 388 | status_t(Parcel::*write_func)(T)); |
| 389 | template<typename T> |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 390 | status_t writeTypedVector(const std::vector<T>& val, |
| 391 | status_t(Parcel::*write_func)(const T&)); |
| 392 | template<typename T> |
| 393 | status_t writeTypedVector(const std::vector<T>& val, |
| 394 | status_t(Parcel::*write_func)(T)); |
| 395 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | status_t mError; |
| 397 | uint8_t* mData; |
| 398 | size_t mDataSize; |
| 399 | size_t mDataCapacity; |
| 400 | mutable size_t mDataPos; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 401 | binder_size_t* mObjects; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | size_t mObjectsSize; |
| 403 | size_t mObjectsCapacity; |
| 404 | mutable size_t mNextObjectHint; |
| 405 | |
| 406 | mutable bool mFdsKnown; |
| 407 | mutable bool mHasFds; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 408 | bool mAllowFds; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | |
| 410 | release_func mOwner; |
| 411 | void* mOwnerCookie; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 412 | |
| 413 | class Blob { |
| 414 | public: |
| 415 | Blob(); |
| 416 | ~Blob(); |
| 417 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 418 | void clear(); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 419 | void release(); |
| 420 | inline size_t size() const { return mSize; } |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 421 | inline int fd() const { return mFd; }; |
| 422 | inline bool isMutable() const { return mMutable; } |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 423 | |
| 424 | protected: |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 425 | void init(int fd, void* data, size_t size, bool isMutable); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 426 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 427 | int mFd; // owned by parcel so not closed when released |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 428 | void* mData; |
| 429 | size_t mSize; |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 430 | bool mMutable; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 431 | }; |
| 432 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 433 | class FlattenableHelperInterface { |
| 434 | protected: |
| 435 | ~FlattenableHelperInterface() { } |
| 436 | public: |
| 437 | virtual size_t getFlattenedSize() const = 0; |
| 438 | virtual size_t getFdCount() const = 0; |
| 439 | virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0; |
| 440 | virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0; |
| 441 | }; |
| 442 | |
| 443 | template<typename T> |
| 444 | class FlattenableHelper : public FlattenableHelperInterface { |
| 445 | friend class Parcel; |
| 446 | const Flattenable<T>& val; |
| 447 | explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { } |
| 448 | |
| 449 | public: |
| 450 | virtual size_t getFlattenedSize() const { |
| 451 | return val.getFlattenedSize(); |
| 452 | } |
| 453 | virtual size_t getFdCount() const { |
| 454 | return val.getFdCount(); |
| 455 | } |
| 456 | virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const { |
| 457 | return val.flatten(buffer, size, fds, count); |
| 458 | } |
| 459 | virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) { |
| 460 | return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count); |
| 461 | } |
| 462 | }; |
| 463 | status_t write(const FlattenableHelperInterface& val); |
| 464 | status_t read(FlattenableHelperInterface& val) const; |
| 465 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 466 | public: |
| 467 | class ReadableBlob : public Blob { |
| 468 | friend class Parcel; |
| 469 | public: |
| 470 | inline const void* data() const { return mData; } |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 471 | inline void* mutableData() { return isMutable() ? mData : NULL; } |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | class WritableBlob : public Blob { |
| 475 | friend class Parcel; |
| 476 | public: |
| 477 | inline void* data() { return mData; } |
| 478 | }; |
Dan Sandler | aa5c234 | 2015-04-10 10:08:45 -0400 | [diff] [blame] | 479 | |
| 480 | private: |
Adrian Roos | cbf3726 | 2015-10-22 16:12:53 -0700 | [diff] [blame] | 481 | size_t mOpenAshmemSize; |
Dan Sandler | aa5c234 | 2015-04-10 10:08:45 -0400 | [diff] [blame] | 482 | |
| 483 | public: |
Adrian Roos | 6bb3114 | 2015-10-22 16:46:12 -0700 | [diff] [blame] | 484 | // TODO: Remove once ABI can be changed. |
Dan Sandler | aa5c234 | 2015-04-10 10:08:45 -0400 | [diff] [blame] | 485 | size_t getBlobAshmemSize() const; |
Adrian Roos | cbf3726 | 2015-10-22 16:12:53 -0700 | [diff] [blame] | 486 | size_t getOpenAshmemSize() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | // --------------------------------------------------------------------------- |
| 490 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 491 | template<typename T> |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 492 | status_t Parcel::write(const Flattenable<T>& val) { |
| 493 | const FlattenableHelper<T> helper(val); |
| 494 | return write(helper); |
| 495 | } |
| 496 | |
| 497 | template<typename T> |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 498 | status_t Parcel::write(const LightFlattenable<T>& val) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 499 | size_t size(val.getFlattenedSize()); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 500 | if (!val.isFixedSize()) { |
| 501 | status_t err = writeInt32(size); |
| 502 | if (err != NO_ERROR) { |
| 503 | return err; |
| 504 | } |
| 505 | } |
Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 506 | if (size) { |
| 507 | void* buffer = writeInplace(size); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 508 | if (buffer == NULL) |
| 509 | return NO_MEMORY; |
| 510 | return val.flatten(buffer, size); |
Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 511 | } |
| 512 | return NO_ERROR; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | template<typename T> |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 516 | status_t Parcel::read(Flattenable<T>& val) const { |
| 517 | FlattenableHelper<T> helper(val); |
| 518 | return read(helper); |
| 519 | } |
| 520 | |
| 521 | template<typename T> |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 522 | status_t Parcel::read(LightFlattenable<T>& val) const { |
| 523 | size_t size; |
| 524 | if (val.isFixedSize()) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 525 | size = val.getFlattenedSize(); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 526 | } else { |
| 527 | int32_t s; |
| 528 | status_t err = readInt32(&s); |
| 529 | if (err != NO_ERROR) { |
| 530 | return err; |
| 531 | } |
| 532 | size = s; |
| 533 | } |
Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 534 | if (size) { |
| 535 | void const* buffer = readInplace(size); |
| 536 | return buffer == NULL ? NO_MEMORY : |
| 537 | val.unflatten(buffer, size); |
| 538 | } |
| 539 | return NO_ERROR; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Casey Dahlin | f0c1377 | 2015-10-27 18:33:56 -0700 | [diff] [blame] | 542 | template<typename T> |
| 543 | status_t Parcel::readStrongBinder(sp<T>* val) const { |
| 544 | sp<IBinder> tmp; |
| 545 | status_t ret = readStrongBinder(&tmp); |
| 546 | |
| 547 | if (ret == OK) { |
| 548 | *val = interface_cast<T>(tmp); |
| 549 | |
| 550 | if (val->get() == nullptr) { |
| 551 | return UNKNOWN_ERROR; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 558 | template<typename T, typename U> |
| 559 | status_t Parcel::unsafeReadTypedVector( |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 560 | std::vector<T>* val, |
| 561 | status_t(Parcel::*read_func)(U*) const) const { |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 562 | int32_t size; |
| 563 | status_t status = this->readInt32(&size); |
| 564 | |
| 565 | if (status != OK) { |
| 566 | return status; |
| 567 | } |
| 568 | |
| 569 | if (size < 0) { |
| 570 | return UNEXPECTED_NULL; |
| 571 | } |
| 572 | |
| 573 | val->resize(size); |
| 574 | |
| 575 | for (auto& v: *val) { |
| 576 | status = (this->*read_func)(&v); |
| 577 | |
| 578 | if (status != OK) { |
| 579 | return status; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | return OK; |
| 584 | } |
| 585 | |
| 586 | template<typename T> |
| 587 | status_t Parcel::readTypedVector(std::vector<T>* val, |
| 588 | status_t(Parcel::*read_func)(T*) const) const { |
| 589 | return unsafeReadTypedVector(val, read_func); |
| 590 | } |
| 591 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 592 | template<typename T> |
| 593 | status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val, |
| 594 | status_t(Parcel::*read_func)(T*) const) const { |
| 595 | const int32_t start = dataPosition(); |
| 596 | int32_t size; |
| 597 | status_t status = readInt32(&size); |
| 598 | val->reset(); |
| 599 | |
| 600 | if (status != OK || size < 0) { |
| 601 | return status; |
| 602 | } |
| 603 | |
| 604 | setDataPosition(start); |
| 605 | val->reset(new std::vector<T>()); |
| 606 | |
| 607 | status = unsafeReadTypedVector(val->get(), read_func); |
| 608 | |
| 609 | if (status != OK) { |
| 610 | val->reset(); |
| 611 | } |
| 612 | |
| 613 | return status; |
| 614 | } |
| 615 | |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 616 | template<typename T, typename U> |
| 617 | status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val, |
| 618 | status_t(Parcel::*write_func)(U)) { |
| 619 | if (val.size() > std::numeric_limits<int32_t>::max()) { |
| 620 | return BAD_VALUE; |
| 621 | } |
| 622 | |
| 623 | status_t status = this->writeInt32(val.size()); |
| 624 | |
| 625 | if (status != OK) { |
| 626 | return status; |
| 627 | } |
| 628 | |
| 629 | for (const auto& item : val) { |
| 630 | status = (this->*write_func)(item); |
| 631 | |
| 632 | if (status != OK) { |
| 633 | return status; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return OK; |
| 638 | } |
| 639 | |
| 640 | template<typename T> |
| 641 | status_t Parcel::writeTypedVector(const std::vector<T>& val, |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 642 | status_t(Parcel::*write_func)(const T&)) { |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 643 | return unsafeWriteTypedVector(val, write_func); |
| 644 | } |
| 645 | |
| 646 | template<typename T> |
| 647 | status_t Parcel::writeTypedVector(const std::vector<T>& val, |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 648 | status_t(Parcel::*write_func)(T)) { |
Christopher Wiley | 03d1eb6 | 2015-11-19 06:42:40 -0800 | [diff] [blame] | 649 | return unsafeWriteTypedVector(val, write_func); |
| 650 | } |
| 651 | |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 652 | template<typename T> |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 653 | status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val, |
| 654 | status_t(Parcel::*write_func)(const T&)) { |
| 655 | if (val.get() == nullptr) { |
| 656 | return this->writeInt32(-1); |
| 657 | } |
| 658 | |
| 659 | return unsafeWriteTypedVector(*val, write_func); |
| 660 | } |
| 661 | |
| 662 | template<typename T> |
| 663 | status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val, |
| 664 | status_t(Parcel::*write_func)(T)) { |
| 665 | if (val.get() == nullptr) { |
| 666 | return this->writeInt32(-1); |
| 667 | } |
| 668 | |
| 669 | return unsafeWriteTypedVector(*val, write_func); |
| 670 | } |
| 671 | |
| 672 | template<typename T> |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 673 | status_t Parcel::readParcelableVector(std::vector<T>* val) const { |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 674 | return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable); |
| 675 | } |
| 676 | |
| 677 | template<typename T> |
| 678 | status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const { |
| 679 | const int32_t start = dataPosition(); |
| 680 | int32_t size; |
| 681 | status_t status = readInt32(&size); |
| 682 | val->reset(); |
| 683 | |
| 684 | if (status != OK || size < 0) { |
| 685 | return status; |
| 686 | } |
| 687 | |
| 688 | setDataPosition(start); |
| 689 | val->reset(new std::vector<T>()); |
| 690 | |
| 691 | status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable); |
| 692 | |
| 693 | if (status != OK) { |
| 694 | val->reset(); |
| 695 | } |
| 696 | |
| 697 | return status; |
| 698 | } |
| 699 | |
| 700 | template<typename T> |
| 701 | status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const { |
| 702 | const int32_t start = dataPosition(); |
| 703 | int32_t present; |
| 704 | status_t status = readInt32(&present); |
| 705 | parcelable->reset(); |
| 706 | |
| 707 | if (status != OK || !present) { |
| 708 | return status; |
| 709 | } |
| 710 | |
| 711 | setDataPosition(start); |
| 712 | parcelable->reset(new T()); |
| 713 | |
| 714 | status = readParcelable(parcelable->get()); |
| 715 | |
| 716 | if (status != OK) { |
| 717 | parcelable->reset(); |
| 718 | } |
| 719 | |
| 720 | return status; |
| 721 | } |
| 722 | |
| 723 | template<typename T> |
| 724 | status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) { |
| 725 | return writeRawNullableParcelable(parcelable.get()); |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | template<typename T> |
| 729 | status_t Parcel::writeParcelableVector(const std::vector<T>& val) { |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame^] | 730 | return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable); |
| 731 | } |
| 732 | |
| 733 | template<typename T> |
| 734 | status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) { |
| 735 | if (val.get() == nullptr) { |
| 736 | return this->writeInt32(-1); |
| 737 | } |
| 738 | |
| 739 | return unsafeWriteTypedVector(*val, &Parcel::writeParcelable); |
Christopher Wiley | 97f048d | 2015-11-19 06:49:05 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 742 | // --------------------------------------------------------------------------- |
| 743 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) |
| 745 | { |
| 746 | parcel.print(to); |
| 747 | return to; |
| 748 | } |
| 749 | |
| 750 | // --------------------------------------------------------------------------- |
| 751 | |
| 752 | // Generic acquire and release of objects. |
| 753 | void acquire_object(const sp<ProcessState>& proc, |
Ian Pedowitz | 6880307 | 2015-10-22 22:08:10 +0000 | [diff] [blame] | 754 | const flat_binder_object& obj, const void* who); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | void release_object(const sp<ProcessState>& proc, |
Ian Pedowitz | 6880307 | 2015-10-22 22:08:10 +0000 | [diff] [blame] | 756 | const flat_binder_object& obj, const void* who); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | |
| 758 | void flatten_binder(const sp<ProcessState>& proc, |
| 759 | const sp<IBinder>& binder, flat_binder_object* out); |
| 760 | void flatten_binder(const sp<ProcessState>& proc, |
| 761 | const wp<IBinder>& binder, flat_binder_object* out); |
| 762 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 763 | const flat_binder_object& flat, sp<IBinder>* out); |
| 764 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 765 | const flat_binder_object& flat, wp<IBinder>* out); |
| 766 | |
| 767 | }; // namespace android |
| 768 | |
| 769 | // --------------------------------------------------------------------------- |
| 770 | |
| 771 | #endif // ANDROID_PARCEL_H |