binder详解(二)_defaultServiceManager

来源:互联网 发布:怎样去开淘宝店 编辑:程序博客网 时间:2024/05/16 00:25

IServiceManager.cpp::defaultServiceManager()

->

sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& caller)

->

sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)// handle为0

sp<IBinder> result;

b = new BpBinder(handle);

result = b;

return result;

 

->

gDefaultServiceManager = interface_cast<IServiceManager>(
                ProcessState::self()->getContextObject(NULL));

 

template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj); // IServiceManager::asInterface(obj);
}

 

IServiceManager类的继承关系:

class IServiceManager : public IInterface

class IInterface : public virtual RefBase

class RefBase

 

asInterface方法的声明:

IServiceManager.h

class IServiceManager : public IInterface
{
public:
    DECLARE_META_INTERFACE(ServiceManager);

 

#define DECLARE_META_INTERFACE(INTERFACE)                               /
    static const String16 descriptor;                                   /
    static sp<I##INTERFACE> asInterface(const sp<IBinder>& obj);        /
    virtual const String16& getInterfaceDescriptor() const;             /
    I##INTERFACE();                                                     /
    virtual ~I##INTERFACE();    


定义:

IServiceManager.cpp

IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");

 

#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)                       /
    const String16 I##INTERFACE::descriptor(NAME);                      /
    const String16& I##INTERFACE::getInterfaceDescriptor() const {      /
        return I##INTERFACE::descriptor;                                /
    }                                                                   /
    sp<I##INTERFACE> I##INTERFACE::asInterface(const sp<IBinder>& obj)  /
    {                                                                   /
        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() { }                                   /

 

而BpServiceManager的继承关系:

class BpServiceManager : public BpInterface<IServiceManager>

class BpInterface : public INTERFACE, public BpRefBase

class BpRefBase : public virtual RefBase

 

因此,对于sp<IServiceManager> gDefaultServiceManager; ,BpServiceManager是IServiceManager的子类,所以可以作为gDefaultServiceManager的构造函数的参数。