Implementation of remote()

2019-08-10 21:01发布

问题:

I'm trying to find the implementation of remote(), as in:

remote()->transact(CODE, data, &reply);

Do you guys know where it is? Searching Google turned out futile. Or if you know what that function does, it would be a big help for me. Many thanks

Update: it seems remote() will return a pointer to an object of type BBinder, IBinder, BpBinder, or IPCThreadState but I'm not sure which one.

回答1:

The implementation of remote is simple:

class BpRefBase : public virtual RefBase
{
protected:
                            BpRefBase(const sp<IBinder>& o);
    virtual                 ~BpRefBase();
    virtual void            onFirstRef();
    virtual void            onLastStrongRef(const void* id);
    virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);

    inline  IBinder*        remote()                { return mRemote; }
    inline  IBinder*        remote() const          { return mRemote; }

private:
                            BpRefBase(const BpRefBase& o);
    BpRefBase&              operator=(const BpRefBase& o);

    IBinder* const          mRemote;
    RefBase::weakref_type*  mRefs;
    volatile int32_t        mState;
};

The ServiceManager will manage all the registered service, for how it works, check an existing answer. When you getService from ServiceManager, it will return an IBinder object represents that service, then this IBinder object will be put into a BpInterface. That is your remote. Then you can use that BpInterface to start binder transaction with the actual service(BnInterface).

template<typename INTERFACE>
class BpInterface : public INTERFACE, public BpRefBase
{
public:
                                BpInterface(const sp<IBinder>& remote);

protected:
    virtual IBinder*            onAsBinder();
};

All familiar BpXXX like BpCamera, BpCameraService extends from BpInterface.