From 3d9b0a7ddcf36ceca9eae3a845e1ff6594bc0341 Mon Sep 17 00:00:00 2001 From: q30043944 Date: Tue, 22 Apr 2025 09:29:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: q30043944 --- .../include/deviceprofile_connector.h | 11 +- .../src/deviceprofile_connector.cpp | 67 +++- .../include/device_manager_service_impl.h | 2 +- .../src/device_manager_service_impl.cpp | 10 +- .../src/device_manager_service_impl_lite.cpp | 3 +- .../service/include/device_manager_service.h | 24 +- .../include/idevice_manager_service_impl.h | 2 +- .../relationshipsyncmgr/dm_comm_tool.h | 21 +- .../relationship_sync_mgr.h | 3 +- .../service/src/device_manager_service.cpp | 354 ++++++++++++++++-- .../src/relationshipsyncmgr/dm_comm_tool.cpp | 222 ++++++++++- .../UTTest_dm_deviceprofile_connector.cpp | 8 +- ...Test_dm_deviceprofile_connector_second.cpp | 7 +- .../UTTest_device_manager_service_impl.cpp | 2 +- ...Test_device_manager_service_impl_first.cpp | 4 +- 15 files changed, 672 insertions(+), 68 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 0279e5f44..375f02943 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -130,6 +130,8 @@ public: virtual int32_t HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid) = 0; virtual int32_t HandleUserStop(int32_t stopUserId, const std::string &localUdid, const std::vector &acceptEventUdids) = 0; + virtual int32_t HandleUserUnlocked(const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) = 0; }; class DeviceProfileConnector : public IDeviceProfileConnector { @@ -221,7 +223,8 @@ public: const std::string &localUdid, const std::string &peerUdid); EXPORT void UpdateACL(std::string &localUdid, const std::vector &localUserIds, const std::string &remoteUdid, - const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds); + const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds, + DmOfflineParam &offlineParam); EXPORT std::multimap GetDevIdAndUserIdByActHash( const std::string &localUdid, const std::string &peerUdid, int32_t peerUserId, const std::string &peerAccountHash); @@ -276,6 +279,9 @@ public: DmOfflineParam &offlineParam); void DeleteCacheAcl(std::vector delAclIdVec, std::vector &profiles); + EXPORT int32_t HandleUserUnlocked(const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string requestDeviceId, @@ -307,7 +313,8 @@ private: std::vector GetAclProfileByUserId(const std::string &localUdid, int32_t userId, const std::string &remoteUdid); void DeleteSigTrustACL(DistributedDeviceProfile::AccessControlProfile profile, const std::string &remoteUdid, - const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds); + const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds, + DmOfflineParam &offlineParam); void UpdatePeerUserId(DistributedDeviceProfile::AccessControlProfile profile, std::string &localUdid, const std::vector &localUserIds, const std::string &remoteUdid, const std::vector &remoteFrontUserIds); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 056624e8e..3fcd86b6b 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -2223,21 +2223,23 @@ EXPORT std::map DeviceProfileConnector::GetUserIdAndBindLevel( EXPORT void DeviceProfileConnector::UpdateACL(std::string &localUdid, const std::vector &localUserIds, const std::string &remoteUdid, - const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds) + const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds, + DmOfflineParam &offlineParam) { LOGI("localUdid %{public}s, remoteUdid %{public}s.", GetAnonyString(localUdid).c_str(), GetAnonyString(remoteUdid).c_str()); std::vector profiles = GetAllAclIncludeLnnAcl(); for (auto item : profiles) { // deleta signal trust acl. - DeleteSigTrustACL(item, remoteUdid, remoteFrontUserIds, remoteBackUserIds); + DeleteSigTrustACL(item, remoteUdid, remoteFrontUserIds, remoteBackUserIds, offlineParam); // update identical account userId. UpdatePeerUserId(item, localUdid, localUserIds, remoteUdid, remoteFrontUserIds); } } void DeviceProfileConnector::DeleteSigTrustACL(AccessControlProfile profile, const std::string &remoteUdid, - const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds) + const std::vector &remoteFrontUserIds, const std::vector &remoteBackUserIds, + DmOfflineParam &offlineParam) { LOGI("start."); std::string accesserUdid = profile.GetAccesser().GetAccesserDeviceId(); @@ -2247,13 +2249,13 @@ void DeviceProfileConnector::DeleteSigTrustACL(AccessControlProfile profile, con if (accesserUdid == remoteUdid && accesserUserid != 0 && accesserUserid != -1 && find(remoteFrontUserIds.begin(), remoteFrontUserIds.end(), accesserUserid) == remoteFrontUserIds.end() && find(remoteBackUserIds.begin(), remoteBackUserIds.end(), accesserUserid) == remoteBackUserIds.end()) { - DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(profile.GetAccessControlId()); + CacheAcerAclId(profile, offlineParam); return; } if (accesseeUdid == remoteUdid && accesseeUserid != 0 && accesseeUserid != -1 && find(remoteFrontUserIds.begin(), remoteFrontUserIds.end(), accesseeUserid) == remoteFrontUserIds.end() && find(remoteBackUserIds.begin(), remoteBackUserIds.end(), accesseeUserid) == remoteBackUserIds.end()) { - DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(profile.GetAccessControlId()); + CacheAceeAclId(profile, offlineParam); return; } } @@ -2582,11 +2584,11 @@ bool DeviceProfileConnector::CheckAclStatusNotMatch(const DistributedDeviceProfi if ((profile.GetAccesser().GetAccesserDeviceId() == localUdid && (find(backgroundUserIds.begin(), backgroundUserIds.end(), profile.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end()) && - (profile.GetStatus() == ACTIVE || profile.GetBindType() != DM_IDENTICAL_ACCOUNT)) || + profile.GetStatus() == ACTIVE) || (profile.GetAccessee().GetAccesseeDeviceId() == localUdid && (find(backgroundUserIds.begin(), backgroundUserIds.end(), profile.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()) && - (profile.GetStatus() == ACTIVE || profile.GetBindType() != DM_IDENTICAL_ACCOUNT))) { + profile.GetStatus() == ACTIVE)) { return true; } if ((profile.GetAccesser().GetAccesserDeviceId() == localUdid && @@ -2613,6 +2615,47 @@ bool DeviceProfileConnector::CheckAclStatusAndForegroundNotMatch(const std::stri return false; } +EXPORT int32_t DeviceProfileConnector::HandleUserUnlocked( + const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) +{ + LOGI("HandleUserUnlocked start"); + if (deviceVec.empty()) { + LOGI("no remote device."); + return DM_OK; + } + std::vector profiles = GetAllAclIncludeLnnAcl(); + std::vector activeProfiles; + std::vector inActiveProfiles; + for (auto &item : profiles) { + if (std::find(deviceVec.begin(), deviceVec.end(), item.GetTrustDeviceId()) == deviceVec.end()) { + continue; + } + if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && + (find(backgroundUserIds.begin(), backgroundUserIds.end(), + item.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE) || + (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + (find(backgroundUserIds.begin(), backgroundUserIds.end(), + item.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE)) { + item.SetStatus(INACTIVE); + inActiveProfiles.push_back(item); + continue; + } + if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && + (find(foregroundUserIds.begin(), foregroundUserIds.end(), + item.GetAccesser().GetAccesserUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE) || ( + item.GetAccessee().GetAccesseeDeviceId() == localUdid && + (find(foregroundUserIds.begin(), foregroundUserIds.end(), + item.GetAccessee().GetAccesseeUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE)) { + item.SetStatus(ACTIVE); + activeProfiles.push_back(item); + continue; + } + } + HandleUserSwitched(activeProfiles, inActiveProfiles); + return DM_OK; +} + EXPORT int32_t DeviceProfileConnector::HandleUserSwitched( const std::string &localUdid, const std::vector &deviceVec, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) @@ -2631,14 +2674,12 @@ EXPORT int32_t DeviceProfileConnector::HandleUserSwitched( } if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && (find(backgroundUserIds.begin(), backgroundUserIds.end(), - item.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end())) || + item.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE) || (item.GetAccessee().GetAccesseeDeviceId() == localUdid && (find(backgroundUserIds.begin(), backgroundUserIds.end(), - item.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()))) { - if (item.GetStatus() == ACTIVE) { - item.SetStatus(INACTIVE); - inActiveProfiles.push_back(item); - } + item.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE)) { + item.SetStatus(INACTIVE); + inActiveProfiles.push_back(item); continue; } if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index aee405d18..0de9f6600 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -156,7 +156,7 @@ public: const std::string remoteUdid, int32_t remoteUserId, std::string &aclList); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); + const std::vector &backgroundUserIds, const std::string &remoteUdid); void HandleUserSwitched(const std::vector &deviceVec, int32_t currentUserId, int32_t beforeUserId); std::multimap GetDeviceIdAndUserId(int32_t localUserId); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 3755199f8..36d0f75d5 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -1946,7 +1946,7 @@ void DeviceManagerServiceImpl::HandleServiceUnBindEvent(int32_t userId, const st } void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) + const std::vector &backgroundUserIds, const std::string &remoteUdid) { LOGI("remote udid: %{public}s, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s", GetAnonyString(remoteUdid).c_str(), GetIntegerList(foregroundUserIds).c_str(), @@ -1962,11 +1962,11 @@ void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector LOGE("Get foreground userids failed, ret: %{public}d", ret); return; } - if (isCheckUserStatus) { - MultipleUserConnector::ClearLockedUser(localUserIds); - } + MultipleUserConnector::ClearLockedUser(localUserIds); + DmOfflineParam offlineParam; DeviceProfileConnector::GetInstance().UpdateACL(localUdid, localUserIds, remoteUdid, - rmtFrontUserIdsTemp, rmtBackUserIdsTemp); + rmtFrontUserIdsTemp, rmtBackUserIdsTemp, offlineParam); + DeleteSkCredAndAcl(offlineParam); DeviceProfileConnector::GetInstance().HandleSyncBackgroundUserIdEvent(rmtBackUserIdsTemp, remoteUdid, localUserIds, localUdid); DeviceProfileConnector::GetInstance().HandleSyncForegroundUserIdEvent(rmtFrontUserIdsTemp, remoteUdid, diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 98fb8feea..165fc45a6 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -566,12 +566,11 @@ int32_t DeviceManagerServiceImpl::ProcessAppUnintall(const std::string &appId, i } void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) + const std::vector &backgroundUserIds, const std::string &remoteUdid) { (void)foregroundUserIds; (void)backgroundUserIds; (void)remoteUdid; - (void)isCheckUserStatus; return; } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 40571c958..d2bfb904e 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -241,7 +241,10 @@ public: int32_t GetDeviceNetworkIdList(const std::string &pkgName, const NetworkIdQueryFilter &queryFilter, std::vector &networkIds); void ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, int32_t userId); - + void ProcessSyncUserUnlocked(const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds, const std::string &remoteUdid); + void DelTimerAndEventTypeSyncUserIds(const std::string &remoteUdid); + void DelTimerAndEventTypeSyncUserUnlocked(const std::string &remoteUdid); private: bool IsDMServiceImplReady(); bool IsDMImplSoLoaded(); @@ -271,6 +274,9 @@ private: const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, bool isNeedResponse); void SendUserRemovedBroadCast(const std::vector &peerUdids, int32_t userId); + void SendUserUnlockedBroadCast(const std::vector &peerUdids, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, + bool isNeedResponse); /** * @brief parse dsoftbus checksum msg * @@ -333,7 +339,7 @@ private: void UpdateAclAndDeleteGroup(const std::string &localUdid, const std::vector &deviceVec, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); void HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId); - void HandleUserStopEvent(int32_t stopUserId); + void HandleUserStopEvent(int32_t currentUserId, int32_t stopUserId); void DivideNotifyMethod(const std::vector &peerUdids, std::vector &bleUdids, std::map &wifiDevices); void NotifyRemoteLocalUserStop(const std::string &localUdid, @@ -342,6 +348,20 @@ private: void HandleUserStopBroadCast(int32_t stopUserId, const std::string &remoteUdid); void NotifyRemoteLocalUserStopByWifi(const std::string &localUdid, const std::map &wifiDevices, int32_t stopUserId); + void HandleUserUnlocked(int32_t currentUserId, int32_t beforeUserId); + void NotifyRemoteLocalUserUnlocked(const std::string &localUdid, const std::vector &peerUdids, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + void HandleUserUnlockedBroadCast(const std::vector &remoteUserIdInfos, const std::string &remoteUdid, + bool isNeedResponse); + void HandleUserUnlockedTimeout(const std::string &localUdid, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds, const std::string &udid); + void UpdateAclUserUnlocked(const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + void NotifyRemoteLocalUserUnlockedByWifi(const std::string &localUdid, + const std::map &wifiDevices, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds); + int32_t SendUserUnlockedByWifi(const std::string &networkId, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void SubscribePublishCommonEvent(); void QueryDependsSwitchState(); diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 3a45e993f..f7e02e7cb 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -253,7 +253,7 @@ public: const std::string remoteUdid, int32_t remoteUserId, std::string &aclList) = 0; virtual int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId) = 0; virtual void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) = 0; + const std::vector &backgroundUserIds, const std::string &remoteUdid) = 0; virtual void HandleRemoteUserRemoved(int32_t userId, const std::string &remoteUdid) = 0; virtual std::map GetDeviceIdAndBindLevel(int32_t userId) = 0; virtual std::multimap GetDeviceIdAndUserId(int32_t localUserId) = 0; diff --git a/services/service/include/relationshipsyncmgr/dm_comm_tool.h b/services/service/include/relationshipsyncmgr/dm_comm_tool.h index d60b00071..b04c7d0f6 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.h @@ -16,11 +16,17 @@ #ifndef OHOS_DM_COMM_TOOL_H #define OHOS_DM_COMM_TOOL_H +#include +#include +#include #include "dm_transport.h" #include "dm_transport_msg.h" +#include "ffrt.h" + namespace OHOS { namespace DistributedHardware { +using EventCallback = std::function; class DMCommTool : public std::enable_shared_from_this { public: @@ -41,7 +47,8 @@ public: void ProcessReceiveUserStopEvent(const std::shared_ptr commMsg); void RspUserStop(const std::string rmtNetworkId, int32_t socketId, int32_t stopUserId); void ProcessResponseUserStopEvent(const std::shared_ptr commMsg); - + int32_t SendUserUnlocked(const std::string rmtNetworkId, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); class DMCommToolEventHandler : public AppExecFwk::EventHandler { public: DMCommToolEventHandler(const std::shared_ptr runner, @@ -58,9 +65,21 @@ public: void ProcessResponseUserIdsEvent(const std::shared_ptr commMsg); int32_t SendLogoutAccountInfo(const std::string &rmtNetworkId, const std::string &accountId, int32_t userId); void ProcessReceiveLogoutEvent(const std::shared_ptr commMsg); + void ProcessReceiveUserUnLockedEvent(const std::shared_ptr commMsg); + void RspUserUnLocked(const std::string rmtNetworkId, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds, int32_t socketId); + void ProcessResponseUserUnLockedEvent(const std::shared_ptr commMsg); + + int32_t StartCommonEvent(std::string commonEventType, int32_t currentUserId, int32_t beforeUserId, int32_t timeOut, + EventCallback eventCallback); + int32_t DeleteCommonEvent(std::string commonEventType); + private: std::shared_ptr dmTransportPtr_; std::shared_ptr eventHandler_; + mutable std::mutex eventMutex_; + std::shared_ptr eventQueue_; + std::unordered_map eventVec_ = {}; }; } // DistributedHardware } // OHOS diff --git a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h index cc6409ce1..4d77ab302 100644 --- a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h +++ b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h @@ -30,7 +30,8 @@ enum class RelationShipChangeType : uint32_t { APP_UNINSTALL = 5, SYNC_USERID = 6, STOP_USER = 7, - TYPE_MAX = 8 + UNLOCKED_USER = 8, + TYPE_MAX = 9 }; struct UserIdInfo { diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 5921e0a3b..541f5a35c 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -81,8 +81,10 @@ namespace { const std::string DHARD_WARE_PKG_NAME = "ohos.dhardware"; const std::string USERID_CHECKSUM_ISCHANGE_KEY = "ischange"; constexpr const char* USER_SWITCH_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:userSwitchByWifi"; + constexpr const char* USER_UNLOCKED_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:userUnlockedByWifi"; constexpr const char* USER_STOP_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:userStopByWifi"; const int32_t USER_SWITCH_BY_WIFI_TIMEOUT_S = 2; + const int32_t EVENT_TIMEOUT_S = 3; } DeviceManagerService::~DeviceManagerService() @@ -1889,7 +1891,13 @@ EXPORT void DeviceManagerService::AccountCommonEventCallback( LOGI("CommonEventType: %{public}s, currentUserId: %{public}d, beforeUserId: %{public}d", commonEventType.c_str(), currentUserId, beforeUserId); if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - HandleUserSwitchedEvent(currentUserId, beforeUserId); + DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); + CheckRegisterInfoWithWise(currentUserId); + MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); + DMCommTool::GetInstance()->StartCommonEvent(commonEventType, + [this, currentUserId, beforeUserId] () { + DeviceManagerService::HandleUserSwitchedEvent(currentUserId, beforeUserId); + }); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { DeviceNameManager::GetInstance().InitDeviceNameWhenLogin(); MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); @@ -1911,22 +1919,75 @@ EXPORT void DeviceManagerService::AccountCommonEventCallback( } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) { DeviceNameManager::GetInstance().InitDeviceNameWhenNickChange(); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_STOPPED && IsPC()) { - HandleUserStopEvent(beforeUserId); + DMCommTool::GetInstance()->StartCommonEvent(commonEventType, + [this, currentUserId, beforeUserId] () { + DeviceManagerService::HandleUserStopEvent(currentUserId, beforeUserId); + }); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) { DeviceNameManager::GetInstance().AccountSysReady(beforeUserId); - if (IsPC()) { - HandleUserSwitched(); - if (IsDMServiceAdapterResidentLoad()) { - dmServiceImplExtResident_->AccountUserSwitched(currentUserId, - MultipleUserConnector::GetOhosAccountId()); - } - } + DMCommTool::GetInstance()->StartCommonEvent(commonEventType, + [this, currentUserId, beforeUserId] () { + DeviceManagerService::HandleUserUnlocked(currentUserId, beforeUserId); + }); } else { LOGE("Invalied account common event."); } return; } +void DeviceManagerService::HandleUserUnlocked(int32_t currentUserId, int32_t beforeUserId) +{ + LOGI("HandleUserUnlocked start"); + (void)currentUserId; + std::vector foregroundUserVec; + int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); + std::vector backgroundUserVec; + int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); + if (retFront != DM_OK || retBack != DM_OK || foregroundUserVec.empty()) { + LOGE("Get userids failed, retFront: %{public}d, retBack: %{public}d, foreground user num: %{public}d", + retFront, retBack, static_cast(foregroundUserVec.size())); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); + return; + } + if (find(foregroundUserVec.begin(), foregroundUserVec.end(), beforeUserId) == foregroundUserVec.end()) { + LOGE("foregroundUserVec not contain beforeUserId"); + return; + } + char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localUdidTemp); + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); + return; + } + if (!discoveryMgr_->GetCommonDependencyObj()->CheckAclStatusAndForegroundNotMatch(localUdid, + foregroundUserVec, backgroundUserVec)) { + LOGI("no unreasonable data."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); + return; + } + std::map curUserDeviceMap = + discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(foregroundUserVec, localUdid); + std::map preUserDeviceMap = + discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(backgroundUserVec, localUdid); + std::vector peerUdids; + for (const auto &item : curUserDeviceMap) { + peerUdids.push_back(item.first); + } + for (const auto &item : preUserDeviceMap) { + if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) { + peerUdids.push_back(item.first); + } + } + if (peerUdids.empty()) { + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); + return; + } + NotifyRemoteLocalUserUnlocked(localUdid, peerUdids, foregroundUserVec, backgroundUserVec); +} + void DeviceManagerService::HandleUserSwitched() { LOGI("onStart, HandleUserSwitched."); @@ -1937,6 +1998,13 @@ void DeviceManagerService::HandleUserSwitched() if (retFront != DM_OK || retBack != DM_OK || foregroundUserVec.empty()) { LOGE("Get userids failed, retFront: %{public}d, retBack: %{public}d, foreground user num: %{public}d", retFront, retBack, static_cast(foregroundUserVec.size())); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + return; + } + MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec); + if (foregroundUserVec.empty()) { + LOGE("foreground User not lock."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; @@ -1945,11 +2013,13 @@ void DeviceManagerService::HandleUserSwitched() CHECK_NULL_VOID(discoveryMgr_); if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } if (!discoveryMgr_->GetCommonDependencyObj()->CheckAclStatusAndForegroundNotMatch(localUdid, foregroundUserVec, backgroundUserVec)) { LOGI("no unreasonable data."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } std::map curUserDeviceMap = @@ -1966,12 +2036,61 @@ void DeviceManagerService::HandleUserSwitched() } } if (peerUdids.empty()) { + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } NotifyRemoteLocalUserSwitch(localUdid, peerUdids, foregroundUserVec, backgroundUserVec); } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +void DeviceManagerService::NotifyRemoteLocalUserUnlocked(const std::string &localUdid, + const std::vector &peerUdids, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + LOGI("NotifyRemoteLocalUserUnlocked, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s", + GetIntegerList(foregroundUserIds).c_str(), GetIntegerList(backgroundUserIds).c_str()); + if (peerUdids.empty()) { + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); + return; + } + if (softbusListener_ == nullptr) { + UpdateAclUserUnlocked(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); + LOGE("softbusListener_ is null"); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); + return; + } + std::vector bleUdids; + std::map wifiDevices; + for (const auto &udid : peerUdids) { + std::string netWorkId = ""; + SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId); + if (netWorkId.empty()) { + LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str()); + bleUdids.push_back(udid); + continue; + } + int32_t networkType = 0; + int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType); + if (ret != DM_OK || networkType <= 0) { + LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str()); + bleUdids.push_back(udid); + continue; + } + if ((static_cast(networkType) & USERID_SYNC_DISCOVERY_TYPE_BLE_MASK) != 0x0) { + bleUdids.push_back(udid); + } else { + wifiDevices.insert(std::pair(udid, netWorkId)); + } + } + if (!bleUdids.empty()) { + UpdateAclUserUnlocked(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); + SendUserUnlockedBroadCast(bleUdids, foregroundUserIds, backgroundUserIds, true); + } + if (!wifiDevices.empty()) { + NotifyRemoteLocalUserUnlockedByWifi(localUdid, wifiDevices, foregroundUserIds, backgroundUserIds); + } +} + void DeviceManagerService::NotifyRemoteLocalUserSwitch(const std::string &localUdid, const std::vector &peerUdids, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) @@ -1979,11 +2098,13 @@ void DeviceManagerService::NotifyRemoteLocalUserSwitch(const std::string &localU LOGI("onstart UserSwitch, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s", GetIntegerList(foregroundUserIds).c_str(), GetIntegerList(backgroundUserIds).c_str()); if (peerUdids.empty()) { + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } if (softbusListener_ == nullptr) { UpdateAclAndDeleteGroup(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); LOGE("softbusListener_ is null"); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } std::vector bleUdids; @@ -2018,6 +2139,60 @@ void DeviceManagerService::NotifyRemoteLocalUserSwitch(const std::string &localU } } +void DeviceManagerService::NotifyRemoteLocalUserUnlockedByWifi(const std::string &localUdid, + const std::map &wifiDevices, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + for (const auto &it : wifiDevices) { + int32_t result = SendUserUnlockedByWifi(it.second, foregroundUserIds, backgroundUserIds); + if (result != DM_OK) { + LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); + std::vector updateUdids; + updateUdids.push_back(it.first); + UpdateAclUserUnlocked(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); + continue; + } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + std::string udid = it.first; + timer_->StartTimer(std::string(USER_UNLOCKED_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), + USER_SWITCH_BY_WIFI_TIMEOUT_S, + [this, localUdid, foregroundUserIds, backgroundUserIds, udid] (std::string name) { + DeviceManagerService::HandleUserUnlockedTimeout(localUdid, foregroundUserIds, backgroundUserIds, udid); + }); + } +} + +void DeviceManagerService::HandleUserUnlockedTimeout(const std::string &localUdid, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, + const std::string &udid) +{ + LOGI("start udid: %{public}s", GetAnonyString(udid).c_str()); + std::vector updateUdids; + updateUdids.push_back(udid); + UpdateAclUserUnlocked(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); +} + +void DeviceManagerService::ProcessSyncUserUnlocked(const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds, const std::string &remoteUdid) +{ + LOGI("process sync foregroundUserIds: %{public}s, backgroundUserIds: %{public}s, remote udid: %{public}s", + GetIntegerList(foregroundUserIds).c_str(), GetIntegerList(backgroundUserIds).c_str(), + GetAnonyString(remoteUdid).c_str()); + + if (softbusListener_ != nullptr) { + softbusListener_->SetForegroundUserIdsToDSoftBus(remoteUdid, foregroundUserIds); + } + if (timer_ != nullptr) { + timer_->DeleteTimer(std::string(USER_UNLOCKED_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid)); + } + if (IsDMServiceImplReady()) { + dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); + } +} + void DeviceManagerService::NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, const std::map &wifiDevices, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) @@ -2051,6 +2226,7 @@ void DeviceManagerService::HandleUserSwitchTimeout(const std::string &localUdid, std::vector updateUdids; updateUdids.push_back(udid); UpdateAclAndDeleteGroup(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); } void DeviceManagerService::UpdateAclAndDeleteGroup(const std::string &localUdid, @@ -2065,6 +2241,19 @@ void DeviceManagerService::UpdateAclAndDeleteGroup(const std::string &localUdid, discoveryMgr_->GetCommonDependencyObj()->HandleUserSwitched(localUdid, deviceVec, foregroundUserIds, backgroundUserIds); } + +void DeviceManagerService::UpdateAclUserUnlocked(const std::string &localUdid, + const std::vector &deviceVec, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + return; + } + discoveryMgr_->GetCommonDependencyObj()->HandleUserUnlocked(localUdid, deviceVec, + foregroundUserIds, backgroundUserIds); +} #endif void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string &accountId, @@ -2106,8 +2295,14 @@ void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string void DeviceManagerService::HandleUserSwitched(int32_t curUserId, int32_t preUserId) { LOGI("currentUserId: %{public}d. previousUserId: %{public}d", curUserId, preUserId); + if (!MultipleUserConnector::IsUserUnlocked(curUserId)) { + LOGE("curUserId has not unlocked"); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + return; + } if (!IsDMServiceImplReady()) { LOGE("Init impl failed."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } std::map curUserDeviceMap; @@ -2124,6 +2319,7 @@ void DeviceManagerService::HandleUserSwitched(int32_t curUserId, int32_t preUser } } if (peerUdids.empty()) { + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } std::vector foregroundUserVec; @@ -2133,6 +2329,12 @@ void DeviceManagerService::HandleUserSwitched(int32_t curUserId, int32_t preUser if (retFront != DM_OK || retBack != DM_OK || foregroundUserVec.empty()) { LOGE("Get userids failed, retFront: %{public}d, retBack: %{public}d, foreground user num: %{public}d", retFront, retBack, static_cast(foregroundUserVec.size())); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + return; + } + if (find(foregroundUserVec.begin(), foregroundUserVec.end(), curUserId) == foregroundUserVec.end()) { + LOGE("foregroundUserVec has not contain curUserId"); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserVec, backgroundUserVec); @@ -2208,7 +2410,28 @@ void DeviceManagerService::SendUserIdsBroadCast(const std::vector & softbusListener_->SendAclChangedBroadcast(broadCastMsg); } -void DeviceManagerService::HandleUserIdsBroadCast(const std::vector &remoteUserIdInfos, +void DeviceManagerService::SendUserUnlockedBroadCast(const std::vector &peerUdids, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, bool isNeedResponse) +{ + LOGI("peerUdids: %{public}s, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s, isNeedRsp: %{public}s", + GetAnonyStringList(peerUdids).c_str(), GetIntegerList(foregroundUserIds).c_str(), + GetIntegerList(backgroundUserIds).c_str(), isNeedResponse ? "true" : "false"); + RelationShipChangeMsg msg; + msg.type = RelationShipChangeType::UNLOCKED_USER; + msg.peerUdids = peerUdids; + msg.syncUserIdFlag = isNeedResponse; + for (const auto &userId : foregroundUserIds) { + msg.userIdInfos.push_back({ true, static_cast(userId) }); + } + for (auto const &userId : backgroundUserIds) { + msg.userIdInfos.push_back({ false, static_cast(userId) }); + } + std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); + CHECK_NULL_VOID(softbusListener_); + softbusListener_->SendAclChangedBroadcast(broadCastMsg); +} + +void DeviceManagerService::HandleUserUnlockedBroadCast(const std::vector &remoteUserIdInfos, const std::string &remoteUdid, bool isNeedResponse) { LOGI("rmtUdid: %{public}s, rmtUserIds: %{public}s, isNeedResponse: %{public}s,", @@ -2219,9 +2442,54 @@ void DeviceManagerService::HandleUserIdsBroadCast(const std::vector std::vector backgroundUserVec; int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); - if (IsPC()) { - MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec); + MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec); + if (retFront != DM_OK || retBack != DM_OK) { + LOGE("Get userid failed, retFront: %{public}d, retBack: %{public}d, frontUserNum:%{public}d," + "backUserNum: %{public}d", retFront, retBack, static_cast(foregroundUserVec.size()), + static_cast(backgroundUserVec.size())); + } else { + LOGE("Send back local frontuserids: %{public}s, backuserids: %{public}s", + GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str()); + std::vector remoteUdids = { remoteUdid }; + SendUserUnlockedBroadCast(remoteUdids, foregroundUserVec, backgroundUserVec, false); } + } + + std::vector foregroundUserIdInfos; + std::vector backgroundUserIdInfos; + GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos); + LOGI("process foreground and background userids"); + // Notify received remote foreground userids to dsoftbus + std::vector foregroundUserIds; + for (const auto &u : foregroundUserIdInfos) { + foregroundUserIds.push_back(static_cast(u.userId)); + } + std::vector backgroundUserIds; + for (const auto &u : backgroundUserIdInfos) { + backgroundUserIds.push_back(static_cast(u.userId)); + } + if (softbusListener_ != nullptr) { + softbusListener_->SetForegroundUserIdsToDSoftBus(remoteUdid, foregroundUserIds); + } + + if (IsDMServiceImplReady()) { + dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); + } + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); +} + +void DeviceManagerService::HandleUserIdsBroadCast(const std::vector &remoteUserIdInfos, + const std::string &remoteUdid, bool isNeedResponse) +{ + LOGI("rmtUdid: %{public}s, rmtUserIds: %{public}s, isNeedResponse: %{public}s,", + GetAnonyString(remoteUdid).c_str(), GetUserIdInfoList(remoteUserIdInfos).c_str(), + isNeedResponse ? "true" : "false"); + if (isNeedResponse) { + std::vector foregroundUserVec; + std::vector backgroundUserVec; + int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); + int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); + MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec); if (retFront != DM_OK || retBack!= DM_OK) { LOGE("Get userid failed, retFront: %{public}d, retBack: %{public}d, frontUserNum:%{public}d," "backUserNum: %{public}d", retFront, retBack, static_cast(foregroundUserVec.size()), @@ -2252,8 +2520,9 @@ void DeviceManagerService::HandleUserIdsBroadCast(const std::vector } if (IsDMServiceImplReady()) { - dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, IsPC()); + dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); } + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); } void DeviceManagerService::ProcessSyncUserIds(const std::vector &foregroundUserIds, @@ -2270,8 +2539,25 @@ void DeviceManagerService::ProcessSyncUserIds(const std::vector &foreg timer_->DeleteTimer(std::string(USER_SWITCH_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid)); } if (IsDMServiceImplReady()) { - dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, IsPC()); + dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); } + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); +} + +void DeviceManagerService::DelTimerAndEventTypeSyncUserIds(const std::string &remoteUdid) +{ + if (timer_ != nullptr) { + timer_->DeleteTimer(std::string(USER_SWITCH_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid)); + } + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); +} + +void DeviceManagerService::DelTimerAndEventTypeSyncUserUnlocked(const std::string &remoteUdid) +{ + if (timer_ != nullptr) { + timer_->DeleteTimer(std::string(USER_UNLOCKED_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid)); + } + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); } void DeviceManagerService::ScreenCommonEventCallback(std::string commonEventType) @@ -2542,6 +2828,10 @@ void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg) case RelationShipChangeType::STOP_USER: HandleUserStopBroadCast(relationShipMsg.userId, relationShipMsg.peerUdid); break; + case RelationShipChangeType::UNLOCKED_USER: + HandleUserUnlockedBroadCast(relationShipMsg.userIdInfos, relationShipMsg.peerUdid, + relationShipMsg.syncUserIdFlag); + break; default: LOGI("Dm have not this event type."); break; @@ -2859,11 +3149,13 @@ void DeviceManagerService::NotifyRemoteLocalUserSwitch(int32_t curUserId, int32_ { LOGI("Send local foreground and background userids"); if (peerUdids.empty()) { + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } if (softbusListener_ == nullptr) { dmServiceImpl_->HandleUserSwitched(peerUdids, curUserId, preUserId); LOGE("softbusListener_ is null"); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); return; } std::vector bleUdids; @@ -2922,6 +3214,21 @@ void DeviceManagerService::NotifyRemoteLocalUserSwitchByWifi(int32_t curUserId, } } +int32_t DeviceManagerService::SendUserUnlockedByWifi(const std::string &networkId, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) +{ + LOGI("Try open softbus session to exchange foreground/background userid"); + std::vector foregroundUserIdsUInt; + for (auto const &u : foregroundUserIds) { + foregroundUserIdsUInt.push_back(static_cast(u)); + } + std::vector backgroundUserIdsUInt; + for (auto const &u : backgroundUserIds) { + backgroundUserIdsUInt.push_back(static_cast(u)); + } + return DMCommTool::GetInstance()->SendUserUnlocked(networkId, foregroundUserIdsUInt, backgroundUserIdsUInt); +} + int32_t DeviceManagerService::SendUserIdsByWifi(const std::string &networkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) { @@ -2947,12 +3254,6 @@ void DeviceManagerService::HandleUserSwitchTimeout(int32_t curUserId, int32_t pr void DeviceManagerService::HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId) { - DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); - CheckRegisterInfoWithWise(currentUserId); - MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); - if (IsPC()) { - return; - } if (beforeUserId == -1 || currentUserId == -1) { HandleUserSwitched(); return; @@ -2965,31 +3266,34 @@ void DeviceManagerService::HandleUserSwitchedEvent(int32_t currentUserId, int32_ } } -void DeviceManagerService::HandleUserStopEvent(int32_t stopUserId) +void DeviceManagerService::HandleUserStopEvent(int32_t currentUserId, int32_t stopUserId) { LOGI("onStart, HandleUserStopEvent %{public}s.", GetAnonyInt32(stopUserId).c_str()); + (void)currentUserId; std::vector stopUserVec; stopUserVec.push_back(stopUserId); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); std::string localUdid = std::string(localUdidTemp); - std::map stopUserDeviceMap; - std::vector peerUdids; CHECK_NULL_VOID(discoveryMgr_); if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_STOPPED); return; } - stopUserDeviceMap = discoveryMgr_->GetCommonDependencyObj()-> - GetDeviceIdAndBindLevel(stopUserVec, localUdid); + std::map stopUserDeviceMap = + discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(stopUserVec, localUdid); + std::vector peerUdids; for (const auto &item : stopUserDeviceMap) { peerUdids.push_back(item.first); } if (peerUdids.empty()) { LOGI("no data to be stoped."); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_STOPPED); return; } NotifyRemoteLocalUserStop(localUdid, peerUdids, stopUserId); + DMCommTool::GetInstance()->DeleteCommonEvent(CommonEventSupport::COMMON_EVENT_USER_STOPPED); } void DeviceManagerService::DivideNotifyMethod(const std::vector &peerUdids, diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index 8abd91e4d..908bba830 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -35,9 +35,11 @@ constexpr int32_t DM_COMM_RSP_LOCAL_USERIDS = 2; constexpr int32_t DM_COMM_SEND_USER_STOP = 3; constexpr int32_t DM_COMM_RSP_USER_STOP = 4; constexpr int32_t DM_COMM_ACCOUNT_LOGOUT = 5; - +constexpr int32_t DM_COMM_SEND_USER_UNLOCKED = 6; +constexpr int32_t DM_COMM_RSP_USER_UNLOCKED = 7; +const int64_t MICROSECOND_TO_SECOND = 1000000L; const char* const USER_STOP_MSG_KEY = "stopUserId"; - +constexpr const char* EVENT_TASK = "EventTask"; DMCommTool::DMCommTool() : dmTransportPtr_(nullptr) { LOGI("Ctor DMCommTool"); @@ -46,6 +48,7 @@ DMCommTool::DMCommTool() : dmTransportPtr_(nullptr) void DMCommTool::Init() { LOGI("Init DMCommTool"); + eventQueue_ = std::make_shared(EVENT_TASK); dmTransportPtr_ = std::make_shared(shared_from_this()); std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); eventHandler_ = std::make_shared(runner, shared_from_this()); @@ -68,6 +71,47 @@ std::shared_ptr DMCommTool::GetInstance() return instance; } +int32_t DMCommTool::SendUserUnlocked(const std::string rmtNetworkId, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) +{ + if (!IsIdLengthValid(rmtNetworkId) || foregroundUserIds.empty() || dmTransportPtr_ == nullptr) { + LOGE("param invalid, networkId: %{public}s, foreground userids size: %{public}d", + GetAnonyString(rmtNetworkId).c_str(), static_cast(foregroundUserIds.size())); + return ERR_DM_INPUT_PARA_INVALID; + } + int32_t socketId; + if (dmTransportPtr_->StartSocket(rmtNetworkId, socketId) != DM_OK || socketId <= 0) { + LOGE("Start socket error"); + return ERR_DM_FAILED; + } + + UserIdsMsg userIdsMsg(foregroundUserIds, backgroundUserIds); + cJSON *root = cJSON_CreateObject(); + if (root == nullptr) { + LOGE("Create cJSON object failed."); + return ERR_DM_FAILED; + } + ToJson(root, userIdsMsg); + char *msg = cJSON_PrintUnformatted(root); + if (msg == nullptr) { + cJSON_Delete(root); + return ERR_DM_FAILED; + } + std::string msgStr(msg); + cJSON_Delete(root); + cJSON_free(msg); + CommMsg commMsg(DM_COMM_SEND_USER_UNLOCKED, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("Send local foreground userids failed, ret: %{public}d", ret); + return ERR_DM_FAILED; + } + LOGI("Send local foreground userids success"); + return DM_OK; +} + int32_t DMCommTool::SendUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) { @@ -76,6 +120,7 @@ int32_t DMCommTool::SendUserIds(const std::string rmtNetworkId, GetAnonyString(rmtNetworkId).c_str(), static_cast(foregroundUserIds.size())); return ERR_DM_INPUT_PARA_INVALID; } + int32_t socketId; if (dmTransportPtr_->StartSocket(rmtNetworkId, socketId) != DM_OK || socketId <= 0) { LOGE("Start socket error"); @@ -109,6 +154,35 @@ int32_t DMCommTool::SendUserIds(const std::string rmtNetworkId, return DM_OK; } +void DMCommTool::RspUserUnLocked(const std::string rmtNetworkId, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, int32_t socketId) +{ + UserIdsMsg userIdsMsg(foregroundUserIds, backgroundUserIds); + cJSON *root = cJSON_CreateObject(); + if (root == nullptr) { + LOGE("Create cJSON object failed."); + return; + } + ToJson(root, userIdsMsg); + char *msg = cJSON_PrintUnformatted(root); + if (msg == nullptr) { + cJSON_Delete(root); + return; + } + std::string msgStr(msg); + cJSON_Delete(root); + cJSON_free(msg); + CommMsg commMsg(DM_COMM_RSP_USER_UNLOCKED, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("Response local foreground userids failed, ret: %{public}d", ret); + return; + } + LOGI("Response local foreground userids success"); +} + void DMCommTool::RspLocalFrontOrBackUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, int32_t socketId) { @@ -144,8 +218,7 @@ DMCommTool::DMCommToolEventHandler::DMCommToolEventHandler(const std::shared_ptr LOGI("Ctor DMCommToolEventHandler"); } -void DMCommTool::DMCommToolEventHandler::ProcessEvent( - const AppExecFwk::InnerEvent::Pointer &event) +void DMCommTool::DMCommToolEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) { uint32_t eventId = event->GetInnerEventId(); std::shared_ptr commMsg = event->GetSharedObject(); @@ -185,13 +258,21 @@ void DMCommTool::DMCommToolEventHandler::ProcessEvent( dmCommToolPtr->ProcessReceiveLogoutEvent(commMsg); break; } + case DM_COMM_SEND_USER_UNLOCKED: { + dmCommToolPtr->ProcessReceiveUserUnLockedEvent(commMsg); + break; + } + case DM_COMM_RSP_USER_UNLOCKED: { + dmCommToolPtr->ProcessResponseUserUnLockedEvent(commMsg); + break; + } default: LOGE("event is undefined, id is %{public}d", eventId); break; } } -void DMCommTool::ProcessReceiveUserIdsEvent(const std::shared_ptr commMsg) +void DMCommTool::ProcessReceiveUserUnLockedEvent(const std::shared_ptr commMsg) { LOGI("Receive remote userids, process and rsp local userid"); std::string rmtUdid = ""; @@ -218,9 +299,90 @@ void DMCommTool::ProcessReceiveUserIdsEvent(const std::shared_ptr std::vector backgroundUserIds; MultipleUserConnector::GetForegroundUserIds(foregroundUserIds); MultipleUserConnector::GetBackgroundUserIds(backgroundUserIds); - if (DeviceManagerService::GetInstance().IsPC()) { - MultipleUserConnector::ClearLockedUser(foregroundUserIds, backgroundUserIds); + MultipleUserConnector::ClearLockedUser(foregroundUserIds, backgroundUserIds); + std::vector foregroundUserIdsU32; + std::vector backgroundUserIdsU32; + for (auto const &u : foregroundUserIds) { + foregroundUserIdsU32.push_back(static_cast(u)); + } + for (auto const &u : backgroundUserIds) { + backgroundUserIdsU32.push_back(static_cast(u)); } + RspUserUnLocked(commMsg->remoteNetworkId, foregroundUserIdsU32, backgroundUserIdsU32, + commMsg->socketId); + + if (userIdsMsg.foregroundUserIds.empty()) { + LOGE("Parse but get none remote foreground userids"); + } else { + DeviceManagerService::GetInstance().ProcessSyncUserUnlocked(userIdsMsg.foregroundUserIds, + userIdsMsg.backgroundUserIds, rmtUdid); + } +} + +void DMCommTool::ProcessResponseUserUnLockedEvent(const std::shared_ptr commMsg) +{ + LOGI("process receive remote userids response"); + // step1: close socket + this->dmTransportPtr_->StopSocket(commMsg->remoteNetworkId); + + std::string rmtUdid = ""; + SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); + if (rmtUdid.empty()) { + LOGE("Can not find remote udid by networkid: %{public}s", commMsg->remoteNetworkId.c_str()); + return; + } + + std::string payload = commMsg->commMsg->msg; + cJSON *root = cJSON_Parse(payload.c_str()); + if (root == NULL) { + LOGE("the msg is not json format"); + return; + } + UserIdsMsg userIdsMsg; + FromJson(root, userIdsMsg); + cJSON_Delete(root); + if (userIdsMsg.foregroundUserIds.empty()) { + LOGE("Parse but get none remote userids"); + return; + } + // step2: process remote foreground/background userids + if (!userIdsMsg.foregroundUserIds.empty()) { + DeviceManagerService::GetInstance().ProcessSyncUserUnlocked(userIdsMsg.foregroundUserIds, + userIdsMsg.backgroundUserIds, rmtUdid); + } else { + DeviceManagerService::GetInstance().DelTimerAndEventTypeSyncUserUnlocked(rmtUdid); + LOGE("Receive remote foreground userid empty"); + } +} + +void DMCommTool::ProcessReceiveUserIdsEvent(const std::shared_ptr commMsg) +{ + LOGI("Receive remote userids, process and rsp local userid"); + std::string rmtUdid = ""; + SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); + if (rmtUdid.empty()) { + LOGE("Can not find remote udid by networkid: %{public}s", commMsg->remoteNetworkId.c_str()); + return; + } + + std::string payload = commMsg->commMsg->msg; + cJSON *root = cJSON_Parse(payload.c_str()); + if (root == NULL) { + LOGE("the msg is not json format"); + return; + } + UserIdsMsg userIdsMsg; + FromJson(root, userIdsMsg); + cJSON_Delete(root); + uint32_t totalUserNum = static_cast(userIdsMsg.foregroundUserIds.size()) + + static_cast(userIdsMsg.backgroundUserIds.size()); + + // step1: send back local userids + std::vector foregroundUserIds; + std::vector backgroundUserIds; + MultipleUserConnector::GetForegroundUserIds(foregroundUserIds); + MultipleUserConnector::GetBackgroundUserIds(backgroundUserIds); + MultipleUserConnector::ClearLockedUser(foregroundUserIds, backgroundUserIds); std::vector foregroundUserIdsU32; std::vector backgroundUserIdsU32; for (auto const &u : foregroundUserIds) { @@ -272,6 +434,7 @@ void DMCommTool::ProcessResponseUserIdsEvent(const std::shared_ptr DeviceManagerService::GetInstance().ProcessSyncUserIds(userIdsMsg.foregroundUserIds, userIdsMsg.backgroundUserIds, rmtUdid); } else { + DeviceManagerService::GetInstance().DelTimerAndEventTypeSyncUserIds(rmtUdid); LOGE("Receive remote foreground userid empty"); } } @@ -498,5 +661,50 @@ void DMCommTool::ProcessReceiveLogoutEvent(const std::shared_ptr c rmtUdid, logoutAccountMsg.userId); LOGI("process remote logout success."); } +//TODO: 事件的任务队列 +int32_t DMCommTool::StartCommonEvent(std::string commonEventType, EventCallback eventCallback) +{ + if (commonEventType.empty() || eventCallback == nullptr) { + LOGE("StartCommonEvent input value invalid"); + return ERR_DM_INPUT_PARA_INVALID; + } + CHECK_NULL_RETURN(eventQueue_, ERR_DM_POINT_NULL); + LOGI("StartCommonEvent start eventType: %{public}s", commonEventType.c_str()); + std::lock_guard locker(eventMutex_); + + auto taskFunc = [eventCallback] () { eventCallback(); }; + ffrt::task_handle handle = + eventQueue_->submit_h(taskFunc, ffrt::task_attr().delay(0)); + if (handle == nullptr) { + LOGE("handle is nullptr."); + return ERR_DM_FAILED; + } + eventVec_[commonEventType] = std::move(handle); + return DM_OK; +} + +int32_t DMCommTool::DeleteCommonEvent(std::string commonEventType) +{ + if (commonEventType.empty()) { + LOGE("DmTimer DeleteTimer timer is null"); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("DeleteCommonEvent start eventType: %{public}s", commonEventType.c_str()); + std::lock_guard locker(eventMutex_); + auto item = eventVec_.find(commonEventType); + if (item == eventVec_.end()) { + LOGI("Invalid task."); + return ERR_DM_FAILED; + } + if (item->second != nullptr && eventQueue_ != nullptr) { + int32_t ret = eventQueue_->cancel(item->second); + if (ret != 0) { + LOGE("Cancel failed, errCode: %{public}d.", ret); + } + } + eventVec_.erase(commonEventType); + return DM_OK; +} + } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp index 1cf70e56a..95e939a7f 100644 --- a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp @@ -1633,8 +1633,9 @@ HWTEST_F(DeviceProfileConnectorTest, GetUserIdAndBindLevel_001, testing::ext::Te remoteFrontUserIds.push_back(userId); std::vector remoteBackUserIds; remoteBackUserIds.push_back(userId); + DmOfflineParam offlineParam; DeviceProfileConnector::GetInstance().UpdateACL(localUdid, localUserIds, remoteUdid, remoteFrontUserIds, - remoteBackUserIds); + remoteBackUserIds, offlineParam); localUdid = "remoteDeviceId"; remoteUdid = "localDeviceId"; @@ -1810,13 +1811,14 @@ HWTEST_F(DeviceProfileConnectorTest, CheckSrcDevIdInAclForDevBind_005, testing:: std::vector remoteFrontUserIds; std::vector remoteBackUserIds; AddAccessControlProfileFirst(profiles); + DmOfflineParam offlineParam; DeviceProfileConnector::GetInstance().DeleteSigTrustACL(profiles, remoteUdid, - remoteFrontUserIds, remoteBackUserIds); + remoteFrontUserIds, remoteBackUserIds, offlineParam); remoteUdid = "localDeviceId"; AddAccessControlProfileSeven(profiles); DeviceProfileConnector::GetInstance().DeleteSigTrustACL(profiles, remoteUdid, - remoteFrontUserIds, remoteBackUserIds); + remoteFrontUserIds, remoteBackUserIds, offlineParam); AddAccessControlProfileEight(profiles); std::string localUdid = "localDeviceId"; diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp index 081b33071..08bff7eaa 100644 --- a/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp @@ -197,10 +197,13 @@ HWTEST_F(DeviceProfileConnectorSecondTest, CheckIdenticalAccount_201, testing::e remoteFrontUserIds.push_back(userIds); std::vector remoteBackUserIds; remoteBackUserIds.push_back(userIds); - DeviceProfileConnector::GetInstance().DeleteSigTrustACL(profile, remoteUdid, remoteFrontUserIds, remoteBackUserIds); + DmOfflineParam offlineParam; + DeviceProfileConnector::GetInstance().DeleteSigTrustACL(profile, remoteUdid, remoteFrontUserIds, remoteBackUserIds, + offlineParam); remoteUdid = "deviceIdEe"; - DeviceProfileConnector::GetInstance().DeleteSigTrustACL(profile, remoteUdid, remoteFrontUserIds, remoteBackUserIds); + DeviceProfileConnector::GetInstance().DeleteSigTrustACL(profile, remoteUdid, remoteFrontUserIds, remoteBackUserIds, + offlineParam); int32_t userIdee = 0; accessee.SetAccesseeUserId(userIdee); diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index f08ba99a9..bf66f716b 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -1851,7 +1851,7 @@ HWTEST_F(DeviceManagerServiceImplTest, GetDeviceIdAndUserId_001, testing::ext::T std::vector foregroundUserIds; std::vector backgroundUserIds; std::string remoteUdid = "deviceId"; - deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false); + deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); std::vector deviceVec; int32_t currentUserId = 1; diff --git a/test/unittest/UTTest_device_manager_service_impl_first.cpp b/test/unittest/UTTest_device_manager_service_impl_first.cpp index af21d1fa7..759050f29 100644 --- a/test/unittest/UTTest_device_manager_service_impl_first.cpp +++ b/test/unittest/UTTest_device_manager_service_impl_first.cpp @@ -79,14 +79,14 @@ HWTEST_F(DeviceManagerServiceImplFirstTest, GetDeviceIdAndUserId_101, testing::e std::vector backgroundUserIds; std::string remoteUdid = "deviceId"; EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)).WillOnce(Return(ERR_DM_FAILED)); - deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false); + deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); std::vector localUserIds; localUserIds.push_back(101); localUserIds.push_back(102); EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)) .WillOnce(DoAll(SetArgReferee<0>(localUserIds), Return(DM_OK))); - deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false); + deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); } } // namespace } // namespace DistributedHardware -- Gitee