diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index e888cde225c58344c6e0da51a09a42526f3c7075..f4b330dbbaea5542e66ca894d92708407e621e2d 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -188,6 +188,11 @@ 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; +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 73eee5e9c48dbb4fbd72ca726c2bb87022404878..be73539b200f08ef2cbca01497d2123dafa12b70 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -176,5 +176,10 @@ 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"; +const char* TIME_STAMP = "time_stamp"; } // 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 1aa8990e17a5031f78fa60426d4f400ebc7c6ec6..30cc2a7c68236197c240644c1044a4f1ed09e9e3 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 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, @@ -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 4b3a3b1dda40d5aa3b6c5316d630de34f47f3499..3a9dbb81a3e0f18d2e724597a5f5d49a8be0a4ad 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; @@ -57,6 +56,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 +3296,56 @@ DM_EXPORT bool DeviceProfileConnector::CheckSinkIsSameAccount(const DmAccessCall return false; } +DM_EXPORT void DeviceProfileConnector::DeleteHoDevice(const std::string &peerUdid, + const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) +{ + if (peerUdid.empty() || foreGroundUserIds.empty() || backGroundUserIds.empty()) { + LOGE("invalid input param."); + return; + } + 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) { + 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 && peerUdid == aceeDeviceId && !CheckExtWhiteList(acerPkgName) && + std::find(localUserIds.begin(), localUserIds.end(), acerUserId) != localUserIds.end()) { + DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); + continue; + } + if (peerUdid == acerDeviceId && localUdid == aceeDeviceId && !CheckExtWhiteList(aceePkgName) && + std::find(localUserIds.begin(), localUserIds.end(), aceeUserId) != localUserIds.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/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index c6e518cadae2ef1a2656b05fee9859a996505f4e..a4f431c53a85d193c9b84d864b6a2fb7b07980f5 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 7e22d16a890f33f2410849e04ada8542e191a20a..c4a7f4f1104d79d06bbb74bbc5390fd51bde37d7 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -203,6 +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 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 f52494632384d2f4ebaf68ac34cacd59e78507b4..bf1655b152cdf5411bf6466e706ea2f87246607b 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -181,6 +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 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 a8192e5f3304470e347b4c0bb17c8958576e6cd6..cebe420eb38a067bdeef036f53fa7f53ed1e0554 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -1336,5 +1336,27 @@ 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.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 (auto &iter : groupList) { + 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 4de0a19f1a0f38b62bb58a94169f70d1cdb78c78..429d1272d6c8f488f1d3f3684ac95339e7736de7 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -2944,6 +2944,19 @@ bool DeviceManagerServiceImpl::CheckSinkIsSameAccount(const DmAccessCaller &call return DeviceProfileConnector::GetInstance().CheckSinkIsSameAccount(caller, srcUdid, callee, sinkUdid); } +void DeviceManagerServiceImpl::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()); + //delete acl + DeviceProfileConnector::GetInstance().DeleteHoDevice(peerUdid, foreGroundUserIds, backGroundUserIds); + //delete group + hiChainConnector_->DeleteHoDevice(peerUdid, foreGroundUserIds, backGroundUserIds); + 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 279490b25ce189ac03908dc4af5583fcce3e4ff6..5ad7cea4064091e854c91ea3f54460fce3d79a26 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -734,6 +734,15 @@ bool DeviceManagerServiceImpl::CheckSinkIsSameAccount(const DmAccessCaller &call return true; } +void DeviceManagerServiceImpl::DeleteHoDevice(const std::string &peerUdid, + const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) +{ + (void)peerUdid; + (void)foreGroundUserIds; + (void)backGroundUserIds; + 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 da3023d9a6460877d2eb3494b63c97d17115f651..e4821c08e1586be7b53124efd5142afc2b61c075 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -403,6 +403,11 @@ 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(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(); @@ -420,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/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 0a49ec5153d0c4d5d011319dcae2b570f206e349..1312914c9f9c7d70bf0e6554a25e66fbb6333cdd 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 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 e6e2ffbd76c8e59aa0db1ec2aa8bb972edda602e..cc50ac399e009f56b5d86be94caba9115cead54e 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -138,6 +138,9 @@ 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); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 2bcd88c8f183ad3f2e4e0c70e53f9f34fe237441..5b70f2ba12dca5a582dbbf2cfe81ea335e3c1030 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); @@ -648,7 +644,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 +746,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 +891,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 +912,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 +1592,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 +1636,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; } @@ -2015,29 +2011,12 @@ 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()); } 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); @@ -2062,26 +2041,60 @@ 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)) { + userIdJson[LOCAL_FOREGROUND_USERID].Get(foregroundUsers); + } + if (IsArray(userIdJson, LOCAL_BACKGROUND_USERID)) { + userIdJson[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; + dBForegroundUserIds = foregroundUserVec; + dBBackgroundUserIds = backgroundUserVec; + PutLocalUserIdToDataBase(dBForegroundUserIds, dBBackgroundUserIds); + return true; } void DeviceManagerService::HandleAccountCommonEvent(const std::string commonEventType) @@ -2097,10 +2110,11 @@ 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; } + DeleteHoDevice(foregroundUserVec, backgroundUserVec); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); std::string localUdid = std::string(localUdidTemp); @@ -3343,7 +3357,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,5 +4277,87 @@ 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) +{ + 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()); + 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::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; + } + 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 f3d2faceff7780cba9d420310736399d305f0f61..6a0894f3552d311f2bcaac9a6c524c8ba348c505 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -29,8 +29,10 @@ #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" #include "multiple_user_connector.h" #endif #include "ipc_skeleton.h" @@ -44,6 +46,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"; @@ -285,9 +288,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 +305,9 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) } } } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + PutOstypeData(peerUdid, info->osType); +#endif } void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) @@ -333,9 +339,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 +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) @@ -1430,5 +1441,70 @@ 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); + int64_t nowTime = GetSecondsSince1970ToNow(); + JsonObject jsonObj; + jsonObj[PEER_OSTYPE] = osType; + jsonObj[TIME_STAMP] = nowTime; + 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; +} + +int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osType) +{ + LOGI("peerUdid %{public}s.", GetAnonyString(peerUdid).c_str()); + int32_t osTypeCount = 0; + KVAdapterManager::GetInstance().GetOsTypeCount(osTypeCount); + 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; + } + } + std::string osTypeStr = ""; + ConvertOsTypeToJson(osType, osTypeStr); + return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); +} +#endif } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 8721e01dc6a888dcb5b7bb314c750003e73ec13b..4bf4a3f6c6a0c458036fea1e454f901a37ed2194 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 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 1c285bcd9809900088dd90e1a656cecc48255d3a..4fcf6b54a2e7e628e4d9e33e9997c20c99e71029 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::CheckIsOnlineByPeerUdid(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/BUILD.gn b/utils/BUILD.gn index 7f4e0fe83efd156c1d13e2cca2b4c7fa15c707b5..c86d938f70aade36b7ef545188be73621d413301 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/include/kvadapter/kv_adapter.h b/utils/include/kvadapter/kv_adapter.h index be755f7dd243d9585d2eaa7995df8d5c65152737..b6b22f8d382a1b068e82586f5438b0f04c3c8a49 100644 --- a/utils/include/kvadapter/kv_adapter.h +++ b/utils/include/kvadapter/kv_adapter.h @@ -45,6 +45,8 @@ 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); + 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 ccd6f74a4fd5a389320c8f51f679a03a30662df5..b3f5ce189327885a6b3aa4977b7b70b64cca426c 100644 --- a/utils/include/kvadapter/kv_adapter_manager.h +++ b/utils/include/kvadapter/kv_adapter_manager.h @@ -41,6 +41,12 @@ 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 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); + 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 b2affd78334a3a55f6c5419cb1403e3d081505d7..ab211c8e47818f61f976664e8ee3cb8ea9b307bf 100644 --- a/utils/src/kvadapter/kv_adapter.cpp +++ b/utils/src/kvadapter/kv_adapter.cpp @@ -22,7 +22,9 @@ #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" #include "dm_log.h" #include "ffrt.h" @@ -39,6 +41,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 +265,60 @@ 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) || !IsInt64(osTyoeJson, TIME_STAMP)) { + LOGE("entry parse error."); + continue; + } + if (entry.key.ToString().size() < DM_OSTYPE_PREFIX_LEN) { + LOGE("entry value invalid."); + continue; + } + JsonObject jsonObj; + jsonObj[PEER_UDID] = entry.key.ToString().substr(DM_OSTYPE_PREFIX_LEN); + jsonObj[PEER_OSTYPE] = osTyoeJson[PEER_OSTYPE].Get(); + jsonObj[TIME_STAMP] = osTyoeJson[TIME_STAMP].Get(); + values.push_back(SafetyDump(jsonObj)); + } + return DM_OK; +} + +int32_t KVAdapter::GetOstypeCountByPrefix(const std::string &prefix, int32_t &count) +{ + LOGI("prefix %{public}s.", prefix.c_str()); + if (prefix.empty()) { + LOGE("prefix is empty."); + return ERR_DM_FAILED; + } + { + std::lock_guard lock(kvAdapterMutex_); + CHECK_NULL_RETURN(kvStorePtr_, ERR_DM_POINT_NULL); + DataQuery prefixQuery; + prefixQuery.InKeys({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 da4f504850a8144bb298f9daff0476fa13748749..5d1720fcad718f5ed74b7ee8dfefeb1e0dfc2b49 100644 --- a/utils/src/kvadapter/kv_adapter_manager.cpp +++ b/utils/src/kvadapter/kv_adapter_manager.cpp @@ -32,10 +32,19 @@ 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); +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); + return dmKey; +} + DM_EXPORT int32_t KVAdapterManager::Init() { LOGI("Init Kv-Adapter manager"); @@ -182,5 +191,74 @@ DM_EXPORT int32_t KVAdapterManager::DeleteFreezeData(const std::string &key) } return DM_OK; } + +DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector &values) +{ + std::string dmKey = ComposeOsTypePrefix(); + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->GetAllOstypeData(dmKey, values) != DM_OK) { + LOGE("Get all 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) +{ + LOGI("key %{publkic}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str()); + std::string dmKey = ComposeOsTypePrefix() + key; + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Put(dmKey, value) != DM_OK) { + LOGE("Insert 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) +{ + LOGI("key %{publkic}s.", GetAnonyString(key).c_str()); + std::string dmKey = ComposeOsTypePrefix() + key; + CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL); + if (kvAdapter_->Delete(dmKey) != DM_OK) { + 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(key, value) != DM_OK) { + LOGE("Get data failed, key: %{public}s", GetAnonyString(key).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(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; +} + +DM_EXPORT int32_t KVAdapterManager::GetOsTypeCount(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