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