[device_profile部件] 清理无绑定关系的设备信息

Signed-off-by: wangyang2022 <wangyang412@huawei.com>
Change-Id: Ie20a3896f16e0b72f6de18691faf4556bda70320
This commit is contained in:
wangyang2022 2022-05-30 18:43:42 +08:00
parent 0a69e91b91
commit 3e25ef2071
9 changed files with 163 additions and 10 deletions

View File

@ -41,6 +41,8 @@ enum {
ERR_DP_DEVICE_SYNC_BUSY = 98566154,
ERR_DP_PERMISSION_DENIED = 98566155,
ERR_DP_UNTRUSTED_GROUP = 98566156,
ERR_DP_NOT_INIT_DB = 98566157,
ERR_DP_GET_NETWORKID_FAILED = 98566158,
};
} // namespace DeviceProfile
} // namespace OHOS

View File

@ -21,6 +21,7 @@
#include "device_auth.h"
#include "device_auth_defines.h"
#include "nlohmann/json.hpp"
#include "event_handler.h"
#include "single_instance.h"
namespace OHOS {
@ -44,8 +45,17 @@ public:
void Init();
bool CheckTrustGroup(const std::string& deviceId);
private:
static void OnDeviceUnBoundAdapter(const char* peerUdid, const char* groupInfo);
static bool CheckDeviceId(const std::string udid);
static std::string GetDeviceUdid();
void InitDataChangeListener();
private:
const DeviceGroupManager* hichainGmInstance_ = nullptr;
DataChangeListener dataChangeListener_;
static std::shared_ptr<AppExecFwk::EventHandler> trustGroupMgrHandler_;
};
} // namespace DeviceProfile
} // namespace OHOS

View File

@ -49,6 +49,7 @@ public:
const std::vector<std::string>& values);
virtual int32_t SyncDeviceProfile(const std::vector<std::string>& deviceIdList,
SyncMode syncMode);
virtual int32_t RemoveDeviceData(const std::string networkId);
virtual int32_t RegisterSyncCallback(const std::shared_ptr<DistributedKv::KvStoreSyncCallback>& sycnCb);
virtual int32_t UnRegisterSyncCallback();

View File

@ -61,6 +61,7 @@ public:
int32_t UnRegisterSyncCallback();
void NotifySyncCompleted();
void NotifySubscriberDied(const sptr<IRemoteObject>& profileEventNotifier);
int32_t RemoveUnBoundDeviceProfile(const std::string& udid);
private:
bool WaitKvDataService();

View File

@ -48,6 +48,7 @@ public:
DeviceIdType toType);
void GetDeviceIdList(std::list<std::string>& deviceIdList);
void GetDeviceList(std::list<std::shared_ptr<DeviceInfo>>& deviceList);
void RemoveDeviceIdsByUdid(const std::string& udid);
private:
static void OnNodeOnlineAdapter(NodeBasicInfo* info);
@ -62,6 +63,7 @@ private:
void AddLocalDeviceIds();
void AddDeviceIds(const std::string& networkId);
void RemoveDeviceIds(const std::string& networkId);
void RemoveExpireDeviceIds(const std::string& networkId);
void RecoverDevicesIfNeeded();
private:

View File

@ -17,6 +17,9 @@
#include "device_profile_errors.h"
#include "device_profile_log.h"
#include "device_profile_storage_manager.h"
#include "device_profile_utils.h"
#include "device_manager.h"
namespace OHOS {
namespace DeviceProfile {
@ -38,6 +41,8 @@ void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo)
jsonObject.at(FIELD_GROUP_VISIBILITY).get_to(groupInfo.groupVisibility);
}
std::shared_ptr<AppExecFwk::EventHandler> TrustGroupManager::trustGroupMgrHandler_;
void TrustGroupManager::Init()
{
if (InitDeviceAuthService() != ERR_OK) {
@ -50,9 +55,22 @@ void TrustGroupManager::Init()
HILOGE("auth GetGmInstance failed");
return;
}
InitDataChangeListener();
if(hichainGmInstance_->regDataChangeListener(AUTH_APPID.c_str(), &dataChangeListener_) != 0){
HILOGE("auth RegDataChangeListener failed");
return;
}
auto runner = AppExecFwk::EventRunner::Create("trustGroupMgr");
trustGroupMgrHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
HILOGI("init succeeded");
}
void TrustGroupManager::InitDataChangeListener()
{
dataChangeListener_.onDeviceUnBound = OnDeviceUnBoundAdapter;
}
bool TrustGroupManager::CheckTrustGroup(const std::string& deviceId)
{
if (hichainGmInstance_ == nullptr) {
@ -105,5 +123,50 @@ bool TrustGroupManager::CheckTrustGroup(const std::string& deviceId)
HILOGE("check failed, not in trust group");
return false;
}
void TrustGroupManager::OnDeviceUnBoundAdapter(const char* peerUdid, const char* groupInfo)
{
const std::string udid = peerUdid;
if (!CheckDeviceId(udid)) {
return;
}
auto removeUnBoundDeviceTask = [udid = std::move(udid)]() {
HILOGI("remove unbound deivce profile task start, udid = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
if(DeviceProfileStorageManager::GetInstance().RemoveUnBoundDeviceProfile(udid) != ERR_OK){
HILOGE("remove unbound device profile task failed, udid = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
} else {
HILOGI("remove unbound deivce profile task success, udid = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
}
DeviceManager::GetInstance().RemoveDeviceIdsByUdid(udid);
};
if (!trustGroupMgrHandler_->PostTask(removeUnBoundDeviceTask)) {
HILOGE("post task failed");
return;
}
}
bool TrustGroupManager::CheckDeviceId(const std::string udid)
{
std::string localDeviceId;
DeviceManager::GetInstance().GetLocalDeviceUdid(localDeviceId);
if (udid.empty() || localDeviceId.empty()) {
HILOGE("device id is empty");
return false;
}
if (udid == localDeviceId) {
HILOGI("device id is local device, not remove device profile, device udid = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
return false;
}
return true;
}
} // namespace DeviceProfile
} // namespace OHOS

View File

@ -267,6 +267,22 @@ int32_t DeviceProfileStorage::SyncDeviceProfile(const std::vector<std::string>&
return static_cast<int32_t>(status);
}
int32_t DeviceProfileStorage::RemoveDeviceData(const std::string networkId)
{
HILOGI("called");
std::unique_lock<std::shared_mutex> writeLock(storageLock_);
if (kvStorePtr_ == nullptr) {
return ERR_DP_INVALID_PARAMS;
}
Status status = kvStorePtr_->RemoveDeviceData(networkId);
if (status != Status::SUCCESS) {
HILOGE("remote device data failed, error = %{public}d", status);
}
return static_cast<int32_t>(status);
}
bool DeviceProfileStorage::CheckTrustGroup(const std::vector<std::string>& deviceIdList)
{
if (deviceIdList.empty()) {

View File

@ -264,6 +264,34 @@ int32_t DeviceProfileStorageManager::DeleteDeviceProfile(const std::string& serv
return errCode;
}
int32_t DeviceProfileStorageManager::RemoveUnBoundDeviceProfile(const std::string& udid)
{
if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_FAILED) {
HILOGE("kvstore init failed");
return ERR_DP_INIT_DB_FAILED;
}
std::unique_lock<std::mutex> autoLock(serviceLock_);
if (onlineSyncTbl_->GetInitStatus() != StorageInitStatus::INIT_SUCCEED) {
HILOGE("kvstore not init");
return ERR_DP_NOT_INIT_DB;
}
int32_t errCode = ERR_OK;
std::string networkId;
if (!DeviceManager::GetInstance().TransformDeviceId(udid, networkId, DeviceIdType::NETWORKID)) {
HILOGE("udid transform to networkid failed, udid = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
return ERR_DP_GET_NETWORKID_FAILED;
}
errCode = onlineSyncTbl_->RemoveDeviceData(networkId);
if (errCode != ERR_OK) {
HILOGE("remove unbound device profile failed, errorCode = %{public}d", errCode);
}
return errCode;
}
int32_t DeviceProfileStorageManager::SyncDeviceProfile(const SyncOptions& syncOptions,
const sptr<IRemoteObject>& profileEventNotifier)
{

View File

@ -94,11 +94,14 @@ void DeviceManager::OnNodeOnline(const std::shared_ptr<DeviceInfo> deviceInfo)
auto onlineNotifyTask = [this, deviceInfo = deviceInfo]() {
HILOGI("online networkId = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(deviceInfo->GetDeviceId()).c_str());
DeviceProfileStorageManager::GetInstance().OnNodeOnline(deviceInfo);
RemoveExpireDeviceIds(deviceInfo->GetDeviceId());
AddDeviceIds(deviceInfo->GetDeviceId());
std::string deviceId = deviceInfo->GetDeviceId();
std::lock_guard<std::mutex> autoLock(deviceLock_);
remoteDeviceInfoMap_[deviceId] = deviceInfo;
{
std::string deviceId = deviceInfo->GetDeviceId();
std::lock_guard<std::mutex> autoLock(deviceLock_);
remoteDeviceInfoMap_[deviceId] = deviceInfo;
}
DeviceProfileStorageManager::GetInstance().OnNodeOnline(deviceInfo);
};
if (!devMgrHandler_->PostTask(onlineNotifyTask)) {
HILOGE("post task failed");
@ -111,7 +114,6 @@ void DeviceManager::OnNodeOffline(const std::string& deviceId)
auto offlineNotifyTask = [this, deviceId = std::move(deviceId)]() {
HILOGI("offline networkId = %{public}s",
DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str());
RemoveDeviceIds(deviceId);
std::lock_guard<std::mutex> autoLock(deviceLock_);
remoteDeviceInfoMap_.erase(deviceId);
};
@ -215,16 +217,44 @@ void DeviceManager::AddDeviceIds(const std::string& networkId)
deviceIdsList_.emplace_back(std::move(deviceIds));
}
void DeviceManager::RemoveExpireDeviceIds(const std::string& networkId)
{
HILOGI("called");
std::string udid;
if (!GetUdidByNetworkId(networkId, udid)) {
return;
}
if(udid.empty()){
return;
}
RemoveDeviceIdsByUdid(udid);
}
void DeviceManager::RemoveDeviceIds(const std::string& networkId)
{
HILOGI("called");
std::lock_guard<std::mutex> autoLock(deviceLock_);
for (auto iter = deviceIdsList_.begin(); iter != deviceIdsList_.end(); iter++) {
if ((*iter)[static_cast<uint8_t>(DeviceIdType::NETWORKID)] == networkId) {
deviceIdsList_.erase(iter);
return;
}
auto iter = std::find_if(deviceIdsList_.begin(), deviceIdsList_.end(), [&networkId](const auto& deviceIds) {
return deviceIds[static_cast<uint8_t>(DeviceIdType::NETWORKID)] == networkId;
});
if (iter != deviceIdsList_.end()) {
deviceIdsList_.erase(iter);
}
return;
}
void DeviceManager::RemoveDeviceIdsByUdid(const std::string& udid)
{
HILOGI("called");
std::lock_guard<std::mutex> autoLock(deviceLock_);
auto iter = std::find_if(deviceIdsList_.begin(), deviceIdsList_.end(), [&udid](const auto& deviceIds) {
return deviceIds[static_cast<uint8_t>(DeviceIdType::UDID)] == udid;
});
if (iter != deviceIdsList_.end()) {
deviceIdsList_.erase(iter);
HILOGI("remove device udid %{public}s", DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
}
return;
}
void DeviceManager::DisconnectSoftbus()