1 /*
2  * Copyright (C) 2018 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 IORAP_BINDER_AUTO_PARCELABLE_H_
18 #define IORAP_BINDER_AUTO_PARCELABLE_H_
19 
20 #include "binder/Parcelable.h"
21 #include "binder/Parcel.h"
22 #include "binder/Status.h"
23 
24 #include "common/introspection.h"
25 
26 namespace iorap {
27 namespace binder {
28 
29 //
30 // Implements the android::Parcelable interface (readFromParcel, writeToParcel)
31 // automatically by using compile-time introspection on T.
32 //
33 // Requires that 'T' implements introspection by using the IORAP_INTROSPECT_ADAPT_STRUCT macro.
34 //
35 template <typename T>
36 struct AutoParcelable : public ::android::Parcelable {
37  private:
38   using Status = android::binder::Status;
39   using Parcel = android::Parcel;
40   using Parcelable = android::Parcelable;
41   using status_t = android::status_t;
42 
43  public:
44   // Write every (introspected) field to the parcel by automatically inferring the correct
45   // write method to invoke on the parcel from the member type.
writeToParcelAutoParcelable46   status_t writeToParcel(Parcel* parcel) const override {
47     if (parcel == nullptr) {
48       return ::android::UNEXPECTED_NULL;
49     }
50 
51     status_t result = android::NO_ERROR;
52     ::iorap::introspect::for_each_member_field_value(*Self(), [&](auto&& value) {
53       if (result == android::NO_ERROR) {
54         result = writeAnyToParcel(/*inout*/parcel, value);
55       }
56     });
57 
58     return result;
59   }
60 
61   // Read every (introspected) field to the parcel by automatically inferring the correct
62   // read method to invoke on the parcel from the member type.
63   //
64   // Resilient to partial read failures: A return code other than NO_ERROR means that
65   // the current value is left unmodified.
readFromParcelAutoParcelable66   status_t readFromParcel(const Parcel* parcel) override {
67     if (parcel == nullptr) {
68       return ::android::UNEXPECTED_NULL;
69     }
70 
71     T tmp{*Self()};
72 
73     // Unpack all the parcelable data into a temporary copy.
74     // Parceling could fail halfway through, in which case
75     // the original object is unaffected.
76 
77     status_t result = android::NO_ERROR;
78     ::iorap::introspect::for_each_member_field_set_value(tmp, [&](auto field_type) {
79       // type<?> field_type
80 
81       using ValueT = typename decltype(field_type)::type;
82 
83       if (result == android::NO_ERROR) {
84         auto&& [res, read_value] = readAnyFromParcel<ValueT>(/*inout*/parcel);
85         result = res;
86         return ::iorap::introspect::aliasing_forward<ValueT>(read_value);
87       } else {
88         // TODO: nice-to-have fold over members to early-out on failure.
89         return ValueT{};
90       }
91     });
92 
93     if (result != android::NO_ERROR) {
94       return result;
95     }
96 
97     // Success! Now we can copy all the data in a single step.
98     *Self() = std::move(tmp);
99 
100     // TODO: nice-to-have some kind of invariants-checking after reading the parcel data.
101 
102     return ::android::NO_ERROR;
103   }
104 
105  private:
106 #define AUTO_PARCELABLE_BINDER_MAPPING(FN) \
107 FN(Byte,int8_t)\
108 FN(Int32,int32_t)\
109 FN(Uint32,uint32_t)\
110 FN(Int64,int64_t)\
111 FN(Uint64,uint64_t)\
112 FN(Float,float)\
113 FN(Double,double)\
114 FN(Bool,bool)\
115 FN(CString,const char*)\
116 FN(String16,const String16&)\
117 FN(String16,const std::unique_ptr<String16>&)\
118 FN(StrongBinder,const sp<IBinder>&)\
119 
120   template <typename F>
writeAnyToParcelAutoParcelable121   static status_t writeAnyToParcel(Parcel* parcel, const F& value) {
122     using namespace android;  // NOLINT
123 
124     // 'F' is the original type of the field here, so it's safe to use it undecayed.
125     // However, to make matching easier we almost always want to match against the decayed type.
126     using D = std::decay_t<F>;  // [const] [volatile] X[&][&] -> X
127 
128     if constexpr (std::is_base_of_v<Parcelable, D>) {
129       return value.writeToParcel(parcel);
130     } else if constexpr (std::is_enum_v<D>) {
131       return writeAnyToParcel(parcel, static_cast<std::underlying_type_t<F>>(value));
132 #define AUTO_PARCELABLE_WRITE_TO_PARCEL(fn_name, type_name) \
133     } else if constexpr (std::is_same_v<D, std::decay_t<type_name>>) { \
134       return parcel->write ## fn_name (value);
135 AUTO_PARCELABLE_BINDER_MAPPING(AUTO_PARCELABLE_WRITE_TO_PARCEL)
136     } else if constexpr (std::is_same_v<D, std::string>) {
137       return parcel->writeUtf8AsUtf16(value);
138     } else {
139       STATIC_FAIL(D, "Unsupported type: Add more manual type conversions above^^^");
140     }
141 
142 #undef AUTO_PARCELABLE_WRITE_TO_PARCEL
143   }
144 
145   template <typename F>
readAnyFromParcelAutoParcelable146   static auto readAnyFromParcel(const Parcel* parcel) {
147     // returns pair(status_t, ~F~)
148     using namespace android;
149 
150     // Since 'F' is almost always an lvalue reference (due to F=decltype(auto&&),
151     // we should lose the references, and also any consts.
152     using D = std::decay_t<F>;
153 
154     D value;
155     status_t result;
156 
157     if constexpr (std::is_base_of_v<Parcelable, D>) {
158       status_t result = value.readFromParcel(/*in*/parcel);
159     } else if constexpr (std::is_enum_v<D>) {
160       auto&& [res, val] = readAnyFromParcel<std::underlying_type_t<D>>(parcel);
161       result = res;
162       value = static_cast<D>(val);
163 #define AUTO_PARCELABLE_READ_FROM_PARCEL(fn_name, type_name) \
164     } else if constexpr (std::is_same_v<D, std::decay_t<type_name>>) { \
165       result = parcel->read ## fn_name (/*out*/&value);
166 AUTO_PARCELABLE_BINDER_MAPPING(AUTO_PARCELABLE_READ_FROM_PARCEL)
167     } else if constexpr (std::is_same_v<D, std::string>) {
168       result = parcel->readUtf8FromUtf16(/*out*/&value);
169     } else {
170       STATIC_FAIL(D, "Unsupported type: Add more manual type conversions above^^^");
171     }
172 #undef AUTO_PARCELABLE_READ_FROM_PARCEL
173 
174     return std::make_pair(result, std::move(value));
175   }
176 
SelfAutoParcelable177   T* Self() {
178     return static_cast<T*>(this);
179   }
SelfAutoParcelable180   const T* Self() const {
181     return static_cast<const T*>(this);
182   }
183 };
184 
185 }  // namespace binder
186 }  // namespace iorap
187 
188 #endif  // IORAP_BINDER_AUTO_PARCELABLE_H_
189