From 9f40a030cfac11b3d262180ca8d9e1e30aa90853 Mon Sep 17 00:00:00 2001 From: guoyi Date: Sat, 13 Sep 2025 20:49:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8C=89GetServiceInfoProfil?= =?UTF-8?q?eByRegServiceId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/include/interfaces/business_event.h | 7 +- .../interfaces/dp_ipc_interface_code.h | 3 +- .../interfaces/i_distributed_device_profile.h | 7 +- .../interfaces/service_info_profile_new.h | 29 ++- common/include/utils/ipc_utils.h | 3 + common/src/interfaces/business_event.cpp | 3 +- .../interfaces/service_info_profile_new.cpp | 54 ++--- common/src/utils/ipc_utils.cpp | 32 +++ common/src/utils/profile_utils.cpp | 2 +- .../distributed_device_profile_client.h | 3 +- .../distributed_device_profile_proxy.h | 5 +- .../src/distributed_device_profile_client.cpp | 17 +- .../src/distributed_device_profile_proxy.cpp | 29 ++- .../data_share/settings_data_manager.h | 10 +- .../distributed_device_profile_service_new.h | 5 +- .../distributed_device_profile_stub_new.h | 1 + .../kvadapter/service_info_kv_adapter.h | 14 +- .../service_info_manager.h | 30 ++- .../src/datashare/settings_data_manager.cpp | 32 +-- ...distributed_device_profile_service_new.cpp | 31 ++- .../distributed_device_profile_stub_new.cpp | 34 ++- .../kvadapter/service_info_kv_adapter.cpp | 48 ++-- .../service_info_manager.cpp | 212 +++++++++++------- .../core/src/utils/profile_control_utils.cpp | 2 +- .../businesseventmanager_fuzzer.h | 4 +- ...stributed_device_profile_stub_new_test.cpp | 21 +- 26 files changed, 399 insertions(+), 239 deletions(-) diff --git a/common/include/interfaces/business_event.h b/common/include/interfaces/business_event.h index 9c430b42..5f6debfc 100644 --- a/common/include/interfaces/business_event.h +++ b/common/include/interfaces/business_event.h @@ -15,14 +15,14 @@ #ifndef OHOS_DP_BUSINESS_EVENT_H #define OHOS_DP_BUSINESS_EVENT_H - + #include #include #include "distributed_device_profile_constants.h" #include "dp_parcel.h" #include "parcel.h" - + namespace OHOS { namespace DistributedDeviceProfile { class BusinessEvent : public DpParcel { @@ -32,7 +32,7 @@ public: BusinessEvent(); ~BusinessEvent(); - + std::string GetBusinessKey() const; void SetBusinessKey(const std::string& businessKey); std::string GetBusinessValue() const; @@ -68,4 +68,3 @@ private: } // namespace DistributedDeviceProfile } // namespace OHOS #endif //OHOS_DP_BUSINESS_EVENT_H - diff --git a/common/include/interfaces/dp_ipc_interface_code.h b/common/include/interfaces/dp_ipc_interface_code.h index 4b2f775c..2454af39 100644 --- a/common/include/interfaces/dp_ipc_interface_code.h +++ b/common/include/interfaces/dp_ipc_interface_code.h @@ -95,7 +95,8 @@ enum class DpIpcInterfaceCode : uint32_t { SYNC_STATIC_PROFILE = 76, GET_SERVICE_INFO_PROFILE_BY_SERVICE_ID = 77, GET_SERVICE_INFO_PROFILE_BY_TOKEN_ID = 78, - MAX = 79 + GET_SERVICE_INFO_PROFILE_BY_REG_SER_ID = 79, + MAX = 80 }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/i_distributed_device_profile.h b/common/include/interfaces/i_distributed_device_profile.h index ba7e7b7c..15e5c03c 100644 --- a/common/include/interfaces/i_distributed_device_profile.h +++ b/common/include/interfaces/i_distributed_device_profile.h @@ -113,8 +113,11 @@ public: virtual int32_t GetBusinessEvent(BusinessEvent& event) = 0; virtual int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfo) = 0; virtual int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) = 0; - virtual int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile)= 0; - virtual int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile)= 0; + virtual int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) = 0; + virtual int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfiles) = 0; + virtual int32_t GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) = 0; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/service_info_profile_new.h b/common/include/interfaces/service_info_profile_new.h index 590c6744..e760423a 100644 --- a/common/include/interfaces/service_info_profile_new.h +++ b/common/include/interfaces/service_info_profile_new.h @@ -12,16 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef OHOS_DP_SERVICE_INFO_H #define OHOS_DP_SERVICE_INFO_H - + #include #include - + #include "distributed_device_profile_constants.h" #include "dp_parcel.h" - + namespace OHOS { namespace DistributedDeviceProfile { class ServiceInfoProfileNew : public DpParcel { @@ -37,40 +37,39 @@ public: serviceName_(""), serviceDisplayName_("") {}; - ~ServiceInfoProfileNew(); - + int32_t GetRegServiceId() const; void SetRegServiceId(const int32_t ®ServiceIdValue); - + std::string GetDeviceId() const; void SetDeviceId(const std::string &deviceIdValue); - + int32_t GetUserId() const; void SetUserId(const int32_t &userIdValue); int64_t GetTokenId() const; void SetTokenId(const int64_t &tokenIdValue); - + int8_t GetSerPubState() const; void SetSerPubState(const int8_t &SerPubStateValue); - + int64_t GetServiceId() const; void SetServiceId(const int64_t serviceIdValue); - + std::string GetServiceType() const; void SetServiceType(const std::string &serviceTypeValue); - + std::string GetServiceName() const; void SetServiceName(const std::string &serviceNameValue); - + std::string GetServiceDisplayName() const; void SetServiceDisplayName(const std::string &serviceDisplayNameValue); - + bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; std::string dump() const override; - + private: int32_t regServiceId_; std::string deviceId_; diff --git a/common/include/utils/ipc_utils.h b/common/include/utils/ipc_utils.h index 4d3ba092..e4fcda01 100644 --- a/common/include/utils/ipc_utils.h +++ b/common/include/utils/ipc_utils.h @@ -35,6 +35,7 @@ #include "dp_subscribe_info.h" #include "product_info.h" #include "service_info_profile.h" +#include "service_info_profile_new.h" #include "service_profile.h" #include "trust_device_profile.h" #include "trusted_device_info.h" @@ -75,6 +76,8 @@ public: OHOS::DistributedDeviceProfile::SubscribeInfo>& listenerMap); static bool UnMarshalling(MessageParcel& parcel, std::unordered_set& changeTypes); static bool UnMarshalling(MessageParcel& parcel, std::vector& deviceInfos); + static bool Marshalling(MessageParcel& parcel, const std::vector& serviceInfoProfiles); + static bool UnMarshalling(MessageParcel& parcel, std::vector& serviceInfoProfiles); }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/src/interfaces/business_event.cpp b/common/src/interfaces/business_event.cpp index 910ae694..b1690231 100644 --- a/common/src/interfaces/business_event.cpp +++ b/common/src/interfaces/business_event.cpp @@ -18,7 +18,7 @@ #include "distributed_device_profile_constants.h" #include "macro_utils.h" #include "profile_utils.h" - + namespace OHOS { namespace DistributedDeviceProfile { namespace { @@ -135,4 +135,3 @@ BusinessEventExt *BusinessEventExt::Unmarshalling(Parcel& parcel) } } // namespace DistributedDeviceProfile } // namespace OHOS - diff --git a/common/src/interfaces/service_info_profile_new.cpp b/common/src/interfaces/service_info_profile_new.cpp index 9b9378bb..d3bdfa0b 100644 --- a/common/src/interfaces/service_info_profile_new.cpp +++ b/common/src/interfaces/service_info_profile_new.cpp @@ -12,38 +12,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #include "service_info_profile_new.h" #include "cJSON.h" #include "distributed_device_profile_constants.h" #include "macro_utils.h" #include "profile_utils.h" - + namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "ServiceInfoProfileNew"; } - + ServiceInfoProfileNew::~ServiceInfoProfileNew() { } - + int64_t ServiceInfoProfileNew::GetServiceId() const { return serviceId_; } - + void ServiceInfoProfileNew::SetServiceId(const int64_t serviceId) { serviceId_ = serviceId; } - + std::string ServiceInfoProfileNew::GetServiceType() const { return serviceType_; } - + void ServiceInfoProfileNew::SetServiceType(const std::string& serviceType) { serviceType_ = serviceType; @@ -53,42 +53,42 @@ std::string ServiceInfoProfileNew::GetServiceName() const { return serviceName_; } - + void ServiceInfoProfileNew::SetServiceName(const std::string &serviceName) { serviceName_ = serviceName; } - + std::string ServiceInfoProfileNew::GetServiceDisplayName() const { return serviceDisplayName_; } - + void ServiceInfoProfileNew::SetServiceDisplayName(const std::string &serviceDisplayName) { serviceDisplayName_ = serviceDisplayName; } - + int32_t ServiceInfoProfileNew::GetRegServiceId() const { return regServiceId_; } - + void ServiceInfoProfileNew::SetRegServiceId(const int32_t ®ServiceId) { regServiceId_ = regServiceId; } - + int8_t ServiceInfoProfileNew::GetSerPubState() const { return servicePubState_; } - + void ServiceInfoProfileNew::SetSerPubState(const int8_t &serPubState) { servicePubState_ = serPubState; } - + std::string ServiceInfoProfileNew::GetDeviceId() const { return deviceId_; @@ -107,7 +107,7 @@ void ServiceInfoProfileNew::SetUserId(const int32_t &userId) { userId_ = userId; } - + int64_t ServiceInfoProfileNew::GetTokenId() const { return tokenId_; @@ -116,7 +116,7 @@ void ServiceInfoProfileNew::SetTokenId(const int64_t &tokenId) { tokenId_ = tokenId; } - + bool ServiceInfoProfileNew::Marshalling(MessageParcel& parcel) const { WRITE_HELPER(parcel, Int32, regServiceId_); @@ -128,10 +128,10 @@ bool ServiceInfoProfileNew::Marshalling(MessageParcel& parcel) const WRITE_HELPER(parcel, String, serviceType_); WRITE_HELPER(parcel, String, serviceName_); WRITE_HELPER(parcel, String, serviceDisplayName_); - + return true; } - + bool ServiceInfoProfileNew::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, Int32, regServiceId_, false); @@ -143,10 +143,10 @@ bool ServiceInfoProfileNew::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, serviceType_, false); READ_HELPER_RET(parcel, String, serviceName_, false); READ_HELPER_RET(parcel, String, serviceDisplayName_, false); - + return true; } - + std::string ServiceInfoProfileNew::dump() const { cJSON* json = cJSON_CreateObject(); @@ -155,17 +155,17 @@ std::string ServiceInfoProfileNew::dump() const return EMPTY_STRING; } cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str()); - + cJSON_AddStringToObject(json, USERID.c_str(), std::to_string(userId_).c_str()); - + cJSON_AddStringToObject(json, TOKENID.c_str(), std::to_string(tokenId_).c_str()); - + cJSON_AddStringToObject(json, PUBLISH_STATE.c_str(), std::to_string(servicePubState_).c_str()); - + cJSON_AddStringToObject(json, SISERVICE_ID.c_str(), std::to_string(serviceId_).c_str()); - + cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str()); - + cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str()); cJSON_AddStringToObject(json, SERVICE_DISPLAY_NAME.c_str(), diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index 7c29e1a2..1edd2b2c 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -541,5 +541,37 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& serviceInfoProfiles) +{ + uint32_t size = serviceInfoProfiles.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + if (serviceInfoProfiles.size() > MAX_PROFILE_SIZE) { + HILOGE("deviceInfos size is invalid!size : %{public}zu", serviceInfoProfiles.size()); + return false; + } + for (const auto& item : serviceInfoProfiles) { + item.Marshalling(parcel); + } + return true; +} + +bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& serviceInfoProfiles) +{ + uint32_t size = parcel.ReadUint32(); + if (size > MAX_PROFILE_SIZE) { + HILOGE("Profile size is invalid!size : %{public}u", size); + return false; + } + for (uint32_t i = 0; i < size; i++) { + ServiceInfoProfileNew serviceInfoProfile; + if (!serviceInfoProfile.UnMarshalling(parcel)) { + HILOGE("Profile UnMarshalling fail!"); + return false; + } + serviceInfoProfiles.emplace_back(serviceInfoProfile); + } + return true; +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/src/utils/profile_utils.cpp b/common/src/utils/profile_utils.cpp index 3443e82e..5089ab46 100644 --- a/common/src/utils/profile_utils.cpp +++ b/common/src/utils/profile_utils.cpp @@ -479,7 +479,7 @@ int32_t ProfileUtils::ServiceInfoProfileToEntries(const ServiceInfoProfileNew& p values[GenerateServiceDBKey(regServiceId, SERVICE_NAME, userIdValue)] = profile.GetServiceName(); values[GenerateServiceDBKey(regServiceId, SERVICE_DISPLAY_NAME, userIdValue)] = profile.GetServiceDisplayName(); - + return DP_SUCCESS; } diff --git a/interfaces/innerkits/core/include/distributed_device_profile_client.h b/interfaces/innerkits/core/include/distributed_device_profile_client.h index 77280102..2d079be3 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_client.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_client.h @@ -110,7 +110,8 @@ public: int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfoProfile); int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId); int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile); - int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile); + int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, std::vector& serviceInfoProfiles); + int32_t GetServiceInfoProfileByRegServiceId(int32_t regServiceId, ServiceInfoProfileNew& serviceInfoProfile); void LoadSystemAbilitySuccess(const sptr &remoteObject); void LoadSystemAbilityFail(); diff --git a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h index b99f307f..6ccab16a 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h @@ -105,7 +105,10 @@ public: int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfoProfile) override; int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) override; int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) override; - int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile) override; + int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfiles) override; + int32_t GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp index 50892b27..ed56cc44 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp @@ -1227,7 +1227,7 @@ int32_t DistributedDeviceProfileClient::PutServiceInfoProfile(const ServiceInfoP } return dpService->PutServiceInfoProfile(serviceInfoProfile); } - + int32_t DistributedDeviceProfileClient::DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) { auto dpService = GetDeviceProfileService(); @@ -1248,8 +1248,19 @@ int32_t DistributedDeviceProfileClient::GetServiceInfoProfileByServiceId(int64_t } return dpService->GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); } - + int32_t DistributedDeviceProfileClient::GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfiles) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetServiceInfoProfileByTokenId(tokenId, serviceInfoProfiles); +} + +int32_t DistributedDeviceProfileClient::GetServiceInfoProfileByRegServiceId(int32_t regServiceId, ServiceInfoProfileNew& serviceInfoProfile) { auto dpService = GetDeviceProfileService(); @@ -1257,7 +1268,7 @@ int32_t DistributedDeviceProfileClient::GetServiceInfoProfileByTokenId(int64_t t HILOGE("Get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->GetServiceInfoProfileByTokenId(tokenId, serviceInfoProfile); + return dpService->GetServiceInfoProfileByRegServiceId(regServiceId, serviceInfoProfile); } } // namespace DeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp index eb4fbf83..250bb3e1 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp @@ -852,7 +852,7 @@ int32_t DistributedDeviceProfileProxy::PutServiceInfoProfile(const ServiceInfoPr SEND_REQUEST(remote, static_cast(DpIpcInterfaceCode::PUT_SERVICE_INFO_PROFILE), data, reply); return DP_SUCCESS; } - + int32_t DistributedDeviceProfileProxy::DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) { sptr remote = nullptr; @@ -861,7 +861,7 @@ int32_t DistributedDeviceProfileProxy::DeleteServiceInfoProfile(int32_t regServi WRITE_INTERFACE_TOKEN(data); WRITE_HELPER(data, Int32, regServiceId); WRITE_HELPER(data, Int32, userId); - + MessageParcel reply; SEND_REQUEST(remote, static_cast(DpIpcInterfaceCode::DELETE_SERVICE_INFO_PROFILE), data, reply); return DP_SUCCESS; @@ -884,18 +884,37 @@ int32_t DistributedDeviceProfileProxy::GetServiceInfoProfileByServiceId(int64_t } return DP_SUCCESS; } - + int32_t DistributedDeviceProfileProxy::GetServiceInfoProfileByTokenId(int64_t tokenId, - ServiceInfoProfileNew& serviceInfoProfile) + std::vector& serviceInfoProfiles) { sptr remote = nullptr; GET_REMOTE_OBJECT(remote); MessageParcel data; WRITE_INTERFACE_TOKEN(data); - + WRITE_HELPER(data, Int64, tokenId); MessageParcel reply; SEND_REQUEST(remote, static_cast(DpIpcInterfaceCode::GET_SERVICE_INFO_PROFILE_BY_TOKEN_ID), data, reply); + if (IpcUtils::UnMarshalling(reply, serviceInfoProfiles)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + + WRITE_HELPER(data, Int32, regServiceId); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DpIpcInterfaceCode::GET_SERVICE_INFO_PROFILE_BY_REG_SER_ID), + data, reply); if (!serviceInfoProfile.UnMarshalling(reply)) { HILOGE("dp ipc read parcel fail"); return DP_READ_PARCEL_FAIL; diff --git a/services/core/include/data_share/settings_data_manager.h b/services/core/include/data_share/settings_data_manager.h index 73953d1b..71a865ff 100644 --- a/services/core/include/data_share/settings_data_manager.h +++ b/services/core/include/data_share/settings_data_manager.h @@ -14,7 +14,7 @@ */ #ifndef OHOS_DP_SETTINGS_DATA_CONNECTOR_H #define OHOS_DP_SETTINGS_DATA_CONNECTOR_H - + #include #include #include @@ -31,7 +31,7 @@ class SettingsDataManager { public: int32_t Init(); int32_t UnInit(); - + int32_t GetUserDefinedDeviceName(int32_t userId, std::string &deviceName); int32_t SetUserDefinedDeviceName(const std::string &deviceName, int32_t userId); int32_t GetDisplayDeviceName(int32_t userId, std::string &deviceName); @@ -40,14 +40,14 @@ public: private: int32_t GetValue(const std::string &tableName, int32_t userId, const std::string &key, std::string &value); int32_t SetValue(const std::string &tableName, int32_t userId, const std::string &key, const std::string &value); - + std::string GetProxyUriStr(const std::string &tableName, int32_t userId); std::shared_ptr CreateDataShareHelper(const std::string &proxyUri); Uri MakeUri(const std::string &proxyUri, const std::string &key); bool ReleaseDataShareHelper(std::shared_ptr helper); - + sptr GetRemoteObj(); - + private: std::mutex remoteObjMtx_; sptr remoteObj_; diff --git a/services/core/include/distributed_device_profile_service_new.h b/services/core/include/distributed_device_profile_service_new.h index db851822..93538bb3 100644 --- a/services/core/include/distributed_device_profile_service_new.h +++ b/services/core/include/distributed_device_profile_service_new.h @@ -127,7 +127,10 @@ public: int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfoProfile) override; int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) override; int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) override; - int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile) override; + int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfile) override; + int32_t GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) override; protected: void OnStart(const SystemAbilityOnDemandReason& startReason) override; diff --git a/services/core/include/distributed_device_profile_stub_new.h b/services/core/include/distributed_device_profile_stub_new.h index c19fb4fc..59100657 100644 --- a/services/core/include/distributed_device_profile_stub_new.h +++ b/services/core/include/distributed_device_profile_stub_new.h @@ -90,6 +90,7 @@ public: int32_t DeleteServiceInfoProfileInner(MessageParcel& data, MessageParcel& reply); int32_t GetServiceInfoProfileByServiceIdInner(MessageParcel& data, MessageParcel& reply); int32_t GetServiceInfoProfileByTokenIdInner(MessageParcel& data, MessageParcel& reply); + int32_t GetServiceInfoProfileByRegServiceIdInner(MessageParcel& data, MessageParcel& reply); private: using Func = int32_t(DistributedDeviceProfileStubNew::*)(MessageParcel& data, MessageParcel& reply); diff --git a/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h b/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h index ed43c603..207b5e3e 100644 --- a/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h +++ b/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef OHOS_DM_SERVICE_KV_ADAPTER_H #define OHOS_DM_SERVICE_KV_ADAPTER_H - + #include #include #include @@ -23,11 +23,11 @@ #include #include #include - + #include "distributed_kv_data_manager.h" #include "ikv_adapter.h" #include "single_instance.h" - + namespace OHOS { namespace DistributedDeviceProfile { class ServiceInfoKvAdapter : public DistributedDeviceProfile::IKVAdapter { @@ -49,12 +49,12 @@ public: int32_t GetDeviceEntries(const std::string& udid, std::map& values) override; int32_t RemoveDeviceData(const std::string& uuid) override; void OnRemoteDied(); - + private: DistributedKv::Status GetKvStorePtr(); int32_t RegisterKvStoreDeathListener(); int32_t UnregisterKvStoreDeathListener(); - + private: DistributedKv::AppId appId_; DistributedKv::StoreId storeId_; @@ -62,7 +62,7 @@ private: DistributedKv::DataType dataType_ = DistributedKv::DataType::TYPE_STATICS; std::shared_ptr kvStorePtr_ = nullptr; std::shared_ptr deathRecipient_ = nullptr; - + std::mutex serviceInfoAdapterMutex_; std::atomic isInited_ = false; std::mutex kvAdapterMutex_; diff --git a/services/core/include/serviceinfo_manager/service_info_manager.h b/services/core/include/serviceinfo_manager/service_info_manager.h index dcf7bd80..09d4836f 100644 --- a/services/core/include/serviceinfo_manager/service_info_manager.h +++ b/services/core/include/serviceinfo_manager/service_info_manager.h @@ -12,19 +12,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef OHOS_DP_SERVICE_INFO_MANAGER_H #define OHOS_DP_SERVICE_INFO_MANAGER_H - + #include #include #include - + #include "service_info_profile_new.h" #include "service_info_kv_adapter.h" #include "distributed_device_profile_constants.h" #include "single_instance.h" - + namespace OHOS { namespace DistributedDeviceProfile { class ServiceInfoProfileManage { @@ -35,24 +35,20 @@ public: int32_t ReInit(); int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfoProfile); int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId = DEFAULT_USER_ID); - int32_t UpdateServiceInfoProfile(const int32_t regServiceId, const ServiceInfoProfileNew &serviceInfoProfile); + int32_t UpdateServiceInfoProfile(int32_t regServiceId, const ServiceInfoProfileNew& serviceInfoProfile); int32_t GetAllServiceInfoProfileList(std::vector& serviceInfoProfileList); int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile); - int32_t GetServiceInfoProfileByRegServiceId(const int32_t ®ServiceId, ServiceInfoProfileNew& serviceInfoProfile); - int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile); - - void RemoveSubstrings(std::string& str, const std::string& substr); - void RemoveAfterSecondHash(std::string& str); - int32_t SetServiceInfoProfile(const std::string& regServiceId, - const std::map& finalSerProfile, ServiceInfoProfileNew& serviceInfoProfile); - + int32_t GetServiceInfoProfileByRegServiceId(int32_t regServiceId, ServiceInfoProfileNew& serviceInfoProfile); + int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, std::vector& serviceInfoProfiles); + private: std::string FindRegServiceId(const std::string& str); + int32_t SetServiceInfoProfile(const std::string& regServiceId, + const std::map& finalSerProfile, ServiceInfoProfileNew& serviceInfoProfile); + int32_t GetServiceInfoProfileByRegServiceId(const std::string& regServiceIdStr, + ServiceInfoProfileNew& serviceInfoProfile); + std::mutex storeMutex_; std::shared_ptr serviceInfoKvAdapter_ = nullptr; - std::mutex dynamicStoreMutex_; - std::atomic isFirst_{true}; - std::map putTempCache_; - std::mutex putTempCacheMutex_; }; } // DistributedDeviceProfile } // OHOS diff --git a/services/core/src/datashare/settings_data_manager.cpp b/services/core/src/datashare/settings_data_manager.cpp index bfbc8b7f..b6b190e6 100644 --- a/services/core/src/datashare/settings_data_manager.cpp +++ b/services/core/src/datashare/settings_data_manager.cpp @@ -37,49 +37,49 @@ const std::string SETTINGSDATA_SYSTEM = "USER_SETTINGSDATA_"; const std::string SETTINGSDATA_SECURE = "USER_SETTINGSDATA_SECURE_"; const std::string SETTINGS_GENERAL_DISPLAY_DEVICE_NAME = "settings.general.display_device_name"; const std::string SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME = "settings.general.user_defined_device_name"; - + const std::string SETTING_COLUMN_VALUE = "VALUE"; const std::string SETTING_COLUMN_KEYWORD = "KEYWORD"; } - + int32_t SettingsDataManager::Init() { GetRemoteObj(); return DP_SUCCESS; } - + int32_t SettingsDataManager::UnInit() { std::lock_guard lock(remoteObjMtx_); remoteObj_ = nullptr; return DP_SUCCESS; } - + int32_t SettingsDataManager::GetUserDefinedDeviceName(int32_t userId, std::string &deviceName) { return GetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME, deviceName); } - + int32_t SettingsDataManager::SetUserDefinedDeviceName(const std::string &deviceName, int32_t userId) { return SetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME, deviceName); } - + int32_t SettingsDataManager::GetDisplayDeviceName(int32_t userId, std::string &deviceName) { return GetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_DISPLAY_DEVICE_NAME, deviceName); } - + int32_t SettingsDataManager::SetDisplayDeviceName(const std::string &deviceName, int32_t userId) { return SetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_DISPLAY_DEVICE_NAME, deviceName); } - + int32_t SettingsDataManager::GetDeviceName(std::string &deviceName) { return GetValue(SETTINGSDATA_GLOBAL, 0, SETTINGS_GENERAL_DEVICE_NAME, deviceName); } - + sptr SettingsDataManager::GetRemoteObj() { std::lock_guard lock(remoteObjMtx_); @@ -99,7 +99,7 @@ sptr SettingsDataManager::GetRemoteObj() remoteObj_ = remoteObj; return remoteObj_; } - + int32_t SettingsDataManager::GetValue(const std::string &tableName, int32_t userId, const std::string &key, std::string &value) { @@ -137,7 +137,7 @@ int32_t SettingsDataManager::GetValue(const std::string &tableName, int32_t user resultSet->Close(); return DP_SUCCESS; } - + int32_t SettingsDataManager::SetValue(const std::string &tableName, int32_t userId, const std::string &key, const std::string &value) { @@ -147,7 +147,7 @@ int32_t SettingsDataManager::SetValue(const std::string &tableName, int32_t user HILOGE("helper is nullptr"); return DP_NULLPTR; } - + DataShare::DataShareValuesBucket val; val.Put(SETTING_COLUMN_KEYWORD, key); val.Put(SETTING_COLUMN_VALUE, value); @@ -166,7 +166,7 @@ int32_t SettingsDataManager::SetValue(const std::string &tableName, int32_t user } return ret; } - + std::shared_ptr SettingsDataManager::CreateDataShareHelper(const std::string &proxyUri) { if (proxyUri.empty()) { @@ -180,7 +180,7 @@ std::shared_ptr SettingsDataManager::CreateDataShare } return helper; } - + std::string SettingsDataManager::GetProxyUriStr(const std::string &tableName, int32_t userId) { if (userId < USERID_HELPER_NUMBER) { @@ -192,7 +192,7 @@ std::string SettingsDataManager::GetProxyUriStr(const std::string &tableName, in return SETTING_URI_PROXY + tableName + std::to_string(userId) + URI_PROXY_SUFFIX; } } - + Uri SettingsDataManager::MakeUri(const std::string &proxyUri, const std::string &key) { if (proxyUri.empty() || key.empty()) { @@ -201,7 +201,7 @@ Uri SettingsDataManager::MakeUri(const std::string &proxyUri, const std::string Uri uri(proxyUri + "&key=" + key); return uri; } - + bool SettingsDataManager::ReleaseDataShareHelper(std::shared_ptr helper) { if (helper == nullptr) { diff --git a/services/core/src/distributed_device_profile_service_new.cpp b/services/core/src/distributed_device_profile_service_new.cpp index 0c75a5cc..9b2da8e2 100644 --- a/services/core/src/distributed_device_profile_service_new.cpp +++ b/services/core/src/distributed_device_profile_service_new.cpp @@ -1513,7 +1513,7 @@ int32_t DistributedDeviceProfileServiceNew::PutServiceInfoProfile(const ServiceI } return DP_SUCCESS; } - + int32_t DistributedDeviceProfileServiceNew::DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) { if (!PermissionManager::GetInstance().CheckCallerPermission()) { @@ -1536,27 +1536,44 @@ int32_t DistributedDeviceProfileServiceNew::GetServiceInfoProfileByServiceId(int HILOGE("the caller is permission denied!"); return DP_PERMISSION_DENIED; } - HILOGD("CheckCallerPermission success interface GetServiceInfoProfileByServiceId"); + HILOGD("CheckCallerPermission success interface"); int32_t ret = ServiceInfoProfileManage::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); if (ret != DP_SUCCESS) { - HILOGE("GetServiceInfoProfileByServiceId failed, ret: %{public}d", ret); + HILOGE("failed, ret: %{public}d", ret); return ret; } return ret; } - + int32_t DistributedDeviceProfileServiceNew::GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfiles) +{ + if (!PermissionManager::GetInstance().CheckCallerPermission()) { + HILOGE("the caller is permission denied!"); + return DP_PERMISSION_DENIED; + } + HILOGD("CheckCallerPermission success interface"); + int32_t ret = ServiceInfoProfileManage::GetInstance().GetServiceInfoProfileByTokenId(tokenId, serviceInfoProfiles); + if (ret != DP_SUCCESS) { + HILOGE("failed, ret: %{public}d", ret); + return ret; + } + return ret; +} + +int32_t DistributedDeviceProfileServiceNew::GetServiceInfoProfileByRegServiceId(int32_t regServiceId, ServiceInfoProfileNew& serviceInfoProfile) { if (!PermissionManager::GetInstance().CheckCallerPermission()) { HILOGE("the caller is permission denied!"); return DP_PERMISSION_DENIED; } - HILOGD("CheckCallerPermission success interface GetServiceInfoProfileByTokenId"); - int32_t ret = ServiceInfoProfileManage::GetInstance().GetServiceInfoProfileByTokenId(tokenId, serviceInfoProfile); + HILOGD("CheckCallerPermission success interface"); + int32_t ret = ServiceInfoProfileManage::GetInstance().GetServiceInfoProfileByRegServiceId(regServiceId, + serviceInfoProfile); if (ret != DP_SUCCESS) { - HILOGE("GetServiceInfoProfileByTokenId failed, ret: %{public}d", ret); + HILOGE("failed, ret: %{public}d", ret); return ret; } return ret; diff --git a/services/core/src/distributed_device_profile_stub_new.cpp b/services/core/src/distributed_device_profile_stub_new.cpp index d9e42f31..c9aece75 100644 --- a/services/core/src/distributed_device_profile_stub_new.cpp +++ b/services/core/src/distributed_device_profile_stub_new.cpp @@ -198,6 +198,8 @@ int32_t DistributedDeviceProfileStubNew::NotifyEventInner(uint32_t code, Message return GetServiceInfoProfileByServiceIdInner(data, reply); case static_cast(DpIpcInterfaceCode::GET_SERVICE_INFO_PROFILE_BY_TOKEN_ID): return GetServiceInfoProfileByTokenIdInner(data, reply); + case static_cast(DpIpcInterfaceCode::GET_SERVICE_INFO_PROFILE_BY_REG_SER_ID): + return GetServiceInfoProfileByRegServiceIdInner(data, reply); default: return NotifyLocalServiceEventInner(code, data, reply, option); } @@ -998,7 +1000,7 @@ int32_t DistributedDeviceProfileStubNew::GetServiceInfoProfileListByTokenIdInner HILOGE("Write reply failed"); return ERR_FLATTEN_OBJECT; } - + if (!IpcUtils::Marshalling(reply, serviceInfoProfiles)) { return DP_READ_PARCEL_FAIL; } @@ -1014,7 +1016,7 @@ int32_t DistributedDeviceProfileStubNew::GetAllServiceInfoProfileListInner(Messa HILOGE("Write reply failed"); return ERR_FLATTEN_OBJECT; } - + if (!IpcUtils::Marshalling(reply, serviceInfoProfiles)) { return DP_READ_PARCEL_FAIL; } @@ -1036,7 +1038,7 @@ int32_t DistributedDeviceProfileStubNew::GetServiceInfoProfileListByBundleNameIn HILOGE("Write reply failed"); return ERR_FLATTEN_OBJECT; } - + if (!IpcUtils::Marshalling(reply, serviceInfoProfiles)) { return DP_READ_PARCEL_FAIL; } @@ -1140,7 +1142,7 @@ int32_t DistributedDeviceProfileStubNew::PutServiceInfoProfileInner(MessageParce } return DP_SUCCESS; } - + int32_t DistributedDeviceProfileStubNew::DeleteServiceInfoProfileInner(MessageParcel& data, MessageParcel& reply) { int32_t regServiceId = 0; @@ -1148,7 +1150,7 @@ int32_t DistributedDeviceProfileStubNew::DeleteServiceInfoProfileInner(MessagePa int32_t userId = 0; READ_HELPER(data, Int32, regServiceId); READ_HELPER(data, Int32, userId); - + int32_t ret = DeleteServiceInfoProfile(regServiceId, userId); if (!reply.WriteInt32(ret)) { HILOGE("Write reply failed"); @@ -1174,13 +1176,31 @@ int32_t DistributedDeviceProfileStubNew::GetServiceInfoProfileByServiceIdInner(M } return DP_SUCCESS; } - + int32_t DistributedDeviceProfileStubNew::GetServiceInfoProfileByTokenIdInner(MessageParcel& data, MessageParcel& reply) { int64_t tokenId = 0; READ_HELPER(data, Int64, tokenId); + std::vector serviceInfoProfiles; + int32_t ret = GetServiceInfoProfileByTokenId(tokenId, serviceInfoProfiles); + if (!reply.WriteInt32(ret)) { + HILOGE("Write reply failed"); + return ERR_FLATTEN_OBJECT; + } + if (!IpcUtils::Marshalling(reply, serviceInfoProfiles)) { + HILOGE("write parcel fail!"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileStubNew::GetServiceInfoProfileByRegServiceIdInner(MessageParcel& data, + MessageParcel& reply) +{ + int32_t regServiceId = 0; + READ_HELPER(data, Int32, regServiceId); ServiceInfoProfileNew serviceInfoProfile; - int32_t ret = GetServiceInfoProfileByTokenId(tokenId, serviceInfoProfile); + int32_t ret = GetServiceInfoProfileByRegServiceId(regServiceId, serviceInfoProfile); if (!reply.WriteInt32(ret)) { HILOGE("Write reply failed"); return ERR_FLATTEN_OBJECT; diff --git a/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp b/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp index e1ee5511..ccc09b8d 100644 --- a/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp +++ b/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp @@ -13,19 +13,19 @@ * limitations under the License. */ #include "service_info_kv_adapter.h" - + #include #include - + #include "datetime_ex.h" #include "string_ex.h" #include "ffrt.h" - + #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" #include "distributed_device_profile_constants.h" #include "profile_cache.h" - + namespace OHOS { namespace DistributedDeviceProfile { using namespace OHOS::DistributedKv; @@ -38,7 +38,7 @@ namespace { const std::string APP_ID = "distributed_device_profile_service"; const std::string STORE_ID = "dp_kv_store_service_info_profile"; } - + ServiceInfoKvAdapter::ServiceInfoKvAdapter( const std::shared_ptr &deathListener, DistributedKv::DataType dataType) { @@ -47,7 +47,7 @@ ServiceInfoKvAdapter::ServiceInfoKvAdapter( this->appId_.appId = APP_ID; this->storeId_.storeId = STORE_ID; } - + int32_t ServiceInfoKvAdapter::Init() { HILOGI("Init local DB, dataType: %{public}d", static_cast(dataType_)); @@ -91,14 +91,14 @@ int32_t ServiceInfoKvAdapter::UnInit() } return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::ReInit() { HILOGI("ServiceInfoKvAdapter ReInit"); UnInit(); return Init(); } - + int32_t ServiceInfoKvAdapter::Put(const std::string& key, const std::string& value) { HILOGI("ServiceInfoKvAdapter::Put"); @@ -113,7 +113,7 @@ int32_t ServiceInfoKvAdapter::Put(const std::string& key, const std::string& val HILOGE("kvDBPtr is null!"); return DP_KV_DB_PTR_NULL; } - + DistributedKv::Key kvKey(key); DistributedKv::Value kvValue(value); status = kvStorePtr_->Put(kvKey, kvValue); @@ -124,7 +124,7 @@ int32_t ServiceInfoKvAdapter::Put(const std::string& key, const std::string& val } return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::Get(const std::string& key, std::string& value) { DistributedKv::Key kvKey(key); @@ -145,7 +145,7 @@ int32_t ServiceInfoKvAdapter::Get(const std::string& key, std::string& value) value = kvValue.ToString(); return DP_SUCCESS; } - + void ServiceInfoKvAdapter::OnRemoteDied() { HILOGI("OnRemoteDied, recover db begin"); @@ -155,7 +155,7 @@ void ServiceInfoKvAdapter::OnRemoteDied() }; ffrt::submit(reInitTask); } - + DistributedKv::Status ServiceInfoKvAdapter::GetKvStorePtr() { HILOGI("ServiceInfoKvAdapter::GetKvStorePtr"); @@ -178,7 +178,7 @@ DistributedKv::Status ServiceInfoKvAdapter::GetKvStorePtr() } return status; } - + int32_t ServiceInfoKvAdapter::RegisterKvStoreDeathListener() { HILOGI("Register death listener"); @@ -188,7 +188,7 @@ int32_t ServiceInfoKvAdapter::RegisterKvStoreDeathListener() } return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::UnregisterKvStoreDeathListener() { HILOGI("UnRegister death listener"); @@ -198,7 +198,7 @@ int32_t ServiceInfoKvAdapter::UnregisterKvStoreDeathListener() } return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::Delete(const std::string& key) { DistributedKv::Status status; @@ -218,14 +218,14 @@ int32_t ServiceInfoKvAdapter::Delete(const std::string& key) HILOGD("Delete kv by key success!"); return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::DeleteBatch(const std::vector& keys) { if (keys.empty() || keys.size() > MAX_PROFILE_SIZE) { HILOGE("keys size(%{public}zu) is invalid!", keys.size()); return DP_INVALID_PARAMS; } - + uint32_t keysSize = static_cast(keys.size()); std::vector> delKeyBatches; for (uint32_t i = 0; i < keysSize; i += MAX_BATCH_SIZE) { @@ -285,7 +285,7 @@ int32_t ServiceInfoKvAdapter::PutBatch(const std::map& } return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::GetByPrefix(const std::string& keyPrefix, std::map& values) { HILOGI("key prefix: %{public}s", ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); @@ -303,7 +303,11 @@ int32_t ServiceInfoKvAdapter::GetByPrefix(const std::string& keyPrefix, std::map ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); return DP_GET_KV_DB_FAIL; } - if (allEntries.size() == 0 || allEntries.size() > MAX_DB_SIZE) { + if (allEntries.empty()) { + HILOGE("Not find in kv_store prefix: %{public}s", ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); + return DP_NOT_FIND_DATA; + } + if (allEntries.size() > MAX_DB_SIZE) { HILOGE("AllEntries size is invalid!size: %{public}zu! prefix: %{public}s", allEntries.size(), ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); return DP_INVALID_PARAMS; @@ -319,21 +323,21 @@ int32_t ServiceInfoKvAdapter::DeleteByPrefix(const std::string& keyPrefix) (void)keyPrefix; return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::Sync(const std::vector& deviceList, SyncMode syncMode) { (void)deviceList; (void)syncMode; return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::GetDeviceEntries(const std::string& udid, std::map& values) { (void)udid; (void)values; return DP_SUCCESS; } - + int32_t ServiceInfoKvAdapter::RemoveDeviceData(const std::string& uuid) { (void)uuid; diff --git a/services/core/src/serviceinfo_manager/service_info_manager.cpp b/services/core/src/serviceinfo_manager/service_info_manager.cpp index 95d3ae7c..373be314 100644 --- a/services/core/src/serviceinfo_manager/service_info_manager.cpp +++ b/services/core/src/serviceinfo_manager/service_info_manager.cpp @@ -12,10 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #include "service_info_manager.h" #include "profile_utils.h" - + +#include + #include "service_info_profile_new.h" #include "distributed_device_profile_constants.h" #include "distributed_device_profile_errors.h" @@ -25,22 +27,22 @@ #include "kv_store_death_recipient.h" #include "kv_sync_completed_listener.h" #include "profile_control_utils.h" - + #include "multi_user_manager.h" - + namespace OHOS { namespace DistributedDeviceProfile { IMPLEMENT_SINGLE_INSTANCE(ServiceInfoProfileManage) - + namespace { const std::string STORE_ID = "dp_kv_store_service_info_profile"; const std::string TAG = "ServiceInfoProfileManage"; } - + int32_t ServiceInfoProfileManage::Init() { HILOGI("call!"); - std::lock_guard lock(dynamicStoreMutex_); + std::lock_guard lock(storeMutex_); serviceInfoKvAdapter_ = std::make_shared( std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); int32_t ret = serviceInfoKvAdapter_->Init(); @@ -51,12 +53,12 @@ int32_t ServiceInfoProfileManage::Init() HILOGI("Init finish, ret: %{public}d", ret); return DP_SUCCESS; } - + int32_t ServiceInfoProfileManage::UnInit() { HILOGI("call!"); { - std::lock_guard lock(dynamicStoreMutex_); + std::lock_guard lock(storeMutex_); if (serviceInfoKvAdapter_ == nullptr) { HILOGE("serviceInfoKvAdapter_ is nullptr"); return DP_KV_DB_PTR_NULL; @@ -67,7 +69,7 @@ int32_t ServiceInfoProfileManage::UnInit() HILOGI("UnInit success"); return DP_SUCCESS; } - + int32_t ServiceInfoProfileManage::ReInit() { HILOGI("call!"); @@ -86,7 +88,7 @@ int32_t ServiceInfoProfileManage::PutServiceInfoProfile(const ServiceInfoProfile std::map entries; ProfileUtils::ServiceInfoProfileToEntries(serviceInfoProfile, entries); { - std::lock_guard lock(dynamicStoreMutex_); + std::lock_guard lock(storeMutex_); if (serviceInfoKvAdapter_ == nullptr) { HILOGE("deviceProfileStore is nullptr!"); return DP_KV_DB_PTR_NULL; @@ -96,15 +98,18 @@ int32_t ServiceInfoProfileManage::PutServiceInfoProfile(const ServiceInfoProfile } } HILOGD("PutServiceInfoProfile success"); - + return DP_SUCCESS; } - + int32_t ServiceInfoProfileManage::DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) { HILOGI("regServiceId: %{public}s, userId %{public}d", ProfileUtils::GetAnonyInt32(regServiceId).c_str(), userId); int32_t res = 0; - res = ProfileControlUtils::DeleteServiceInfoProfile(serviceInfoKvAdapter_, regServiceId, userId); + { + std::lock_guard lock(storeMutex_); + res = ProfileControlUtils::DeleteServiceInfoProfile(serviceInfoKvAdapter_, regServiceId, userId); + } if (res != DP_SUCCESS) { HILOGE("DeleteServiceInfoProfile fail, reason: %{public}d!", res); return res; @@ -122,48 +127,115 @@ int32_t ServiceInfoProfileManage::GetAllServiceInfoProfileList( int32_t ServiceInfoProfileManage::GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) { - HILOGI("serviceId:%{public}s", ProfileUtils::GetAnonyInt32(serviceId).c_str()); - std::lock_guard lock(dynamicStoreMutex_); - if (serviceInfoKvAdapter_ == nullptr) { - HILOGE("serviceInfoKvAdapter_ is nullptr"); - return DP_KV_DB_PTR_NULL; + if (serviceId == 0) { + HILOGE("serviceId:%{public}s invalid", ProfileUtils::GetAnonyInt32(serviceId).c_str()); + return DP_INVALID_PARAMS; } + HILOGI("serviceId:%{public}s", ProfileUtils::GetAnonyInt32(serviceId).c_str()); std::map allEntries; - int32_t ret = serviceInfoKvAdapter_->GetByPrefix("serviceInfo", allEntries); - if (ret != DP_SUCCESS) { - HILOGE("GetServiceInfoProfileByServiceId fail, ret: %{public}d", ret); - return ret; + { + std::lock_guard lock(storeMutex_); + if (serviceInfoKvAdapter_ == nullptr) { + HILOGE("serviceInfoKvAdapter_ is nullptr"); + return DP_KV_DB_PTR_NULL; + } + int32_t ret = serviceInfoKvAdapter_->GetByPrefix(SERVICE_INFO, allEntries); + if (ret != DP_SUCCESS) { + HILOGE("GetServiceInfoProfileByServiceId fail, ret: %{public}d", ret); + return ret; + } } std::string regServiceId = ""; - std::map allServiceIdEntries; - for (const auto& pair : allEntries) { - if (pair.first.find("serviceId") != std::string::npos) { - allServiceIdEntries.insert(pair); + std::string serviceIdStr = std::to_string(serviceId); + for (const auto& [key, value] : allEntries) { + if (key.find(TOKENID) == std::string::npos || value != serviceIdStr) { + continue; } - } - for (const auto& pair : allServiceIdEntries) { - if (pair.second == std::to_string(serviceId)) { - regServiceId = FindRegServiceId(pair.first); + regServiceId = FindRegServiceId(key); + if (!regServiceId.empty()) { + break; } } if (regServiceId.empty()) { HILOGE("No match regServiceId."); return DP_NOT_FIND_DATA; } - std::string finalPrefix = SERVICE_INFO + SEPARATOR +regServiceId; - std::map profileEntries; - ret = serviceInfoKvAdapter_->GetByPrefix(finalPrefix, profileEntries); - if (ret != DP_SUCCESS) { - HILOGE("GetByPrefix fail, ret: %{public}d", ret); - return ret; + return GetServiceInfoProfileByRegServiceId(regServiceId, serviceInfoProfile); +} + +int32_t ServiceInfoProfileManage::GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) +{ + if (regServiceId == 0) { + HILOGE("regServiceId:%{public}s invalid", ProfileUtils::GetAnonyInt32(regServiceId).c_str()); + return DP_INVALID_PARAMS; + } + HILOGI("regServiceId:%{public}s", ProfileUtils::GetAnonyInt32(regServiceId).c_str()); + return GetServiceInfoProfileByRegServiceId(std::to_string(regServiceId), serviceInfoProfile); +} + +int32_t ServiceInfoProfileManage::GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfiles) +{ + if (tokenId == 0) { + HILOGE("tokenId:%{public}s invalid", ProfileUtils::GetAnonyInt32(tokenId).c_str()); + return DP_INVALID_PARAMS; } - ret = SetServiceInfoProfile(regServiceId, profileEntries, serviceInfoProfile); - if (ret != DP_SUCCESS) { - HILOGE("SetServiceInfoProfile failed"); + HILOGI("tokenId:%{public}s", ProfileUtils::GetAnonyInt32(tokenId).c_str()); + std::map allEntries; + { + std::lock_guard lock(storeMutex_); + if (serviceInfoKvAdapter_ == nullptr) { + HILOGE("serviceInfoKvAdapter_ is nullptr"); + return DP_KV_DB_PTR_NULL; + } + int32_t ret = serviceInfoKvAdapter_->GetByPrefix(SERVICE_INFO, allEntries); + if (ret != DP_SUCCESS) { + HILOGE("GetServiceInfoProfileByTokenId fail, ret: %{public}d", ret); + return ret; + } + } + std::string tokenIdStr = std::to_string(tokenId); + std::unordered_set regServiceIds; + for (const auto& [key, value] : allEntries) { + if (key.find(TOKENID) == std::string::npos || value != tokenIdStr) { + continue; + } + std::string regServiceId = FindRegServiceId(key); + if (regServiceId.empty()) { + continue; + } + regServiceIds.insert(regServiceId); + } + if (regServiceIds.empty()) { + HILOGE("No match regServiceId."); + return DP_NOT_FIND_DATA; + } + for (const auto& regServiceId : regServiceIds) { + ServiceInfoProfileNew serviceInfoProfile; + if (GetServiceInfoProfileByRegServiceId(regServiceId, serviceInfoProfile) == DP_SUCCESS) { + serviceInfoProfiles.emplace_back(serviceInfoProfile); + } + } + if (serviceInfoProfiles.empty()) { + HILOGE("serviceInfoProfiles is empty"); return DP_NOT_FIND_DATA; } - HILOGI("serviceInfoProfile: %{public}s", serviceInfoProfile.dump().c_str()); + HILOGI("serviceInfoProfiles.size: %{public}zu", serviceInfoProfiles.size()); return DP_SUCCESS; + } + +std::string ServiceInfoProfileManage::FindRegServiceId(const std::string& str) +{ + size_t firstPos = str.find(SEPARATOR); + if (firstPos == std::string::npos) { + return ""; + } + size_t secondPos = str.find(SEPARATOR, firstPos + 1); + if (secondPos == std::string::npos) { + return ""; + } + return str.substr(firstPos + 1, secondPos - firstPos - 1); } int32_t ServiceInfoProfileManage::SetServiceInfoProfile(const std::string& regServiceId, @@ -206,45 +278,24 @@ int32_t ServiceInfoProfileManage::SetServiceInfoProfile(const std::string& regSe return DP_SUCCESS; } -int32_t ServiceInfoProfileManage::GetServiceInfoProfileByTokenId(int64_t tokenId, +int32_t ServiceInfoProfileManage::GetServiceInfoProfileByRegServiceId(const std::string& regServiceIdStr, ServiceInfoProfileNew& serviceInfoProfile) { - HILOGI("tokenId:%{public}s", ProfileUtils::GetAnonyInt32(tokenId).c_str()); - std::lock_guard lock(dynamicStoreMutex_); - if (serviceInfoKvAdapter_ == nullptr) { - HILOGE("serviceInfoKvAdapter_ is nullptr"); - return DP_KV_DB_PTR_NULL; - } + int32_t ret = DP_SUCCESS; std::map allEntries; - int32_t ret = serviceInfoKvAdapter_->GetByPrefix("serviceInfo", allEntries); - if (ret != DP_SUCCESS) { - HILOGE("GetServiceInfoProfileByTokenId fail, ret: %{public}d", ret); - return ret; - } - std::string regServiceId = ""; - std::map allServiceIdEntries; - for (const auto& pair : allEntries) { - if (pair.first.find("tokenId") != std::string::npos) { - allServiceIdEntries.insert(pair); + { + std::lock_guard lock(storeMutex_); + if (serviceInfoKvAdapter_ == nullptr) { + HILOGE("serviceInfoKvAdapter_ is nullptr"); + return DP_KV_DB_PTR_NULL; } - } - for (const auto& pair : allServiceIdEntries) { - if (pair.second == std::to_string(tokenId)) { - regServiceId = FindRegServiceId(pair.first); + ret = serviceInfoKvAdapter_->GetByPrefix(SERVICE_INFO + SEPARATOR + regServiceIdStr, allEntries); + if (ret != DP_SUCCESS) { + HILOGE("Get by regServiceId fail, ret: %{public}d", ret); + return ret; } } - if (regServiceId.empty()) { - HILOGE("No match regServiceId."); - return DP_NOT_FIND_DATA; - } - std::string finalPrefix = SERVICE_INFO + SEPARATOR +regServiceId; - std::map profileEntries; - ret = serviceInfoKvAdapter_->GetByPrefix(finalPrefix, profileEntries); - if (ret != DP_SUCCESS) { - HILOGE("GetByPrefix fail, ret: %{public}d", ret); - return ret; - } - ret = SetServiceInfoProfile(regServiceId, profileEntries, serviceInfoProfile); + ret = SetServiceInfoProfile(regServiceIdStr, allEntries, serviceInfoProfile); if (ret != DP_SUCCESS) { HILOGE("SetServiceInfoProfile failed"); return DP_NOT_FIND_DATA; @@ -252,18 +303,5 @@ int32_t ServiceInfoProfileManage::GetServiceInfoProfileByTokenId(int64_t tokenId HILOGI("serviceInfoProfile: %{public}s", serviceInfoProfile.dump().c_str()); return DP_SUCCESS; } - -std::string ServiceInfoProfileManage::FindRegServiceId(const std::string& str) -{ - size_t firstPos = str.find('#'); - if (firstPos == std::string::npos) { - return ""; - } - size_t secondPos = str.find('#', firstPos + 1); - if (secondPos == std::string::npos) { - return ""; - } - return str.substr(firstPos + 1, secondPos - firstPos - 1); -} } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/utils/profile_control_utils.cpp b/services/core/src/utils/profile_control_utils.cpp index 90484742..ea217e52 100644 --- a/services/core/src/utils/profile_control_utils.cpp +++ b/services/core/src/utils/profile_control_utils.cpp @@ -464,7 +464,7 @@ int32_t ProfileControlUtils::DeleteServiceInfoProfile(std::shared_ptr keys; ProfileUtils::GenerateServiceInfoProfilekeys(strRegServiceId, keys, userId); - + if (kvStore->DeleteBatch(keys) != DP_SUCCESS) { HILOGE("DeleteServiceProfile fail!"); return DP_DEL_KV_DB_FAIL; diff --git a/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.h b/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.h index 351d45cf..355fdcf5 100644 --- a/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.h +++ b/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.h @@ -15,7 +15,7 @@ #ifndef BUSINESS_EVENT_MANAGER_FUZZER_H #define BUSINESS_EVENT_MANAGER_FUZZER_H - + #define FUZZ_PROJECT_NAME "business_event_manager_fuzzer" - + #endif \ No newline at end of file diff --git a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp index 3d01bcb6..e300cef7 100644 --- a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp @@ -104,7 +104,10 @@ class MockDistributedDeviceProfileStubNew : public DistributedDeviceProfileStubN int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfo) override; int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) override; int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) override; - int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile) override; + int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, + std::vector& serviceInfoProfiles) override; + int32_t GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) override; void DelayUnloadTask() override; bool IsInited() override; }; @@ -487,14 +490,14 @@ int32_t MockDistributedDeviceProfileStubNew::PutServiceInfoProfile(const Service (void)serviceInfo; return 0; } - + int32_t MockDistributedDeviceProfileStubNew::DeleteServiceInfoProfile(const int32_t regServiceId, int32_t userId) { (void)regServiceId; (void)userId; return 0; } - + int32_t MockDistributedDeviceProfileStubNew::GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) { @@ -502,11 +505,19 @@ int32_t MockDistributedDeviceProfileStubNew::GetServiceInfoProfileByServiceId(in (void)serviceInfoProfile; return 0; } - + int32_t MockDistributedDeviceProfileStubNew::GetServiceInfoProfileByTokenId(int64_t tokenId, - ServiceInfoProfileNew& serviceInfoProfile) + std::vector& serviceInfoProfiles) { (void)tokenId; + (void)serviceInfoProfiles; + return 0; +} + +int32_t MockDistributedDeviceProfileStubNew::GetServiceInfoProfileByRegServiceId(int32_t regServiceId, + ServiceInfoProfileNew& serviceInfoProfile) +{ + (void)regServiceId; (void)serviceInfoProfile; return 0; } -- Gitee