-----------------------------------------重點在BpServiceManager和BpBinder(0)-------------------------------------
1、
RefBase.cpp
RefBase.h
定義了如下幾個類:
一、class
RefBase{}
定義了所有類的基類,他應該是一個普遍基類,并不是一個抽象類。
二、
template
class sp{}
模板智能指針sp,是binder系統(tǒng)中最常用的類型,用來執(zhí)行具體的類型變量,
類內(nèi)使用
T*m_ptr;
存儲指向類型對象的指針。
使用
inlineT*get() const{ return m_ptr; }
get函數(shù)獲取指向的對象。
三、
template
class wp{}
2、
IBinder.h
Binder.cpp
只實現(xiàn)一個類,
class IBinder : public virtual RefBase{}
注意繼承自RefBase,并且指定為虛基類,防止“二義性”。
這是Binder最重要的一個低層類,上層所有Binder類型都是繼承自它。
他是一個抽象類,除了幾個重要虛函數(shù)外,其他的都是純虛函數(shù)。
幾個重要的虛函數(shù)如下所示,
spIBinder::queryLocalInterface(const String16&descriptor)----后續(xù)可以用來查詢派生類的IInterface
BBinder* IBinder::localBinder()----返回一個本地的Binder對象--BBinder
BpBinder* IBinder::remoteBinder()----返回一個代理的Binder對象--BpBinder
bool IBinder::checkSubclass(const void*
/*subclassID*/) const----檢查派生類
3、
Binder.h
Binder.cpp
實現(xiàn)兩個類,如下所示:
一、class
BBinder : public IBinder{}
這個類public繼承自IBinder,用來實現(xiàn)Binder的本地對象。注意它在private部分聲明了
BBinder&operator=(const BBinder& o);
說明這個類對象不能使用operator=進行賦值操作,即對operator=放到了private區(qū),進行了隱藏。
另外,此類對其基類IBinder的所有純虛函數(shù)進行了實現(xiàn),因此此類可以創(chuàng)建對象,不是一個抽線類。
二、class
BpRefBase : public virtual RefBase{}
這個類應該是最深基類RefBase的一個“代理”,它也是一個“虛基類”,防止后續(xù)派生類產(chǎn)生“二義性”
因為是代理,它有一個重要的private成員,
IBinder* constmRemote;
IBinder指針,指向遠程的IBinder對象。
使用函數(shù),
inlineIBinder*remote(){ return mRemote; }
來獲取。
另外,在private區(qū),有如下定義,
BpRefBase&operator=(const BpRefBase&o);
對operator=進行了隱藏,說明此類對象不支持賦值操作。
private區(qū)還有一個變量,
RefBase::weakref_type*mRefs;
目前不知道做什么用的。
4、
BpBinder.h
BpBinder.cpp
class BpBinder : public IBinder{}
這個類是一個重要的類,用它來實現(xiàn)和其他service的交互。public繼承自IBinder,用來實現(xiàn)Binder的代理對象。
定義了一個重要的函數(shù),
virtual BpBinder*remoteBinder();
用來返回它自身。
注意函數(shù)defaultServiceManager中,
sp b =
ProcessState::self()->getContextObject(NULL);
相當于,
sp b = new BpBinder(0);
注意此類中的成員變量mHandle保存有Binder設備ID,永遠為0。
5、
IInterface.h
IInterface.cpp
主要實現(xiàn)如下幾個類:
一、class
IInterface : public virtual RefBase{}
所有類的一個公共接口,他是一個抽象類,只能用作繼承。注意它使用了virtual繼承RefBase,因此它成為“虛基類”,防止后續(xù)類再繼承它時產(chǎn)生“二義性”。
此類注意onAsBinder是一個純虛函數(shù),后續(xù)的派生類都需要有一個實現(xiàn),他的返回值為IBinder。
二、template
INTERFACE>
class BnInterface : public INTERFACE,
public BBinder
模板類BnInterface,除了繼承模板指定的類外,還繼承了BBinder。
有三個重要的虛基類,
virtual spqueryLocalInterface(const String16&_descriptor);
virtual const String16&getInterfaceDescriptor() const;
virtual IBinder*onAsBinder();
其中onAsBinder繼承了基類IInterface的純虛函數(shù)。
三、template
INTERFACE>
class BpInterface : public INTERFACE,
public BpRefBase
模板類BpInterface,除了繼承模板指定的類外,還繼承了代理類BpRefBase。
BpInterface是BnInterface的代理,聲明一個重要函數(shù)
virtual IBinder*onAsBinder();
6、
IServiceManager.h
IServiceManager.cpp
主要實現(xiàn)如下幾個類:
一、
class IServiceManager : public IInterface{}
繼承自抽象類IInterface,自身也是抽象類,定義了如下幾個純虛函數(shù):
virtual spgetService( const String16& name)const = 0;
virtual spcheckService( const String16&name) const = 0;
virtual status_taddService( const String16&name,
const sp& service) = 0;
virtual VectorlistServices() = 0;
二、class
BnServiceManager : public BnInterface
展開就是classBnServiceManager :publicIServiceManager, public BBinder
即繼承了IServiceManager和BBinder,他是ServiceManager的一個本地對象。
聲明一個重要的虛汗數(shù)
virtual status_tonTransact(uint32_t code,
const Parcel& data,
Parcel*reply,
uint32_tflags = 0);
用來對消息進行分發(fā)。
三、class
BpServiceManager : public BpInterface
展開就class
BpServiceManager : public IServiceManager, public BpRefBase
即繼承了IServiceManager和BpRefBase,他是ServiceManager的一個代理對象。
此代理對象對所有的消息進行處理
四、
這個文件中注意一個重要的全局函數(shù),
sp
defaultServiceManager()
用來創(chuàng)建一個全局的IServiceManager對象,
sp
gDefaultServiceManager;
這個函數(shù)也創(chuàng)建的是單例,因為函數(shù)實現(xiàn)的第一句是,
if (gDefaultServiceManager != NULL) return
gDefaultServiceManager;
里面有這樣一句話,
gDefaultServiceManager = interface_cast(
ProcessState::self()->getContextObject(NULL));
這樣演變,
gDefaultServiceManager = interface_cast(newBpBinder(0));
gDefaultServiceManager = IServiceManager::asInterface(new BpBinder(0));
看下IServiceManager::asInterface的定義:
android::sp I##INTERFACE::asInterface(\
const android::sp& obj)\
{\
android::sp intr;\
if (obj != NULL) {\
intr =static_cast(\
obj->queryLocalInterface(\
I##INTERFACE::descriptor).get());\
if (intr == NULL) {\
intr = newBp##INTERFACE(obj);\
}\
}\
return intr;\
}\
即
android::sp IServiceManager::asInterface(constandroid::sp& obj)
{
android::sp intr;
if (obj != NULL) {
intr = static_cast(
obj->queryLocalInterface(IServiceManager::descriptor).get());
if (intr == NULL) {
intr = new BpServiceManager(obj);
}
}
return intr;
}
因為new BpBinder(0)不為空,所以執(zhí)行
static_cast(
obj->queryLocalInterface(IServiceManager::descriptor).get());
即
執(zhí)行(new
BpBinder(0))的queryLocalInterface方法,BpBinder沒有重定義此虛函數(shù),它繼承自IBinder,IBinder已經(jīng)實現(xiàn),將調(diào)用基類IBinder的此函數(shù),可以看到,
spIBinder::queryLocalInterface(constString16& descriptor)
{
return NULL;
}
返回NULL,因此它將執(zhí)行
intr = new BpServiceManager(obj);
即
intr = new BpServiceManager(new
BpBinder(0));
因此函數(shù)defaultServiceManager,最終返回new BpServiceManager(new
BpBinder(0))給gDefaultServiceManager。
gDefaultServiceManager = new
BpServiceManager(new BpBinder(0));
五、
通過BpServiceManager的構(gòu)造函數(shù)
BpServiceManager(const sp& impl)
: BpInterface(impl)
{
}
可以看到new
BpBinder(0)傳給BpInterface(new
BpBinder(0)),在調(diào)用BpInterface的構(gòu)造函數(shù),
template
inline
BpInterface::BpInterface(const sp& remote)
:BpRefBase(remote)
{
}
繼續(xù)傳給基類BpRefBase的構(gòu)造函數(shù),
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.
}
}
所以
new BpBinder(0)
最終是保存在了BpRefBase中的
IBinder* constmRemote;
成員變量中。
7、
ProcessState.h
ProcessState.cpp
class ProcessState : public virtual
RefBase{}
此文件定義類對象
gProcess = new ProcessState
,他是一個單例模式,每個進程只會有一個,可以通過函數(shù)聲明
staticspself();
它是static的確定。
ProcessState的主要作用:
(1)、打開/dev/binder,建立與內(nèi)核Binder驅(qū)動交互的通道;
(2)、對返回的fd進行mmap映射,使內(nèi)核Binder驅(qū)動分配一塊內(nèi)存接受數(shù)據(jù),地址起始地址由成員變量mVMStart保存;
(3)、由于具有唯一性,只會創(chuàng)建一次,因此一個設備直打開設備/dev/binder一次,一旦打開,全局有效(因為使用全變變量gProcess保存的),后續(xù)直接使用即可。
8、
IPCThreadState.h
IPCThreadState.cpp
定義類class
IPCThreadState{}
此類用來創(chuàng)建一個線程在和Binder交互工作中使用的數(shù)據(jù)IPCThreadState對象。
每個線程都有一個IPCThreadState,每個IPCThreadState都有一個mIn,一個mOut,mIn用來接收來自Binder設備的數(shù)據(jù),而mOut用來存儲發(fā)往Binder設備數(shù)據(jù)的。
一、靜態(tài)取自身函數(shù)和構(gòu)造函數(shù)
IPCThreadState* IPCThreadState::self()
{
if (gHaveTLS) {
restart:
const pthread_key_t k = gTLS;
IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
if (st) return st;
return new IPCThreadState;
}
if (gShutdown) return NULL;
pthread_mutex_lock(&gTLSMutex);
if (!gHaveTLS) {
if (pthread_key_create(&gTLS, threadDestructor) != 0) {
pthread_mutex_unlock(&gTLSMutex);
return NULL;
}
gHaveTLS = true;
}
pthread_mutex_unlock(&gTLSMutex);
goto restart;
}
其中
static pthread_key_t gTLS = 0;
用來表示線程本地存儲空間(Thread
Local Storage)的ID,如果它存在表示IPCThreadState(代表線程本地存儲空間)已經(jīng)創(chuàng)建,使用
IPCThreadState* st =
(IPCThreadState*)pthread_getspecific(k)將其取出,然后返回它的值。
如果為空,使用return
new IPCThreadState;創(chuàng)建一個。
如果gHaveTLS不存在,代表線程本地存儲空間未建立,用
(pthread_key_create(&gTLS,
threadDestructor) != 0)
進行創(chuàng)建,然后跳到restart處,將其放回。
IPCThreadState::IPCThreadState()
:mProcess(ProcessState::self()),
mMyThreadId(androidGetTid()),
mStrictModePolicy(0),
mLastTransactionBinderFlags(0)
{
pthread_setspecific(gTLS,this);
clearCaller();
mIn.setDataCapacity(256);
mOut.setDataCapacity(256);
}
注意其中的函數(shù)調(diào)用
pthread_setspecific(gTLS, this);
將gTLS和對象IPCThreadState聯(lián)系到一起。
另外,初始化列表中有mProcess(ProcessState::self()),說明使用mProcess將本進程進行了保存。