1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/froyo/binder/IInterface.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 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 String16 descriptor; \ 1.79 + static sp<I##INTERFACE> asInterface(const sp<IBinder>& obj); \ 1.80 + virtual const String16& getInterfaceDescriptor() const; \ 1.81 + I##INTERFACE(); \ 1.82 + virtual ~I##INTERFACE(); \ 1.83 + 1.84 + 1.85 +#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 1.86 + const String16 I##INTERFACE::descriptor(NAME); \ 1.87 + const String16& I##INTERFACE::getInterfaceDescriptor() const { \ 1.88 + return I##INTERFACE::descriptor; \ 1.89 + } \ 1.90 + sp<I##INTERFACE> I##INTERFACE::asInterface(const sp<IBinder>& obj) \ 1.91 + { \ 1.92 + sp<I##INTERFACE> intr; \ 1.93 + if (obj != NULL) { \ 1.94 + intr = static_cast<I##INTERFACE*>( \ 1.95 + obj->queryLocalInterface( \ 1.96 + I##INTERFACE::descriptor).get()); \ 1.97 + if (intr == NULL) { \ 1.98 + intr = new Bp##INTERFACE(obj); \ 1.99 + } \ 1.100 + } \ 1.101 + return intr; \ 1.102 + } \ 1.103 + I##INTERFACE::I##INTERFACE() { } \ 1.104 + I##INTERFACE::~I##INTERFACE() { } \ 1.105 + 1.106 + 1.107 +#define CHECK_INTERFACE(interface, data, reply) \ 1.108 + if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \ 1.109 + 1.110 + 1.111 +// ---------------------------------------------------------------------- 1.112 +// No user-serviceable parts after this... 1.113 + 1.114 +template<typename INTERFACE> 1.115 +inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( 1.116 + const String16& _descriptor) 1.117 +{ 1.118 + if (_descriptor == INTERFACE::descriptor) return this; 1.119 + return NULL; 1.120 +} 1.121 + 1.122 +template<typename INTERFACE> 1.123 +inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const 1.124 +{ 1.125 + return INTERFACE::getInterfaceDescriptor(); 1.126 +} 1.127 + 1.128 +template<typename INTERFACE> 1.129 +IBinder* BnInterface<INTERFACE>::onAsBinder() 1.130 +{ 1.131 + return this; 1.132 +} 1.133 + 1.134 +template<typename INTERFACE> 1.135 +inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) 1.136 + : BpRefBase(remote) 1.137 +{ 1.138 +} 1.139 + 1.140 +template<typename INTERFACE> 1.141 +inline IBinder* BpInterface<INTERFACE>::onAsBinder() 1.142 +{ 1.143 + return remote(); 1.144 +} 1.145 + 1.146 +// ---------------------------------------------------------------------- 1.147 + 1.148 +}; // namespace android 1.149 + 1.150 +#endif // ANDROID_IINTERFACE_H