簡(jiǎn)介
samgr組件是OpenHarmony的核心組件,提供OpenHarmony系統(tǒng)服務(wù)啟動(dòng)、注冊(cè)、查詢等功能。
系統(tǒng)架構(gòu)
圖 1 系統(tǒng)服務(wù)管理系統(tǒng)架構(gòu)圖
說明
samgr服務(wù)接收到sa框架層發(fā)送的注冊(cè)消息,會(huì)在本地緩存中存入系統(tǒng)服務(wù)相關(guān)信息。
int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sptr< IRemoteObject >& ability, const SAExtraProp& extraProp) { if (!CheckInputSysAbilityId(systemAbilityId) || ability == nullptr) { HILOGE("AddSystemAbilityExtra input params is invalid."); return ERR_INVALID_VALUE; } { unique_lock< shared_mutex > writeLock(abilityMapLock_); auto saSize = abilityMap_.size(); if (saSize >= MAX_SERVICES) { HILOGE("map size error, (Has been greater than %zu)", saSize); return ERR_INVALID_VALUE; } SAInfo saInfo; saInfo.remoteObj = ability; saInfo.isDistributed = extraProp.isDistributed; saInfo.capability = extraProp.capability; saInfo.permission = Str16ToStr8(extraProp.permission); abilityMap_[systemAbilityId] = std::move(saInfo); HILOGI("insert %{public}d. size : %{public}zu", systemAbilityId, abilityMap_.size()); } RemoveCheckLoadedMsg(systemAbilityId); if (abilityDeath_ != nullptr) { ability- >AddDeathRecipient(abilityDeath_); } u16string strName = Str8ToStr16(to_string(systemAbilityId)); if (extraProp.isDistributed && dBinderService_ != nullptr) { dBinderService_- >RegisterRemoteProxy(strName, systemAbilityId); HILOGD("AddSystemAbility RegisterRemoteProxy, serviceId is %{public}d", systemAbilityId); } if (systemAbilityId == SOFTBUS_SERVER_SA_ID && !isDbinderStart_) { if (dBinderService_ != nullptr && rpcCallbackImp_ != nullptr) { bool ret = dBinderService_- >StartDBinderService(rpcCallbackImp_); HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); isDbinderStart_ = true; } } SendSystemAbilityAddedMsg(systemAbilityId, ability); return ERR_OK; }
對(duì)于本地服務(wù)而言,samgr服務(wù)接收到sa框架層發(fā)送的獲取消息,會(huì)通過服務(wù)id,查找到對(duì)應(yīng)服務(wù)的代理對(duì)象,然后返回給sa框架。
sptr< IRemoteObject > SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId) { if (!CheckInputSysAbilityId(systemAbilityId)) { HILOGW("CheckSystemAbility CheckSystemAbility invalid!"); return nullptr; } shared_lock< shared_mutex > readLock(abilityMapLock_); auto iter = abilityMap_.find(systemAbilityId); if (iter != abilityMap_.end()) { HILOGI("found service : %{public}d.", systemAbilityId); return iter- >second.remoteObj; } HILOGI("NOT found service : %{public}d", systemAbilityId); return nullptr; }
動(dòng)態(tài)加載系統(tǒng)服務(wù)進(jìn)程及SystemAbility, 系統(tǒng)進(jìn)程無需開機(jī)啟動(dòng),而是在SystemAbility被訪問的時(shí)候按需拉起,并加載指定SystemAbility。
3.1 繼承SystemAbilityLoadCallbackStub類,并覆寫OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject)、OnLoadSystemAbilityFail(int32_t systemAbilityId)方法。class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub { public: void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr< IRemoteObject >& remoteObject) override; void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; }; void OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr< IRemoteObject >& remoteObject) // systemAbilityId為指定加載的SAID,remoteObject為指定systemAbility的代理對(duì)象 { cout < < "OnLoadSystemAbilitySuccess systemAbilityId:" < < systemAbilityId < < " IRemoteObject result:" < < ((remoteObject != nullptr) ? "succeed" : "failed") < < endl; } void OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) // systemAbilityId為指定加載的SAID { cout < < "OnLoadSystemAbilityFail systemAbilityId:" < < systemAbilityId < < endl; }
3.2 調(diào)用samgr提供的動(dòng)態(tài)加載接口LoadSystemAbility(int32_t systemAbilityId, const sptr& callback)。
// 構(gòu)造步驟1的SystemAbilityLoadCallbackStub子類的實(shí)例 sptr< OnDemandLoadCallback > loadCallback_ = new OnDemandLoadCallback(); // 調(diào)用LoadSystemAbility方法 sptr< ISystemAbilityManager > sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sm == nullptr) { cout < < "GetSystemAbilityManager samgr object null!" < < endl; return; } int32_t result = sm- >LoadSystemAbility(systemAbilityId, loadCallback_); if (result != ERR_OK) { cout < < "systemAbilityId:" < < systemAbilityId < < " load failed, result code:" < < result < < endl; return; }
鴻蒙開發(fā)知識(shí)更新,前往[qr23.cn/AKFP8k
]可參考
說明:
1.LoadSystemAbility方法調(diào)用成功后,指定SystemAbility加載成功后會(huì)觸發(fā)回調(diào)OnLoadSystemAbilitySuccess,加載失敗觸發(fā)回調(diào)OnLoadSystemAbilityFail。
2.動(dòng)態(tài)加載的進(jìn)程cfg文件不能配置為開機(jī)啟動(dòng),需指定"ondemand" : true, 示例如下:
{ "services" : [{ "name" : "listen_test", "path" : ["/system/bin/sa_main", "/system/profile/listen_test.json"], "ondemand" : true, "uid" : "system", "gid" : ["system", "shell"] } ] }
3.LoadSystemAbility方法適用于動(dòng)態(tài)加載場(chǎng)景,其他獲取SystemAbility場(chǎng)景建議使用CheckSystemAbility方法。
4.cfg里進(jìn)程名稱需要與SA的配置json文件里進(jìn)程名保持一致
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
57文章
2302瀏覽量
42689 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3636瀏覽量
16061
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論