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