OpenHarmony系统服务的核心组件

简介

samgr组件是OpenHarmony的核心组件,提供OpenHarmony系统服务启动、注册、查询等功能。

系统架构

图 1 系统服务管理系统架构图

img

目录

/foundation/distributedschedule
├── samgr
│   ├── bundle.json  # 部件描述及编译文件
│   ├── frameworks   # 框架实现存在目录
│   ├── interfaces   # 接口目录
│   ├── services     # 组件服务端目录
│   ├── test         # 测试代码存放目录
│   ├── utils        # 工具类目录

说明

  1. samgr服务接收到sa框架层发送的注册消息,会在本地缓存中存入系统服务相关信息。
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;
}
  1. 对于本地服务而言,samgr服务接收到sa框架层发送的获取消息,会通过服务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;
}
  1. 动态加载系统服务进程及SystemAbility, 系统进程无需开机启动,而是在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提供的动态加载接口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;
}

说明:
1.LoadSystemAbility方法调用成功后,指定SystemAbility加载成功后会触发回调OnLoadSystemAbilitySuccess,加载失败触发回调OnLoadSystemAbilityFail。
2.动态加载的进程cfg文件不能配置为开机启动,需指定"ondemand" : true, 示例如下:

{
    "services" : [{
        "name" : "listen_test",
        "path" : ["/system/bin/sa_main", "/system/profile/listen_test.xml"],
        "ondemand" : true,
        "uid" : "system",
        "gid" : ["system", "shell"]
        }
    ]
}

3.LoadSystemAbility方法适用于动态加载场景,其他获取SystemAbility场景建议使用CheckSystemAbility方法。
4.cfg里进程名称需要与SA的配置xml文件里进程名保持一致

写在最后

最后分享一份鸿蒙(HarmonyOS)开发学习指南需要的可以扫码免费领取!!!

《鸿蒙(HarmonyOS)开发学习指南》

第一章 快速入门

1、开发准备

2、构建第一个ArkTS应用(Stage模型)

3、构建第一个ArkTS应用(FA模型)

4、构建第一个JS应用(FA模型)

5、…

图片

第二章 开发基础知识

1、应用程序包基础知识

2、应用配置文件(Stage模型)

3、应用配置文件概述(FA模型)

4、…

图片

第三章 资源分类与访问

1、 资源分类与访问

2、 创建资源目录和资源文件

3、 资源访问

4、…

图片

第四章 学习ArkTs语言

1、初识ArkTS语言

2、基本语法

3、状态管理

4、其他状态管理

5、渲染控制

6、…

图片

第五章 UI开发

1.方舟开发框架(ArkUI)概述

2.基于ArkTS声明式开发范式

3.兼容JS的类Web开发范式

4…

图片

第六章 Web开发

1.Web组件概述

2.使用Web组件加载页面

3.设置基本属性和事件

4.在应用中使用前端页面JavaScript

5.ArkTS语言基础类库概述

6.并发

7…

图片

11.网络与连接

12.电话服务

13.数据管理

14.文件管理

15.后台任务管理

16.设备管理

17…

图片

第七章 应用模型

1.应用模型概述

2.Stage模型开发指导

3.FA模型开发指导

4…

  • 22
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值