From 7284f3c5f73aa6795eb45cc593678689062b07f4 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 21 Jun 2025 14:52:02 +0800 Subject: [PATCH 01/18] =?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 | 4 + common/src/dm_constants.cpp | 4 + .../include/deviceprofile_connector.h | 3 + .../src/deviceprofile_connector.cpp | 52 +++++++++++++ .../include/device_manager_service_impl.h | 1 + .../device_manager_service_impl_lite.h | 1 + .../src/device_manager_service_impl.cpp | 12 +++ .../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 | 2 + .../service/src/device_manager_service.cpp | 77 ++++++++++++++++--- .../service/src/softbus/softbus_listener.cpp | 38 ++++++++- utils/include/kvadapter/kv_adapter.h | 1 + utils/include/kvadapter/kv_adapter_manager.h | 4 + utils/src/kvadapter/kv_adapter.cpp | 32 ++++++++ utils/src/kvadapter/kv_adapter_manager.cpp | 50 ++++++++++++ 17 files changed, 280 insertions(+), 14 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index e888cde22..bae92fa52 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -188,6 +188,10 @@ extern const char* DM_VERSION_5_1_1; extern const char* DM_CURRENT_VERSION; 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 const char* PEER_UDID; +extern const 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 73eee5e9c..492fb8684 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -176,5 +176,9 @@ const char* DM_VERSION_5_1_1 = "5.1.1"; const char* DM_CURRENT_VERSION = DM_VERSION_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; +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/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 1aa8990e1..22ba428b1 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 4b3a3b1dd..7bffaab4b 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -57,6 +57,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) @@ -3293,6 +3297,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 = item.GetAccesser().GetAccesserUserId(); + std::string acerPkgName = item.GetAccesser().GetAccesserBundleName(); + std::string aceeDeviceId = item.GetAccessee().GetAccesseeDeviceId(); + int32_t aceeUserId = item.GetAccessee().GetAccesseeUserId(); + std::string aceePkgName = item.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/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 4de0a19f1..0b69563a6 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -2944,6 +2944,18 @@ 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) +{ + LOGI("peerudid %{public}s, foreGroundUserIds %{public}s.", GetAnonyString(udid).c_str(), + GetIntegerList(foreGroundUserIds).c_str()); + //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 da3023d9a..725772bd6 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -420,6 +420,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 DeleteForegroundUserHoDevice(); 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..619c881f3 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 DeleteHoDeviceByForeGroundUserId(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..4f8b919b1 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -142,6 +142,8 @@ 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); + 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 2bcd88c8f..573766aec 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."); + DeleteForegroundUserHoDevice(); 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()); @@ -4263,5 +4255,70 @@ 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()); + } + 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, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE)) { + LOGE("osTypeObj value invalid."); + continue; + } + if (osTypeObj[PEER_OSTYPE].Get() == DM_HO_OSTYPE) { + peerUdids.push_back(osTypeObj[PEER_UDID].Get()); + } + } +} + +void DeviceManagerService::DeleteForegroundUserHoDevice() +{ + std::vector peerUdids; + GetHoOsTypeUdids(peerUdids); + if (peerUdids.empty()) { + LOGE("peerUdids empty."); + return; + } + std::vector foreGroundUserIds; + MultipleUserConnector::GetForegroundUserIds(foreGroundUserIds); + if (foreGroundUserIds.empty()) { + LOGE("foreGroundUserIds is empty"); + return; + } + if (!IsDMServiceImplReady()) { + LOGE("instance init failed."); + return; + } + + for (const auto &item : peerUdids) { + dmServiceImpl_->DeleteHoDeviceByForeGroundUserId(item, foreGroundUserIds); + } +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index f3d2facef..472f17246 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) @@ -333,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), @@ -349,6 +353,9 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } } } + if (!CheckPeerUdidTrusted(peerUdid)) { + KVAdapterManager::GetInstance().DeleteOstypeData(peerUdid); + } } void SoftbusListener::UpdateDeviceName(NodeBasicInfo *info) @@ -1430,5 +1437,28 @@ 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[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 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..6462c5776 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, const 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..ba2b8b063 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -23,6 +23,7 @@ #include "string_ex.h" #include "dm_anonymous.h" +#include "dm_constants.h" #include "dm_error_type.h" #include "dm_log.h" #include "ffrt.h" @@ -39,6 +40,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() @@ -262,5 +264,35 @@ 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 osTyoeJson(entry.value.ToString()); + if (osTyoeJson.IsDiscarded() || !IsInt32(osTyoeJson, PEER_OSTYPE)) { + LOGE("entry parse error."); + continue; + } + JsonObject jsonObj; + jsonObj[PEER_UDID] = entry.key.ToString().substr(DM_OSTYPE_PREFIX_LEN); + jsonObj[PEER_OSTYPE] = osTyoeJson[PEER_OSTYPE].Get(); + 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..2cf414073 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,53 @@ 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 = 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()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &values) +{ + 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()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const std::string &value) +{ + 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()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &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()); + return ERR_DM_FAILED; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From f3b75fc139885aead65eed676ea2f4db0cd500cf Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 22 Jun 2025 16:36:53 +0800 Subject: [PATCH 02/18] =?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 --- .../include/deviceprofile_connector.h | 4 ++-- .../src/deviceprofile_connector.cpp | 17 ++++++++------- .../dependency/hichain/hichain_connector.h | 3 ++- .../include/device_manager_service_impl.h | 3 ++- .../device_manager_service_impl_lite.h | 3 ++- .../dependency/hichain/hichain_connector.cpp | 21 +++++++++++++++++++ .../src/device_manager_service_impl.cpp | 13 ++++++------ .../src/device_manager_service_impl_lite.cpp | 7 ++++--- .../service/include/device_manager_service.h | 2 +- .../include/idevice_manager_service_impl.h | 4 ++-- .../include/softbus/softbus_listener.h | 2 +- .../service/src/device_manager_service.cpp | 12 ++++++----- utils/src/kvadapter/kv_adapter.cpp | 4 ++++ 13 files changed, 64 insertions(+), 31 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 22ba428b1..30cc2a7c6 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -315,8 +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); + DM_EXPORT void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string requestDeviceId, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 7bffaab4b..cd278a963 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -3297,17 +3297,18 @@ DM_EXPORT bool DeviceProfileConnector::CheckSinkIsSameAccount(const DmAccessCall return false; } -DM_EXPORT void DeviceProfileConnector::DeleteHoDeviceByForeGroundUserId(const std::string &udid, - const std::vector &foreGroundUserIds) +DM_EXPORT void DeviceProfileConnector::DeleteHoDevice(const std::string &peerUdid, + const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) { - if (udid.empty() || foreGroundUserIds.size() == 0) { + if (udid.empty() || foreGroundUserIds.empty() || backGroundUserIds.empty()) { LOGE("invalid input param."); return; } + std::vector localUserIds = foreGroundUserIds + backGroundUserIds; std::vector profiles = GetAllAccessControlProfile(); std::string localUdid = GetLocalDeviceId(); for (const auto &item : profiles) { - if (udid != item.GetTrustDeviceId()) { + if (udid != item.GetTrustDeviceId() || item.GetBindType() == DM_IDENTICAL_ACCOUNT) { continue; } std::string acerDeviceId = item.GetAccesser().GetAccesserDeviceId(); @@ -3317,13 +3318,13 @@ DM_EXPORT void DeviceProfileConnector::DeleteHoDeviceByForeGroundUserId(const st int32_t aceeUserId = item.GetAccessee().GetAccesseeUserId(); std::string aceePkgName = item.GetAccessee().GetAccesseeBundleName(); - if (localUdid == acerDeviceId && udid == aceeDeviceId && !CheckExtWhiteList(acerPkgName) && - std::find(foreGroundUserIds.begin(), foreGroundUserIds.end(), acerUserId) != foreGroundUserIds.end()) { + if (localUdid == acerDeviceId && peerUdid == aceeDeviceId && !CheckExtWhiteList(acerPkgName) && + std::find(localUserIds.begin(), localUserIds.end(), acerUserId) != localUserIds.end()) { DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); continue; } - if (udid == acerDeviceId && localUdid == aceeDeviceId && !CheckExtWhiteList(aceePkgName) && - std::find(foreGroundUserIds.begin(), foreGroundUserIds.end(), aceeUserId) != foreGroundUserIds.end()) { + if (peerUdid == acerDeviceId && localUdid == aceeDeviceId && !CheckExtWhiteList(aceePkgName) && + std::find(localUserIds.begin(), localUserIds.end(), aceeUserId) != localUserIds.end()) { DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); continue; } diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index c6e518cad..a4f431c53 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -236,7 +236,8 @@ public: std::vector &userIdVec); bool IsNeedDelete(std::string &groupName, int32_t userId, std::vector> &delACLInfoVec); - + void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds); private: int64_t GenRequestId(); int32_t SyncGroups(std::string deviceId, std::vector &remoteGroupIdList); diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index c32aaf7fc..c4a7f4f11 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -203,7 +203,8 @@ 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); + void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds); 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 d5f0c548b..bf1655b15 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -181,7 +181,8 @@ 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); + void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds); private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index a8192e5f3..c868fc0bb 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -1336,5 +1336,26 @@ bool HiChainConnector::IsNeedDelete(std::string &groupName, int32_t userId, } return false; } + +void HiChainConnector::DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds) +{ + LOGI("peerudid %{public}s, foreGroundUserIds %{public}s, backGroundUserIds %{public}s.", + GetAnonyString(peerUdid).c_str(), GetIntegerList(foreGroundUserIds).c_str(), + GetIntegerList(backGroundUserIds).c_str()); + std::vector localUserIds = foreGroundUserIds + backGroundUserIds; + for (const auto &item : localUserIds) { + std::vector groupList; + GetRelatedGroupsCommon(item, peerUdid, DM_PKG_NAME_EXT, groupList); + for (const auto &iter : groupListExt) { + if (iter.groupType == GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP) { + continue; + } + if (DeleteGroupExt(item, iter.groupId) != DM_OK) { + LOGE("DeleteGroupExt groupId %{public}s failed.", GetAnonyString(iter.groupId).c_str()); + } + } + } +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 0b69563a6..429d1272d 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -2944,15 +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) +void DeviceManagerServiceImpl::DeleteHoDevice(const std::string &peerUdid, + const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) { - LOGI("peerudid %{public}s, foreGroundUserIds %{public}s.", GetAnonyString(udid).c_str(), - GetIntegerList(foreGroundUserIds).c_str()); + LOGI("peerudid %{public}s, foreGroundUserIds %{public}s, backGroundUserIds %{public}s.", + GetAnonyString(peerUdid).c_str(), GetIntegerList(foreGroundUserIds).c_str(), + GetIntegerList(backGroundUserIds).c_str()); //delete acl - DeviceProfileConnector::GetInstance().DeleteHoDeviceByForeGroundUserId(udid, foreGroundUserIds); + DeviceProfileConnector::GetInstance().DeleteHoDevice(peerUdid, foreGroundUserIds, backGroundUserIds); //delete group - hiChainConnector_->DeleteAllGroupByUdid(udid); + hiChainConnector_->DeleteHoDevice(peerUdid, foreGroundUserIds, backGroundUserIds); return; } diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 887754e8f..5ad7cea40 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -734,11 +734,12 @@ bool DeviceManagerServiceImpl::CheckSinkIsSameAccount(const DmAccessCaller &call return true; } -void DeviceManagerServiceImpl::DeleteHoDeviceByForeGroundUserId(const std::string &udid, - const std::vector &foreGroundUserIds) +void DeviceManagerServiceImpl::DeleteHoDevice(const std::string &peerUdid, + const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) { - (void)udid; + (void)peerUdid; (void)foreGroundUserIds; + (void)backGroundUserIds; return; } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 725772bd6..956686d9b 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -422,7 +422,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 DeleteForegroundUserHoDevice(); + void DeleteHoDevice(); 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 619c881f3..1312914c9 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -285,8 +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 DeleteHoDeviceByForeGroundUserId(const std::string &udid, - const std::vector &foreGroundUserIds) = 0; + virtual void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds) = 0; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 4f8b919b1..0ecfe226c 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -143,7 +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); + static 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 573766aec..afabe832f 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1629,7 +1629,6 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT return ERR_DM_UNSUPPORTED_METHOD; } LOGI("BindTarget unstardard begin."); - DeleteForegroundUserHoDevice(); return dmServiceImplExtResident_->BindTargetExt(pkgName, targetId, bindParam); } @@ -4297,7 +4296,7 @@ void DeviceManagerService::GetHoOsTypeUdids(std::vector &peerUdids) } } -void DeviceManagerService::DeleteForegroundUserHoDevice() +void DeviceManagerService::DeleteHoDevice() { std::vector peerUdids; GetHoOsTypeUdids(peerUdids); @@ -4307,8 +4306,11 @@ void DeviceManagerService::DeleteForegroundUserHoDevice() } std::vector foreGroundUserIds; MultipleUserConnector::GetForegroundUserIds(foreGroundUserIds); - if (foreGroundUserIds.empty()) { - LOGE("foreGroundUserIds is empty"); + std::vector backGroundUserIds; + MultipleUserConnector::GetBackgroundUserIds(backGroundUserIds); + if (foreGroundUserIds.empty() || backGroundUserIds.empty()) { + LOGE("backGroundUserIds %{public}s, foreGroundUserIds %{public}s.", GetIntegerList(backGroundUserIds).c_str(), + GetIntegerList(foreGroundUserIds).c_str()); return; } if (!IsDMServiceImplReady()) { @@ -4317,7 +4319,7 @@ void DeviceManagerService::DeleteForegroundUserHoDevice() } for (const auto &item : peerUdids) { - dmServiceImpl_->DeleteHoDeviceByForeGroundUserId(item, foreGroundUserIds); + dmServiceImpl_->DeleteHoDevice(item, foreGroundUserIds, backGroundUserIds); } } } // namespace DistributedHardware diff --git a/utils/src/kvadapter/kv_adapter.cpp b/utils/src/kvadapter/kv_adapter.cpp index ba2b8b063..99cb66843 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -287,6 +287,10 @@ int32_t KVAdapter::GetAllOstypeData(const std::string &key, std::vector(); -- Gitee From fc7f6fe8cdfefc66dd6c900e1feb4de9f30c6004 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 23 Jun 2025 09:41:57 +0800 Subject: [PATCH 03/18] =?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 --- commondependency/src/deviceprofile_connector.cpp | 8 ++++---- .../src/dependency/hichain/hichain_connector.cpp | 5 +++-- services/service/src/device_manager_service.cpp | 3 +-- utils/BUILD.gn | 3 +++ utils/src/kvadapter/kv_adapter_manager.cpp | 2 ++ 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index cd278a963..059044165 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -3300,15 +3300,15 @@ DM_EXPORT bool DeviceProfileConnector::CheckSinkIsSameAccount(const DmAccessCall DM_EXPORT void DeviceProfileConnector::DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) { - if (udid.empty() || foreGroundUserIds.empty() || backGroundUserIds.empty()) { + if (peerUdid.empty() || foreGroundUserIds.empty() || backGroundUserIds.empty()) { LOGE("invalid input param."); return; } - std::vector localUserIds = foreGroundUserIds + backGroundUserIds; - std::vector profiles = GetAllAccessControlProfile(); + std::vector localUserIds(foreGroundUserIds.begin(), foreGroundUserIds.end()); + std::copy(backGroundUserIds.begin(), backGroundUserIds.end(), std::back_inserter(localUserIds)); std::string localUdid = GetLocalDeviceId(); for (const auto &item : profiles) { - if (udid != item.GetTrustDeviceId() || item.GetBindType() == DM_IDENTICAL_ACCOUNT) { + if (peerUdid != item.GetTrustDeviceId() || item.GetBindType() == DM_IDENTICAL_ACCOUNT) { continue; } std::string acerDeviceId = item.GetAccesser().GetAccesserDeviceId(); diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index c868fc0bb..cebe420eb 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -1343,11 +1343,12 @@ void HiChainConnector::DeleteHoDevice(const std::string &peerUdid, const std::ve LOGI("peerudid %{public}s, foreGroundUserIds %{public}s, backGroundUserIds %{public}s.", GetAnonyString(peerUdid).c_str(), GetIntegerList(foreGroundUserIds).c_str(), GetIntegerList(backGroundUserIds).c_str()); - std::vector localUserIds = foreGroundUserIds + backGroundUserIds; + std::vector localUserIds(foreGroundUserIds.begin(), foreGroundUserIds.end()); + std::copy(backGroundUserIds.begin(), backGroundUserIds.end(), std::back_inserter(localUserIds)); for (const auto &item : localUserIds) { std::vector groupList; GetRelatedGroupsCommon(item, peerUdid, DM_PKG_NAME_EXT, groupList); - for (const auto &iter : groupListExt) { + for (auto &iter : groupList) { if (iter.groupType == GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP) { continue; } diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index afabe832f..ad13f4f4a 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4263,8 +4263,7 @@ void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &comm DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); if (beforeUserId != -1 && currentUserId != -1 && IsDMServiceAdapterResidentLoad()) { - std::vector peerUdids; - GetHoOsTypeUdids(peerUdids); + DeleteHoDevice(); dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); } DMCommTool::GetInstance()->StartCommonEvent(commonEventType, diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 7f4e0fe83..c86d938f7 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -45,6 +45,7 @@ if (defined(ohos_lite)) { "${common_path}/src/dfx/lite/dm_hisysevent.cpp", "${common_path}/src/dfx/lite/dm_hitrace.cpp", "${common_path}/src/dm_anonymous.cpp", + "${common_path}/src/dm_constants.cpp", "${common_path}/src/dm_error_message.cpp", "${common_path}/src/ipc/lite/ipc_cmd_register.cpp", "src/appInfo/lite/app_manager.cpp", @@ -125,6 +126,7 @@ if (defined(ohos_lite)) { sources = [ "${common_path}/src/dm_anonymous.cpp", + "${common_path}/src/dm_constants.cpp", "src/appInfo/standard/app_manager.cpp", "src/crypto/dm_crypto.cpp", "src/dm_random.cpp", @@ -204,6 +206,7 @@ if (defined(ohos_lite)) { sources = [ "${common_path}/src/dm_anonymous.cpp", + "${common_path}/src/dm_constants.cpp", "src/appInfo/standard/app_manager.cpp", "src/crypto/dm_crypto.cpp", "src/dm_random.cpp", diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index 2cf414073..7816ba27b 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -211,6 +211,7 @@ DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &v DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const std::string &value) { + LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(dmKey).c_str(), value.c_str()); 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); @@ -223,6 +224,7 @@ DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) { + LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(dmKey).c_str(), value.c_str()); 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); -- Gitee From ed563ab273861b3b6c925bca0c14596b109f1eaf Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 23 Jun 2025 09:50:16 +0800 Subject: [PATCH 04/18] =?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 --- commondependency/src/deviceprofile_connector.cpp | 1 + utils/src/kvadapter/kv_adapter_manager.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 059044165..0fcb802a4 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -3306,6 +3306,7 @@ DM_EXPORT void DeviceProfileConnector::DeleteHoDevice(const std::string &peerUdi } std::vector localUserIds(foreGroundUserIds.begin(), foreGroundUserIds.end()); std::copy(backGroundUserIds.begin(), backGroundUserIds.end(), std::back_inserter(localUserIds)); + std::vector profiles = GetAllAccessControlProfile(); std::string localUdid = GetLocalDeviceId(); for (const auto &item : profiles) { if (peerUdid != item.GetTrustDeviceId() || item.GetBindType() == DM_IDENTICAL_ACCOUNT) { diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index 7816ba27b..e4e63eec6 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -211,7 +211,7 @@ DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &v DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const std::string &value) { - LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(dmKey).c_str(), value.c_str()); + LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); 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); @@ -224,7 +224,7 @@ DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) { - LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(dmKey).c_str(), value.c_str()); + LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); 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); -- Gitee From efde2c2608bc3d6489ae2bf7055b4f38d1008c83 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 23 Jun 2025 17:40:07 +0800 Subject: [PATCH 05/18] =?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 --- utils/src/kvadapter/kv_adapter_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index e4e63eec6..436a44e19 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -224,7 +224,7 @@ DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) { - LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); + LOGI("key %{publkic}s.", GetAnonyString(key).c_str()); 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); -- Gitee From 9d916259597c721bf203c0682557eb10f39ca5d9 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 23 Jun 2025 17:43:51 +0800 Subject: [PATCH 06/18] =?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 --- services/service/src/device_manager_service.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index ad13f4f4a..e2340b5cf 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4258,7 +4258,7 @@ bool DeviceManagerService::CheckSinkIsSameAccount(const DmAccessCaller &caller, 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(), + 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()); @@ -4268,7 +4268,7 @@ void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &comm } DMCommTool::GetInstance()->StartCommonEvent(commonEventType, [this, commonEventType] () { - DeviceManagerService::HandleAccountCommonEvent(commonEventType); + DeviceManagerService::HandleAccountCommonEvent(commonEventType); }); } -- Gitee From 51e71cca6681ec2b0b1391740b6ee15de0c0e437 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 23 Jun 2025 19:55:38 +0800 Subject: [PATCH 07/18] =?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 --- services/service/include/device_manager_service.h | 6 +++--- services/service/src/device_manager_service.cpp | 2 ++ services/service/src/softbus/softbus_listener.cpp | 8 +++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 956686d9b..c9b3011c0 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -417,12 +417,12 @@ private: #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG) bool IsCallerInWhiteList(); bool IsDMAdapterCheckApiWhiteListLoaded(); -#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 DeleteHoDevice(); +#endif + bool GetAccessUdidByNetworkId(const std::string &srcNetWorkId, std::string &srcUdid, + const std::string &sinkNetWorkId, std::string &sinkUdid); private: bool isImplsoLoaded_ = false; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index e2340b5cf..25a06579f 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4255,6 +4255,7 @@ bool DeviceManagerService::CheckSinkIsSameAccount(const DmAccessCaller &caller, return dmServiceImpl_->CheckSinkIsSameAccount(caller, srcUdid, callee, sinkUdid); } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId) { @@ -4321,5 +4322,6 @@ void DeviceManagerService::DeleteHoDevice() dmServiceImpl_->DeleteHoDevice(item, foreGroundUserIds, backGroundUserIds); } } +#endif } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 472f17246..ac9e29209 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -31,10 +31,10 @@ #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "dm_transport_msg.h" #include "ffrt.h" +#include "kv_adapter_manager.h" #include "multiple_user_connector.h" #endif #include "ipc_skeleton.h" -#include "kv_adapter_manager.h" #include "parameter.h" #include "system_ability_definition.h" @@ -305,7 +305,9 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) } std::string osTypeStr = ""; ConvertOsTypeToJson(info->osType, osTypeStr); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); +#endif } void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) @@ -353,9 +355,11 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } } } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) if (!CheckPeerUdidTrusted(peerUdid)) { KVAdapterManager::GetInstance().DeleteOstypeData(peerUdid); } +#endif } void SoftbusListener::UpdateDeviceName(NodeBasicInfo *info) @@ -1449,6 +1453,7 @@ void SoftbusListener::ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr bool SoftbusListener::CheckPeerUdidTrusted(const std::string &udid) { LOGI("udid %{public}s.", GetAnonyString(udid).c_str()); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::vector allProfile = DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); for (const auto &item : allProfile) { @@ -1458,6 +1463,7 @@ bool SoftbusListener::CheckPeerUdidTrusted(const std::string &udid) } } LOGI("udid %{public}s not in acl.", GetAnonyString(udid).c_str()); +#endif return false; } } // namespace DistributedHardware -- Gitee From bc1457a1bf9cdd8a241b16b9156c2e38d7127a22 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 23 Jun 2025 20:42:24 +0800 Subject: [PATCH 08/18] =?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 --- services/service/include/device_manager_service.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index c9b3011c0..aec69a0d2 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -403,6 +403,9 @@ private: void SendCommonEventBroadCast(const std::vector &peerUdids, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, bool isNeedResponse); + void HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); + void GetHoOsTypeUdids(std::vector &peerUdids); + void DeleteHoDevice(); #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void SubscribePublishCommonEvent(); @@ -417,9 +420,6 @@ private: #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG) bool IsCallerInWhiteList(); bool IsDMAdapterCheckApiWhiteListLoaded(); - void HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); - void GetHoOsTypeUdids(std::vector &peerUdids); - void DeleteHoDevice(); #endif bool GetAccessUdidByNetworkId(const std::string &srcNetWorkId, std::string &srcUdid, const std::string &sinkNetWorkId, std::string &sinkUdid); -- Gitee From 6d26fc7e096e3cfa92d11e9d387634b43469051d Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 24 Jun 2025 21:01:07 +0800 Subject: [PATCH 09/18] =?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 | 1 + common/src/dm_constants.cpp | 1 + .../service/include/device_manager_service.h | 4 +- .../include/softbus/softbus_listener.h | 1 + .../service/src/device_manager_service.cpp | 66 ++++++++++--------- .../service/src/softbus/softbus_listener.cpp | 37 ++++++++++- .../softbuscache/src/dm_softbus_cache.cpp | 21 ++++-- utils/include/kvadapter/kv_adapter_manager.h | 1 - utils/src/kvadapter/kv_adapter.cpp | 3 +- utils/src/kvadapter/kv_adapter_manager.cpp | 28 +++----- 10 files changed, 106 insertions(+), 57 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index bae92fa52..f4b330dbb 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -192,6 +192,7 @@ extern const int32_t DM_HO_OSTYPE; extern const int32_t DM_OH_OSTYPE; extern const char* PEER_UDID; extern const char* PEER_OSTYPE; +extern const char* TIME_STAMP; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H diff --git a/common/src/dm_constants.cpp b/common/src/dm_constants.cpp index 492fb8684..be73539b2 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -180,5 +180,6 @@ 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"; +const char* TIME_STAMP = "time_stamp"; } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index aec69a0d2..d3743e0a6 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -405,7 +405,9 @@ private: bool isNeedResponse); void HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); void GetHoOsTypeUdids(std::vector &peerUdids); - void DeleteHoDevice(); + void DeleteHoDevice(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + void HandleAccountLogoutEventCallback(const std::string &commonEventType, int32_t currentUserId, + int32_t beforeUserId); #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void SubscribePublishCommonEvent(); diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 0ecfe226c..8b9b911e0 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -144,6 +144,7 @@ private: static int32_t GetAttrFromExtraData(DmDeviceInfo &dmDevInfo, int32_t &actionId); static void ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr); static bool CheckPeerUdidTrusted(const std::string &udid); + static int32_t PutOstypeData(const std::string &peerUdid); 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 25a06579f..2320c899f 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -648,7 +648,7 @@ int32_t DeviceManagerService::AuthenticateDevice(const std::string &pkgName, int LOGE("The caller: %{public}s does not have permission to call AuthenticateDevice.", pkgName.c_str()); return ERR_DM_NO_PERMISSION; } - if (pkgName.empty() || deviceId.empty()) { + if (pkgName.empty() || deviceId.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("DeviceManagerService::AuthenticateDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); return ERR_DM_INPUT_PARA_INVALID; } @@ -750,7 +750,7 @@ int32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t aut LOGE("The caller does not have permission to call BindDevice."); return ERR_DM_NO_PERMISSION; } - if (pkgName.empty() || deviceId.empty()) { + if (pkgName.empty() || deviceId.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("DeviceManagerService::BindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); return ERR_DM_INPUT_PARA_INVALID; } @@ -895,7 +895,7 @@ int32_t DeviceManagerService::ValidateUnBindDeviceParams(const std::string &pkgN LOGE("The caller does not have permission to call UnBindDevice."); return ERR_DM_NO_PERMISSION; } - if (pkgName.empty() || udidHash.empty()) { + if (pkgName.empty() || udidHash.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); return ERR_DM_INPUT_PARA_INVALID; } @@ -916,7 +916,7 @@ int32_t DeviceManagerService::ValidateUnBindDeviceParams(const std::string &pkgN LOGE("The caller does not have permission to call UnBindDevice."); return ERR_DM_NO_PERMISSION; } - if (pkgName.empty() || udidHash.empty()) { + if (pkgName.empty() || udidHash.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); return ERR_DM_INPUT_PARA_INVALID; } @@ -1596,7 +1596,7 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT return ERR_DM_NO_PERMISSION; } LOGI("Start for pkgName = %{public}s", pkgName.c_str()); - if (pkgName.empty()) { + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } @@ -1640,7 +1640,7 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee return ERR_DM_NO_PERMISSION; } LOGI("Start for pkgName = %{public}s", pkgName.c_str()); - if (pkgName.empty()) { + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } @@ -2020,15 +2020,7 @@ DM_EXPORT void DeviceManagerService::AccountCommonEventCallback( DeviceNameManager::GetInstance().InitDeviceNameWhenLogin(); MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) { - DeviceNameManager::GetInstance().InitDeviceNameWhenLogout(); - DMAccountInfo dmAccountInfo = MultipleUserConnector::GetAccountInfoByUserId(beforeUserId); - if (dmAccountInfo.accountId.empty()) { - return; - } - HandleAccountLogout(currentUserId, dmAccountInfo.accountId, dmAccountInfo.accountName); - MultipleUserConnector::DeleteAccountInfoByUserId(currentUserId); - MultipleUserConnector::SetAccountInfo(MultipleUserConnector::GetCurrentAccountUserID(), - MultipleUserConnector::GetCurrentDMAccountInfo()); + HandleAccountLogoutEventCallback(commonEventType, currentUserId, beforeUserId); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { HandleUserRemoved(beforeUserId); MultipleUserConnector::DeleteAccountInfoByUserId(beforeUserId); @@ -2092,6 +2084,7 @@ void DeviceManagerService::HandleAccountCommonEvent(const std::string commonEven LOGI("User status has not changed."); return; } + DeleteHoDevice(foregroundUserVec, backgroundUserVec); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); std::string localUdid = std::string(localUdidTemp); @@ -3334,7 +3327,7 @@ int32_t DeviceManagerService::RegDevStateCallbackToService(const std::string &pk int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, std::vector &deviceList) { LOGI("Begin for pkgName = %{public}s.", pkgName.c_str()); - if (pkgName.empty()) { + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) { LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } @@ -4263,10 +4256,6 @@ void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &comm currentUserId, beforeUserId); DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); - if (beforeUserId != -1 && currentUserId != -1 && IsDMServiceAdapterResidentLoad()) { - DeleteHoDevice(); - dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); - } DMCommTool::GetInstance()->StartCommonEvent(commonEventType, [this, commonEventType] () { DeviceManagerService::HandleAccountCommonEvent(commonEventType); @@ -4296,32 +4285,49 @@ void DeviceManagerService::GetHoOsTypeUdids(std::vector &peerUdids) } } -void DeviceManagerService::DeleteHoDevice() +void DeviceManagerService::DeleteHoDevice(const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) { + if (foreGroundUserIds.empty() || backGroundUserIds.empty()) { + LOGE("backGroundUserIds %{public}s, foreGroundUserIds %{public}s.", GetIntegerList(backGroundUserIds).c_str(), + GetIntegerList(foreGroundUserIds).c_str()); + return; + } std::vector peerUdids; GetHoOsTypeUdids(peerUdids); if (peerUdids.empty()) { LOGE("peerUdids empty."); return; } - std::vector foreGroundUserIds; - MultipleUserConnector::GetForegroundUserIds(foreGroundUserIds); - std::vector backGroundUserIds; - MultipleUserConnector::GetBackgroundUserIds(backGroundUserIds); - if (foreGroundUserIds.empty() || backGroundUserIds.empty()) { - LOGE("backGroundUserIds %{public}s, foreGroundUserIds %{public}s.", GetIntegerList(backGroundUserIds).c_str(), - GetIntegerList(foreGroundUserIds).c_str()); - return; + if (IsDMServiceAdapterResidentLoad()) { + dmServiceImplExtResident_->AccountUserSwitched(MultipleUserConnector::GetCurrentAccountUserID(), + MultipleUserConnector::GetOhosAccountId()); } if (!IsDMServiceImplReady()) { LOGE("instance init failed."); return; } - for (const auto &item : peerUdids) { dmServiceImpl_->DeleteHoDevice(item, foreGroundUserIds, backGroundUserIds); } } + +void DeviceManagerService::HandleAccountLogoutEventCallback(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().InitDeviceNameWhenLogout(); + DMAccountInfo dmAccountInfo = MultipleUserConnector::GetAccountInfoByUserId(beforeUserId); + if (dmAccountInfo.accountId.empty()) { + LOGE("dmAccountInfo accountId empty."); + return; + } + HandleAccountLogout(currentUserId, dmAccountInfo.accountId, dmAccountInfo.accountName); + MultipleUserConnector::DeleteAccountInfoByUserId(currentUserId); + MultipleUserConnector::SetAccountInfo(MultipleUserConnector::GetCurrentAccountUserID(), + MultipleUserConnector::GetCurrentDMAccountInfo()); +} #endif } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index ac9e29209..13b25d65a 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -45,6 +45,7 @@ const int32_t SOFTBUS_CHECK_INTERVAL = 100000; // 100ms const int32_t SOFTBUS_SUBSCRIBE_ID_MASK = 0x0000FFFF; const int32_t MAX_CACHED_DISCOVERED_DEVICE_SIZE = 100; const int32_t MAX_SOFTBUS_MSG_LEN = 2000; +const int32_t MAX_OSTYPE_SIZE = 1000; #if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) constexpr const char* DEVICE_ONLINE = "deviceOnLine"; constexpr const char* DEVICE_OFFLINE = "deviceOffLine"; @@ -303,11 +304,41 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) } } } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + PutOstypeData(peerUdid); +#endif +} + +int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid) +{ + LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); + std::vector osTypeStrs; + if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { + LOGE("Get all ostype failed."); + return; + } + if (osTypeStrs.size() > MAX_OSTYPE_SIZE) { + int64_t currentTimeStamp = GetSecondsSince1970ToNow(); + std::string deleteUdid = ""; + for (const auto &item : osTypeStrs) { + JsonObject osTypeObj(item); + if (osTypeObj.IsDiscarded() || !IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE) || + !IsInt64(osTypeObj, TIME_STAMP)) { + LOGE("osTypeObj value invalid."); + continue; + } + int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); + std::string osTypeUdid = osTypeObj[PEER_UDID].Get() + if (osTypeTimeStamp < timeStamp && !CheckIsOnline(osTypeUdid)) { + timeStamp = osTypeTimeStamp; + deleteUdid = osTypeUdid; + } + } + KVAdapterManager::GetInstance().DeleteOstypeData(deleteUdid); + } std::string osTypeStr = ""; ConvertOsTypeToJson(info->osType, osTypeStr); -#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); -#endif } void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) @@ -1445,8 +1476,10 @@ void SoftbusListener::GetActionId(const std::string &deviceId, int32_t &actionId void SoftbusListener::ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr) { LOGI("ostype %{public}d.", osType); + int64_t nowTime = GetSecondsSince1970ToNow(); JsonObject jsonObj; jsonObj[PEER_OSTYPE] = osType; + jsonObj[TIME_STAMP] = nowTime; osTypeStr = SafetyDump(jsonObj); } diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 1c285bcd9..da22c5fd3 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -457,14 +457,14 @@ int32_t SoftbusCache::GetDeviceNameFromCache(const std::string &udid, std::strin return ERR_DM_FAILED; } -bool SoftbusCache::CheckIsOnline(const std::string &deviceId) +bool SoftbusCache::CheckIsOnline(const std::string &udidHash) { { std::lock_guard mutexLock(deviceInfosMutex_); for (const auto &item : deviceInfo_) { - LOGI("deviceId %{public}s, cache deviceId %{public}s.", GetAnonyString(deviceId).c_str(), + LOGI("deviceId %{public}s, cache udidHash %{public}s.", GetAnonyString(udidHash).c_str(), GetAnonyString(std::string(item.second.second.deviceId)).c_str()); - if (std::string(item.second.second.deviceId) == deviceId) { + if (std::string(item.second.second.deviceId) == udidHash) { LOGI("CheckIsOnline is true."); return true; } @@ -472,5 +472,18 @@ bool SoftbusCache::CheckIsOnline(const std::string &deviceId) } return false; } + +bool SoftbusCache::CheckIsOnline(const std::string &peerUdid) +{ + { + std::lock_guard mutexLock(deviceInfosMutex_); + if (deviceInfo_.find(peerUdid) != deviceInfo_.end()) { + LOGI("peerUdid %{public}s is online.", GetAnonyString(peerUdid).c_str()); + return true; + } + } + LOGI("peerUdid %{public}s is not online.", GetAnonyString(peerUdid).c_str()); + return false; +} } // namespace DistributedHardware -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/utils/include/kvadapter/kv_adapter_manager.h b/utils/include/kvadapter/kv_adapter_manager.h index 6462c5776..3af7e771c 100644 --- a/utils/include/kvadapter/kv_adapter_manager.h +++ b/utils/include/kvadapter/kv_adapter_manager.h @@ -41,7 +41,6 @@ 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, const std::string &value); DM_EXPORT int32_t DeleteOstypeData(const std::string &key); diff --git a/utils/src/kvadapter/kv_adapter.cpp b/utils/src/kvadapter/kv_adapter.cpp index 99cb66843..c5af99855 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -283,7 +283,7 @@ int32_t KVAdapter::GetAllOstypeData(const std::string &key, std::vector(); + jsonObj[TIME_STAMP] = osTyoeJson[TIME_STAMP].Get(); 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 436a44e19..ce8832d29 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -38,6 +38,13 @@ constexpr const char* DM_UDID_PREFIX = "udid"; DM_IMPLEMENT_SINGLE_INSTANCE(KVAdapterManager); +std::string ComposePrefix() +{ + std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + + std::string(DB_KEY_DELIMITER); + return dmKey; +} + DM_EXPORT int32_t KVAdapterManager::Init() { LOGI("Init Kv-Adapter manager"); @@ -185,22 +192,9 @@ 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 = 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()); - return ERR_DM_FAILED; - } - return DM_OK; -} - DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &values) { - std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + - std::string(DB_KEY_DELIMITER); + std::string dmKey = ComposePrefix(); 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()); @@ -212,8 +206,7 @@ DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &v DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const std::string &value) { LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); - std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + - std::string(DB_KEY_DELIMITER) + key; + std::string dmKey = ComposePrefix() + 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()); @@ -225,8 +218,7 @@ DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) { LOGI("key %{publkic}s.", GetAnonyString(key).c_str()); - std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + - std::string(DB_KEY_DELIMITER) + key; + std::string dmKey = ComposePrefix() + 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 From 9c57fa2412b5591df5ca710e9214d6bd39b14412 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 24 Jun 2025 21:07:16 +0800 Subject: [PATCH 10/18] =?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 --- services/softbuscache/include/dm_softbus_cache.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 8721e01dc..d603d1520 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -51,6 +51,7 @@ public: int32_t GetNetworkIdFromCache(const std::string &udid, std::string &networkId); int32_t GetDeviceNameFromCache(const std::string &udid, std::string &deviceName); bool CheckIsOnline(const std::string &udidHash); + bool CheckIsOnline(const std::string &peerUdid); private: int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); -- Gitee From cfd09a8e73cbb6f07ed279b599fc34ea7d988228 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 24 Jun 2025 21:21:26 +0800 Subject: [PATCH 11/18] =?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 --- .../service/include/device_manager_service.h | 2 +- .../include/softbus/softbus_listener.h | 2 +- .../service/src/device_manager_service.cpp | 4 ++-- .../service/src/softbus/softbus_listener.cpp | 22 +++++++++++-------- .../softbuscache/include/dm_softbus_cache.h | 2 +- .../softbuscache/src/dm_softbus_cache.cpp | 2 +- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index d3743e0a6..1600bc31d 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -405,7 +405,7 @@ private: bool isNeedResponse); void HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); void GetHoOsTypeUdids(std::vector &peerUdids); - void DeleteHoDevice(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + void DeleteHoDevice(const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); void HandleAccountLogoutEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 8b9b911e0..068e05725 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -144,7 +144,7 @@ private: static int32_t GetAttrFromExtraData(DmDeviceInfo &dmDevInfo, int32_t &actionId); static void ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr); static bool CheckPeerUdidTrusted(const std::string &udid); - static int32_t PutOstypeData(const std::string &peerUdid); + static int32_t PutOstypeData(const std::string &peerUdid, int32_t osType); 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 2320c899f..5601d8b2d 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4285,8 +4285,8 @@ void DeviceManagerService::GetHoOsTypeUdids(std::vector &peerUdids) } } -void DeviceManagerService::DeleteHoDevice(const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds) +void DeviceManagerService::DeleteHoDevice(const std::vector &foreGroundUserIds, + const std::vector &backGroundUserIds) { if (foreGroundUserIds.empty() || backGroundUserIds.empty()) { LOGE("backGroundUserIds %{public}s, foreGroundUserIds %{public}s.", GetIntegerList(backGroundUserIds).c_str(), diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 13b25d65a..cda48442e 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -22,6 +22,7 @@ #include #include +#include "datetime_ex.h" #include "device_manager_service.h" #include "dm_crypto.h" #include "dm_constants.h" @@ -305,20 +306,20 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) } } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - PutOstypeData(peerUdid); + PutOstypeData(peerUdid, info->osType); #endif } -int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid) +int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) { LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); std::vector osTypeStrs; if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { LOGE("Get all ostype failed."); - return; + return ERR_DM_FAILED; } if (osTypeStrs.size() > MAX_OSTYPE_SIZE) { - int64_t currentTimeStamp = GetSecondsSince1970ToNow(); + int64_t timeStamp = GetSecondsSince1970ToNow(); std::string deleteUdid = ""; for (const auto &item : osTypeStrs) { JsonObject osTypeObj(item); @@ -328,17 +329,20 @@ int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid) continue; } int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); - std::string osTypeUdid = osTypeObj[PEER_UDID].Get() - if (osTypeTimeStamp < timeStamp && !CheckIsOnline(osTypeUdid)) { + std::string osTypeUdid = osTypeObj[PEER_UDID].Get(); + if (osTypeTimeStamp < timeStamp && !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { timeStamp = osTypeTimeStamp; deleteUdid = osTypeUdid; } } - KVAdapterManager::GetInstance().DeleteOstypeData(deleteUdid); + if (KVAdapterManager::GetInstance().DeleteOstypeData(deleteUdid) != DM_OK) { + LOGE("DeleteOstypeData failed."); + return ERR_DM_FAILED; + } } std::string osTypeStr = ""; - ConvertOsTypeToJson(info->osType, osTypeStr); - KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); + ConvertOsTypeToJson(osType, osTypeStr); + return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); } void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index d603d1520..4bf4a3f6c 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -51,7 +51,7 @@ public: int32_t GetNetworkIdFromCache(const std::string &udid, std::string &networkId); int32_t GetDeviceNameFromCache(const std::string &udid, std::string &deviceName); bool CheckIsOnline(const std::string &udidHash); - bool CheckIsOnline(const std::string &peerUdid); + bool CheckIsOnlineByPeerUdid(const std::string &peerUdid); private: int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index da22c5fd3..4fcf6b54a 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -473,7 +473,7 @@ bool SoftbusCache::CheckIsOnline(const std::string &udidHash) return false; } -bool SoftbusCache::CheckIsOnline(const std::string &peerUdid) +bool SoftbusCache::CheckIsOnlineByPeerUdid(const std::string &peerUdid) { { std::lock_guard mutexLock(deviceInfosMutex_); -- Gitee From 099258a9ac05430b3d84c44a644f715d16eee405 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 24 Jun 2025 21:39:46 +0800 Subject: [PATCH 12/18] =?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 --- .../include/softbus/softbus_listener.h | 6 +- .../service/src/softbus/softbus_listener.cpp | 76 +++++++++---------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 068e05725..cc50ac399 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -138,13 +138,13 @@ private: int32_t InitSoftPublishLNN(); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void ConvertAclToDeviceInfo(DistributedDeviceProfile::AccessControlProfile &profile, DmDeviceInfo &dmDevice); + static void ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr); + static bool CheckPeerUdidTrusted(const std::string &udid); + static int32_t PutOstypeData(const std::string &peerUdid, int32_t osType); #endif 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); - static bool CheckPeerUdidTrusted(const std::string &udid); - static int32_t PutOstypeData(const std::string &peerUdid, int32_t osType); private: static std::string hostName_; static bool isRadarSoLoad_; diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index cda48442e..1f2db8c00 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -22,7 +22,6 @@ #include #include -#include "datetime_ex.h" #include "device_manager_service.h" #include "dm_crypto.h" #include "dm_constants.h" @@ -30,6 +29,7 @@ #include "dm_log.h" #include "dm_softbus_cache.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +#include "datetime_ex.h" #include "dm_transport_msg.h" #include "ffrt.h" #include "kv_adapter_manager.h" @@ -310,41 +310,6 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) #endif } -int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) -{ - LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); - std::vector osTypeStrs; - if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { - LOGE("Get all ostype failed."); - return ERR_DM_FAILED; - } - if (osTypeStrs.size() > MAX_OSTYPE_SIZE) { - int64_t timeStamp = GetSecondsSince1970ToNow(); - std::string deleteUdid = ""; - for (const auto &item : osTypeStrs) { - JsonObject osTypeObj(item); - if (osTypeObj.IsDiscarded() || !IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE) || - !IsInt64(osTypeObj, TIME_STAMP)) { - LOGE("osTypeObj value invalid."); - continue; - } - int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); - std::string osTypeUdid = osTypeObj[PEER_UDID].Get(); - if (osTypeTimeStamp < timeStamp && !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { - timeStamp = osTypeTimeStamp; - deleteUdid = osTypeUdid; - } - } - if (KVAdapterManager::GetInstance().DeleteOstypeData(deleteUdid) != DM_OK) { - LOGE("DeleteOstypeData failed."); - return ERR_DM_FAILED; - } - } - std::string osTypeStr = ""; - ConvertOsTypeToJson(osType, osTypeStr); - return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); -} - void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) { LOGI("received device offline callback from softbus."); @@ -1477,6 +1442,7 @@ void SoftbusListener::GetActionId(const std::string &deviceId, int32_t &actionId actionId = discoveredDeviceActionIdMap.find(deviceId)->second; } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void SoftbusListener::ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr) { LOGI("ostype %{public}d.", osType); @@ -1490,7 +1456,6 @@ void SoftbusListener::ConvertOsTypeToJson(int32_t osType, std::string &osTypeStr bool SoftbusListener::CheckPeerUdidTrusted(const std::string &udid) { LOGI("udid %{public}s.", GetAnonyString(udid).c_str()); -#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::vector allProfile = DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); for (const auto &item : allProfile) { @@ -1500,8 +1465,43 @@ bool SoftbusListener::CheckPeerUdidTrusted(const std::string &udid) } } LOGI("udid %{public}s not in acl.", GetAnonyString(udid).c_str()); -#endif return false; } + +int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) +{ + LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); + std::vector osTypeStrs; + if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { + LOGE("Get all ostype failed."); + return ERR_DM_FAILED; + } + if (osTypeStrs.size() > MAX_OSTYPE_SIZE) { + int64_t timeStamp = GetSecondsSince1970ToNow(); + std::string deleteUdid = ""; + for (const auto &item : osTypeStrs) { + JsonObject osTypeObj(item); + if (osTypeObj.IsDiscarded() || !IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE) || + !IsInt64(osTypeObj, TIME_STAMP)) { + LOGE("osTypeObj value invalid."); + continue; + } + int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); + std::string osTypeUdid = osTypeObj[PEER_UDID].Get(); + if (osTypeTimeStamp < timeStamp && !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { + timeStamp = osTypeTimeStamp; + deleteUdid = osTypeUdid; + } + } + if (KVAdapterManager::GetInstance().DeleteOstypeData(deleteUdid) != DM_OK) { + LOGE("DeleteOstypeData failed."); + return ERR_DM_FAILED; + } + } + std::string osTypeStr = ""; + ConvertOsTypeToJson(osType, osTypeStr); + return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); +} +#endif } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee From aa85d29c7d3555f0c9c2733da72df4afe7c6054c Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Fri, 27 Jun 2025 00:01:51 +0800 Subject: [PATCH 13/18] =?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 --- .../src/deviceprofile_connector.cpp | 13 ++-- .../service/include/device_manager_service.h | 3 + .../service/src/device_manager_service.cpp | 66 +++++++++++++------ utils/include/kvadapter/kv_adapter_manager.h | 2 + utils/src/kvadapter/kv_adapter_manager.cpp | 36 ++++++++-- 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 0fcb802a4..3a9dbb81a 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -30,13 +30,12 @@ using namespace OHOS::DistributedDeviceProfile; const uint32_t INVALIED_TYPE = 0; const uint32_t APP_PEER_TO_PEER_TYPE = 1; const uint32_t APP_ACROSS_ACCOUNT_TYPE = 2; -const uint32_t SHARE_TYPE = 3; -const uint32_t DEVICE_PEER_TO_PEER_TYPE = 4; -const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 5; -const uint32_t IDENTICAL_ACCOUNT_TYPE = 6; -const uint32_t SERVICE_PEER_TO_PEER_TYPE = 7; -const uint32_t SERVICE_ACROSS_ACCOUNT_TYPE = 8; - +const uint32_t SERVICE_PEER_TO_PEER_TYPE = 3; +const uint32_t SERVICE_ACROSS_ACCOUNT_TYPE = 4; +const uint32_t SHARE_TYPE = 5; +const uint32_t DEVICE_PEER_TO_PEER_TYPE = 6; +const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 7; +const uint32_t IDENTICAL_ACCOUNT_TYPE = 8; const uint32_t DM_INVALIED_TYPE = 2048; const uint32_t SERVICE = 2; diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 1600bc31d..e4821c08e 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -425,6 +425,9 @@ private: #endif bool GetAccessUdidByNetworkId(const std::string &srcNetWorkId, std::string &srcUdid, const std::string &sinkNetWorkId, std::string &sinkUdid); + void GetLocalUserIdFromDataBase(std::vector &foregroundUsers, std::vector &backgroundUsers); + void PutLocalUserIdToDataBase(const std::vector &foregroundUsers, + const std::vector &backgroundUsers); private: bool isImplsoLoaded_ = false; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 5601d8b2d..b6b731148 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -94,6 +94,9 @@ namespace { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG) const std::string GET_LOCAL_DEVICE_NAME_API_NAME = "GetLocalDeviceName"; #endif + constexpr const char* LOCAL_ALL_USERID = "local_all_userId"; + constexpr const char* LOCAL_FOREGROUND_USERID = "local_foreground_userId"; + constexpr const char* LOCAL_BACKGROUND_USERID = "local_background_userId"; } DeviceManagerService::~DeviceManagerService() @@ -280,13 +283,6 @@ int32_t DeviceManagerService::InitDMServiceListener() } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) DMCommTool::GetInstance()->Init(); - int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec_); - int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec_); - MultipleUserConnector::ClearLockedUser(foregroundUserVec_, backgroundUserVec_); - if (retFront != DM_OK || retBack != DM_OK) { - LOGE("retFront: %{public}d, retBack: %{public}d, frontuserids: %{public}s, backuserids: %{public}s", - retFront, retBack, GetIntegerList(foregroundUserVec_).c_str(), GetIntegerList(backgroundUserVec_).c_str()); - } int32_t currentUserId = MultipleUserConnector::GetFirstForegroundUserId(); if (IsPC() && !MultipleUserConnector::IsUserUnlocked(currentUserId)) { HandleUserStopEvent(currentUserId); @@ -2045,26 +2041,58 @@ DM_EXPORT void DeviceManagerService::AccountCommonEventCallback( return; } +void DeviceManagerService::GetLocalUserIdFromDataBase(std::vector &foregroundUsers, + std::vector &backgroundUsers) +{ + std::string userIdStr; + KVAdapterManager::GetInstance().GetLocalUserIdData(LOCAL_ALL_USERID, userIdStr); + if (userIdStr.empty()) { + LOGE("result is empty"); + return; + } + JsonObject userIdJson(userIdStr); + if (userIdJson.IsDiscarded()) { + LOGE("userIdJson parse failed"); + return; + } + if (IsArray(userIdJson, LOCAL_FOREGROUND_USERID)) { + resultJson[LOCAL_FOREGROUND_USERID].Get(foregroundUsers); + } + if (IsArray(userIdJson, LOCAL_BACKGROUND_USERID)) { + resultJson[LOCAL_BACKGROUND_USERID].Get(backgroundUsers); + } +} + +void DeviceManagerService::PutLocalUserIdToDataBase(const std::vector &foregroundUsers, + const std::vector &backgroundUsers) +{ + JsonObject jsonObj; + jsonObj[LOCAL_FOREGROUND_USERID] = foregroundUsers; + jsonObj[LOCAL_BACKGROUND_USERID] = backgroundUsers; + std::string localUserIdStr = SafetyDump(jsonObj); + KVAdapterManager::GetInstance().PutLocalUserIdData(LOCAL_ALL_USERID, localUserIdStr); +} + bool DeviceManagerService::IsUserStatusChanged(std::vector foregroundUserVec, std::vector backgroundUserVec) { LOGI("foregroundUserVec: %{public}s, backgroundUserVec: %{public}s", GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str()); - std::lock_guard lock(userVecLock_); + std::vector dBForegroundUserIds; + std::vector dBBackgroundUserIds; + GetLocalUserIdFromDataBase(dBForegroundUserIds, dBBackgroundUserIds); + LOGI("dBForegroundUserIds: %{public}s, dBBackgroundUserIds: %{public}s", + GetIntegerList(dBForegroundUserIds).c_str(), GetIntegerList(dBBackgroundUserIds).c_str()); std::sort(foregroundUserVec.begin(), foregroundUserVec.end()); std::sort(backgroundUserVec.begin(), backgroundUserVec.end()); - std::sort(foregroundUserVec_.begin(), foregroundUserVec_.end()); - std::sort(backgroundUserVec_.begin(), backgroundUserVec_.end()); - if (foregroundUserVec == foregroundUserVec_ && backgroundUserVec == backgroundUserVec_) { + std::sort(dBForegroundUserIds.begin(), dBForegroundUserIds.end()); + std::sort(dBBackgroundUserIds.begin(), dBBackgroundUserIds.end()); + if (foregroundUserVec == dBForegroundUserIds && backgroundUserVec == dBBackgroundUserIds) { LOGI("User status has not changed."); - return true; + return false; } - LOGD("User status has changed"); - foregroundUserVec_.clear(); - backgroundUserVec_.clear(); - foregroundUserVec_ = foregroundUserVec; - backgroundUserVec_ = backgroundUserVec; - return false; + PutLocalUserIdToDataBase(dBForegroundUserIds, dBBackgroundUserIds); + return true; } void DeviceManagerService::HandleAccountCommonEvent(const std::string commonEventType) @@ -2080,7 +2108,7 @@ void DeviceManagerService::HandleAccountCommonEvent(const std::string commonEven retFront, retBack, GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str()); return; } - if (IsUserStatusChanged(foregroundUserVec, backgroundUserVec)) { + if (!IsUserStatusChanged(foregroundUserVec, backgroundUserVec)) { LOGI("User status has not changed."); return; } diff --git a/utils/include/kvadapter/kv_adapter_manager.h b/utils/include/kvadapter/kv_adapter_manager.h index 3af7e771c..5515ffa35 100644 --- a/utils/include/kvadapter/kv_adapter_manager.h +++ b/utils/include/kvadapter/kv_adapter_manager.h @@ -44,6 +44,8 @@ public: DM_EXPORT int32_t GetAllOstypeData(std::vector &values); DM_EXPORT int32_t PutOstypeData(const std::string &key, const std::string &value); DM_EXPORT int32_t DeleteOstypeData(const std::string &key); + DM_EXPORT int32_t GetLocalUserIdData(const std::string &key, std::string &value); + DM_EXPORT int32_t PutLocalUserIdData(const std::string &key, const std::string &value); private: KVAdapterManager() = default; diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index ce8832d29..928be84f9 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -38,7 +38,7 @@ constexpr const char* DM_UDID_PREFIX = "udid"; DM_IMPLEMENT_SINGLE_INSTANCE(KVAdapterManager); -std::string ComposePrefix() +std::string ComposeOsTypePrefix() { std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) + std::string(DB_KEY_DELIMITER); @@ -194,10 +194,10 @@ DM_EXPORT int32_t KVAdapterManager::DeleteFreezeData(const std::string &key) DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &values) { - std::string dmKey = ComposePrefix(); + std::string dmKey = ComposeOsTypePrefix(); 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()); + LOGE("Get all data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); return ERR_DM_FAILED; } return DM_OK; @@ -206,10 +206,10 @@ DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &v DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const std::string &value) { LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); - std::string dmKey = ComposePrefix() + key; + std::string dmKey = ComposeOsTypePrefix() + 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()); + LOGE("Insert data failed, k:%{public}s, v:%{public}s", GetAnonyString(dmKey).c_str(), value.c_str()); return ERR_DM_FAILED; } return DM_OK; @@ -218,10 +218,32 @@ DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key) { LOGI("key %{publkic}s.", GetAnonyString(key).c_str()); - std::string dmKey = ComposePrefix() + key; + std::string dmKey = ComposeOsTypePrefix() + 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()); + LOGE("delete data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::GetLocalUserIdData(const std::string &key, std::string &value) +{ + LOGI("key %{public}s.", GetAnonyString(key).c_str()); + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Get(dmKey, value) != DM_OK) { + LOGE("Get data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); + return ERR_DM_FAILED; + } + return DM_OK; +} + +DM_EXPORT int32_t KVAdapterManager::PutLocalUserIdData(const std::string &key, const std::string &value) +{ + LOGI("key %{public}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str() ); + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Put(dmKey, value) != DM_OK) { + LOGE("Put data failed, key:%{public}s, value:%{public}s", GetAnonyString(dmKey).c_str(), value.c_str()); return ERR_DM_FAILED; } return DM_OK; -- Gitee From 79b241c5bdf1045ed02fb7bd0f131d80014b1a34 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Fri, 27 Jun 2025 10:30:29 +0800 Subject: [PATCH 14/18] =?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 --- services/service/src/device_manager_service.cpp | 6 ++++-- utils/src/kvadapter/kv_adapter_manager.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index b6b731148..68303e082 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -2056,10 +2056,10 @@ void DeviceManagerService::GetLocalUserIdFromDataBase(std::vector &fore return; } if (IsArray(userIdJson, LOCAL_FOREGROUND_USERID)) { - resultJson[LOCAL_FOREGROUND_USERID].Get(foregroundUsers); + userIdJson[LOCAL_FOREGROUND_USERID].Get(foregroundUsers); } if (IsArray(userIdJson, LOCAL_BACKGROUND_USERID)) { - resultJson[LOCAL_BACKGROUND_USERID].Get(backgroundUsers); + userIdJson[LOCAL_BACKGROUND_USERID].Get(backgroundUsers); } } @@ -2091,6 +2091,8 @@ bool DeviceManagerService::IsUserStatusChanged(std::vector foregroundUs LOGI("User status has not changed."); return false; } + dBForegroundUserIds = backgroundUserVec; + dBForegroundUserIds = dBForegroundUserIds; PutLocalUserIdToDataBase(dBForegroundUserIds, dBBackgroundUserIds); return true; } diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index 928be84f9..e656a9c17 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -231,8 +231,8 @@ DM_EXPORT int32_t KVAdapterManager::GetLocalUserIdData(const std::string &key, s { LOGI("key %{public}s.", GetAnonyString(key).c_str()); CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); - if (kvAdapter_->Get(dmKey, value) != DM_OK) { - LOGE("Get data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str()); + if (kvAdapter_->Get(key, value) != DM_OK) { + LOGE("Get data failed, key: %{public}s", GetAnonyString(key).c_str()); return ERR_DM_FAILED; } return DM_OK; @@ -242,8 +242,8 @@ DM_EXPORT int32_t KVAdapterManager::PutLocalUserIdData(const std::string &key, c { LOGI("key %{public}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str() ); CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); - if (kvAdapter_->Put(dmKey, value) != DM_OK) { - LOGE("Put data failed, key:%{public}s, value:%{public}s", GetAnonyString(dmKey).c_str(), value.c_str()); + if (kvAdapter_->Put(key, value) != DM_OK) { + LOGE("Put data failed, key:%{public}s, value:%{public}s", GetAnonyString(key).c_str(), value.c_str()); return ERR_DM_FAILED; } return DM_OK; -- Gitee From ca9bfc94ca24aba30241e7ec88e89c5d4f9325f0 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Fri, 27 Jun 2025 11:27:13 +0800 Subject: [PATCH 15/18] =?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 --- services/service/src/device_manager_service.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 68303e082..cb525ef52 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -2091,8 +2091,8 @@ bool DeviceManagerService::IsUserStatusChanged(std::vector foregroundUs LOGI("User status has not changed."); return false; } - dBForegroundUserIds = backgroundUserVec; - dBForegroundUserIds = dBForegroundUserIds; + dBForegroundUserIds = foregroundUserVec; + dBBackgroundUserIds = backgroundUserVec; PutLocalUserIdToDataBase(dBForegroundUserIds, dBBackgroundUserIds); return true; } -- Gitee From 994c5021ea0d05be1e34f84c0d6b4aeb454f082a Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Fri, 27 Jun 2025 14:30:35 +0800 Subject: [PATCH 16/18] =?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 --- .../service/src/device_manager_service.cpp | 2 +- .../service/src/softbus/softbus_listener.cpp | 53 ++++++++++++++++--- utils/include/kvadapter/kv_adapter.h | 1 + utils/include/kvadapter/kv_adapter_manager.h | 1 + utils/src/kvadapter/kv_adapter.cpp | 21 ++++++++ utils/src/kvadapter/kv_adapter_manager.cpp | 14 ++++- 6 files changed, 84 insertions(+), 8 deletions(-) diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index cb525ef52..5b70f2ba1 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4343,7 +4343,7 @@ void DeviceManagerService::DeleteHoDevice(const std::vector &foreGround } void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &commonEventType, int32_t currentUserId, - int32_t beforeUserId) + int32_t beforeUserId) { LOGI("commonEventType %{public}s, currentUserId %{public}d, beforeUserId %{public}d.", commonEventType.c_str(), currentUserId, beforeUserId); diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 1f2db8c00..3742c24b3 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1477,8 +1477,8 @@ int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osTy return ERR_DM_FAILED; } if (osTypeStrs.size() > MAX_OSTYPE_SIZE) { - int64_t timeStamp = GetSecondsSince1970ToNow(); - std::string deleteUdid = ""; + int64_t earliestTimeStamp = GetSecondsSince1970ToNow(); + std::string earliestPeerUdid = ""; for (const auto &item : osTypeStrs) { JsonObject osTypeObj(item); if (osTypeObj.IsDiscarded() || !IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE) || @@ -1488,12 +1488,53 @@ int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osTy } int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); std::string osTypeUdid = osTypeObj[PEER_UDID].Get(); - if (osTypeTimeStamp < timeStamp && !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { - timeStamp = osTypeTimeStamp; - deleteUdid = osTypeUdid; + if (osTypeTimeStamp < earliestTimeStamp && !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { + earliestTimeStamp = osTypeTimeStamp; + earliestPeerUdid = osTypeUdid; } } - if (KVAdapterManager::GetInstance().DeleteOstypeData(deleteUdid) != DM_OK) { + if (KVAdapterManager::GetInstance().DeleteOstypeData(earliestPeerUdid) != DM_OK) { + LOGE("DeleteOstypeData failed."); + return ERR_DM_FAILED; + } + } + std::string osTypeStr = ""; + ConvertOsTypeToJson(osType, osTypeStr); + return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); +} + +int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) +{ + LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); + int32_t osTypeCount = 0; + if (KVAdapterManager::GetInstance().GetCountByPrefix(osTypeCount) != DM_OK) { + LOGE("Get ostype count %{public}d failed.", osTypeCount); + return ERR_DM_FAILED; + } + if (osTypeCount > MAX_OSTYPE_SIZE) { + std::vector osTypeStrs; + if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { + LOGE("Get all ostype failed."); + return ERR_DM_FAILED; + } + int64_t earliestTimeStamp = GetSecondsSince1970ToNow(); + std::string earliestPeerUdid = ""; + for (const auto &item : osTypeStrs) { + JsonObject osTypeObj(item); + if (osTypeObj.IsDiscarded() || !IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE) || + !IsInt64(osTypeObj, TIME_STAMP)) { + LOGE("osTypeObj value invalid."); + continue; + } + int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); + std::string osTypeUdid = osTypeObj[PEER_UDID].Get(); + if (osTypeTimeStamp < earliestTimeStamp && + !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { + earliestTimeStamp = osTypeTimeStamp; + earliestPeerUdid = osTypeUdid; + } + } + if (KVAdapterManager::GetInstance().DeleteOstypeData(earliestPeerUdid) != DM_OK) { LOGE("DeleteOstypeData failed."); return ERR_DM_FAILED; } diff --git a/utils/include/kvadapter/kv_adapter.h b/utils/include/kvadapter/kv_adapter.h index 573b5143a..b6b22f8d3 100644 --- a/utils/include/kvadapter/kv_adapter.h +++ b/utils/include/kvadapter/kv_adapter.h @@ -46,6 +46,7 @@ public: int32_t Delete(const std::string& key); void OnRemoteDied() override; int32_t GetAllOstypeData(const std::string &key, std::vector &values); + int32_t GetOstypeCountByPrefix(const std::string &prefix, int32_t &count); private: DistributedKv::Status GetLocalKvStorePtr(); diff --git a/utils/include/kvadapter/kv_adapter_manager.h b/utils/include/kvadapter/kv_adapter_manager.h index 5515ffa35..b3f5ce189 100644 --- a/utils/include/kvadapter/kv_adapter_manager.h +++ b/utils/include/kvadapter/kv_adapter_manager.h @@ -46,6 +46,7 @@ public: DM_EXPORT int32_t DeleteOstypeData(const std::string &key); DM_EXPORT int32_t GetLocalUserIdData(const std::string &key, std::string &value); DM_EXPORT int32_t PutLocalUserIdData(const std::string &key, const std::string &value); + DM_EXPORT int32_t GetOsTypeCount(int32_t &count); private: KVAdapterManager() = default; diff --git a/utils/src/kvadapter/kv_adapter.cpp b/utils/src/kvadapter/kv_adapter.cpp index c5af99855..a65494566 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -22,6 +22,7 @@ #include "datetime_ex.h" #include "string_ex.h" +#include "data_query.h" #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_error_type.h" @@ -299,5 +300,25 @@ int32_t KVAdapter::GetAllOstypeData(const std::string &key, std::vector lock(kvAdapterMutex_); + CHECK_NULL_RETURN(kvStorePtr_, ERR_DM_POINT_NULL); + DataQuery prefixQuery; + prefixQuery.KeyPrefix(prefix); + if (kvStorePtr_->GetCount(prefixQuery, count) != DistributedKv::Status::SUCCESS) { + LOGE("GetCount failed."); + return ERR_DM_FAILED; + } + } + 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 e656a9c17..46f84240e 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -240,7 +240,7 @@ DM_EXPORT int32_t KVAdapterManager::GetLocalUserIdData(const std::string &key, s DM_EXPORT int32_t KVAdapterManager::PutLocalUserIdData(const std::string &key, const std::string &value) { - LOGI("key %{public}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str() ); + LOGI("key %{public}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); if (kvAdapter_->Put(key, value) != DM_OK) { LOGE("Put data failed, key:%{public}s, value:%{public}s", GetAnonyString(key).c_str(), value.c_str()); @@ -248,5 +248,17 @@ DM_EXPORT int32_t KVAdapterManager::PutLocalUserIdData(const std::string &key, c } return DM_OK; } + +DM_EXPORT int32_t KVAdapterManager::GetCountByPrefix(int32_t &count) +{ + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + std::string osTypePrefix = ComposeOsTypePrefix(); + if (kvAdapter_->GetOstypeCountByPrefix(osTypePrefix, count) != DM_OK) { + LOGE("GetOstypeCountByPrefix failed, osTypePrefix:%{public}s.", osTypePrefix.c_str()); + return ERR_DM_FAILED; + } + LOGI("count %{public}d.", count); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From ff6f4f58a45f2d359ec047f4e0d9a7d659fdca8a Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Fri, 27 Jun 2025 14:44:10 +0800 Subject: [PATCH 17/18] =?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 --- .../service/src/softbus/softbus_listener.cpp | 37 +------------------ utils/src/kvadapter/kv_adapter_manager.cpp | 2 +- 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 3742c24b3..eb69b04a5 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1468,46 +1468,11 @@ bool SoftbusListener::CheckPeerUdidTrusted(const std::string &udid) return false; } -int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) -{ - LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); - std::vector osTypeStrs; - if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { - LOGE("Get all ostype failed."); - return ERR_DM_FAILED; - } - if (osTypeStrs.size() > MAX_OSTYPE_SIZE) { - int64_t earliestTimeStamp = GetSecondsSince1970ToNow(); - std::string earliestPeerUdid = ""; - for (const auto &item : osTypeStrs) { - JsonObject osTypeObj(item); - if (osTypeObj.IsDiscarded() || !IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE) || - !IsInt64(osTypeObj, TIME_STAMP)) { - LOGE("osTypeObj value invalid."); - continue; - } - int64_t osTypeTimeStamp = osTypeObj[TIME_STAMP].Get(); - std::string osTypeUdid = osTypeObj[PEER_UDID].Get(); - if (osTypeTimeStamp < earliestTimeStamp && !SoftbusCache::GetInstance().CheckIsOnlineByPeerUdid(osTypeUdid)) { - earliestTimeStamp = osTypeTimeStamp; - earliestPeerUdid = osTypeUdid; - } - } - if (KVAdapterManager::GetInstance().DeleteOstypeData(earliestPeerUdid) != DM_OK) { - LOGE("DeleteOstypeData failed."); - return ERR_DM_FAILED; - } - } - std::string osTypeStr = ""; - ConvertOsTypeToJson(osType, osTypeStr); - return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); -} - int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) { LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); int32_t osTypeCount = 0; - if (KVAdapterManager::GetInstance().GetCountByPrefix(osTypeCount) != DM_OK) { + if (KVAdapterManager::GetInstance().GetOsTypeCount(osTypeCount) != DM_OK) { LOGE("Get ostype count %{public}d failed.", osTypeCount); return ERR_DM_FAILED; } diff --git a/utils/src/kvadapter/kv_adapter_manager.cpp b/utils/src/kvadapter/kv_adapter_manager.cpp index 46f84240e..5d1720fca 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -249,7 +249,7 @@ DM_EXPORT int32_t KVAdapterManager::PutLocalUserIdData(const std::string &key, c return DM_OK; } -DM_EXPORT int32_t KVAdapterManager::GetCountByPrefix(int32_t &count) +DM_EXPORT int32_t KVAdapterManager::GetOsTypeCount(int32_t &count) { CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); std::string osTypePrefix = ComposeOsTypePrefix(); -- Gitee From 0265028dd7d700533675516d799a2b098d8a3ef0 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Fri, 27 Jun 2025 18:21:41 +0800 Subject: [PATCH 18/18] =?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 --- services/service/src/softbus/softbus_listener.cpp | 5 +---- utils/src/kvadapter/kv_adapter.cpp | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index eb69b04a5..6a0894f35 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1472,10 +1472,7 @@ int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osTy { LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); int32_t osTypeCount = 0; - if (KVAdapterManager::GetInstance().GetOsTypeCount(osTypeCount) != DM_OK) { - LOGE("Get ostype count %{public}d failed.", osTypeCount); - return ERR_DM_FAILED; - } + KVAdapterManager::GetInstance().GetOsTypeCount(osTypeCount); if (osTypeCount > MAX_OSTYPE_SIZE) { std::vector osTypeStrs; if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) { diff --git a/utils/src/kvadapter/kv_adapter.cpp b/utils/src/kvadapter/kv_adapter.cpp index a65494566..ab211c8e4 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -312,7 +312,7 @@ int32_t KVAdapter::GetOstypeCountByPrefix(const std::string &prefix, int32_t &co std::lock_guard lock(kvAdapterMutex_); CHECK_NULL_RETURN(kvStorePtr_, ERR_DM_POINT_NULL); DataQuery prefixQuery; - prefixQuery.KeyPrefix(prefix); + prefixQuery.InKeys({prefix}); if (kvStorePtr_->GetCount(prefixQuery, count) != DistributedKv::Status::SUCCESS) { LOGE("GetCount failed."); return ERR_DM_FAILED; -- Gitee