修改发现回调

Signed-off-by: yangwei_814916 <yangwei271@huawei.com>
This commit is contained in:
yangwei_814916 2024-11-17 11:56:15 +08:00
parent 6a2c7e1103
commit 30227ba850
6 changed files with 73 additions and 113 deletions

View File

@ -19,7 +19,6 @@
#include <map>
#include <string>
#include "nlohmann/json.hpp"
#include <unordered_map>
#include "dm_device_info.h"
namespace OHOS {

View File

@ -398,13 +398,24 @@ std::vector<OHOS::DistributedHardware::ProcessInfo> DeviceProfileConnector::GetP
LOGI("filterProfiles size is %{public}zu", filterProfiles.size());
std::vector<OHOS::DistributedHardware::ProcessInfo> 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<AccessControlProfile> profiles = GetAccessControlProfileByUserId(beforeUserId);
LOGI("to inactive ACL size is %{public}zu", profiles.size());
std::vector<AccessControlProfile> 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<i
find(localUserIds.begin(), localUserIds.end(),
item.GetAccesser().GetAccesserUserId()) != localUserIds.end() &&
find(remoteUserIds.begin(), remoteUserIds.end(),
item.GetAccessee().GetAccesseeUserId()) != remoteUserIds.end()) {
item.GetAccessee().GetAccesseeUserId()) != remoteUserIds.end() && item.GetStatus() == INACTIVE) {
item.SetStatus(ACTIVE);
DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
} else if (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
@ -1160,7 +1170,7 @@ void DeviceProfileConnector::HandleSyncForegroundUserIdEvent(const std::vector<i
find(localUserIds.begin(), localUserIds.end(),
item.GetAccessee().GetAccesseeUserId()) != localUserIds.end() &&
find(remoteUserIds.begin(), remoteUserIds.end(),
item.GetAccesser().GetAccesserUserId()) != remoteUserIds.end()) {
item.GetAccesser().GetAccesserUserId()) != remoteUserIds.end() && item.GetStatus() == INACTIVE) {
item.SetStatus(ACTIVE);
DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
}
@ -1177,28 +1187,27 @@ std::vector<ProcessInfo> 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<int32_t>(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<int32_t>(item.GetBindLevel()));
processInfos.push_back(processInfo);
}
processInfos.push_back(processInfo);
}
if (bindLevel == INVALIED_TYPE || bindLevel == DEVICE) {
processInfos.clear();
@ -1220,9 +1229,6 @@ std::map<int32_t, int32_t> DeviceProfileConnector::GetUserIdAndBindLevel(const s
std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
std::map<int32_t, int32_t> 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<i
GetAnonyString(remoteUdid).c_str());
std::vector<AccessControlProfile> 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);
}

View File

@ -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<std::string, in
char localDeviceId[DEVICE_UUID_LENGTH] = {0};
GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
std::string localUdid = static_cast<std::string>(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<ProcessInfo> 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<uint32_t>
}
DeviceProfileConnector::GetInstance().UpdatePeerUserId(localUdid, localUserIds, remoteUdid,
rmtFrontUserIdsTemp, rmtBackUserIdsTemp);
std::vector<ProcessInfo> 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,

View File

@ -170,7 +170,9 @@ void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState
CHECK_NULL_VOID(listener_);
std::vector<ProcessInfo> processInfoVec = softbusConnector_->GetProcessInfo();
for (const auto &item : processInfoVec) {
listener_->OnDeviceStateChange(item, devState, devInfo);
if (!item.pkgName.empty()) {
listener_->OnDeviceStateChange(item, devState, devInfo);
}
}
}

View File

@ -109,12 +109,11 @@ private:
const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo);
void ProcessAppOffline(const std::vector<ProcessInfo> procInfoVec, const ProcessInfo &processInfo,
const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo);
void RemoveNotExistProcess(const std::vector<ProcessInfo> &procInfoVec);
private:
#if !defined(__LITEOS_M__)
IpcServerListener ipcServerListener_;
static std::mutex alreadyNotifyPkgNameLock_;
static std::map<DmNotifyKey, DmDeviceInfo> alreadyOnlinePkgName_;
static std::map<std::string, DmDeviceInfo> alreadyOnlinePkgName_;
#endif
};
} // namespace DistributedHardware

View File

@ -49,7 +49,7 @@
namespace OHOS {
namespace DistributedHardware {
std::mutex DeviceManagerServiceListener::alreadyNotifyPkgNameLock_;
std::map<DmNotifyKey, DmDeviceInfo> DeviceManagerServiceListener::alreadyOnlinePkgName_ = {};
std::map<std::string, DmDeviceInfo> 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<std::mutex> 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<Process
std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
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<std::mutex> 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<Proces
GetAnonyString(info.deviceId).c_str());
std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
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<std::mutex> 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<ProcessInf
std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
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<std::mutex> 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<ProcessIn
std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
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<std::mutex> 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<std::mutex> 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<ProcessIn
}
}
void DeviceManagerServiceListener::RemoveNotExistProcess(const std::vector<ProcessInfo> &procInfoVec)
{
std::lock_guard<std::mutex> 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<std::mutex> 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;