From b6b2173b1a1b29e7a23566bb954a91d194d9e0b9 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 16 Jun 2025 10:51:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8D=95=E5=8F=8C=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- common/include/dm_constants.h | 2 + common/src/dm_constants.cpp | 2 + .../include/deviceprofile_connector.h | 3 + .../src/deviceprofile_connector.cpp | 52 ++++++++++++ .../include/i_dm_service_impl_ext.h | 3 +- .../include/i_dm_service_impl_ext_resident.h | 3 +- .../include/device_manager_service_impl.h | 1 + .../device_manager_service_impl_lite.h | 1 + .../src/device_manager_service_impl.cpp | 10 +++ .../src/device_manager_service_impl_lite.cpp | 8 ++ .../service/include/device_manager_service.h | 3 + .../include/idevice_manager_service_impl.h | 2 + .../include/softbus/softbus_listener.h | 1 + .../service/src/device_manager_service.cpp | 83 ++++++++++++++++--- .../service/src/softbus/softbus_listener.cpp | 16 +++- utils/include/kvadapter/kv_adapter.h | 1 + utils/include/kvadapter/kv_adapter_manager.h | 4 + utils/src/kvadapter/kv_adapter.cpp | 25 ++++++ utils/src/kvadapter/kv_adapter_manager.cpp | 46 ++++++++++ 19 files changed, 251 insertions(+), 15 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 0a2645f57..506080dbf 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -185,6 +185,8 @@ extern const char* DM_VERSION_5_1_0; extern const char* DM_VERSION_5_1_1; extern const char* DM_ACL_AGING_VERSION; extern const char* DM_VERSION_5_0_OLD_MAX; // Estimated highest version number of the old version +extern const int32_t DM_HO_OSTYPE; +extern const int32_t DM_OH_OSTYPE; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H diff --git a/common/src/dm_constants.cpp b/common/src/dm_constants.cpp index 9be2b9fb7..00176edfb 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -173,5 +173,7 @@ const char* DM_VERSION_5_1_0 = "5.1.0"; const char* DM_VERSION_5_1_1 = "5.1.1"; const char* DM_ACL_AGING_VERSION = DM_VERSION_5_1_0; const char* DM_VERSION_5_0_OLD_MAX = "5.0.99"; // Estimated highest version number of the old version +const int32_t DM_HO_OSTYPE = -1; +const int32_t DM_OH_OSTYPE = 10; } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 1aa8990e1..be82d7a47 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -315,6 +315,8 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); DM_EXPORT bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid); + DM_EXPORT void DeleteHoDeviceByForeGroundUserId(const std::string &udid, + const std::vector &foreGroundUserIds) private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string requestDeviceId, @@ -413,6 +415,7 @@ private: bool CheckSinkAppOrServiceP2PAcl(const DistributedDeviceProfile::AccessControlProfile &profile, const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid); + bool CheckExtWhiteList(const std::string &bundleName); }; extern "C" IDeviceProfileConnector *CreateDpConnectorInstance(); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index aebb45459..b121064eb 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -59,6 +59,10 @@ namespace { const int32_t DM_SUPPORT_ACL_AGING_VERSION_NUM = 1; const std::string DM_VERSION_STR_5_1_0 = DM_VERSION_5_1_0; const std::vector DM_SUPPORT_ACL_AGING_VERSIONS = {DM_VERSION_STR_5_1_0}; +constexpr uint32_t AUTH_EXT_WHITE_LIST_NUM = 1; +constexpr const static char* g_extWhiteList[AUTH_EXT_WHITE_LIST_NUM] = { + "CastEngineService", +}; } DM_IMPLEMENT_SINGLE_INSTANCE(DeviceProfileConnector); void PrintProfile(const AccessControlProfile &profile) @@ -3311,6 +3315,54 @@ DM_EXPORT bool DeviceProfileConnector::CheckSinkIsSameAccount(const DmAccessCall return false; } +DM_EXPORT void DeviceProfileConnector::DeleteHoDeviceByForeGroundUserId(const std::string &udid, + const std::vector &foreGroundUserIds) +{ + if (udid.empty() || foreGroundUserIds.size() == 0) { + LOGE("invalid input param."); + return; + } + std::vector profiles = GetAllAccessControlProfile(); + std::string localUdid = GetLocalDeviceId(); + for (const auto &item : profiles) { + if (udid != item.GetTrustDeviceId()) { + continue; + } + std::string acerDeviceId = item.GetAccesser().GetAccesserDeviceId(); + int32_t acerUserId = profile.GetAccesser().GetAccesserUserId(); + std::string acerPkgName = profile.GetAccesser().GetAccesserBundleName(); + std::string aceeDeviceId = item.GetAccessee().GetAccesseeDeviceId(); + int32_t aceeUserId = profile.GetAccessee().GetAccesseeUserId(); + std::string aceePkgName = profile.GetAccessee().GetAccesseeBundleName(); + + if (localUdid == acerDeviceId && udid == aceeDeviceId && !CheckExtWhiteList(acerPkgName) && + std::find(foreGroundUserIds.begin(), foreGroundUserIds.end(), acerUserId) != foreGroundUserIds.end()) { + DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); + continue; + } + if (udid == acerDeviceId && localUdid == aceeDeviceId && !CheckExtWhiteList(aceePkgName) && + std::find(foreGroundUserIds.begin(), foreGroundUserIds.end(), aceeUserId) != foreGroundUserIds.end()) { + DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); + continue; + } + } +} + +bool DeviceProfileConnector::CheckExtWhiteList(const std::string &pkgName) +{ + LOGI("start pkgName %{public}s.", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("bundleName empty."); + return false; + } + for (uint32_t index = 0 ; index < AUTH_EXT_WHITE_LIST_NUM ; index++) { + if (pkgName == g_extWhiteList[index]) { + return true; + } + } + return false; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h index cb4bb85bc..10d5abf03 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h @@ -98,7 +98,8 @@ public: * @tc.desc: AccountUserSwitched * @tc.type: FUNC */ - virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId) = 0; + virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId, + const std::vector &peerUdids) = 0; virtual int32_t InitResident(std::shared_ptr residentCallback, const std::shared_ptr &listener) = 0; virtual void OnResidentBytesReceived(const int32_t sessionId, const std::string &message) = 0; diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h index b1685ebec..365755980 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h @@ -83,7 +83,8 @@ public: * @tc.desc: AccountUserSwitched * @tc.type: FUNC */ - virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId) = 0; + virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId, + const std::vector &peerUdids) = 0; /** * @tc.name: IDMServiceImplExt::GetDeviceProfileInfoList diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 7e22d16a8..c32aaf7fc 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -203,6 +203,7 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid); + void DeleteHoDeviceByForeGroundUserId(const std::string &udid, const std::vector &foreGroundUserIds); private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index f52494632..d5f0c548b 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -181,6 +181,7 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid); + void DeleteHoDeviceByForeGroundUserId(const std::string &udid, const std::vector &foreGroundUserIds); private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 3ffe3af1e..103e2301e 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -2944,6 +2944,16 @@ bool DeviceManagerServiceImpl::CheckSinkIsSameAccount(const DmAccessCaller &call return DeviceProfileConnector::GetInstance().CheckSinkIsSameAccount(caller, srcUdid, callee, sinkUdid); } +void DeviceManagerServiceImpl::DeleteHoDeviceByForeGroundUserId(const std::string &udid, + const std::vector &foreGroundUserIds) +{ + //delete acl + DeviceProfileConnector::GetInstance().DeleteHoDeviceByForeGroundUserId(udid, foreGroundUserIds); + //delete group + hiChainConnector_->DeleteAllGroupByUdid(udid); + return; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 279490b25..887754e8f 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -734,6 +734,14 @@ bool DeviceManagerServiceImpl::CheckSinkIsSameAccount(const DmAccessCaller &call return true; } +void DeviceManagerServiceImpl::DeleteHoDeviceByForeGroundUserId(const std::string &udid, + const std::vector &foreGroundUserIds) +{ + (void)udid; + (void)foreGroundUserIds; + return; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index d2bec5c49..6dd27ea16 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -421,6 +421,9 @@ private: #endif bool GetAccessUdidByNetworkId(const std::string &srcNetWorkId, std::string &srcUdid, const std::string &sinkNetWorkId, std::string &sinkUdid); + void HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); + void GetHoOsTypeUdids(std::vector &peerUdids); + void DeleteBackGroundUserHoDevice(); private: bool isImplsoLoaded_ = false; diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 0a49ec515..247796efa 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -285,6 +285,8 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid) = 0; virtual bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid) = 0; + virtual void DeleteBackGroundUserHoDevice(const std::string &udid, + const std::vector &foreGroundUserIds) = 0; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index e6e2ffbd7..9fde1a771 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -142,6 +142,7 @@ private: static int32_t GetAttrFromCustomData(const cJSON *const customDataJson, DmDeviceInfo &dmDevInfo, int32_t &actionId); static int32_t GetAttrFromExtraData(DmDeviceInfo &dmDevInfo, int32_t &actionId); + static void ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr); private: static std::string hostName_; static bool isRadarSoLoad_; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index c32ff62d2..0086f016d 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1629,6 +1629,7 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT return ERR_DM_UNSUPPORTED_METHOD; } LOGI("BindTarget unstardard begin."); + DeleteBackGroundUserHoDevice(); return dmServiceImplExtResident_->BindTargetExt(pkgName, targetId, bindParam); } @@ -2015,16 +2016,7 @@ DM_EXPORT void DeviceManagerService::AccountCommonEventCallback( const std::string commonEventType, int32_t currentUserId, int32_t beforeUserId) { if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); - MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); - if (beforeUserId != -1 && currentUserId != -1 && IsDMServiceAdapterResidentLoad()) { - dmServiceImplExtResident_->AccountUserSwitched( - currentUserId, MultipleUserConnector::GetOhosAccountId()); - } - DMCommTool::GetInstance()->StartCommonEvent(commonEventType, - [this, commonEventType] () { - DeviceManagerService::HandleAccountCommonEvent(commonEventType); - }); + HandleUserSwitchEventCallback(commonEventType, currentUserId, beforeUserId); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { DeviceNameManager::GetInstance().InitDeviceNameWhenLogin(); MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); @@ -4272,5 +4264,74 @@ bool DeviceManagerService::CheckSinkIsSameAccount(const DmAccessCaller &caller, } return dmServiceImpl_->CheckSinkIsSameAccount(caller, srcUdid, callee, sinkUdid); } + +void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, + int32_t beforeUserId) +{ + LOGI("commonEventType %{public}s, currentUserId %{public}d, beforeUserId %{public}d.",commonEventType.c_str(), + currentUserId, beforeUserId); + DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); + MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); + if (beforeUserId != -1 && currentUserId != -1 && IsDMServiceAdapterResidentLoad()) { + std::vector peerUdids; + GetHoOsTypeUdids(peerUdids); + dmServiceImplExtResident_->AccountUserSwitched( + currentUserId, MultipleUserConnector::GetOhosAccountId(), peerUdids); + } + DMCommTool::GetInstance()->StartCommonEvent(commonEventType, + [this, commonEventType] () { + DeviceManagerService::HandleAccountCommonEvent(commonEventType); + }); +} + +void DeviceManagerService::GetHoOsTypeUdids(std::vector &peerUdids) +{ + std::vector osTypeStrs; + if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { + LOGE("Get all ostype failed."); + return; + } + for (const auto &item : osTypeStrs) { + JsonObject osTypeObj(item); + if (osTypeObj.IsDiscarded()) { + LOGE("msg prase error."); + continue; + } + if (!IsString(osTypeObj, "udid") || !IsInt32(osTypeObj, "osType")) { + LOGE("osTypeObj value invalid."); + continue; + } + LOGI("yangwei udid %{public}s, osType %{public}d.", osTypeObj["udid"].Get().c_str(), + osTypeObj["osType"].Get()); + if (osTypeObj["osType"].Get() == DM_HO_OSTYPE) { + peerUdids.push_back(osTypeObj["udid"].Get()); + } + } +} + +void DeviceManagerService::DeleteBackGroundUserHoDevice() +{ + std::vector peerUdids; + GetHoOsTypeUdids(peerUdids); + if (peerUdids.empty()) { + LOGE("peerUdids empty."); + return; + } + std::vector foreGroundUserIds; + MultipleUserConnector::GetInstance().GetForegroundUserIds(foreGroundUserIds ); + if (backGroundUserIds.empty()) { + LOGE("backGroundUserIds is empty"); + return; + } + if (!IsDMServiceImplReady()) { + LOGE("GetAccessUdidByNetworkId failed, instance not init or init failed."); + return false; + } + LOGI("peerudid %{public}s, backGroundUserIds %{public}s.", GetAnonyString(udid).c_str(), + GetIntegerList(foreGroundUserIds).c_str()); + for (const auto &item : peerUdids) { + dmServiceImpl_->DeleteHoDeviceByForeGroundUserId(item, foreGroundUserIds); + } +} } // namespace DistributedHardware -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index f3d2facef..5c81bc0a9 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -34,6 +34,7 @@ #include "multiple_user_connector.h" #endif #include "ipc_skeleton.h" +#include "kv_adapter_manager.h" #include "parameter.h" #include "system_ability_definition.h" @@ -285,9 +286,9 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) } deviceOnLine.detach(); #endif + std::string peerUdid; + GetUdidByNetworkId(info->networkId, peerUdid); { - std::string peerUdid; - GetUdidByNetworkId(info->networkId, peerUdid); struct RadarInfo radarInfo = { .funcName = "OnSoftbusDeviceOnline", .stageRes = static_cast(StageRes::STAGE_SUCC), @@ -302,6 +303,9 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) } } } + std::string osTypeStr = ""; + ConvertOsTypeToJson(info->osType, osTypeStr); + KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); } void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) @@ -1430,5 +1434,13 @@ void SoftbusListener::GetActionId(const std::string &deviceId, int32_t &actionId } actionId = discoveredDeviceActionIdMap.find(deviceId)->second; } + +void SoftbusListener::ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr) +{ + LOGI("ostype %{public}d.", osType); + JsonObject jsonObj; + jsonObj["osType"] = osType; + osTypeStr = SafetyDump(jsonObj); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/utils/include/kvadapter/kv_adapter.h b/utils/include/kvadapter/kv_adapter.h index be755f7dd..573b5143a 100644 --- a/utils/include/kvadapter/kv_adapter.h +++ b/utils/include/kvadapter/kv_adapter.h @@ -45,6 +45,7 @@ public: int32_t DeleteBatch(const std::vector &keys); int32_t Delete(const std::string& key); void OnRemoteDied() override; + int32_t GetAllOstypeData(const std::string &key, std::vector &values); private: DistributedKv::Status GetLocalKvStorePtr(); diff --git a/utils/include/kvadapter/kv_adapter_manager.h b/utils/include/kvadapter/kv_adapter_manager.h index ccd6f74a4..af44d5820 100644 --- a/utils/include/kvadapter/kv_adapter_manager.h +++ b/utils/include/kvadapter/kv_adapter_manager.h @@ -41,6 +41,10 @@ public: DM_EXPORT int32_t GetFreezeData(const std::string &key, std::string &value); DM_EXPORT int32_t PutFreezeData(const std::string &key, std::string &value); DM_EXPORT int32_t DeleteFreezeData(const std::string &key); + DM_EXPORT int32_t GetOstypeData(const std::string &key, std::string &value); + DM_EXPORT int32_t GetAllOstypeData(std::vector &values); + DM_EXPORT int32_t PutOstypeData(const std::string &key, std::string &value); + DM_EXPORT int32_t DeleteOstypeData(const std::string &key); private: KVAdapterManager() = default; diff --git a/utils/src/kvadapter/kv_adapter.cpp b/utils/src/kvadapter/kv_adapter.cpp index b2affd783..fd9404ca4 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -262,5 +262,30 @@ int32_t KVAdapter::Delete(const std::string& key) } return DM_OK; } + +int32_t KVAdapter::GetAllOstypeData(const std::string &key, std::vector &values) +{ + if (key.empty()) { + LOGE("key is empty"); + return ERR_DM_FAILED; + } + std::vector localEntries; + { + std::lock_guard lock(kvAdapterMutex_); + CHECK_NULL_RETURN(kvStorePtr_, ERR_DM_POINT_NULL); + if (kvStorePtr_->GetEntries(key, localEntries) != DistributedKv::Status::SUCCESS) { + LOGE("Get entrys from DB failed."); + return ERR_DM_FAILED; + } + } + values.clear(); + for (const auto &entry : localEntries) { + JsonObject jsonObj; + jsonObj["udid"] = entry.key.ToString(); + jsonObj["osType"] = entry.value.ToString(); + values.push_back(SafetyDump(jsonObj)); + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index da4f50485..58baa4d38 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -32,6 +32,8 @@ constexpr const char* DM_KV_STORE_FREEZE_PREFIX = "anti_ddos_local_"; constexpr const char* DB_KEY_DELIMITER = "###"; constexpr int64_t DM_KV_STORE_REFRESH_TIME = 24 * 60 * 60; // one day constexpr int64_t MAX_SUPPORTED_EXIST_TIME = 3 * 24 * 60 * 60; // 3days +constexpr const char* DM_OSTYPE_PREFIX = "ostype"; +constexpr const char* DM_UDID_PREFIX = "udid"; } DM_IMPLEMENT_SINGLE_INSTANCE(KVAdapterManager); @@ -182,5 +184,49 @@ DM_EXPORT int32_t KVAdapterManager::DeleteFreezeData(const std::string &key) } return DM_OK; } + +DM_EXPORT int32_t KVAdapterManager::GetOstypeData(const std::string &key, std::string &value) +{ + std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER + key; + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Get(dmKey, value) != DM_OK) { + LOGE("Get freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &values) +{ + std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER; + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->GetAllOstypeData(dmKey, values) != DM_OK) { + LOGE("Get freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, std::string &value) +{ + std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER + key; + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Put(dmKey, value) != DM_OK) { + LOGE("Insert freeze data failed, k:%{public}s, v:%{public}s", GetAnonyString(dmKey).c_str(), value.c_str()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) +{ + std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER + key; + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Delete(dmKey) != DM_OK) { + LOGE("delete freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); + return ERR_DM_FAILED; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From ea69e1d3afd4c6c194f976701c0f9c3b5b0e64c6 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Thu, 19 Jun 2025 11:48:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- common/include/dm_constants.h | 2 ++ common/src/dm_constants.cpp | 2 ++ .../include/i_dm_service_impl_ext.h | 3 +- .../include/i_dm_service_impl_ext_resident.h | 3 +- .../src/device_manager_service_impl.cpp | 2 ++ .../service/include/device_manager_service.h | 2 +- .../include/softbus/softbus_listener.h | 1 + .../service/src/device_manager_service.cpp | 28 +++++++++---------- .../service/src/softbus/softbus_listener.cpp | 26 ++++++++++++++--- utils/src/kvadapter/kv_adapter.cpp | 8 ++++-- utils/src/kvadapter/kv_adapter_manager.cpp | 12 +++++--- 11 files changed, 59 insertions(+), 30 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 506080dbf..b224b94f0 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -187,6 +187,8 @@ extern const char* DM_ACL_AGING_VERSION; extern const char* DM_VERSION_5_0_OLD_MAX; // Estimated highest version number of the old version extern const int32_t DM_HO_OSTYPE; extern const int32_t DM_OH_OSTYPE; +extern char* PEER_UDID; +extern char* PEER_OSTYPE; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H diff --git a/common/src/dm_constants.cpp b/common/src/dm_constants.cpp index 00176edfb..b4b518d99 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -175,5 +175,7 @@ const char* DM_ACL_AGING_VERSION = DM_VERSION_5_1_0; const char* DM_VERSION_5_0_OLD_MAX = "5.0.99"; // Estimated highest version number of the old version const int32_t DM_HO_OSTYPE = -1; const int32_t DM_OH_OSTYPE = 10; +const char* PEER_UDID = "peer_udid"; +const char* PEER_OSTYPE = "peer_ostype"; } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h index 10d5abf03..cb4bb85bc 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h @@ -98,8 +98,7 @@ public: * @tc.desc: AccountUserSwitched * @tc.type: FUNC */ - virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId, - const std::vector &peerUdids) = 0; + virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId) = 0; virtual int32_t InitResident(std::shared_ptr residentCallback, const std::shared_ptr &listener) = 0; virtual void OnResidentBytesReceived(const int32_t sessionId, const std::string &message) = 0; diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h index 365755980..b1685ebec 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h @@ -83,8 +83,7 @@ public: * @tc.desc: AccountUserSwitched * @tc.type: FUNC */ - virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId, - const std::vector &peerUdids) = 0; + virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId) = 0; /** * @tc.name: IDMServiceImplExt::GetDeviceProfileInfoList diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 103e2301e..921601649 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -2947,6 +2947,8 @@ bool DeviceManagerServiceImpl::CheckSinkIsSameAccount(const DmAccessCaller &call void DeviceManagerServiceImpl::DeleteHoDeviceByForeGroundUserId(const std::string &udid, const std::vector &foreGroundUserIds) { + LOGI("peerudid %{public}s, foreGroundUserIds %{public}s.", GetAnonyString(udid).c_str(), + GetIntegerList(foreGroundUserIds).c_str()); //delete acl DeviceProfileConnector::GetInstance().DeleteHoDeviceByForeGroundUserId(udid, foreGroundUserIds); //delete group diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 6dd27ea16..3930c877e 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -423,7 +423,7 @@ private: const std::string &sinkNetWorkId, std::string &sinkUdid); void HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); void GetHoOsTypeUdids(std::vector &peerUdids); - void DeleteBackGroundUserHoDevice(); + void DeleteForegroundUserHoDevice(); private: bool isImplsoLoaded_ = false; diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 9fde1a771..4f8b919b1 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -143,6 +143,7 @@ private: int32_t &actionId); static int32_t GetAttrFromExtraData(DmDeviceInfo &dmDevInfo, int32_t &actionId); static void ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr); + bool CheckPeerUdidTrusted(const std::string &udid); private: static std::string hostName_; static bool isRadarSoLoad_; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 0086f016d..a97886897 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1629,7 +1629,7 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT return ERR_DM_UNSUPPORTED_METHOD; } LOGI("BindTarget unstardard begin."); - DeleteBackGroundUserHoDevice(); + DeleteForegroundUserHoDevice(); return dmServiceImplExtResident_->BindTargetExt(pkgName, targetId, bindParam); } @@ -4275,8 +4275,7 @@ void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &comm if (beforeUserId != -1 && currentUserId != -1 && IsDMServiceAdapterResidentLoad()) { std::vector peerUdids; GetHoOsTypeUdids(peerUdids); - dmServiceImplExtResident_->AccountUserSwitched( - currentUserId, MultipleUserConnector::GetOhosAccountId(), peerUdids); + dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); } DMCommTool::GetInstance()->StartCommonEvent(commonEventType, [this, commonEventType] () { @@ -4297,19 +4296,19 @@ void DeviceManagerService::GetHoOsTypeUdids(std::vector &peerUdids) LOGE("msg prase error."); continue; } - if (!IsString(osTypeObj, "udid") || !IsInt32(osTypeObj, "osType")) { + if (!IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE)) { LOGE("osTypeObj value invalid."); continue; } - LOGI("yangwei udid %{public}s, osType %{public}d.", osTypeObj["udid"].Get().c_str(), - osTypeObj["osType"].Get()); - if (osTypeObj["osType"].Get() == DM_HO_OSTYPE) { - peerUdids.push_back(osTypeObj["udid"].Get()); + LOGI("yangwei udid %{public}s, osType %{public}d.", osTypeObj[PEER_UDID].Get().c_str(), + osTypeObj[PEER_OSTYPE].Get()); + if (osTypeObj[PEER_OSTYPE].Get() == DM_HO_OSTYPE) { + peerUdids.push_back(osTypeObj[PEER_UDID].Get()); } } } -void DeviceManagerService::DeleteBackGroundUserHoDevice() +void DeviceManagerService::DeleteForegroundUserHoDevice() { std::vector peerUdids; GetHoOsTypeUdids(peerUdids); @@ -4318,17 +4317,16 @@ void DeviceManagerService::DeleteBackGroundUserHoDevice() return; } std::vector foreGroundUserIds; - MultipleUserConnector::GetInstance().GetForegroundUserIds(foreGroundUserIds ); - if (backGroundUserIds.empty()) { - LOGE("backGroundUserIds is empty"); + MultipleUserConnector::GetInstance().GetForegroundUserIds(foreGroundUserIds); + if (foreGroundUserIds.empty()) { + LOGE("foreGroundUserIds is empty"); return; } if (!IsDMServiceImplReady()) { - LOGE("GetAccessUdidByNetworkId failed, instance not init or init failed."); + LOGE("instance init failed."); return false; } - LOGI("peerudid %{public}s, backGroundUserIds %{public}s.", GetAnonyString(udid).c_str(), - GetIntegerList(foreGroundUserIds).c_str()); + for (const auto &item : peerUdids) { dmServiceImpl_->DeleteHoDeviceByForeGroundUserId(item, foreGroundUserIds); } diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 5c81bc0a9..d510e0862 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -337,9 +337,9 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } deviceOffLine.detach(); #endif + std::string peerUdid; + GetUdidByNetworkId(info->networkId, peerUdid); { - std::string peerUdid; - GetUdidByNetworkId(info->networkId, peerUdid); struct RadarInfo radarInfo = { .funcName = "OnSoftbusDeviceOffline", .stageRes = static_cast(StageRes::STAGE_SUCC), @@ -353,6 +353,9 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } } } + if (!CheckPeerUdidTrusted(peerUdid)) { + KVAdapterManager::GetInstance().DeleteOstypeData(peerUdid); + } } void SoftbusListener::UpdateDeviceName(NodeBasicInfo *info) @@ -1439,8 +1442,23 @@ void SoftbusListener::ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr { LOGI("ostype %{public}d.", osType); JsonObject jsonObj; - jsonObj["osType"] = osType; + jsonObj[PEER_OSTYPE] = osType; osTypeStr = SafetyDump(jsonObj); } + +bool SoftbusListener::CheckPeerUdidTrusted(const std::string &udid) +{ + LOGI("udid %{public}s.", GetAnonyString(udid).c_str()); + std::vector allProfile = + DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); + for (const auto &item : allProfile) { + if (item.GetTrustDeviceId() == udid) { + LOGI("udid %{public}s in acl.", GetAnonyString(udid).c_str()); + return true; + } + } + LOGI("udid %{public}s not in acl.", GetAnonyString(udid).c_str()); + return false; +} } // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file +} // namespace OHOS \ No newline at end of file diff --git a/utils/src/kvadapter/kv_adapter.cpp b/utils/src/kvadapter/kv_adapter.cpp index fd9404ca4..660593ce7 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -39,6 +39,7 @@ namespace { constexpr int32_t MAX_STRING_LEN = 4096; constexpr int32_t MAX_INIT_RETRY_TIMES = 20; constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 200 * 1000; // 200ms + constexpr uint32_t DM_OSTYPE_PREFIX_LEN = 16; } int32_t KVAdapter::Init() @@ -281,8 +282,11 @@ int32_t KVAdapter::GetAllOstypeData(const std::string &key, std::vector(); values.push_back(SafetyDump(jsonObj)); } return DM_OK; diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index 58baa4d38..3fbde7557 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -187,7 +187,8 @@ DM_EXPORT int32_t KVAdapterManager::DeleteFreezeData(const std::string &key) DM_EXPORT int32_t KVAdapterManager::GetOstypeData(const std::string &key, std::string &value) { - std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER + key; + std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + + std::string(DB_KEY_DELIMITER) + key; CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); if (kvAdapter_->Get(dmKey, value) != DM_OK) { LOGE("Get freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); @@ -198,7 +199,8 @@ DM_EXPORT int32_t KVAdapterManager::GetOstypeData(const std::string &key, std::s DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &values) { - std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER; + std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + + std::string(DB_KEY_DELIMITER); CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); if (kvAdapter_->GetAllOstypeData(dmKey, values) != DM_OK) { LOGE("Get freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); @@ -209,7 +211,8 @@ DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &v DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, std::string &value) { - std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER + key; + std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + + std::string(DB_KEY_DELIMITER) + key; CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); if (kvAdapter_->Put(dmKey, value) != DM_OK) { LOGE("Insert freeze data failed, k:%{public}s, v:%{public}s", GetAnonyString(dmKey).c_str(), value.c_str()); @@ -220,7 +223,8 @@ DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, std::s DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) { - std::string dmKey = DM_OSTYPE_PREFIX + DB_KEY_DELIMITER + DM_UDID_PREFIX + DB_KEY_DELIMITER + key; + std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + + std::string(DB_KEY_DELIMITER) + key; CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); if (kvAdapter_->Delete(dmKey) != DM_OK) { LOGE("delete freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); -- Gitee