diff --git a/common/include/dm_anonymous.h b/common/include/dm_anonymous.h index 12e5d9776..7fbd6facb 100644 --- a/common/include/dm_anonymous.h +++ b/common/include/dm_anonymous.h @@ -19,7 +19,6 @@ #include #include #include "nlohmann/json.hpp" -#include #include "dm_device_info.h" namespace OHOS { diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 36f7dcb03..8683ed3d1 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -398,13 +398,24 @@ std::vector DeviceProfileConnector::GetP LOGI("filterProfiles size is %{public}zu", filterProfiles.size()); std::vector processInfoVec; for (auto &item : filterProfiles) { - if (item.GetTrustDeviceId() != targetDeviceId || item.GetStatus() != ACTIVE) { + if (item.GetTrustDeviceId() != targetDeviceId) { continue; } + std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); + std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId(); OHOS::DistributedHardware::ProcessInfo processInfo; - processInfo.pkgName = item.GetAccesser().GetAccesserBundleName(); - processInfo.userId = item.GetAccesser().GetAccesserUserId(); - processInfoVec.push_back(processInfo); + if (accesserUdid == localDeviceId) { + processInfo.pkgName = item.GetAccesser().GetAccesserBundleName(); + processInfo.userId = item.GetAccesser().GetAccesserUserId(); + processInfoVec.push_back(processInfo); + continue; + } + if (accesseeUdid == localDeviceId) { + processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName(); + processInfo.userId = item.GetAccessee().GetAccesseeUserId(); + processInfoVec.push_back(processInfo); + continue; + } } return processInfoVec; } @@ -1096,21 +1107,20 @@ int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid, int32_t beforeUserId) { LOGI("Start."); - std::vector profiles = GetAccessControlProfileByUserId(beforeUserId); - LOGI("to inactive ACL size is %{public}zu", profiles.size()); + std::vector profiles = GetAllAccessControlProfile(); for (auto &item : profiles) { if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && - item.GetAccesser().GetAccesserUserId() == beforeUserId) || + item.GetAccesser().GetAccesserUserId() == beforeUserId && item.GetStatus() == ACTIVE) || (item.GetAccessee().GetAccesseeDeviceId() == localUdid && - item.GetAccessee().GetAccesseeUserId() == beforeUserId)) { + item.GetAccessee().GetAccesseeUserId() == beforeUserId && item.GetStatus() == ACTIVE)) { item.SetStatus(INACTIVE); DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); continue; } if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && - item.GetAccesser().GetAccesserUserId() == currentUserId) || ( + item.GetAccesser().GetAccesserUserId() == currentUserId && item.GetStatus() == INACTIVE) || ( item.GetAccessee().GetAccesseeDeviceId() == localUdid && - item.GetAccessee().GetAccesseeUserId() == currentUserId)) { + item.GetAccessee().GetAccesseeUserId() == currentUserId && item.GetStatus() == INACTIVE)) { item.SetStatus(ACTIVE); DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); continue; @@ -1152,7 +1162,7 @@ void DeviceProfileConnector::HandleSyncForegroundUserIdEvent(const std::vector DeviceProfileConnector::GetOfflineProcessInfo(std::stri int32_t bindLevel = 100; for (const auto &item : profiles) { ProcessInfo processInfo; - if (item.GetAccesser().GetAccesserDeviceId() == localUdid && - item.GetAccessee().GetAccesseeDeviceId() == remoteUdid && - find(localUserIds.begin(), localUserIds.end(), - item.GetAccesser().GetAccesserUserId()) != localUserIds.end() && - find(remoteUserIds.begin(), remoteUserIds.end(), - item.GetAccessee().GetAccesseeUserId()) != remoteUserIds.end() && + std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); + std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId(); + int32_t accesserUserId = item.GetAccesser().GetAccesserUserId(); + int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId(); + if (accesserUdid == localUdid && accesseeUdid == remoteUdid && + find(localUserIds.begin(), localUserIds.end(), accesserUserId) != localUserIds.end() && + find(remoteUserIds.begin(), remoteUserIds.end(), accesseeUserId) != remoteUserIds.end() && item.GetStatus() == ACTIVE) { processInfo.pkgName = item.GetAccesser().GetAccesserBundleName(); processInfo.userId = item.GetAccesser().GetAccesserUserId(); bindLevel = std::min(bindLevel, static_cast(item.GetBindLevel())); - } else if (item.GetAccessee().GetAccesseeDeviceId() == localUdid && - item.GetAccesser().GetAccesserDeviceId() == remoteUdid && - find(localUserIds.begin(), localUserIds.end(), - item.GetAccessee().GetAccesseeUserId()) != localUserIds.end() && - find(remoteUserIds.begin(), remoteUserIds.end(), - item.GetAccesser().GetAccesserUserId()) != remoteUserIds.end() && + processInfos.push_back(processInfo); + } else if (accesseeUdid == localUdid && accesserUdid == remoteUdid && + find(localUserIds.begin(), localUserIds.end(), accesseeUserId) != localUserIds.end() && + find(remoteUserIds.begin(), remoteUserIds.end(), accesserUserId) != remoteUserIds.end() && item.GetStatus() == ACTIVE) { processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName(); processInfo.userId = item.GetAccessee().GetAccesseeUserId(); bindLevel = std::min(bindLevel, static_cast(item.GetBindLevel())); + processInfos.push_back(processInfo); } - processInfos.push_back(processInfo); } if (bindLevel == INVALIED_TYPE || bindLevel == DEVICE) { processInfos.clear(); @@ -1220,9 +1229,6 @@ std::map DeviceProfileConnector::GetUserIdAndBindLevel(const s std::vector profiles = GetAllAccessControlProfile(); std::map userIdAndBindLevel; for (const auto &item : profiles) { - if (item.GetStatus() == INACTIVE) { - continue; - } std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId(); int32_t accesserUserid = item.GetAccesser().GetAccesserUserId(); @@ -1370,16 +1376,16 @@ void DeviceProfileConnector::HandleSyncBackgroundUserIdEvent(const std::vector profiles = GetAllAccessControlProfile(); for (auto &item : profiles) { - if (item.GetAccesser().GetAccesserDeviceId() == localUdid && - item.GetAccessee().GetAccesseeDeviceId() == remoteUdid && - find(remoteUserIds.begin(), remoteUserIds.end(), - item.GetAccessee().GetAccesseeUserId()) != remoteUserIds.end()) { + std::string accesserDeviceId = item.GetAccesser().GetAccesserDeviceId(); + std::string accesseeDeviceId = item.GetAccessee().GetAccesseeDeviceId(); + int32_t accesserUserId = item.GetAccesser().GetAccesserUserId(); + int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId(); + if (accesserDeviceId == localUdid && accesseeDeviceId == remoteUdid && item.GetStatus() == ACTIVE && + find(remoteUserIds.begin(), remoteUserIds.end(), accesseeUserId) != remoteUserIds.end()) { item.SetStatus(INACTIVE); DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); - } else if ((item.GetAccessee().GetAccesseeDeviceId() == localUdid && - item.GetAccesser().GetAccesserDeviceId() == remoteUdid) && - find(remoteUserIds.begin(), remoteUserIds.end(), - item.GetAccesser().GetAccesserUserId()) == remoteUserIds.end()) { + } else if ((accesseeDeviceId == localUdid && accesserDeviceId == remoteUdid) && item.GetStatus() == ACTIVE && + find(remoteUserIds.begin(), remoteUserIds.end(), accesserUserId) != remoteUserIds.end()) { item.SetStatus(INACTIVE); DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); } diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index a0df243b8..19bb1be7e 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -235,8 +235,8 @@ void DeviceManagerServiceImpl::HandleOffline(DmDeviceState devState, DmDeviceInf item.first); softbusConnector_->SetProcessInfoVec(processInfoVec); } + deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo); } - deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo); } void DeviceManagerServiceImpl::HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo) @@ -731,20 +731,6 @@ void DeviceManagerServiceImpl::HandleUserSwitched(const std::map(localDeviceId); - ProcessInfo processInfo; - processInfo.pkgName = std::string(DM_PKG_NAME); - processInfo.userId = beforeUserId; - for (const auto &item : deviceMap) { - if (item.second == INVALIED_TYPE || item.second == DEVICE) { - softbusConnector_->SetProcessInfo(processInfo); - } else { - std::vector processInfoVec = - DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(localUdid, - item.first, beforeUserId); - softbusConnector_->SetProcessInfoVec(processInfoVec); - } - softbusConnector_->HandleDeviceOffline(item.first); - } DeviceProfileConnector::GetInstance().HandleUserSwitched(localUdid, currentUserId, beforeUserId); } @@ -897,14 +883,6 @@ void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector } DeviceProfileConnector::GetInstance().UpdatePeerUserId(localUdid, localUserIds, remoteUdid, rmtFrontUserIdsTemp, rmtBackUserIdsTemp); - std::vector offlineProcInfo = - DeviceProfileConnector::GetInstance().GetOfflineProcessInfo(localUdid, localUserIds, - remoteUdid, rmtBackUserIdsTemp); - if (!offlineProcInfo.empty()) { - CHECK_NULL_VOID(softbusConnector_); - softbusConnector_->SetProcessInfoVec(offlineProcInfo); - softbusConnector_->HandleDeviceOffline(remoteUdid); - } DeviceProfileConnector::GetInstance().HandleSyncForegroundUserIdEvent(rmtFrontUserIdsTemp, remoteUdid, localUserIds, localUdid); DeviceProfileConnector::GetInstance().HandleSyncBackgroundUserIdEvent(rmtBackUserIdsTemp, remoteUdid, diff --git a/services/implementation/src/devicestate/dm_device_state_manager.cpp b/services/implementation/src/devicestate/dm_device_state_manager.cpp index 9c3d367a6..bce366f51 100644 --- a/services/implementation/src/devicestate/dm_device_state_manager.cpp +++ b/services/implementation/src/devicestate/dm_device_state_manager.cpp @@ -170,7 +170,9 @@ void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState CHECK_NULL_VOID(listener_); std::vector processInfoVec = softbusConnector_->GetProcessInfo(); for (const auto &item : processInfoVec) { - listener_->OnDeviceStateChange(item, devState, devInfo); + if (!item.pkgName.empty()) { + listener_->OnDeviceStateChange(item, devState, devInfo); + } } } diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 74df27405..8573c5046 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -109,12 +109,11 @@ private: const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo); void ProcessAppOffline(const std::vector procInfoVec, const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo); - void RemoveNotExistProcess(const std::vector &procInfoVec); private: #if !defined(__LITEOS_M__) IpcServerListener ipcServerListener_; static std::mutex alreadyNotifyPkgNameLock_; - static std::map alreadyOnlinePkgName_; + static std::map alreadyOnlinePkgName_; #endif }; } // namespace DistributedHardware diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index f6cf0be99..86ac6e138 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -49,7 +49,7 @@ namespace OHOS { namespace DistributedHardware { std::mutex DeviceManagerServiceListener::alreadyNotifyPkgNameLock_; -std::map DeviceManagerServiceListener::alreadyOnlinePkgName_ = {}; +std::map DeviceManagerServiceListener::alreadyOnlinePkgName_ = {}; void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo) { @@ -557,7 +557,7 @@ void DeviceManagerServiceListener::OnAppUnintall(const std::string &pkgName) { std::lock_guard autoLock(alreadyNotifyPkgNameLock_); for (auto it = alreadyOnlinePkgName_.begin(); it != alreadyOnlinePkgName_.end();) { - if (it->first.processPkgName == pkgName) { + if (it->first.find(pkgName) != std::string::npos) { it = alreadyOnlinePkgName_.erase(it); } else { ++it; @@ -651,18 +651,14 @@ void DeviceManagerServiceListener::ProcessDeviceOnline(const std::vector pReq = std::make_shared(); std::shared_ptr pRsp = std::make_shared(); for (const auto &it : procInfoVec) { - DmNotifyKey notifyKey; - notifyKey.processPkgName = it.pkgName; - notifyKey.processUserId = it.userId; - notifyKey.notifyUserId = processInfo.userId; - notifyKey.udid = info.deviceId; + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); DmDeviceState notifyState = state; { std::lock_guard autoLock(alreadyNotifyPkgNameLock_); - if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { notifyState = DmDeviceState::DEVICE_INFO_CHANGED; } else { - alreadyOnlinePkgName_[notifyKey] = info; + alreadyOnlinePkgName_[notifyPkgName] = info; } } SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); @@ -678,20 +674,14 @@ void DeviceManagerServiceListener::ProcessDeviceOffline(const std::vector pReq = std::make_shared(); std::shared_ptr pRsp = std::make_shared(); - RemoveNotExistProcess(procInfoVec); - if (!SoftbusCache::GetInstance().CheckIsOnline(std::string(info.deviceId))) { - RemoveOnlinePkgName(info); - } for (const auto &it : procInfoVec) { - DmNotifyKey notifyKey; - notifyKey.processPkgName = it.pkgName; - notifyKey.processUserId = it.userId; - notifyKey.notifyUserId = processInfo.userId; - notifyKey.udid = info.deviceId; + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); { std::lock_guard autoLock(alreadyNotifyPkgNameLock_); - if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { alreadyOnlinePkgName_.erase(notifyKey); + } else { + continue; } } SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); @@ -722,18 +712,14 @@ void DeviceManagerServiceListener::ProcessAppOnline(const std::vector pReq = std::make_shared(); std::shared_ptr pRsp = std::make_shared(); for (const auto &it : procInfoVec) { - DmNotifyKey notifyKey; - notifyKey.processPkgName = it.pkgName; - notifyKey.processUserId = it.userId; - notifyKey.notifyUserId = processInfo.userId; - notifyKey.udid = info.deviceId; + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); DmDeviceState notifyState = state; { std::lock_guard autoLock(alreadyNotifyPkgNameLock_); - if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { notifyState = DmDeviceState::DEVICE_INFO_CHANGED; } else { - alreadyOnlinePkgName_[notifyKey] = info; + alreadyOnlinePkgName_[notifyPkgName] = info; } } SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); @@ -750,21 +736,25 @@ void DeviceManagerServiceListener::ProcessAppOffline(const std::vector pReq = std::make_shared(); std::shared_ptr pRsp = std::make_shared(); if (!SoftbusCache::GetInstance().CheckIsOnline(std::string(info.deviceId))) { - RemoveOnlinePkgName(info); for (const auto &it : procInfoVec) { + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); + { + std::lock_guard autoLock(alreadyNotifyPkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { + alreadyOnlinePkgName_.erase(notifyKey); + } else { + continue; + } + } SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); } } else { - DmNotifyKey notifyKey; - notifyKey.processPkgName = processInfo.pkgName; - notifyKey.processUserId = processInfo.userId; - notifyKey.notifyUserId = processInfo.userId; - notifyKey.udid = info.deviceId; + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); { std::lock_guard autoLock(alreadyNotifyPkgNameLock_); - if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { - alreadyOnlinePkgName_.erase(notifyKey); + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { + alreadyOnlinePkgName_.erase(notifyPkgName); } } SetDeviceInfo(pReq, processInfo, state, info, deviceBasicInfo); @@ -772,26 +762,12 @@ void DeviceManagerServiceListener::ProcessAppOffline(const std::vector &procInfoVec) -{ - std::lock_guard autoLock(alreadyNotifyPkgNameLock_); - for (auto it = alreadyOnlinePkgName_.begin(); it != alreadyOnlinePkgName_.end();) { - ProcessInfo processInfo; - processInfo.pkgName = it->first.processPkgName; - processInfo.userId = it->first.processUserId; - if (find(procInfoVec.begin(), procInfoVec.end(), processInfo) != procInfoVec.end()) { - it = alreadyOnlinePkgName_.erase(it); - } else { - ++it; - } - } -} - void DeviceManagerServiceListener::OnProcessRemove(const ProcessInfo &processInfo) { std::lock_guard autoLock(alreadyNotifyPkgNameLock_); + std::string notifyPkgName = processInfo.pkgName + "#" + std::to_string(processInfo.userId) for (auto it = alreadyOnlinePkgName_.begin(); it != alreadyOnlinePkgName_.end();) { - if (processInfo.pkgName == it->first.processPkgName && processInfo.userId == it->first.processUserId) { + if (it->first.find(notifyPkgName) != std::string::npos) { it = alreadyOnlinePkgName_.erase(it); } else { ++it;