ServiceManager遠程代理的獲取

ServiceManager管理着系統中所有的服務,無論是service 還是client只要跟系統中的服務打交道,必先獲取ServiceManager的代理,才能獲取ServiceManager提供的各項服務。在安卓系統中Service Manager遠程代理是一個特殊的Binder引用,它的引用句柄一定是0。獲取遠程代理的方法defaultServiceManager()的實現在frameworks/native/libs/binder/IServiceManager.cpp文件中:

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;
}

gDefaultServiceManager在frameworks/native/libs/binder/Static.cpp中聲明:

// ------------ ServiceManager.cpp

Mutex gDefaultServiceManagerLock;
sp<IServiceManager> gDefaultServiceManager;
sp<IPermissionController> gPermissionController;

gDefaultServiceManagerLock和gDefaultServiceManager爲單列模式,每個進程只有一個。
進程第一次調用defaultServiceManager()時,會通過interface_cast<IServiceManager>(
ProcessState::self()->getContextObject(NULL));
創建,下面詳細介紹Service Manager遠程接口代理的創建過程。
我們先來看下老羅畫的個圖:
這裏寫圖片描述
從圖中我們可以清晰的看出各個類的繼承關係。
*IServiceManager類繼承了IInterface類,而IInterface類和BpRefBase類又分別繼承了RefBase類。在BpRefBase類中,有一個成員變量mRemote,它的類型是IBinder,實現類爲BpBinder,它表示一個Binder引用,引用句柄值保存在BpBinder類的mHandle成員變量中。BpBinder類通過IPCThreadState類來和Binder驅動程序並互,而IPCThreadState又通過它的成員變量mProcess來打開/dev/binder設備文件,mProcess成員變量的類型爲ProcessState。ProcessState類打開設備/dev/binder之後,將打開文件描述符保存在mDriverFD成員變量中,以供後續使用。
首先是調用ProcessState::self函數,self函數是ProcessState的靜態成員函數,它的作用是返回一個全局唯一的ProcessState實例變量,就是單例模式了,這個變量名爲gProcess。如果gProcess尚未創建,就會執行創建操作,在ProcessState的構造函數中,會通過open文件操作函數打開設備文件/dev/binder,並且返回來的設備文件描述符保存在成員變量mDriverFD中。**
接着調用gProcess->getContextObject函數來獲得一個句柄值爲0的Binder引用,即BpBinder了,於是創建Service Manager遠程接口的語句可以簡化爲:

gDefaultServiceManager = interface_cast<IServiceManager>(new BpBinder(0));  

interface_cast(new BpBinder(0));是一個模板函數,定義在framework/base/include/binder/IInterface.h文件中:

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

這裏的INTERFACE是IServiceManager,於是調用了IServiceManager::asInterface函數。IServiceManager::asInterface是通過DECLARE_META_INTERFACE(ServiceManager)宏在IServiceManager類中聲明的,它位於framework/base/include/binder/IServiceManager.h文件中:

DECLARE_META_INTERFACE(ServiceManager);  
展開即爲:
#define DECLARE_META_INTERFACE(ServiceManager)                              \  
    static const android::String16 descriptor;                          \  
    static android::sp<IServiceManager> asInterface(                    \  
    const android::sp<android::IBinder>& obj);                          \  
    virtual const android::String16& getInterfaceDescriptor() const;    \  
    IServiceManager();                                                  \  
    virtual ~IServiceManager();  
    /*    
    *IServiceManager::asInterface的實現是通過IMPLEMENT_META_INTERFACE(ServiceManager,  *"android.os.IServiceManager")宏定義的,它位於*framework/base/libs/binder/IServiceManager.cpp文件中:
*/
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager"); 
展開即爲:
#define 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)                                              \  
    {                                                                                      \  
    android::sp<IServiceManager> intr;                                                     \  
    if (obj != NULL) {                                                                     \  
    intr = static_cast<IServiceManager*>(                                                  \  
    obj->queryLocalInterface(                                                              \  
    IServiceManager::descriptor).get());                                                   \  
    if (intr == NULL) {                                                                    \  
    intr = new BpServiceManager(obj);                                                      \  
    }                                                                                      \  
    }                                                                                      \  
    return intr;                                                                           \  
    }                                                                                      \  
    IServiceManager::IServiceManager() { }                                                 \  
    IServiceManager::~IServiceManager() { }      

從中可以看到asInterface的實現:

android::sp<IServiceManager> IServiceManager::asInterface(const android::sp<android::IBinder>& obj)                                                
{                                                                                       
    android::sp<IServiceManager> intr;                                                      

    if (obj != NULL) {                                                                       
        intr = static_cast<IServiceManager*>(                                                    
                    obj->queryLocalInterface(IServiceManager::descriptor).get());  

        if (intr == NULL) {                  
            intr = new BpServiceManager(obj);                                          
        }                                            
    }  
    return intr;                                    
}     

obj就爲new BpBinder(0),binder類成員函數queryLocalInterface返回的一定爲NULL,最終會調用
intr = new BpServiceManager(obj);<===>gDefaultServiceManager = new BpServiceManager(new BpBinder(0));
gDefaultServiceManager 本質上是一個BpServiceManager,包含了一個句柄值爲0的Binder引用。

發佈了95 篇原創文章 · 獲贊 23 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章