研究Android底層代碼時,尤爲是Binder跨進程通訊時,常常會發現interface_cast和asBinder,很容易被這兩個函數繞暈,下面經過分析源碼來說解一下:android
interface_cast
下面根據下述ICameraClient例子進行分析一下:函數
//僞代碼
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(BpBinder(handle));
看下interface_cast的實現,其代碼在IInterface.h中this
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
return INTERFACE::asInterface(obj);
}
//這是一個模板函數,展開即爲:
inline sp<ICameraClient > interface_cast(const sp<IBinder>& obj)
{
return ICameraClient ::asInterface(obj);
}
那ICameraClient的asInterface在哪實現的呢?發現找了ICameraClient.h和ICameraClient.cpp只有下面兩個定義:代理
//frameworks/av/include/camera/android/hardware/ICameraClient.h
DECLARE_META_INTERFACE(CameraClient);對象
//frameworks/av/camera/ICameraClient.cpp
IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");
DECLARE_META_INTERFACE和IMPLEMENT_META_INTERFACE函數是其父類IInterface(frameworks\native\include\binder\IInterace.h)的宏定義:繼承
//聲明asInterface函數
#define DECLARE_META_INTERFACE(INTERFACE)
static const android::String16 descriptor;
//聲明asInterface函數
static android::sp<I##INTERFACE> asInterface(
const android::sp<android::IBinder>& obj);
virtual const android::String16& getInterfaceDescriptor() const;
I##INTERFACE();
virtual ~I##INTERFACE();接口
展開爲:
#define DECLARE_META_INTERFACE(CameraClient)
//增長一個描述符
static const android::String16 descriptor;
//聲明asInterface函數
static android::sp<ICameraClient> asInterface(
const android::sp<android::IBinder>& obj);
//獲取描述符函數
virtual const android::String16& getInterfaceDescriptor() const;
//構造函數以及折構函數
ICameraClient();
virtual ~ICameraClient();進程
//實現asInterface函數
#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); //展開即爲intr = new BpServiceManager(obj);
}
}
return intr;
}
I##INTERFACE::I##INTERFACE() { }
I##INTERFACE::~I##INTERFACE() { }ip
展開爲:
#define IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient")
//定義ICameraClient的描述符爲"android.hardware.ICameraClient"
const android::String16 ICameraClient ::descriptor("android.hardware.ICameraClient");
//獲取描述符"android.hardware.ICameraClient"
const android::String16&
ICameraClient ::getInterfaceDescriptor() const {
return ICameraClient ::descriptor;
}
//實現asInterface函數
android::sp<ICameraClient> ICameraClient::asInterface(
const android::sp<android::IBinder>& obj)
{
android::sp<ICameraClient> intr;
if (obj != NULL) {
intr = static_cast<ICameraClient*>(
//queryLocalInterface是在IBinder中定義的,默認返回NULL,但在BBinder的子類BnInterface中,重載了該方法,返回this,而BpBinder沒有重載,使用IBinder的默認實現,返回NULL
obj->queryLocalInterface(
ICameraClient::descriptor).get());
if (intr == NULL) {
//構建INTERFACE的Bp端代理對象
intr = new BpCameraClient(obj);
}
}
return intr;
}
ICameraClient::ICameraClient() { }
ICameraClient::~ICameraClient() { }
總結一下, 若是interface_cast的參數obj是BnInterface,則返回其自身,若是參數obj是BpInterface,則new一個Bp代理對象並返回。這裏咱們用的是ICameraClient例子來說解的,則返回BpCameraClient,別的接口也是一樣分析的,好比IServiceManager,也會有相似聲明以下,則返回BpServiceManager。rem
//frameworks\native\include\binder\IServiceManager.h
DECLARE_META_INTERFACE(ServiceManager);
//frameworks\native\libs\binder\IServiceManager.cpp
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
asBinder
接着使用上面ICameraClient例子進行分析一下:
//僞代碼,根據interface_cast的分析,知道cameraClient即爲BpCameraClient(BpBinder(handle))
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
IInterface::asBinder(cameraClient);
看下asBinder的方法,在IInterface.cpp中
sp<IBinder> IInterface::asBinder(const IInterface* iface)
{
if (iface == NULL) return NULL;
return const_cast<IInterface*>(iface)->onAsBinder();
}
sp<IBinder> IInterface::asBinder(const sp<IInterface>& iface)
{
if (iface == NULL) return NULL;
return iface->onAsBinder();
}
都會走到onAsBinder方法
BnInterface
BnInterface的onAsBinder方法,直接返回自身,由於BnInterface繼承自BBinder,而BBinder又繼承自IBinder
template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
return this;
}
根據例子展開爲:
template<typename ICameraClient>
IBinder* BnInterface<ICameraClient>::onAsBinder()
{
return this;
}
BpInterface
BpInterface的onAsBinder方法,調用remote()方法並返回
template<typename INTERFACE>
inline IBinder* BpInterface<INTERFACE>::onAsBinder()
{
return remote();
}
根據例子展開爲:
template<typename ICameraClient >
inline IBinder* BpInterface<ICameraClient>::onAsBinder()
{
return remote();
}
remote()方法在其父類BpRefBase中實現,就是返回mRemote變量
inline IBinder* remote() { return mRemote; }
1
而mRemote變量是在建立BpInterface對象時,將remote變量傳給了其父類BpRefBase,咱們這個例子裏面remote就是BpBinder(handle)
template
inline BpInterface::BpInterface(const sp& remote)
: BpRefBase(remote)
{
}
BpRefBase::BpRefBase(const sp& o)
: mRemote(o.get()), mRefs(NULL), mState(0)
{
extendObjectLifetime(OBJECT_LIFETIME_WEAK);
if (mRemote) {
mRemote->incStrong(this); // Removed on first IncStrong().
mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
}
總結一下, 若是asBinder的參數iface是BnInterface類型,則返回其自身,若是參數iface是BpInterface類型,則返回其mRemote遠程代理對象BpBinder(handle) 。