Android(安卓)Binder 机制之ServiceManager 续
上一篇写完了servicemanager进程,其主要功能1. 注册服务(addService) 2. 获取服务(getService),并且通过一个链表来维护当前已经注册的service组件。接下来的分析,将进入binder的框架的世界,首先来分析下IServiceManager接口,注册服务和获取服务都是从这里开始的。
在介绍IServiceManager接口之前,先看下系统进程mediaserver中运行的服务,main_mediaserver.cpp 的main函数
int main(int argc __unused, char** argv){ //这里只看重点内容,其他不相关代码省略 if (doLog && (childPid = fork()) != 0) { } else { // all other services if (doLog) { prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also setpgid(0, 0); // but if I die first, don't kill my parent } InitializeIcuOrDie(); sp proc(ProcessState::self()); sp sm = defaultServiceManager(); ALOGI("ServiceManager: %p", sm.get()); AudioFlinger::instantiate(); MediaPlayerService::instantiate(); //将MediaPlayService服务注册到ServiceManager中 ResourceManagerService::instantiate(); CameraService::instantiate(); AudioPolicyService::instantiate(); SoundTriggerHwService::instantiate(); RadioService::instantiate(); //上面这些服务都是通过IServieManager接口,注册到ServiceManager的 registerExtensions(); ProcessState::self()->startThreadPool(); //接下来这两句代码,也是重量级的,没有他们,都是白搭 IPCThreadState::self()->joinThreadPool(); }}
大家都知道,Native层的很多服务都是运行在mediaserver进程中的(Android 7.0 之后,很多服务就运行在其独立的进程了,比如ICameraService,就单独运行在cameraserver进程中了)。但是上面好像并没有出现sm->addService(serviceName)之类的语句,那是因为有BinderService,这个一个模板类,对service组件进行注册。
下面截取BinderService的注册服务的关键代码
templateclass BinderService{public: static status_t publish(bool allowIsolated = false) { sp sm(defaultServiceManager()); //获取代理对象BpServiceManager(BpBinder) return sm->addService( //将SERVICE 注册到ServiceManager中 String16(SERVICE::getServiceName()), //getServiceName由service组件实现 SERVICE即是service类名 new SERVICE(), allowIsolated); } //service组件通过继承BinderService,调用该接口完成到ServiceManager的注册 static void instantiate() { publish(); }
上面的代码中 sp
sp defaultServiceManager(){ if (gDefaultServiceManager != NULL) return gDefaultServiceManager; // 这个是个单例的ServiceManager代理对象 { AutoMutex _l(gDefaultServiceManagerLock); while (gDefaultServiceManager == NULL) { gDefaultServiceManager = interface_cast( ProcessState::self()->getContextObject(NULL)); //得到一个IBinder对象, interface_cast将其转为BpServiceManager if (gDefaultServiceManager == NULL) sleep(1); } } //返回单例的BpServiceManager对象 return gDefaultServiceManager;}
上面比较有意思的是,ProccessState::self()->getContextObject(NULL),它干什么,能返回一个IBinder对象?
可分三步走:1. self() 2. getContextObject(NULL) 3. getStrongProxyForHandle(0)
sp ProcessState::getContextObject(const sp& /*caller*/){ return getStrongProxyForHandle(0); //返回一个BpBinder对象}
sp ProcessState::getStrongProxyForHandle(int32_t handle){ sp result; AutoMutex _l(mLock); // 一般情况下,返回的e是一个非空指针 handle_entry* e = lookupHandleLocked(handle); if (e != NULL) { IBinder* b = e->binder; if (b == NULL || !e->refs->attemptIncWeak(this)) { if (handle == 0) { Parcel data; status_t status = IPCThreadState::self()->transact( 0, IBinder::PING_TRANSACTION, data, NULL, 0); if (status == DEAD_OBJECT) return NULL; } b = new BpBinder(handle); //创建BpBinder(0); e->binder = b; if (b) e->refs = b->getWeakRefs(); result = b; //返回BpBinder(0) 指针 } else { // This little bit of nastyness is to allow us to add a primary // reference to the remote proxy when this team doesn't have one // but another team is sending the handle to us. result.force_set(b); e->refs->decWeak(this); } } return result;}
上面看清楚了,ProccessState::self()->getContextObject(NULL); 返回了BpBinder(0)指针
在回到defaultServiceManager中,
gDefaultServiceManager = interface_cast( // 关键在于interface_cast,它又做了什么? ProcessState::self()->getContextObject(NULL));
template //这是一个模板方法inline sp interface_cast(const sp& obj){ return INTERFACE::asInterface(obj); //这里才是关键的地方,要找到这个,还要从一个宏看起}
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager"); //IServiceManager.cpp 中实现
IMPLEMENT_META_INTERFACE //其定义如下
#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::asInterface( \ const android::sp& obj) \ { \ android::sp intr; \ if (obj != NULL) { \ intr = static_cast( \ obj->queryLocalInterface( \ I##INTERFACE::descriptor).get()); \ if (intr == NULL) { \ intr = new Bp##INTERFACE(obj); \ } \ } \ return intr; \ } \ I##INTERFACE::I##INTERFACE() { } \ I##INTERFACE::~I##INTERFACE() { }
根据上面的宏定义,将asInterface接口进行宏展开就是如下这个样子
android::sp< IServiceManager > IServiceManager::asInterface( const android::sp& obj) { android::sp< IServiceManager > intr; if (obj != NULL) { intr = static_cast< IServiceManager *>( obj->queryLocalInterface(IServiceManager::descriptor).get()); if (intr == NULL) { //第一次进来,intr是为NULL,所有创建了BpServiceManager(obj) intr = new BpServiceManager(obj); //创建BpServiceManager } } return intr; } }}
BpServiceManager(const sp& impl) //BpServiceManager的构造函数 : BpInterface(impl) { }
通过BpBinder(0) 来创建了BpServiceManager对象,这是一个代理对象。而BpServiceManager继承BpInterface,
上面BpInterface
templateinline BpInterface::BpInterface(const sp& remote) : BpRefBase(remote) //BpBinder(0) 传递到remote中{}
BpRefBase::BpRefBase(const sp& o) : mRemote(o.get()), mRefs(NULL), mState(0) //mRemote(o.get()),最终BpBinder(0) 传递到BpRefBase::mRemote中{ extendObjectLifetime(OBJECT_LIFETIME_WEAK); if (mRemote) { mRemote->incStrong(this); // Removed on first IncStrong(). mRefs = mRemote->createWeak(this); // Held for our entire lifetime. }}
从上面的代码跟踪,我们知道创建了BpBinder(0),BpServiceManager(BpBinder(0)), BpInterface(BpBinder(0)), BpRefBase(BpBinder(0)), 最终传递到IBinder* mRemote中。其中0就是ServiceManager代理对象对应于Binder引用的。
这里仅仅介绍的是IServiceManager接口,对应的Client组件的创建过程,仍然没有到Binder通信的流程中。
未完,待续。。。
更多相关文章
- Android中实现IPC的几种方式详细分析及比较
- Android(安卓)Intent安全性检查
- android多线程机制
- Android4.2.2 Gallery2源码分析(8)——假装的Activity
- android通过Camera录制视频
- Android开发框架-架构篇
- Android(安卓)项目组件化之创建module,生成aar,引入aar
- Android(安卓)Webview组件使用总结
- 如何通过日志来调试Web App