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