簡介
samgr組件是OpenHarmony的核心組件,提供OpenHarmony系統(tǒng)服務啟動、注冊、查詢等功能。
系統(tǒng)架構
圖 1 系統(tǒng)服務管理系統(tǒng)架構圖
說明
samgr服務接收到sa框架層發(fā)送的注冊消息,會在本地緩存中存入系統(tǒng)服務相關信息。
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; }
對于本地服務而言,samgr服務接收到sa框架層發(fā)送的獲取消息,會通過服務id,查找到對應服務的代理對象,然后返回給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; }
動態(tài)加載系統(tǒng)服務進程及SystemAbility, 系統(tǒng)進程無需開機啟動,而是在SystemAbility被訪問的時候按需拉起,并加載指定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的代理對象 { 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 調用samgr提供的動態(tài)加載接口LoadSystemAbility(int32_t systemAbilityId, const sptr& callback)。
// 構造步驟1的SystemAbilityLoadCallbackStub子類的實例 sptr< OnDemandLoadCallback > loadCallback_ = new OnDemandLoadCallback(); // 調用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ā)知識更新,前往[qr23.cn/AKFP8k
]可參考
說明:
1.LoadSystemAbility方法調用成功后,指定SystemAbility加載成功后會觸發(fā)回調OnLoadSystemAbilitySuccess,加載失敗觸發(fā)回調OnLoadSystemAbilityFail。
2.動態(tài)加載的進程cfg文件不能配置為開機啟動,需指定"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方法適用于動態(tài)加載場景,其他獲取SystemAbility場景建議使用CheckSystemAbility方法。
4.cfg里進程名稱需要與SA的配置json文件里進程名保持一致
審核編輯 黃宇
-
鴻蒙
+關注
關注
57文章
2345瀏覽量
42822 -
OpenHarmony
+關注
關注
25文章
3716瀏覽量
16273
發(fā)布評論請先 登錄
相關推薦
評論