美文网首页
ServiceManager 代理对象的获取过程

ServiceManager 代理对象的获取过程

作者: ColdWave | 来源:发表于2018-07-09 16:52 被阅读0次

    IServiceManager.h

    
    namespace android {
    
    // ----------------------------------------------------------------------
    
    class IServiceManager : public IInterface
    {
    public:
        DECLARE_META_INTERFACE(ServiceManager)
    
        /**
         * Retrieve an existing service, blocking for a few seconds
         * if it doesn't yet exist.
         */
        virtual sp<IBinder>         getService( const String16& name) const = 0;
    
        /**
         * Retrieve an existing service, non-blocking.
         */
        virtual sp<IBinder>         checkService( const String16& name) const = 0;
    
        /**
         * Register a service.
         */
        virtual status_t            addService( const String16& name,
                                                const sp<IBinder>& service,
                                                bool allowIsolated = false) = 0;
    
        /**
         * Return list of all existing services.
         */
        virtual Vector<String16>    listServices() = 0;
    
        // ServiceManager Proxy 提供的接口
        enum {
            GET_SERVICE_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
            CHECK_SERVICE_TRANSACTION,
            ADD_SERVICE_TRANSACTION,
            LIST_SERVICES_TRANSACTION,
        };
    };
    
    sp<IServiceManager> defaultServiceManager();
    
    template<typename INTERFACE>
    status_t getService(const String16& name, sp<INTERFACE>* outService)
    {
        const sp<IServiceManager> sm = defaultServiceManager();
        if (sm != NULL) {
            *outService = interface_cast<INTERFACE>(sm->getService(name));
            if ((*outService) != NULL) return NO_ERROR;
        }
        return NAME_NOT_FOUND;
    }
    
    }; // namespace android
    

    IServiceManager.cpp

    namespace android {
    
    sp<IServiceManager> defaultServiceManager()
    {
        if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
    
        {
            AutoMutex _l(gDefaultServiceManagerLock);
            while (gDefaultServiceManager == NULL) {
                gDefaultServiceManager = interface_cast<IServiceManager>(
                    ProcessState::self()->getContextObject(NULL));
                if (gDefaultServiceManager == NULL)
                    sleep(1);
            }
        }
    
        return gDefaultServiceManager;
    }
    
    /*
     * 1. ProcessState::self()->getContextObject(NULL): 返回的是一个 BpBinder. ServiceManager 的 desc 默认为0.
     * 2. interface_cast 就是将 BpBinder 封装为 IServiceManager,这样可以直接调用 IServiceManager 的接口.
    */
    
    /*
     * 这里,有一个设计思想.
     * 1. defaultServiceManager 首先实例化 BpBinder.
     * 2. interface_cast 就是 实例化 BpXXX,并将 BpBinder 交给其管理.
     * 
     * Proxy 端的用户无法直接看到 BpBinder , BpBinder 由 BpXXX 持有.用户本身不关心 BpBinder 的能力,只关心 IXXX 定义的 接口.
     * 所以这里很好的进行了封装.
    */
    
    /***************************************************************************************************************/
    
    template<typename INTERFACE>
    inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) // 调用了 IServiceManager::asInterface
    {
        return INTERFACE::asInterface(obj);
    }
    
    #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);                          \
                }                                                           \
            }                                                               \
            return intr;                                                    \
        }                                                                   \
        I##INTERFACE::I##INTERFACE() { }                                    \
        I##INTERFACE::~I##INTERFACE() { }                                   \
    
    
    // 展开: IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
    
        const ::android::String16 IServiceManager::descriptor("android.os.IServiceManager");
        const ::android::String16&
                IServiceManager::getInterfaceDescriptor() const {
            return IServiceManager::descriptor;
        }
        ::android::sp<IServiceManager> IServiceManager::asInterface(
                const ::android::sp<::android::IBinder>& obj)        // obj 就是 BpBinder
        {
            ::android::sp<IServiceManager> intr;
            if (obj != NULL) {
                intr = static_cast<IServiceManager*>(
                    obj->queryLocalInterface(                        // BpBinder 的 queryLocalInterface 返回 NULL
                            IServiceManager::descriptor).get());
                if (intr == NULL) {                                  // 此时,创建 BpServiceManager.
                    intr = new BpServiceManager(obj);
                }
            }
            return intr;
        }
        IServiceManager::IServiceManager() { }
        IServiceManager::~IServiceManager() { }
    
    
    /***************************************************************************************************************/
    
    // ----------------------------------------------------------------------
    
    class BpServiceManager : public BpInterface<IServiceManager>
    {
    public:
        explicit BpServiceManager(const sp<IBinder>& impl)
            : BpInterface<IServiceManager>(impl)
        {
        }
    
        virtual sp<IBinder> getService(const String16& name) const
        {
            unsigned n;
            for (n = 0; n < 5; n++){
                if (n > 0) {
                    if (!strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder")) {
                        ALOGI("Waiting for vendor service %s...", String8(name).string());
                        CallStack stack(LOG_TAG);
                    } else {
                        ALOGI("Waiting for service %s...", String8(name).string());
                    }
                    sleep(1);
                }
                sp<IBinder> svc = checkService(name);
                if (svc != NULL) return svc;
            }
            return NULL;
        }
    
        virtual sp<IBinder> checkService( const String16& name) const
        {
            Parcel data, reply;
            data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
            data.writeString16(name);
            remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
            return reply.readStrongBinder();
        }
    
        virtual status_t addService(const String16& name, const sp<IBinder>& service,
                bool allowIsolated)
        {
            Parcel data, reply;
            data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
            data.writeString16(name);
            data.writeStrongBinder(service);
            data.writeInt32(allowIsolated ? 1 : 0);
            status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
            return err == NO_ERROR ? reply.readExceptionCode() : err;
        }
    
        virtual Vector<String16> listServices()
        {
            Vector<String16> res;
            int n = 0;
    
            for (;;) {
                Parcel data, reply;
                data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
                data.writeInt32(n++);
                status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
                if (err != NO_ERROR)
                    break;
                res.add(reply.readString16());
            }
            return res;
        }
    };
    
    IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
    
    }; // namespace android
    

    相关文章

      网友评论

          本文标题:ServiceManager 代理对象的获取过程

          本文链接:https://www.haomeiwen.com/subject/iastpftx.html