参考:
正文:
一. 系统服务的调用过程。
- Natvie 中,先通过 IPC 获取 samgr 的 Server 端的 Binder。
sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
- 在 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_;
}
- 获取到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。
二. 系统服务的注册&启动过程
- 在 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;
}
- 在 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();
}
- 在 InitSaProfile 方法中,通过遍历 /system/profile 路径下所有的 xml / json(4.0+) 文件来初始化服务。(load)
void SystemAbilityManager::InitSaProfile()

7760

被折叠的 条评论
为什么被折叠?



