From 14babb772bee40d0c5d60e541d4504bcad1ae93b Mon Sep 17 00:00:00 2001 From: BrainL Date: Thu, 22 May 2025 11:39:57 +0800 Subject: [PATCH 1/5] sycn acl Signed-off-by: BrainL --- .../include/deviceprofile_connector.h | 2 + .../src/deviceprofile_connector.cpp | 37 ++ .../include/device_manager_service_impl.h | 13 +- .../device_manager_service_impl_lite.h | 4 +- .../src/device_manager_service_impl.cpp | 131 ++++++- .../src/device_manager_service_impl_lite.cpp | 19 +- .../service/include/device_manager_service.h | 23 ++ .../include/idevice_manager_service_impl.h | 8 +- .../relationshipsyncmgr/dm_comm_tool.h | 12 + .../relationshipsyncmgr/dm_transport_msg.h | 24 ++ .../relationship_sync_mgr.h | 2 + .../service/src/device_manager_service.cpp | 344 ++++++++++++++++-- .../src/relationshipsyncmgr/dm_comm_tool.cpp | 223 ++++++++++++ .../relationshipsyncmgr/dm_transport_msg.cpp | 69 ++++ .../relationship_sync_mgr.cpp | 72 +++- 15 files changed, 947 insertions(+), 36 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 791a29c72..85b626dae 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -206,6 +206,8 @@ public: DM_EXPORT int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid, const std::string &udid, uint64_t &tokenId); std::map GetDeviceIdAndBindLevel(std::vector userIds, const std::string &localUdid); + std::vector GetDeviceIdAndUdidListByTokenId(std::vector userIds, + const std::string &localUdid, int32_t tokenId); DM_EXPORT std::multimap GetDeviceIdAndUserId( int32_t userId, const std::string &accountId, const std::string &localUdid); int32_t HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index e81ec4c19..56983150b 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1807,6 +1807,43 @@ std::map DeviceProfileConnector::GetDeviceIdAndBindLevel(s return deviceIdMap; } +std::vector DeviceProfileConnector::GetDeviceIdAndUdidListByTokenId(std::vector userIds, + const std::string &localUdid, int32_t tokenId) +{ + std::vector profiles = GetAllAccessControlProfile(); + std::map deviceIdMap; + std::vector udidList; + + for (const auto &item : profiles) { + if (IsLnnAcl(item)) { + continue; + } + + if (find(userIds.begin(), userIds.end(), item.GetAccesser().GetAccesserUserId()) != userIds.end() && + item.GetAccesser().GetAccesserDeviceId() == localUdid && + static_cast(item.GetAccesser().GetAccesserTokenId()) == tokenId) { + LOGI("Get Device Bind type localUdid %{public}s is src, tokenId %{public}d.", + GetAnonyString(localUdid).c_str(), tokenId); + UpdateBindType(item.GetTrustDeviceId(), item.GetBindLevel(), deviceIdMap); + continue; + } + + if (find(userIds.begin(), userIds.end(), item.GetAccessee().GetAccesseeUserId()) != userIds.end() && + item.GetAccessee().GetAccesseeDeviceId() == localUdid && + static_cast(item.GetAccessee().GetAccesseeTokenId()) == tokenId) { + LOGI("Get Device Bind type localUdid %{public}s is sink, tokenId %{public}d.", + GetAnonyString(localUdid).c_str(), tokenId); + UpdateBindType(item.GetTrustDeviceId(), item.GetBindLevel(), deviceIdMap); + continue; + } + } + + for (const auto &item : deviceIdMap) { + udidList.push_back(item.first); + } + return udidList; +} + DM_EXPORT std::multimap DeviceProfileConnector::GetDeviceIdAndUserId( int32_t userId, const std::string &accountId, const std::string &localUdid) { diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 132e913fa..3c801097d 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -165,6 +165,8 @@ public: int32_t GetAclListHash(const DevUserInfo &localDevUserInfo, const DevUserInfo &remoteDevUserInfo, std::string &aclList); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); + int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId); + void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, std::string extra, std::string udid); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); void HandleUserSwitched(const std::vector &deviceVec, int32_t currentUserId, @@ -177,7 +179,7 @@ public: void DeleteAlwaysAllowTimeOut(); void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId); void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, - std::string &remoteUdid); + std::string &remoteUdid, bool &isShareType); void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, const std::string &localUdid); int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); int32_t DeleteAcl(const std::string &sessionName, const std::string &localUdid, const std::string &remoteUdid, @@ -203,6 +205,8 @@ private: void HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo); bool CheckSharePeerSrc(const std::string &peerUdid, const std::string &localUdid); std::map GetDeviceIdAndBindLevel(int32_t userId); + std::vector GetDeviceIdByUserIdAndTokenId(int32_t userId, + int32_t tokenId); std::multimap GetDeviceIdAndUserId(int32_t userId, const std::string &accountId); void HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, const std::string &remoteUdid); @@ -257,6 +261,13 @@ private: std::vector &profiles, std::map &delProfileMap, std::vector> &delACLInfoVec, std::vector &userIdVec); + + void DeleteAclByTokenId(const int32_t &accessTokenId, + std::vector &profiles, + std::map &delProfileMap, + std::vector> &delACLInfoVec, std::vector &userIdVec, + uint32_t userId, std::string localUdid); + bool CheckLnnAcl(DistributedDeviceProfile::AccessControlProfile delProfile, DistributedDeviceProfile::AccessControlProfile lastprofile); void CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 43f5aada8..6a20f775d 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -151,6 +151,8 @@ public: int32_t GetAclListHash(const DevUserInfo &localDevUserInfo, const DevUserInfo &remoteDevUserInfo, std::string &aclList); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); + int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId); + void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, std::string extra, std::string udid); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); void HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid); @@ -163,7 +165,7 @@ public: void DeleteAlwaysAllowTimeOut(); void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId); void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, - std::string &remoteUdid); + std::string &remoteUdid, bool &isShareType); void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, const std::string &localUdid); int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); void HandleServiceUnBindEvent(int32_t userId, const std::string &remoteUdid, diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 938587ce2..e91ac1d8c 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -1902,6 +1902,17 @@ std::map DeviceManagerServiceImpl::GetDeviceIdAndBindLevel return DeviceProfileConnector::GetInstance().GetDeviceIdAndBindLevel(userIds, localUdid); } +std::vector DeviceManagerServiceImpl::GetDeviceIdByUserIdAndTokenId(int32_t userId, + int32_t tokenId) +{ + char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localUdidTemp); + std::vector userIds; + userIds.push_back(userId); + return DeviceProfileConnector::GetInstance().GetDeviceIdAndUdidListByTokenId(userIds, localUdid, tokenId); +} + std::multimap DeviceManagerServiceImpl::GetDeviceIdAndUserId(int32_t userId, const std::string &accountId) { @@ -2033,7 +2044,9 @@ void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId, int32_t peerTokenId) { - LOGI("peerTokenId = %{public}d.", peerTokenId); + LOGI("DeviceManagerServiceImpl::HandleAppUnBindEvent remoteUserId: %{public}s, remoteUdid: %{public}s, " + "tokenId = %{public}s, peerTokenId = %{public}s.",GetAnonyInt32(remoteUserId).c_str(), + GetAnonyString(remoteUdid).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyInt32(peerTokenId).c_str()); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); std::string localUdid = std::string(localUdidTemp); @@ -2219,6 +2232,66 @@ int32_t DeviceManagerServiceImpl::ProcessAppUnintall(const std::string &appId, i return DM_OK; } +int32_t DeviceManagerServiceImpl::ProcessAppUninstall(int32_t userId, int32_t accessTokenId) +{ + LOGE("DeviceManagerServiceImpl::ProcessAppUninstall userId = %{public}s, accessTokenId = %{public}s.", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(accessTokenId).c_str()); + CHECK_NULL_RETURN(listener_, ERR_DM_POINT_NULL); + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAllAclIncludeLnnAcl(); + LOGI("delete ACL size is %{public}zu", profiles.size()); + if (profiles.size() == 0) { + return DM_OK; + } + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localDeviceId); + std::vector> delACLInfoVec; + std::vector userIdVec; + std::map delProfileMap; + DeleteAclByTokenId(accessTokenId, profiles, delProfileMap, delACLInfoVec, userIdVec, userId, localUdid); + for (auto item : delProfileMap) { + DmOfflineParam lnnAclParam; + bool isLastLnnAcl = false; + for (auto it : profiles) { + CheckIsLastLnnAcl(it, item.second, lnnAclParam, isLastLnnAcl, localUdid); + } + if (!isLastLnnAcl) { + DeleteSkCredAndAcl(lnnAclParam.needDelAclInfos); + } + } + + if (delACLInfoVec.size() == 0) { + return DM_OK; + } + if (userIdVec.size() == 0) { + return DM_OK; + } + CHECK_NULL_RETURN(hiChainConnector_, ERR_DM_POINT_NULL); + hiChainConnector_->DeleteGroupByACL(delACLInfoVec, userIdVec); + return DM_OK; +} + +void DeviceManagerServiceImpl::ProcessUnBindApp(int32_t userId, int32_t accessTokenId, + std::string extra, std::string udid) +{ + LOGE("DeviceManagerServiceImpl::ProcessUnBindApp userId = %{public}s, accessTokenId = %{public}s," + "extra = %{public}s.", GetAnonyInt32(userId).c_str(), GetAnonyInt32(accessTokenId).c_str(), + GetAnonyString(extra).c_str()); + + JsonObject extraInfoJson(extra); + if (extraInfoJson.IsDiscarded()) { + LOGE("ParseExtra extraInfoJson error"); + return; + } + if (!extraInfoJson[TAG_PEER_TOKENID].IsNumberInteger()) { + HandleAppUnBindEvent(userId, udid, accessTokenId); + return; + } + uint64_t peerTokenId = extraInfoJson[TAG_PEER_TOKENID].Get(); + HandleAppUnBindEvent(userId, udid, accessTokenId, static_cast(peerTokenId)); +} + void DeviceManagerServiceImpl::CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, DistributedDeviceProfile::AccessControlProfile delProfile, DmOfflineParam &lnnAclParam, bool &isLastLnnAcl, const std::string &localUdid) @@ -2284,6 +2357,59 @@ void DeviceManagerServiceImpl::DeleteAclByTokenId(const int32_t accessTokenId, } } +void DeviceManagerServiceImpl::DeleteAclByTokenId(const int32_t &accessTokenId, + std::vector &profiles, + std::map &delProfileMap, + std::vector> &delACLInfoVec, std::vector &userIdVec, + uint32_t userId, std::string localUdid) +{ + for (auto &item : profiles) { + int64_t accesssertokenId = item.GetAccesser().GetAccesserTokenId(); + int64_t accessseetokenId = item.GetAccessee().GetAccesseeTokenId(); + if (accessTokenId != static_cast(accesssertokenId) && + accessTokenId != static_cast(accessseetokenId)) { + continue; + } + if (accessTokenId == static_cast(accesssertokenId) && + userId == item.GetAccesser().GetAccesserUserId() && + localUdid == item.GetAccessee().GetAccesseeDeviceId()) { + DmOfflineParam offlineParam; + delProfileMap[item.GetAccessControlId()] = item; + DeviceProfileConnector::GetInstance().CacheAcerAclId(item, offlineParam.needDelAclInfos); + DeleteSkCredAndAcl(offlineParam.needDelAclInfos); + listener_->OnAppUnintall(item.GetAccesser().GetAccesserBundleName()); + if (item.GetBindLevel() == USER) { + userIdVec.push_back(item.GetAccesser().GetAccesserUserId()); + delACLInfoVec.push_back(std::pair(item.GetAccesser().GetAccesserUserId(), + item.GetAccessee().GetAccesseeDeviceId())); + } + } + if (accessTokenId == static_cast(accessseetokenId) && + userId == item.GetAccessee().GetAccesseeUserId() && + localUdid == item.GetAccesser().GetAccesserDeviceId()) { + DmOfflineParam offlineParam; + DeviceProfileConnector::GetInstance().CacheAceeAclId(item, offlineParam.needDelAclInfos); + delProfileMap[item.GetAccessControlId()] = item; + DeleteSkCredAndAcl(offlineParam.needDelAclInfos); + listener_->OnAppUnintall(item.GetAccessee().GetAccesseeBundleName()); + if (item.GetBindLevel() == USER) { + userIdVec.push_back(item.GetAccessee().GetAccesseeUserId()); + delACLInfoVec.push_back(std::pair(item.GetAccessee().GetAccesseeUserId(), + item.GetAccesser().GetAccesserDeviceId())); + } + } + } + for (auto item : delProfileMap) { + for (auto it = profiles.begin(); it != profiles.end();) { + if (item.first == it->GetAccessControlId()) { + it = profiles.erase(it); + } else { + it++; + } + } + } +} + bool DeviceManagerServiceImpl::CheckLnnAcl(DistributedDeviceProfile::AccessControlProfile delProfile, DistributedDeviceProfile::AccessControlProfile lastprofile) { @@ -2392,7 +2518,7 @@ void DeviceManagerServiceImpl::CheckDeleteCredential(const std::string &remoteUd } void DeviceManagerServiceImpl::HandleCredentialDeleted(const char *credId, const char *credInfo, - const std::string &localUdid, std::string &remoteUdid) + const std::string &localUdid, std::string &remoteUdid, bool &isShareType) { std::vector profiles = DeviceProfileConnector::GetInstance().GetAccessControlProfile(); @@ -2427,6 +2553,7 @@ void DeviceManagerServiceImpl::HandleCredentialDeleted(const char *credId, const item.GetAccessee().GetAccesseeUserId() == localUserId && item.GetAccesser().GetAccesserUserId() == userId && item.GetAccesser().GetAccesserDeviceId() == remoteUdid)) { + isShareType = true; DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId()); } } diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 057214392..6685f8573 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -331,12 +331,13 @@ int32_t DeviceManagerServiceImpl::ImportAuthCode(const std::string &pkgName, con } void DeviceManagerServiceImpl::HandleCredentialDeleted(const char *credId, const char *credInfo, - const std::string &localUdid, std::string &remoteUdid) + const std::string &localUdid, std::string &remoteUdid, bool &isShareType) { (void)credId; (void)credInfo; (void)localUdid; (void)remoteUdid; + (void)isShareType; return; } @@ -570,6 +571,22 @@ int32_t DeviceManagerServiceImpl::ProcessAppUnintall(const std::string &appId, i return 0; } +int32_t DeviceManagerServiceImpl::ProcessAppUninstall(int32_t userId, int32_t accessTokenId) +{ + (void)userId; + (void)accessTokenId; + return 0; +} + +void DeviceManagerServiceImpl::ProcessUnBindApp(int32_t userId, int32_t accessTokenId, std::string extra, std::string udid) +{ + (void)userId; + (void)accessTokenId; + (void)extra; + (void)udid; + return; +} + void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) { diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 1c80fe67e..eed2bdedb 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -215,6 +215,10 @@ public: const DevUserInfo &remoteDevUserInfo, std::string &aclList); void ProcessSyncUserIds(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid); + + void ProcessUninstApp(int32_t userId, int32_t tokenId); + void ProcessUnBindApp(int32_t userId, int32_t tokenId, std::string extra, std::string udid); + int32_t SetLocalDisplayNameToSoftbus(const std::string &displayName); void RemoveNotifyRecord(const ProcessInfo &processInfo); int32_t RegDevStateCallbackToService(const std::string &pkgName); @@ -245,6 +249,8 @@ public: std::vector &networkIds); void ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, int32_t userId); int32_t UnRegisterPinHolderCallback(const std::string &pkgName); + void ProcessReceiveRspAppUninstall(const std::string &remoteUdid); + void ProcessReceiveRspAppUnbind(const std::string &remoteUdid); void ProcessCommonUserStatusEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid); int32_t GetLocalDeviceName(std::string &deviceName); @@ -264,6 +270,8 @@ private: void SendAppUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId); void SendAppUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId, uint64_t peerTokenId); + void SendAppUnInstallBroadCast(const std::vector &peerUdids, int32_t userId, + uint64_t tokenId); void SendServiceUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId); void SendAccountLogoutBroadCast(const std::vector &peerUdids, const std::string &accountId, const std::string &accountName, int32_t userId); @@ -333,6 +341,19 @@ private: void InitServiceInfos(const std::vector &dpLocalServiceItems, std::vector &serviceInfos); void HandleUserSwitched(); + + void NotifyRemoteUninstallApp(int32_t userId, int32_t tokenId); + void NotifyRemoteUninstallAppByWifi(int32_t userId, int32_t tokenId, + const std::map &wifiDevices); + int32_t SendUninstAppByWifi(int32_t userId, int32_t tokenId, const std::string &networkId); + + void GetNotifyRemoteUnBindAppWay(int32_t userId, int32_t tokenId, + std::map &wifiDevices, bool &isBleWay); + void NotifyRemoteUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, + const std::map &wifiDevices); + int32_t SendUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, + const std::string &networkId, const std::string &udid); + void NotifyRemoteLocalUserSwitch(const std::string &localUdid, const std::vector &peerUdids, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); void NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, @@ -411,6 +432,8 @@ private: std::shared_ptr screenCommonEventManager_; std::vector foregroundUserVec_; std::vector backgroundUserVec_; + std::mutex unInstallLock_; + int64_t SendLastBroadCastTime_ = 0; #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) std::shared_ptr publshCommonEventManager_; #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 29291117e..81ef11af4 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -252,10 +252,16 @@ public: virtual int32_t GetAclListHash(const DevUserInfo &localDevUserInfo, const DevUserInfo &remoteDevUserInfo, std::string &aclList) = 0; virtual int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId) = 0; + virtual int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId) = 0; + virtual void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, + std::string extra, std::string udid) = 0; + virtual void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, 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::vector GetDeviceIdByUserIdAndTokenId(int32_t userId, + int32_t tokenId) = 0; virtual std::multimap GetDeviceIdAndUserId(int32_t localUserId) = 0; virtual int32_t SaveOnlineDeviceInfo(const std::vector &deviceList) = 0; virtual void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, @@ -264,7 +270,7 @@ public: virtual void DeleteAlwaysAllowTimeOut() = 0; virtual void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId) = 0; virtual void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, - std::string &remoteUdid) = 0; + std::string &remoteUdid, bool &isShareType) = 0; virtual void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, const std::string &localUdid) = 0; virtual int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId) = 0; diff --git a/services/service/include/relationshipsyncmgr/dm_comm_tool.h b/services/service/include/relationshipsyncmgr/dm_comm_tool.h index 0ee63517b..6fa98f02d 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.h @@ -36,6 +36,11 @@ public: int32_t SendUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + + int32_t SendUninstAppObj(int32_t userId, int32_t tokenId, const std::string &networkId); + + int32_t SendUnBindAppObj(int32_t userId, int32_t tokenId, std::string extra, const std::string &networkId, const std::string &udid); + 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); @@ -45,6 +50,11 @@ 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 RspAppUninstall(const std::string rmtNetworkId, int32_t socketId); + int32_t RspAppUnbind(const std::string rmtNetworkId, int32_t socketId); + void StopSocket(const std::string &networkId); + void ProcessReceiveRspAppUninstallEvent(const std::shared_ptr &commMsg); + void ProcessReceiveRspAppUnbindEvent(const std::shared_ptr &commMsg); class DMCommToolEventHandler : public AppExecFwk::EventHandler { public: @@ -66,6 +76,8 @@ public: int32_t StartCommonEvent(std::string commonEventType, EventCallback eventCallback); void ProcessReceiveCommonEvent(const std::shared_ptr commMsg); void ProcessResponseCommonEvent(const std::shared_ptr commMsg); + void ProcessReceiveUninstAppEvent(const std::shared_ptr &commMsg); + void ProcessReceiveUnBindAppEvent(const std::shared_ptr &commMsg); private: std::shared_ptr dmTransportPtr_; std::shared_ptr eventHandler_; diff --git a/services/service/include/relationshipsyncmgr/dm_transport_msg.h b/services/service/include/relationshipsyncmgr/dm_transport_msg.h index 74230997a..2d36fc108 100644 --- a/services/service/include/relationshipsyncmgr/dm_transport_msg.h +++ b/services/service/include/relationshipsyncmgr/dm_transport_msg.h @@ -76,6 +76,30 @@ struct LogoutAccountMsg { void ToJson(cJSON *jsonObject, const LogoutAccountMsg &accountInfo); void FromJson(const cJSON *jsonObject, LogoutAccountMsg &accountInfo); + +struct UninstAppMsg { + int32_t userId_; + int32_t tokenId_; + UninstAppMsg() : userId_(-1), tokenId_(-1) {} + UninstAppMsg(const int32_t &userId,const int32_t &tokenId) + : userId_(userId), tokenId_(tokenId) {} +}; + +void ToJson(cJSON *jsonObject, const UninstAppMsg &uninstAppMsg); +void FromJson(const cJSON *jsonObject, UninstAppMsg &uninstAppMsg); + +struct UnBindAppMsg { + int32_t userId_; + int32_t tokenId_; + std::string extra_; + std::string udid_; + UnBindAppMsg() : userId_(-1), tokenId_(-1), extra_(""), udid_("") {} + UnBindAppMsg(const int32_t &userId, const int32_t &tokenId, const std::string &extra, const std::string &udid) + : userId_(userId), tokenId_(tokenId), extra_(extra), udid_(udid) {} +}; + +void ToJson(cJSON *jsonObject, const UnBindAppMsg &unBindAppMsg); +void FromJson(const cJSON *jsonObject, UnBindAppMsg &unBindAppMsg); } // DistributedHardware } // OHOS #endif \ No newline at end of file diff --git a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h index 1c9279a69..2d0591a97 100644 --- a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h +++ b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h @@ -75,6 +75,7 @@ struct RelationShipChangeMsg { void ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const; void ToDeviceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; void ToAppUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; + void ToAppUninstallPayLoad(uint8_t *&msg, uint32_t &len) const; void ToServiceUnbindPayLoad(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; @@ -90,6 +91,7 @@ struct RelationShipChangeMsg { bool FromDelUserPayLoad(const cJSON *payloadJson); bool FromStopUserPayLoad(const cJSON *payloadJson); bool FromShareUnbindPayLoad(const cJSON *payloadJson); + bool FromAppUninstallPayLoad(const cJSON *payloadJson); bool GetBroadCastId(const cJSON *payloadJson, uint32_t userIdNum); std::string ToJson() const; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 010515b1b..760934b1f 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -85,8 +85,12 @@ namespace { 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"; + constexpr const char* APP_UNINSTALL_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:appUninstallByWifi"; + constexpr const char* APP_UNBIND_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:appUnbindByWifi"; constexpr const char* ACCOUNT_COMMON_EVENT_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:accountCommonEventByWifi"; const int32_t USER_SWITCH_BY_WIFI_TIMEOUT_S = 2; + const int32_t SEND_DELAY_MAX_TIME = 5; + const int32_t SEND_DELAY_MIN_TIME = 0; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG) const std::string GET_LOCAL_DEVICE_NAME_API_NAME = "GetLocalDeviceName"; #endif @@ -785,6 +789,8 @@ int32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t aut int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash) { + LOGE("DeviceManagerService::UnBindDevice pkgName: %{public}s, udidHash: %{public}s", + pkgName.c_str(), GetAnonyString(udidHash).c_str()); if (!PermissionManager::GetInstance().CheckNewPermission()) { LOGE("The caller does not have permission to call UnBindDevice."); return ERR_DM_NO_PERMISSION; @@ -827,7 +833,16 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::vector peerUdids; peerUdids.emplace_back(udid); - SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, bindLevel); + + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::map wifiDevices; + bool isBleActive = false; + GetNotifyRemoteUnBindAppWay(userId, tokenId, wifiDevices, isBleActive); + if (isBleActive) { + SendUnBindBroadCast(peerUdids, userId, tokenId, bindLevel); + } else { + NotifyRemoteUnBindAppByWifi(userId, tokenId, "", wifiDevices); + } #endif return DM_OK; } @@ -835,6 +850,8 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash, const std::string &extra) { + LOGE("DeviceManagerService::UnBindDevice pkgName: %{public}s, udidHash: %{public}s, extra: %{public}s", + GetAnonyString(pkgName).c_str(), GetAnonyString(udidHash).c_str(), GetAnonyString(extra).c_str()); if (!PermissionManager::GetInstance().CheckNewPermission()) { LOGE("The caller does not have permission to call UnBindDevice."); return ERR_DM_NO_PERMISSION; @@ -871,16 +888,26 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std return ERR_DM_FAILED; } [[maybe_unused]] uint64_t peerTokenId = dmServiceImpl_->GetTokenIdByNameAndDeviceId(extra, udid); - if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel, extra) != DM_OK) { - LOGE("dmServiceImpl_ UnBindDevice failed."); - return ERR_DM_FAILED; - } + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::vector peerUdids; peerUdids.emplace_back(udid); - SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, - bindLevel, peerTokenId); + + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::map wifiDevices; + bool isBleActive = false; + GetNotifyRemoteUnBindAppWay(userId, tokenId, wifiDevices, isBleActive); + if (isBleActive) { + SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, + bindLevel, peerTokenId); + } else { + NotifyRemoteUnBindAppByWifi(userId, tokenId, extra, wifiDevices); + } #endif + if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel, extra) != DM_OK) { + LOGE("dmServiceImpl_ UnBindDevice failed."); + return ERR_DM_FAILED; + } return DM_OK; } @@ -1941,7 +1968,6 @@ void DeviceManagerService::SendShareTypeUnBindBroadCast(const char *credId, cons msg.credId = credId; msg.peerUdids = peerUdids; std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); - LOGI("broadCastMsg = %{public}s.", broadCastMsg.c_str()); CHECK_NULL_VOID(softbusListener_); softbusListener_->SendAclChangedBroadcast(broadCastMsg); } @@ -2547,6 +2573,24 @@ void DeviceManagerService::ProcessSyncUserIds(const std::vector &foreg } } +void DeviceManagerService::ProcessUninstApp(int32_t userId, int32_t tokenId) +{ + LOGE("DeviceManagerService::ProcessUninstApp userId: %{public}s, tokenId: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str()); + if (IsDMServiceImplReady()) { + dmServiceImpl_->ProcessAppUninstall(userId, tokenId); + } +} + +void DeviceManagerService::ProcessUnBindApp(int32_t userId, int32_t tokenId, std::string extra, std::string udid) +{ + LOGE("DeviceManagerService::ProcessUnBindApp userId: %{public}s, tokenId: %{public}s, udid: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(udid).c_str()); + if (IsDMServiceImplReady()) { + dmServiceImpl_->ProcessUnBindApp(userId, tokenId, extra, udid); + } +} + void DeviceManagerService::SendCommonEventBroadCast(const std::vector &peerUdids, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, bool isNeedResponse) { @@ -2809,28 +2853,91 @@ void DeviceManagerService::SendDeviceUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId) { - RelationShipChangeMsg msg; - msg.type = RelationShipChangeType::APP_UNBIND; - msg.userId = static_cast(userId); - msg.peerUdids = peerUdids; - msg.tokenId = tokenId; - std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); - CHECK_NULL_VOID(softbusListener_); - softbusListener_->SendAclChangedBroadcast(broadCastMsg); + int64_t currentTime = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + if (SendLastBroadCastTime_ == 0) { + SendLastBroadCastTime_ = currentTime; + } + int64_t timeDiff = currentTime - SendLastBroadCastTime_; + int32_t delayTime = SEND_DELAY_MAX_TIME - timeDiff; + if (delayTime < SEND_DELAY_MIN_TIME || delayTime == SEND_DELAY_MAX_TIME) { + delayTime = SEND_DELAY_MIN_TIME; + } + LOGI("SendAppUnBindBroadCast delayTime: %{public}d, currentTime = %{public}lld, SendLastBroadCastTime_ = %{public}lld.", + delayTime, currentTime, SendLastBroadCastTime_); + std::function task = [=]() { + LOGI("SendAppUnBindBroadCast Start."); + RelationShipChangeMsg msg; + msg.type = RelationShipChangeType::APP_UNBIND; + msg.userId = static_cast(userId); + msg.peerUdids = peerUdids; + msg.tokenId = tokenId; + std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); + CHECK_NULL_VOID(softbusListener_); + softbusListener_->SendAclChangedBroadcast(broadCastMsg); + }; + ffrt::submit(task, ffrt::task_attr().delay(delayTime * 1000 * 1000)); + SendLastBroadCastTime_ = currentTime; } void DeviceManagerService::SendAppUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId, uint64_t peerTokenId) { - RelationShipChangeMsg msg; - msg.type = RelationShipChangeType::APP_UNBIND; - msg.userId = static_cast(userId); - msg.peerUdids = peerUdids; - msg.tokenId = tokenId; - msg.peerTokenId = peerTokenId; - std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); - CHECK_NULL_VOID(softbusListener_); - softbusListener_->SendAclChangedBroadcast(broadCastMsg); + int64_t currentTime = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + if (SendLastBroadCastTime_ == 0) { + SendLastBroadCastTime_ = currentTime; + } + int64_t timeDiff = currentTime - SendLastBroadCastTime_; + int32_t delayTime = SEND_DELAY_MAX_TIME - timeDiff; + if (delayTime < SEND_DELAY_MIN_TIME || delayTime == SEND_DELAY_MAX_TIME) { + delayTime = SEND_DELAY_MIN_TIME; + } + LOGI("SendAppUnBindBroadCast delayTime: %{public}d, currentTime = %{public}lld, SendLastBroadCastTime_ = %{public}lld.", + delayTime, currentTime, SendLastBroadCastTime_); + std::function task = [=]() { + LOGI("SendAppUnBindBroadCast Start."); + RelationShipChangeMsg msg; + msg.type = RelationShipChangeType::APP_UNBIND; + msg.userId = static_cast(userId); + msg.peerUdids = peerUdids; + msg.tokenId = tokenId; + msg.peerTokenId = peerTokenId; + std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); + CHECK_NULL_VOID(softbusListener_); + softbusListener_->SendAclChangedBroadcast(broadCastMsg); + }; + ffrt::submit(task, ffrt::task_attr().delay(delayTime * 1000 * 1000)); + SendLastBroadCastTime_ = currentTime; +} + +void DeviceManagerService::SendAppUnInstallBroadCast(const std::vector &peerUdids, int32_t userId, + uint64_t tokenId) +{ + int64_t currentTime = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + if (SendLastBroadCastTime_ == 0) { + SendLastBroadCastTime_ = currentTime; + } + int64_t timeDiff = currentTime - SendLastBroadCastTime_; + int32_t delayTime = SEND_DELAY_MAX_TIME - timeDiff; + if (delayTime < SEND_DELAY_MIN_TIME || delayTime == SEND_DELAY_MAX_TIME) { + delayTime = SEND_DELAY_MIN_TIME; + } + LOGI("SendAppUnInstallBroadCast delayTime: %{public}d, currentTime = %{public}lld, SendLastBroadCastTime_ = %{public}lld.", delayTime, currentTime, SendLastBroadCastTime_); + std::function task = [=]() { + LOGI("SendAppUnInstallBroadCast Start."); + RelationShipChangeMsg msg; + msg.type = RelationShipChangeType::APP_UNINSTALL; + msg.userId = static_cast(userId); + msg.peerUdids = peerUdids; + msg.tokenId = tokenId; + std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); + CHECK_NULL_VOID(softbusListener_); + softbusListener_->SendAclChangedBroadcast(broadCastMsg); + }; + ffrt::submit(task, ffrt::task_attr().delay(delayTime * 1000 * 1000)); + SendLastBroadCastTime_ = currentTime; } void DeviceManagerService::SendServiceUnBindBroadCast(const std::vector &peerUdids, int32_t userId, @@ -2861,11 +2968,16 @@ void DeviceManagerService::HandleCredentialDeleted(const char *credId, const cha return; } std::string remoteUdid = ""; - dmServiceImpl_->HandleCredentialDeleted(credId, credInfo, localUdid, remoteUdid); + bool isShareType = false; + dmServiceImpl_->HandleCredentialDeleted(credId, credInfo, localUdid, remoteUdid, isShareType); if (remoteUdid.empty()) { LOGE("HandleCredentialDeleted failed, remoteUdid is empty."); return; } + if (!isShareType) { + LOGI("HandleCredentialDeleted not share type."); + return; + } std::vector peerUdids; peerUdids.emplace_back(remoteUdid); SendShareTypeUnBindBroadCast(credId, MultipleUserConnector::GetCurrentAccountUserID(), peerUdids); @@ -2933,6 +3045,9 @@ bool DeviceManagerService::ParseRelationShipChangeType(const RelationShipChangeM case RelationShipChangeType::SHARE_UNBIND: HandleShareUnbindBroadCast(relationShipMsg.userId, relationShipMsg.credId); break; + case RelationShipChangeType::APP_UNINSTALL: + ProcessUninstApp(relationShipMsg.userId, static_cast(relationShipMsg.tokenId)); + break; default: LOGI("Dm have not this event type."); return false; @@ -3138,6 +3253,9 @@ void DeviceManagerService::SubscribePackageCommonEvent() packageCommonEventManager_ = std::make_shared(); } PackageEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) { + std::lock_guard lock(unInstallLock_); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + NotifyRemoteUninstallApp(userId, arg3); if (IsDMServiceImplReady()) { dmServiceImpl_->ProcessAppUnintall(arg1, arg3); } @@ -3152,6 +3270,180 @@ void DeviceManagerService::SubscribePackageCommonEvent() #endif } +void DeviceManagerService::NotifyRemoteUninstallApp(int32_t userId, int32_t tokenId) +{ + LOGE("DeviceManagerService::NotifyRemoteUninstallApp userId: %{public}s, tokenId: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str()); + std::vector peerUdids; + int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID(); + peerUdids = dmServiceImpl_->GetDeviceIdByUserIdAndTokenId(currentUserId, tokenId); + + if (peerUdids.empty()) { + LOGE("peerUdids is empty"); + return; + } + if (softbusListener_ == nullptr) { + LOGE("softbusListener_ is null"); + 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; + } + uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE; + if ((static_cast(networkType) & addrTypeMask) != 0x0) { + bleUdids.push_back(udid); + } else { + wifiDevices.insert(std::pair(udid, netWorkId)); + } + } + if (!bleUdids.empty()) { + SendAppUnInstallBroadCast(peerUdids, userId, tokenId); + } + if (!wifiDevices.empty()) { + NotifyRemoteUninstallAppByWifi(userId, tokenId, wifiDevices); + } +} + +void DeviceManagerService::NotifyRemoteUninstallAppByWifi(int32_t userId, int32_t tokenId, + const std::map &wifiDevices) +{ + LOGE("DeviceManagerService::NotifyRemoteUninstallAppByWifi userId: %{public}s, tokenId: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str()); + for (const auto &it : wifiDevices) { + int32_t result = SendUninstAppByWifi(userId, tokenId, it.second); + if (result != DM_OK) { + LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); + continue; + } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + std::string udid = it.first; + std::string networkId = it.second; + timer_->StartTimer(std::string(APP_UNINSTALL_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), + USER_SWITCH_BY_WIFI_TIMEOUT_S, [this, networkId] (std::string name) { + DMCommTool::GetInstance()->StopSocket(networkId); + }); + } +} + +void DeviceManagerService::NotifyRemoteUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, + const std::map &wifiDevices) +{ + LOGE("DeviceManagerService::NotifyRemoteUnBindAppByWifi userId: %{public}s, tokenId: %{public}s, extra: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str()); + for (const auto &it : wifiDevices) { + int32_t result = SendUnBindAppByWifi(userId, tokenId, extra, it.second, it.first); + if (result != DM_OK) { + LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); + continue; + } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + std::string udid = it.first; + std::string networkId = it.second; + timer_->StartTimer(std::string(APP_UNBIND_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), + USER_SWITCH_BY_WIFI_TIMEOUT_S, [this, networkId] (std::string name) { + DMCommTool::GetInstance()->StopSocket(networkId); + }); + } +} + +void DeviceManagerService::ProcessReceiveRspAppUninstall(const std::string &remoteUdid) +{ + LOGI("ProcessReceiveRspAppUninstall remoteUdid: %{public}s", GetAnonyString(remoteUdid).c_str()); + if (timer_ != nullptr) { + timer_->DeleteTimer(std::string(APP_UNINSTALL_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid)); + } +} + +void DeviceManagerService::ProcessReceiveRspAppUnbind(const std::string &remoteUdid) +{ + LOGI("ProcessReceiveRspAppUnbind remoteUdid: %{public}s", GetAnonyString(remoteUdid).c_str()); + if (timer_ != nullptr) { + timer_->DeleteTimer(std::string(APP_UNBIND_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid)); + } +} + +int32_t DeviceManagerService::SendUninstAppByWifi(int32_t userId, int32_t tokenId, const std::string &networkId) +{ + LOGE("DeviceManagerService::SendUninstAppByWifi userId: %{public}s, tokenId: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str()); + return DMCommTool::GetInstance()->SendUninstAppObj(userId, tokenId, networkId); +} + +int32_t DeviceManagerService::SendUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, + const std::string &networkId, const std::string &udid) +{ + LOGE("DeviceManagerService::SendUnBindAppByWifi"); + return DMCommTool::GetInstance()->SendUnBindAppObj(userId, tokenId, extra, networkId, udid); +} + +void DeviceManagerService::GetNotifyRemoteUnBindAppWay(int32_t userId, int32_t tokenId, + std::map &wifiDevices, bool &isBleWay) +{ + std::vector peerUdids; + int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID(); + std::map deviceMap = dmServiceImpl_->GetDeviceIdAndBindLevel(currentUserId); + for (const auto &item : deviceMap) { + peerUdids.push_back(item.first); + } + if (peerUdids.empty()) { + LOGE("peerUdids is empty"); + return; + } + if (softbusListener_ == nullptr) { + LOGE("softbusListener_ is null"); + return; + } + + std::vector bleUdids; + 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; + } + uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE; + if ((static_cast(networkType) & addrTypeMask) != 0x0) { + bleUdids.push_back(udid); + } else { + wifiDevices.insert(std::pair(udid, netWorkId)); + } + } + + if (!bleUdids.empty()) { + isBleWay = true; + } + if (!wifiDevices.empty()) { + isBleWay = false; + } +} + int32_t DeviceManagerService::SyncLocalAclListProcess(const DevUserInfo &localDevUserInfo, const DevUserInfo &remoteDevUserInfo, std::string remoteAclList) { diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index 6e7f69d1c..1a4edf547 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -35,6 +35,10 @@ 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_LOCAL_APP_UNINSTALL = 6; +constexpr int32_t DM_COMM_SEND_LOCAL_APP_UNBIND = 7; +constexpr int32_t DM_COMM_RSP_APP_UNINSTALL = 8; +constexpr int32_t DM_COMM_RSP_APP_UNBIND = 9; constexpr const char* EVENT_TASK = "EventTask"; const char* const USER_STOP_MSG_KEY = "stopUserId"; @@ -110,6 +114,125 @@ int32_t DMCommTool::SendUserIds(const std::string rmtNetworkId, return DM_OK; } +int32_t DMCommTool::SendUninstAppObj(int32_t userId, int32_t tokenId, const std::string &networkId) +{ + LOGE("SendUninstAppObj, userId: %{public}s, tokenId: %{public}s", GetAnonyInt32(userId).c_str(), + GetAnonyInt32(tokenId).c_str()); + if (!IsIdLengthValid(networkId)) { + LOGE("param invalid, networkId: %{public}s",GetAnonyString(networkId).c_str()); + return ERR_DM_INPUT_PARA_INVALID; + } + int32_t socketId; + if (dmTransportPtr_->StartSocket(networkId, socketId) != DM_OK || socketId <= 0) { + LOGE("Start socket error"); + return ERR_DM_FAILED; + } + + UninstAppMsg uninstAppMsg(userId, tokenId); + cJSON *root = cJSON_CreateObject(); + if (root == nullptr) { + LOGE("Create cJSON object failed."); + return ERR_DM_FAILED; + } + ToJson(root, uninstAppMsg); + 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_LOCAL_APP_UNINSTALL, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(networkId, 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::RspAppUninstall(const std::string rmtNetworkId, int32_t socketId) +{ + LOGI("RspAppUninstall Start."); + std::string msgStr(""); + CommMsg commMsg(DM_COMM_RSP_APP_UNINSTALL, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("RspAppUninstall failed, ret: %{public}d", ret); + return ERR_DM_FAILED; + } + + LOGI("RspAppUninstall success"); + return DM_OK; +} + +int32_t DMCommTool::RspAppUnbind(const std::string rmtNetworkId, int32_t socketId) +{ + LOGI("RspAppUnbind Start."); + std::string msgStr(""); + CommMsg commMsg(DM_COMM_RSP_APP_UNINSTALL, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("RspAppUnbind failed, ret: %{public}d", ret); + return ERR_DM_FAILED; + } + + LOGI("RspAppUnbind success"); + return DM_OK; +} + +int32_t DMCommTool::SendUnBindAppObj(int32_t userId, int32_t tokenId, std::string extra, const std::string &networkId, + const std::string &udid) +{ + LOGE("DMCommTool::SendUnBindAppObj, userId: %{public}s, tokenId: %{public}s, extra: %{public}s, udid: %{public}s", + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str(), + GetAnonyString(udid).c_str()); + if (!IsIdLengthValid(networkId)) { + LOGE("param invalid, networkId: %{public}s",GetAnonyString(networkId).c_str()); + return ERR_DM_INPUT_PARA_INVALID; + } + int32_t socketId; + if (dmTransportPtr_->StartSocket(networkId, socketId) != DM_OK || socketId <= 0) { + LOGE("Start socket error"); + return ERR_DM_FAILED; + } + + UnBindAppMsg unBindAppMsg(userId, tokenId, extra, udid); + cJSON *root = cJSON_CreateObject(); + if (root == nullptr) { + LOGE("Create cJSON object failed."); + return ERR_DM_FAILED; + } + ToJson(root, unBindAppMsg); + 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_LOCAL_APP_UNBIND, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(networkId, 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; +} + void DMCommTool::RspLocalFrontOrBackUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, int32_t socketId) { @@ -204,6 +327,22 @@ void DMCommTool::DMCommToolEventHandler::ProcessEvent(const AppExecFwk::InnerEve dmCommToolPtr->ProcessReceiveLogoutEvent(commMsg); break; } + case DM_COMM_SEND_LOCAL_APP_UNINSTALL: { + dmCommToolPtr->ProcessReceiveUninstAppEvent(commMsg); + break; + } + case DM_COMM_SEND_LOCAL_APP_UNBIND: { + dmCommToolPtr->ProcessReceiveUnBindAppEvent(commMsg); + break; + } + case DM_COMM_RSP_APP_UNINSTALL: { + dmCommToolPtr->ProcessReceiveRspAppUninstallEvent(commMsg); + break; + } + case DM_COMM_RSP_APP_UNBIND: { + dmCommToolPtr->ProcessReceiveRspAppUnbindEvent(commMsg); + break; + } default: LOGE("event is undefined, id is %{public}d", eventId); break; @@ -329,6 +468,90 @@ void DMCommTool::ProcessReceiveUserIdsEvent(const std::shared_ptr } } +void DMCommTool::ProcessReceiveUninstAppEvent(const std::shared_ptr &commMsg) +{ + LOGI("DMCommTool::ProcessReceiveUninstAppEvent commMsg = %{public}s", GetAnonyString(commMsg->commMsg->msg).c_str()); + std::string payload = commMsg->commMsg->msg; + cJSON *root = cJSON_Parse(payload.c_str()); + if (root == NULL) { + LOGE("the msg is not json format"); + return; + } + UninstAppMsg uninstAppMsg; + FromJson(root, uninstAppMsg); + cJSON_Delete(root); + RspAppUninstall(commMsg->remoteNetworkId, commMsg->socketId); + + if (uninstAppMsg.userId_ == -1 || uninstAppMsg.tokenId_ == -1) { + LOGE("param invalid, userId: %{public}d, tokenId: %{public}d", + uninstAppMsg.userId_, uninstAppMsg.tokenId_); + return; + } else { + DeviceManagerService::GetInstance().ProcessUninstApp(uninstAppMsg.userId_, + uninstAppMsg.tokenId_); + } +} + +void DMCommTool::ProcessReceiveUnBindAppEvent(const std::shared_ptr &commMsg) +{ + LOGI("DMCommTool::ProcessReceiveUnBindAppEvent Receive remote uninstall app"); + std::string payload = commMsg->commMsg->msg; + cJSON *root = cJSON_Parse(payload.c_str()); + if (root == NULL) { + LOGE("the msg is not json format"); + return; + } + UnBindAppMsg unBindAppMsg; + FromJson(root, unBindAppMsg); + cJSON_Delete(root); + RspAppUnbind(commMsg->remoteNetworkId, commMsg->socketId); + + if (unBindAppMsg.userId_ == -1 || unBindAppMsg.tokenId_ == -1) { + LOGE("param invalid, userId: %{public}d, tokenId: %{public}d", + unBindAppMsg.userId_, unBindAppMsg.tokenId_); + return; + } else { + DeviceManagerService::GetInstance().ProcessUnBindApp(unBindAppMsg.userId_, + unBindAppMsg.tokenId_, unBindAppMsg.extra_, unBindAppMsg.udid_); + } +} + +void DMCommTool::StopSocket(const std::string &networkId) +{ + LOGI("DMCommTool::StopSocket, networkId = %{public}s", networkId.c_str()); + if (dmTransportPtr_ == nullptr) { + LOGE("dmTransportPtr_ is null"); + return; + } + dmTransportPtr_->StopSocket(networkId); +} + +void DMCommTool::ProcessReceiveRspAppUninstallEvent(const std::shared_ptr &commMsg) +{ + LOGI("DMCommTool::ProcessReceiveRspAppUninstallEvent commMsg = %{public}s", commMsg->commMsg->msg.c_str()); + 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; + } + DeviceManagerService::GetInstance().ProcessReceiveRspAppUninstall(rmtUdid); +} + +void DMCommTool::ProcessReceiveRspAppUnbindEvent(const std::shared_ptr &commMsg) +{ + LOGI("DMCommTool::ProcessReceiveRspAppUnbindEvent commMsg = %{public}s", commMsg->commMsg->msg.c_str()); + 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; + } + DeviceManagerService::GetInstance().ProcessReceiveRspAppUnbind(rmtUdid); +} + void DMCommTool::ProcessResponseUserIdsEvent(const std::shared_ptr commMsg) { LOGI("start"); diff --git a/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp b/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp index 3011d225c..d405e65f4 100644 --- a/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp +++ b/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp @@ -30,6 +30,9 @@ const char* const DSOFTBUS_NOTIFY_USERIDS_UDIDKEY = "remoteUdid"; const char* const DSOFTBUS_NOTIFY_USERIDS_USERIDKEY = "foregroundUserIds"; const char* const DSOFTBUS_NOTIFY_ACCOUNTID_KEY = "accountId"; const char* const DSOFTBUS_NOTIFY_USERID_KEY = "userId"; +const char* const DSOFTBUS_NOTIFY_TOKENID_KEY = "tokenId"; +const char* const DSOFTBUS_NOTIFY_EXTRA_KEY = "extra"; +const char* const DSOFTBUS_NOTIFY_UDID_KEY = "udid"; } void ToJson(cJSON *jsonObject, const UserIdsMsg &userIdsMsg) { @@ -262,5 +265,71 @@ void FromJson(const cJSON *jsonObject, LogoutAccountMsg &accountInfo) accountInfo.userId = userIdObj->valueint; } } + +void ToJson(cJSON *jsonObject, const UninstAppMsg &uninstAppMsg) +{ + if (jsonObject == nullptr) { + LOGE("Json pointer is nullptr!"); + return; + } + + cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY, uninstAppMsg.userId_); + cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY, uninstAppMsg.tokenId_); +} + +void FromJson(const cJSON *jsonObject, UninstAppMsg &uninstAppMsg) +{ + if (jsonObject == nullptr) { + LOGE("Json pointer is nullptr!"); + return; + } + + cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY); + if (cJSON_IsNumber(userIdObj)) { + uninstAppMsg.userId_ = userIdObj->valueint; + } + cJSON *tokenIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY); + if (cJSON_IsNumber(tokenIdObj)) { + uninstAppMsg.tokenId_ = tokenIdObj->valueint; + } +} + +void ToJson(cJSON *jsonObject, const UnBindAppMsg &unBindAppMsg) +{ + if (jsonObject == nullptr) { + LOGE("Json pointer is nullptr!"); + return; + } + + cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY, unBindAppMsg.userId_); + cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY, unBindAppMsg.tokenId_); + cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_EXTRA_KEY, unBindAppMsg.extra_.c_str()); + cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_UDID_KEY, unBindAppMsg.udid_.c_str()); +} + +void FromJson(const cJSON *jsonObject, UnBindAppMsg &unBindAppMsg) +{ + if (jsonObject == nullptr) { + LOGE("Json pointer is nullptr!"); + return; + } + + cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY); + if (cJSON_IsNumber(userIdObj)) { + unBindAppMsg.userId_ = userIdObj->valueint; + } + cJSON *tokenIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY); + if (cJSON_IsNumber(tokenIdObj)) { + unBindAppMsg.tokenId_ = tokenIdObj->valueint; + } + cJSON *extraObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_EXTRA_KEY); + if (cJSON_IsNumber(extraObj)) { + unBindAppMsg.extra_ = extraObj->valuestring; + } + cJSON *udidObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_UDID_KEY); + if (cJSON_IsNumber(udidObj)) { + unBindAppMsg.udid_ = udidObj->valuestring; + } +} } // 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 428531518..876e63bd3 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -52,6 +52,7 @@ namespace { const int32_t STOP_USER_PAYLOAD_LEN = 3; const int32_t SHARE_UNBIND_PAYLOAD_LEN = 9; const int32_t SHARE_UNBIND_BROADCAST_LEN = 8; + const int32_t APP_UNINSTALL_PAYLOAD_LEN = 7; /** * @brief the userid payload cost 2 bytes. * @@ -147,6 +148,10 @@ bool RelationShipChangeMsg::ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) con ToShareUnbindPayLoad(msg, len); ret = true; break; + case RelationShipChangeType::APP_UNINSTALL: + ToAppUninstallPayLoad(msg, len); + ret = true; + break; default: LOGE("RelationShipChange type invalid"); break; @@ -208,6 +213,9 @@ bool RelationShipChangeMsg::FromBroadcastPayLoad(const cJSON *payloadJson, Relat case RelationShipChangeType::SHARE_UNBIND: ret = FromShareUnbindPayLoad(payloadJson); break; + case RelationShipChangeType::APP_UNINSTALL: + ret = FromAppUninstallPayLoad(payloadJson); + break; default: LOGE("RelationShipChange type invalid"); break; @@ -280,8 +288,7 @@ bool RelationShipChangeMsg::IsValid() const ret = (userId != UINT32_MAX); break; case RelationShipChangeType::APP_UNINSTALL: - // current NOT support - ret = false; + ret = (userId != UINT32_MAX && tokenId != UINT64_MAX); break; case RelationShipChangeType::SYNC_USERID: ret = (!userIdInfos.empty() && @@ -302,7 +309,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::STOP_USER) || - (type == RelationShipChangeType::SERVICE_UNBIND); + (type == RelationShipChangeType::SERVICE_UNBIND) || (type == RelationShipChangeType::APP_UNINSTALL); } bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type) @@ -314,7 +321,8 @@ bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type) (type == (uint32_t)RelationShipChangeType::DEL_USER) || (type == (uint32_t)RelationShipChangeType::STOP_USER) || (type == (uint32_t)RelationShipChangeType::SHARE_UNBIND) || - (type == (uint32_t)RelationShipChangeType::SERVICE_UNBIND); + (type == (uint32_t)RelationShipChangeType::SERVICE_UNBIND) || + (type == (uint32_t)RelationShipChangeType::APP_UNINSTALL); } void RelationShipChangeMsg::ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const @@ -368,6 +376,23 @@ void RelationShipChangeMsg::ToAppUnbindPayLoad(uint8_t *&msg, uint32_t &len) con len = APP_UNBIND_PAYLOAD_LEN; } +void RelationShipChangeMsg::ToAppUninstallPayLoad(uint8_t *&msg, uint32_t &len) const +{ + msg = new uint8_t[APP_UNINSTALL_PAYLOAD_LEN](); + for (int i = 0; i < USERID_PAYLOAD_LEN; i++) { + msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF; + } + + for (int i = USERID_PAYLOAD_LEN; i < TOKENID_PAYLOAD_LEN; i++) { + msg[i] |= (tokenId >> ((i - USERID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF; + } + + for (int i = TOKENID_PAYLOAD_LEN; i < APP_UNINSTALL_PAYLOAD_LEN; i++) { + msg[i] |= (broadCastId >> ((i - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF; + } + len = APP_UNINSTALL_PAYLOAD_LEN; +} + void RelationShipChangeMsg::ToServiceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const { ToAppUnbindPayLoad(msg, len); @@ -554,6 +579,45 @@ bool RelationShipChangeMsg::FromAppUnbindPayLoad(const cJSON *payloadJson) return true; } +bool RelationShipChangeMsg::FromAppUninstallPayLoad(const cJSON *payloadJson) +{ + if (payloadJson == NULL) { + LOGE("App unbind payloadJson is null."); + return false; + } + int32_t arraySize = cJSON_GetArraySize(payloadJson); + if (arraySize < APP_UNINSTALL_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) { + LOGE("Payload invalied,the size is %{public}d.", arraySize); + return false; + } + 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)) { + userId |= (static_cast(payloadItem->valueint)) << (i * BITS_PER_BYTE); + } + } + tokenId = 0; + for (uint32_t j = USERID_PAYLOAD_LEN; j < TOKENID_PAYLOAD_LEN; j++) { + cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); + CHECK_NULL_RETURN(payloadItem, false); + if (cJSON_IsNumber(payloadItem)) { + tokenId |= (static_cast(payloadItem->valueint)) << ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE); + } + } + this->broadCastId = 0; + for (uint32_t j = TOKENID_PAYLOAD_LEN; j < APP_UNINSTALL_PAYLOAD_LEN; j++) { + cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); + CHECK_NULL_RETURN(payloadItem, true); + if (cJSON_IsNumber(payloadItem)) { + this->broadCastId |= (static_cast(payloadItem->valueint)) << + ((j - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE); + } + } + return true; +} + bool RelationShipChangeMsg::FromServiceUnbindPayLoad(const cJSON *payloadJson) { return FromAppUnbindPayLoad(payloadJson); -- Gitee From 695086c9ec95bfe1e3329bfb99b371e49cbc0950 Mon Sep 17 00:00:00 2001 From: BrainL Date: Thu, 22 May 2025 17:01:43 +0800 Subject: [PATCH 2/5] sycn acl Signed-off-by: BrainL --- .../src/deviceprofile_connector.cpp | 2 +- .../include/device_manager_service_impl.h | 7 +- .../device_manager_service_impl_lite.h | 2 +- .../src/device_manager_service_impl.cpp | 18 ++-- .../src/device_manager_service_impl_lite.cpp | 3 +- .../service/include/device_manager_service.h | 6 +- .../include/idevice_manager_service_impl.h | 4 +- .../relationshipsyncmgr/dm_comm_tool.h | 13 ++- .../relationshipsyncmgr/dm_transport_msg.h | 2 +- .../service/src/device_manager_service.cpp | 101 ++++++++++-------- .../src/relationshipsyncmgr/dm_comm_tool.cpp | 60 +++++++---- .../relationship_sync_mgr.cpp | 2 +- 12 files changed, 129 insertions(+), 91 deletions(-) diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 56983150b..f2df14add 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1808,7 +1808,7 @@ std::map DeviceProfileConnector::GetDeviceIdAndBindLevel(s } std::vector DeviceProfileConnector::GetDeviceIdAndUdidListByTokenId(std::vector userIds, - const std::string &localUdid, int32_t tokenId) + const std::string &localUdid, int32_t tokenId) { std::vector profiles = GetAllAccessControlProfile(); std::map deviceIdMap; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 3c801097d..ebf1ff010 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -166,7 +166,7 @@ public: const DevUserInfo &remoteDevUserInfo, std::string &aclList); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId); - void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, std::string extra, std::string udid); + void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, const std::string &extra, const std::string &udid); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); void HandleUserSwitched(const std::vector &deviceVec, int32_t currentUserId, @@ -205,8 +205,7 @@ private: void HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo); bool CheckSharePeerSrc(const std::string &peerUdid, const std::string &localUdid); std::map GetDeviceIdAndBindLevel(int32_t userId); - std::vector GetDeviceIdByUserIdAndTokenId(int32_t userId, - int32_t tokenId); + std::vector GetDeviceIdByUserIdAndTokenId(int32_t userId, int32_t tokenId); std::multimap GetDeviceIdAndUserId(int32_t userId, const std::string &accountId); void HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, const std::string &remoteUdid); @@ -266,7 +265,7 @@ private: std::vector &profiles, std::map &delProfileMap, std::vector> &delACLInfoVec, std::vector &userIdVec, - uint32_t userId, std::string localUdid); + const uint32_t &userId, const std::string &localUdid); bool CheckLnnAcl(DistributedDeviceProfile::AccessControlProfile delProfile, DistributedDeviceProfile::AccessControlProfile lastprofile); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 6a20f775d..9325be561 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -152,7 +152,7 @@ public: const DevUserInfo &remoteDevUserInfo, std::string &aclList); int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId); - void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, std::string extra, std::string udid); + void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, const std::string &extra, const std::string &udid); void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); void HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index e91ac1d8c..c81ecc4ef 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -1903,7 +1903,7 @@ std::map DeviceManagerServiceImpl::GetDeviceIdAndBindLevel } std::vector DeviceManagerServiceImpl::GetDeviceIdByUserIdAndTokenId(int32_t userId, - int32_t tokenId) + int32_t tokenId) { char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); @@ -2044,8 +2044,8 @@ void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId, int32_t peerTokenId) { - LOGI("DeviceManagerServiceImpl::HandleAppUnBindEvent remoteUserId: %{public}s, remoteUdid: %{public}s, " - "tokenId = %{public}s, peerTokenId = %{public}s.",GetAnonyInt32(remoteUserId).c_str(), + LOGI("DeviceManagerServiceImpl::HandleAppUnBindEvent remoteUserId: %{public}s, remoteUdid: %{public}s," + "tokenId = %{public}s, peerTokenId = %{public}s.", GetAnonyInt32(remoteUserId).c_str(), GetAnonyString(remoteUdid).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyInt32(peerTokenId).c_str()); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); @@ -2273,7 +2273,7 @@ int32_t DeviceManagerServiceImpl::ProcessAppUninstall(int32_t userId, int32_t ac } void DeviceManagerServiceImpl::ProcessUnBindApp(int32_t userId, int32_t accessTokenId, - std::string extra, std::string udid) + const std::string &extra, const std::string &udid) { LOGE("DeviceManagerServiceImpl::ProcessUnBindApp userId = %{public}s, accessTokenId = %{public}s," "extra = %{public}s.", GetAnonyInt32(userId).c_str(), GetAnonyInt32(accessTokenId).c_str(), @@ -2284,12 +2284,12 @@ void DeviceManagerServiceImpl::ProcessUnBindApp(int32_t userId, int32_t accessTo LOGE("ParseExtra extraInfoJson error"); return; } - if (!extraInfoJson[TAG_PEER_TOKENID].IsNumberInteger()) { + if (extraInfoJson.Contains(TAG_PEER_TOKENID) && extraInfoJson[TAG_PEER_TOKENID].IsNumberInteger()) { + uint64_t peerTokenId = extraInfoJson[TAG_PEER_TOKENID].Get(); + HandleAppUnBindEvent(userId, udid, accessTokenId, static_cast(peerTokenId)); + } else { HandleAppUnBindEvent(userId, udid, accessTokenId); - return; } - uint64_t peerTokenId = extraInfoJson[TAG_PEER_TOKENID].Get(); - HandleAppUnBindEvent(userId, udid, accessTokenId, static_cast(peerTokenId)); } void DeviceManagerServiceImpl::CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, @@ -2361,7 +2361,7 @@ void DeviceManagerServiceImpl::DeleteAclByTokenId(const int32_t &accessTokenId, std::vector &profiles, std::map &delProfileMap, std::vector> &delACLInfoVec, std::vector &userIdVec, - uint32_t userId, std::string localUdid) + const uint32_t &userId, const std::string &localUdid) { for (auto &item : profiles) { int64_t accesssertokenId = item.GetAccesser().GetAccesserTokenId(); diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 6685f8573..1d9ee03a3 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -578,7 +578,8 @@ int32_t DeviceManagerServiceImpl::ProcessAppUninstall(int32_t userId, int32_t ac return 0; } -void DeviceManagerServiceImpl::ProcessUnBindApp(int32_t userId, int32_t accessTokenId, std::string extra, std::string udid) +void DeviceManagerServiceImpl::ProcessUnBindApp(int32_t userId, int32_t accessTokenId, const std::string &extra, + const std::string &udid) { (void)userId; (void)accessTokenId; diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index eed2bdedb..87cf07ec1 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -100,6 +100,10 @@ public: int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash, const std::string &extra); + int32_t ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash); + + int32_t ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash, const std::string &extra); + int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms); void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo); @@ -217,7 +221,7 @@ public: const std::vector &backgroundUserIds, const std::string &remoteUdid); void ProcessUninstApp(int32_t userId, int32_t tokenId); - void ProcessUnBindApp(int32_t userId, int32_t tokenId, std::string extra, std::string udid); + void ProcessUnBindApp(int32_t userId, int32_t tokenId, const std::string &extra, const std::string &udid); int32_t SetLocalDisplayNameToSoftbus(const std::string &displayName); void RemoveNotifyRecord(const ProcessInfo &processInfo); diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 81ef11af4..e145faf7d 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -253,8 +253,8 @@ public: const DevUserInfo &remoteDevUserInfo, std::string &aclList) = 0; virtual int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId) = 0; virtual int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId) = 0; - virtual void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, - std::string extra, std::string udid) = 0; + virtual void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, const std::string &extra, + const std::string &udid) = 0; virtual void HandleSyncUserIdEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus) = 0; diff --git a/services/service/include/relationshipsyncmgr/dm_comm_tool.h b/services/service/include/relationshipsyncmgr/dm_comm_tool.h index 6fa98f02d..856d920f6 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.h @@ -39,7 +39,8 @@ public: int32_t SendUninstAppObj(int32_t userId, int32_t tokenId, const std::string &networkId); - int32_t SendUnBindAppObj(int32_t userId, int32_t tokenId, std::string extra, const std::string &networkId, const std::string &udid); + int32_t SendUnBindAppObj(int32_t userId, int32_t tokenId, const std::string &extra, const std::string &networkId, + const std::string &udid); void RspLocalFrontOrBackUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, int32_t socketId); @@ -53,8 +54,8 @@ public: int32_t RspAppUninstall(const std::string rmtNetworkId, int32_t socketId); int32_t RspAppUnbind(const std::string rmtNetworkId, int32_t socketId); void StopSocket(const std::string &networkId); - void ProcessReceiveRspAppUninstallEvent(const std::shared_ptr &commMsg); - void ProcessReceiveRspAppUnbindEvent(const std::shared_ptr &commMsg); + void ProcessReceiveRspAppUninstallEvent(const std::shared_ptr commMsg); + void ProcessReceiveRspAppUnbindEvent(const std::shared_ptr commMsg); class DMCommToolEventHandler : public AppExecFwk::EventHandler { public: @@ -62,6 +63,8 @@ public: std::shared_ptr dmCommToolPtr); ~DMCommToolEventHandler() override = default; void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; + void HandleEvent(const std::shared_ptr &dmCommToolPtr, uint32_t eventId, + const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg); private: void ParseUserIdsMsg(std::shared_ptr commMsg, UserIdsMsg &userIdsMsg); std::weak_ptr dmCommToolWPtr_; @@ -76,8 +79,8 @@ public: int32_t StartCommonEvent(std::string commonEventType, EventCallback eventCallback); void ProcessReceiveCommonEvent(const std::shared_ptr commMsg); void ProcessResponseCommonEvent(const std::shared_ptr commMsg); - void ProcessReceiveUninstAppEvent(const std::shared_ptr &commMsg); - void ProcessReceiveUnBindAppEvent(const std::shared_ptr &commMsg); + void ProcessReceiveUninstAppEvent(const std::shared_ptr commMsg); + void ProcessReceiveUnBindAppEvent(const std::shared_ptr commMsg); private: std::shared_ptr dmTransportPtr_; std::shared_ptr eventHandler_; diff --git a/services/service/include/relationshipsyncmgr/dm_transport_msg.h b/services/service/include/relationshipsyncmgr/dm_transport_msg.h index 2d36fc108..c53abee8b 100644 --- a/services/service/include/relationshipsyncmgr/dm_transport_msg.h +++ b/services/service/include/relationshipsyncmgr/dm_transport_msg.h @@ -81,7 +81,7 @@ struct UninstAppMsg { int32_t userId_; int32_t tokenId_; UninstAppMsg() : userId_(-1), tokenId_(-1) {} - UninstAppMsg(const int32_t &userId,const int32_t &tokenId) + UninstAppMsg(const int32_t &userId, const int32_t &tokenId) : userId_(userId), tokenId_(tokenId) {} }; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 760934b1f..d151f5342 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -91,6 +91,7 @@ namespace { const int32_t USER_SWITCH_BY_WIFI_TIMEOUT_S = 2; const int32_t SEND_DELAY_MAX_TIME = 5; const int32_t SEND_DELAY_MIN_TIME = 0; + const int32_t DELAY_TIME_SEC_CONVERSION (1000 * 1000); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG) const std::string GET_LOCAL_DEVICE_NAME_API_NAME = "GetLocalDeviceName"; #endif @@ -789,20 +790,9 @@ int32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t aut int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash) { - LOGE("DeviceManagerService::UnBindDevice pkgName: %{public}s, udidHash: %{public}s", - pkgName.c_str(), GetAnonyString(udidHash).c_str()); - if (!PermissionManager::GetInstance().CheckNewPermission()) { - LOGE("The caller does not have permission to call UnBindDevice."); - return ERR_DM_NO_PERMISSION; - } - LOGI("Begin for pkgName = %{public}s, udidHash = %{public}s", pkgName.c_str(), GetAnonyString(udidHash).c_str()); - if (pkgName.empty() || udidHash.empty()) { - LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); - return ERR_DM_INPUT_PARA_INVALID; - } - if (!IsDMServiceImplReady()) { - LOGE("UnBindDevice failed, instance not init or init failed."); - return ERR_DM_NOT_INIT; + int32_t result = ValidateUnBindDeviceParams(pkgName, udidHash); + if (result != DM_OK) { + return result; } std::string realDeviceId = udidHash; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -850,20 +840,9 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash, const std::string &extra) { - LOGE("DeviceManagerService::UnBindDevice pkgName: %{public}s, udidHash: %{public}s, extra: %{public}s", - GetAnonyString(pkgName).c_str(), GetAnonyString(udidHash).c_str(), GetAnonyString(extra).c_str()); - if (!PermissionManager::GetInstance().CheckNewPermission()) { - LOGE("The caller does not have permission to call UnBindDevice."); - return ERR_DM_NO_PERMISSION; - } - LOGI("Begin for pkgName = %{public}s, udidHash = %{public}s", pkgName.c_str(), GetAnonyString(udidHash).c_str()); - if (pkgName.empty() || udidHash.empty()) { - LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); - return ERR_DM_INPUT_PARA_INVALID; - } - if (!IsDMServiceImplReady()) { - LOGE("UnBindDevice failed, instance not init or init failed."); - return ERR_DM_NOT_INIT; + int32_t result = ValidateUnBindDeviceParams(pkgName, udidHash, extra); + if (result != DM_OK) { + return result; } std::string realDeviceId = udidHash; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -888,11 +867,9 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std return ERR_DM_FAILED; } [[maybe_unused]] uint64_t peerTokenId = dmServiceImpl_->GetTokenIdByNameAndDeviceId(extra, udid); - #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::vector peerUdids; peerUdids.emplace_back(udid); - int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); std::map wifiDevices; bool isBleActive = false; @@ -911,6 +888,46 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std return DM_OK; } +int32_t DeviceManagerService::ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash) +{ + LOGE("DeviceManagerService::ValidateUnBindDeviceParams pkgName: %{public}s, udidHash: %{public}s", + GetAnonyString(pkgName).c_str(), GetAnonyString(udidHash).c_str()); + if (!PermissionManager::GetInstance().CheckNewPermission()) { + LOGE("The caller does not have permission to call UnBindDevice."); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || udidHash.empty()) { + LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceImplReady()) { + LOGE("UnBindDevice failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return DM_OK; +} + +int32_t DeviceManagerService::ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash, + const std::string &extra) +{ + LOGE("DeviceManagerService::ValidateUnBindDeviceParams pkgName: %{public}s, udidHash: %{public}s, " + "extra: %{public}s", GetAnonyString(pkgName).c_str(), GetAnonyString(udidHash).c_str(), + GetAnonyString(extra).c_str()); + if (!PermissionManager::GetInstance().CheckNewPermission()) { + LOGE("The caller does not have permission to call UnBindDevice."); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || udidHash.empty()) { + LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceImplReady()) { + LOGE("UnBindDevice failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return DM_OK; +} + int32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms) { if (!PermissionManager::GetInstance().CheckPermission()) { @@ -2576,16 +2593,17 @@ void DeviceManagerService::ProcessSyncUserIds(const std::vector &foreg void DeviceManagerService::ProcessUninstApp(int32_t userId, int32_t tokenId) { LOGE("DeviceManagerService::ProcessUninstApp userId: %{public}s, tokenId: %{public}s", - GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str()); + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str()); if (IsDMServiceImplReady()) { dmServiceImpl_->ProcessAppUninstall(userId, tokenId); } } -void DeviceManagerService::ProcessUnBindApp(int32_t userId, int32_t tokenId, std::string extra, std::string udid) +void DeviceManagerService::ProcessUnBindApp(int32_t userId, int32_t tokenId, const std::string &extra, + const std::string &udid) { LOGE("DeviceManagerService::ProcessUnBindApp userId: %{public}s, tokenId: %{public}s, udid: %{public}s", - GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(udid).c_str()); + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(udid).c_str()); if (IsDMServiceImplReady()) { dmServiceImpl_->ProcessUnBindApp(userId, tokenId, extra, udid); } @@ -2863,8 +2881,6 @@ void DeviceManagerService::SendAppUnBindBroadCast(const std::vector if (delayTime < SEND_DELAY_MIN_TIME || delayTime == SEND_DELAY_MAX_TIME) { delayTime = SEND_DELAY_MIN_TIME; } - LOGI("SendAppUnBindBroadCast delayTime: %{public}d, currentTime = %{public}lld, SendLastBroadCastTime_ = %{public}lld.", - delayTime, currentTime, SendLastBroadCastTime_); std::function task = [=]() { LOGI("SendAppUnBindBroadCast Start."); RelationShipChangeMsg msg; @@ -2876,7 +2892,7 @@ void DeviceManagerService::SendAppUnBindBroadCast(const std::vector CHECK_NULL_VOID(softbusListener_); softbusListener_->SendAclChangedBroadcast(broadCastMsg); }; - ffrt::submit(task, ffrt::task_attr().delay(delayTime * 1000 * 1000)); + ffrt::submit(task, ffrt::task_attr().delay(delayTime * DELAY_TIME_SEC_CONVERSION)); SendLastBroadCastTime_ = currentTime; } @@ -2893,8 +2909,6 @@ void DeviceManagerService::SendAppUnBindBroadCast(const std::vector if (delayTime < SEND_DELAY_MIN_TIME || delayTime == SEND_DELAY_MAX_TIME) { delayTime = SEND_DELAY_MIN_TIME; } - LOGI("SendAppUnBindBroadCast delayTime: %{public}d, currentTime = %{public}lld, SendLastBroadCastTime_ = %{public}lld.", - delayTime, currentTime, SendLastBroadCastTime_); std::function task = [=]() { LOGI("SendAppUnBindBroadCast Start."); RelationShipChangeMsg msg; @@ -2907,7 +2921,7 @@ void DeviceManagerService::SendAppUnBindBroadCast(const std::vector CHECK_NULL_VOID(softbusListener_); softbusListener_->SendAclChangedBroadcast(broadCastMsg); }; - ffrt::submit(task, ffrt::task_attr().delay(delayTime * 1000 * 1000)); + ffrt::submit(task, ffrt::task_attr().delay(delayTime * DELAY_TIME_SEC_CONVERSION)); SendLastBroadCastTime_ = currentTime; } @@ -2924,7 +2938,6 @@ void DeviceManagerService::SendAppUnInstallBroadCast(const std::vector task = [=]() { LOGI("SendAppUnInstallBroadCast Start."); RelationShipChangeMsg msg; @@ -2936,7 +2949,7 @@ void DeviceManagerService::SendAppUnInstallBroadCast(const std::vectorSendAclChangedBroadcast(broadCastMsg); }; - ffrt::submit(task, ffrt::task_attr().delay(delayTime * 1000 * 1000)); + ffrt::submit(task, ffrt::task_attr().delay(delayTime * DELAY_TIME_SEC_CONVERSION)); SendLastBroadCastTime_ = currentTime; } @@ -3277,7 +3290,6 @@ void DeviceManagerService::NotifyRemoteUninstallApp(int32_t userId, int32_t toke std::vector peerUdids; int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID(); peerUdids = dmServiceImpl_->GetDeviceIdByUserIdAndTokenId(currentUserId, tokenId); - if (peerUdids.empty()) { LOGE("peerUdids is empty"); return; @@ -3342,7 +3354,7 @@ void DeviceManagerService::NotifyRemoteUninstallAppByWifi(int32_t userId, int32_ } void DeviceManagerService::NotifyRemoteUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, - const std::map &wifiDevices) + const std::map &wifiDevices) { LOGE("DeviceManagerService::NotifyRemoteUnBindAppByWifi userId: %{public}s, tokenId: %{public}s, extra: %{public}s", GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str()); @@ -3438,8 +3450,7 @@ void DeviceManagerService::GetNotifyRemoteUnBindAppWay(int32_t userId, int32_t t if (!bleUdids.empty()) { isBleWay = true; - } - if (!wifiDevices.empty()) { + } else { isBleWay = false; } } diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index 1a4edf547..edf649363 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -117,12 +117,12 @@ int32_t DMCommTool::SendUserIds(const std::string rmtNetworkId, int32_t DMCommTool::SendUninstAppObj(int32_t userId, int32_t tokenId, const std::string &networkId) { LOGE("SendUninstAppObj, userId: %{public}s, tokenId: %{public}s", GetAnonyInt32(userId).c_str(), - GetAnonyInt32(tokenId).c_str()); + GetAnonyInt32(tokenId).c_str()); if (!IsIdLengthValid(networkId)) { - LOGE("param invalid, networkId: %{public}s",GetAnonyString(networkId).c_str()); + LOGE("param invalid, networkId: %{public}s", GetAnonyString(networkId).c_str()); return ERR_DM_INPUT_PARA_INVALID; } - int32_t socketId; + int32_t socketId = 0; if (dmTransportPtr_->StartSocket(networkId, socketId) != DM_OK || socketId <= 0) { LOGE("Start socket error"); return ERR_DM_FAILED; @@ -176,7 +176,7 @@ int32_t DMCommTool::RspAppUnbind(const std::string rmtNetworkId, int32_t socketI { LOGI("RspAppUnbind Start."); std::string msgStr(""); - CommMsg commMsg(DM_COMM_RSP_APP_UNINSTALL, msgStr); + CommMsg commMsg(DM_COMM_RSP_APP_UNBIND, msgStr); std::string payload = GetCommMsgString(commMsg); int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); @@ -189,14 +189,14 @@ int32_t DMCommTool::RspAppUnbind(const std::string rmtNetworkId, int32_t socketI return DM_OK; } -int32_t DMCommTool::SendUnBindAppObj(int32_t userId, int32_t tokenId, std::string extra, const std::string &networkId, - const std::string &udid) +int32_t DMCommTool::SendUnBindAppObj(int32_t userId, int32_t tokenId, const std::string &extra, + const std::string &networkId, const std::string &udid) { LOGE("DMCommTool::SendUnBindAppObj, userId: %{public}s, tokenId: %{public}s, extra: %{public}s, udid: %{public}s", - GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str(), - GetAnonyString(udid).c_str()); + GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str(), + GetAnonyString(udid).c_str()); if (!IsIdLengthValid(networkId)) { - LOGE("param invalid, networkId: %{public}s",GetAnonyString(networkId).c_str()); + LOGE("param invalid, networkId: %{public}s", GetAnonyString(networkId).c_str()); return ERR_DM_INPUT_PARA_INVALID; } int32_t socketId; @@ -288,29 +288,32 @@ void DMCommTool::DMCommToolEventHandler::ProcessEvent(const AppExecFwk::InnerEve CHECK_NULL_VOID(commMsg); UserIdsMsg userIdsMsg; ParseUserIdsMsg(commMsg, userIdsMsg); - if (dmCommToolWPtr_.expired()) { LOGE("dmCommToolWPtr_ is expired"); return; } std::shared_ptr dmCommToolPtr = dmCommToolWPtr_.lock(); + HandleEvent(dmCommToolPtr, eventId, commMsg, userIdsMsg); +} + +void DMCommTool::DMCommToolEventHandler::HandleEvent(const std::shared_ptr &dmCommToolPtr, uint32_t eventId, + const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg) +{ CHECK_NULL_VOID(dmCommToolPtr); + switch (eventId) { case DM_COMM_SEND_LOCAL_USERIDS: { if (userIdsMsg.isNewEvent) { dmCommToolPtr->ProcessReceiveCommonEvent(commMsg); } else { - // Process remote foreground userids and send back local user ids dmCommToolPtr->ProcessReceiveUserIdsEvent(commMsg); } break; } case DM_COMM_RSP_LOCAL_USERIDS: { if (userIdsMsg.isNewEvent) { - // Process remote foreground userids and close session dmCommToolPtr->ProcessResponseCommonEvent(commMsg); } else { - // Process remote foreground userids and close session dmCommToolPtr->ProcessResponseUserIdsEvent(commMsg); } break; @@ -468,9 +471,14 @@ void DMCommTool::ProcessReceiveUserIdsEvent(const std::shared_ptr } } -void DMCommTool::ProcessReceiveUninstAppEvent(const std::shared_ptr &commMsg) +void DMCommTool::ProcessReceiveUninstAppEvent(const std::shared_ptr commMsg) { - LOGI("DMCommTool::ProcessReceiveUninstAppEvent commMsg = %{public}s", GetAnonyString(commMsg->commMsg->msg).c_str()); + if (commMsg == nullptr || commMsg->commMsg == nullptr) { + LOGE("commMsg or commMsg->commMsg is null"); + return; + } + LOGI("DMCommTool::ProcessReceiveUninstAppEvent commMsg = %{public}s", + GetAnonyString(commMsg->commMsg->msg).c_str()); std::string payload = commMsg->commMsg->msg; cJSON *root = cJSON_Parse(payload.c_str()); if (root == NULL) { @@ -492,8 +500,12 @@ void DMCommTool::ProcessReceiveUninstAppEvent(const std::shared_ptr &commMsg) +void DMCommTool::ProcessReceiveUnBindAppEvent(const std::shared_ptr commMsg) { + if (commMsg == nullptr || commMsg->commMsg == nullptr) { + LOGE("commMsg or commMsg->commMsg is null"); + return; + } LOGI("DMCommTool::ProcessReceiveUnBindAppEvent Receive remote uninstall app"); std::string payload = commMsg->commMsg->msg; cJSON *root = cJSON_Parse(payload.c_str()); @@ -526,9 +538,13 @@ void DMCommTool::StopSocket(const std::string &networkId) dmTransportPtr_->StopSocket(networkId); } -void DMCommTool::ProcessReceiveRspAppUninstallEvent(const std::shared_ptr &commMsg) +void DMCommTool::ProcessReceiveRspAppUninstallEvent(const std::shared_ptr commMsg) { - LOGI("DMCommTool::ProcessReceiveRspAppUninstallEvent commMsg = %{public}s", commMsg->commMsg->msg.c_str()); + if (commMsg == nullptr || commMsg->remoteNetworkId == "") { + LOGE("commMsg or commMsg->remoteNetworkId is null"); + return; + } + LOGI("DMCommTool::ProcessReceiveRspAppUninstallEvent Start."); this->dmTransportPtr_->StopSocket(commMsg->remoteNetworkId); std::string rmtUdid = ""; SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); @@ -539,9 +555,13 @@ void DMCommTool::ProcessReceiveRspAppUninstallEvent(const std::shared_ptr &commMsg) +void DMCommTool::ProcessReceiveRspAppUnbindEvent(const std::shared_ptr commMsg) { - LOGI("DMCommTool::ProcessReceiveRspAppUnbindEvent commMsg = %{public}s", commMsg->commMsg->msg.c_str()); + if (commMsg == nullptr || commMsg->remoteNetworkId == "") { + LOGE("commMsg or commMsg->remoteNetworkId is null"); + return; + } + LOGI("DMCommTool::ProcessReceiveRspAppUnbindEvent Start."); this->dmTransportPtr_->StopSocket(commMsg->remoteNetworkId); std::string rmtUdid = ""; SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index 876e63bd3..6c2aa108e 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -609,7 +609,7 @@ bool RelationShipChangeMsg::FromAppUninstallPayLoad(const cJSON *payloadJson) this->broadCastId = 0; for (uint32_t j = TOKENID_PAYLOAD_LEN; j < APP_UNINSTALL_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { this->broadCastId |= (static_cast(payloadItem->valueint)) << ((j - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE); -- Gitee From a06b1aabf4621460701a3345176683efcc11f026 Mon Sep 17 00:00:00 2001 From: BrainL Date: Thu, 22 May 2025 17:19:52 +0800 Subject: [PATCH 3/5] modify Signed-off-by: BrainL --- .../service/include/device_manager_service.h | 3 +- .../relationshipsyncmgr/dm_comm_tool.h | 2 + .../service/src/device_manager_service.cpp | 2 +- .../src/relationshipsyncmgr/dm_comm_tool.cpp | 44 ++++++++++++------- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 87cf07ec1..9445b3677 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -102,7 +102,8 @@ public: int32_t ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash); - int32_t ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash, const std::string &extra); + int32_t ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash, + const std::string &extra); int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms); diff --git a/services/service/include/relationshipsyncmgr/dm_comm_tool.h b/services/service/include/relationshipsyncmgr/dm_comm_tool.h index 856d920f6..d580944a9 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.h @@ -65,6 +65,8 @@ public: void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; void HandleEvent(const std::shared_ptr &dmCommToolPtr, uint32_t eventId, const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg); + void HandleLocalUserIdEvent(const std::shared_ptr &dmCommToolPtr, uint32_t eventId, + const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg) private: void ParseUserIdsMsg(std::shared_ptr commMsg, UserIdsMsg &userIdsMsg); std::weak_ptr dmCommToolWPtr_; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index d151f5342..3e8354fc1 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -3354,7 +3354,7 @@ void DeviceManagerService::NotifyRemoteUninstallAppByWifi(int32_t userId, int32_ } void DeviceManagerService::NotifyRemoteUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, - const std::map &wifiDevices) + const std::map &wifiDevices) { LOGE("DeviceManagerService::NotifyRemoteUnBindAppByWifi userId: %{public}s, tokenId: %{public}s, extra: %{public}s", GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str()); diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index edf649363..147f610a3 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -300,24 +300,8 @@ void DMCommTool::DMCommToolEventHandler::HandleEvent(const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg) { CHECK_NULL_VOID(dmCommToolPtr); - + HandleLocalUserIdEvent(dmCommToolPtr, eventId, commMsg, userIdsMsg); switch (eventId) { - case DM_COMM_SEND_LOCAL_USERIDS: { - if (userIdsMsg.isNewEvent) { - dmCommToolPtr->ProcessReceiveCommonEvent(commMsg); - } else { - dmCommToolPtr->ProcessReceiveUserIdsEvent(commMsg); - } - break; - } - case DM_COMM_RSP_LOCAL_USERIDS: { - if (userIdsMsg.isNewEvent) { - dmCommToolPtr->ProcessResponseCommonEvent(commMsg); - } else { - dmCommToolPtr->ProcessResponseUserIdsEvent(commMsg); - } - break; - } case DM_COMM_SEND_USER_STOP: { dmCommToolPtr->ProcessReceiveUserStopEvent(commMsg); break; @@ -352,6 +336,32 @@ void DMCommTool::DMCommToolEventHandler::HandleEvent(const std::shared_ptr &dmCommToolPtr, + uint32_t eventId, const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg) +{ + CHECK_NULL_VOID(dmCommToolPtr); + switch (eventId) { + case DM_COMM_SEND_LOCAL_USERIDS: { + if (userIdsMsg.isNewEvent) { + dmCommToolPtr->ProcessReceiveCommonEvent(commMsg); + } else { + dmCommToolPtr->ProcessReceiveUserIdsEvent(commMsg); + } + break; + } + case DM_COMM_RSP_LOCAL_USERIDS: { + if (userIdsMsg.isNewEvent) { + dmCommToolPtr->ProcessResponseCommonEvent(commMsg); + } else { + dmCommToolPtr->ProcessResponseUserIdsEvent(commMsg); + } + break; + } + default: + break; + } +} + void DMCommTool::ProcessReceiveCommonEvent(const std::shared_ptr commMsg) { LOGI("start"); -- Gitee From 267e8a0976d6f930a588748223d75724d2873c72 Mon Sep 17 00:00:00 2001 From: BrainL Date: Thu, 22 May 2025 17:35:22 +0800 Subject: [PATCH 4/5] modify Signed-off-by: BrainL --- services/service/src/device_manager_service.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 3e8354fc1..730fadcde 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -816,10 +816,6 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel); return ERR_DM_FAILED; } - if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel) != DM_OK) { - LOGE("dmServiceImpl_ UnBindDevice failed."); - return ERR_DM_FAILED; - } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::vector peerUdids; peerUdids.emplace_back(udid); @@ -834,6 +830,10 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std NotifyRemoteUnBindAppByWifi(userId, tokenId, "", wifiDevices); } #endif + if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel) != DM_OK) { + LOGE("dmServiceImpl_ UnBindDevice failed."); + return ERR_DM_FAILED; + } return DM_OK; } -- Gitee From ec45f54492fd657e689e965682b4bfea7af378de Mon Sep 17 00:00:00 2001 From: BrainL Date: Thu, 22 May 2025 17:39:12 +0800 Subject: [PATCH 5/5] modify Signed-off-by: BrainL --- services/service/include/relationshipsyncmgr/dm_comm_tool.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/include/relationshipsyncmgr/dm_comm_tool.h b/services/service/include/relationshipsyncmgr/dm_comm_tool.h index d580944a9..ca1136c13 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.h @@ -64,9 +64,9 @@ public: ~DMCommToolEventHandler() override = default; void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; void HandleEvent(const std::shared_ptr &dmCommToolPtr, uint32_t eventId, - const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg); + const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg); void HandleLocalUserIdEvent(const std::shared_ptr &dmCommToolPtr, uint32_t eventId, - const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg) + const std::shared_ptr &commMsg, const UserIdsMsg &userIdsMsg); private: void ParseUserIdsMsg(std::shared_ptr commMsg, UserIdsMsg &userIdsMsg); std::weak_ptr dmCommToolWPtr_; -- Gitee