From 535abf867f9cb5d0591cb1a894d468f1bac40fe4 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_client.h | 8 ++++ .../include/appmgr/app_mgr_interface.h | 8 ++++ .../appmgr/app_mgr_ipc_interface_code.h | 1 + .../include/appmgr/app_mgr_proxy.h | 3 ++ .../app_manager/src/appmgr/app_mgr_client.cpp | 14 +++++++ .../app_manager/src/appmgr/app_mgr_proxy.cpp | 32 ++++++++++++++++ services/appmgr/include/app_mgr_service.h | 10 +++++ .../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 +++++++++++++ .../include/mock_app_mgr_service.h | 1 + .../include/mock_app_mgr_service.h | 1 + .../mock/include/mock_app_mgr_service.h | 1 + .../app_mgr_service_test.cpp | 38 +++++++++++++++++++ .../include/mock_app_mgr_service.h | 1 + .../mock_iapp_mgr.h | 5 +++ .../mock/include/mock_app_mgr_service.h | 1 + 17 files changed, 161 insertions(+) diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h index 911ac982581..8a0f63a122c 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h @@ -441,6 +441,14 @@ public: */ virtual AppMgrResultCode RegisterConfigurationObserver(const sptr &observer); + /** + * 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 AppMgrResultCode RegisterConfigurationObserver(const sptr &observer, int userId); + /** * Unregister configuration observer. * 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/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h index 84c34ddefb1..20fce4da930 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h @@ -136,6 +136,7 @@ enum class AppMgrInterfaceCode { PRELOAD_MODULE_FINISHED = 111, QUERY_RUNNING_SHARED_BUNDLES = 112, EXIT_MASTER_PROCESS_ROLE = 113, + REGISTER_CONFIGURATION_OBSERVER_WITH_USRID = 114, }; } // AppExecFwk } // OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h index 2e071da6023..29eb1ec27ac 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h @@ -448,6 +448,9 @@ public: virtual int32_t RegisterConfigurationObserver(const sptr &observer) override; + virtual int32_t RegisterConfigurationObserver(const sptr &observer, + int32_t userId) override; + virtual int32_t UnregisterConfigurationObserver(const sptr &observer) override; /** diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp index af1eabdb77d..9699160b979 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp @@ -908,6 +908,20 @@ AppMgrResultCode AppMgrClient::RegisterConfigurationObserver(const sptr &observer, + int32_t userId) +{ + sptr service = iface_cast(mgrHolder_->GetRemoteObject()); + if (service != nullptr) { + int32_t result = service->RegisterConfigurationObserver(observer, userId); + if (result == ERR_OK) { + return AppMgrResultCode::RESULT_OK; + } + return AppMgrResultCode::ERROR_SERVICE_NOT_READY; + } + return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED; +} + AppMgrResultCode AppMgrClient::UnregisterConfigurationObserver(const sptr &observer) { sptr service = iface_cast(mgrHolder_->GetRemoteObject()); diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp index ee5d9a12e9a..6eb6692bf43 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -1103,6 +1103,38 @@ int32_t AppMgrProxy::RegisterConfigurationObserver(const sptr& observer, int32_t userId) +{ + if (!observer) { + TAG_LOGE(AAFwkTag::APPMGR, "observer null"); + return ERR_INVALID_VALUE; + } + TAG_LOGD(AAFwkTag::APPMGR, "RegisterConfigurationObserver start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInt32(userId)) { + TAG_LOGE(AAFwkTag::APPMGR, " invilid userId"); + return ERR_INVALID_DATA; + } + + if (!WriteInterfaceToken(data)) { + return ERR_FLATTEN_OBJECT; + } + + if (!data.WriteRemoteObject(observer->AsObject())) { + TAG_LOGE(AAFwkTag::APPMGR, "observer write failed."); + return ERR_FLATTEN_OBJECT; + } + + auto error = SendRequest(AppMgrInterfaceCode::REGISTER_CONFIGURATION_OBSERVER_WITH_USRID, data, reply, option); + if (error != NO_ERROR) { + TAG_LOGE(AAFwkTag::APPMGR, "Send request error: %{public}d", error); + return error; + } + return reply.ReadInt32(); +} + int32_t AppMgrProxy::UnregisterConfigurationObserver(const sptr &observer) { TAG_LOGD(AAFwkTag::APPMGR, "UnregisterConfigurationObserver start"); diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index 65c785c843b..a836bfed63f 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -470,6 +470,16 @@ 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/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h b/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h index e6d5a1fe687..b4f91142c70 100644 --- a/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h +++ b/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h @@ -61,6 +61,7 @@ public: MOCK_METHOD1(GetConfiguration, int32_t(Configuration& config)); MOCK_METHOD2(UpdateConfiguration, int32_t(const Configuration& config, const int32_t userId)); MOCK_METHOD1(RegisterConfigurationObserver, int32_t(const sptr& observer)); + MOCK_METHOD2(RegisterConfigurationObserver, int32_t(const sptr& observer, int32_t userId)); MOCK_METHOD1(UnregisterConfigurationObserver, int32_t(const sptr& observer)); MOCK_METHOD1(GetAppRunningStateByBundleName, bool(const std::string& bundleName)); MOCK_METHOD2(NotifyLoadRepairPatch, int32_t(const std::string& bundleName, diff --git a/test/mock/services_appmgr_test/include/mock_app_mgr_service.h b/test/mock/services_appmgr_test/include/mock_app_mgr_service.h index 859875dbefa..08087efaac0 100644 --- a/test/mock/services_appmgr_test/include/mock_app_mgr_service.h +++ b/test/mock/services_appmgr_test/include/mock_app_mgr_service.h @@ -79,6 +79,7 @@ public: MOCK_METHOD3(UpdateConfigurationByBundleName, int32_t(const Configuration& config, const std::string &name, int32_t appIndex)); MOCK_METHOD1(RegisterConfigurationObserver, int32_t(const sptr& observer)); + MOCK_METHOD2(RegisterConfigurationObserver, int32_t(const sptr& observer, int32_t userId)); MOCK_METHOD1(UnregisterConfigurationObserver, int32_t(const sptr& observer)); MOCK_METHOD1(GetAppRunningStateByBundleName, bool(const std::string& bundleName)); MOCK_METHOD2(NotifyLoadRepairPatch, int32_t(const std::string& bundleName, diff --git a/test/unittest/ability_permission_util_second_test/mock/include/mock_app_mgr_service.h b/test/unittest/ability_permission_util_second_test/mock/include/mock_app_mgr_service.h index 988782453e3..2993a87b5b1 100644 --- a/test/unittest/ability_permission_util_second_test/mock/include/mock_app_mgr_service.h +++ b/test/unittest/ability_permission_util_second_test/mock/include/mock_app_mgr_service.h @@ -76,6 +76,7 @@ public: MOCK_METHOD3(UpdateConfigurationByBundleName, int32_t(const Configuration& config, const std::string &name, int32_t appIndex)); MOCK_METHOD1(RegisterConfigurationObserver, int32_t(const sptr& observer)); + MOCK_METHOD2(RegisterConfigurationObserver, int32_t(const sptr& observer, int32_t userId)); MOCK_METHOD1(UnregisterConfigurationObserver, int32_t(const sptr& observer)); MOCK_METHOD1(GetAppRunningStateByBundleName, bool(const std::string& bundleName)); MOCK_METHOD2(NotifyLoadRepairPatch, int32_t(const std::string& bundleName, 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 diff --git a/test/unittest/multi_app_utils_test/include/mock_app_mgr_service.h b/test/unittest/multi_app_utils_test/include/mock_app_mgr_service.h index c11b4ec4954..18b5c792e93 100644 --- a/test/unittest/multi_app_utils_test/include/mock_app_mgr_service.h +++ b/test/unittest/multi_app_utils_test/include/mock_app_mgr_service.h @@ -76,6 +76,7 @@ public: MOCK_METHOD3(UpdateConfigurationByBundleName, int32_t(const Configuration& config, const std::string &name, int32_t appIndex)); MOCK_METHOD1(RegisterConfigurationObserver, int32_t(const sptr& observer)); + MOCK_METHOD2(RegisterConfigurationObserver, int32_t(const sptr& observer, int32_t userId)); MOCK_METHOD1(UnregisterConfigurationObserver, int32_t(const sptr& observer)); MOCK_METHOD1(GetAppRunningStateByBundleName, bool(const std::string& bundleName)); MOCK_METHOD2(NotifyLoadRepairPatch, int32_t(const std::string& bundleName, diff --git a/test/unittest/multi_instance_utils_second_test/mock_iapp_mgr.h b/test/unittest/multi_instance_utils_second_test/mock_iapp_mgr.h index 127d92c641b..8cf53371726 100644 --- a/test/unittest/multi_instance_utils_second_test/mock_iapp_mgr.h +++ b/test/unittest/multi_instance_utils_second_test/mock_iapp_mgr.h @@ -250,6 +250,11 @@ public: return 0; } + virtual int32_t RegisterConfigurationObserver(const sptr& observer, int32_t userId) + { + return 0; + } + virtual int32_t UnregisterConfigurationObserver(const sptr& observer) { return 0; diff --git a/test/unittest/ui_ability_lifecycle_manager_third_test/mock/include/mock_app_mgr_service.h b/test/unittest/ui_ability_lifecycle_manager_third_test/mock/include/mock_app_mgr_service.h index 25a36af0fa2..aeac6bcdfac 100644 --- a/test/unittest/ui_ability_lifecycle_manager_third_test/mock/include/mock_app_mgr_service.h +++ b/test/unittest/ui_ability_lifecycle_manager_third_test/mock/include/mock_app_mgr_service.h @@ -76,6 +76,7 @@ public: MOCK_METHOD3(UpdateConfigurationByBundleName, int32_t(const Configuration& config, const std::string &name, int32_t appIndex)); MOCK_METHOD1(RegisterConfigurationObserver, int32_t(const sptr& observer)); + MOCK_METHOD2(RegisterConfigurationObserver, int32_t(const sptr& observer, int32_t userId)); MOCK_METHOD1(UnregisterConfigurationObserver, int32_t(const sptr& observer)); MOCK_METHOD1(GetAppRunningStateByBundleName, bool(const std::string& bundleName)); MOCK_METHOD2(NotifyLoadRepairPatch, int32_t(const std::string& bundleName, -- Gitee