DP多用户update

Signed-off-by: wangzhaohao <wangzhaohao@huawei.com>
This commit is contained in:
wangzhaohao 2024-11-07 19:29:29 +08:00
parent 7b485a81ac
commit 0a3be585b4
17 changed files with 44 additions and 68 deletions

View File

@ -53,7 +53,7 @@ public:
void SetCharacteristicKey(const std::string& characteristicId);
std::string GetCharacteristicValue() const;
void SetCharacteristicValue(const std::string& characteristicValue);
bool GetIsMultiUser() const;
bool IsMultiUser() const;
void SetIsMultiUser(bool isMultiUser);
int32_t GetUserId() const;
void SetUserId(int32_t userId);

View File

@ -64,7 +64,7 @@ public:
void SetOsVersion(const std::string& osVersion);
int32_t GetOsType() const;
void SetOsType(int32_t osType);
bool GetIsMultiUser() const;
bool IsMultiUser() const;
void SetIsMultiUser(bool isMultiUser);
int32_t GetUserId() const;
void SetUserId(int32_t userId);

View File

@ -37,7 +37,7 @@ public:
void SetServiceName(const std::string& serviceName);
std::string GetServiceType() const;
void SetServiceType(const std::string& serviceType);
bool GetIsMultiUser() const;
bool IsMultiUser() const;
void SetIsMultiUser(bool isMultiUser);
int32_t GetUserId() const;
void SetUserId(int32_t userId);

View File

@ -99,7 +99,7 @@ const std::string SUBSCRIBE_DEVICE_PROFILE = "SubscribeDeviceProfile";
const std::string UNSUBSCRIBE_DEVICE_PROFILE = "UnSubscribeDeviceProfile";
const std::string SYNC_DEVICE_PROFILE = "SyncDeviceProfile";
/* Common constants */
const std::string IS_MULTI_USER = "is_mulit_user";
const std::string IS_MULTI_USER = "is_multi_user";
const std::string SEPARATOR = "#";
const std::string DEV_PREFIX = "dev";
const std::string SVR_PREFIX = "svr";

View File

@ -64,7 +64,7 @@ void CharacteristicProfile::SetCharacteristicValue(const std::string& characteri
characteristicValue_ = characteristicValue;
}
bool CharacteristicProfile::GetIsMultiUser() const
bool CharacteristicProfile::IsMultiUser() const
{
return isMultiUser_;
}
@ -110,7 +110,7 @@ bool CharacteristicProfile::operator!=(const CharacteristicProfile& charProfile)
{
bool isNotEqual = (deviceId_ != charProfile.GetDeviceId() || serviceName_ != charProfile.GetServiceName() ||
characteristicKey_ != charProfile.GetCharacteristicKey() ||
characteristicValue_ != charProfile.GetCharacteristicValue() || isMultiUser_ != charProfile.GetIsMultiUser() ||
characteristicValue_ != charProfile.GetCharacteristicValue() || isMultiUser_ != charProfile.IsMultiUser() ||
userId_ != charProfile.GetUserId());
if (isNotEqual) {
return true;
@ -130,7 +130,7 @@ std::string CharacteristicProfile::dump() const
cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str());
cJSON_AddStringToObject(json, CHARACTERISTIC_KEY.c_str(), characteristicKey_.c_str());
cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_);
cJSON_AddStringToObject(json, USER_ID.c_str(), ProfileUtils::GetAnonyInt32(userId_).c_str());
cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_);
if (characteristicKey_ == SWITCH_STATUS) {
cJSON_AddStringToObject(json, CHARACTERISTIC_VALUE.c_str(), characteristicValue_.c_str());
} else {

View File

@ -134,7 +134,7 @@ void DeviceProfile::SetOsSysCap(const std::string& osSysCap)
osSysCap_ = osSysCap;
}
bool DeviceProfile::GetIsMultiUser() const
bool DeviceProfile::IsMultiUser() const
{
return isMultiUser_;
}
@ -197,7 +197,7 @@ bool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const
manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() ||
storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() ||
osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || osType_ !=
deviceProfile.GetOsType() || isMultiUser_ != deviceProfile.GetIsMultiUser() || userId_ !=
deviceProfile.GetOsType() || isMultiUser_ != deviceProfile.IsMultiUser() || userId_ !=
deviceProfile.GetUserId());
if (isNotEqual) {
return true;
@ -225,7 +225,7 @@ std::string DeviceProfile::dump() const
cJSON_AddStringToObject(json, OS_VERSION.c_str(), osVersion_.c_str());
cJSON_AddNumberToObject(json, OS_TYPE.c_str(), osType_);
cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_);
cJSON_AddStringToObject(json, USER_ID.c_str(), ProfileUtils::GetAnonyInt32(userId_).c_str());
cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_);
char* jsonChars = cJSON_PrintUnformatted(json);
if (jsonChars == NULL) {
cJSON_Delete(json);
@ -262,7 +262,7 @@ std::string DeviceProfile::AnnoymizeDump() const
cJSON_AddStringToObject(json, OS_TYPE.c_str(),
ProfileUtils::GetAnonyString(std::to_string(osType_)).c_str());
cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_);
cJSON_AddStringToObject(json, USER_ID.c_str(), ProfileUtils::GetAnonyInt32(userId_).c_str());
cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_);
char* jsonChars = cJSON_PrintUnformatted(json);
if (jsonChars == NULL) {
cJSON_Delete(json);

View File

@ -100,6 +100,9 @@ std::string SubscribeInfo::GetSubscribeKey() const
void SubscribeInfo::SetSubscribeKey(const std::string& subscribeKey, int32_t userId)
{
subscribeKey_ = subscribeKey;
if (userId != DEFAULT_USER_ID) {
subscribeKey_ = subscribeKey_ + SEPARATOR + std::to_string(userId);
}
}
sptr<IRemoteObject> SubscribeInfo::GetListener() const

View File

@ -73,7 +73,7 @@ void ServiceProfile::SetServiceType(const std::string& serviceType)
serviceType_ = serviceType;
}
bool ServiceProfile::GetIsMultiUser() const
bool ServiceProfile::IsMultiUser() const
{
return isMultiUser_;
}
@ -116,7 +116,7 @@ bool ServiceProfile::UnMarshalling(MessageParcel& parcel)
bool ServiceProfile::operator!=(const ServiceProfile& serviceProfile) const
{
bool isNotEqual = (deviceId_ != serviceProfile.GetDeviceId() || serviceName_ != serviceProfile.GetServiceName() ||
serviceType_ != serviceProfile.GetServiceType() || isMultiUser_ != serviceProfile.GetIsMultiUser() ||
serviceType_ != serviceProfile.GetServiceType() || isMultiUser_ != serviceProfile.IsMultiUser() ||
userId_ != serviceProfile.GetUserId());
if (isNotEqual) {
return true;
@ -136,7 +136,7 @@ std::string ServiceProfile::dump() const
cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str());
cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str());
cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_);
cJSON_AddStringToObject(json, USER_ID.c_str(), ProfileUtils::GetAnonyInt32(userId_).c_str());
cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_);
char* jsonChars = cJSON_PrintUnformatted(json);
if (jsonChars == NULL) {
cJSON_Delete(json);

View File

@ -82,22 +82,16 @@ std::string ProfileUtils::GetAnonyString(const std::string& value)
std::string ProfileUtils::GetAnonyInt32(const int32_t value)
{
constexpr int32_t INT32_STRING_LENGTH = 40;
char tempBuffer[INT32_STRING_LENGTH] = "";
int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value);
size_t length = strlen(tempBuffer);
if (secRet <= 0 || length == 0) {
std::string nullString("");
return nullString;
}
for (size_t i = 1; i <= length - 1; ++i) {
tempBuffer[i] = '*';
}
std::string tempString = std::to_string(value);
size_t length = tempString.length();
if (length == 0x01) {
tempBuffer[0] = '*';
tempString[0] = '*';
return tempString;
}
std::string tempBufferStr(tempBuffer);
return tempBufferStr;
for (size_t i = 1; i < length - 1; i++) {
tempString[i] = '*';
}
return tempString;
}
std::vector<std::string> ProfileUtils::GetOnlineDevices()

View File

@ -60,11 +60,8 @@ public:
ServiceProfile& serviceProfile);
int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
const std::string& characteristicId, CharacteristicProfile& characteristicProfile);
int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName);
int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName,
bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID);
int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
const std::string& characteristicKey);
int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
const std::string& characteristicKey, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID);
int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo);

View File

@ -285,12 +285,6 @@ int32_t DistributedDeviceProfileClient::GetCharacteristicProfile(const std::stri
return dpService->GetCharacteristicProfile(deviceId, serviceName, characteristicId, characteristicProfile);
}
int32_t DistributedDeviceProfileClient::DeleteServiceProfile(const std::string& deviceId,
const std::string& serviceName)
{
return DeleteServiceProfile(deviceId, serviceName, false, DEFAULT_USER_ID);
}
int32_t DistributedDeviceProfileClient::DeleteServiceProfile(const std::string& deviceId,
const std::string& serviceName, bool isMultiUser, int32_t userId)
{
@ -302,12 +296,6 @@ int32_t DistributedDeviceProfileClient::DeleteServiceProfile(const std::string&
return dpService->DeleteServiceProfile(deviceId, serviceName, isMultiUser, userId);
}
int32_t DistributedDeviceProfileClient::DeleteCharacteristicProfile(const std::string& deviceId,
const std::string& serviceName, const std::string& characteristicKey)
{
return DeleteCharacteristicProfile(deviceId, serviceName, characteristicKey, false, DEFAULT_USER_ID);
}
int32_t DistributedDeviceProfileClient::DeleteCharacteristicProfile(const std::string& deviceId,
const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser, int32_t userId)
{

View File

@ -200,7 +200,7 @@ int32_t DistributedDeviceProfileProxy::GetDeviceProfile(const std::string& devic
MessageParcel data;
WRITE_INTERFACE_TOKEN(data);
WRITE_HELPER(data, String, deviceId);
WRITE_HELPER(data, Bool, deviceProfile.GetIsMultiUser());
WRITE_HELPER(data, Bool, deviceProfile.IsMultiUser());
WRITE_HELPER(data, Int32, deviceProfile.GetUserId());
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(DPInterfaceCode::GET_DEVICE_PROFILE_NEW), data, reply);
@ -220,7 +220,7 @@ int32_t DistributedDeviceProfileProxy::GetServiceProfile(const std::string& devi
WRITE_INTERFACE_TOKEN(data);
WRITE_HELPER(data, String, deviceId);
WRITE_HELPER(data, String, serviceName);
WRITE_HELPER(data, Bool, serviceProfile.GetIsMultiUser());
WRITE_HELPER(data, Bool, serviceProfile.IsMultiUser());
WRITE_HELPER(data, Int32, serviceProfile.GetUserId());
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(DPInterfaceCode::GET_SERVICE_PROFILE), data, reply);
@ -241,7 +241,7 @@ int32_t DistributedDeviceProfileProxy::GetCharacteristicProfile(const std::strin
WRITE_HELPER(data, String, deviceId);
WRITE_HELPER(data, String, serviceName);
WRITE_HELPER(data, String, characteristicId);
WRITE_HELPER(data, Bool, charProfile.GetIsMultiUser());
WRITE_HELPER(data, Bool, charProfile.IsMultiUser());
WRITE_HELPER(data, Int32, charProfile.GetUserId());
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(DPInterfaceCode::GET_CHAR_PROFILE), data, reply);

View File

@ -113,7 +113,7 @@ int32_t DeviceProfileManager::PutDeviceProfile(const DeviceProfile& deviceProfil
return DP_INVALID_PARAMS;
}
std::map<std::string, std::string> entries;
if (deviceProfile.GetIsMultiUser()) {
if (deviceProfile.IsMultiUser()) {
ProfileUtils::DeviceProfileToEntries(deviceProfile, entries, true);
if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == deviceProfile.GetUserId()) {
ProfileUtils::DeviceProfileToEntries(deviceProfile, entries);
@ -150,7 +150,7 @@ int32_t DeviceProfileManager::PutServiceProfile(const ServiceProfile& servicePro
return DP_INVALID_PARAMS;
}
std::map<std::string, std::string> entries;
if (serviceProfile.GetIsMultiUser()) {
if (serviceProfile.IsMultiUser()) {
ProfileUtils::ServiceProfileToEntries(serviceProfile, entries, true);
if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == serviceProfile.GetUserId()) {
ProfileUtils::ServiceProfileToEntries(serviceProfile, entries);
@ -188,7 +188,7 @@ int32_t DeviceProfileManager::PutServiceProfileBatch(const std::vector<ServicePr
HILOGE("the profile is invalid! serviceProfile:%{public}s", serviceProfile.dump().c_str());
continue;
}
if (serviceProfile.GetIsMultiUser()) {
if (serviceProfile.IsMultiUser()) {
ProfileUtils::ServiceProfileToEntries(serviceProfile, entries, true);
if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == serviceProfile.GetUserId()) {
ProfileUtils::ServiceProfileToEntries(serviceProfile, entries);
@ -226,7 +226,7 @@ int32_t DeviceProfileManager::PutCharacteristicProfile(const CharacteristicProfi
return DP_INVALID_PARAMS;
}
std::map<std::string, std::string> entries;
if (charProfile.GetIsMultiUser()) {
if (charProfile.IsMultiUser()) {
ProfileUtils::CharacteristicProfileToEntries(charProfile, entries, true);
if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == charProfile.GetUserId()) {
ProfileUtils::CharacteristicProfileToEntries(charProfile, entries);
@ -264,7 +264,7 @@ int32_t DeviceProfileManager::PutCharacteristicProfileBatch(const std::vector<Ch
HILOGE("the profile is invalid! charProfile:%{public}s", charProfile.dump().c_str());
continue;
}
if (charProfile.GetIsMultiUser()) {
if (charProfile.IsMultiUser()) {
ProfileUtils::CharacteristicProfileToEntries(charProfile, entries, true);
if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == charProfile.GetUserId()) {
ProfileUtils::CharacteristicProfileToEntries(charProfile, entries);
@ -1061,8 +1061,7 @@ void DeviceProfileManager::FixDiffProfiles()
void DeviceProfileManager::OnUserChange(int32_t lastUserId, int32_t curUserId)
{
HILOGI("lastUserId:%{public}s,curUserId:%{public}s",
ProfileUtils::GetAnonyInt32(lastUserId).c_str(), ProfileUtils::GetAnonyInt32(curUserId).c_str());
HILOGI("lastUserId:%{public}d,curUserId:%{public}d", lastUserId, curUserId);
if (lastUserId == curUserId) {
HILOGW("user not change");
return;
@ -1110,7 +1109,6 @@ void DeviceProfileManager::OnUserChange(int32_t lastUserId, int32_t curUserId)
HILOGE("DeleteBatchByKeys fail ret=%{public}d", ret);
return;
}
HILOGI("end");
}
int32_t DeviceProfileManager::SaveBatchByKeys(const std::map<std::string, std::string>& entries)
@ -1136,11 +1134,10 @@ int32_t DeviceProfileManager::SaveBatchByKeys(const std::map<std::string, std::s
template <typename T>
int32_t DeviceProfileManager::IsMultiUserValid(const T& profile)
{
if (profile.GetDeviceId().empty() || (profile.GetIsMultiUser() && profile.GetUserId() <= 0) ||
(!profile.GetIsMultiUser() && profile.GetUserId() != DEFAULT_USER_ID)) {
HILOGE("multi-user params are invalid, isMultiUser: %{public}d, userId: %{public}s",
profile.GetIsMultiUser(),
ProfileUtils::GetAnonyInt32(profile.GetUserId()).c_str());
if (profile.GetDeviceId().empty() || (profile.IsMultiUser() && profile.GetUserId() <= 0) ||
(!profile.IsMultiUser() && profile.GetUserId() != DEFAULT_USER_ID)) {
HILOGE("multi-user params are invalid, isMultiUser: %{public}d, userId: %{public}d",
profile.IsMultiUser(), profile.GetUserId());
return DP_GET_MULTI_USER_PROFILE_PARAMS_INVALID;
}
return DP_SUCCESS;

View File

@ -651,9 +651,6 @@ void DistributedDeviceProfileServiceNew::AccountCommonEventCallback(int32_t user
if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
// swithed
MultiUserManager::GetInstance().SetCurrentForegroundUserID(userId);
} else if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
} else if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
} else if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
} else {
HILOGE("Invalied account common event.");
}

View File

@ -41,7 +41,7 @@ int32_t MultiUserManager::Init()
if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) {
HILOGD("GetForegroundId failed, res:%{public}d", res);
}
HILOGI("current foregroundId = %{public}s", ProfileUtils::GetAnonyInt32(foregroundId).c_str());
HILOGI("current foregroundId = %{public}d", foregroundId);
SetCurrentForegroundUserID(foregroundId);
return DP_SUCCESS;
@ -55,7 +55,7 @@ int32_t MultiUserManager::UnInit()
void MultiUserManager::SetCurrentForegroundUserID(int32_t userId)
{
std::lock_guard<std::mutex> lock(foregroundUserIdLock_);
DeviceProfileManager::GetInstance().OnUserChange(userId, foregroundUserId_);
DeviceProfileManager::GetInstance().OnUserChange(foregroundUserId_, userId);
foregroundUserId_ = userId;
}
@ -73,7 +73,7 @@ int32_t MultiUserManager::GetForegroundUserIDFromOs()
HILOGD("GetForegroundId failed, res:%{public}d", res);
return DP_GET_FOREGROUND_ID_FAIL;
}
HILOGI("GetForegroundUserIDFromOs foregroundId = %{public}s", ProfileUtils::GetAnonyInt32(foregroundId).c_str());
HILOGI("GetForegroundUserIDFromOs foregroundId = %{public}d", foregroundId);
return foregroundId;
}

View File

@ -71,7 +71,7 @@ int32_t SwitchAdapter::GetSwitch(const std::string& appId, const std::string& ne
auto res = kvDataMgr_.GetSwitch(appID, networkId);
if (res.first != DistributedKv::Status::SUCCESS) {
HILOGE("switch from db failed, ret: %{public}d", res.first);
return DP_PUT_KV_DB_FAIL;
return DP_GET_KV_DB_FAIL;
}
HILOGI("switch value %{public}u, length : %{public}u", res.second.value, res.second.length);
value = res.second.value;

View File

@ -134,7 +134,7 @@ HWTEST_F(SwitchAdapterTest, GetSwitch_003, TestSize.Level1)
uint32_t switchLength = 0;
int32_t ret = OHOS::DistributedDeviceProfile::
SwitchAdapter::GetInstance().GetSwitch(appId, networkId, value, switchLength);
EXPECT_EQ(ret, DP_PUT_KV_DB_FAIL);
EXPECT_EQ(ret, DP_GET_KV_DB_FAIL);
}
/*
@ -189,4 +189,4 @@ HWTEST_F(SwitchAdapterTest, UnsubscribeSwitchData_002, TestSize.Level1)
EXPECT_EQ(ret, DP_SUBSCRIBE_FAILED);
}
} // namespace DistributedDeviceProfile
} // namespace OHOS
} // namespace OHOS