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