From 649776e9fdd887d6c8c4d05305f5a65068dd08f0 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 19 May 2025 16:58:00 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=9B=B2=E7=A9=BA?= =?UTF-8?q?=E9=96=93=E7=AB=AF=E4=BA=91=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/cloud_service_impl.cpp | 92 +++++++++++++++++++ .../service/cloud/cloud_service_impl.h | 16 ++++ .../service/cloud/cloud_service_stub.cpp | 29 ++++++ .../service/cloud/cloud_service_stub.h | 2 + .../service/cloud/cloud_types_util.cpp | 12 +++ .../service/cloud/cloud_types_util.h | 7 ++ 6 files changed, 158 insertions(+) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index f566532a7..ada9572de 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -23,6 +23,7 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "checker/checker_manager.h" +#include "cloud/change_event.h" #include "cloud/cloud_last_sync_info.h" #include "cloud/cloud_mark.h" #include "cloud/cloud_server.h" @@ -1231,6 +1232,97 @@ bool CloudServiceImpl::ReleaseUserInfo(int32_t user, CloudSyncScene scene) return true; } +bool CloudServiceImpl::CheckAccess(const std::string &bundleName, const std::string &storeId) +{ + CheckerManager::StoreInfo storeInfo; + storeInfo.uid = IPCSkeleton::GetCallingUid(); + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = bundleName; + storeInfo.storeId = storeId; + return !CheckerManager::GetInstance().GetAppId(storeInfo).empty(); +} + +int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptr notifier) +{ + if (!CheckAccess(bundleName, "")) { + ZLOGE("Permission error, bundleName:%{public}s", bundleName.c_str()); + return ERROR; + } + if (notifier == nullptr) { + ZLOGE("no notifier, bundleName:%{public}s", bundleName.c_str()); + return ERROR; + } + auto notifierProxy = iface_cast(notifier); + pid_t pid = IPCSkeleton::GetCallingPid(); + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + syncAgents_.Compute(tokenId, [bundleName, notifierProxy, pid](auto, SyncAgents &agents) { + auto agent = agents.find(pid); + if (agent == agents.end()) { + SyncAgent temp; + temp.notifiers_emplace(bundleName, notifierProxy); + agents.emplace(pid, temp); + } else { + agent->second.notifiers_.emplace(bundleName, notifierProxy); + } + return true; + }); + return SUCCESS; +} + +Details CloudServiceImpl::HandleGenDetails(const GenDetails &details) +{ + Details dbDetails; + for (const auto& [id, detail] : details) { + auto &dbDetail = dbDetails[id]; + dbDetail.progress = detail.progress; + dbDetail.code = detail.code; + for (auto &[name, table] : detail.details) { + auto &dbTable = dbDetail.details[name]; + Constant::Copy(&dbTable, &table); + } + } + return dbDetails; +} + +void CloudServiceImpl::OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result) +{ + sptr notifier = nullptr; + syncAgents_.ComputeIfPresent(storeInfo.tokenId, [¬ifier, pid, storeInfo, seqNum](auto, SyncAgents &syncAgents) { + auto it = syncAgents.find(pid); + if (it != syncAgents.end()) { + auto iter = it->second.notifiers_.find(storeInfo.bundleName); + if (iter != it->second.notifiers_.end()) { + notifier = iter->second; + } + } + return true; + }); + if (notifier != nullptr) { + notifier->OnComplete(seqNum, std::move(result)); + } +} + +int32_t CloudServiceImpl::CloudSync(const std::string &bundleName, const std::string &storeId, + const Option &option, const AsyncDetail &async) +{ + StoreInfo storeInfo; + storeInfo.bundleName = bundleName; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(storeInfo.tokenId); + storeInfo.storeName = storeId; + auto pid = IPCSkeleton::GetCallingPid(); + GenAsync asyncCallback = [this, storeInfo, seqNum = option.seqNum, pid](const GenDetails &result) mutable { + OnAsyncComplete(storeInfo, pid, seqNum, HandleGenDetails(result)); + } + auto highMode = GeneralStore::MANUAL_SYNC_MODE; + auto mixMode = static_cast(GeneralStore::MixMode(option.syncMode, highMode)); + SyncParam syncParam = { mixMode, 0, false }; + auto info = ChangeEvent::EventInfo(syncParam, false, nullptr, asyncCallback); + auto evt = std::make_unique(std::move(storeInfo), std::move(info)); + EventCenter::GetInstance().PostEvent(std::move(evt)); + return SUCCESS; +} + bool CloudServiceImpl::DoCloudSync(int32_t user, CloudSyncScene scene) { auto [status, cloudInfo] = GetCloudInfo(user); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index d0242197b..14d1ad423 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -28,11 +28,14 @@ #include "cloud_service_stub.h" #include "dfx/dfx_types.h" #include "feature/static_acts.h" +#include "../rdb/rdb_notifier_proxy.h" +#include "../rdb/rdb_watcher.h" #include "store/general_store.h" #include "sync_manager.h" #include "values_bucket.h" namespace OHOS::CloudData { +using namespace DistributedRdb; class CloudServiceImpl : public CloudServiceStub { public: using CloudLastSyncInfo = DistributedData::CloudLastSyncInfo; @@ -51,6 +54,9 @@ public: std::pair QueryLastSyncInfo( const std::string &id, const std::string &bundleName, const std::string &storeId) override; int32_t SetGlobalCloudStrategy(Strategy strategy, const std::vector &values) override; + int32_t CloudSync(const std::string &bundleName, const std::string &storeId, const Option &option, + const AsyncDetail &async) override; + int32_t InitNotifier(const std::string &bundleName, sptr notifier) override; std::pair> AllocResourceAndShare(const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates, const std::vector &columns, @@ -126,6 +132,12 @@ private: std::string bundleName; }; + struct SyncAgent { + SyncAgent() = default; + std::map> notifiers_; + } + using SyncAgents = std::map; + static std::map ConvertAction(const std::map &actions); static HapInfo GetHapInfo(uint32_t tokenId); static std::string GetDfxFaultType(CloudSyncScene scene); @@ -192,8 +204,11 @@ private: static int32_t UpdateSchemaFromHap(const HapInfo &hapInfo); static void UpdateClearWaterMark( const HapInfo &hapInfo, const SchemaMeta &newSchemaMeta, const SchemaMeta &schemaMeta); + static Details HandleGenDetails(const GenDetails &details); QueryLastResults AssembleLastResults(const std::vector &databases, const std::map &lastSyncInfos); + void OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result); + bool CheckAccess(const std::string &bundleName, const std::string &storeId); std::shared_ptr executor_; SyncManager syncManager_; @@ -203,6 +218,7 @@ private: uint64_t expireTime_ = static_cast( std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) .count()); + ConcurrentMap syncAgents_; static constexpr Handle WORK_CLOUD_INFO_UPDATE = &CloudServiceImpl::UpdateCloudInfo; static constexpr Handle WORK_SCHEMA_UPDATE = &CloudServiceImpl::UpdateSchema; diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index ace668b55..ee85df8a0 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -36,6 +36,7 @@ const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnQueryStatistics, &CloudServiceStub::OnQueryLastSyncInfo, &CloudServiceStub::OnSetGlobalCloudStrategy, + &CloudServiceStub::OnCloudSync, &CloudServiceStub::OnAllocResourceAndShare, &CloudServiceStub::OnShare, &CloudServiceStub::OnUnshare, @@ -46,6 +47,7 @@ const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnConfirmInvitation, &CloudServiceStub::OnChangeConfirmation, &CloudServiceStub::OnSetCloudStrategy, + &CloudServiceStub::OnInitNotifier, }; int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) @@ -326,4 +328,31 @@ int32_t CloudServiceStub::OnSetCloudStrategy(MessageParcel &data, MessageParcel auto status = SetCloudStrategy(strategy, values); return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } + +int32_t CloudServiceStub::OnCloudSync(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + std::string storeId; + Option option; + if (!ITypesUtil::Unmarshal(data, bundleName, storeId, option)) { + ZLOGE("Unmarshal failed, bundleName:%{public}s, storeId:%{public}s, syncMode:%{public}d, seqNum:%{public}u", + bundleName.c_str(), Anonymous::Change(storeId).c_str(), option.syncMode, option.seqNum); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = CloudSync(bundleName, storeId, option, nullptr); + return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnInitNotifier(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + sptr notifier = nullptr; + if (!ITypesUtil::Unmarshal(data, bundleName, notifier) || notifier == nullptr) { + ZLOGE("Unmarshal failed, bundleName:%{public}s, notifier is nullptr?[%{public}u]", + bundleName.c_str(), notifier == nullptr); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = InitNotifier(bundleName, notifier); + return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.h b/services/distributeddataservice/service/cloud/cloud_service_stub.h index 5f51fe846..ea1d57bd4 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.h +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.h @@ -37,6 +37,7 @@ private: int32_t OnQueryStatistics(MessageParcel &data, MessageParcel &reply); int32_t OnQueryLastSyncInfo(MessageParcel &data, MessageParcel &reply); int32_t OnSetGlobalCloudStrategy(MessageParcel &data, MessageParcel &reply); + int32_t OnCloudSync(MessageParcel &data, MessageParcel &reply); int32_t OnAllocResourceAndShare(MessageParcel &data, MessageParcel &reply); int32_t OnShare(MessageParcel &data, MessageParcel &reply); @@ -49,6 +50,7 @@ private: int32_t OnChangeConfirmation(MessageParcel &data, MessageParcel &reply); int32_t OnSetCloudStrategy(MessageParcel &data, MessageParcel &reply); + int32_t OnInitNotifier(MessageParcel &data, MessageParcel &reply); static const Handler HANDLERS[TRANS_BUTT]; }; } // namespace OHOS::CloudData diff --git a/services/distributeddataservice/service/cloud/cloud_types_util.cpp b/services/distributeddataservice/service/cloud/cloud_types_util.cpp index 116d1b72e..cc6aa141e 100644 --- a/services/distributeddataservice/service/cloud/cloud_types_util.cpp +++ b/services/distributeddataservice/service/cloud/cloud_types_util.cpp @@ -109,4 +109,16 @@ bool Unmarshalling(CloudSyncInfo &output, MessageParcel &data) { return Unmarshal(data, output.startTime, output.finishTime, output.code, output.syncStatus); } + +template<> +bool Marshalling(const Option &input, MessageParcel &data) +{ + return Marshal(data, input.syncMode, input.seqNum); +} + +template<> +bool Unmarshalling(Option &output, MessageParcel &data) +{ + return Unmarshal(data, output.syncMode, output.seqNum); +} } // namespace OHOS::ITypesUtil \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_types_util.h b/services/distributeddataservice/service/cloud/cloud_types_util.h index 7a53f6938..e4c029956 100644 --- a/services/distributeddataservice/service/cloud/cloud_types_util.h +++ b/services/distributeddataservice/service/cloud/cloud_types_util.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_TYPES_UTIL_H #include "cloud_types.h" +#include "cloud_service.h" #include "itypes_util.h" #include "values_bucket.h" @@ -32,6 +33,7 @@ using ValuesBucket = OHOS::NativeRdb::ValuesBucket; using StatisticInfo = OHOS::CloudData::StatisticInfo; using Strategy = OHOS::CloudData::Strategy; using CloudSyncInfo = OHOS::CloudData::CloudSyncInfo; +using Option = OHOS::CloudData::CloudService::Option; template<> bool Marshalling(const Participant &input, MessageParcel &data); @@ -68,5 +70,10 @@ template<> bool Marshalling(const CloudSyncInfo &input, MessageParcel &data); template<> bool Unmarshalling(CloudSyncInfo &output, MessageParcel &data); + +template<> +bool Marshalling(const Option &input, MessageParcel &data); +template<> +bool Unmarshalling(Option &output, MessageParcel &data); } // namespace OHOS::ITypesUtil #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_TYPES_UTIL_H \ No newline at end of file -- Gitee From 2cb064fa50929378fbd6e4945266048cb2b57d62 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 19 May 2025 16:58:00 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=9B=B2=E7=A9=BA?= =?UTF-8?q?=E9=96=93=E7=AB=AF=E4=BA=91=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/cloud_service_impl.cpp | 92 +++++++++++++++++++ .../service/cloud/cloud_service_impl.h | 16 ++++ .../service/cloud/cloud_service_stub.cpp | 29 ++++++ .../service/cloud/cloud_service_stub.h | 2 + .../service/cloud/cloud_types_util.cpp | 12 +++ .../service/cloud/cloud_types_util.h | 7 ++ .../service/test/cloud_data_test.cpp | 83 +++++++++++++++++ 7 files changed, 241 insertions(+) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index f566532a7..bcd73954c 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -23,6 +23,7 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "checker/checker_manager.h" +#include "cloud/change_event.h" #include "cloud/cloud_last_sync_info.h" #include "cloud/cloud_mark.h" #include "cloud/cloud_server.h" @@ -1231,6 +1232,97 @@ bool CloudServiceImpl::ReleaseUserInfo(int32_t user, CloudSyncScene scene) return true; } +bool CloudServiceImpl::CheckAccess(const std::string &bundleName, const std::string &storeId) +{ + CheckerManager::StoreInfo storeInfo; + storeInfo.uid = IPCSkeleton::GetCallingUid(); + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = bundleName; + storeInfo.storeId = storeId; + return !CheckerManager::GetInstance().GetAppId(storeInfo).empty(); +} + +int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptr notifier) +{ + if (!CheckAccess(bundleName, "")) { + ZLOGE("Permission error, bundleName:%{public}s", bundleName.c_str()); + return INVALID_ARGUMENT; + } + if (notifier == nullptr) { + ZLOGE("no notifier, bundleName:%{public}s", bundleName.c_str()); + return INVALID_ARGUMENT; + } + auto notifierProxy = iface_cast(notifier); + pid_t pid = IPCSkeleton::GetCallingPid(); + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + syncAgents_.Compute(tokenId, [bundleName, notifierProxy, pid](auto, SyncAgents &agents) { + auto agent = agents.find(pid); + if (agent == agents.end()) { + SyncAgent temp; + temp.notifiers_emplace(bundleName, notifierProxy); + agents.emplace(pid, temp); + } else { + agent->second.notifiers_.emplace(bundleName, notifierProxy); + } + return true; + }); + return SUCCESS; +} + +Details CloudServiceImpl::HandleGenDetails(const GenDetails &details) +{ + Details dbDetails; + for (const auto& [id, detail] : details) { + auto &dbDetail = dbDetails[id]; + dbDetail.progress = detail.progress; + dbDetail.code = detail.code; + for (auto &[name, table] : detail.details) { + auto &dbTable = dbDetail.details[name]; + Constant::Copy(&dbTable, &table); + } + } + return dbDetails; +} + +void CloudServiceImpl::OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result) +{ + sptr notifier = nullptr; + syncAgents_.ComputeIfPresent(storeInfo.tokenId, [¬ifier, pid, storeInfo, seqNum](auto, SyncAgents &syncAgents) { + auto it = syncAgents.find(pid); + if (it != syncAgents.end()) { + auto iter = it->second.notifiers_.find(storeInfo.bundleName); + if (iter != it->second.notifiers_.end()) { + notifier = iter->second; + } + } + return true; + }); + if (notifier != nullptr) { + notifier->OnComplete(seqNum, std::move(result)); + } +} + +int32_t CloudServiceImpl::CloudSync(const std::string &bundleName, const std::string &storeId, + const Option &option, const AsyncDetail &async) +{ + StoreInfo storeInfo; + storeInfo.bundleName = bundleName; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(storeInfo.tokenId); + storeInfo.storeName = storeId; + auto pid = IPCSkeleton::GetCallingPid(); + GenAsync asyncCallback = [this, storeInfo, seqNum = option.seqNum, pid](const GenDetails &result) mutable { + OnAsyncComplete(storeInfo, pid, seqNum, HandleGenDetails(result)); + } + auto highMode = GeneralStore::MANUAL_SYNC_MODE; + auto mixMode = static_cast(GeneralStore::MixMode(option.syncMode, highMode)); + SyncParam syncParam = { mixMode, 0, false }; + auto info = ChangeEvent::EventInfo(syncParam, false, nullptr, asyncCallback); + auto evt = std::make_unique(std::move(storeInfo), std::move(info)); + EventCenter::GetInstance().PostEvent(std::move(evt)); + return SUCCESS; +} + bool CloudServiceImpl::DoCloudSync(int32_t user, CloudSyncScene scene) { auto [status, cloudInfo] = GetCloudInfo(user); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index d0242197b..14d1ad423 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -28,11 +28,14 @@ #include "cloud_service_stub.h" #include "dfx/dfx_types.h" #include "feature/static_acts.h" +#include "../rdb/rdb_notifier_proxy.h" +#include "../rdb/rdb_watcher.h" #include "store/general_store.h" #include "sync_manager.h" #include "values_bucket.h" namespace OHOS::CloudData { +using namespace DistributedRdb; class CloudServiceImpl : public CloudServiceStub { public: using CloudLastSyncInfo = DistributedData::CloudLastSyncInfo; @@ -51,6 +54,9 @@ public: std::pair QueryLastSyncInfo( const std::string &id, const std::string &bundleName, const std::string &storeId) override; int32_t SetGlobalCloudStrategy(Strategy strategy, const std::vector &values) override; + int32_t CloudSync(const std::string &bundleName, const std::string &storeId, const Option &option, + const AsyncDetail &async) override; + int32_t InitNotifier(const std::string &bundleName, sptr notifier) override; std::pair> AllocResourceAndShare(const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates, const std::vector &columns, @@ -126,6 +132,12 @@ private: std::string bundleName; }; + struct SyncAgent { + SyncAgent() = default; + std::map> notifiers_; + } + using SyncAgents = std::map; + static std::map ConvertAction(const std::map &actions); static HapInfo GetHapInfo(uint32_t tokenId); static std::string GetDfxFaultType(CloudSyncScene scene); @@ -192,8 +204,11 @@ private: static int32_t UpdateSchemaFromHap(const HapInfo &hapInfo); static void UpdateClearWaterMark( const HapInfo &hapInfo, const SchemaMeta &newSchemaMeta, const SchemaMeta &schemaMeta); + static Details HandleGenDetails(const GenDetails &details); QueryLastResults AssembleLastResults(const std::vector &databases, const std::map &lastSyncInfos); + void OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result); + bool CheckAccess(const std::string &bundleName, const std::string &storeId); std::shared_ptr executor_; SyncManager syncManager_; @@ -203,6 +218,7 @@ private: uint64_t expireTime_ = static_cast( std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) .count()); + ConcurrentMap syncAgents_; static constexpr Handle WORK_CLOUD_INFO_UPDATE = &CloudServiceImpl::UpdateCloudInfo; static constexpr Handle WORK_SCHEMA_UPDATE = &CloudServiceImpl::UpdateSchema; diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index ace668b55..ee85df8a0 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -36,6 +36,7 @@ const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnQueryStatistics, &CloudServiceStub::OnQueryLastSyncInfo, &CloudServiceStub::OnSetGlobalCloudStrategy, + &CloudServiceStub::OnCloudSync, &CloudServiceStub::OnAllocResourceAndShare, &CloudServiceStub::OnShare, &CloudServiceStub::OnUnshare, @@ -46,6 +47,7 @@ const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnConfirmInvitation, &CloudServiceStub::OnChangeConfirmation, &CloudServiceStub::OnSetCloudStrategy, + &CloudServiceStub::OnInitNotifier, }; int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) @@ -326,4 +328,31 @@ int32_t CloudServiceStub::OnSetCloudStrategy(MessageParcel &data, MessageParcel auto status = SetCloudStrategy(strategy, values); return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } + +int32_t CloudServiceStub::OnCloudSync(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + std::string storeId; + Option option; + if (!ITypesUtil::Unmarshal(data, bundleName, storeId, option)) { + ZLOGE("Unmarshal failed, bundleName:%{public}s, storeId:%{public}s, syncMode:%{public}d, seqNum:%{public}u", + bundleName.c_str(), Anonymous::Change(storeId).c_str(), option.syncMode, option.seqNum); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = CloudSync(bundleName, storeId, option, nullptr); + return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnInitNotifier(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + sptr notifier = nullptr; + if (!ITypesUtil::Unmarshal(data, bundleName, notifier) || notifier == nullptr) { + ZLOGE("Unmarshal failed, bundleName:%{public}s, notifier is nullptr?[%{public}u]", + bundleName.c_str(), notifier == nullptr); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = InitNotifier(bundleName, notifier); + return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.h b/services/distributeddataservice/service/cloud/cloud_service_stub.h index 5f51fe846..ea1d57bd4 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.h +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.h @@ -37,6 +37,7 @@ private: int32_t OnQueryStatistics(MessageParcel &data, MessageParcel &reply); int32_t OnQueryLastSyncInfo(MessageParcel &data, MessageParcel &reply); int32_t OnSetGlobalCloudStrategy(MessageParcel &data, MessageParcel &reply); + int32_t OnCloudSync(MessageParcel &data, MessageParcel &reply); int32_t OnAllocResourceAndShare(MessageParcel &data, MessageParcel &reply); int32_t OnShare(MessageParcel &data, MessageParcel &reply); @@ -49,6 +50,7 @@ private: int32_t OnChangeConfirmation(MessageParcel &data, MessageParcel &reply); int32_t OnSetCloudStrategy(MessageParcel &data, MessageParcel &reply); + int32_t OnInitNotifier(MessageParcel &data, MessageParcel &reply); static const Handler HANDLERS[TRANS_BUTT]; }; } // namespace OHOS::CloudData diff --git a/services/distributeddataservice/service/cloud/cloud_types_util.cpp b/services/distributeddataservice/service/cloud/cloud_types_util.cpp index 116d1b72e..cc6aa141e 100644 --- a/services/distributeddataservice/service/cloud/cloud_types_util.cpp +++ b/services/distributeddataservice/service/cloud/cloud_types_util.cpp @@ -109,4 +109,16 @@ bool Unmarshalling(CloudSyncInfo &output, MessageParcel &data) { return Unmarshal(data, output.startTime, output.finishTime, output.code, output.syncStatus); } + +template<> +bool Marshalling(const Option &input, MessageParcel &data) +{ + return Marshal(data, input.syncMode, input.seqNum); +} + +template<> +bool Unmarshalling(Option &output, MessageParcel &data) +{ + return Unmarshal(data, output.syncMode, output.seqNum); +} } // namespace OHOS::ITypesUtil \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_types_util.h b/services/distributeddataservice/service/cloud/cloud_types_util.h index 7a53f6938..e4c029956 100644 --- a/services/distributeddataservice/service/cloud/cloud_types_util.h +++ b/services/distributeddataservice/service/cloud/cloud_types_util.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_TYPES_UTIL_H #include "cloud_types.h" +#include "cloud_service.h" #include "itypes_util.h" #include "values_bucket.h" @@ -32,6 +33,7 @@ using ValuesBucket = OHOS::NativeRdb::ValuesBucket; using StatisticInfo = OHOS::CloudData::StatisticInfo; using Strategy = OHOS::CloudData::Strategy; using CloudSyncInfo = OHOS::CloudData::CloudSyncInfo; +using Option = OHOS::CloudData::CloudService::Option; template<> bool Marshalling(const Participant &input, MessageParcel &data); @@ -68,5 +70,10 @@ template<> bool Marshalling(const CloudSyncInfo &input, MessageParcel &data); template<> bool Unmarshalling(CloudSyncInfo &output, MessageParcel &data); + +template<> +bool Marshalling(const Option &input, MessageParcel &data); +template<> +bool Unmarshalling(Option &output, MessageParcel &data); } // namespace OHOS::ITypesUtil #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_TYPES_UTIL_H \ No newline at end of file diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index fde1eea08..819cab502 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -1045,6 +1045,44 @@ HWTEST_F(CloudDataTest, SetCloudStrategy001, TestSize.Level1) EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); } +/** +* @tc.name: CloudSync001 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync001, TestSize.Level1) +{ + int32_t syncMode = 4; + uint32_t seqNum = 10; + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); +} + +/** +* @tc.name: InitNotifier001 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, InitNotifier001, TestSize.Level1) +{ + int32_t syncMode = 4; + uint32_t seqNum = 10; + std::string bundleName = ""; + sptr notifier = nullptr; + auto ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); + EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); + + bundleName = "bundleName"; + ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); + EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); + + sptr notifier = new RdbNotifierProxy(); + ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); + EXPECT_EQ(ret, SUCCESS); +} + /** * @tc.name: Clean * @tc.desc: @@ -1700,6 +1738,51 @@ HWTEST_F(CloudDataTest, OnSetCloudStrategy, TestSize.Level0) EXPECT_EQ(ret, ERR_NONE); } +/** +* @tc.name: OnCloudSync +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, OnCloudSync, TestSize.Level0) +{ + MessageParcel reply; + MessageParcel data; + data.WriteInterfaceToken(cloudServiceImpl_->GetDescriptor()); + auto ret = cloudServiceImpl_->OnRemoteRequest(CloudData::CloudService::TRANS_CLOUD_SYNC, data, reply); + EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR); + data.WriteInterfaceToken(cloudServiceImpl_->GetDescriptor()); + std::string bundleName = "bundleName"; + std::string storeId = "storeId"; + CloudData::CloudService::Option option; + option.syncMode = 4; + option.seqNum = 1; + ITypesUtil::Marshal(data, bundleName, storeId, option); + ret = cloudServiceImpl_->OnRemoteRequest(CloudData::CloudService::TRANS_CLOUD_SYNC, data, reply); + EXPECT_EQ(ret, ERR_NONE); +} + +/** +* @tc.name: OnInitNotifier +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, OnInitNotifier, TestSize.Level0) +{ + MessageParcel reply; + MessageParcel data; + data.WriteInterfaceToken(cloudServiceImpl_->GetDescriptor()); + auto ret = cloudServiceImpl_->OnRemoteRequest(CloudData::CloudService::TRANS_INIT_NOTIFIER, data, reply); + EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR); + data.WriteInterfaceToken(cloudServiceImpl_->GetDescriptor()); + std::string bundleName = "bundleName"; + sptr notifier = nullptr; + ITypesUtil::Marshal(data, bundleName, notifier); + ret = cloudServiceImpl_->OnRemoteRequest(CloudData::CloudService::TRANS_INIT_NOTIFIER, data, reply); + EXPECT_EQ(ret, ERR_NONE); +} + /** * @tc.name: SharingUtil001 * @tc.desc: -- Gitee From d3622f086e48cca8f9fd85d91121e9e74d8ae3bc Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 21 May 2025 09:54:34 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=AF=B9bundleName?= =?UTF-8?q?=E7=9A=84=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/include/store/general_store.h | 2 +- .../service/cloud/BUILD.gn | 1 + .../service/cloud/cloud_service_impl.cpp | 28 ++--- .../service/cloud/cloud_service_impl.h | 9 +- .../service/rdb/rdb_general_store.h | 2 +- .../service/rdb/rdb_service_impl.cpp | 2 +- .../service/test/cloud_data_test.cpp | 104 ++++++++++++++++-- 7 files changed, 111 insertions(+), 37 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index dc109463a..7f1d83b3d 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -45,7 +45,7 @@ public: CLOUD_BEGIN = 4, CLOUD_TIME_FIRST = CLOUD_BEGIN, CLOUD_NATIVE_FIRST, - CLOUD_ClOUD_FIRST, + CLOUD_CLOUD_FIRST, CLOUD_END, NEARBY_SUBSCRIBE_REMOTE, NEARBY_UNSUBSCRIBE_REMOTE, diff --git a/services/distributeddataservice/service/cloud/BUILD.gn b/services/distributeddataservice/service/cloud/BUILD.gn index b36909c07..6558bbdfd 100755 --- a/services/distributeddataservice/service/cloud/BUILD.gn +++ b/services/distributeddataservice/service/cloud/BUILD.gn @@ -47,6 +47,7 @@ ohos_source_set("distributeddata_cloud") { "${data_service_path}/service/kvdb", "${data_service_path}/service/matrix/include", "${data_service_path}/service/permission/include", + "${data_service_path}/service/rdb", "${data_service_path}/framework/include", "${data_service_path}/adapter/include/communicator", "sync_strategies", diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index bcd73954c..d2ce85a83 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -49,6 +49,7 @@ #include "sync_manager.h" #include "sync_strategies/network_sync_strategy.h" #include "utils/anonymous.h" +#include "utils/constant.h" #include "values_bucket.h" #include "xcollie.h" @@ -181,6 +182,7 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) } Execute(GenTask(0, cloudInfo.user, CloudSyncScene::DISABLE_CLOUD, { WORK_STOP_CLOUD_SYNC, WORK_SUB })); ZLOGI("DisableCloud success, id:%{public}s", Anonymous::Change(id).c_str()); + syncAgents_.Clear(); return SUCCESS; } @@ -1232,22 +1234,8 @@ bool CloudServiceImpl::ReleaseUserInfo(int32_t user, CloudSyncScene scene) return true; } -bool CloudServiceImpl::CheckAccess(const std::string &bundleName, const std::string &storeId) -{ - CheckerManager::StoreInfo storeInfo; - storeInfo.uid = IPCSkeleton::GetCallingUid(); - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.bundleName = bundleName; - storeInfo.storeId = storeId; - return !CheckerManager::GetInstance().GetAppId(storeInfo).empty(); -} - int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptr notifier) { - if (!CheckAccess(bundleName, "")) { - ZLOGE("Permission error, bundleName:%{public}s", bundleName.c_str()); - return INVALID_ARGUMENT; - } if (notifier == nullptr) { ZLOGE("no notifier, bundleName:%{public}s", bundleName.c_str()); return INVALID_ARGUMENT; @@ -1259,7 +1247,7 @@ int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptrsecond.notifiers_.emplace(bundleName, notifierProxy); @@ -1286,8 +1274,9 @@ Details CloudServiceImpl::HandleGenDetails(const GenDetails &details) void CloudServiceImpl::OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result) { + ZLOGI("tokenId=%{public}x, pid=%{public}d, seqnum=%{public}u", storeInfo.tokenId, pid, seqNum); sptr notifier = nullptr; - syncAgents_.ComputeIfPresent(storeInfo.tokenId, [¬ifier, pid, storeInfo, seqNum](auto, SyncAgents &syncAgents) { + syncAgents_.ComputeIfPresent(storeInfo.tokenId, [¬ifier, pid, storeInfo](auto, SyncAgents &syncAgents) { auto it = syncAgents.find(pid); if (it != syncAgents.end()) { auto iter = it->second.notifiers_.find(storeInfo.bundleName); @@ -1305,6 +1294,11 @@ void CloudServiceImpl::OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, ui int32_t CloudServiceImpl::CloudSync(const std::string &bundleName, const std::string &storeId, const Option &option, const AsyncDetail &async) { + if (option.syncMode < DistributedData::GeneralStore::CLOUD_BEGIN || + option.syncMode >= DistributedData::GeneralStore::CLOUD_END) { + ZLOGE("not support cloud sync, syncMode = %{public}d", option.syncMode); + return INVALID_ARGUMENT; + } StoreInfo storeInfo; storeInfo.bundleName = bundleName; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); @@ -1313,7 +1307,7 @@ int32_t CloudServiceImpl::CloudSync(const std::string &bundleName, const std::st auto pid = IPCSkeleton::GetCallingPid(); GenAsync asyncCallback = [this, storeInfo, seqNum = option.seqNum, pid](const GenDetails &result) mutable { OnAsyncComplete(storeInfo, pid, seqNum, HandleGenDetails(result)); - } + }; auto highMode = GeneralStore::MANUAL_SYNC_MODE; auto mixMode = static_cast(GeneralStore::MixMode(option.syncMode, highMode)); SyncParam syncParam = { mixMode, 0, false }; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 14d1ad423..570ac9a0f 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -28,8 +28,8 @@ #include "cloud_service_stub.h" #include "dfx/dfx_types.h" #include "feature/static_acts.h" -#include "../rdb/rdb_notifier_proxy.h" -#include "../rdb/rdb_watcher.h" +#include "rdb_notifier_proxy.h" +#include "rdb_watcher.h" #include "store/general_store.h" #include "sync_manager.h" #include "values_bucket.h" @@ -135,7 +135,7 @@ private: struct SyncAgent { SyncAgent() = default; std::map> notifiers_; - } + }; using SyncAgents = std::map; static std::map ConvertAction(const std::map &actions); @@ -204,11 +204,10 @@ private: static int32_t UpdateSchemaFromHap(const HapInfo &hapInfo); static void UpdateClearWaterMark( const HapInfo &hapInfo, const SchemaMeta &newSchemaMeta, const SchemaMeta &schemaMeta); - static Details HandleGenDetails(const GenDetails &details); + static Details HandleGenDetails(const DistributedData::GenDetails &details); QueryLastResults AssembleLastResults(const std::vector &databases, const std::map &lastSyncInfos); void OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result); - bool CheckAccess(const std::string &bundleName, const std::string &storeId); std::shared_ptr executor_; SyncManager syncManager_; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index dd7c60f61..9ba84ff17 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -59,7 +59,6 @@ public: bool IsBound(uint32_t user) override; bool IsValid(); int32_t Execute(const std::string &table, const std::string &sql) override; - int32_t SetReference(const std::vector &references); int32_t SetDistributedTables(const std::vector &tables, int32_t type, const std::vector &references) override; int32_t SetTrackerTable(const std::string& tableName, const std::set& trackerColNames, @@ -94,6 +93,7 @@ public: private: RdbGeneralStore(const RdbGeneralStore& rdbGeneralStore); RdbGeneralStore& operator=(const RdbGeneralStore& rdbGeneralStore); + int32_t SetReference(const std::vector &references); using RdbDelegate = DistributedDB::RelationalStoreDelegate; using RdbManager = DistributedDB::RelationalStoreManager; using SyncProcess = DistributedDB::SyncProcess; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index d93120bba..9e15dd01b 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -605,7 +605,7 @@ void RdbServiceImpl::DoCloudSync(const RdbSyncerParam ¶m, const RdbService:: async(HandleGenDetails(details)); } }; - auto highMode = (!predicates.tables_.empty() && option.mode == DistributedData::GeneralStore::CLOUD_ClOUD_FIRST) + auto highMode = (!predicates.tables_.empty() && option.mode == DistributedData::GeneralStore::CLOUD_CLOUD_FIRST) ? GeneralStore::ASSETS_SYNC_MODE : (option.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE); auto mixMode = static_cast(GeneralStore::MixMode(option.mode, highMode)); diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 819cab502..e830771ce 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -1053,12 +1053,98 @@ HWTEST_F(CloudDataTest, SetCloudStrategy001, TestSize.Level1) */ HWTEST_F(CloudDataTest, CloudSync001, TestSize.Level1) { - int32_t syncMode = 4; + int32_t syncMode = DistributedData::GeneralStore::NEARBY_BEGIN; + uint32_t seqNum = 10; + // invalid syncMode + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); +} + +/** +* @tc.name: CloudSync002 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync002, TestSize.Level1) +{ + int32_t syncMode = DistributedData::GeneralStore::NEARBY_PULL_PUSH; + uint32_t seqNum = 10; + // invalid syncMode + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); +} + +/** +* @tc.name: CloudSync003 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync003, TestSize.Level1) +{ + int32_t syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; + uint32_t seqNum = 10; + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); +} + +/** +* @tc.name: CloudSync004 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync004, TestSize.Level1) +{ + int32_t syncMode = DistributedData::GeneralStore::CLOUD_TIME_FIRST; + uint32_t seqNum = 10; + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); +} + +/** +* @tc.name: CloudSync005 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync005, TestSize.Level1) +{ + int32_t syncMode = DistributedData::GeneralStore::CLOUD_NATIVE_FIRST; + uint32_t seqNum = 10; + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); +} + +/** +* @tc.name: CloudSync006 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync006, TestSize.Level1) +{ + int32_t syncMode = DistributedData::GeneralStore::CLOUD_CLOUD_FIRST; uint32_t seqNum = 10; auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); } +/** +* @tc.name: CloudSync007 +* @tc.desc: +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, CloudSync007, TestSize.Level1) +{ + int32_t syncMode = DistributedData::GeneralStore::CLOUD_END; + uint32_t seqNum = 10; + auto ret = cloudServiceImpl_->CloudSync("bundleName", "storeId", { syncMode, seqNum }, nullptr); + EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); +} + /** * @tc.name: InitNotifier001 * @tc.desc: @@ -1067,20 +1153,14 @@ HWTEST_F(CloudDataTest, CloudSync001, TestSize.Level1) */ HWTEST_F(CloudDataTest, InitNotifier001, TestSize.Level1) { - int32_t syncMode = 4; - uint32_t seqNum = 10; std::string bundleName = ""; - sptr notifier = nullptr; + sptr notifier = nullptr; auto ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); bundleName = "bundleName"; ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); - - sptr notifier = new RdbNotifierProxy(); - ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); - EXPECT_EQ(ret, SUCCESS); } /** @@ -1780,7 +1860,7 @@ HWTEST_F(CloudDataTest, OnInitNotifier, TestSize.Level0) sptr notifier = nullptr; ITypesUtil::Marshal(data, bundleName, notifier); ret = cloudServiceImpl_->OnRemoteRequest(CloudData::CloudService::TRANS_INIT_NOTIFIER, data, reply); - EXPECT_EQ(ret, ERR_NONE); + EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR); } /** @@ -2508,7 +2588,7 @@ HWTEST_F(CloudDataTest, GetPriorityLevel001, TestSize.Level1) }); DistributedRdb::RdbServiceImpl rdbServiceImpl; DistributedRdb::RdbSyncerParam param{ .bundleName_ = TEST_CLOUD_BUNDLE, .storeName_ = TEST_CLOUD_STORE }; - DistributedRdb::RdbService::Option option{ .mode = GeneralStore::SyncMode::CLOUD_ClOUD_FIRST, .isAsync = true }; + DistributedRdb::RdbService::Option option{ .mode = GeneralStore::SyncMode::CLOUD_CLOUD_FIRST, .isAsync = true }; DistributedRdb::PredicatesMemo memo; memo.tables_ = { TEST_CLOUD_TABLE }; rdbServiceImpl.DoCloudSync(param, option, memo, nullptr); @@ -2552,7 +2632,7 @@ HWTEST_F(CloudDataTest, GetPriorityLevel003, TestSize.Level1) }); DistributedRdb::RdbServiceImpl rdbServiceImpl; DistributedRdb::RdbSyncerParam param{ .bundleName_ = TEST_CLOUD_BUNDLE, .storeName_ = TEST_CLOUD_STORE }; - DistributedRdb::RdbService::Option option{ .mode = GeneralStore::SyncMode::CLOUD_ClOUD_FIRST, .isAsync = true }; + DistributedRdb::RdbService::Option option{ .mode = GeneralStore::SyncMode::CLOUD_CLOUD_FIRST, .isAsync = true }; DistributedRdb::PredicatesMemo memo; rdbServiceImpl.DoCloudSync(param, option, memo, nullptr); } @@ -2573,7 +2653,7 @@ HWTEST_F(CloudDataTest, GetPriorityLevel004, TestSize.Level1) }); DistributedRdb::RdbServiceImpl rdbServiceImpl; DistributedRdb::RdbSyncerParam param{ .bundleName_ = TEST_CLOUD_BUNDLE, .storeName_ = TEST_CLOUD_STORE }; - DistributedRdb::RdbService::Option option{ .mode = GeneralStore::SyncMode::CLOUD_ClOUD_FIRST, + DistributedRdb::RdbService::Option option{ .mode = GeneralStore::SyncMode::CLOUD_CLOUD_FIRST, .seqNum = 0, .isAsync = true, .isAutoSync = true }; -- Gitee From edfa4b1055db15bbc4372a6515493cd31b55d623 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 30 May 2025 14:37:17 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=AD=BB?= =?UTF-8?q?=E4=BA=A1=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/cloud_service_impl.cpp | 13 ++++++++ .../service/cloud/cloud_service_impl.h | 5 +-- .../service/test/BUILD.gn | 1 + .../service/test/cloud_service_impl_test.cpp | 32 +++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index d2ce85a83..7b6d16c13 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -1234,6 +1234,19 @@ bool CloudServiceImpl::ReleaseUserInfo(int32_t user, CloudSyncScene scene) return true; } +int32_t CloudServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) +{ + ZLOGI("tokenId=%{public}x, pid=%{public}d, uid=%{public}d", tokenId, pid, uid); + syncAgents_.ComputeIfPresent(tokenId, [pid, &bundleName](auto, SyncAgents &syncAgents) { + auto it = syncAgents.find(pid); + if (it != syncAgents.end()) { + syncAgents.erase(pid); + } + return !syncAgents.empty(); + }); + return SUCCESS; +} + int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptr notifier) { if (notifier == nullptr) { diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 570ac9a0f..10cf7a9ed 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -57,6 +57,7 @@ public: int32_t CloudSync(const std::string &bundleName, const std::string &storeId, const Option &option, const AsyncDetail &async) override; int32_t InitNotifier(const std::string &bundleName, sptr notifier) override; + int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; std::pair> AllocResourceAndShare(const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates, const std::vector &columns, @@ -164,7 +165,9 @@ private: static std::pair GetCloudInfoFromServer(int32_t userId); static int32_t UpdateCloudInfoFromServer(int32_t user); static std::pair GetAppSchemaFromServer(int32_t user, const std::string &bundleName); + static Details HandleGenDetails(const DistributedData::GenDetails &details); + void OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result); std::pair GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); void UpgradeSchemaMeta(int32_t user, const SchemaMeta &schemaMeta); std::map ExecuteStatistics( @@ -204,10 +207,8 @@ private: static int32_t UpdateSchemaFromHap(const HapInfo &hapInfo); static void UpdateClearWaterMark( const HapInfo &hapInfo, const SchemaMeta &newSchemaMeta, const SchemaMeta &schemaMeta); - static Details HandleGenDetails(const DistributedData::GenDetails &details); QueryLastResults AssembleLastResults(const std::vector &databases, const std::map &lastSyncInfos); - void OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result); std::shared_ptr executor_; SyncManager syncManager_; diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index e9d0d0a84..3e7304128 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -134,6 +134,7 @@ ohos_unittest("CloudServiceImplTest") { external_deps = [ "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", "access_token:libtoken_setproc", "c_utils:utils", "hicollie:libhicollie", diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index 0a501550d..9fe06ff75 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -42,6 +42,7 @@ #include "mock/db_store_mock.h" #include "mock/general_store_mock.h" #include "model/component_config.h" +#include "nativetoken_kit.h" #include "network/network_delegate.h" #include "network_delegate_mock.h" #include "rdb_query.h" @@ -65,6 +66,7 @@ static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; static constexpr const char *TEST_CLOUD_STORE = "test_cloud_store"; static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_alias_1"; +static OHOS::DistributedKv::AppId appId = { "ohos.test.cloud_service_test" }; class CloudServiceImplTest : public testing::Test { public: static void SetUpTestCase(void); @@ -316,6 +318,36 @@ HWTEST_F(CloudServiceImplTest, DoSubscribe, TestSize.Level0) EXPECT_TRUE(status); } +/** +* @tc.name: OnAppExitTest001 +* @tc.desc: OnAppExit test +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(CloudServiceImplTest, OnAppExitTest001, TestSize.Level0) +{ + ZLOGI("OnAppExitTest001 start"); + pid_t uid = 1; + pid_t pid = 2; + const char **perms = new const char *[2]; + perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC"; + perms[1] = "ohos.permission.ACCESS_SERVICE_DM"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "distributed_data_test", + .aplStr = "system_basic", + }; + uint32_t tokenId = GetAccessTokenId(&infoInstance); + auto status = cloudServiceImpl_->OnAppExit(uid, pid, tokenId, appId); + ZLOGI("OnAppExitTest001 status = :%{public}d", status); + ASSERT_EQ(status, Status::SUCCESS); +} + /** * @tc.name: Share001 * @tc.desc: Test the Share with invalid parameters -- Gitee From 4d7f69003327a66fbb0aa0e76cbeb686c4fffa43 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 6 Jun 2025 18:00:08 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/BUILD.gn | 1 + .../service/cloud/cloud_notifier_proxy.cpp | 59 +++++++++++++++++++ .../service/cloud/cloud_notifier_proxy.h | 41 +++++++++++++ .../service/cloud/cloud_service_impl.cpp | 36 +++++------ .../service/cloud/cloud_service_impl.h | 6 +- .../service/test/BUILD.gn | 2 + 6 files changed, 122 insertions(+), 23 deletions(-) create mode 100644 services/distributeddataservice/service/cloud/cloud_notifier_proxy.cpp create mode 100644 services/distributeddataservice/service/cloud/cloud_notifier_proxy.h diff --git a/services/distributeddataservice/service/cloud/BUILD.gn b/services/distributeddataservice/service/cloud/BUILD.gn index 6558bbdfd..3a94f00d2 100755 --- a/services/distributeddataservice/service/cloud/BUILD.gn +++ b/services/distributeddataservice/service/cloud/BUILD.gn @@ -28,6 +28,7 @@ ohos_source_set("distributeddata_cloud") { } sources = [ "cloud_data_translate.cpp", + "cloud_notifier_proxy.cpp", "cloud_service_impl.cpp", "cloud_service_stub.cpp", "cloud_types_util.cpp", diff --git a/services/distributeddataservice/service/cloud/cloud_notifier_proxy.cpp b/services/distributeddataservice/service/cloud/cloud_notifier_proxy.cpp new file mode 100644 index 000000000..40d39763e --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_notifier_proxy.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "CloudNotifierProxy" + +#include "cloud_notifier_proxy.h" + +#include "cloud_service.h" +#include "itypes_util.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::CloudData { +using namespace DistributedRdb; +using NotifierCode = RelationalStore::ICloudNotifierInterfaceCode; + +CloudNotifierProxy::CloudNotifierProxy(const sptr &object) + : IRemoteProxy(object) +{} + +CloudNotifierProxy::~CloudNotifierProxy() noexcept {} + +int32_t CloudNotifierProxy::OnComplete(uint32_t seqNum, DistributedRdb::Details &&result) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + ZLOGE("write descriptor failed"); + return CloudService::IPC_PARCEL_ERROR; + } + if (!ITypesUtil::Marshal(data, seqNum, result)) { + return CloudService::IPC_PARCEL_ERROR; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + auto remote = Remote(); + if (remote == nullptr) { + ZLOGE("get remote failed, seqNum:%{public}u", seqNum); + return CloudService::IPC_ERROR; + } + auto status = remote->SendRequest(static_cast(NotifierCode::CLOUD_NOTIFIER_CMD_SYNC_COMPLETE), + data, reply, option); + if (status != CloudService::SUCCESS) { + ZLOGE("seqNum:%{public}u, send request failed, status:%{public}d", seqNum, status); + } + return status; +} +} // namespace OHOS::CloudData diff --git a/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h b/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h new file mode 100644 index 000000000..e5287ff38 --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_NOTIFIER_PROXY_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_NOTIFIER_PROXY_H + +#include +#include + +#include "cloud_notifier.h" + +namespace OHOS::CloudData { +class CloudNotifierProxyBroker : public ICloudNotifier, public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.CloudData.ICloudNotifier"); +}; + +class CloudNotifierProxy : public IRemoteProxy { +public: + explicit CloudNotifierProxy(const sptr& object); + virtual ~CloudNotifierProxy() noexcept; + + int32_t OnComplete(uint32_t seqNum, Details &&result) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_NOTIFIER_PROXY_H diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 7b6d16c13..791f40f3d 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -1237,9 +1237,10 @@ bool CloudServiceImpl::ReleaseUserInfo(int32_t user, CloudSyncScene scene) int32_t CloudServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) { ZLOGI("tokenId=%{public}x, pid=%{public}d, uid=%{public}d", tokenId, pid, uid); - syncAgents_.ComputeIfPresent(tokenId, [pid, &bundleName](auto, SyncAgents &syncAgents) { + syncAgents_.ComputeIfPresent(tokenId, [pid](auto, SyncAgents &syncAgents) { auto it = syncAgents.find(pid); if (it != syncAgents.end()) { + it->second.notifier_ = nullptr; syncAgents.erase(pid); } return !syncAgents.empty(); @@ -1253,18 +1254,15 @@ int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptr(notifier); + auto notifierProxy = iface_cast(notifier); pid_t pid = IPCSkeleton::GetCallingPid(); uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - syncAgents_.Compute(tokenId, [bundleName, notifierProxy, pid](auto, SyncAgents &agents) { - auto agent = agents.find(pid); - if (agent == agents.end()) { - SyncAgent temp; - temp.notifiers_.emplace(bundleName, notifierProxy); - agents.emplace(pid, temp); - } else { - agent->second.notifiers_.emplace(bundleName, notifierProxy); + syncAgents_.Compute(tokenId, [notifierProxy, pid](auto, SyncAgents &agents) { + auto [it, success] = agents.try_emplace(pid, SyncAgent()); + if (it == agents.end()) { + return true; } + it->second.notifier_ = notifierProxy; return true; }); return SUCCESS; @@ -1285,17 +1283,14 @@ Details CloudServiceImpl::HandleGenDetails(const GenDetails &details) return dbDetails; } -void CloudServiceImpl::OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result) +void CloudServiceImpl::OnAsyncComplete(uint32_t tokenId, pid_t pid, uint32_t seqNum, Details &&result) { - ZLOGI("tokenId=%{public}x, pid=%{public}d, seqnum=%{public}u", storeInfo.tokenId, pid, seqNum); - sptr notifier = nullptr; - syncAgents_.ComputeIfPresent(storeInfo.tokenId, [¬ifier, pid, storeInfo](auto, SyncAgents &syncAgents) { + ZLOGI("tokenId=%{public}x, pid=%{public}d, seqnum=%{public}u", tokenId, pid, seqNum); + sptr notifier = nullptr; + syncAgents_.ComputeIfPresent(tokenId, [¬ifier, pid](auto, SyncAgents &syncAgents) { auto it = syncAgents.find(pid); if (it != syncAgents.end()) { - auto iter = it->second.notifiers_.find(storeInfo.bundleName); - if (iter != it->second.notifiers_.end()) { - notifier = iter->second; - } + notifier = it->second.notifier_; } return true; }); @@ -1318,8 +1313,9 @@ int32_t CloudServiceImpl::CloudSync(const std::string &bundleName, const std::st storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(storeInfo.tokenId); storeInfo.storeName = storeId; auto pid = IPCSkeleton::GetCallingPid(); - GenAsync asyncCallback = [this, storeInfo, seqNum = option.seqNum, pid](const GenDetails &result) mutable { - OnAsyncComplete(storeInfo, pid, seqNum, HandleGenDetails(result)); + GenAsync asyncCallback = + [this, tokenId = storeInfo.tokenId, seqNum = option.seqNum, pid](const GenDetails &result) mutable { + OnAsyncComplete(tokenId, pid, seqNum, HandleGenDetails(result)); }; auto highMode = GeneralStore::MANUAL_SYNC_MODE; auto mixMode = static_cast(GeneralStore::MixMode(option.syncMode, highMode)); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 10cf7a9ed..3eecf563a 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -22,13 +22,13 @@ #include "cloud/cloud_event.h" #include "cloud/cloud_extra_data.h" #include "cloud/cloud_info.h" +#include "cloud_notifier_proxy.h" #include "cloud/schema_meta.h" #include "cloud/sharing_center.h" #include "cloud/subscription.h" #include "cloud_service_stub.h" #include "dfx/dfx_types.h" #include "feature/static_acts.h" -#include "rdb_notifier_proxy.h" #include "rdb_watcher.h" #include "store/general_store.h" #include "sync_manager.h" @@ -135,7 +135,7 @@ private: struct SyncAgent { SyncAgent() = default; - std::map> notifiers_; + sptr notifier_; }; using SyncAgents = std::map; @@ -167,7 +167,7 @@ private: static std::pair GetAppSchemaFromServer(int32_t user, const std::string &bundleName); static Details HandleGenDetails(const DistributedData::GenDetails &details); - void OnAsyncComplete(const StoreInfo &storeInfo, pid_t pid, uint32_t seqNum, Details &&result); + void OnAsyncComplete(uint32_t tokenId, pid_t pid, uint32_t seqNum, Details &&result); std::pair GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); void UpgradeSchemaMeta(int32_t user, const SchemaMeta &schemaMeta); std::map ExecuteStatistics( diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 3e7304128..b8320db6e 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -68,6 +68,7 @@ ohos_unittest("CloudDataTest") { module_out_path = module_output_path sources = [ "${data_service_path}/service/cloud/cloud_data_translate.cpp", + "${data_service_path}/service/cloud/cloud_notifier_proxy.cpp", "${data_service_path}/service/cloud/cloud_service_impl.cpp", "${data_service_path}/service/cloud/cloud_service_stub.cpp", "${data_service_path}/service/cloud/cloud_types_util.cpp", @@ -120,6 +121,7 @@ ohos_unittest("CloudServiceImplTest") { module_out_path = module_output_path sources = [ "${data_service_path}/service/cloud/cloud_data_translate.cpp", + "${data_service_path}/service/cloud/cloud_notifier_proxy.cpp", "${data_service_path}/service/cloud/cloud_service_impl.cpp", "${data_service_path}/service/cloud/cloud_service_stub.cpp", "${data_service_path}/service/cloud/cloud_types_util.cpp", -- Gitee From 50c7cc9f4e775ddb1b19091457f44e523412c690 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 9 Jun 2025 22:35:48 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=AD=BB=E4=BA=A1?= =?UTF-8?q?=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/BUILD.gn | 1 - .../service/cloud/cloud_notifier_proxy.h | 2 +- .../service/cloud/cloud_service_impl.cpp | 45 +++++-------------- .../service/cloud/cloud_service_impl.h | 9 ++-- .../service/cloud/cloud_service_stub.cpp | 8 ++-- .../service/test/BUILD.gn | 1 - .../service/test/cloud_data_test.cpp | 6 +-- .../service/test/cloud_service_impl_test.cpp | 32 ------------- 8 files changed, 20 insertions(+), 84 deletions(-) diff --git a/services/distributeddataservice/service/cloud/BUILD.gn b/services/distributeddataservice/service/cloud/BUILD.gn index 3a94f00d2..3da5401eb 100755 --- a/services/distributeddataservice/service/cloud/BUILD.gn +++ b/services/distributeddataservice/service/cloud/BUILD.gn @@ -48,7 +48,6 @@ ohos_source_set("distributeddata_cloud") { "${data_service_path}/service/kvdb", "${data_service_path}/service/matrix/include", "${data_service_path}/service/permission/include", - "${data_service_path}/service/rdb", "${data_service_path}/framework/include", "${data_service_path}/adapter/include/communicator", "sync_strategies", diff --git a/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h b/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h index e5287ff38..97829ba0c 100644 --- a/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h +++ b/services/distributeddataservice/service/cloud/cloud_notifier_proxy.h @@ -32,7 +32,7 @@ public: explicit CloudNotifierProxy(const sptr& object); virtual ~CloudNotifierProxy() noexcept; - int32_t OnComplete(uint32_t seqNum, Details &&result) override; + int32_t OnComplete(uint32_t seqNum, DistributedRdb::Details &&result) override; private: static inline BrokerDelegator delegator_; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 791f40f3d..dfdc9ecc8 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -182,7 +182,6 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) } Execute(GenTask(0, cloudInfo.user, CloudSyncScene::DISABLE_CLOUD, { WORK_STOP_CLOUD_SYNC, WORK_SUB })); ZLOGI("DisableCloud success, id:%{public}s", Anonymous::Change(id).c_str()); - syncAgents_.Clear(); return SUCCESS; } @@ -1234,35 +1233,17 @@ bool CloudServiceImpl::ReleaseUserInfo(int32_t user, CloudSyncScene scene) return true; } -int32_t CloudServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) -{ - ZLOGI("tokenId=%{public}x, pid=%{public}d, uid=%{public}d", tokenId, pid, uid); - syncAgents_.ComputeIfPresent(tokenId, [pid](auto, SyncAgents &syncAgents) { - auto it = syncAgents.find(pid); - if (it != syncAgents.end()) { - it->second.notifier_ = nullptr; - syncAgents.erase(pid); - } - return !syncAgents.empty(); - }); - return SUCCESS; -} - -int32_t CloudServiceImpl::InitNotifier(const std::string &bundleName, sptr notifier) +int32_t CloudServiceImpl::InitNotifier(sptr notifier) { if (notifier == nullptr) { - ZLOGE("no notifier, bundleName:%{public}s", bundleName.c_str()); + ZLOGE("no notifier."); return INVALID_ARGUMENT; } auto notifierProxy = iface_cast(notifier); - pid_t pid = IPCSkeleton::GetCallingPid(); uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - syncAgents_.Compute(tokenId, [notifierProxy, pid](auto, SyncAgents &agents) { - auto [it, success] = agents.try_emplace(pid, SyncAgent()); - if (it == agents.end()) { - return true; - } - it->second.notifier_ = notifierProxy; + syncAgents_.Compute(tokenId, [notifierProxy](auto, SyncAgent &agent) { + agent = SyncAgent(); + agent.notifier_ = notifierProxy; return true; }); return SUCCESS; @@ -1283,15 +1264,12 @@ Details CloudServiceImpl::HandleGenDetails(const GenDetails &details) return dbDetails; } -void CloudServiceImpl::OnAsyncComplete(uint32_t tokenId, pid_t pid, uint32_t seqNum, Details &&result) +void CloudServiceImpl::OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details &&result) { - ZLOGI("tokenId=%{public}x, pid=%{public}d, seqnum=%{public}u", tokenId, pid, seqNum); + ZLOGI("tokenId=%{public}x, seqnum=%{public}u", tokenId, seqNum); sptr notifier = nullptr; - syncAgents_.ComputeIfPresent(tokenId, [¬ifier, pid](auto, SyncAgents &syncAgents) { - auto it = syncAgents.find(pid); - if (it != syncAgents.end()) { - notifier = it->second.notifier_; - } + syncAgents_.ComputeIfPresent(tokenId, [¬ifier](auto, SyncAgent &syncAgent) { + notifier = syncAgent.notifier_; return true; }); if (notifier != nullptr) { @@ -1312,10 +1290,9 @@ int32_t CloudServiceImpl::CloudSync(const std::string &bundleName, const std::st storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(storeInfo.tokenId); storeInfo.storeName = storeId; - auto pid = IPCSkeleton::GetCallingPid(); GenAsync asyncCallback = - [this, tokenId = storeInfo.tokenId, seqNum = option.seqNum, pid](const GenDetails &result) mutable { - OnAsyncComplete(tokenId, pid, seqNum, HandleGenDetails(result)); + [this, tokenId = storeInfo.tokenId, seqNum = option.seqNum](const GenDetails &result) mutable { + OnAsyncComplete(tokenId, seqNum, HandleGenDetails(result)); }; auto highMode = GeneralStore::MANUAL_SYNC_MODE; auto mixMode = static_cast(GeneralStore::MixMode(option.syncMode, highMode)); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 3eecf563a..271627229 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -29,7 +29,6 @@ #include "cloud_service_stub.h" #include "dfx/dfx_types.h" #include "feature/static_acts.h" -#include "rdb_watcher.h" #include "store/general_store.h" #include "sync_manager.h" #include "values_bucket.h" @@ -56,8 +55,7 @@ public: int32_t SetGlobalCloudStrategy(Strategy strategy, const std::vector &values) override; int32_t CloudSync(const std::string &bundleName, const std::string &storeId, const Option &option, const AsyncDetail &async) override; - int32_t InitNotifier(const std::string &bundleName, sptr notifier) override; - int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; + int32_t InitNotifier(sptr notifier) override; std::pair> AllocResourceAndShare(const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates, const std::vector &columns, @@ -137,7 +135,6 @@ private: SyncAgent() = default; sptr notifier_; }; - using SyncAgents = std::map; static std::map ConvertAction(const std::map &actions); static HapInfo GetHapInfo(uint32_t tokenId); @@ -167,7 +164,7 @@ private: static std::pair GetAppSchemaFromServer(int32_t user, const std::string &bundleName); static Details HandleGenDetails(const DistributedData::GenDetails &details); - void OnAsyncComplete(uint32_t tokenId, pid_t pid, uint32_t seqNum, Details &&result); + void OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details &&result); std::pair GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); void UpgradeSchemaMeta(int32_t user, const SchemaMeta &schemaMeta); std::map ExecuteStatistics( @@ -218,7 +215,7 @@ private: uint64_t expireTime_ = static_cast( std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) .count()); - ConcurrentMap syncAgents_; + ConcurrentMap syncAgents_; static constexpr Handle WORK_CLOUD_INFO_UPDATE = &CloudServiceImpl::UpdateCloudInfo; static constexpr Handle WORK_SCHEMA_UPDATE = &CloudServiceImpl::UpdateSchema; diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index ee85df8a0..f1a2fd85f 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -345,14 +345,12 @@ int32_t CloudServiceStub::OnCloudSync(MessageParcel &data, MessageParcel &reply) int32_t CloudServiceStub::OnInitNotifier(MessageParcel &data, MessageParcel &reply) { - std::string bundleName; sptr notifier = nullptr; - if (!ITypesUtil::Unmarshal(data, bundleName, notifier) || notifier == nullptr) { - ZLOGE("Unmarshal failed, bundleName:%{public}s, notifier is nullptr?[%{public}u]", - bundleName.c_str(), notifier == nullptr); + if (!ITypesUtil::Unmarshal(data, notifier) || notifier == nullptr) { + ZLOGE("Unmarshal failed, notifier is nullptr?[%{public}u]", notifier == nullptr); return IPC_STUB_INVALID_DATA_ERR; } - auto status = InitNotifier(bundleName, notifier); + auto status = InitNotifier(notifier); return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index b8320db6e..b741c1dc2 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -136,7 +136,6 @@ ohos_unittest("CloudServiceImplTest") { external_deps = [ "access_token:libaccesstoken_sdk", - "access_token:libnativetoken", "access_token:libtoken_setproc", "c_utils:utils", "hicollie:libhicollie", diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index e830771ce..86440d4ff 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -1153,13 +1153,11 @@ HWTEST_F(CloudDataTest, CloudSync007, TestSize.Level1) */ HWTEST_F(CloudDataTest, InitNotifier001, TestSize.Level1) { - std::string bundleName = ""; sptr notifier = nullptr; - auto ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); + auto ret = cloudServiceImpl_->InitNotifier(notifier); EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); - bundleName = "bundleName"; - ret = cloudServiceImpl_->InitNotifier(bundleName, notifier); + ret = cloudServiceImpl_->InitNotifier(notifier); EXPECT_EQ(ret, CloudData::CloudService::INVALID_ARGUMENT); } diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index 9fe06ff75..0a501550d 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -42,7 +42,6 @@ #include "mock/db_store_mock.h" #include "mock/general_store_mock.h" #include "model/component_config.h" -#include "nativetoken_kit.h" #include "network/network_delegate.h" #include "network_delegate_mock.h" #include "rdb_query.h" @@ -66,7 +65,6 @@ static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; static constexpr const char *TEST_CLOUD_STORE = "test_cloud_store"; static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_alias_1"; -static OHOS::DistributedKv::AppId appId = { "ohos.test.cloud_service_test" }; class CloudServiceImplTest : public testing::Test { public: static void SetUpTestCase(void); @@ -318,36 +316,6 @@ HWTEST_F(CloudServiceImplTest, DoSubscribe, TestSize.Level0) EXPECT_TRUE(status); } -/** -* @tc.name: OnAppExitTest001 -* @tc.desc: OnAppExit test -* @tc.type: FUNC -* @tc.author: -*/ -HWTEST_F(CloudServiceImplTest, OnAppExitTest001, TestSize.Level0) -{ - ZLOGI("OnAppExitTest001 start"); - pid_t uid = 1; - pid_t pid = 2; - const char **perms = new const char *[2]; - perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC"; - perms[1] = "ohos.permission.ACCESS_SERVICE_DM"; - TokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = 2, - .aclsNum = 0, - .dcaps = nullptr, - .perms = perms, - .acls = nullptr, - .processName = "distributed_data_test", - .aplStr = "system_basic", - }; - uint32_t tokenId = GetAccessTokenId(&infoInstance); - auto status = cloudServiceImpl_->OnAppExit(uid, pid, tokenId, appId); - ZLOGI("OnAppExitTest001 status = :%{public}d", status); - ASSERT_EQ(status, Status::SUCCESS); -} - /** * @tc.name: Share001 * @tc.desc: Test the Share with invalid parameters -- Gitee