OpenHarmony Binder调用过程

参考:

OpenHarmony之NAPI框架介绍

浅谈Openharmony系统服务框架Samgr

Android Binder 驱动框架设计与分析

写给 Android 应用工程师的 Binder 原理剖析

正文:

一. 系统服务的调用过程。

  1. Natvie 中,先通过 IPC 获取 samgr 的 Server 端的 Binder。
sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 
  1. 在 foundation/systemabilitymgr/samgr/services/samgr/native/source/service_registry.cpp ,GetSystemAbilityManager方法中,通过 IPCSkeleton 开启 IPC 通信,并指定获取 SystemAbilityManager 的 Binder
sptr<ISystemAbilityManager> SystemAbilityManagerClient::GetSystemAbilityManager()
{
    std::lock_guard<std::mutex> lock(systemAbilityManagerLock_);
    if (systemAbilityManager_ != nullptr) {
        return systemAbilityManager_;
    }
    sptr<IRemoteObject> registryObject = IPCSkeleton::GetContextObject();
    systemAbilityManager_ = iface_cast<ISystemAbilityManager>(registryObject);
    return systemAbilityManager_;
}
  1. 获取到SystemAbilityManager的 Binder 后,再通过 Binder 调用 foundation/systemabilitymgr/samgr/services/samgr/native/source/system_ability_manager.cpp 中 CheckSystemAbility 方法,通过 SystemAbilityId 获取指定的系统服务的 Binder。
sptr<IRemoteObject> SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId)                                                                                                                                    
{                                                                                                                                                                                                                        
    HILOGD("%{public}s called, systemAbilityId = %{public}d", __func__, systemAbilityId);                                                                                                                                    
    if (!CheckInputSysAbilityId(systemAbilityId)) {                                                                                                                                                                          
        HILOGW("CheckSystemAbility CheckSystemAbility invalid!");                                                                                                                                                                
    return nullptr;                                                                                                                                                                                                          
    }                                                                                                                                                                                                                        
    UpdateSaFreMap(IPCSkeleton::GetCallingUid(), systemAbilityId);                                                                                                                                                           
    shared_lock<shared_mutex> readLock(abilityMapLock_);                                                                                                                                                                     
    auto iter = abilityMap_.find(systemAbilityId);                                                                                                                                                                           
    if (iter != abilityMap_.end()) {                                                                                                                                                                                         
        HILOGD("found service : %{public}d.", systemAbilityId);                                                                                                                                                                  
        return iter->second.remoteObj;                                                                                                                                                                                           
    }                                                                                                                                                                                                                        
    HILOGW("NOT found service : %{public}d", systemAbilityId);                                                                                                                                                               
    return nullptr;                                                                                                                                                                                                          
}

在 SystemAbilityManager#CheckSystemAbility 方法里,最终是通过 systemAbilityId 从abilityMap_ 中获取已经初始化完成的系统服务 Binder。

二. 系统服务的注册&启动过程

  1. 在 foundation/systemabilitymgr/samgr/services/samgr/native/source/main.cpp 源码 main 函数中,调取 SystemAbilityManager 的 Init 方法进行初始化操作。
int main(int argc, char *argv[])                                                                                                                                                                                                        
{                                                                                                                                                                                                                                       
    HILOGI("%{public}s called, enter System Ability Manager ", __func__);                                                                                                                                                               
    Samgr::MemoryGuard cacheGuard;                                                                                                                                                                                                      
    OHOS::sptr<OHOS::SystemAbilityManager> manager = OHOS::SystemAbilityManager::GetInstance();                                                                                                                                         
    manager->Init();                                                                                                                                                                                                                    
    OHOS::sptr<OHOS::IRemoteObject> serv = manager->AsObject();                                                                                                                                                                         
        
    if (!IPCSkeleton::SetContextObject(serv)) {                                                                                                                                                                                         
        HILOGE("set context fail!"); // add log for dfx                                                                                                                                                                                 
    }                                                                                                                                                                                                                                   
    int result = SetParameter("bootevent.samgr.ready", "true");                                                                                                                                                                         
    HILOGI("set samgr ready ret : %{public}s", result == 0 ? "succeed" : "failed");                                                                                                                                                     
    manager->StartDfxTimer();                                                                                                                                                                                                           
    OHOS::IPCSkeleton::JoinWorkThread();                                                                                                                                                                                                
    return -1;                                                                                                                                                                                                                          
 }
  1. 在 foundation/systemabilitymgr/samgr/services/samgr/native/source/system_ability_manager.cpp 源码的 Init 方法,来初始化&启动各个系统服务。
void SystemAbilityManager::Init()                                                                                                                                                                                                
{                                                                                                                                                                                                                                
    abilityDeath_ = sptr<IRemoteObject::DeathRecipient>(new AbilityDeathRecipient());                                                                                                                                            
    systemProcessDeath_ = sptr<IRemoteObject::DeathRecipient>(new SystemProcessDeathRecipient());                                                                                                                                
    abilityStatusDeath_ = sptr<IRemoteObject::DeathRecipient>(new AbilityStatusDeathRecipient());                                                                                                                                
    abilityCallbackDeath_ = sptr<IRemoteObject::DeathRecipient>(new AbilityCallbackDeathRecipient());                                                                                                                            
    remoteCallbackDeath_ = sptr<IRemoteObject::DeathRecipient>(new RemoteCallbackDeathRecipient());                                                                                                                              

    rpcCallbackImp_ = make_shared<RpcCallbackImp>();                                                                                                                                                                             
    if (workHandler_ == nullptr) {                                                                                                                                                                                               
    auto runner = AppExecFwk::EventRunner::Create("workHandler");                                                                                                                                                                
    workHandler_ = make_shared<AppExecFwk::EventHandler>(runner);                                                                                                                                                                
          workHandler_->PostTask([]() { Samgr::MemoryGuard cacheGuard; });                                                                                                                                                       
    }                                                                                                                                                                                                                            
    collectManager_ = sptr<DeviceStatusCollectManager>(new DeviceStatusCollectManager());                                                                                                                                        
    abilityStateScheduler_ = std::make_shared<SystemAbilityStateScheduler>();                                                                                                                                                    
    InitSaProfile();                                                                                                                                                                                                             
    WatchDogInit();                                                                                                                                                                                                              
    reportEventTimer_ = std::make_unique<Utils::Timer>("DfxReporter");                                                                                                                                                           
    OndemandLoadForPerf();                                                                                                                                                                                                       
}
  1. 在 InitSaProfile 方法中,通过遍历 /system/profile 路径下所有的 xml / json(4.0+) 文件来初始化服务。(load)
void SystemAbilityManager::InitSaProfile()                                                                              
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值