Android Binder机制 – interface_cast和asBinder源码分析

  • Post author:
  • Post category:其他


研究Android底层代码时,尤其是Binder跨进程通信时,经常会发现interface_cast和asBinder,很容易被这两个函数绕晕,下面通过分析源码来讲解一下:

interface_cast

下面根据下述ICameraClient例子进行分析一下:

//伪代码
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(BpBinder(handle));

看下interface_cast的实现,其代码在IInterface.h中

template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}
//这是一个模板函数,展开即为:
inline sp<ICameraClient > interface_cast(const sp<IBinder>& obj)
{
    return ICameraClient ::asInterface(obj);
}

那ICameraClient的asInterface在哪实现的呢?发现找了ICameraClient.h和ICameraClient.cpp只有下面两个定义:

//frameworks/av/include/camera/android/hardware/ICameraClient.h
DECLARE_META_INTERFACE(CameraClient);

//frameworks/av/camera/ICameraClient.cpp
IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");

DECLARE_META_INTERFACE和IMPLEMENT_META_INTERFACE函数是其父类IInterface(frameworks\native\include\binder\IInterace.h)的宏定义:

//声明asInterface函数
#define DECLARE_META_INTERFACE(INTERFACE)
    static const android::String16 descriptor;
    //声明asInterface函数
    static android::sp<I##INTERFACE> asInterface( 
            const android::sp<android::IBinder>& obj);
    virtual const android::String16& getInterfaceDescriptor() const;
    I##INTERFACE();
    virtual ~I##INTERFACE();

展开为:
#define DECLARE_META_INTERFACE(CameraClient)
    //增加一个描述符
    static const android::String16 descriptor;
    //声明asInterface函数
    static android::sp<ICameraClient> asInterface( 
            const android::sp<android::IBinder>& obj);
    //获取描述符函数
    virtual const android::String16& getInterfaceDescriptor() const;
    //构造函数以及折构函数
    ICameraClient();
    virtual ~ICameraClient();

//实现asInterface函数
#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)
    const android::String16 I##INTERFACE::descriptor(NAME); 
    const android::String16& 
            I##INTERFACE::getInterfaceDescriptor() const { 
        return I##INTERFACE::descriptor; 
    }                                               
    android::sp<I##INTERFACE> I##INTERFACE::asInterface(
            const android::sp<android::IBinder>& obj)
    {
        android::sp<I##INTERFACE> intr; 
        if (obj != NULL) {
            intr = static_cast<I##INTERFACE*>(
                obj->queryLocalInterface( 
                        I##INTERFACE::descriptor).get()); 
            if (intr == NULL) {
                intr = new Bp##INTERFACE(obj); //展开即为intr = new BpServiceManager(obj);
            }
        }
        return intr;
    }
    I##INTERFACE::I##INTERFACE() { }
    I##INTERFACE::~I##INTERFACE() { }

展开为:
#define IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient")
    //定义ICameraClient的描述符为"android.hardware.ICameraClient"
    const android::String16 ICameraClient ::descriptor("android.hardware.ICameraClient"); 
    //获取描述符"android.hardware.ICameraClient"
    const android::String16& 
            ICameraClient ::getInterfaceDescriptor() const { 
        return ICameraClient ::descriptor; 
    }                        
    //实现asInterface函数
    android::sp<ICameraClient> ICameraClient::asInterface(
            const android::sp<android::IBinder>& obj)
    {
        android::sp<ICameraClient> intr; 
        if (obj != NULL) {
            intr = static_cast<ICameraClient*>(
                //queryLocalInterface是在IBinder中定义的,默认返回NULL,但在BBinder的子类BnInterface中,重载了该方法,返回this,而BpBinder没有重载,使用IBinder的默认实现,返回NULL
                obj->queryLocalInterface( 
                        ICameraClient::descriptor).get()); 
            if (intr == NULL) {
                //构建INTERFACE的Bp端代理对象
                intr = new BpCameraClient(obj);
            }
        }
        return intr;
    }
    ICameraClient::ICameraClient() { }
    ICameraClient::~ICameraClient() { }

总结一下, 如果interface_cast的参数obj是BnInterface,则返回其自身,如果参数obj是BpInterface,则new一个Bp代理对象并返回。这里我们用的是ICameraClient例子来讲解的,则返回BpCameraClient,别的接口也是同样分析的,比如IServiceManager,也会有类似声明如下,则返回BpServiceManager。

//frameworks\native\include\binder\IServiceManager.h
DECLARE_META_INTERFACE(ServiceManager);

//frameworks\native\libs\binder\IServiceManager.cpp
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");

asBinder

接着使用上面ICameraClient例子进行分析一下:

//伪代码,根据interface_cast的分析,知道cameraClient即为BpCameraClient(BpBinder(handle))
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
IInterface::asBinder(cameraClient);

看下asBinder的方法,在IInterface.cpp中

sp<IBinder> IInterface::asBinder(const IInterface* iface)
{
    if (iface == NULL) return NULL;
    return const_cast<IInterface*>(iface)->onAsBinder();
}

sp<IBinder> IInterface::asBinder(const sp<IInterface>& iface)
{
    if (iface == NULL) return NULL;
    return iface->onAsBinder();
}

都会走到onAsBinder方法

BnInterface

BnInterface的onAsBinder方法,直接返回自身,因为BnInterface继承自BBinder,而BBinder又继承自IBinder

template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
    return this;
}

根据例子展开为:
template<typename ICameraClient>
IBinder* BnInterface<ICameraClient>::onAsBinder()
{
    return this;
}
BpInterface

BpInterface的onAsBinder方法,调用remote()方法并返回

template<typename INTERFACE>
inline IBinder* BpInterface<INTERFACE>::onAsBinder()
{
    return remote();
}

根据例子展开为:
template<typename ICameraClient >
inline IBinder* BpInterface<ICameraClient>::onAsBinder()
{
    return remote();
}

remote()方法在其父类BpRefBase中实现,就是返回mRemote变量

inline  IBinder*        remote()                { return mRemote; }

而mRemote变量是在创建BpInterface对象时,将remote变量传给了其父类BpRefBase,我们这个例子里面remote就是BpBinder(handle)

template

inline BpInterface::BpInterface(const sp& remote)

: BpRefBase(remote)

{

}

BpRefBase::BpRefBase(const sp& o)

: mRemote(o.get()), mRefs(NULL), mState(0)

{

extendObjectLifetime(OBJECT_LIFETIME_WEAK);

if (mRemote) {
    mRemote->incStrong(this);           // Removed on first IncStrong().
    mRefs = mRemote->createWeak(this);  // Held for our entire lifetime.
}

}

“`

总结一下, 如果asBinder的参数iface是BnInterface类型,则返回其自身,如果参数iface是BpInterface类型,则返回其mRemote远程代理对象BpBinder(handle) 。

作者:lb377463323

出处:

http://blog.csdn.net/lb377463323


原文链接:

http://blog.csdn.net/lb377463323/article/details/78385845


转载请注明出处!



版权声明:本文为lb377463323原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。