1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/ics/binder/IInterface.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* 1.5 + * Copyright (C) 2005 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +// 1.21 +#ifndef ANDROID_IINTERFACE_H 1.22 +#define ANDROID_IINTERFACE_H 1.23 + 1.24 +#include <binder/Binder.h> 1.25 + 1.26 +namespace android { 1.27 + 1.28 +// ---------------------------------------------------------------------- 1.29 + 1.30 +class IInterface : public virtual RefBase 1.31 +{ 1.32 +public: 1.33 + IInterface(); 1.34 + sp<IBinder> asBinder(); 1.35 + sp<const IBinder> asBinder() const; 1.36 + 1.37 +protected: 1.38 + virtual ~IInterface(); 1.39 + virtual IBinder* onAsBinder() = 0; 1.40 +}; 1.41 + 1.42 +// ---------------------------------------------------------------------- 1.43 + 1.44 +template<typename INTERFACE> 1.45 +inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) 1.46 +{ 1.47 + return INTERFACE::asInterface(obj); 1.48 +} 1.49 + 1.50 +// ---------------------------------------------------------------------- 1.51 + 1.52 +template<typename INTERFACE> 1.53 +class BnInterface : public INTERFACE, public BBinder 1.54 +{ 1.55 +public: 1.56 + virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); 1.57 + virtual const String16& getInterfaceDescriptor() const; 1.58 + 1.59 +protected: 1.60 + virtual IBinder* onAsBinder(); 1.61 +}; 1.62 + 1.63 +// ---------------------------------------------------------------------- 1.64 + 1.65 +template<typename INTERFACE> 1.66 +class BpInterface : public INTERFACE, public BpRefBase 1.67 +{ 1.68 +public: 1.69 + BpInterface(const sp<IBinder>& remote); 1.70 + 1.71 +protected: 1.72 + virtual IBinder* onAsBinder(); 1.73 +}; 1.74 + 1.75 +// ---------------------------------------------------------------------- 1.76 + 1.77 +#define DECLARE_META_INTERFACE(INTERFACE) \ 1.78 + static const android::String16 descriptor; \ 1.79 + static android::sp<I##INTERFACE> asInterface( \ 1.80 + const android::sp<android::IBinder>& obj); \ 1.81 + virtual const android::String16& getInterfaceDescriptor() const; \ 1.82 + I##INTERFACE(); \ 1.83 + virtual ~I##INTERFACE(); \ 1.84 + 1.85 + 1.86 +#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 1.87 + const android::String16 I##INTERFACE::descriptor(NAME); \ 1.88 + const android::String16& \ 1.89 + I##INTERFACE::getInterfaceDescriptor() const { \ 1.90 + return I##INTERFACE::descriptor; \ 1.91 + } \ 1.92 + android::sp<I##INTERFACE> I##INTERFACE::asInterface( \ 1.93 + const android::sp<android::IBinder>& obj) \ 1.94 + { \ 1.95 + android::sp<I##INTERFACE> intr; \ 1.96 + if (obj != NULL) { \ 1.97 + intr = static_cast<I##INTERFACE*>( \ 1.98 + obj->queryLocalInterface( \ 1.99 + I##INTERFACE::descriptor).get()); \ 1.100 + if (intr == NULL) { \ 1.101 + intr = new Bp##INTERFACE(obj); \ 1.102 + } \ 1.103 + } \ 1.104 + return intr; \ 1.105 + } \ 1.106 + I##INTERFACE::I##INTERFACE() { } \ 1.107 + I##INTERFACE::~I##INTERFACE() { } \ 1.108 + 1.109 + 1.110 +#define CHECK_INTERFACE(interface, data, reply) \ 1.111 + if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \ 1.112 + 1.113 + 1.114 +// ---------------------------------------------------------------------- 1.115 +// No user-serviceable parts after this... 1.116 + 1.117 +template<typename INTERFACE> 1.118 +inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( 1.119 + const String16& _descriptor) 1.120 +{ 1.121 + if (_descriptor == INTERFACE::descriptor) return this; 1.122 + return NULL; 1.123 +} 1.124 + 1.125 +template<typename INTERFACE> 1.126 +inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const 1.127 +{ 1.128 + return INTERFACE::getInterfaceDescriptor(); 1.129 +} 1.130 + 1.131 +template<typename INTERFACE> 1.132 +IBinder* BnInterface<INTERFACE>::onAsBinder() 1.133 +{ 1.134 + return this; 1.135 +} 1.136 + 1.137 +template<typename INTERFACE> 1.138 +inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) 1.139 + : BpRefBase(remote) 1.140 +{ 1.141 +} 1.142 + 1.143 +template<typename INTERFACE> 1.144 +inline IBinder* BpInterface<INTERFACE>::onAsBinder() 1.145 +{ 1.146 + return remote(); 1.147 +} 1.148 + 1.149 +// ---------------------------------------------------------------------- 1.150 + 1.151 +}; // namespace android 1.152 + 1.153 +#endif // ANDROID_IINTERFACE_H