defaultServiceManager函數的實現在IServiceManager.cpp中完成。它會返回一個IServiceManager對象,通過這個對象,我們可以神奇地與另一個進程ServiceManager進行交互。是不是有一種觀看時空穿越魔術表演的感覺?
1. 魔術前的準備工作
先來看看defaultServiceManager都調用了哪些函數?返回的這個IServiceManager到底是什么?具體實現代碼如下所示:
**IServiceManager.cpp**
~~~
sp<IServiceManager> defaultServiceManager()
{
//看樣子又是一個單例,英文名叫 Singleton,Android是一個優秀的源碼庫,大量使用了
//設計模式,建議讀者以此為契機學習設計模式,首推GOF的《設計模式:可復用面向對象軟件的基礎》
if(gDefaultServiceManager != NULL) return gDefaultServiceManager;
{
AutoMutex _l(gDefaultServiceManagerLock);
if(gDefaultServiceManager == NULL) {
//真正的gDefaultServiceManager是在這里創建的。
gDefaultServiceManager = interface_cast<IServiceManager>(
ProcessState::self()->getContextObject(NULL));
}
}
returngDefaultServiceManager;
}
~~~
哦,是調用了ProcessState的getContextObject函數!注意:傳給它的參數是NULL,即0。既然是“庖丁解牛”,就還要一層一層往下切。下面再看getContextObject函數,如下所示:
**ProcessState.cpp**
~~~
sp<IBinder>ProcessState::getContextObject(const sp<IBinder>& caller)
{
/*
caller的值為0!注意,該函數返回的是IBinder。它是什么?我們后面再說。
supportsProcesses函數根據openDriver函數打開設備是否成功來判斷是否支持process
真實設備肯定支持process。
*/
if(supportsProcesses()) {
//真實設備上肯定是支持進程的,所以會調用下面這個函數
return getStrongProxyForHandle(0);
} else {
return getContextObject(String16("default"), caller);
}
}
~~~
getStrongProxyForHandle這個函數名怪怪的,可能會讓人感到些許困惑。請注意,它的調用參數的名字叫handle,Windows編程中經常使用這個名稱,它是對資源的一種標識。說白了,其實就是有一個資源項,保存在一個資源數組(也可以是別的組織結構)中,handle的值正是該資源項在數組中的索引。
**ProcessState.cpp**
~~~
sp<IBinder>ProcessState::getStrongProxyForHandle(int32_t handle)
{
sp<IBinder> result;
AutoMutex_l(mLock);
/*
根據索引查找對應資源。如果lookupHandleLocked發現沒有對應的資源項,則會創建一個新的項并返回。
這個新項的內容需要填充。
*/
handle_entry* e = lookupHandleLocked(handle);
if (e !=NULL) {
IBinder* b = e->binder;
if (b== NULL || !e->refs->attemptIncWeak(this)) {
//對于新創建的資源項,它的binder為空,所以走這個分支。注意,handle的值為0
b= new BpBinder(handle); //創建一個BpBinder
e->binder = b; //填充entry的內容
if (b) e->refs = b->getWeakRefs();
result = b;
}else {
result.force_set(b);
e->refs->decWeak(this);
}
}
returnresult; //返回BpBinder(handle),注意,handle的值為0
}
~~~
2. 魔術表演的道具——BpBinder
眾所周知,玩魔術是必須有道具的。這個穿越魔術的道具就是BpBinder。BpBinder是什么呢?有必要先來介紹它的孿生兄弟BBinder。
BpBinder和BBinder都是Android中與Binder通信相關的代表,它們都從IBinder類中派生而來,如圖6-2所示:
:-: 
圖6-2 Binder家族圖譜
從上圖中可以看出:
- BpBinder是客戶端用來與Server交互的代理類,p即Proxy的意思。
- BBinder則是proxy相對的一端,它是proxy交互的目的端。如果說Proxy代表客戶端,那么BBinder則代表服務端。這里的BpBinder和BBinder是一一對應的,即某個BpBinder只能和對應的BBinder交互。我們當然不希望通過BpBinderA發送的請求,卻由BBinderB來處理。
剛才我們在defaultServiceManager()函數中創建了這個BpBinder。這里有兩個問題:
- 為什么創建的不是BBinder?
因為我們是ServiceManager的客戶端,當然得使用代理端以與ServiceManager交互了。
- 前面說了,BpBinder和BBinder是一一對應的,那么BpBinder如何標識它所對應的BBinder端呢?
答案是Binder系統通過handler來對應BBinder。以后我們會確認這個Handle值的作用。
* * * * *
**注意**:我們給BpBinder構造函數傳的參數handle的值是0。這個0在整個Binder系統中有重要含義—因為0代表的就是ServiceManager所對應的BBinder。
* * * * *
BpBinder是如此重要,必須對它進行深入分析,其代碼如下所示:
**BpBinder.cpp**
~~~
BpBinder::BpBinder(int32_t handle)
:mHandle(handle)//handle是0
,mAlive(1)
,mObitsSent(0)
,mObituaries(NULL)
{
extendObjectLifetime(OBJECT_LIFETIME_WEAK);
//另一個重要對象是IPCThreadState,我們稍后會詳細講解。
IPCThreadState::self()->incWeakHandle(handle);
}
~~~
看上面的代碼,會覺得BpBinder確實簡單,不過再仔細查看,你或許會發現,BpBinder、BBinder這兩個類沒有任何地方操作ProcessState打開的那個/dev/binder設備,換言之,這兩個Binder類沒有和binder設備直接交互。那為什么說BpBinder會與通信相關呢?注意本小節的標題,BpBinder只是道具嘛!所以它后面一定還另有機關。不必急著揭秘,還是先回顧一下道具出場的歷程。
我們是從下面這個函數開始分析的:
~~~
gDefaultServiceManager =interface_cast<IServiceManager>( ProcessState::self()->getContextObject(NULL));
~~~
現在這個函數調用將變成如下所示:
~~~
gDefaultServiceManager =interface_cast<IServiceManager>(new BpBinder(0));
~~~
這里出現了一個interface_cast。它是什么?其實是一個障眼法!下面就來具體分析它。
3. 障眼法——interface_cast
interface_cast、dynamic_cast和static_cast看起來是否非常眼熟?它們是指針類型轉換的意思嗎?如果是,那又是如何將BpBinder*類型強制轉化成IServiceManager*類型的?BpBinder的家譜我們剛才也看了,它的“爸爸的爸爸的爸爸”這條線上沒有任何一個與IServiceManager有任何關系。
問題談到這里,我們得去看看interface_cast的具體實現,其代碼如下所示:
**IInterface.h**
~~~
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(constsp<IBinder>& obj)
{
returnINTERFACE::asInterface(obj);
}
~~~
哦,僅僅是一個模板函數,所以interface_cast<IServiceManager>()等價于:
~~~
inline sp<IServiceManager>interface_cast(const sp<IBinder>& obj)
{
return IServiceManager::asInterface(obj);
}
~~~
又轉移到IServiceManager對象中去了,這難道不是障眼法嗎?既然找到了“真身”,不妨就來見識見識它吧。
4. 撥開浮云見月明——IServiceManager
剛才提到,IBinder家族的BpBinder和BBinder是與通信業務相關的,那么業務層的邏輯又是如何巧妙地架構在Binder機制上的呢?關于這些問題,可以用一個絕好的例子來解釋,它就是IServiceManager。
(1)定義業務邏輯
先回答第一個問題:如何表述應用的業務層邏輯。可以先分析一下IServiceManager是怎么做的。IServiceManager定義了ServiceManager所提供的服務,看它的定義可知,其中有很多有趣的內容。IServiceManager定義在IServiceManager.h中,代碼如下所示:
**IServiceManager.h**
~~~
class IServiceManager : public IInterface
{
public:
//關鍵無比的宏!
DECLARE_META_INTERFACE(ServiceManager);
//下面是ServiceManager所提供的業務函數
virtualsp<IBinder> getService( constString16& name) const = 0;
virtualsp<IBinder> checkService( constString16& name) const = 0;
virtualstatus_t addService( const String16& name,
const sp<IBinder>&service) = 0;
virtual Vector<String16> listServices() = 0;
......
};
~~~
(2)業務與通信的掛鉤
Android巧妙地通過DECLARE_META_INTERFACE和IMPLENT宏,將業務和通信牢牢地鉤在了一起。DECLARE_META_INTERFACE和IMPLEMENT_META_INTERFACE這兩個宏都定義在剛才的IInterface.h中。先看DECLARE_META_INTERFACE這個宏,如下所示:
**IInterface.h::DECLARE_META_INTERFACE**
~~~
#define DECLARE_META_INTERFACE(INTERFACE) \
staticconst android::String16 descriptor; \
staticandroid::sp<I##INTERFACE> asInterface( \
const android::sp<android::IBinder>& obj); \
virtualconst android::String16& getInterfaceDescriptor() const; \
I##INTERFACE(); \
virtual~I##INTERFACE();
~~~
將IServiceManager的DELCARE宏進行相應的替換后得到的代碼如下所示:
**DECLARE_META_INTERFACE(IServiceManager)**
~~~
//定義一個描述字符串
static const android::String16 descriptor;
//定義一個asInterface函數
static android::sp< IServiceManager >
asInterface(constandroid::sp<android::IBinder>& obj)
//定義一個getInterfaceDescriptor函數,估計就是返回descriptor字符串
virtual const android::String16&getInterfaceDescriptor() const;
//定義IServiceManager的構造函數和析構函數
IServiceManager ();
virtual ~IServiceManager();
~~~
DECLARE宏聲明了一些函數和一個變量,那么,IMPLEMENT宏的作用肯定就是定義它們了。IMPLEMENT的定義在IInterface.h中,IServiceManager是如何使用了這個宏呢?只有一行代碼,在IServiceManager.cpp中,如下所示:
~~~
IMPLEMENT_META_INTERFACE(ServiceManager,"android.os.IServiceManager");
~~~
很簡單,可直接將IServiceManager中的IMPLEMENT宏的定義展開,如下所示:
~~~
const android::String16
IServiceManager::descriptor(“android.os.IServiceManager”);
//實現getInterfaceDescriptor函數
const android::String16& IServiceManager::getInterfaceDescriptor()const
{
//返回字符串descriptor,值是“android.os.IServiceManager”
return IServiceManager::descriptor;
}
//實現asInterface函數
android::sp<IServiceManager>
IServiceManager::asInterface(constandroid::sp<android::IBinder>& obj)
{
android::sp<IServiceManager> intr;
if(obj != NULL) {
intr = static_cast<IServiceManager *>(
obj->queryLocalInterface(IServiceManager::descriptor).get());
if (intr == NULL) {
//obj是我們剛才創建的那個BpBinder(0)
intr = new BpServiceManager(obj);
}
}
return intr;
}
//實現構造函數和析構函數
IServiceManager::IServiceManager () { }
IServiceManager::~ IServiceManager() { }
~~~
我們曾提出過疑問:interface_cast是如何把BpBinder指針轉換成一個IServiceManager指針的呢?答案就在asInterface函數的一行代碼中,如下所示:
~~~
intr = new BpServiceManager(obj);
~~~
明白了!interface_cast不是指針的轉換,而是利用BpBinder對象作為參數新建了一個BpServiceManager對象。我們已經知道BpBinder和BBinder與通信有關系,這里怎么突然冒出來一個BpServiceManager?它們之間又有什么關系呢?
(3)IServiceManager家族
要搞清這個問題,必須先了解IServiceManager家族之間的關系,先來看圖6-3,它展示了IServiceManager的家族圖譜。
:-: 
圖6-3 IServiceManager的家族圖譜
根據圖6-3和相關的代碼可知,這里有以下幾個重要的點值得注意:
- IServiceManager、BpServiceManager和BnServiceManager都與業務邏輯相關。
- BnServiceManager同時從BBinder派生,表示它可以直接參與Binder通信。
- BpServiceManager雖然從BpInterface中派生,但是這條分支似乎與BpBinder沒有關系。
- BnServiceManager是一個虛類,它的業務函數最終需要子類來實現。
>[info]重要說明:以上這些關系很復雜,但ServiceManager并沒有使用錯綜復雜的派生關系,它直接打開Binder設備并與之交互。后文,還會詳細分析它的實現代碼。
圖6-3中的BpServiceManager,既然不像它的兄弟BnServiceManager那樣直接與Binder有血緣關系,那么它又是如何與Binder交互的呢?簡言之,BpRefBase中的mRemote的值就是BpBinder。如果你不相信,仔細看BpServiceManager左邊的派生分支樹上的一系列代碼,它們都在IServiceManager.cpp中,如下所示:
**IServiceManager.cpp::BpServiceManager類**
~~~
//通過它的參數可得知,impl是IBinder類型,看來與Binder有間接關系,它實際上是BpBinder對象
BpServiceManager(const sp<IBinder>& impl)
//調用基類BpInterface的構造函數
: BpInterface<IServiceManager>(impl)
{
}
~~~
BpInterface的實現代碼如下所示:
**IInterface.h::BpInterface類**
~~~
template<typename INTERFACE>
inlineBpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
:BpRefBase(remote)//基類構造函數
{
}
~~~
BpRefBase()的實現代碼如下所示:
**Binder.cpp::BpRefBase類**
~~~
BpRefBase::BpRefBase(const sp<IBinder>&o)
//mRemote最終等于那個new 出來的BpBinder(0)
:mRemote(o.get()), mRefs(NULL), mState(0)
{
extendObjectLifetime(OBJECT_LIFETIME_WEAK);
if(mRemote) {
mRemote->incStrong(this);
mRefs= mRemote->createWeak(this);
}
}
~~~
原來,BpServiceManager的一個變量mRemote是指向了BpBinder。至此,我們的魔術表演完了,回想一下defaultServiceManager函數,可以得到以下兩個關鍵對象:
* 有一個BpBinder對象,它的handle值是0。
* 有一個BpServiceManager對象,它的mRemote值是BpBinder。
BpServiceManager對象實現了IServiceManager的業務函數,現在又有BpBinder作為通信的代表,接下來的工作就簡單了。下面,要通過分析MediaPlayerService的注冊過程,進一步分析業務函數的內部是如何工作的。
- 前言
- 第1章 閱讀前的準備工作
- 1.1 系統架構
- 1.1.1 Android系統架構
- 1.1.2 本書的架構
- 1.2 搭建開發環境
- 1.2.1 下載源碼
- 1.2.2 編譯源碼
- 1.3 工具介紹
- 1.3.1 Source Insight介紹
- 1.3.2 Busybox的使用
- 1.4 本章小結
- 第2章 深入理解JNI
- 2.1 JNI概述
- 2.2 學習JNI的實例:MediaScanner
- 2.3 Java層的MediaScanner分析
- 2.3.1 加載JNI庫
- 2.3.2 Java的native函數和總結
- 2.4 JNI層MediaScanner的分析
- 2.4.1 注冊JNI函數
- 2.4.2 數據類型轉換
- 2.4.3 JNIEnv介紹
- 2.4.4 通過JNIEnv操作jobject
- 2.4.5 jstring介紹
- 2.4.6 JNI類型簽名介紹
- 2.4.7 垃圾回收
- 2.4.8 JNI中的異常處理
- 2.5 本章小結
- 第3章 深入理解init
- 3.1 概述
- 3.2 init分析
- 3.2.1 解析配置文件
- 3.2.2 解析service
- 3.2.3 init控制service
- 3.2.4 屬性服務
- 3.3 本章小結
- 第4章 深入理解zygote
- 4.1 概述
- 4.2 zygote分析
- 4.2.1 AppRuntime分析
- 4.2.2 Welcome to Java World
- 4.2.3 關于zygote的總結
- 4.3 SystemServer分析
- 4.3.1 SystemServer的誕生
- 4.3.2 SystemServer的重要使命
- 4.3.3 關于 SystemServer的總結
- 4.4 zygote的分裂
- 4.4.1 ActivityManagerService發送請求
- 4.4.2 有求必應之響應請求
- 4.4.3 關于zygote分裂的總結
- 4.5 拓展思考
- 4.5.1 虛擬機heapsize的限制
- 4.5.2 開機速度優化
- 4.5.3 Watchdog分析
- 4.6 本章小結
- 第5章 深入理解常見類
- 5.1 概述
- 5.2 以“三板斧”揭秘RefBase、sp和wp
- 5.2.1 第一板斧--初識影子對象
- 5.2.2 第二板斧--由弱生強
- 5.2.3 第三板斧--破解生死魔咒
- 5.2.4 輕量級的引用計數控制類LightRefBase
- 5.2.5 題外話-三板斧的來歷
- 5.3 Thread類及常用同步類分析
- 5.3.1 一個變量引發的思考
- 5.3.2 常用同步類
- 5.4 Looper和Handler類分析
- 5.4.1 Looper類分析
- 5.4.2 Handler分析
- 5.4.3 Looper和Handler的同步關系
- 5.4.4 HandlerThread介紹
- 5.5 本章小結
- 第6章 深入理解Binder
- 6.1 概述
- 6.2 庖丁解MediaServer
- 6.2.1 MediaServer的入口函數
- 6.2.2 獨一無二的ProcessState
- 6.2.3 時空穿越魔術-defaultServiceManager
- 6.2.4 注冊MediaPlayerService
- 6.2.5 秋風掃落葉-StartThread Pool和join Thread Pool分析
- 6.2.6 你徹底明白了嗎
- 6.3 服務總管ServiceManager
- 6.3.1 ServiceManager的原理
- 6.3.2 服務的注冊
- 6.3.3 ServiceManager存在的意義
- 6.4 MediaPlayerService和它的Client
- 6.4.1 查詢ServiceManager
- 6.4.2 子承父業
- 6.5 拓展思考
- 6.5.1 Binder和線程的關系
- 6.5.2 有人情味的訃告
- 6.5.3 匿名Service
- 6.6 學以致用
- 6.6.1 純Native的Service
- 6.6.2 扶得起的“阿斗”(aidl)
- 6.7 本章小結
- 第7章 深入理解Audio系統
- 7.1 概述
- 7.2 AudioTrack的破解
- 7.2.1 用例介紹
- 7.2.2 AudioTrack(Java空間)分析
- 7.2.3 AudioTrack(Native空間)分析
- 7.2.4 關于AudioTrack的總結
- 7.3 AudioFlinger的破解
- 7.3.1 AudioFlinger的誕生
- 7.3.2 通過流程分析AudioFlinger
- 7.3.3 audio_track_cblk_t分析
- 7.3.4 關于AudioFlinger的總結
- 7.4 AudioPolicyService的破解
- 7.4.1 AudioPolicyService的創建
- 7.4.2 重回AudioTrack
- 7.4.3 聲音路由切換實例分析
- 7.4.4 關于AudioPolicy的總結
- 7.5 拓展思考
- 7.5.1 DuplicatingThread破解
- 7.5.2 題外話
- 7.6 本章小結
- 第8章 深入理解Surface系統
- 8.1 概述
- 8.2 一個Activity的顯示
- 8.2.1 Activity的創建
- 8.2.2 Activity的UI繪制
- 8.2.3 關于Activity的總結
- 8.3 初識Surface
- 8.3.1 和Surface有關的流程總結
- 8.3.2 Surface之乾坤大挪移
- 8.3.3 乾坤大挪移的JNI層分析
- 8.3.4 Surface和畫圖
- 8.3.5 初識Surface小結
- 8.4 深入分析Surface
- 8.4.1 與Surface相關的基礎知識介紹
- 8.4.2 SurfaceComposerClient分析
- 8.4.3 SurfaceControl分析
- 8.4.4 writeToParcel和Surface對象的創建
- 8.4.5 lockCanvas和unlockCanvasAndPost分析
- 8.4.6 GraphicBuffer介紹
- 8.4.7 深入分析Surface的總結
- 8.5 SurfaceFlinger分析
- 8.5.1 SurfaceFlinger的誕生
- 8.5.2 SF工作線程分析
- 8.5.3 Transaction分析
- 8.5.4 關于SurfaceFlinger的總結
- 8.6 拓展思考
- 8.6.1 Surface系統的CB對象分析
- 8.6.2 ViewRoot的你問我答
- 8.6.3 LayerBuffer分析
- 8.7 本章小結
- 第9章 深入理解Vold和Rild
- 9.1 概述
- 9.2 Vold的原理與機制分析
- 9.2.1 Netlink和Uevent介紹
- 9.2.2 初識Vold
- 9.2.3 NetlinkManager模塊分析
- 9.2.4 VolumeManager模塊分析
- 9.2.5 CommandListener模塊分析
- 9.2.6 Vold實例分析
- 9.2.7 關于Vold的總結
- 9.3 Rild的原理與機制分析
- 9.3.1 初識Rild
- 9.3.2 RIL_startEventLoop分析
- 9.3.3 RIL_Init分析
- 9.3.4 RIL_register分析
- 9.3.5 關于Rild main函數的總結
- 9.3.6 Rild實例分析
- 9.3.7 關于Rild的總結
- 9.4 拓展思考
- 9.4.1 嵌入式系統的存儲知識介紹
- 9.4.2 Rild和Phone的改進探討
- 9.5 本章小結
- 第10章 深入理解MediaScanner
- 10.1 概述
- 10.2 android.process.media分析
- 10.2.1 MSR模塊分析
- 10.2.2 MSS模塊分析
- 10.2.3 android.process.media媒體掃描工作的流程總結
- 10.3 MediaScanner分析
- 10.3.1 Java層分析
- 10.3.2 JNI層分析
- 10.3.3 PVMediaScanner分析
- 10.3.4 關于MediaScanner的總結
- 10.4 拓展思考
- 10.4.1 MediaScannerConnection介紹
- 10.4.2 我問你答
- 10.5 本章小結