Home | History | Annotate | Download | only in binder
      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 //
     18 #ifndef ANDROID_IINTERFACE_H
     19 #define ANDROID_IINTERFACE_H
     20 
     21 #include <binder/Binder.h>
     22 
     23 namespace android {
     24 
     25 // ----------------------------------------------------------------------
     26 
     27 class IInterface : public virtual RefBase
     28 {
     29 public:
     30             IInterface();
     31             static sp<IBinder>  asBinder(const IInterface*);
     32             static sp<IBinder>  asBinder(const sp<IInterface>&);
     33 
     34 protected:
     35     virtual                     ~IInterface();
     36     virtual IBinder*            onAsBinder() = 0;
     37 };
     38 
     39 // ----------------------------------------------------------------------
     40 
     41 template<typename INTERFACE>
     42 inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
     43 {
     44     return INTERFACE::asInterface(obj);
     45 }
     46 
     47 // ----------------------------------------------------------------------
     48 
     49 template<typename INTERFACE>
     50 class BnInterface : public INTERFACE, public BBinder
     51 {
     52 public:
     53     virtual sp<IInterface>      queryLocalInterface(const String16& _descriptor);
     54     virtual const String16&     getInterfaceDescriptor() const;
     55 
     56 protected:
     57     typedef INTERFACE           BaseInterface;
     58     virtual IBinder*            onAsBinder();
     59 };
     60 
     61 // ----------------------------------------------------------------------
     62 
     63 template<typename INTERFACE>
     64 class BpInterface : public INTERFACE, public BpRefBase
     65 {
     66 public:
     67     explicit                    BpInterface(const sp<IBinder>& remote);
     68 
     69 protected:
     70     typedef INTERFACE           BaseInterface;
     71     virtual IBinder*            onAsBinder();
     72 };
     73 
     74 // ----------------------------------------------------------------------
     75 
     76 #define DECLARE_META_INTERFACE(INTERFACE)                               \
     77 public:                                                                 \
     78     static const ::android::String16 descriptor;                        \
     79     static ::android::sp<I##INTERFACE> asInterface(                     \
     80             const ::android::sp<::android::IBinder>& obj);              \
     81     virtual const ::android::String16& getInterfaceDescriptor() const;  \
     82     I##INTERFACE();                                                     \
     83     virtual ~I##INTERFACE();                                            \
     84     static bool setDefaultImpl(std::unique_ptr<I##INTERFACE> impl);     \
     85     static const std::unique_ptr<I##INTERFACE>& getDefaultImpl();       \
     86 private:                                                                \
     87     static std::unique_ptr<I##INTERFACE> default_impl;                  \
     88 public:                                                                 \
     89 
     90 
     91 #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)                       \
     92     const ::android::String16 I##INTERFACE::descriptor(NAME);           \
     93     const ::android::String16&                                          \
     94             I##INTERFACE::getInterfaceDescriptor() const {              \
     95         return I##INTERFACE::descriptor;                                \
     96     }                                                                   \
     97     ::android::sp<I##INTERFACE> I##INTERFACE::asInterface(              \
     98             const ::android::sp<::android::IBinder>& obj)               \
     99     {                                                                   \
    100         ::android::sp<I##INTERFACE> intr;                               \
    101         if (obj != nullptr) {                                           \
    102             intr = static_cast<I##INTERFACE*>(                          \
    103                 obj->queryLocalInterface(                               \
    104                         I##INTERFACE::descriptor).get());               \
    105             if (intr == nullptr) {                                      \
    106                 intr = new Bp##INTERFACE(obj);                          \
    107             }                                                           \
    108         }                                                               \
    109         return intr;                                                    \
    110     }                                                                   \
    111     std::unique_ptr<I##INTERFACE> I##INTERFACE::default_impl;           \
    112     bool I##INTERFACE::setDefaultImpl(std::unique_ptr<I##INTERFACE> impl)\
    113     {                                                                   \
    114         if (!I##INTERFACE::default_impl && impl) {                      \
    115             I##INTERFACE::default_impl = std::move(impl);               \
    116             return true;                                                \
    117         }                                                               \
    118         return false;                                                   \
    119     }                                                                   \
    120     const std::unique_ptr<I##INTERFACE>& I##INTERFACE::getDefaultImpl() \
    121     {                                                                   \
    122         return I##INTERFACE::default_impl;                              \
    123     }                                                                   \
    124     I##INTERFACE::I##INTERFACE() { }                                    \
    125     I##INTERFACE::~I##INTERFACE() { }                                   \
    126 
    127 
    128 #define CHECK_INTERFACE(interface, data, reply)                         \
    129     do {                                                                \
    130       if (!(data).checkInterface(this)) { return PERMISSION_DENIED; }   \
    131     } while (false)                                                     \
    132 
    133 
    134 // ----------------------------------------------------------------------
    135 // No user-serviceable parts after this...
    136 
    137 template<typename INTERFACE>
    138 inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
    139         const String16& _descriptor)
    140 {
    141     if (_descriptor == INTERFACE::descriptor) return this;
    142     return nullptr;
    143 }
    144 
    145 template<typename INTERFACE>
    146 inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
    147 {
    148     return INTERFACE::getInterfaceDescriptor();
    149 }
    150 
    151 template<typename INTERFACE>
    152 IBinder* BnInterface<INTERFACE>::onAsBinder()
    153 {
    154     return this;
    155 }
    156 
    157 template<typename INTERFACE>
    158 inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
    159     : BpRefBase(remote)
    160 {
    161 }
    162 
    163 template<typename INTERFACE>
    164 inline IBinder* BpInterface<INTERFACE>::onAsBinder()
    165 {
    166     return remote();
    167 }
    168 
    169 // ----------------------------------------------------------------------
    170 
    171 }; // namespace android
    172 
    173 #endif // ANDROID_IINTERFACE_H
    174