diff --git a/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h b/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h index 242c9970..fe9b110e 100644 --- a/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h +++ b/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h @@ -87,7 +87,6 @@ public: void OnContinueSwitchOff(); void OnUserSwitch(); std::string GetContinueType(const std::string& bundleName); - bool CheckRegSoftbusListener(); private: void StartEvent(); @@ -119,7 +118,6 @@ private: std::mutex eventMutex_; std::mutex iconMutex_; std::shared_ptr eventHandler_; - bool hasRegSoftbusEventListener_ = false; }; } // namespace DistributedSchedule } // namespace OHOS diff --git a/services/dtbschedmgr/include/multi_user_manager.h b/services/dtbschedmgr/include/multi_user_manager.h index 01a11e8a..f11bd57d 100644 --- a/services/dtbschedmgr/include/multi_user_manager.h +++ b/services/dtbschedmgr/include/multi_user_manager.h @@ -34,8 +34,8 @@ public: void OnUserSwitched(int32_t userId); void OnUserRemoved(int32_t userId); AccountSA::OsAccountType GetOsAccountType(int32_t &accountId); - int32_t CreateNewSendMgr(); - int32_t CreateNewRecvMgr(); + int32_t CreateNewSendMgrLocked(); + int32_t CreateNewRecvMgrLocked(); std::shared_ptr GetCurrentSendMgr(); std::shared_ptr GetCurrentRecvMgr(); std::shared_ptr GetSendMgrByCallingUid(int32_t callingUid); @@ -45,6 +45,8 @@ public: int32_t GetForegroundUser(); bool IsUserForeground(int32_t userId); bool IsCallerForeground(int32_t callingUid); + bool CheckRegSoftbusListener(); + void RegisterSoftbusListener(); private: void UserSwitchedOnRegisterListenerCache(); @@ -57,6 +59,7 @@ private: std::mutex sendMutex_; std::mutex recvMutex_; std::mutex listenerMutex_; + bool hasRegSoftbusEventListener_ = false; }; } // namespace DistributedSchedule } // namespace OHOS diff --git a/services/dtbschedmgr/src/distributed_sched_service.cpp b/services/dtbschedmgr/src/distributed_sched_service.cpp index 4067877b..20fe095f 100644 --- a/services/dtbschedmgr/src/distributed_sched_service.cpp +++ b/services/dtbschedmgr/src/distributed_sched_service.cpp @@ -270,15 +270,10 @@ void DistributedSchedService::DeviceOnlineNotify(const std::string& networkId) DistributedSchedAdapter::GetInstance().DeviceOnline(networkId); #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER DistributedSchedMissionManager::GetInstance().DeviceOnlineNotify(networkId); - auto recvMgr = MultiUserManager::GetInstance().GetCurrentRecvMgr(); - if (recvMgr == nullptr) { - HILOGI("GetRecvMgr failed."); - return; - } - if (!recvMgr->CheckRegSoftbusListener() && + if (!MultiUserManager::GetInstance().CheckRegSoftbusListener() && DistributedHardware::DeviceManager::GetInstance().IsSameAccount(networkId)) { HILOGI("DMSContinueRecvMgr need init"); - recvMgr->Init(); + MultiUserManager::GetInstance().RegisterSoftbusListener(); } #endif } diff --git a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp b/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp index e5104cc5..2b42e5e7 100644 --- a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp +++ b/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp @@ -403,15 +403,10 @@ void DtbschedmgrDeviceInfoStorage::DeviceOfflineNotify(const std::string& networ void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& deviceId) { HILOGI("OnDeviceInfoChanged called"); - auto recvMgr = MultiUserManager::GetInstance().GetCurrentRecvMgr(); - if (recvMgr == nullptr) { - HILOGI("GetRecvMgr failed."); - return; - } - if (!recvMgr->CheckRegSoftbusListener() && + if (!MultiUserManager::GetInstance().CheckRegSoftbusListener() && DistributedHardware::DeviceManager::GetInstance().IsSameAccount(deviceId)) { HILOGI("DMSContinueRecvMgr need init"); - recvMgr->Init(); + MultiUserManager::GetInstance().RegisterSoftbusListener(); } } diff --git a/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp b/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp index 35539618..2bc6717f 100644 --- a/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp +++ b/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp @@ -58,7 +58,6 @@ void DMSContinueRecvMgr::Init() return; } { - hasRegSoftbusEventListener_ = true; missionDiedListener_ = new DistributedMissionDiedListener(); eventThread_ = std::thread(&DMSContinueRecvMgr::StartEvent, this); std::unique_lock lock(eventMutex_); @@ -658,10 +657,5 @@ std::string DMSContinueRecvMgr::GetContinueType(const std::string& bundleName) return iconInfo_.continueType; } - -bool DMSContinueRecvMgr::CheckRegSoftbusListener() -{ - return hasRegSoftbusEventListener_; -} } // namespace DistributedSchedule } // namespace OHOS diff --git a/services/dtbschedmgr/src/multi_user_manager.cpp b/services/dtbschedmgr/src/multi_user_manager.cpp index 99eec7ee..254128b0 100644 --- a/services/dtbschedmgr/src/multi_user_manager.cpp +++ b/services/dtbschedmgr/src/multi_user_manager.cpp @@ -44,18 +44,13 @@ void MultiUserManager::Init() HILOGI("GetSendMgr failed."); return; } - sendMgr->Init(); auto recvMgr = GetCurrentRecvMgr(); if (recvMgr == nullptr) { HILOGI("GetRecvMgr failed."); return; } - recvMgr->Init(); - std::shared_ptr missionBroadcastListener = - std::make_shared(); - int32_t ret = SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(missionBroadcastListener); - if (ret != ERR_OK) { - HILOGE("get RegisterSoftbusEventListener failed, ret: %{public}d", ret); + if (!CheckRegSoftbusListener()) { + RegisterSoftbusListener(); } HILOGI("Init end."); } @@ -130,14 +125,11 @@ void MultiUserManager::OnUserSwitched(int32_t accountId) HILOGI("GetSendMgr failed."); return; } - sendMgr->Init(); recvMgr = GetCurrentRecvMgr(); if (recvMgr == nullptr) { HILOGI("GetRecvMgr failed."); return; } - recvMgr->Init(); - if (!DataShareManager::GetInstance().IsCurrentContinueSwitchOn()) { recvMgr->OnContinueSwitchOff(); HILOGI("ICurrentContinueSwitch is off, %{public}d", DataShareManager::GetInstance() @@ -214,19 +206,21 @@ AccountSA::OsAccountType MultiUserManager::GetOsAccountType(int32_t &accountId) return type; } -int32_t MultiUserManager::CreateNewSendMgr() +int32_t MultiUserManager::CreateNewSendMgrLocked() { HILOGI("CreateNewSendMgr begin. accountId: %{public}d.", currentUserId_); auto sendMgr = std::make_shared(); + sendMgr->Init(); sendMgrMap_.emplace(currentUserId_, sendMgr); HILOGI("CreateNewSendMgr end."); return ERR_OK; } -int32_t MultiUserManager::CreateNewRecvMgr() +int32_t MultiUserManager::CreateNewRecvMgrLocked() { HILOGI("CreateNewRecvMgr begin. accountId: %{public}d.", currentUserId_); auto recvMgr = std::make_shared(); + recvMgr->Init(); recvMgrMap_.emplace(currentUserId_, recvMgr); HILOGI("CreateNewRecvMgr end."); return ERR_OK; @@ -238,7 +232,7 @@ std::shared_ptr MultiUserManager::GetCurrentSendMgr() std::lock_guard lock(sendMutex_); if (sendMgrMap_.empty() || sendMgrMap_.find(currentUserId_) == sendMgrMap_.end()) { HILOGI("sendMgr need to create."); - CreateNewSendMgr(); + CreateNewSendMgrLocked(); } auto cur = sendMgrMap_.find(currentUserId_); return cur->second; @@ -250,7 +244,7 @@ std::shared_ptr MultiUserManager::GetCurrentRecvMgr() std::lock_guard lock(recvMutex_); if (recvMgrMap_.empty() || recvMgrMap_.find(currentUserId_) == recvMgrMap_.end()) { HILOGI("recvMgr need to create."); - CreateNewRecvMgr(); + CreateNewRecvMgrLocked(); } auto cur = recvMgrMap_.find(currentUserId_); return cur->second; @@ -260,11 +254,11 @@ std::shared_ptr MultiUserManager::GetSendMgrByCallingUid(int { int32_t accountId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, accountId); - HILOGI("GetRecvMgrByCallingUid. accountId: %{public}d , callingUid: %{public}d.", accountId, callingUid); + HILOGI("GetSendMgrByCallingUid. accountId: %{public}d , callingUid: %{public}d.", accountId, callingUid); std::lock_guard lock(sendMutex_); if (sendMgrMap_.empty() || sendMgrMap_.find(accountId) == sendMgrMap_.end()) { HILOGI("sendMgr need to create."); - CreateNewSendMgr(); + CreateNewSendMgrLocked(); } auto cur = sendMgrMap_.find(accountId); return cur->second; @@ -278,7 +272,7 @@ std::shared_ptr MultiUserManager::GetRecvMgrByCallingUid(int std::lock_guard lock(recvMutex_); if (recvMgrMap_.empty() || recvMgrMap_.find(accountId) == recvMgrMap_.end()) { HILOGI("recvMgr need to create."); - CreateNewRecvMgr(); + CreateNewRecvMgrLocked(); } auto cur = recvMgrMap_.find(accountId); return cur->second; @@ -361,5 +355,23 @@ bool MultiUserManager::IsCallerForeground(int32_t callingUid) OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, accountId); return IsUserForeground(accountId); } + +void MultiUserManager::RegisterSoftbusListener() +{ + HILOGI("Register softbusListener start. accountId: %{public}d.", currentUserId_); + std::shared_ptr missionBroadcastListener = + std::make_shared(); + int32_t ret = SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(missionBroadcastListener); + if (ret != ERR_OK) { + HILOGE("get RegisterSoftbusEventListener failed, ret: %{public}d", ret); + } + hasRegSoftbusEventListener_ = true; + HILOGI("Register softbusListener end."); +} + +bool MultiUserManager::CheckRegSoftbusListener() +{ + return hasRegSoftbusEventListener_; +} } // namespace DistributedSchedule } // namespace OHOS diff --git a/services/dtbschedmgr/test/unittest/multi_user_manager_test.cpp b/services/dtbschedmgr/test/unittest/multi_user_manager_test.cpp index a6197b6a..919bd8ed 100644 --- a/services/dtbschedmgr/test/unittest/multi_user_manager_test.cpp +++ b/services/dtbschedmgr/test/unittest/multi_user_manager_test.cpp @@ -179,11 +179,8 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_CreateNewSendMgr_001, TestSize.L /** * @tc.steps: step1. test OnUserRemoved with create current user sendMgr; */ - MultiUserManager::GetInstance().Init(); - int32_t ret = MultiUserManager::GetInstance().CreateNewSendMgr(); + int32_t ret = MultiUserManager::GetInstance().CreateNewSendMgrLocked(); EXPECT_EQ(ret, ERR_OK); - - MultiUserManager::GetInstance().UnInit(); DTEST_LOG << "MultiUserManager_CreateNewSendMgr_001 end" << std::endl; } @@ -198,11 +195,8 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_CreateNewRecvMgr_001, TestSize.L /** * @tc.steps: step1. test OnUserRemoved with create current user recvMgr; */ - MultiUserManager::GetInstance().Init(); - int32_t ret = MultiUserManager::GetInstance().CreateNewRecvMgr(); + int32_t ret = MultiUserManager::GetInstance().CreateNewRecvMgrLocked(); EXPECT_EQ(ret, ERR_OK); - - MultiUserManager::GetInstance().UnInit(); DTEST_LOG << "MultiUserManager_CreateNewRecvMgr_001 end" << std::endl; } @@ -246,7 +240,7 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_GetCurrentRecvMgr_001, TestSize. int32_t accountId = 100; MultiUserManager::GetInstance().Init(); auto recvMgr = MultiUserManager::GetInstance().recvMgrMap_.find(accountId)->second; - + MultiUserManager::GetInstance().hasRegSoftbusEventListener_ = true; auto ret = MultiUserManager::GetInstance().GetCurrentRecvMgr(); EXPECT_EQ(ret, recvMgr); @@ -259,6 +253,53 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_GetCurrentRecvMgr_001, TestSize. DTEST_LOG << "MultiUserManager_GetCurrentRecvMgr_001 end" << std::endl; } +/** + * @tc.name: MultiUserManager_GetCurrentRecvMgr_002 + * @tc.desc: test GetCurrentRecvMgr + * @tc.type: FUNC + */ +HWTEST_F(MultiUserManagerTest, MultiUserManager_GetCurrentRecvMgr_002, TestSize.Level3) +{ + DTEST_LOG << "MultiUserManager_GetCurrentRecvMgr_002 start" << std::endl; + /** + * @tc.steps: step1. test OnUserRemoved with create current user recvMgr; + */ + int32_t accountId = 100; + MultiUserManager::GetInstance().Init(); + auto recvMgr = MultiUserManager::GetInstance().recvMgrMap_.find(accountId)->second; + MultiUserManager::GetInstance().hasRegSoftbusEventListener_ = false; + auto ret = MultiUserManager::GetInstance().GetCurrentRecvMgr(); + EXPECT_EQ(ret, recvMgr); + EXPECT_TRUE(MultiUserManager::GetInstance().hasRegSoftbusEventListener_); + + MultiUserManager::GetInstance().recvMgrMap_.insert({accountId, nullptr}); + ret = MultiUserManager::GetInstance().GetCurrentRecvMgr(); + EXPECT_NE(ret, nullptr); + + + MultiUserManager::GetInstance().UnInit(); + DTEST_LOG << "MultiUserManager_GetCurrentRecvMgr_002 end" << std::endl; +} + +/** + * @tc.name: MultiUserManager_CheckRegSoftbusListener_001 + * @tc.desc: test CheckRegSoftbusListener + * @tc.type: FUNC + */ +HWTEST_F(MultiUserManagerTest, MultiUserManager_CheckRegSoftbusListener_001, TestSize.Level3) +{ + DTEST_LOG << "MultiUserManager_CheckRegSoftbusListener_001 start" << std::endl; + /** + * @tc.steps: step1. test OnUserRemoved with create current user recvMgr; + */ + MultiUserManager::GetInstance().hasRegSoftbusEventListener_ = false; + EXPECT_FALSE(MultiUserManager::GetInstance().CheckRegSoftbusListener()); + MultiUserManager::GetInstance().RegisterSoftbusListener(); + EXPECT_TRUE(MultiUserManager::GetInstance().hasRegSoftbusEventListener_); + EXPECT_TRUE(MultiUserManager::GetInstance().CheckRegSoftbusListener()); + DTEST_LOG << "MultiUserManager_CheckRegSoftbusListener_001 end" << std::endl; +} + /** * @tc.name: MultiUserManager_OnRegisterOnListener_001 * @tc.desc: test OnRegisterOnListener