writeStrongBinder

status_t Parcel::writeStrongBinder(const sp<IBinder>& val){
    return flatten_binder(ProcessState::self(), val, this);
}
status_t flatten_binder(const sp<ProcessState>& proc,
    const sp<IBinder>& binder, Parcel* out) {
    flat_binder_object obj; 
    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
    if (binder != NULL) {
        IBinder *local = binder->localBinder();
        if (!local) {
            // 远程Binder
            BpBinder *proxy = binder->remoteBinder();
            if (proxy == NULL) {
                ALOGE("null proxy");
            }
            const int32_t handle = proxy ? proxy->handle() : 0;
            obj.type = BINDER_TYPE_HANDLE;
            obj.handle = handle;
            obj.cookie = NULL;
        } else {
            // 本地Binder
            obj.type = BINDER_TYPE_BINDER;
            obj.binder = local->getWeakRefs();
            obj.cookie = local;
        }
    } else {
        obj.type = BINDER_TYPE_BINDER;
        obj.binder = NULL;
        obj.cookie = NULL;
    }
    return finish_flatten_binder(binder, obj, out);
}

这里需要注意的是如果不是在同一进程,那么传输介质是BpBinder;如果在同一进程,那么传输介质是JavaBBinder的localBinder返回值,即为JavaBBinder对象自身,如下图所示:

BBinder* BBinder::localBinder(){
    return this;
}

如果是BpBinder,我们看一下localBinder方法的返回值:

BBinder* IBinder::localBinder()
{
    return NULL;
}
inline static status_t finish_flatten_binder(const sp<IBinder>& binder, const flat_binder_object& flat, Parcel* out) {
    return out->writeObject(flat, false);
}

调用自身的writeObject方法,如果成功的话会返回NO_ERROR。

results matching ""

    No results matching ""