1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2005 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#ifndef ANDROID_PARCEL_H
18edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define ANDROID_PARCEL_H
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
209a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley#include <string>
21451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin#include <vector>
22451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <cutils/native_handle.h>
2406673e38983baad380e818c5493b7faccf3b2393Casey Dahlin#include <nativehelper/ScopedFd.h>
25edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Errors.h>
26edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/RefBase.h>
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/String16.h>
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Vector.h>
298683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian#include <utils/Flattenable.h>
3084e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg#include <linux/binder.h>
31edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
32f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin#include <binder/IInterface.h>
3397f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley#include <binder/Parcelable.h>
34f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin
35edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
36edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
37edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
38e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopiantemplate <typename T> class Flattenable;
398683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopiantemplate <typename T> class LightFlattenable;
40edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass IBinder;
4170081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrickclass IPCThreadState;
42edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass ProcessState;
43edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass String8;
44edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass TextOutput;
45edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
46e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopianclass Parcel {
47f683e0163a84d93448b9388126902242367cd961Serban Constantinescu    friend class IPCThreadState;
48edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectpublic:
495707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    class ReadableBlob;
505707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    class WritableBlob;
515707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                        Parcel();
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                        ~Parcel();
54edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
55edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const uint8_t*      data() const;
56edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              dataSize() const;
57edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              dataAvail() const;
58edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              dataPosition() const;
59edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              dataCapacity() const;
608938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn
61edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            setDataSize(size_t size);
62edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                setDataPosition(size_t pos) const;
63edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            setDataCapacity(size_t size);
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
65edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            setData(const uint8_t* buffer, size_t len);
66edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
6751faf46e2958c57dab46d9420e585319125c7c42Andreas Huber    status_t            appendFrom(const Parcel *parcel,
6851faf46e2958c57dab46d9420e585319125c7c42Andreas Huber                                   size_t start, size_t len);
69edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7013b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    bool                allowFds() const;
717746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn    bool                pushAllowFds(bool allowFds);
727746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn    void                restoreAllowFds(bool lastValue);
738938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn
74edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    bool                hasFileDescriptors() const;
75edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
76702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick    // Writes the RPC header.
77edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeInterfaceToken(const String16& interface);
78a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick
79702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick    // Parses the RPC header, returning true if the interface name
80702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick    // in the header matches the expected interface from the caller.
8170081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    //
8270081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    // Additionally, enforceInterface does part of the work of
8370081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    // propagating the StrictMode policy mask, populating the current
8470081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    // IPCThreadState, which as an optimization may optionally be
8570081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    // passed in.
86a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick    bool                enforceInterface(const String16& interface,
8770081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick                                         IPCThreadState* threadState = NULL) const;
88702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick    bool                checkInterface(IBinder*) const;
8983c0446f27b9542d6c2e724817b2b2d8d1f55085Mathias Agopian
90edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                freeData();
91edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
9284e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevågprivate:
9384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg    const binder_size_t* objects() const;
9484e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg
9584e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevågpublic:
96edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              objectsCount() const;
97edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
98edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            errorCheck() const;
99edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                setError(status_t err);
100edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
101edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            write(const void* data, size_t len);
102edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void*               writeInplace(size_t len);
103edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeUnpadded(const void* data, size_t len);
104edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeInt32(int32_t val);
10541a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza    status_t            writeUint32(uint32_t val);
106edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeInt64(int64_t val);
1072d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu    status_t            writeUint64(uint64_t val);
108edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeFloat(float val);
109edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeDouble(double val);
110edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeCString(const char* str);
111edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeString8(const String8& str);
112edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeString16(const String16& str);
113b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeString16(const std::unique_ptr<String16>& str);
114edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeString16(const char16_t* str, size_t len);
115edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeStrongBinder(const sp<IBinder>& val);
116edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeWeakBinder(const wp<IBinder>& val);
1175c0106e134366312206702f1b072347e20c90374Marco Nelissen    status_t            writeInt32Array(size_t len, const int32_t *val);
118f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen    status_t            writeByteArray(size_t len, const uint8_t *val);
119d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    status_t            writeBool(bool val);
120d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    status_t            writeChar(char16_t val);
121d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    status_t            writeByte(int8_t val);
122e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
1239a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    // Take a UTF8 encoded string, convert to UTF16, write it to the parcel.
1249a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            writeUtf8AsUtf16(const std::string& str);
1259a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            writeUtf8AsUtf16(const std::unique_ptr<std::string>& str);
1269a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
127b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val);
128451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeByteVector(const std::vector<int8_t>& val);
1290b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    status_t            writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val);
1300b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    status_t            writeByteVector(const std::vector<uint8_t>& val);
131b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val);
132451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeInt32Vector(const std::vector<int32_t>& val);
133b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val);
134451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeInt64Vector(const std::vector<int64_t>& val);
135b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeFloatVector(const std::unique_ptr<std::vector<float>>& val);
136451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeFloatVector(const std::vector<float>& val);
137b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeDoubleVector(const std::unique_ptr<std::vector<double>>& val);
138451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeDoubleVector(const std::vector<double>& val);
139b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeBoolVector(const std::unique_ptr<std::vector<bool>>& val);
140451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeBoolVector(const std::vector<bool>& val);
141b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val);
142451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeCharVector(const std::vector<char16_t>& val);
143b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeString16Vector(
144b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                            const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val);
145451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            writeString16Vector(const std::vector<String16>& val);
1469a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            writeUtf8VectorAsUtf16Vector(
1479a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley                            const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val);
1489a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val);
149451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
150b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val);
151eb8e15f9bb29f8794f8be819530631c358fd6a15Casey Dahlin    status_t            writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
152eb8e15f9bb29f8794f8be819530631c358fd6a15Casey Dahlin
153e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    template<typename T>
154b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val);
155b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
15697f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley    status_t            writeParcelableVector(const std::vector<T>& val);
157b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
158b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
159b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeNullableParcelable(const std::unique_ptr<T>& parcelable);
160b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
16197f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley    status_t            writeParcelable(const Parcelable& parcelable);
16297f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley
16397f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley    template<typename T>
164e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    status_t            write(const Flattenable<T>& val);
165edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1668683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    template<typename T>
1678683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    status_t            write(const LightFlattenable<T>& val);
1688683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian
1698683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian
170a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // Place a native_handle into the parcel (the native_handle's file-
171a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // descriptors are dup'ed, so it is safe to delete the native_handle
172451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    // when this function returns).
173a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // Doesn't take ownership of the native_handle.
174a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    status_t            writeNativeHandle(const native_handle* handle);
175edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
176edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Place a file descriptor into the parcel.  The given fd must remain
177edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // valid for the lifetime of the parcel.
17893ff1f985e233fe04412a65041e977c9f0c21f5bJeff Brown    // The Parcel does not take ownership of the given fd unless you ask it to.
17993ff1f985e233fe04412a65041e977c9f0c21f5bJeff Brown    status_t            writeFileDescriptor(int fd, bool takeOwnership = false);
180edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
181edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Place a file descriptor into the parcel.  A dup of the fd is made, which
182edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // will be closed once the parcel is destroyed.
183edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeDupFileDescriptor(int fd);
1845707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
18506673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // Place a file descriptor into the parcel.  This will not affect the
18606673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // semantics of the smart file descriptor. A new descriptor will be
18706673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // created, and will be closed when the parcel is destroyed.
18806673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    status_t            writeUniqueFileDescriptor(
18906673e38983baad380e818c5493b7faccf3b2393Casey Dahlin                            const ScopedFd& fd);
19006673e38983baad380e818c5493b7faccf3b2393Casey Dahlin
19106673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // Place a vector of file desciptors into the parcel. Each descriptor is
19206673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // dup'd as in writeDupFileDescriptor
19306673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    status_t            writeUniqueFileDescriptorVector(
194b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                            const std::unique_ptr<std::vector<ScopedFd>>& val);
195b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeUniqueFileDescriptorVector(
19606673e38983baad380e818c5493b7faccf3b2393Casey Dahlin                            const std::vector<ScopedFd>& val);
19706673e38983baad380e818c5493b7faccf3b2393Casey Dahlin
1985707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    // Writes a blob to the parcel.
1995707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    // If the blob is small, then it is stored in-place, otherwise it is
20013b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    // transferred by way of an anonymous shared memory region.  Prefer sending
20113b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    // immutable blobs if possible since they may be subsequently transferred between
20213b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    // processes without further copying whereas mutable blobs always need to be copied.
2035707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    // The caller should call release() on the blob after writing its contents.
20413b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    status_t            writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
20513b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown
20613b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    // Write an existing immutable blob file descriptor to the parcel.
20713b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    // This allows the client to send the same blob to multiple processes
20813b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    // as long as it keeps a dup of the blob file descriptor handy for later.
20913b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    status_t            writeDupImmutableBlobFileDescriptor(int fd);
2105707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
211edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            writeObject(const flat_binder_object& val, bool nullMetaData);
212edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
213837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // Like Parcel.java's writeNoException().  Just writes a zero int32.
214837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // Currently the native implementation doesn't do any of the StrictMode
215837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // stack gathering and serialization that the Java implementation does.
216837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    status_t            writeNoException();
217837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick
218edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                remove(size_t start, size_t amt);
219edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
220edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            read(void* outData, size_t len) const;
221edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const void*         readInplace(size_t len) const;
222edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int32_t             readInt32() const;
223edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            readInt32(int32_t *pArg) const;
22441a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza    uint32_t            readUint32() const;
22541a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza    status_t            readUint32(uint32_t *pArg) const;
226edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int64_t             readInt64() const;
227edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            readInt64(int64_t *pArg) const;
2282d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu    uint64_t            readUint64() const;
2292d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu    status_t            readUint64(uint64_t *pArg) const;
230edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    float               readFloat() const;
231edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            readFloat(float *pArg) const;
232edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    double              readDouble() const;
233edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            readDouble(double *pArg) const;
23484a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    intptr_t            readIntPtr() const;
23584a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    status_t            readIntPtr(intptr_t *pArg) const;
236d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    bool                readBool() const;
237d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    status_t            readBool(bool *pArg) const;
238d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    char16_t            readChar() const;
239d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    status_t            readChar(char16_t *pArg) const;
240d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    int8_t              readByte() const;
241d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    status_t            readByte(int8_t *pArg) const;
242edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2439a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    // Read a UTF16 encoded string, convert to UTF8
2449a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            readUtf8FromUtf16(std::string* str) const;
2459a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
2469a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
247edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const char*         readCString() const;
248edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    String8             readString8() const;
249edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    String16            readString16() const;
250451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readString16(String16* pArg) const;
251b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readString16(std::unique_ptr<String16>* pArg) const;
252edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const char16_t*     readString16Inplace(size_t* outLen) const;
253edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    sp<IBinder>         readStrongBinder() const;
254f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    status_t            readStrongBinder(sp<IBinder>* val) const;
255edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    wp<IBinder>         readWeakBinder() const;
256e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
257f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    template<typename T>
258b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readParcelableVector(
259b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                            std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
260b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
26197f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley    status_t            readParcelableVector(std::vector<T>* val) const;
262b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
26397f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley    status_t            readParcelable(Parcelable* parcelable) const;
26497f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley
26597f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley    template<typename T>
266b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readParcelable(std::unique_ptr<T>* parcelable) const;
267b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
268b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
269eb8e15f9bb29f8794f8be819530631c358fd6a15Casey Dahlin    status_t            readStrongBinder(sp<T>* val) const;
270eb8e15f9bb29f8794f8be819530631c358fd6a15Casey Dahlin
271b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
272eb8e15f9bb29f8794f8be819530631c358fd6a15Casey Dahlin    status_t            readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
273f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin
274b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
275451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readByteVector(std::vector<int8_t>* val) const;
2760b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    status_t            readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const;
2770b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    status_t            readByteVector(std::vector<uint8_t>* val) const;
278b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
279451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readInt32Vector(std::vector<int32_t>* val) const;
280b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
281451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readInt64Vector(std::vector<int64_t>* val) const;
282b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
283451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readFloatVector(std::vector<float>* val) const;
284b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
285451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readDoubleVector(std::vector<double>* val) const;
286b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
287451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readBoolVector(std::vector<bool>* val) const;
288b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
289451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readCharVector(std::vector<char16_t>* val) const;
290b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readString16Vector(
291b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                            std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
292451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    status_t            readString16Vector(std::vector<String16>* val) const;
2939a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            readUtf8VectorFromUtf16Vector(
2949a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley                            std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
2959a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t            readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
296451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
297e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    template<typename T>
298e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    status_t            read(Flattenable<T>& val) const;
299837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick
3008683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    template<typename T>
3018683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    status_t            read(LightFlattenable<T>& val) const;
3028683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian
303837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // Like Parcel.java's readExceptionCode().  Reads the first int32
304837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // off of a Parcel's header, returning 0 or the negative error
305837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // code on exceptions, but also deals with skipping over rich
306837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // response headers.  Callers should use this to read & parse the
307837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    // response headers rather than doing it by hand.
308837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick    int32_t             readExceptionCode() const;
309837a0d0fb2c3fba8082d47d04cb6120af1eb9a54Brad Fitzpatrick
310a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // Retrieve native_handle from the parcel. This returns a copy of the
311a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // parcel's native_handle (the caller takes ownership). The caller
312a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // must free the native_handle with native_handle_close() and
313a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    // native_handle_delete().
314a47f02afb1e1f2c4abd9379c13903a5dde89d354Mathias Agopian    native_handle*     readNativeHandle() const;
315edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
316edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
317edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Retrieve a file descriptor from the parcel.  This returns the raw fd
318edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // in the parcel, which you do not own -- use dup() to get your own copy.
319edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int                 readFileDescriptor() const;
3205707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
32106673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // Retrieve a smart file descriptor from the parcel.
32206673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    status_t            readUniqueFileDescriptor(
32306673e38983baad380e818c5493b7faccf3b2393Casey Dahlin                            ScopedFd* val) const;
32406673e38983baad380e818c5493b7faccf3b2393Casey Dahlin
32506673e38983baad380e818c5493b7faccf3b2393Casey Dahlin
32606673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    // Retrieve a vector of smart file descriptors from the parcel.
32706673e38983baad380e818c5493b7faccf3b2393Casey Dahlin    status_t            readUniqueFileDescriptorVector(
328b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                            std::unique_ptr<std::vector<ScopedFd>>* val) const;
329b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readUniqueFileDescriptorVector(
33006673e38983baad380e818c5493b7faccf3b2393Casey Dahlin                            std::vector<ScopedFd>* val) const;
33106673e38983baad380e818c5493b7faccf3b2393Casey Dahlin
3325707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    // Reads a blob from the parcel.
3335707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    // The caller should call release() on the blob after reading its contents.
3345707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    status_t            readBlob(size_t len, ReadableBlob* outBlob) const;
3355707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
336edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const flat_binder_object* readObject(bool nullMetaData) const;
337edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
338edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Explicitly close all file descriptors in the parcel.
339edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                closeFileDescriptors();
3407e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn
3417e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn    // Debugging: get metrics on current allocations.
3427e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn    static size_t       getGlobalAllocSize();
3437e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn    static size_t       getGlobalAllocCount();
3447e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn
34584e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevågprivate:
346edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    typedef void        (*release_func)(Parcel* parcel,
347edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                        const uint8_t* data, size_t dataSize,
34884e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                                        const binder_size_t* objects, size_t objectsSize,
349edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                        void* cookie);
350edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
35184e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg    uintptr_t           ipcData() const;
352edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              ipcDataSize() const;
35384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg    uintptr_t           ipcObjects() const;
354edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              ipcObjectsCount() const;
355edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                ipcSetDataReference(const uint8_t* data, size_t dataSize,
35684e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                                            const binder_size_t* objects, size_t objectsCount,
357edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                            release_func relFunc, void* relCookie);
358edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
35984e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevågpublic:
360edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                print(TextOutput& to, uint32_t flags = 0) const;
3615707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
362edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectprivate:
363edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                        Parcel(const Parcel& o);
364edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Parcel&             operator=(const Parcel& o);
365edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
366edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            finishWrite(size_t len);
367edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                releaseObjects();
368edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                acquireObjects();
369edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            growData(size_t len);
370edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            restartWrite(size_t desired);
371edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            continueWrite(size_t desired);
372f683e0163a84d93448b9388126902242367cd961Serban Constantinescu    status_t            writePointer(uintptr_t val);
373f683e0163a84d93448b9388126902242367cd961Serban Constantinescu    status_t            readPointer(uintptr_t *pArg) const;
374f683e0163a84d93448b9388126902242367cd961Serban Constantinescu    uintptr_t           readPointer() const;
375edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                freeDataNoInit();
376edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                initState();
377edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void                scanForFds() const;
378edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
37984a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    template<class T>
38084a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    status_t            readAligned(T *pArg) const;
38184a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber
38284a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    template<class T>   T readAligned() const;
38384a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber
38484a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    template<class T>
38584a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    status_t            writeAligned(T val);
38684a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber
387b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeRawNullableParcelable(const Parcelable*
388b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                                   parcelable);
389b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
39003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    template<typename T, typename U>
39103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t            unsafeReadTypedVector(std::vector<T>* val,
39203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                              status_t(Parcel::*read_func)(U*) const) const;
39303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    template<typename T>
394b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
395b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                                status_t(Parcel::*read_func)(T*) const) const;
396b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
39703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t            readTypedVector(std::vector<T>* val,
39803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                        status_t(Parcel::*read_func)(T*) const) const;
39903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    template<typename T, typename U>
40003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t            unsafeWriteTypedVector(const std::vector<T>& val,
40103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                               status_t(Parcel::*write_func)(U));
40203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    template<typename T>
403b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
404b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                                 status_t(Parcel::*write_func)(const T&));
405b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
406b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t            writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
407b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                                 status_t(Parcel::*write_func)(T));
408b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    template<typename T>
40903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t            writeTypedVector(const std::vector<T>& val,
41003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                         status_t(Parcel::*write_func)(const T&));
41103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    template<typename T>
41203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t            writeTypedVector(const std::vector<T>& val,
41303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                         status_t(Parcel::*write_func)(T));
41403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
415edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t            mError;
416edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    uint8_t*            mData;
417edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              mDataSize;
418edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              mDataCapacity;
419edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mutable size_t      mDataPos;
42084e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg    binder_size_t*      mObjects;
421edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              mObjectsSize;
422edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t              mObjectsCapacity;
423edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mutable size_t      mNextObjectHint;
424edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
425edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mutable bool        mFdsKnown;
426edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mutable bool        mHasFds;
4278938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn    bool                mAllowFds;
428e4e0ae8abc7f612960fc381a792d0bef4c3eb6f9Christopher Tate
429edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    release_func        mOwner;
430edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void*               mOwnerCookie;
4315707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
4325707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    class Blob {
4335707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    public:
4345707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        Blob();
4355707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        ~Blob();
4365707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
43713b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        void clear();
4385707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        void release();
4395707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        inline size_t size() const { return mSize; }
44013b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        inline int fd() const { return mFd; };
44113b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        inline bool isMutable() const { return mMutable; }
4425707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
4435707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    protected:
44413b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        void init(int fd, void* data, size_t size, bool isMutable);
4455707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
44613b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        int mFd; // owned by parcel so not closed when released
4475707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        void* mData;
4485707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        size_t mSize;
44913b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        bool mMutable;
4505707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    };
4515707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
452e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    class FlattenableHelperInterface {
453e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    protected:
454e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        ~FlattenableHelperInterface() { }
455e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    public:
456e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual size_t getFlattenedSize() const = 0;
457e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual size_t getFdCount() const = 0;
458e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
459e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
460e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    };
461e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
462e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    template<typename T>
463e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    class FlattenableHelper : public FlattenableHelperInterface {
464e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        friend class Parcel;
465e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        const Flattenable<T>& val;
466e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
467e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
468e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    public:
469e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual size_t getFlattenedSize() const {
470e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian            return val.getFlattenedSize();
471e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        }
472e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual size_t getFdCount() const {
473e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian            return val.getFdCount();
474e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        }
475e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
476e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian            return val.flatten(buffer, size, fds, count);
477e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        }
478e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
479e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian            return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
480e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        }
481e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    };
482e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    status_t write(const FlattenableHelperInterface& val);
483e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    status_t read(FlattenableHelperInterface& val) const;
484e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
4855707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brownpublic:
4865707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    class ReadableBlob : public Blob {
4875707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        friend class Parcel;
4885707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    public:
4895707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        inline const void* data() const { return mData; }
49013b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown        inline void* mutableData() { return isMutable() ? mData : NULL; }
4915707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    };
4925707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown
4935707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    class WritableBlob : public Blob {
4945707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        friend class Parcel;
4955707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    public:
4965707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown        inline void* data() { return mData; }
4975707dbf15d0b44c88fbaa6dd271097f2d42932e0Jeff Brown    };
498aa5c2346c7291465aaca53f59878582dccbe4835Dan Sandler
499aa5c2346c7291465aaca53f59878582dccbe4835Dan Sandlerprivate:
500cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos    size_t mOpenAshmemSize;
501aa5c2346c7291465aaca53f59878582dccbe4835Dan Sandler
502aa5c2346c7291465aaca53f59878582dccbe4835Dan Sandlerpublic:
5036bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos    // TODO: Remove once ABI can be changed.
504aa5c2346c7291465aaca53f59878582dccbe4835Dan Sandler    size_t getBlobAshmemSize() const;
505cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos    size_t getOpenAshmemSize() const;
506edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};
507edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
508edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
509edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
5108683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopiantemplate<typename T>
511e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopianstatus_t Parcel::write(const Flattenable<T>& val) {
512e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    const FlattenableHelper<T> helper(val);
513e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    return write(helper);
514e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian}
515e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
516e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopiantemplate<typename T>
5178683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopianstatus_t Parcel::write(const LightFlattenable<T>& val) {
518e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    size_t size(val.getFlattenedSize());
5198683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    if (!val.isFixedSize()) {
5208683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        status_t err = writeInt32(size);
5218683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        if (err != NO_ERROR) {
5228683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian            return err;
5238683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        }
5248683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    }
5252098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian    if (size) {
5262098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian        void* buffer = writeInplace(size);
527e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        if (buffer == NULL)
528e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian            return NO_MEMORY;
529e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        return val.flatten(buffer, size);
5302098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian    }
5312098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian    return NO_ERROR;
5328683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian}
5338683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian
5348683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopiantemplate<typename T>
535e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopianstatus_t Parcel::read(Flattenable<T>& val) const {
536e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    FlattenableHelper<T> helper(val);
537e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    return read(helper);
538e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian}
539e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
540e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopiantemplate<typename T>
5418683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopianstatus_t Parcel::read(LightFlattenable<T>& val) const {
5428683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    size_t size;
5438683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    if (val.isFixedSize()) {
544e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        size = val.getFlattenedSize();
5458683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    } else {
5468683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        int32_t s;
5478683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        status_t err = readInt32(&s);
5488683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        if (err != NO_ERROR) {
5498683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian            return err;
5508683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        }
5518683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian        size = s;
5528683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian    }
5532098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian    if (size) {
5542098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian        void const* buffer = readInplace(size);
5552098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian        return buffer == NULL ? NO_MEMORY :
5562098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian                val.unflatten(buffer, size);
5572098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian    }
5582098517e3e12a401005d7a7510d6c4943707b98dMathias Agopian    return NO_ERROR;
5598683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian}
5608683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian
561f0c13772d95486d98f034361883b2415bb26a614Casey Dahlintemplate<typename T>
562f0c13772d95486d98f034361883b2415bb26a614Casey Dahlinstatus_t Parcel::readStrongBinder(sp<T>* val) const {
563f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    sp<IBinder> tmp;
564f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    status_t ret = readStrongBinder(&tmp);
565f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin
566f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    if (ret == OK) {
567f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin        *val = interface_cast<T>(tmp);
568f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin
569f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin        if (val->get() == nullptr) {
570f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin            return UNKNOWN_ERROR;
571f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin        }
572f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    }
573f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin
574f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin    return ret;
575f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin}
576f0c13772d95486d98f034361883b2415bb26a614Casey Dahlin
57703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileytemplate<typename T, typename U>
57803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileystatus_t Parcel::unsafeReadTypedVector(
579b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        std::vector<T>* val,
580b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        status_t(Parcel::*read_func)(U*) const) const {
58103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    int32_t size;
58203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t status = this->readInt32(&size);
58303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
58403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    if (status != OK) {
58503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        return status;
58603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    }
58703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
58803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    if (size < 0) {
58903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        return UNEXPECTED_NULL;
59003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    }
59103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
59203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    val->resize(size);
59303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
59403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    for (auto& v: *val) {
59503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        status = (this->*read_func)(&v);
59603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
59703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        if (status != OK) {
59803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley            return status;
59903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        }
60003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    }
60103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
60203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return OK;
60303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley}
60403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
60503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileytemplate<typename T>
60603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileystatus_t Parcel::readTypedVector(std::vector<T>* val,
60703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                 status_t(Parcel::*read_func)(T*) const) const {
60803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return unsafeReadTypedVector(val, read_func);
60903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley}
61003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
611b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
612b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
613b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                         status_t(Parcel::*read_func)(T*) const) const {
614b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    const int32_t start = dataPosition();
615b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    int32_t size;
616b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t status = readInt32(&size);
617b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    val->reset();
618b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
619b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (status != OK || size < 0) {
620b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        return status;
621b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
622b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
623b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    setDataPosition(start);
624b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    val->reset(new std::vector<T>());
625b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
626b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status = unsafeReadTypedVector(val->get(), read_func);
627b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
628b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (status != OK) {
629b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        val->reset();
630b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
631b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
632b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return status;
633b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
634b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
63503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileytemplate<typename T, typename U>
63603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileystatus_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
63703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley                                        status_t(Parcel::*write_func)(U)) {
63803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    if (val.size() > std::numeric_limits<int32_t>::max()) {
63903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        return BAD_VALUE;
64003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    }
64103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
64203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    status_t status = this->writeInt32(val.size());
64303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
64403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    if (status != OK) {
64503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        return status;
64603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    }
64703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
64803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    for (const auto& item : val) {
64903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        status = (this->*write_func)(item);
65003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
65103d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        if (status != OK) {
65203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley            return status;
65303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley        }
65403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    }
65503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
65603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return OK;
65703d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley}
65803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
65903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileytemplate<typename T>
66003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileystatus_t Parcel::writeTypedVector(const std::vector<T>& val,
661b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                  status_t(Parcel::*write_func)(const T&)) {
66203d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return unsafeWriteTypedVector(val, write_func);
66303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley}
66403d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
66503d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileytemplate<typename T>
66603d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wileystatus_t Parcel::writeTypedVector(const std::vector<T>& val,
667b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                  status_t(Parcel::*write_func)(T)) {
66803d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return unsafeWriteTypedVector(val, write_func);
66903d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley}
67003d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley
67197f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wileytemplate<typename T>
672b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
673b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                          status_t(Parcel::*write_func)(const T&)) {
674b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (val.get() == nullptr) {
675b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        return this->writeInt32(-1);
676b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
677b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
678b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return unsafeWriteTypedVector(*val, write_func);
679b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
680b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
681b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
682b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
683b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin                                          status_t(Parcel::*write_func)(T)) {
684b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (val.get() == nullptr) {
685b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        return this->writeInt32(-1);
686b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
687b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
688b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return unsafeWriteTypedVector(*val, write_func);
689b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
690b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
691b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
69297f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wileystatus_t Parcel::readParcelableVector(std::vector<T>* val) const {
693b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
694b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
695b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
696b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
697b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
698b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    const int32_t start = dataPosition();
699b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    int32_t size;
700b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t status = readInt32(&size);
701b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    val->reset();
702b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
703b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (status != OK || size < 0) {
704b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        return status;
705b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
706b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
707b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    setDataPosition(start);
708f26a3ab7758bdbc1c87940216e542b2cc58c4377Janis Danisevskis    val->reset(new std::vector<std::unique_ptr<T>>());
709b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
710f26a3ab7758bdbc1c87940216e542b2cc58c4377Janis Danisevskis    status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable<T>);
711b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
712b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (status != OK) {
713b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        val->reset();
714b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
715b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
716b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return status;
717b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
718b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
719b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
720b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
721b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    const int32_t start = dataPosition();
722b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    int32_t present;
723b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status_t status = readInt32(&present);
724b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    parcelable->reset();
725b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
726b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (status != OK || !present) {
727b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        return status;
728b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
729b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
730b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    setDataPosition(start);
731b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    parcelable->reset(new T());
732b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
733b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    status = readParcelable(parcelable->get());
734b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
735b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (status != OK) {
736b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        parcelable->reset();
737b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
738b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
739b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return status;
740b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
741b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
742b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
743b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
744b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeRawNullableParcelable(parcelable.get());
74597f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley}
74697f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley
74797f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wileytemplate<typename T>
74897f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wileystatus_t Parcel::writeParcelableVector(const std::vector<T>& val) {
749b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
750b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
751b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
752b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlintemplate<typename T>
753b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
754b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    if (val.get() == nullptr) {
755b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        return this->writeInt32(-1);
756b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    }
757b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
758b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return unsafeWriteTypedVector(*val, &Parcel::writeParcelable);
75997f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley}
76097f048d19e51da4ea6ff98d8a9daf38f2577f182Christopher Wiley
7618683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian// ---------------------------------------------------------------------------
7628683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopian
763edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectinline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
764edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
765edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    parcel.print(to);
766edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return to;
767edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
768edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
769edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
770edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
771edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// Generic acquire and release of objects.
772edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid acquire_object(const sp<ProcessState>& proc,
7736880307e8e35a6c484942443fb4ddd6173126152Ian Pedowitz                    const flat_binder_object& obj, const void* who);
774edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid release_object(const sp<ProcessState>& proc,
7756880307e8e35a6c484942443fb4ddd6173126152Ian Pedowitz                    const flat_binder_object& obj, const void* who);
776edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
777edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid flatten_binder(const sp<ProcessState>& proc,
778edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                    const sp<IBinder>& binder, flat_binder_object* out);
779edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid flatten_binder(const sp<ProcessState>& proc,
780edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                    const wp<IBinder>& binder, flat_binder_object* out);
781edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t unflatten_binder(const sp<ProcessState>& proc,
782edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                          const flat_binder_object& flat, sp<IBinder>* out);
783edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t unflatten_binder(const sp<ProcessState>& proc,
784edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                          const flat_binder_object& flat, wp<IBinder>* out);
785edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
786edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
787edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
788edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
789edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
790edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif // ANDROID_PARCEL_H
791