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 #pragma once
18 
19 #include <android/binder_parcel.h>
20 
21 #include <sys/cdefs.h>
22 
23 #include <binder/Parcel.h>
24 #include "ibinder_internal.h"
25 
26 struct AParcel {
getAParcel27     const ::android::Parcel* get() const { return mParcel; }
getAParcel28     ::android::Parcel* get() { return mParcel; }
29 
AParcelAParcel30     explicit AParcel(const AIBinder* binder)
31         : AParcel(binder, new ::android::Parcel, true /*owns*/) {}
AParcelAParcel32     AParcel(const AIBinder* binder, ::android::Parcel* parcel, bool owns)
33         : mBinder(binder), mParcel(parcel), mOwns(owns) {}
34 
~AParcelAParcel35     ~AParcel() {
36         if (mOwns) {
37             delete mParcel;
38         }
39     }
40 
readOnlyAParcel41     static const AParcel readOnly(const AIBinder* binder, const ::android::Parcel* parcel) {
42         return AParcel(binder, const_cast<::android::Parcel*>(parcel), false);
43     }
44 
getBinderAParcel45     const AIBinder* getBinder() { return mBinder; }
46 
47    private:
48     // This object is associated with a calls to a specific AIBinder object. This is used for sanity
49     // checking to make sure that a parcel is one that is expected.
50     const AIBinder* mBinder;
51 
52     ::android::Parcel* mParcel;
53     bool mOwns;
54 };
55