From 0a524e2af62c95a03d9a9d946bafc69a43a9699e Mon Sep 17 00:00:00 2001 From: liuzhongming Date: Thu, 6 Mar 2025 15:01:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DPC=E5=88=87=E6=8D=A2=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BA=8B=E4=BB=B6=20Signed-off-by:=20liuzhongming=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/deviceprofile_connector.h | 11 +- .../include/multiple_user_connector.h | 5 +- .../src/deviceprofile_connector.cpp | 70 ++++- .../src/multiple_user_connector.cpp | 41 +++ .../include/device_manager_service_impl.h | 4 +- .../device_manager_service_impl_lite.h | 4 +- .../src/device_manager_service_impl.cpp | 9 +- .../src/device_manager_service_impl_lite.cpp | 5 +- .../service/include/device_manager_service.h | 15 +- .../include/idevice_manager_service_impl.h | 4 +- .../relationshipsyncmgr/dm_comm_tool.h | 9 +- .../relationship_sync_mgr.h | 7 +- .../service/src/device_manager_service.cpp | 255 +++++++++++++++--- .../dm_account_common_event.cpp | 4 +- .../src/relationshipsyncmgr/dm_comm_tool.cpp | 157 ++++++++++- .../relationship_sync_mgr.cpp | 50 +++- .../UTTest_device_manager_service_impl.cpp | 4 +- ...Test_device_manager_service_impl_first.cpp | 6 +- 18 files changed, 590 insertions(+), 70 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 30869a252..f61e5265b 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -99,6 +99,9 @@ class IDeviceProfileConnector { public: virtual ~IDeviceProfileConnector() {} virtual int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm) = 0; + 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; }; class DeviceProfileConnector : public IDeviceProfileConnector { @@ -157,6 +160,9 @@ public: void DeleteAccessControlById(int64_t accessControlId); int32_t HandleUserSwitched(const std::string &localUdid, const std::vector &deviceVec, int32_t currentUserId, int32_t beforeUserId); + void HandleUserSwitched(const std::vector &activeProfiles, + const std::vector &inActiveProfiles, + const std::vector &delActiveProfiles); void HandleSyncForegroundUserIdEvent(const std::vector &remoteUserIds, const std::string &remoteUdid, const std::vector &localUserIds, std::string &localUdid); std::vector GetOfflineProcessInfo(std::string &localUdid, const std::vector &localUserIds, @@ -187,6 +193,9 @@ public: int32_t GetServiceInfoProfileListByBundleName(const DistributedDeviceProfile::ServiceInfoUniqueKey& key, std::vector& serviceInfoProfiles); int32_t PutSessionKey(const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId); + int32_t HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid); + int32_t HandleUserStop(int32_t stopUserId, const std::string &localUdid, + const std::vector &acceptEventUdids); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/include/multiple_user_connector.h b/commondependency/include/multiple_user_connector.h index 692316e2c..32a43617e 100644 --- a/commondependency/include/multiple_user_connector.h +++ b/commondependency/include/multiple_user_connector.h @@ -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 @@ -110,6 +110,9 @@ public: static int32_t GetBackgroundUserIds(std::vector &userIdVec); static int32_t GetAllUserIds(std::vector &userIdVec); static std::string GetAccountNickName(int32_t userId); + static bool IsUserUnlocked(int32_t userId); + static void ClearLockedUser(std::vector &foregrounUserVec); + static void ClearLockedUser(std::vector &foregrounUserVec, std::vector &backgroundUserVec); private: static int32_t oldUserId_; diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 63207c775..09ef165d5 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -1314,16 +1314,22 @@ int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid, std::vector profiles = GetAllAccessControlProfile(); std::vector activeProfiles; std::vector inActiveProfiles; + std::vector delActiveProfiles; for (auto &item : profiles) { if (std::find(deviceVec.begin(), deviceVec.end(), item.GetTrustDeviceId()) == deviceVec.end()) { continue; } if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && - item.GetAccesser().GetAccesserUserId() == beforeUserId && item.GetStatus() == ACTIVE) || + item.GetAccesser().GetAccesserUserId() == beforeUserId) || (item.GetAccessee().GetAccesseeDeviceId() == localUdid && - item.GetAccessee().GetAccesseeUserId() == beforeUserId && item.GetStatus() == ACTIVE)) { - item.SetStatus(INACTIVE); - inActiveProfiles.push_back(item); + item.GetAccessee().GetAccesseeUserId() == beforeUserId)) { + if (item.GetStatus() == ACTIVE) { + item.SetStatus(INACTIVE); + inActiveProfiles.push_back(item); + } + if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) { + delActiveProfiles.push_back(item); + } continue; } if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && @@ -1335,16 +1341,23 @@ int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid, continue; } } + HandleUserSwitched(activeProfiles, inActiveProfiles, delActiveProfiles); + return DM_OK; +} + +void DeviceProfileConnector::HandleUserSwitched(const std::vector &activeProfiles, + const std::vector &inActiveProfiles, + const std::vector &delActiveProfiles) +{ for (auto &item : inActiveProfiles) { DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); - if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) { - DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); - } + } + for (auto &item : delActiveProfiles) { + DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); } for (auto &item : activeProfiles) { DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); } - return DM_OK; } std::vector DeviceProfileConnector::GetAclProfileByUserId(const std::string &localUdid, @@ -1840,6 +1853,45 @@ int32_t DeviceProfileConnector::PutSessionKey( return DM_OK; } +int32_t DeviceProfileConnector::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid) +{ + std::vector profiles = GetAllAccessControlProfile(); + for (auto &item : profiles) { + if ((item.GetAccesser().GetAccesserDeviceId() == stopEventUdid && + item.GetAccesser().GetAccesserUserId() == stopUserId && item.GetStatus() == ACTIVE) || + (item.GetAccessee().GetAccesseeDeviceId() == stopEventUdid && + item.GetAccessee().GetAccesseeUserId() == stopUserId && item.GetStatus() == ACTIVE)) { + item.SetStatus(INACTIVE); + DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); + } + } + return DM_OK; +} + +int32_t DeviceProfileConnector::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid, + const std::vector &acceptEventUdids) +{ + if (acceptEventUdids.empty()) { + LOGI("no remote device."); + return DM_OK; + } + std::vector profiles = GetAllAccessControlProfile(); + for (auto &item : profiles) { + if (std::find(acceptEventUdids.begin(), acceptEventUdids.end(), item.GetTrustDeviceId()) == + acceptEventUdids.end()) { + continue; + } + if ((item.GetAccesser().GetAccesserDeviceId() == stopEventUdid && + item.GetAccesser().GetAccesserUserId() == stopUserId && item.GetStatus() == ACTIVE) || + (item.GetAccessee().GetAccesseeDeviceId() == stopEventUdid && + item.GetAccessee().GetAccesseeUserId() == stopUserId && item.GetStatus() == ACTIVE)) { + item.SetStatus(INACTIVE); + DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); + } + } + return DM_OK; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/commondependency/src/multiple_user_connector.cpp b/commondependency/src/multiple_user_connector.cpp index ceebcb04e..01537c644 100644 --- a/commondependency/src/multiple_user_connector.cpp +++ b/commondependency/src/multiple_user_connector.cpp @@ -317,5 +317,46 @@ std::string MultipleUserConnector::GetAccountNickName(int32_t userId) return ""; #endif } + +bool MultipleUserConnector::IsUserUnlocked(int32_t userId) +{ +#if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) + return true; +#elif OS_ACCOUNT_PART_EXISTS + bool isUserUnlocked = false; + ErrCode ret = OsAccountManager::IsOsAccountVerified(userId, isUserUnlocked); + if (ret != 0) { + LOGE("IsUserUnlocked error ret: %{public}d", ret); + return false; + } + return isUserUnlocked; +#else + return true; +#endif +} + +void MultipleUserConnector::ClearLockedUser(std::vector &foregrounUserVec) +{ + for (auto iter = foregrounUserVec.begin(); iter != foregrounUserVec.end();) { + if (!IsUserUnlocked(*iter)) { + iter = foregrounUserVec.erase(iter); + } else { + ++iter; + } + } +} + +void MultipleUserConnector::ClearLockedUser(std::vector &foregrounUserVec, + std::vector &backgroundUserVec) +{ + for (auto iter = foregrounUserVec.begin(); iter != foregrounUserVec.end();) { + if (!IsUserUnlocked(*iter)) { + backgroundUserVec.push_back(*iter); + iter = foregrounUserVec.erase(iter); + } else { + ++iter; + } + } +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 55e5426b0..4fcb6cd47 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -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 @@ -126,7 +126,7 @@ public: void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid); + const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); void HandleUserSwitched(const std::vector &deviceVec, int32_t currentUserId, int32_t beforeUserId); std::multimap GetDeviceIdAndUserId(int32_t localUserId); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 6d05f91e9..2ad955b5e 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -148,7 +148,7 @@ public: void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid); + const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); void HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid); std::map GetDeviceIdAndBindLevel(int32_t userId); 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 c1f50e77c..99ee961ab 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.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 @@ -891,7 +891,7 @@ void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const } void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid) + const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) { LOGI("remote udid: %{public}s, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s", GetAnonyString(remoteUdid).c_str(), GetIntegerList(foregroundUserIds).c_str(), @@ -903,10 +903,13 @@ void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector std::vector rmtBackUserIdsTemp(backgroundUserIds.begin(), backgroundUserIds.end()); std::vector localUserIds; int32_t ret = MultipleUserConnector::GetForegroundUserIds(localUserIds); - if (ret != DM_OK || localUserIds.empty()) { + if (ret != DM_OK) { LOGE("Get foreground userids failed, ret: %{public}d", ret); return; } + if (isCheckUserStatus) { + MultipleUserConnector::CheckUserStatus(localUserIds); + } DeviceProfileConnector::GetInstance().UpdateACL(localUdid, localUserIds, remoteUdid, rmtFrontUserIdsTemp, rmtBackUserIdsTemp); DeviceProfileConnector::GetInstance().HandleSyncBackgroundUserIdEvent(rmtBackUserIdsTemp, remoteUdid, diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index a9488e7c3..f82b8f95f 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -543,11 +543,12 @@ int32_t DeviceManagerServiceImpl::ProcessAppUnintall(const std::string &appId, i } void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &remoteUdid) + const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) { (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 6b70ff2d9..1bd5f6715 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -197,6 +197,9 @@ public: #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void HandleDeviceTrustedChange(const std::string &msg); void HandleUserIdCheckSumChange(const std::string &msg); + void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid); + void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid, + const std::vector &acceptEventUdids); #endif int32_t SetDnPolicy(const std::string &pkgName, std::map &policy); void ClearDiscoveryCache(const ProcessInfo &processInfo); @@ -231,6 +234,7 @@ public: int32_t GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo); int32_t GetCallerServiceInfos(std::vector &serviceInfos); void ClearPulishIdCache(const std::string &pkgName); + bool IsPC(); private: bool IsDMServiceImplReady(); @@ -309,12 +313,21 @@ private: void InitServiceInfos(const std::vector &profiles, std::vector &serviceInfos); bool InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); + void HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId); + void HandleUserStopEvent(int32_t stopUserId); + void DivideNotifyMethod(const std::vector &peerUdids, std::vector &bleUdids, + std::map &wifiDevices); + void NotifyRemoteLocalUserStop(const std::string &localUdid, + const std::vector &peerUdids, int32_t stopUserId); + void SendUserStopBroadCast(const std::vector &peerUdids, int32_t stopUserId); + void HandleUserStopBroadCast(int32_t stopUserId, const std::string &remoteUdid); + void NotifyRemoteLocalUserStopByWifi(const std::string &localUdid, + const std::map &wifiDevices, int32_t stopUserId); #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void SubscribePublishCommonEvent(); void QueryDependsSwitchState(); #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI #endif - private: bool isImplsoLoaded_ = false; bool isAdapterResidentSoLoaded_ = false; diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 7df04af0a..8ba83a8ad 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -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 @@ -249,7 +249,7 @@ public: int32_t errcode) = 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) = 0; + const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) = 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 7cd55bdad..37f1361ab 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.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 @@ -40,6 +40,13 @@ public: const std::vector &backgroundUserIds); void RspLocalFrontOrBackUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, int32_t socketId); + int32_t CreateUserStopMessage(int32_t stopUserId, std::string &msgStr); + int32_t SendMsg(const std::string rmtNetworkId, int32_t msgType, const std::string &msg); + int32_t SendUserStop(const std::string rmtNetworkId, int32_t stopUserId); + int32_t ParseUserStopMessage(const std::string &msgStr, int32_t &stopUserId); + 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); class DMCommToolEventHandler : public AppExecFwk::EventHandler { public: diff --git a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h index baffe7f3f..50caeca14 100644 --- a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h +++ b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.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 @@ -30,7 +30,8 @@ enum class RelationShipChangeType : uint32_t { DEL_USER = 4, APP_UNINSTALL = 5, SYNC_USERID = 6, - TYPE_MAX = 7 + STOP_USER = 7, + TYPE_MAX = 8 }; struct UserIdInfo { @@ -70,6 +71,7 @@ struct RelationShipChangeMsg { void ToAppUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; bool ToSyncFrontOrBackUserIdPayLoad(uint8_t *&msg, uint32_t &len) const; void ToDelUserPayLoad(uint8_t *&msg, uint32_t &len) const; + void ToStopUserPayLoad(uint8_t *&msg, uint32_t &len) const; cJSON *ToPayLoadJson() const; bool FromAccountLogoutPayLoad(const cJSON *payloadJson); @@ -77,6 +79,7 @@ struct RelationShipChangeMsg { bool FromAppUnbindPayLoad(const cJSON *payloadJson); bool FromSyncFrontOrBackUserIdPayLoad(const cJSON *payloadJson); bool FromDelUserPayLoad(const cJSON *payloadJson); + bool FromStopUserPayLoad(const cJSON *payloadJson); std::string ToJson() const; bool FromJson(const std::string &msgJson); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 778d1df67..d3bc61804 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -86,6 +86,7 @@ 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_STOP_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:userStopByWifi"; const int32_t USER_SWITCH_BY_WIFI_TIMEOUT_S = 2; } @@ -248,6 +249,10 @@ int32_t DeviceManagerService::InitDMServiceListener() #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) DMCommTool::GetInstance()->Init(); #endif + int32_t currentUserId = MultipleUserConnector::GetFirstForegroundUserId(); + if (IsPC() && !MultipleUserConnector::IsUserUnlocked(currentUserId)) { + HandleUserStopEvent(currentUserId); + } LOGI("Init success."); return DM_OK; } @@ -1905,6 +1910,8 @@ void DeviceManagerService::SubscribeAccountCommonEvent() AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_HWID_LOGOUT); AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_HWID_LOGIN); AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED); + AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_STOPPED); + AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED); if (accountCommonEventManager_->SubscribeAccountCommonEvent(AccountCommonEventVec, callback)) { LOGI("Success"); } @@ -1932,19 +1939,7 @@ void DeviceManagerService::AccountCommonEventCallback(const std::string commonEv LOGI("CommonEventType: %{public}s, currentUserId: %{public}d, beforeUserId: %{public}d", commonEventType.c_str(), currentUserId, beforeUserId); if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); - DMAccountInfo dmAccountInfo; - dmAccountInfo.accountId = MultipleUserConnector::GetOhosAccountId(); - dmAccountInfo.accountName = MultipleUserConnector::GetOhosAccountName(); - MultipleUserConnector::SetAccountInfo(currentUserId, dmAccountInfo); - if (beforeUserId == -1 || currentUserId == -1) { - return; - } else if (beforeUserId != -1 && currentUserId != -1) { - HandleUserSwitched(currentUserId, beforeUserId); - } - if (IsDMServiceAdapterResidentLoad()) { - dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); - } + HandleUserSwitchedEvent(currentUserId, beforeUserId); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { DeviceNameManager::GetInstance().InitDeviceNameWhenLogin(); DMAccountInfo dmAccountInfo; @@ -1972,6 +1967,13 @@ void DeviceManagerService::AccountCommonEventCallback(const std::string commonEv MultipleUserConnector::SetAccountInfo(MultipleUserConnector::GetCurrentAccountUserID(), curDmAccountInfo); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) { DeviceNameManager::GetInstance().InitDeviceNameWhenNickChange(); + } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_STOPPED && IsPC()) { + HandleUserStopEvent(beforeUserId); + } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED && IsPC()) { + HandleUserSwitched(); + if (IsDMServiceAdapterResidentLoad()) { + dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); + } } else { LOGE("Invalied account common event."); } @@ -2128,7 +2130,10 @@ void DeviceManagerService::HandleUserIdsBroadCast(const std::vector std::vector backgroundUserVec; int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); - if (retFront != DM_OK || retBack!= DM_OK || foregroundUserVec.empty()) { + if (IsPC()) { + 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())); @@ -2143,26 +2148,22 @@ void DeviceManagerService::HandleUserIdsBroadCast(const std::vector std::vector foregroundUserIdInfos; std::vector backgroundUserIdInfos; GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos); - if (foregroundUserIdInfos.empty()) { - LOGE("receive remote foreground userid empty"); - } else { - 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); - } + 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); - } + if (IsDMServiceImplReady()) { + dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, IsPC()); } } @@ -2180,7 +2181,7 @@ 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); + dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, IsPC()); } } @@ -2437,6 +2438,9 @@ void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg) case RelationShipChangeType::DEL_USER: dmServiceImpl_->HandleRemoteUserRemoved(relationShipMsg.userId, relationShipMsg.peerUdid); break; + case RelationShipChangeType::STOP_USER: + HandleUserStopBroadCast(relationShipMsg.userId, relationShipMsg.peerUdid); + break; default: LOGI("Dm have not this event type."); break; @@ -2819,6 +2823,178 @@ void DeviceManagerService::HandleUserSwitchTimeout(int32_t curUserId, int32_t pr updateUdids.push_back(udid); dmServiceImpl_->HandleUserSwitched(updateUdids, curUserId, preUserId); } + +void DeviceManagerService::HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId) +{ + DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); + DMAccountInfo dmAccountInfo; + dmAccountInfo.accountId = MultipleUserConnector::GetOhosAccountId(); + dmAccountInfo.accountName = MultipleUserConnector::GetOhosAccountName(); + MultipleUserConnector::SetAccountInfo(currentUserId, dmAccountInfo); + if (beforeUserId == -1 || currentUserId == -1) { + if (IsPC()) { + return; + } + HandleUserSwitched(); + return; + } else if (beforeUserId != -1 && currentUserId != -1) { + if (IsPC()) { + return; + } + HandleUserSwitched(currentUserId, beforeUserId); + } + if (IsDMServiceAdapterResidentLoad()) { + dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); + } +} + +void DeviceManagerService::HandleUserStopEvent(int32_t stopUserId) +{ + LOGI("onStart, HandleUserStopEvent."); + 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."); + return; + } + stopUserDeviceMap = discoveryMgr_->GetCommonDependencyObj()-> + GetDeviceIdAndBindLevel(stopUserVec, localUdid); + for (const auto &item : stopUserDeviceMap) { + peerUdids.push_back(item.first); + } + if (peerUdids.empty()) { + LOGI("no data to be stoped."); + return; + } + NotifyRemoteLocalUserStop(localUdid, peerUdids, stopUserId); +} + +void DeviceManagerService::DivideNotifyMethod(const std::vector &peerUdids, + std::vector &bleUdids, std::map &wifiDevices) +{ + if (peerUdids.empty()) { + return; + } + if (softbusListener_ == nullptr) { + bleUdids = peerUdids; + LOGI("softbusListener_ is null"); + return; + } + 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)); + } + } +} + +void DeviceManagerService::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid, + const std::vector &acceptEventUdids) +{ + if (timer_ != nullptr) { + for (const auto &udid : acceptEventUdids) { + timer_->DeleteTimer(std::string(USER_STOP_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid)); + } + } + if (MultipleUserConnector::IsUserUnlocked(stopUserId)) { + LOGE("user has unlocked."); + return; + } + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + return; + } + discoveryMgr_->GetCommonDependencyObj()->HandleUserStop(stopUserId, stopEventUdid, acceptEventUdids); +} + +void DeviceManagerService::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid) +{ + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + return; + } + discoveryMgr_->GetCommonDependencyObj()->HandleUserStop(stopUserId, stopEventUdid); +} + +void DeviceManagerService::NotifyRemoteLocalUserStop(const std::string &localUdid, + const std::vector &peerUdids, int32_t stopUserId) +{ + std::vector bleUdids; + std::map wifiDevices; + DivideNotifyMethod(peerUdids, bleUdids, wifiDevices); + if (!bleUdids.empty()) { + HandleUserStop(stopUserId, localUdid, bleUdids); + SendUserStopBroadCast(bleUdids, stopUserId); + } + if (!wifiDevices.empty()) { + NotifyRemoteLocalUserStopByWifi(localUdid, wifiDevices, stopUserId); + } +} + +void DeviceManagerService::SendUserStopBroadCast(const std::vector &peerUdids, int32_t stopUserId) +{ + LOGI("peerUdids: %{public}s", GetAnonyStringList(peerUdids).c_str()); + RelationShipChangeMsg msg; + msg.type = RelationShipChangeType::STOP_USER; + msg.userId = static_cast(stopUserId); + msg.peerUdids = peerUdids; + std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); + CHECK_NULL_VOID(softbusListener_); + softbusListener_->SendAclChangedBroadcast(broadCastMsg); +} + +void DeviceManagerService::HandleUserStopBroadCast(int32_t stopUserId, const std::string &remoteUdid) +{ + LOGI("start"); + HandleUserStop(stopUserId, remoteUdid); +} + +void DeviceManagerService::NotifyRemoteLocalUserStopByWifi(const std::string &localUdid, + const std::map &wifiDevices, int32_t stopUserId) +{ + for (const auto &it : wifiDevices) { + std::vector updateUdids; + updateUdids.push_back(it.first); + int32_t result = DMCommTool::GetInstance()->SendUserStop(it.second, stopUserId); + if (result != DM_OK) { + LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); + HandleUserStop(stopUserId, localUdid, updateUdids); + continue; + } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + std::string udid = it.first; + timer_->StartTimer(std::string(USER_STOP_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), + USER_SWITCH_BY_WIFI_TIMEOUT_S, + [this, stopUserId, localUdid, updateUdids] (std::string name) { + DeviceManagerService::HandleUserStop(stopUserId, localUdid, updateUdids); + }); + } +} #endif int32_t DeviceManagerService::RegisterAuthenticationType(const std::string &pkgName, @@ -2959,5 +3135,16 @@ void DeviceManagerService::ClearPulishIdCache(const std::string &pkgName) CHECK_NULL_VOID(advertiseMgr_); advertiseMgr_->ClearPulishIdCache(pkgName); } + +bool DeviceManagerService::IsPC() +{ + if (softbusListener_ == nullptr) { + LOGE("softbusListener_ is null."); + return false; + } + DmDeviceInfo info; + GetLocalDeviceInfo(info); + return (info.deviceTypeId == DmDeviceType::DEVICE_TYPE_PC || info.deviceTypeId == DmDeviceType::DEVICE_TYPE_2IN1); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/src/publishcommonevent/dm_account_common_event.cpp b/services/service/src/publishcommonevent/dm_account_common_event.cpp index f761bb6ff..8fa862105 100644 --- a/services/service/src/publishcommonevent/dm_account_common_event.cpp +++ b/services/service/src/publishcommonevent/dm_account_common_event.cpp @@ -144,7 +144,9 @@ void DmAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data) beforeUserId = std::atoi(data.GetWant().GetStringParam("oldId").c_str()); accountValidEvent = true; } - if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED || + receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED || + receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) { beforeUserId = data.GetCode(); accountValidEvent = true; } diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index ee96ca3aa..48058e075 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.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 @@ -25,6 +25,7 @@ #include "dm_log.h" #include "dm_softbus_cache.h" #include "multiple_user_connector.h" +#include "parameter.h" namespace OHOS { namespace DistributedHardware { @@ -33,6 +34,10 @@ constexpr int32_t DM_COMM_SEND_LOCAL_USERIDS = 1; // if receive remote device send foreground userids, response local foreground uerids // This msg no need response 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; + +const char* const USER_STOP_MSG_KEY = "stopUserId"; DMCommTool::DMCommTool() : dmTransportPtr_(nullptr) { @@ -92,6 +97,7 @@ int32_t DMCommTool::SendUserIds(const std::string rmtNetworkId, } std::string msgStr(msg); cJSON_Delete(root); + cJSON_free(msg); CommMsg commMsg(DM_COMM_SEND_LOCAL_USERIDS, msgStr); std::string payload = GetCommMsgString(commMsg); @@ -121,6 +127,7 @@ void DMCommTool::RspLocalFrontOrBackUserIds(const std::string rmtNetworkId, } std::string msgStr(msg); cJSON_Delete(root); + cJSON_free(msg); CommMsg commMsg(DM_COMM_RSP_LOCAL_USERIDS, msgStr); std::string payload = GetCommMsgString(commMsg); @@ -167,6 +174,14 @@ void DMCommTool::DMCommToolEventHandler::ProcessEvent( dmCommToolPtr->ProcessResponseUserIdsEvent(commMsg); break; } + case DM_COMM_SEND_USER_STOP: { + dmCommToolPtr->ProcessReceiveUserStopEvent(commMsg); + break; + } + case DM_COMM_RSP_USER_STOP: { + dmCommToolPtr->ProcessResponseUserStopEvent(commMsg); + break; + } default: LOGE("event is undefined, id is %{public}d", eventId); break; @@ -200,6 +215,9 @@ void DMCommTool::ProcessReceiveUserIdsEvent(const std::shared_ptr std::vector backgroundUserIds; MultipleUserConnector::GetForegroundUserIds(foregroundUserIds); MultipleUserConnector::GetBackgroundUserIds(backgroundUserIds); + if (DeviceManagerService::GetInstance().IsPC()) { + MultipleUserConnector::ClearLockedUser(foregroundUserIds, backgroundUserIds); + } std::vector foregroundUserIdsU32; std::vector backgroundUserIdsU32; for (auto const &u : foregroundUserIds) { @@ -264,5 +282,142 @@ const std::shared_ptr DMCommTool::GetDMTransportPtr() { return this->dmTransportPtr_; } + +int32_t DMCommTool::CreateUserStopMessage(int32_t stopUserId, std::string &msgStr) +{ + cJSON *root = cJSON_CreateObject(); + if (root == nullptr) { + LOGE("Create cJSON object failed."); + return ERR_DM_FAILED; + } + cJSON *numberObj = cJSON_CreateNumber(stopUserId); + if (numberObj == nullptr) { + cJSON_Delete(root); + return ERR_DM_FAILED; + } + cJSON_AddItemToObject(root, USER_STOP_MSG_KEY, numberObj); + char *msg = cJSON_PrintUnformatted(root); + if (msg == nullptr) { + cJSON_Delete(numberObj); + cJSON_Delete(root); + return ERR_DM_FAILED; + } + msgStr = std::string(msg); + cJSON_free(msg); + cJSON_Delete(root); + return DM_OK; +} + +int32_t DMCommTool::ParseUserStopMessage(const std::string &msgStr, int32_t &stopUserId) +{ + cJSON *root = cJSON_Parse(msgStr.c_str()); + if (root == NULL) { + LOGE("the msg is not json format"); + return ERR_DM_FAILED; + } + cJSON *stopUserIdObj = cJSON_GetObjectItem(root, USER_STOP_MSG_KEY); + if (stopUserIdObj == NULL || !cJSON_IsNumber(stopUserIdObj)) { + LOGE("parse stopUserId id failed."); + cJSON_Delete(root); + return ERR_DM_FAILED; + } + stopUserId = stopUserIdObj->valueint; + cJSON_Delete(root); + return DM_OK; +} + +int32_t DMCommTool::SendUserStop(const std::string rmtNetworkId, int32_t stopUserId) +{ + std::string msgStr; + int32_t ret = CreateUserStopMessage(stopUserId, msgStr); + if (ret != DM_OK) { + LOGE("error ret: %{public}d", ret); + return ret; + } + return SendMsg(rmtNetworkId, DM_COMM_SEND_USER_STOP, msgStr); +} + +int32_t DMCommTool::SendMsg(const std::string rmtNetworkId, int32_t msgType, const std::string &msg) +{ + if (!IsIdLengthValid(rmtNetworkId) || dmTransportPtr_ == nullptr) { + LOGE("param invalid, networkId: %{public}s", GetAnonyString(rmtNetworkId).c_str()); + 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; + } + CommMsg commMsg(msgType, msg); + std::string payload = GetCommMsgString(commMsg); + int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("SendMsg failed, ret: %{public}d", ret); + return ERR_DM_FAILED; + } + LOGI("SendMsg success"); + return DM_OK; +} + +void DMCommTool::ProcessReceiveUserStopEvent(const std::shared_ptr commMsg) +{ + LOGI("start"); + std::string rmtUdid = ""; + SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); + if (rmtUdid.empty()) { + LOGE("Can not find remote udid by networkid: %{public}s", GetAnonyString(commMsg->remoteNetworkId).c_str()); + return; + } + int32_t stopUserId; + int32_t ret = ParseUserStopMessage(commMsg->commMsg->msg, stopUserId); + if (ret != DM_OK) { + LOGE("ParseUserStopMessage error ret: %{public}d", ret); + return; + } + RspUserStop(commMsg->remoteNetworkId, commMsg->socketId, stopUserId); + DeviceManagerService::GetInstance().HandleUserStop(stopUserId, rmtUdid); +} + +void DMCommTool::RspUserStop(const std::string rmtNetworkId, int32_t socketId, int32_t stopUserId) +{ + std::string msgStr; + int32_t ret = CreateUserStopMessage(stopUserId, msgStr); + if (ret != DM_OK) { + LOGE("error ret: %{public}d", ret); + return; + } + CommMsg commMsg(DM_COMM_RSP_USER_STOP, msgStr); + std::string payload = GetCommMsgString(commMsg); + ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("failed, ret: %{public}d", ret); + return; + } + LOGI("success"); +} + +void DMCommTool::ProcessResponseUserStopEvent(const std::shared_ptr commMsg) +{ + LOGI("start"); + 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; + } + int32_t stopUserId; + int32_t ret = ParseUserStopMessage(commMsg->commMsg->msg, stopUserId); + if (ret != DM_OK) { + LOGE("ParseUserStopMessage error ret: %{public}d", ret); + return; + } + std::vector acceptEventUdids; + acceptEventUdids.push_back(rmtUdid); + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid = static_cast(localDeviceId); + DeviceManagerService::GetInstance().HandleUserStop(stopUserId, localUdid, acceptEventUdids); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index c2117f939..4c9a46be5 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.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 @@ -50,6 +50,7 @@ namespace { * | userid lower 2 bytes | */ const int32_t DEL_USER_PAYLOAD_LEN = 2; + const int32_t STOP_USER_PAYLOAD_LEN = 2; /** * @brief the userid payload cost 2 bytes. * @@ -127,6 +128,10 @@ bool RelationShipChangeMsg::ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) con ToDelUserPayLoad(msg, len); ret = true; break; + case RelationShipChangeType::STOP_USER: + ToStopUserPayLoad(msg, len); + ret = true; + break; default: LOGE("RelationShipChange type invalid"); break; @@ -158,6 +163,9 @@ bool RelationShipChangeMsg::FromBroadcastPayLoad(const cJSON *payloadJson, Relat case RelationShipChangeType::DEL_USER: ret = FromDelUserPayLoad(payloadJson); break; + case RelationShipChangeType::STOP_USER: + ret = FromStopUserPayLoad(payloadJson); + break; default: LOGE("RelationShipChange type invalid"); break; @@ -181,6 +189,9 @@ bool RelationShipChangeMsg::IsValid() const case RelationShipChangeType::DEL_USER: ret = (userId != UINT32_MAX); break; + case RelationShipChangeType::STOP_USER: + ret = (userId != UINT32_MAX); + break; case RelationShipChangeType::SERVICE_UNBIND: case RelationShipChangeType::APP_UNINSTALL: // current NOT support @@ -204,7 +215,7 @@ bool RelationShipChangeMsg::IsChangeTypeValid() { return (type == RelationShipChangeType::ACCOUNT_LOGOUT) || (type == RelationShipChangeType::DEVICE_UNBIND) || (type == RelationShipChangeType::APP_UNBIND) || (type == RelationShipChangeType::SYNC_USERID) || - (type == RelationShipChangeType::DEL_USER); + (type == RelationShipChangeType::DEL_USER) || (type == RelationShipChangeType::STOP_USER); } bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type) @@ -213,7 +224,8 @@ bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type) (type == (uint32_t)RelationShipChangeType::DEVICE_UNBIND) || (type == (uint32_t)RelationShipChangeType::APP_UNBIND) || (type == (uint32_t)RelationShipChangeType::SYNC_USERID) || - (type == (uint32_t)RelationShipChangeType::DEL_USER); + (type == (uint32_t)RelationShipChangeType::DEL_USER) || + (type == (uint32_t)RelationShipChangeType::STOP_USER); } void RelationShipChangeMsg::ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const @@ -300,6 +312,15 @@ void RelationShipChangeMsg::ToDelUserPayLoad(uint8_t *&msg, uint32_t &len) const } } +void RelationShipChangeMsg::ToStopUserPayLoad(uint8_t *&msg, uint32_t &len) const +{ + len = STOP_USER_PAYLOAD_LEN; + msg = new uint8_t[STOP_USER_PAYLOAD_LEN](); + for (int i = 0; i < STOP_USER_PAYLOAD_LEN; i++) { + msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF; + } +} + bool RelationShipChangeMsg::FromAccountLogoutPayLoad(const cJSON *payloadJson) { if (payloadJson == NULL) { @@ -469,6 +490,29 @@ bool RelationShipChangeMsg::FromDelUserPayLoad(const cJSON *payloadJson) return true; } +bool RelationShipChangeMsg::FromStopUserPayLoad(const cJSON *payloadJson) +{ + if (payloadJson == NULL) { + LOGE("FromStopUserPayLoad payloadJson is null."); + return false; + } + + int32_t arraySize = cJSON_GetArraySize(payloadJson); + if (arraySize < STOP_USER_PAYLOAD_LEN) { + LOGE("Payload invalid, the size is %{public}d.", arraySize); + return false; + } + this->userId = 0; + for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) { + cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i); + CHECK_NULL_RETURN(payloadItem, false); + if (cJSON_IsNumber(payloadItem)) { + this->userId |= (static_cast(payloadItem->valueint)) << (i * BITS_PER_BYTE); + } + } + return true; +} + cJSON *RelationShipChangeMsg::ToPayLoadJson() const { uint8_t *payload = nullptr; diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index 40bc76691..1603bc0a9 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.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 @@ -1752,7 +1752,7 @@ HWTEST_F(DeviceManagerServiceImplTest, GetDeviceIdAndUserId_001, testing::ext::T std::vector foregroundUserIds; std::vector backgroundUserIds; std::string remoteUdid = "deviceId"; - deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid); + deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false); 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 36673ce13..eece6ae4e 100644 --- a/test/unittest/UTTest_device_manager_service_impl_first.cpp +++ b/test/unittest/UTTest_device_manager_service_impl_first.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 @@ -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); + deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false); 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); + deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false); } } // namespace } // namespace DistributedHardware -- Gitee