Init时序修改

Signed-off-by: 韦国庆 <weiguoqing2@huawei.com>
This commit is contained in:
韦国庆 2024-11-12 15:43:34 +08:00
parent aa4bb7e4c5
commit 0713d0c9d9
7 changed files with 88 additions and 50 deletions

View File

@ -87,7 +87,6 @@ public:
void OnContinueSwitchOff(); void OnContinueSwitchOff();
void OnUserSwitch(); void OnUserSwitch();
std::string GetContinueType(const std::string& bundleName); std::string GetContinueType(const std::string& bundleName);
bool CheckRegSoftbusListener();
private: private:
void StartEvent(); void StartEvent();
@ -119,7 +118,6 @@ private:
std::mutex eventMutex_; std::mutex eventMutex_;
std::mutex iconMutex_; std::mutex iconMutex_;
std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
bool hasRegSoftbusEventListener_ = false;
}; };
} // namespace DistributedSchedule } // namespace DistributedSchedule
} // namespace OHOS } // namespace OHOS

View File

@ -34,8 +34,8 @@ public:
void OnUserSwitched(int32_t userId); void OnUserSwitched(int32_t userId);
void OnUserRemoved(int32_t userId); void OnUserRemoved(int32_t userId);
AccountSA::OsAccountType GetOsAccountType(int32_t &accountId); AccountSA::OsAccountType GetOsAccountType(int32_t &accountId);
int32_t CreateNewSendMgr(); int32_t CreateNewSendMgrLocked();
int32_t CreateNewRecvMgr(); int32_t CreateNewRecvMgrLocked();
std::shared_ptr<DMSContinueSendMgr> GetCurrentSendMgr(); std::shared_ptr<DMSContinueSendMgr> GetCurrentSendMgr();
std::shared_ptr<DMSContinueRecvMgr> GetCurrentRecvMgr(); std::shared_ptr<DMSContinueRecvMgr> GetCurrentRecvMgr();
std::shared_ptr<DMSContinueSendMgr> GetSendMgrByCallingUid(int32_t callingUid); std::shared_ptr<DMSContinueSendMgr> GetSendMgrByCallingUid(int32_t callingUid);
@ -45,6 +45,8 @@ public:
int32_t GetForegroundUser(); int32_t GetForegroundUser();
bool IsUserForeground(int32_t userId); bool IsUserForeground(int32_t userId);
bool IsCallerForeground(int32_t callingUid); bool IsCallerForeground(int32_t callingUid);
bool CheckRegSoftbusListener();
void RegisterSoftbusListener();
private: private:
void UserSwitchedOnRegisterListenerCache(); void UserSwitchedOnRegisterListenerCache();
@ -57,6 +59,7 @@ private:
std::mutex sendMutex_; std::mutex sendMutex_;
std::mutex recvMutex_; std::mutex recvMutex_;
std::mutex listenerMutex_; std::mutex listenerMutex_;
bool hasRegSoftbusEventListener_ = false;
}; };
} // namespace DistributedSchedule } // namespace DistributedSchedule
} // namespace OHOS } // namespace OHOS

View File

@ -270,15 +270,10 @@ void DistributedSchedService::DeviceOnlineNotify(const std::string& networkId)
DistributedSchedAdapter::GetInstance().DeviceOnline(networkId); DistributedSchedAdapter::GetInstance().DeviceOnline(networkId);
#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
DistributedSchedMissionManager::GetInstance().DeviceOnlineNotify(networkId); DistributedSchedMissionManager::GetInstance().DeviceOnlineNotify(networkId);
auto recvMgr = MultiUserManager::GetInstance().GetCurrentRecvMgr(); if (!MultiUserManager::GetInstance().CheckRegSoftbusListener() &&
if (recvMgr == nullptr) {
HILOGI("GetRecvMgr failed.");
return;
}
if (!recvMgr->CheckRegSoftbusListener() &&
DistributedHardware::DeviceManager::GetInstance().IsSameAccount(networkId)) { DistributedHardware::DeviceManager::GetInstance().IsSameAccount(networkId)) {
HILOGI("DMSContinueRecvMgr need init"); HILOGI("DMSContinueRecvMgr need init");
recvMgr->Init(); MultiUserManager::GetInstance().RegisterSoftbusListener();
} }
#endif #endif
} }

View File

@ -403,15 +403,10 @@ void DtbschedmgrDeviceInfoStorage::DeviceOfflineNotify(const std::string& networ
void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& deviceId) void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& deviceId)
{ {
HILOGI("OnDeviceInfoChanged called"); HILOGI("OnDeviceInfoChanged called");
auto recvMgr = MultiUserManager::GetInstance().GetCurrentRecvMgr(); if (!MultiUserManager::GetInstance().CheckRegSoftbusListener() &&
if (recvMgr == nullptr) {
HILOGI("GetRecvMgr failed.");
return;
}
if (!recvMgr->CheckRegSoftbusListener() &&
DistributedHardware::DeviceManager::GetInstance().IsSameAccount(deviceId)) { DistributedHardware::DeviceManager::GetInstance().IsSameAccount(deviceId)) {
HILOGI("DMSContinueRecvMgr need init"); HILOGI("DMSContinueRecvMgr need init");
recvMgr->Init(); MultiUserManager::GetInstance().RegisterSoftbusListener();
} }
} }

View File

@ -58,7 +58,6 @@ void DMSContinueRecvMgr::Init()
return; return;
} }
{ {
hasRegSoftbusEventListener_ = true;
missionDiedListener_ = new DistributedMissionDiedListener(); missionDiedListener_ = new DistributedMissionDiedListener();
eventThread_ = std::thread(&DMSContinueRecvMgr::StartEvent, this); eventThread_ = std::thread(&DMSContinueRecvMgr::StartEvent, this);
std::unique_lock<std::mutex> lock(eventMutex_); std::unique_lock<std::mutex> lock(eventMutex_);
@ -658,10 +657,5 @@ std::string DMSContinueRecvMgr::GetContinueType(const std::string& bundleName)
return iconInfo_.continueType; return iconInfo_.continueType;
} }
bool DMSContinueRecvMgr::CheckRegSoftbusListener()
{
return hasRegSoftbusEventListener_;
}
} // namespace DistributedSchedule } // namespace DistributedSchedule
} // namespace OHOS } // namespace OHOS

View File

@ -44,18 +44,13 @@ void MultiUserManager::Init()
HILOGI("GetSendMgr failed."); HILOGI("GetSendMgr failed.");
return; return;
} }
sendMgr->Init();
auto recvMgr = GetCurrentRecvMgr(); auto recvMgr = GetCurrentRecvMgr();
if (recvMgr == nullptr) { if (recvMgr == nullptr) {
HILOGI("GetRecvMgr failed."); HILOGI("GetRecvMgr failed.");
return; return;
} }
recvMgr->Init(); if (!CheckRegSoftbusListener()) {
std::shared_ptr<SoftbusAdapterListener> missionBroadcastListener = RegisterSoftbusListener();
std::make_shared<DistributedMissionBroadcastListener>();
int32_t ret = SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(missionBroadcastListener);
if (ret != ERR_OK) {
HILOGE("get RegisterSoftbusEventListener failed, ret: %{public}d", ret);
} }
HILOGI("Init end."); HILOGI("Init end.");
} }
@ -130,14 +125,11 @@ void MultiUserManager::OnUserSwitched(int32_t accountId)
HILOGI("GetSendMgr failed."); HILOGI("GetSendMgr failed.");
return; return;
} }
sendMgr->Init();
recvMgr = GetCurrentRecvMgr(); recvMgr = GetCurrentRecvMgr();
if (recvMgr == nullptr) { if (recvMgr == nullptr) {
HILOGI("GetRecvMgr failed."); HILOGI("GetRecvMgr failed.");
return; return;
} }
recvMgr->Init();
if (!DataShareManager::GetInstance().IsCurrentContinueSwitchOn()) { if (!DataShareManager::GetInstance().IsCurrentContinueSwitchOn()) {
recvMgr->OnContinueSwitchOff(); recvMgr->OnContinueSwitchOff();
HILOGI("ICurrentContinueSwitch is off, %{public}d", DataShareManager::GetInstance() HILOGI("ICurrentContinueSwitch is off, %{public}d", DataShareManager::GetInstance()
@ -214,19 +206,21 @@ AccountSA::OsAccountType MultiUserManager::GetOsAccountType(int32_t &accountId)
return type; return type;
} }
int32_t MultiUserManager::CreateNewSendMgr() int32_t MultiUserManager::CreateNewSendMgrLocked()
{ {
HILOGI("CreateNewSendMgr begin. accountId: %{public}d.", currentUserId_); HILOGI("CreateNewSendMgr begin. accountId: %{public}d.", currentUserId_);
auto sendMgr = std::make_shared<DMSContinueSendMgr>(); auto sendMgr = std::make_shared<DMSContinueSendMgr>();
sendMgr->Init();
sendMgrMap_.emplace(currentUserId_, sendMgr); sendMgrMap_.emplace(currentUserId_, sendMgr);
HILOGI("CreateNewSendMgr end."); HILOGI("CreateNewSendMgr end.");
return ERR_OK; return ERR_OK;
} }
int32_t MultiUserManager::CreateNewRecvMgr() int32_t MultiUserManager::CreateNewRecvMgrLocked()
{ {
HILOGI("CreateNewRecvMgr begin. accountId: %{public}d.", currentUserId_); HILOGI("CreateNewRecvMgr begin. accountId: %{public}d.", currentUserId_);
auto recvMgr = std::make_shared<DMSContinueRecvMgr>(); auto recvMgr = std::make_shared<DMSContinueRecvMgr>();
recvMgr->Init();
recvMgrMap_.emplace(currentUserId_, recvMgr); recvMgrMap_.emplace(currentUserId_, recvMgr);
HILOGI("CreateNewRecvMgr end."); HILOGI("CreateNewRecvMgr end.");
return ERR_OK; return ERR_OK;
@ -238,7 +232,7 @@ std::shared_ptr<DMSContinueSendMgr> MultiUserManager::GetCurrentSendMgr()
std::lock_guard<std::mutex> lock(sendMutex_); std::lock_guard<std::mutex> lock(sendMutex_);
if (sendMgrMap_.empty() || sendMgrMap_.find(currentUserId_) == sendMgrMap_.end()) { if (sendMgrMap_.empty() || sendMgrMap_.find(currentUserId_) == sendMgrMap_.end()) {
HILOGI("sendMgr need to create."); HILOGI("sendMgr need to create.");
CreateNewSendMgr(); CreateNewSendMgrLocked();
} }
auto cur = sendMgrMap_.find(currentUserId_); auto cur = sendMgrMap_.find(currentUserId_);
return cur->second; return cur->second;
@ -250,7 +244,7 @@ std::shared_ptr<DMSContinueRecvMgr> MultiUserManager::GetCurrentRecvMgr()
std::lock_guard<std::mutex> lock(recvMutex_); std::lock_guard<std::mutex> lock(recvMutex_);
if (recvMgrMap_.empty() || recvMgrMap_.find(currentUserId_) == recvMgrMap_.end()) { if (recvMgrMap_.empty() || recvMgrMap_.find(currentUserId_) == recvMgrMap_.end()) {
HILOGI("recvMgr need to create."); HILOGI("recvMgr need to create.");
CreateNewRecvMgr(); CreateNewRecvMgrLocked();
} }
auto cur = recvMgrMap_.find(currentUserId_); auto cur = recvMgrMap_.find(currentUserId_);
return cur->second; return cur->second;
@ -260,11 +254,11 @@ std::shared_ptr<DMSContinueSendMgr> MultiUserManager::GetSendMgrByCallingUid(int
{ {
int32_t accountId = -1; int32_t accountId = -1;
OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, accountId); 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<std::mutex> lock(sendMutex_); std::lock_guard<std::mutex> lock(sendMutex_);
if (sendMgrMap_.empty() || sendMgrMap_.find(accountId) == sendMgrMap_.end()) { if (sendMgrMap_.empty() || sendMgrMap_.find(accountId) == sendMgrMap_.end()) {
HILOGI("sendMgr need to create."); HILOGI("sendMgr need to create.");
CreateNewSendMgr(); CreateNewSendMgrLocked();
} }
auto cur = sendMgrMap_.find(accountId); auto cur = sendMgrMap_.find(accountId);
return cur->second; return cur->second;
@ -278,7 +272,7 @@ std::shared_ptr<DMSContinueRecvMgr> MultiUserManager::GetRecvMgrByCallingUid(int
std::lock_guard<std::mutex> lock(recvMutex_); std::lock_guard<std::mutex> lock(recvMutex_);
if (recvMgrMap_.empty() || recvMgrMap_.find(accountId) == recvMgrMap_.end()) { if (recvMgrMap_.empty() || recvMgrMap_.find(accountId) == recvMgrMap_.end()) {
HILOGI("recvMgr need to create."); HILOGI("recvMgr need to create.");
CreateNewRecvMgr(); CreateNewRecvMgrLocked();
} }
auto cur = recvMgrMap_.find(accountId); auto cur = recvMgrMap_.find(accountId);
return cur->second; return cur->second;
@ -361,5 +355,23 @@ bool MultiUserManager::IsCallerForeground(int32_t callingUid)
OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, accountId); OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, accountId);
return IsUserForeground(accountId); return IsUserForeground(accountId);
} }
void MultiUserManager::RegisterSoftbusListener()
{
HILOGI("Register softbusListener start. accountId: %{public}d.", currentUserId_);
std::shared_ptr<SoftbusAdapterListener> missionBroadcastListener =
std::make_shared<DistributedMissionBroadcastListener>();
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 DistributedSchedule
} // namespace OHOS } // namespace OHOS

View File

@ -179,11 +179,8 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_CreateNewSendMgr_001, TestSize.L
/** /**
* @tc.steps: step1. test OnUserRemoved with create current user sendMgr; * @tc.steps: step1. test OnUserRemoved with create current user sendMgr;
*/ */
MultiUserManager::GetInstance().Init(); int32_t ret = MultiUserManager::GetInstance().CreateNewSendMgrLocked();
int32_t ret = MultiUserManager::GetInstance().CreateNewSendMgr();
EXPECT_EQ(ret, ERR_OK); EXPECT_EQ(ret, ERR_OK);
MultiUserManager::GetInstance().UnInit();
DTEST_LOG << "MultiUserManager_CreateNewSendMgr_001 end" << std::endl; 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; * @tc.steps: step1. test OnUserRemoved with create current user recvMgr;
*/ */
MultiUserManager::GetInstance().Init(); int32_t ret = MultiUserManager::GetInstance().CreateNewRecvMgrLocked();
int32_t ret = MultiUserManager::GetInstance().CreateNewRecvMgr();
EXPECT_EQ(ret, ERR_OK); EXPECT_EQ(ret, ERR_OK);
MultiUserManager::GetInstance().UnInit();
DTEST_LOG << "MultiUserManager_CreateNewRecvMgr_001 end" << std::endl; DTEST_LOG << "MultiUserManager_CreateNewRecvMgr_001 end" << std::endl;
} }
@ -246,7 +240,7 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_GetCurrentRecvMgr_001, TestSize.
int32_t accountId = 100; int32_t accountId = 100;
MultiUserManager::GetInstance().Init(); MultiUserManager::GetInstance().Init();
auto recvMgr = MultiUserManager::GetInstance().recvMgrMap_.find(accountId)->second; auto recvMgr = MultiUserManager::GetInstance().recvMgrMap_.find(accountId)->second;
MultiUserManager::GetInstance().hasRegSoftbusEventListener_ = true;
auto ret = MultiUserManager::GetInstance().GetCurrentRecvMgr(); auto ret = MultiUserManager::GetInstance().GetCurrentRecvMgr();
EXPECT_EQ(ret, recvMgr); EXPECT_EQ(ret, recvMgr);
@ -259,6 +253,53 @@ HWTEST_F(MultiUserManagerTest, MultiUserManager_GetCurrentRecvMgr_001, TestSize.
DTEST_LOG << "MultiUserManager_GetCurrentRecvMgr_001 end" << std::endl; 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.name: MultiUserManager_OnRegisterOnListener_001
* @tc.desc: test OnRegisterOnListener * @tc.desc: test OnRegisterOnListener