解释为何 remote()->transact可以直接调用->transact?

来源:互联网 发布:电脑摄像头全屏软件 编辑:程序博客网 时间:2024/05/22 10:44

在看程序代码时,疑惑remote()返回的明明是Ibinder类,而Ibinder类中的transact是纯虚函数,没有函数体。

其实原因主要在 sp<IServiceManager> sm = defaultServiceManager();这行程序中。 

 在defaultServiceManager();中有一下语句:

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

其中ProcessState::self()->getContextObject(NULL)返回的是BPbinder,而interface_cast<IServiceManager>中有以下内容:在asInterface()函数内有以下语句:

                                        if (intr == NULL)                                   
                                                intr = new Bp##INTERFACE(obj);    

这条语句中的obj就是bpbinder.而在new Bp##INTERFACE(obj)的程序中有以下赋值:

  inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
    : BpRefBase(remote)

注意BpRefBase(remote)这句赋值语句。它实际上是把Bpbinder赋值给了mRemote,这个mRemote其实就是remote()的返回值。它返回的不是Ibinder,而是Bpbinder。