From c7c2065610fe0977407e003fa4a793819d1c3afe Mon Sep 17 00:00:00 2001 From: RuiChen Date: Thu, 28 Aug 2025 21:02:57 +0800 Subject: [PATCH] add support for dh_multiuser Change-Id: Ie5cf70dd32185acc6b2afff0900879a746cfdbf9 Signed-off-by: RuiChen --- .../include/appmgr/app_mgr_interface.h | 8 ++++ services/appmgr/include/app_mgr_service.h | 9 +++++ .../appmgr/include/app_mgr_service_inner.h | 2 + services/appmgr/src/app_mgr_service.cpp | 9 +++++ services/appmgr/src/app_mgr_service_inner.cpp | 26 +++++++++++++ .../app_mgr_service_test.cpp | 38 +++++++++++++++++++ 6 files changed, 92 insertions(+) diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h index 2c19e0b08a2..668a4c0675d 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h @@ -488,6 +488,14 @@ public: */ virtual int32_t RegisterConfigurationObserver(const sptr &observer) = 0; + /** + * Register configuration observer with userId. + * + * @param observer Configuration observer. When configuration changed, observer will be called. + * @return Returns RESULT_OK on success, others on failure. + */ + virtual int32_t RegisterConfigurationObserver(const sptr &observer, int32_t userId) = 0; + /** * Unregister configuration observer. * diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index 65c785c843b..7b45be42e93 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -470,6 +470,15 @@ public: */ virtual int32_t RegisterConfigurationObserver(const sptr &observer) override; + /** + * @brief register a configuration observer bound to userId which will receive notifies when updated. + * @param observer the configuration observer to receive notify. + * @param userId the userId registered before + * + * @return Returns ERR_OK on success, others on failure. + */ + virtual int32_t RegisterConfigurationObserver(const sptr &observer, int32_t userId) override; + /** * @brief unregister a configuration observer registered before. * @param observer the configuration observer registered before. diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index 7f8bd2fa425..beb4c37105a 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -813,6 +813,8 @@ public: int32_t RegisterConfigurationObserver(const sptr& observer); + int32_t RegisterConfigurationObserver(const sptr& observer, int32_t userId); + int32_t UnregisterConfigurationObserver(const sptr& observer); /** diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index dd1cf4fe04d..c85ea03f19f 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -1157,6 +1157,15 @@ int32_t AppMgrService::RegisterConfigurationObserver(const sptrRegisterConfigurationObserver(observer); } +int32_t AppMgrService::RegisterConfigurationObserver(const sptr &observer, int32_t userId) +{ + if (!IsReady()) { + TAG_LOGE(AAFwkTag::APPMGR, "not ready"); + return ERR_INVALID_OPERATION; + } + return appMgrServiceInner_->RegisterConfigurationObserver(observer, userId); +} + int32_t AppMgrService::UnregisterConfigurationObserver(const sptr &observer) { if (!IsReady()) { diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 7058992c0f9..b1487278d67 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -6028,6 +6028,32 @@ int32_t AppMgrServiceInner::RegisterConfigurationObserver(const sptr& observer, int32_t userId) +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) { + TAG_LOGE(AAFwkTag::APPMGR, "caller not SA"); + return ERR_INVALID_VALUE; + } + + if (observer == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "observer null"); + return ERR_INVALID_VALUE; + } + std::lock_guard registerLock(configurationObserverLock_); + auto it = std::find_if(configurationObservers_.begin(), configurationObservers_.end(), + [&](const ConfigurationObserverWithUserId& item) { + return (item.observer && item.observer->AsObject() == observer->AsObject()); + }); + if (it != configurationObservers_.end()) { + TAG_LOGE(AAFwkTag::APPMGR, "observer exist"); + return ERR_INVALID_VALUE; + } + configurationObservers_.push_back( + ConfigurationObserverWithUserId { observer, userId }); + return NO_ERROR; +} + int32_t AppMgrServiceInner::UnregisterConfigurationObserver(const sptr& observer) { TAG_LOGI(AAFwkTag::APPMGR, "call"); diff --git a/test/unittest/app_mgr_service_test/app_mgr_service_test.cpp b/test/unittest/app_mgr_service_test/app_mgr_service_test.cpp index 8d9b609212c..c9545015a7d 100644 --- a/test/unittest/app_mgr_service_test/app_mgr_service_test.cpp +++ b/test/unittest/app_mgr_service_test/app_mgr_service_test.cpp @@ -992,6 +992,44 @@ HWTEST_F(AppMgrServiceTest, RegisterConfigurationObserver_002, TestSize.Level2) EXPECT_NE(res, ERR_INVALID_OPERATION); } +/* + * Feature: AppMgrService + * Function: RegisterConfigurationObserver + * SubFunction: NA + * FunctionPoints: AppMgrService RegisterConfigurationObserver + * EnvConditions: NA + * CaseDescription: Verify RegisterConfigurationObserver + */ +HWTEST_F(AppMgrServiceTest, RegisterConfigurationObserver_003, TestSize.Level2) +{ + auto appMgrService = std::make_shared(); + int32_t userId = 1; + sptr observer = nullptr; + appMgrService->SetInnerService(nullptr); + int32_t res = appMgrService->RegisterConfigurationObserver(observer, userId); + EXPECT_EQ(res, ERR_INVALID_OPERATION); +} + +/* + * Feature: AppMgrService + * Function: RegisterConfigurationObserver + * SubFunction: NA + * FunctionPoints: AppMgrService RegisterConfigurationObserver + * EnvConditions: NA + * CaseDescription: Verify RegisterConfigurationObserver + */ +HWTEST_F(AppMgrServiceTest, RegisterConfigurationObserver_004, TestSize.Level2) +{ + auto appMgrService = std::make_shared(); + int32_t userId = 1; + sptr observer = nullptr; + appMgrService->SetInnerService(std::make_shared()); + appMgrService->taskHandler_ = taskHandler_; + appMgrService->eventHandler_ = std::make_shared(taskHandler_, appMgrService->appMgrServiceInner_); + int32_t res = appMgrService->RegisterConfigurationObserver(observer, userId); + EXPECT_NE(res, ERR_INVALID_OPERATION); +} + /* * Feature: AppMgrService * Function: UnregisterConfigurationObserver -- Gitee