ServiceManager管理着系统中所有的服务,无论是service 还是client只要跟系统中的服务打交道,必先获取ServiceManager的代理,才能获取ServiceManager提供的各项服务。在安卓系统中Service Manager远程代理是一个特殊的Binder引用,它的引用句柄一定是0。获取远程代理的方法defaultServiceManager()的实现在frameworks/native/libs/binder/IServiceManager.cpp文件中:
sp<IServiceManager> defaultServiceManager()
{
if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
{
AutoMutex _l(gDefaultServiceManagerLock);
while (gDefaultServiceManager == NULL) {
gDefaultServiceManager = interface_cast<IServiceManager>(
ProcessState::self()->getContextObject(NULL));
if (gDefaultServiceManager == NULL)
sleep(1);
}
}
return gDefaultServiceManager;
}
gDefaultServiceManager在frameworks/native/libs/binder/Static.cpp中声明:
// ------------ ServiceManager.cpp
Mutex gDefaultServiceManagerLock;
sp<IServiceManager> gDefaultServiceManager;
sp<IPermissionController> gPermissionController;
gDefaultServiceManagerLock和gDefaultServiceManager为单列模式,每个进程只有一个。
进程第一次调用defaultServiceManager()时,会通过interface_cast<IServiceManager>(
创建,下面详细介绍Service Manager远程接口代理的创建过程。
ProcessState::self()->getContextObject(NULL));
我们先来看下老罗画的个图:
从图中我们可以清晰的看出各个类的继承关系。
*IServiceManager类继承了IInterface类,而IInterface类和BpRefBase类又分别继承了RefBase类。在BpRefBase类中,有一个成员变量mRemote,它的类型是IBinder,实现类为BpBinder,它表示一个Binder引用,引用句柄值保存在BpBinder类的mHandle成员变量中。BpBinder类通过IPCThreadState类来和Binder驱动程序并互,而IPCThreadState又通过它的成员变量mProcess来打开/dev/binder设备文件,mProcess成员变量的类型为ProcessState。ProcessState类打开设备/dev/binder之后,将打开文件描述符保存在mDriverFD成员变量中,以供后续使用。
首先是调用ProcessState::self函数,self函数是ProcessState的静态成员函数,它的作用是返回一个全局唯一的ProcessState实例变量,就是单例模式了,这个变量名为gProcess。如果gProcess尚未创建,就会执行创建操作,在ProcessState的构造函数中,会通过open文件操作函数打开设备文件/dev/binder,并且返回来的设备文件描述符保存在成员变量mDriverFD中。**
接着调用gProcess->getContextObject函数来获得一个句柄值为0的Binder引用,即BpBinder了,于是创建Service Manager远程接口的语句可以简化为:
gDefaultServiceManager = interface_cast<IServiceManager>(new BpBinder(0));
interface_cast(new BpBinder(0));是一个模板函数,定义在framework/base/include/binder/IInterface.h文件中:
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
return INTERFACE::asInterface(obj);
}
这里的INTERFACE是IServiceManager,于是调用了IServiceManager::asInterface函数。IServiceManager::asInterface是通过DECLARE_META_INTERFACE(ServiceManager)宏在IServiceManager类中声明的,它位于framework/base/include/binder/IServiceManager.h文件中:
DECLARE_META_INTERFACE(ServiceManager);
展开即为:
#define DECLARE_META_INTERFACE(ServiceManager) \
static const android::String16 descriptor; \
static android::sp<IServiceManager> asInterface( \
const android::sp<android::IBinder>& obj); \
virtual const android::String16& getInterfaceDescriptor() const; \
IServiceManager(); \
virtual ~IServiceManager();
/*
*IServiceManager::asInterface的实现是通过IMPLEMENT_META_INTERFACE(ServiceManager, *"android.os.IServiceManager")宏定义的,它位于*framework/base/libs/binder/IServiceManager.cpp文件中:
*/
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
展开即为:
#define IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager") \
const android::String16 IServiceManager::descriptor("android.os.IServiceManager"); \
const android::String16& \
IServiceManager::getInterfaceDescriptor() const { \
return IServiceManager::descriptor; \
} \
android::sp<IServiceManager> IServiceManager::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<IServiceManager> intr; \
if (obj != NULL) { \
intr = static_cast<IServiceManager*>( \
obj->queryLocalInterface( \
IServiceManager::descriptor).get()); \
if (intr == NULL) { \
intr = new BpServiceManager(obj); \
} \
} \
return intr; \
} \
IServiceManager::IServiceManager() { } \
IServiceManager::~IServiceManager() { }
从中可以看到asInterface的实现:
android::sp<IServiceManager> IServiceManager::asInterface(const android::sp<android::IBinder>& obj)
{
android::sp<IServiceManager> intr;
if (obj != NULL) {
intr = static_cast<IServiceManager*>(
obj->queryLocalInterface(IServiceManager::descriptor).get());
if (intr == NULL) {
intr = new BpServiceManager(obj);
}
}
return intr;
}
obj就为new BpBinder(0),binder类成员函数queryLocalInterface返回的一定为NULL,最终会调用
intr = new BpServiceManager(obj);<===>gDefaultServiceManager = new BpServiceManager(new BpBinder(0));
gDefaultServiceManager 本质上是一个BpServiceManager,包含了一个句柄值为0的Binder引用。