From 0e7b398139a5952950e3b42611438b178b3118c4 Mon Sep 17 00:00:00 2001 From: yuechunyang Date: Sat, 14 Jun 2025 16:38:22 +0800 Subject: [PATCH] Add backend component support for the multi-user function on the front end. Signed-off-by: yuechunyang --- .../include/os_account_manager_wrapper.h | 30 +++++++- .../src/os_account_manager_wrapper.cpp | 58 +++++++++++++++- .../include/ability_connect_manager.h | 8 +-- .../include/ability_manager_service.h | 7 ++ .../abilitymgr/include/sub_managers_helper.h | 5 +- .../src/ability_connect_manager.cpp | 26 +++---- .../src/ability_manager_service.cpp | 68 +++++++++++++++++-- .../abilitymgr/src/sub_managers_helper.cpp | 14 +++- .../ability_connect_manager_test.cpp | 39 ++++------- .../ability_manager_service_eighth_test.cpp | 35 ++++++++++ ...bility_manager_service_fourteenth_test.cpp | 40 +++++++++++ .../mock/include/mock_sub_managers_helper.h | 5 +- .../mock/src/mock_sub_managers_helper.cpp | 16 ++++- .../mock/include/mock_sub_managers_helper.h | 5 +- .../mock/src/mock_ability_connect_manager.cpp | 6 +- .../mock/src/mock_sub_managers_helper.cpp | 16 ++++- .../os_account_manager_wrapper_test.cpp | 59 +++++++++++++++- 17 files changed, 379 insertions(+), 58 deletions(-) diff --git a/interfaces/inner_api/deps_wrapper/include/os_account_manager_wrapper.h b/interfaces/inner_api/deps_wrapper/include/os_account_manager_wrapper.h index 826ca5b843a..faa7d725980 100644 --- a/interfaces/inner_api/deps_wrapper/include/os_account_manager_wrapper.h +++ b/interfaces/inner_api/deps_wrapper/include/os_account_manager_wrapper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -85,6 +85,34 @@ public: * @return int32_t user ID */ static int32_t GetCurrentActiveAccountId(); + + /** + * @brief Checks whether the specified OS account is currently in the foreground login state. + * + * This function determines if the OS account identified by the given userId + * is the one currently active and in the foreground on the device. + * + * @param userId The user ID of the OS account to check. + * @param isSwitchToUser Whether to check the foreground state during a user switch operation. + * If true, the check is performed in the context of switching users. + * If false (default), the check is performed in the current context. + * @param callerUid The UID of the caller process. + * Default is -1, which means the current process UID will be used. + * This parameter can be used to specify a different UID for permission or context checks. + * @return Returns true if the specified OS account is in the foreground; false otherwise. + */ + bool IsOsAccountForeground(const int32_t userId, bool isSwitchToUser = false, int32_t callerUid = -1); + + /** + * @brief Gets the local ID of the current foreground OS account. + * + * This function retrieves the local user ID of the OS account that is currently + * in the foreground (active) on the device. + * + * @param userId [out] The local ID of the current foreground OS account. + * @return error code, ERR_OK on success, others on failure. + */ + ErrCode GetForegroundOsAccountLocalId(int32_t &userId); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/deps_wrapper/src/os_account_manager_wrapper.cpp b/interfaces/inner_api/deps_wrapper/src/os_account_manager_wrapper.cpp index 388620c0931..00e3ff29657 100644 --- a/interfaces/inner_api/deps_wrapper/src/os_account_manager_wrapper.cpp +++ b/interfaces/inner_api/deps_wrapper/src/os_account_manager_wrapper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,13 +22,14 @@ namespace OHOS { namespace AppExecFwk { -#ifndef OS_ACCOUNT_PART_ENABLED namespace { +#ifndef OS_ACCOUNT_PART_ENABLED const int32_t DEFAULT_OS_ACCOUNT_ID = 0; // default id when there is no os_account part const int32_t USER_ID_U100 = 100; const int32_t UID_TRANSFORM_DIVISOR = 200000; -} #endif // OS_ACCOUNT_PART_ENABLED +constexpr int32_t ACCOUNT_MGR_SERVICE_UID = 3058; +} ErrCode OsAccountManagerWrapper::QueryActiveOsAccountIds(std::vector& ids) { @@ -127,5 +128,56 @@ int32_t OsAccountManagerWrapper::GetCurrentActiveAccountId() TAG_LOGD(AAFwkTag::DEFAULT, "accountId: %{public}d", accountIds[0]); return accountIds[0]; } + +bool OsAccountManagerWrapper::IsOsAccountForeground(const int32_t userId, bool isSwitchToUser, int32_t callerUid) +{ +#ifndef OS_ACCOUNT_PART_ENABLED + TAG_LOGD(AAFwkTag::DEFAULT, "userId: %{public}d is not in foreground accounts.", userId); + return USER_ID_U100 == userId; +#else // OS_ACCOUNT_PART_ENABLED + if (isSwitchToUser && callerUid == ACCOUNT_MGR_SERVICE_UID) { + TAG_LOGD(AAFwkTag::DEFAULT, + "userId: %{public}d is not in foreground accounts, but it is a account mgr service uid.", userId); + return true; // Account Manager Service UID is always considered as foreground + } + + std::vector accounts; + ErrCode errCode = AccountSA::OsAccountManager::GetForegroundOsAccounts(accounts); + if (errCode != ERR_OK) { + TAG_LOGE(AAFwkTag::DEFAULT, "GetForegroundOsAccounts failed, errCode: %{public}d, userId: %{public}d", + errCode, userId); + return false; + } + + for (const auto& account : accounts) { + if (account.localId == userId) { + TAG_LOGD(AAFwkTag::DEFAULT, "userId: %{public}d is in foreground accounts.", userId); + return true; + } + } + + TAG_LOGW(AAFwkTag::DEFAULT, + "userId: %{public}d is not in foreground accounts. size: %{public}zu", userId, accounts.size()); + return false; +#endif // OS_ACCOUNT_PART_ENABLED +} + +ErrCode OsAccountManagerWrapper::GetForegroundOsAccountLocalId(int32_t &userId) +{ +#ifndef OS_ACCOUNT_PART_ENABLED + userId = USER_ID_U100; + TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem, set userId: %{public}d", userId); + return ERR_OK; // No foreground account in this case +#else // OS_ACCOUNT_PART_ENABLED + auto errCode = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (errCode != ERR_OK) { + TAG_LOGE(AAFwkTag::DEFAULT, + "GetForegroundOsAccountLocalId failed, errCode: %{public}d, userId: %{public}d", errCode, userId); + return ERR_OK; + } + return errCode; +#endif // OS_ACCOUNT_PART_ENABLED +} + } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/include/ability_connect_manager.h b/services/abilitymgr/include/ability_connect_manager.h index d52b29295d9..a33b680f1af 100644 --- a/services/abilitymgr/include/ability_connect_manager.h +++ b/services/abilitymgr/include/ability_connect_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -304,7 +304,7 @@ public: * * @param abilityRecord, service ability record. */ - void OnAbilityDied(const std::shared_ptr &abilityRecord, int32_t currentUserId); + void OnAbilityDied(const std::shared_ptr &abilityRecord); /** * DisconnectBeforeCleanup. @@ -588,7 +588,7 @@ private: * * @param abilityRecord, died ability. */ - void HandleAbilityDiedTask(const std::shared_ptr &abilityRecord, int32_t currentUserId); + void HandleAbilityDiedTask(const std::shared_ptr &abilityRecord); void HandleUIExtensionDied(const std::shared_ptr &abilityRecord); @@ -681,7 +681,7 @@ private: void AddConnectObjectToMap(sptr connectObject, const ConnectListType &connectRecordList, bool updateOnly); - void KeepAbilityAlive(const std::shared_ptr &abilityRecord, int32_t currentUserId); + void KeepAbilityAlive(const std::shared_ptr &abilityRecord); void ProcessEliminateAbilityRecord(std::shared_ptr eliminateRecord); std::string GetServiceKey(const std::shared_ptr &service); diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index ec5b8768550..c4fb6d6d55c 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -1590,6 +1590,10 @@ public: */ int32_t GetUserId() const; + int32_t GetDefalutUserId() const; + + bool IsOsAccountForeground(int32_t userId) const; + virtual int32_t RegisterStatusBarDelegate(sptr delegate) override; virtual int32_t KillProcessWithPrepareTerminate(const std::vector& pids) override; @@ -2338,6 +2342,7 @@ private: std::shared_ptr GetCurrentUIAbilityManager(); std::shared_ptr GetUIAbilityManagerByUserId(int32_t userId) const; std::shared_ptr GetUIAbilityManagerByUid(int32_t uid); + ErrCode GetForegroundOsAccountLocalId(int32_t &userId) const; bool JudgeSelfCalled(const std::shared_ptr &abilityRecord); bool IsAppSelfCalled(const std::shared_ptr &abilityRecord); @@ -2828,6 +2833,8 @@ private: mutable ffrt::mutex timeoutMapLock_; std::mutex whiteListMutex_; + std::atomic isUserSwitching_ {false}; + std::mutex prepareTermiationCallbackMutex_; std::map> prepareTermiationCallbacks_; std::shared_ptr abilityEventHelper_; diff --git a/services/abilitymgr/include/sub_managers_helper.h b/services/abilitymgr/include/sub_managers_helper.h index 48f9e5fd260..c753de67f02 100644 --- a/services/abilitymgr/include/sub_managers_helper.h +++ b/services/abilitymgr/include/sub_managers_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -80,6 +80,9 @@ public: std::shared_ptr GetMissionListWrap(); std::shared_ptr CreateMissionListMgr(int32_t userId); + + bool IsOsAccountForeground(const int32_t userId, bool isSwitchToUser = false, int32_t callerUid = -1); + ErrCode GetForegroundOsAccountLocalId(int32_t &userId); private: DISALLOW_COPY_AND_MOVE(SubManagersHelper); diff --git a/services/abilitymgr/src/ability_connect_manager.cpp b/services/abilitymgr/src/ability_connect_manager.cpp index 3401c74cfde..6a9610b2ba0 100644 --- a/services/abilitymgr/src/ability_connect_manager.cpp +++ b/services/abilitymgr/src/ability_connect_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1806,7 +1806,7 @@ void AbilityConnectManager::HandleStartTimeoutTask(const std::shared_ptr::GetInstance()->AttachTimeOut(abilityRecord->GetToken()); if (abilityRecord->IsSceneBoard()) { - if (DelayedSingleton::GetInstance()->GetUserId() == userId_) { + if (DelayedSingleton::GetInstance()->IsOsAccountForeground(userId_)) { RestartAbility(abilityRecord, userId_); } return; @@ -2296,7 +2296,7 @@ void AbilityConnectManager::OnLoadAbilityFailed(std::shared_ptr a HandleStartTimeoutTask(abilityRecord); } -void AbilityConnectManager::OnAbilityDied(const std::shared_ptr &abilityRecord, int32_t currentUserId) +void AbilityConnectManager::OnAbilityDied(const std::shared_ptr &abilityRecord) { CHECK_POINTER(abilityRecord); TAG_LOGI(AAFwkTag::SERVICE_EXT, "on ability died: %{public}s", abilityRecord->GetURI().c_str()); @@ -2318,10 +2318,10 @@ void AbilityConnectManager::OnAbilityDied(const std::shared_ptr & taskHandler_->CancelTask("terminate_" + std::to_string(abilityRecord->GetAbilityRecordId())); } if (taskHandler_) { - auto task = [abilityRecord, connectManagerWeak = weak_from_this(), currentUserId]() { + auto task = [abilityRecord, connectManagerWeak = weak_from_this()]() { auto connectManager = connectManagerWeak.lock(); CHECK_POINTER(connectManager); - connectManager->HandleAbilityDiedTask(abilityRecord, currentUserId); + connectManager->HandleAbilityDiedTask(abilityRecord); }; taskHandler_->SubmitTask(task, TASK_ON_ABILITY_DIED); } @@ -2395,7 +2395,7 @@ void AbilityConnectManager::CleanActivatingTimeoutAbility(std::shared_ptrIsSceneBoard() || - DelayedSingleton::GetInstance()->GetUserId() == userId_) { + DelayedSingleton::GetInstance()->IsOsAccountForeground(userId_)) { RestartAbility(abilityRecord, userId_); } } @@ -2430,7 +2430,7 @@ void AbilityConnectManager::ClearPreloadUIExtensionRecord(const std::shared_ptr< uiExtensionAbilityRecordMgr_->RemovePreloadUIExtensionRecordById(extensionRecordMapKey, extensionRecordId); } -void AbilityConnectManager::KeepAbilityAlive(const std::shared_ptr &abilityRecord, int32_t currentUserId) +void AbilityConnectManager::KeepAbilityAlive(const std::shared_ptr &abilityRecord) { CHECK_POINTER(abilityRecord); auto abilityInfo = abilityRecord->GetAbilityInfo(); @@ -2440,14 +2440,16 @@ void AbilityConnectManager::KeepAbilityAlive(const std::shared_ptrIsSceneBoard()) && token != nullptr) { IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton::GetInstance()->ClearProcessByToken( token->AsObject())); - if (abilityRecord->IsSceneBoard() && currentUserId != userId_) { + if (abilityRecord->IsSceneBoard() && + !DelayedSingleton::GetInstance()->IsOsAccountForeground(userId_)) { TAG_LOGI(AAFwkTag::ABILITYMGR, "not current user's SCB, clear user and not restart"); KillProcessesByUserId(); return; } } - if (userId_ != USER_ID_NO_HEAD && userId_ != U1_USER_ID && userId_ != currentUserId) { + if (userId_ != USER_ID_NO_HEAD && + !DelayedSingleton::GetInstance()->IsOsAccountForeground(userId_)) { TAG_LOGI(AAFwkTag::SERVICE_EXT, "Not current user's ability"); return; } @@ -2473,7 +2475,7 @@ void AbilityConnectManager::KeepAbilityAlive(const std::shared_ptr &abilityRecord, int32_t currentUserId) + const std::shared_ptr &abilityRecord) { TAG_LOGD(AAFwkTag::SERVICE_EXT, "called"); std::lock_guard guard(serialMutex_); @@ -2574,7 +2576,7 @@ void AbilityConnectManager::HandleAbilityDiedTask( } if (IsAbilityNeedKeepAlive(abilityRecord)) { - KeepAbilityAlive(abilityRecord, currentUserId); + KeepAbilityAlive(abilityRecord); } else { HandleNotifyAssertFaultDialogDied(abilityRecord); } diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 741a79b5be6..65608b4f3ef 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -243,6 +243,21 @@ constexpr int32_t U1_USER_ID = 1; constexpr const char* APPSPAWN_STARTED = "startup.service.ctl.appspawn.pid"; constexpr const char* APP_LINKING_ONLY = "appLinkingOnly"; +class UserSwitchGuard +{ +public: + explicit UserSwitchGuard(std::atomic &flag) : isUserSwitching_(flag) + { + isUserSwitching_.store(true); + } + ~UserSwitchGuard() + { + isUserSwitching_.store(false); + } +private: + std::atomic &isUserSwitching_; +}; + void SendAbilityEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo) { ffrt::submit([eventName, type, eventInfo]() { @@ -6826,6 +6841,18 @@ int32_t AbilityManagerService::GetUserId() const } return U0_USER_ID; } + +int32_t AbilityManagerService::GetDefalutUserId() const +{ + int32_t validUserId = U0_USER_ID; + auto errCode = GetForegroundOsAccountLocalId(validUserId); + if (errCode != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "GetForegroundOsAccountLocalId failed, errCode: %{public}d", errCode); + return U0_USER_ID; + } + return validUserId; +} + #ifndef DISABLE_LAUNCHER int AbilityManagerService::StartHighestPriorityAbility(int32_t userId, bool isBoot, bool isAppRecovery) { @@ -7144,7 +7171,7 @@ void AbilityManagerService::OnAbilityDied(std::shared_ptr ability auto connectManager = GetConnectManagerByToken(abilityRecord->GetToken()); if (connectManager) { - connectManager->OnAbilityDied(abilityRecord, GetUserId()); + connectManager->OnAbilityDied(abilityRecord); return; } else { TAG_LOGW(AAFwkTag::ABILITYMGR, "connectManager not found"); @@ -7601,7 +7628,25 @@ std::unordered_map> AbilityManagerSe std::shared_ptr AbilityManagerService::GetCurrentConnectManager() { CHECK_POINTER_AND_RETURN(subManagersHelper_, nullptr); - return subManagersHelper_->GetCurrentConnectManager(); + int32_t userId = IPCSkeleton::GetCallingUid() / BASE_USER_RANGE; + if (userId == U0_USER_ID || userId == U1_USER_ID) { + auto errCode = GetForegroundOsAccountLocalId(userId); + if (errCode != ERR_OK) { + TAG_LOGW(AAFwkTag::ABILITYMGR, + "GetForegroundOsAccountLocalId failed, errCode: %{public}d, userid: %{public}d", errCode, userId); + return subManagersHelper_->GetCurrentConnectManager(); + } + TAG_LOGD(AAFwkTag::ABILITYMGR, "no head call, connectManager userId is %{public}d", userId); + return subManagersHelper_->GetConnectManagerByUserId(userId); + } + + if (!IsOsAccountForeground(userId)) { + TAG_LOGW(AAFwkTag::ABILITYMGR, "non-front desk user call, connectManager userId is %{public}d", userId); + return subManagersHelper_->GetCurrentConnectManager(); + } + + TAG_LOGD(AAFwkTag::ABILITYMGR, "front desk user call, connectManager userId is %{public}d", userId); + return subManagersHelper_->GetConnectManagerByUserId(userId); } std::shared_ptr AbilityManagerService::GetConnectManagerByUserId(int32_t userId) @@ -7690,6 +7735,18 @@ std::shared_ptr AbilityManagerService::GetUIAbilityMa return subManagersHelper_->GetUIAbilityManagerByUid(uid); } +bool AbilityManagerService::IsOsAccountForeground(int32_t userId) const +{ + CHECK_POINTER_AND_RETURN(subManagersHelper_, false); + return subManagersHelper_->IsOsAccountForeground(userId, isUserSwitching_.load(), IPCSkeleton::GetCallingUid()); +} + +ErrCode AbilityManagerService::GetForegroundOsAccountLocalId(int32_t &userId) const +{ + CHECK_POINTER_AND_RETURN(subManagersHelper_, ERR_NULL_OBJECT); + return subManagersHelper_->GetForegroundOsAccountLocalId(userId); +} + void AbilityManagerService::StartResidentApps(int32_t userId) { TAG_LOGI(AAFwkTag::ABILITYMGR, "StartResidentApps %{public}d", userId); @@ -8286,6 +8343,7 @@ int AbilityManagerService::JudgeAbilityVisibleControl(const AppExecFwk::AbilityI int AbilityManagerService::StartUser(int userId, sptr callback, bool isAppRecovery) { TAG_LOGI(AAFwkTag::ABILITYMGR, "startUser in service:%{public}d", userId); + UserSwitchGuard switchGuard(isUserSwitching_); if (callback == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "startUser callback is nullptr"); return INVALID_PARAMETERS_ERR; @@ -9092,7 +9150,7 @@ int32_t AbilityManagerService::GetValidUserId(const int32_t userId) TAG_LOGD(AAFwkTag::ABILITYMGR, "validUserId = %{public}d, CallingUid = %{public}d.", validUserId, IPCSkeleton::GetCallingUid()); if (validUserId == U0_USER_ID || validUserId == U1_USER_ID) { - validUserId = GetUserId(); + validUserId = GetDefalutUserId(); } } return validUserId; @@ -9378,7 +9436,9 @@ bool AbilityManagerService::JudgeMultiUserConcurrency(const int32_t userId) // Only non-concurrent mode is supported bool concurrencyMode = CONCURRENCY_MODE_FALSE; if (!concurrencyMode) { - return (userId == GetUserId()); + auto callerUserId = IPCSkeleton::GetCallingUid() / BASE_USER_RANGE; + return (userId == callerUserId || U0_USER_ID == callerUserId || U1_USER_ID == callerUserId) && + IsOsAccountForeground(userId); } return true; diff --git a/services/abilitymgr/src/sub_managers_helper.cpp b/services/abilitymgr/src/sub_managers_helper.cpp index ca4759384ad..9771e7fd35a 100644 --- a/services/abilitymgr/src/sub_managers_helper.cpp +++ b/services/abilitymgr/src/sub_managers_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -539,5 +539,17 @@ std::shared_ptr SubManagersHelper::CreateMissionLis return nullptr; } + +bool SubManagersHelper::IsOsAccountForeground(const int32_t userId, bool isSwitchToUser, int32_t callerUid) +{ + return DelayedSingleton::GetInstance()->IsOsAccountForeground( + userId, isSwitchToUser, callerUid); +} + +ErrCode SubManagersHelper::GetForegroundOsAccountLocalId(int32_t &userId) +{ + return + DelayedSingleton::GetInstance()->GetForegroundOsAccountLocalId(userId); +} } // namespace AAFwk } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp index 72de2ade6be..cf256beaf11 100644 --- a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp +++ b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1243,13 +1243,12 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_024, TestSize.Level1) auto abilityRecord = serviceMap.at(elementNameUri); auto token = abilityRecord->GetToken(); - int userId = 0; - auto task = [abilityRecord, connectManager = ConnectManager(), userId]() { - connectManager->HandleAbilityDiedTask(abilityRecord, userId); + auto task = [abilityRecord, connectManager = ConnectManager()]() { + connectManager->HandleAbilityDiedTask(abilityRecord); }; EXPECT_CALL(*taskHandler_, SubmitTaskInner(_, _)).WillRepeatedly(DoAll(SetArgReferee<0>(task), testing::Invoke(taskHandler_.get(), &MockTaskHandlerWrap::MockTaskHandler))); - ConnectManager()->OnAbilityDied(abilityRecord, 0); + ConnectManager()->OnAbilityDied(abilityRecord); auto list = abilityRecord->GetConnectRecordList(); EXPECT_EQ(static_cast(list.size()), 0); @@ -1258,12 +1257,12 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_024, TestSize.Level1) serviceMap = ConnectManager()->GetServiceMap(); auto abilityRecord1 = serviceMap.at(elementNameUri1); auto token1 = abilityRecord1->GetToken(); - auto task1 = [abilityRecord1, connectManager = ConnectManager(), userId]() { - connectManager->HandleAbilityDiedTask(abilityRecord1, userId); + auto task1 = [abilityRecord1, connectManager = ConnectManager()]() { + connectManager->HandleAbilityDiedTask(abilityRecord1); }; EXPECT_CALL(*taskHandler_, SubmitTaskInner(_, _)).WillRepeatedly(DoAll(SetArgReferee<0>(task1), testing::Invoke(taskHandler_.get(), &MockTaskHandlerWrap::MockTaskHandler))); - ConnectManager()->OnAbilityDied(abilityRecord1, 0); + ConnectManager()->OnAbilityDied(abilityRecord1); auto list1 = abilityRecord1->GetConnectRecordList(); EXPECT_EQ(static_cast(list1.size()), 0); } @@ -2093,12 +2092,11 @@ HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAbilityDied_001, TestSize. std::shared_ptr connectManager = std::make_shared(0); ASSERT_NE(connectManager, nullptr); std::shared_ptr abilityRecord = serviceRecord_; - int32_t currentUserId = 0; abilityRecord->abilityInfo_.type = AbilityType::PAGE; connectManager->SetEventHandler(nullptr); - connectManager->OnAbilityDied(abilityRecord, currentUserId); + connectManager->OnAbilityDied(abilityRecord); abilityRecord->abilityInfo_.type = AbilityType::EXTENSION; - connectManager->OnAbilityDied(abilityRecord, currentUserId); + connectManager->OnAbilityDied(abilityRecord); } /* @@ -2154,9 +2152,8 @@ HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleAbilityDiedTask_001, T std::shared_ptr connectManager = std::make_shared(0); ASSERT_NE(connectManager, nullptr); std::shared_ptr abilityRecord = serviceRecord_; - int32_t currentUserId = 0; connectManager->serviceMap_.clear(); - connectManager->HandleAbilityDiedTask(abilityRecord, currentUserId); + connectManager->HandleAbilityDiedTask(abilityRecord); } /* @@ -2367,8 +2364,6 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_001, TestSize.Level1) ConnectManager()->SetTaskHandler(TaskHandler()); ConnectManager()->SetEventHandler(EventHandler()); - int userId = 0; - auto result = ConnectManager()->StartAbility(abilityRequest_); EXPECT_EQ(OHOS::ERR_OK, result); @@ -2378,12 +2373,12 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_001, TestSize.Level1) EXPECT_EQ(static_cast(ConnectManager()->GetServiceMap().size()), 1); // HandleTerminate - auto task = [service, connectManager = ConnectManager(), userId]() { - connectManager->HandleAbilityDiedTask(service, userId); + auto task = [service, connectManager = ConnectManager()]() { + connectManager->HandleAbilityDiedTask(service); }; EXPECT_CALL(*taskHandler_, SubmitTaskInner(_, _)).WillRepeatedly(DoAll(SetArgReferee<0>(task), testing::Invoke(taskHandler_.get(), &MockTaskHandlerWrap::MockTaskHandler))); - ConnectManager()->OnAbilityDied(service, userId); + ConnectManager()->OnAbilityDied(service); EXPECT_EQ(static_cast(ConnectManager()->GetServiceMap().size()), 0); } @@ -2401,8 +2396,6 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_002, TestSize.Level1) ConnectManager()->SetTaskHandler(TaskHandler()); ConnectManager()->SetEventHandler(EventHandler()); - int userId = 0; - auto result = ConnectManager()->StartAbility(abilityRequest2_); EXPECT_EQ(OHOS::ERR_OK, result); WaitUntilTaskDone(TaskHandler()); @@ -2413,7 +2406,7 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_002, TestSize.Level1) EXPECT_EQ(static_cast(ConnectManager()->GetServiceMap().size()), 1); // HandleTerminate - ConnectManager()->HandleAbilityDiedTask(service, userId); + ConnectManager()->HandleAbilityDiedTask(service); EXPECT_EQ(static_cast(ConnectManager()->GetServiceMap().size()), 0); } @@ -2431,8 +2424,6 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_003, TestSize.Level1) ConnectManager()->SetTaskHandler(TaskHandler()); ConnectManager()->SetEventHandler(EventHandler()); - int userId = 0; - auto result = ConnectManager()->StartAbility(abilityRequest2_); EXPECT_EQ(OHOS::ERR_OK, result); WaitUntilTaskDone(TaskHandler()); @@ -2446,7 +2437,7 @@ HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_003, TestSize.Level1) service->SetRestartTime(AbilityUtil::SystemTimeMillis() + 1000); // HandleTerminate - ConnectManager()->HandleAbilityDiedTask(service, userId); + ConnectManager()->HandleAbilityDiedTask(service); EXPECT_EQ(static_cast(ConnectManager()->GetServiceMap().size()), 0); } diff --git a/test/unittest/ability_manager_service_eighth_test/ability_manager_service_eighth_test.cpp b/test/unittest/ability_manager_service_eighth_test/ability_manager_service_eighth_test.cpp index 3984f0a987a..d158b047387 100644 --- a/test/unittest/ability_manager_service_eighth_test/ability_manager_service_eighth_test.cpp +++ b/test/unittest/ability_manager_service_eighth_test/ability_manager_service_eighth_test.cpp @@ -587,5 +587,40 @@ HWTEST_F(AbilityManagerServiceEighthTest, TerminateUIServiceExtensionAbility_002 EXPECT_NE(result, ERR_OK); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceEighthTest TerminateUIServiceExtensionAbility_002 end"); } + +/* + * Feature: AbilityManagerService + * Function: JudgeMultiUserConcurrency + * SubFunction: NA + * FunctionPoints: AbilityManagerService JudgeMultiUserConcurrency + */ +HWTEST_F(AbilityManagerServiceEighthTest, JudgeMultiUserConcurrency_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceEighthTest JudgeMultiUserConcurrency_001 start"); + const auto u0UserId = 0; + const auto u1UserId = 1; + auto abilityMs_ = std::make_shared(); + EXPECT_TRUE(abilityMs_->JudgeMultiUserConcurrency(u0UserId)); + EXPECT_TRUE(abilityMs_->JudgeMultiUserConcurrency(u1UserId)); + + auto subHelper = abilityMs_->subManagersHelper_; + abilityMs_->subManagersHelper_ = std::make_shared(nullptr, nullptr); + auto u100UserId = 100; + EXPECT_EQ(abilityMs_->GetForegroundOsAccountLocalId(u100UserId), ERR_OK); + const auto invalidUserId = 9956; + IPCSkeleton::SetCallingUid(BASE_USER_RANGE * invalidUserId); + EXPECT_FALSE(abilityMs_->JudgeMultiUserConcurrency(u100UserId)); + + IPCSkeleton::SetCallingUid(BASE_USER_RANGE * u100UserId); + EXPECT_TRUE(abilityMs_->JudgeMultiUserConcurrency(u100UserId)); + + IPCSkeleton::SetCallingUid(BASE_USER_RANGE * u1UserId); + EXPECT_TRUE(abilityMs_->JudgeMultiUserConcurrency(u100UserId)); + + IPCSkeleton::SetCallingUid(BASE_USER_RANGE * u0UserId); + EXPECT_TRUE(abilityMs_->JudgeMultiUserConcurrency(u100UserId)); + abilityMs_->subManagersHelper_ = subHelper; + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceEighthTest JudgeMultiUserConcurrency_001 end"); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp b/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp index 790e525d64b..bbc669dc24a 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp +++ b/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp @@ -1236,5 +1236,45 @@ HWTEST_F(AbilityManagerServiceFourteenthTest, CheckUIExtensionIsFocused_001, Tes EXPECT_EQ(retCode, CHECK_PERMISSION_FAILED); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest CheckUIExtensionIsFocused_001 end"); } + +/* + * Feature: AbilityManagerService + * Name: IsOsAccountForeground_001 + * Function: IsOsAccountForeground + * SubFunction: NA + * FunctionPoints: AbilityManagerService IsOsAccountForeground + */ +HWTEST_F(AbilityManagerServiceFourteenthTest, IsOsAccountForeground_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest IsOsAccountForeground_001 start"); + auto abilityMs_ = std::make_shared(); + EXPECT_NE(abilityMs_, nullptr); + auto mockSubManagersHelper = std::make_shared(nullptr, nullptr); + EXPECT_NE(mockSubManagersHelper, nullptr); + abilityMs_->subManagersHelper_ = mockSubManagersHelper; + auto userId = 100; + EXPECT_TRUE(abilityMs_->IsOsAccountForeground(userId)); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest IsOsAccountForeground_001 end"); +} + +/* + * Feature: AbilityManagerService + * Name: GetForegroundOsAccountLocalId_001 + * Function: GetForegroundOsAccountLocalId + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetForegroundOsAccountLocalId + */ +HWTEST_F(AbilityManagerServiceFourteenthTest, GetForegroundOsAccountLocalId_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest GetForegroundOsAccountLocalId_001 start"); + auto abilityMs_ = std::make_shared(); + EXPECT_NE(abilityMs_, nullptr); + auto mockSubManagersHelper = std::make_shared(nullptr, nullptr); + EXPECT_NE(mockSubManagersHelper, nullptr); + abilityMs_->subManagersHelper_ = mockSubManagersHelper; + auto userId = 100; + EXPECT_EQ(abilityMs_->GetForegroundOsAccountLocalId(userId), ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest GetForegroundOsAccountLocalId_001 end"); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_sub_managers_helper.h b/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_sub_managers_helper.h index 48f9e5fd260..c753de67f02 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_sub_managers_helper.h +++ b/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_sub_managers_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -80,6 +80,9 @@ public: std::shared_ptr GetMissionListWrap(); std::shared_ptr CreateMissionListMgr(int32_t userId); + + bool IsOsAccountForeground(const int32_t userId, bool isSwitchToUser = false, int32_t callerUid = -1); + ErrCode GetForegroundOsAccountLocalId(int32_t &userId); private: DISALLOW_COPY_AND_MOVE(SubManagersHelper); diff --git a/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_sub_managers_helper.cpp b/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_sub_managers_helper.cpp index 079d6cd7fcb..343f6ba7851 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_sub_managers_helper.cpp +++ b/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_sub_managers_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,6 +19,8 @@ namespace OHOS { namespace AAFwk { +constexpr const int32_t U100_USER_ID = 100; + SubManagersHelper::SubManagersHelper( std::shared_ptr taskHandler, std::shared_ptr eventHandler) : taskHandler_(taskHandler), eventHandler_(eventHandler) {} @@ -202,5 +204,17 @@ std::shared_ptr SubManagersHelper::CreateMissionLis { return nullptr; } + +bool SubManagersHelper::IsOsAccountForeground(const int32_t userId, bool isSwitchToUser, int32_t callerUid) +{ + return userId == U100_USER_ID; // Mocking a user ID for testing purposes +} + +ErrCode SubManagersHelper::GetForegroundOsAccountLocalId(int32_t &userId) +{ + userId = U100_USER_ID; // Mocking a user ID for testing purposes + return ERR_OK; +} + } // namespace AAFwk } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_sub_managers_helper.h b/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_sub_managers_helper.h index 48f9e5fd260..c753de67f02 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_sub_managers_helper.h +++ b/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_sub_managers_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -80,6 +80,9 @@ public: std::shared_ptr GetMissionListWrap(); std::shared_ptr CreateMissionListMgr(int32_t userId); + + bool IsOsAccountForeground(const int32_t userId, bool isSwitchToUser = false, int32_t callerUid = -1); + ErrCode GetForegroundOsAccountLocalId(int32_t &userId); private: DISALLOW_COPY_AND_MOVE(SubManagersHelper); diff --git a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ability_connect_manager.cpp b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ability_connect_manager.cpp index c206a7fe0c0..2c1c2cab4e9 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ability_connect_manager.cpp +++ b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ability_connect_manager.cpp @@ -465,7 +465,7 @@ void AbilityConnectManager::OnLoadAbilityFailed(std::shared_ptr a { } -void AbilityConnectManager::OnAbilityDied(const std::shared_ptr &abilityRecord, int32_t currentUserId) +void AbilityConnectManager::OnAbilityDied(const std::shared_ptr &abilityRecord) { } @@ -490,7 +490,7 @@ void AbilityConnectManager::ClearPreloadUIExtensionRecord(const std::shared_ptr< { } -void AbilityConnectManager::KeepAbilityAlive(const std::shared_ptr &abilityRecord, int32_t currentUserId) +void AbilityConnectManager::KeepAbilityAlive(const std::shared_ptr &abilityRecord) { } @@ -505,7 +505,7 @@ void AbilityConnectManager::DisconnectBeforeCleanup() } void AbilityConnectManager::HandleAbilityDiedTask( - const std::shared_ptr &abilityRecord, int32_t currentUserId) + const std::shared_ptr &abilityRecord) { } diff --git a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_sub_managers_helper.cpp b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_sub_managers_helper.cpp index 079d6cd7fcb..343f6ba7851 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_sub_managers_helper.cpp +++ b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_sub_managers_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,6 +19,8 @@ namespace OHOS { namespace AAFwk { +constexpr const int32_t U100_USER_ID = 100; + SubManagersHelper::SubManagersHelper( std::shared_ptr taskHandler, std::shared_ptr eventHandler) : taskHandler_(taskHandler), eventHandler_(eventHandler) {} @@ -202,5 +204,17 @@ std::shared_ptr SubManagersHelper::CreateMissionLis { return nullptr; } + +bool SubManagersHelper::IsOsAccountForeground(const int32_t userId, bool isSwitchToUser, int32_t callerUid) +{ + return userId == U100_USER_ID; // Mocking a user ID for testing purposes +} + +ErrCode SubManagersHelper::GetForegroundOsAccountLocalId(int32_t &userId) +{ + userId = U100_USER_ID; // Mocking a user ID for testing purposes + return ERR_OK; +} + } // namespace AAFwk } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/os_account_manager_wrapper_test/os_account_manager_wrapper_test.cpp b/test/unittest/os_account_manager_wrapper_test/os_account_manager_wrapper_test.cpp index 77569fbf529..094f7cada1b 100644 --- a/test/unittest/os_account_manager_wrapper_test/os_account_manager_wrapper_test.cpp +++ b/test/unittest/os_account_manager_wrapper_test/os_account_manager_wrapper_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -126,5 +126,62 @@ HWTEST_F(OsAccountManagerWrapperTest, GetCurrentActiveAccountId_0100, TestSize.L int ret = DelayedSingleton::GetInstance()->GetCurrentActiveAccountId(); EXPECT_EQ(ret, 100); } + +/** + * @tc.name: IsOsAccountForeground_0100 + * @tc.desc: Get current accountId. + * @tc.type: FUNC + */ +HWTEST_F(OsAccountManagerWrapperTest, IsOsAccountForeground_0100, TestSize.Level1) +{ + bool isSwitchToUser = true; + int32_t accountMgrServiceUid = 3058; + auto ret = DelayedSingleton::GetInstance()->IsOsAccountForeground( + UID, isSwitchToUser, accountMgrServiceUid); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: IsOsAccountForeground_0200 + * @tc.desc: Get current accountId. + * @tc.type: FUNC + */ +HWTEST_F(OsAccountManagerWrapperTest, IsOsAccountForeground_0200, TestSize.Level1) +{ + bool isSwitchToUser = true; + int32_t accountMgrServiceUid = 30580; + auto ret = DelayedSingleton::GetInstance()->IsOsAccountForeground( + UID, isSwitchToUser, accountMgrServiceUid); + EXPECT_FALSE(ret); +} + +/** + * @tc.name: IsOsAccountForeground_0300 + * @tc.desc: Get current accountId. + * @tc.type: FUNC + */ +HWTEST_F(OsAccountManagerWrapperTest, IsOsAccountForeground_0300, TestSize.Level1) +{ + bool isSwitchToUser = false; + auto ret = DelayedSingleton::GetInstance()->IsOsAccountForeground( + UID, isSwitchToUser); + EXPECT_FALSE(ret); +} + +/** + * @tc.name: IsOsAccountForeground_0400 + * @tc.desc: Get current accountId. + * @tc.type: FUNC + */ +HWTEST_F(OsAccountManagerWrapperTest, IsOsAccountForeground_0400, TestSize.Level1) +{ + bool isSwitchToUser = false; + auto userId = UID; + auto errCode = DelayedSingleton::GetInstance()->GetForegroundOsAccountLocalId(userId); + EXPECT_EQ(errCode, ERR_OK); + auto ret = DelayedSingleton::GetInstance()->IsOsAccountForeground( + userId, isSwitchToUser); + EXPECT_TRUE(ret); +} } // namespace AAFwk } // namespace OHOS -- Gitee