你好,游客 登录 注册 搜索
背景:
阅读新闻

Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析

[日期:2011-07-31] 来源:csdn  作者:Luoshengyang [字体:大 中 小]

ServiceManager类有一个静态成员函数getIServiceManager,它的作用就是用来获取Service Manager的Java远程接口了,而这个函数又是通过ServiceManagerNative来获取Service Manager的Java远程接口的。

接下来,我们就看一下ServiceManager.getIServiceManager这个函数的实现,这个函数定义在frameworks/base/core/java/Android/os/ServiceManager.java文件中:

  1. public final class ServiceManager {  
  2.     ......  
  3.     private static IServiceManager sServiceManager;  
  4.     ......  
  5.     private static IServiceManager getIServiceManager() {  
  6.         if (sServiceManager != null) {  
  7.             return sServiceManager;  
  8.         }  
  9.   
  10.         // Find the service manager   
  11.         sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());  
  12.         return sServiceManager;  
  13.     }  
  14.     ......  
  15. }  

如果其静态成员变量sServiceManager尚未创建,那么就调用ServiceManagerNative.asInterface函数来创建。在调用ServiceManagerNative.asInterface函数之前,首先要通过BinderInternal.getContextObject函数来获得一个BinderProxy对象。

我们来看一下BinderInternal.getContextObject的实现,这个函数定义在frameworks/base/core/java/com/android/internal/os/BinderInternal.java文件中:

  1. public class BinderInternal {  
  2.     ......  
  3.     /** 
  4.     * Return the global "context object" of the system.  This is usually 
  5.     * an implementation of IServiceManager, which you can use to find 
  6.     * other services. 
  7.     */  
  8.     public static final native IBinder getContextObject();  
  9.       
  10.     ......  
  11. }   

这里可以看出,BinderInternal.getContextObject是一个JNI方法,它实现在frameworks/base/core/jni/android_util_Binder.cpp文件中:

  1. static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)  
  2. {  
  3.     sp<IBinder> b = ProcessState::self()->getContextObject(NULL);  
  4.     return javaObjectForIBinder(env, b);  
  5. }  

这里看到我们熟悉的ProcessState::self()->getContextObject函数,具体可以参考浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路一文。ProcessState::self()->getContextObject函数返回一个BpBinder对象,它的句柄值是0,即下面语句:

  1. sp<IBinder> b = ProcessState::self()->getContextObject(NULL);  

   相当于是:

  1. sp<IBinder> b = new BpBinder(0);  

接着调用javaObjectForIBinder把这个BpBinder对象转换成一个BinderProxy对象:

  1. jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val)  
  2. {  
  3.     if (val == NULL) return NULL;  
  4.   
  5.     if (val->checkSubclass(&gBinderOffsets)) {  
  6.         // One of our own!   
  7.         jobject object = static_cast<JavaBBinder*>(val.get())->object();  
  8.         //printf("objectForBinder %p: it's our own %p!\n", val.get(), object);   
  9.         return object;  
  10.     }  
  11.   
  12.     // For the rest of the function we will hold this lock, to serialize   
  13.     // looking/creation of Java proxies for native Binder proxies.   
  14.     AutoMutex _l(mProxyLock);  
  15.   
  16.     // Someone else's...  do we know about it?   
  17.     jobject object = (jobject)val->findObject(&gBinderProxyOffsets);  
  18.     if (object != NULL) {  
  19.         jobject res = env->CallObjectMethod(object, gWeakReferenceOffsets.mGet);  
  20.         if (res != NULL) {  
  21.             LOGV("objectForBinder %p: found existing %p!\n", val.get(), res);  
  22.             return res;  
  23.         }  
  24.         LOGV("Proxy object %p of IBinder %p no longer in working set!!!", object, val.get());  
  25.         android_atomic_dec(&gNumProxyRefs);  
  26.         val->detachObject(&gBinderProxyOffsets);  
  27.         env->DeleteGlobalRef(object);  
  28.     }  
  29.   
  30.     object = env->NewObject(gBinderProxyOffsets.mClass, gBinderProxyOffsets.mConstructor);  
  31.     if (object != NULL) {  
  32.         LOGV("objectForBinder %p: created new %p!\n", val.get(), object);  
  33.         // The proxy holds a reference to the native object.   
  34.         env->SetIntField(object, gBinderProxyOffsets.mObject, (int)val.get());  
  35.         val->incStrong(object);  
  36.   
  37.         // The native object needs to hold a weak reference back to the   
  38.         // proxy, so we can retrieve the same proxy if it is still active.   
  39.         jobject refObject = env->NewGlobalRef(  
  40.                 env->GetObjectField(object, gBinderProxyOffsets.mSelf));  
  41.         val->attachObject(&gBinderProxyOffsets, refObject,  
  42.                 jnienv_to_javavm(env), proxy_cleanup);  
  43.   
  44.         // Note that a new object reference has been created.   
  45.         android_atomic_inc(&gNumProxyRefs);  
  46.         incRefsCreated(env);  
  47.     }  
  48.   
  49.     return object;  
  50. }  
linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款