diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index aff70ea74eab98065f19343ec6e82520109788d2..1cca4c4f8e9cec8e30870cbb02d01304cc77d7f7 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -59,19 +59,45 @@ bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) std::string CloudInfo::GetKey() const { - return GetKey(INFO_PREFIX, { id, std::to_string(user), account }); + return GetKey(INFO_PREFIX, { std::to_string(user), id }); } std::map CloudInfo::GetSchemaKey() const { std::map keys; for (const auto &app : apps) { - const auto key = GetKey(SCHEMA_PREFIX, { id, std::to_string(user), account, app.bundleName }); + const auto key = GetKey(SCHEMA_PREFIX, { std::to_string(user), id, app.bundleName }); keys.insert_or_assign(app.bundleName, key); } return keys; } +bool CloudInfo::IsValid() const +{ + return !id.empty(); +} + +bool CloudInfo::IsExist(const std::string &appId) const +{ + for (const auto &app : apps) { + if (app.appId == appId) { + return true; + } + } + return false; +} + +void CloudInfo::DelApp(const std::string &appId) +{ + for (auto it = apps.begin(); it != apps.end();) { + if ((*it).appId == appId) { + it = apps.erase(it); + break; + } + it++; + } +} + std::string CloudInfo::GetPrefix(const std::initializer_list &fields) { return GetKey(INFO_PREFIX, fields).append(Constant::KEY_SEPARATOR); diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index 9148e8b214b05478e76074ae07bfb91a9aefbaa2..89b09e86f8bd1781aed3862e634ac5ba31fb561c 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -20,9 +20,10 @@ namespace OHOS::DistributedData { class API_EXPORT CloudInfo final : public Serializable { public: struct API_EXPORT AppInfo final : public Serializable { + static constexpr uint32_t CURRENT_VERSION = 0x04000001; std::string bundleName; std::string appId; - uint64_t version; + uint64_t version = CURRENT_VERSION; bool cloudSwitch = false; bool Marshal(json &node) const override; @@ -30,7 +31,6 @@ public: }; int32_t user = 0; std::string id = ""; - std::string account = ""; uint64_t totalSpace = 0; uint64_t remainSpace = 0; bool enableCloud = false; @@ -38,7 +38,10 @@ public: std::string GetKey() const; std::map GetSchemaKey() const; - static std::string GetPrefix(const std::initializer_list &fields); + bool IsValid() const; + bool IsExist(const std::string &appId) const; + void DelApp(const std::string &appId); + static std::string GetPrefix(const std::initializer_list &field); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 81b3ebdec861bb387f946b69b15ec06a902eaee0..983ca503fcef6bdaf14ea7e0dc216d6c60d439c8 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -24,7 +24,7 @@ #include "eventcenter/event_center.h" #include "feature/feature_system.h" #include "ipc_skeleton.h" -#include "kvstore_utils.h" +#include "utils/anonymous.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "rdb_event.h" @@ -48,13 +48,17 @@ CloudServiceImpl::Factory::~Factory() {} CloudServiceImpl::CloudServiceImpl() { EventCenter::GetInstance().Subscribe(RdbEvent::RDB_FEATURE_INIT, [this](const Event &event) { - auto &rdbEvent = static_cast(event); - auto user = AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); - auto cloudInfo = CloudServer::GetInstance()->GetServerInfo(user); - if (cloudInfo.id.empty()) { + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { ZLOGI("no cloud server"); return; } + auto &rdbEvent = static_cast(event); + auto cloudInfo = instance->GetServerInfo(rdbEvent.GetUser()); + if (!cloudInfo.IsValid()) { + ZLOGI("accountId empty"); + return; + } UpdateCloudInfo(cloudInfo); UpdateSchema(cloudInfo); }); @@ -138,11 +142,9 @@ int32_t CloudServiceImpl::GetCloudInfo(const std::string &id, CloudInfo &cloudIn cloudInfo.id = id; uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); - cloudInfo.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { - ZLOGE("invalid argument id:%{public}s, user:%{public}d, account:%{public}s", - KvStoreUtils::ToBeAnonymous(cloudInfo.id).c_str(), cloudInfo.user, - KvStoreUtils::ToBeAnonymous(cloudInfo.account).c_str()); + ZLOGE("invalid argument id:%{public}s, user:%{public}d", + Anonymous::Change(cloudInfo.id).c_str(), cloudInfo.user); return ERROR; } return SUCCESS; @@ -172,5 +174,19 @@ void CloudServiceImpl::UpdateCloudInfo(DistributedData::CloudInfo &cloudInfo) void CloudServiceImpl::UpdateSchema(DistributedData::CloudInfo &cloudInfo) { + auto keys = cloudInfo.GetSchemaKey(); + for (const auto &key : keys) { + SchemaMeta schemaMeta; + if (MetaDataManager::GetInstance().LoadMeta(key.second, schemaMeta, true)) { + continue; + } + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("no cloud server"); + return; + } + schemaMeta = instance->GetAppSchema(cloudInfo.user, key.first); + MetaDataManager::GetInstance().SaveMeta(key.second, schemaMeta, true); + } } } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp index e968beb2b29ccd101eebea9af22b8f6df2813b30..a80cca1855757dd88654a246194cc2448074f6fb 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp @@ -16,13 +16,29 @@ #include "rdb_event.h" namespace OHOS::DistributedRdb { -RdbEvent::RdbEvent(int32_t evtId, uint32_t tokenId) - : DistributedData::Event(evtId), tokenId_(tokenId) +RdbEvent::RdbEvent(int32_t evtId, int32_t user, const std::string &bundleName, const std::string &storeName) + : DistributedData::Event(evtId), user_(user), bundleName_(std::move(bundleName)), storeName_(std::move(storeName)) { } -uint32_t RdbEvent::GetTokenId() const +int32_t RdbEvent::GetUser() const { - return tokenId_; + return user_; +} + +std::string RdbEvent::GetBundleName() const +{ + return bundleName_; +} + +std::string RdbEvent::GetStoreName() const +{ + return storeName_; +} + +bool RdbEvent::Equals(const DistributedData::Event &event) const +{ + auto &evt = static_cast(event); + return (user_ == evt.user_) && (bundleName_ == evt.bundleName_) && (storeName_ == evt.storeName_); } } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h index a70525a1ceb6db4c6836d22f79b6201210ceb5a5..d3d83c3358e6bc424a8e74cc911d46e5d81f7fc2 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H #define OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H +#include #include "eventcenter/event.h" namespace OHOS::DistributedRdb { @@ -27,12 +28,19 @@ public: RDB_BUTT }; - RdbEvent(int32_t evtId, uint32_t tokenId); + RdbEvent(int32_t evtId, int32_t user, const std::string &bundleName, const std::string &storeName); ~RdbEvent() = default; - uint32_t GetTokenId() const; + int32_t GetUser() const; + std::string GetBundleName() const; + std::string GetStoreName() const; + bool Equals(const DistributedData::Event &event) const override; private: - uint32_t tokenId_; + int32_t user_; + int32_t schemaVersion_; + std::string bundleName_; + std::string storeName_; + std::string cloudId_; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index a19ad18b3680ac2f692ac472c602a28ff50c217d..35a9fbf1a135d598758006ad348b67fb8d3f01b2 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -17,6 +17,7 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "checker/checker_manager.h" +#include "cloud/cloud_server.h" #include "communicator/device_manager_adapter.h" #include "crypto_manager.h" #include "eventcenter/event_center.h" @@ -140,16 +141,12 @@ void RdbServiceImpl::OnClientDied(pid_t pid) bool RdbServiceImpl::CheckAccess(const RdbSyncerParam ¶m) { - CheckerManager::StoreInfo storeInfo; - storeInfo.uid = IPCSkeleton::GetCallingUid(); - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.bundleName = param.bundleName_; - storeInfo.storeId = RdbSyncer::RemoveSuffix(param.storeName_); - auto instanceId = RdbSyncer::GetInstIndex(storeInfo.tokenId, storeInfo.bundleName); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + auto instanceId = RdbSyncer::GetInstIndex(tokenId, param.bundleName_); if (instanceId != 0) { return false; } - return !CheckerManager::GetInstance().GetAppId(storeInfo).empty(); + return !GetAppId(param.bundleName_).empty(); } std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device, const std::string &table) @@ -169,7 +166,10 @@ int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam& param, const sptrGetUserByToken(token); - std::string appId = CheckerManager::GetInstance().GetAppId(storeInfo); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + auto user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + std::string appId = GetAppId(param.bundleName_); std::string identifier = RelationalStoreManager::GetRelationalStoreIdentifier( - std::to_string(userId), appId, storeId); + std::to_string(user), appId, RdbSyncer::RemoveSuffix(param.storeName_)); return TransferStringToHex(identifier); } @@ -393,6 +390,109 @@ int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::stri return syncer->RemoteQuery(device, sql, selectionArgs, resultSet); } +int32_t RdbServiceImpl::CloudConfig(const CloudParam ¶m, std::string &schema) +{ + if (!CheckAccess({.bundleName_ = param.bundleName, .storeName_ = param.storeName})) { + ZLOGE("permission error"); + return RDB_ERROR; + } + auto tokenId = IPCSkeleton::GetCallingTokenID(); + auto user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + if (ConfigCloudInfo(param, user) != RDB_OK) { + return RDB_ERROR; + } + auto createEvt = std::make_unique( + RdbEvent::RDB_CREATE, user, param.bundleName, RdbSyncer::RemoveSuffix(param.storeName)); + EventCenter::GetInstance().PostEvent(std::move(createEvt)); + if (GetSchema(param, user, schema) != RDB_OK) { + return RDB_ERROR; + } + return RDB_OK; +} + +int32_t RdbServiceImpl::GetSchema(const CloudParam ¶m, int32_t user, std::string &schema) +{ + CloudInfo cloudInfo; + cloudInfo.id = param.cloudId; + cloudInfo.user = user; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + ZLOGE("cloudId:%{public}s, user:%{public}d", param.cloudId.c_str(), user); + return RDB_ERROR; + } + auto appId = GetAppId(param.bundleName); + if (!cloudInfo.IsExist(appId)) { + ZLOGE("bundleName:%{public}s", param.bundleName.c_str()); + return RDB_ERROR; + } + SchemaMeta schemaMeta; + auto keys = cloudInfo.GetSchemaKey(); + if (!MetaDataManager::GetInstance().LoadMeta(keys[param.bundleName], schemaMeta, true)) { + ZLOGE("schema empty, bundleName:%{public}s", param.bundleName.c_str()); + return RDB_ERROR; + } + schema = Serializable::Marshall(schemaMeta); + return RDB_OK; +} + +int32_t RdbServiceImpl::ConfigCloudInfo(const CloudParam ¶m, int32_t user) +{ + CloudInfo cloudInfo; + cloudInfo.id = param.cloudId; + cloudInfo.user = user; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + return RDB_ERROR; + } + cloudInfo = instance->GetServerInfo(user); + if (!cloudInfo.IsValid()) { + return RDB_ERROR; + } + } + auto appId = GetAppId(param.bundleName); + if (!cloudInfo.IsExist(appId)) { + CloudInfo::AppInfo appInfo; + appInfo.bundleName = param.bundleName; + appInfo.appId = appId; + cloudInfo.apps.emplace_back(appInfo); + } + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + return RDB_OK; +} + +int32_t RdbServiceImpl::ClearCloudConfig(const CloudParam ¶m) +{ + if (!CheckAccess({.bundleName_ = param.bundleName, .storeName_ = param.storeName})) { + ZLOGE("permission error"); + return RDB_ERROR; + } + auto tokenId = IPCSkeleton::GetCallingTokenID(); + CloudInfo cloudInfo; + cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + cloudInfo.id = param.cloudId; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + ZLOGE("invalid argument id:%{public}s, user:%{public}d", + Anonymous::Change(cloudInfo.id).c_str(), cloudInfo.user); + return RDB_ERROR; + } + auto appId = GetAppId(param.bundleName); + if (cloudInfo.IsExist(appId)) { + auto keys = cloudInfo.GetSchemaKey(); + MetaDataManager::GetInstance().DelMeta(keys[param.bundleName], true); + cloudInfo.DelApp(appId); + } + return RDB_OK; +} + +std::string RdbServiceImpl::GetAppId(const std::string &bundleName) +{ + CheckerManager::StoreInfo storeInfo; + storeInfo.uid = IPCSkeleton::GetCallingUid(); + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = bundleName; + return CheckerManager::GetInstance().GetAppId(storeInfo); +} + int32_t RdbServiceImpl::CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) { @@ -451,9 +551,9 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - EventCenter::Defer defer; auto tokenId = IPCSkeleton::GetCallingTokenID(); - auto initEvt = std::make_unique(RdbEvent::RDB_FEATURE_INIT, tokenId); + auto user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + auto initEvt = std::make_unique(RdbEvent::RDB_FEATURE_INIT, user, "", ""); EventCenter::GetInstance().PostEvent(std::move(initEvt)); return RDB_OK; } diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h index 3d2324bd92b5a19179f3ac629381cae03a6aa12b..64792d8ddc1b0bd003b1d611e1c3f3f4e0a07610 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -69,6 +69,10 @@ protected: int32_t DoUnSubscribe(const RdbSyncerParam& param) override; + int32_t CloudConfig(const CloudParam ¶m, std::string &schema) override; + + int32_t ClearCloudConfig(const CloudParam ¶m) override; + private: std::string GenIdentifier(const RdbSyncerParam& param); @@ -80,6 +84,12 @@ private: void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result); + int32_t ConfigCloudInfo(const CloudParam ¶m, int32_t user); + + int32_t GetSchema(const CloudParam ¶m, int32_t user, std::string &schema); + + std::string GetAppId(const std::string &bundleName); + class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: using DeathCallback = std::function; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index fc8a54726d85534da98269ed891fa1120e6f8d96..de06ccf82152564f69d06d01e6203e1fee445fdb 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -44,19 +44,9 @@ int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel reply.WriteInt32(RDB_ERROR); return RDB_OK; } - if (notifier == nullptr) { - ZLOGE("notifier is null"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - if (InitNotifier(param, notifier) != RDB_OK) { - ZLOGE("init notifier failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - ZLOGI("success"); - reply.WriteInt32(RDB_OK); - return RDB_OK; + + auto status = InitNotifier(param, notifier); + return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; } int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply) @@ -69,8 +59,8 @@ int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, Messag return RDB_OK; } - reply.WriteInt32(SetDistributedTables(param, tables)); - return RDB_OK; + auto status = SetDistributedTables(param, tables); + return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; } int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply) @@ -85,15 +75,8 @@ int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply } SyncResult result; - if (DoSync(param, option, predicates, result) != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - if (!ITypesUtil::Marshal(reply, result)) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - return RDB_OK; + auto status = DoSync(param, option, predicates, result); + return ITypesUtil::Marshal(reply, status, result) ? RDB_OK : RDB_ERROR; } int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply) @@ -108,8 +91,8 @@ int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &repl return RDB_OK; } - reply.WriteInt32(DoAsync(param, seqNum, option, predicates)); - return RDB_OK; + auto status = DoAsync(param, seqNum, option, predicates); + return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; } int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply) @@ -150,13 +133,7 @@ int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel sptr resultSet; int32_t status = RemoteQuery(param, device, sql, selectionArgs, resultSet); - if (status != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - reply.WriteInt32(RDB_OK); - reply.WriteRemoteObject(resultSet); - return RDB_OK; + return ITypesUtil::Marshal(reply, status, resultSet) ? RDB_OK : RDB_ERROR; } bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data) @@ -194,12 +171,7 @@ int32_t RdbServiceStub::OnRemoteDoCreateTable(MessageParcel &data, MessageParcel } int32_t status = CreateRDBTable(param, writePermission, readPermission); - if (status != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - reply.WriteInt32(RDB_OK); - return RDB_OK; + return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; } int32_t RdbServiceStub::OnRemoteDoDestroyTable(MessageParcel &data, MessageParcel &reply) @@ -212,11 +184,33 @@ int32_t RdbServiceStub::OnRemoteDoDestroyTable(MessageParcel &data, MessageParce } int32_t status = DestroyRDBTable(param); - if (status != RDB_OK) { + return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; +} + +int32_t RdbServiceStub::OnRemoteDoCloudConfig(MessageParcel& data, MessageParcel& reply) +{ + CloudParam param; + if (!ITypesUtil::Unmarshal(data, param)) { + ZLOGE("read from message parcel failed"); reply.WriteInt32(RDB_ERROR); return RDB_OK; } - reply.WriteInt32(RDB_OK); - return RDB_OK; + + std::string schema; + int32_t status = CloudConfig(param, schema); + return ITypesUtil::Marshal(reply, status, schema) ? RDB_OK : RDB_ERROR; +} + +int32_t RdbServiceStub::OnRemoteDoClearCloudConfig(MessageParcel& data, MessageParcel& reply) +{ + CloudParam param; + if (!ITypesUtil::Unmarshal(data, param)) { + ZLOGE("read from message parcel failed"); + reply.WriteInt32(RDB_ERROR); + return RDB_OK; + } + + int32_t status = ClearCloudConfig(param); + return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; } } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h index 901a7d6e779d013968eb098a7bba2ddc7b2b5eaf..49599de2b549d0128a2eb1248d9a30830410ced7 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -68,6 +68,10 @@ private: int32_t OnRemoteDoDestroyTable(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteDoCloudConfig(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteDoClearCloudConfig(MessageParcel& data, MessageParcel& reply); + using RequestHandle = int (RdbServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[RDB_SERVICE_CMD_MAX] = { [RDB_SERVICE_CMD_OBTAIN_TABLE] = &RdbServiceStub::OnRemoteObtainDistributedTableName, @@ -79,7 +83,9 @@ private: [RDB_SERVICE_CMD_UNSUBSCRIBE] = &RdbServiceStub::OnRemoteDoUnSubscribe, [RDB_SERVICE_CMD_REMOTE_QUERY] = &RdbServiceStub::OnRemoteDoRemoteQuery, [RDB_SERVICE_CREATE_RDB_TABLE] = &RdbServiceStub::OnRemoteDoCreateTable, - [RDB_SERVICE_DESTROY_RDB_TABLE] = &RdbServiceStub::OnRemoteDoDestroyTable + [RDB_SERVICE_DESTROY_RDB_TABLE] = &RdbServiceStub::OnRemoteDoDestroyTable, + [RDB_SERVICE_CLOUD_CONFIG] = &RdbServiceStub::OnRemoteDoCloudConfig, + [RDB_SERVICE_CLEAR_CLOUD_CONFIG] = &RdbServiceStub::OnRemoteDoClearCloudConfig }; }; } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 1bd74d455409bfeaf4e6f6517f0b86e0dc0bac87..0e6fc39c620c8004c1a8978e1691bfd2c01ba680 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -428,10 +428,11 @@ int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predica } ZLOGI("delegate sync"); - return delegate->Sync(devices, static_cast(option.mode), - MakeQuery(predicates), [&result] (const auto& syncStatus) { - HandleSyncStatus(syncStatus, result); - }, true); + auto status = delegate->Sync(devices, static_cast(option.mode), + MakeQuery(predicates), [&result] (const auto& syncStatus) { + HandleSyncStatus(syncStatus, result); + }, true); + return status == DistributedDB::DBStatus::OK ? RDB_OK : RDB_ERROR; } int32_t RdbSyncer::DoAsync(const SyncOption &option, const RdbPredicates &predicates, const SyncCallback& callback) @@ -450,12 +451,13 @@ int32_t RdbSyncer::DoAsync(const SyncOption &option, const RdbPredicates &predic } ZLOGI("delegate sync"); - return delegate->Sync(devices, static_cast(option.mode), - MakeQuery(predicates), [callback] (const auto& syncStatus) { - SyncResult result; - HandleSyncStatus(syncStatus, result); - callback(result); - }, false); + auto status = delegate->Sync(devices, static_cast(option.mode), + MakeQuery(predicates), [callback] (const auto& syncStatus) { + SyncResult result; + HandleSyncStatus(syncStatus, result); + callback(result); + }, false); + return status == DistributedDB::DBStatus::OK ? RDB_OK : RDB_ERROR; } int32_t RdbSyncer::RemoteQuery(const std::string& device, const std::string& sql, diff --git a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h index 24651ee6c400afa2f440479074bddd4ab1c22c18..333616f0078473a5a999c403493c95ca652e4965 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -67,6 +67,10 @@ protected: int32_t DoUnSubscribe(const RdbSyncerParam& param) override; + int32_t CloudConfig(const CloudParam& param, std::string &schema) override; + + int32_t ClearCloudConfig(const CloudParam& param) override; + private: uint32_t GetSeqNum(); @@ -88,6 +92,7 @@ private: ObserverMap observers_; sptr notifier_; + sptr remote_; static inline BrokerDelegator delegator_; }; } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/include/rdb_types_util.h b/relational_store/frameworks/native/rdb/include/rdb_types_util.h index 71dcf135fc400e1f983ba4b668c0a2e2d91c7c93..ca589d3784c466806633a1f63c68538c80e460f8 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_types_util.h +++ b/relational_store/frameworks/native/rdb/include/rdb_types_util.h @@ -21,6 +21,7 @@ #include "values_bucket.h" #include "rdb_visibility.h" namespace OHOS::ITypesUtil { +using CloudParam = DistributedRdb::CloudParam; using SyncerParam = DistributedRdb::RdbSyncerParam; using SyncOption = DistributedRdb::SyncOption; using RdbPredicates = DistributedRdb::RdbPredicates; @@ -56,5 +57,9 @@ template<> bool Marshalling(const Asset &input, MessageParcel &data); template<> bool Unmarshalling(Asset &output, MessageParcel &data); +template<> +bool Marshalling(const CloudParam &input, MessageParcel &data); +template<> +bool Unmarshalling(CloudParam &output, MessageParcel &data); } #endif // DISTRIBUTED_RDB_RDB_TYPES_UTIL_H diff --git a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp index 3554b6a95904da628a4cb02eed65a996715b3a4a..a8f00b9d3bf1998036f84a4e1d85a7723f30b19b 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -20,9 +20,35 @@ #include "log_print.h" namespace OHOS::DistributedRdb { +#define IPC_SEND(code, reply, ...) \ +({ \ + int32_t __status = RDB_OK; \ + do { \ + MessageParcel request; \ + if (!request.WriteInterfaceToken(GetDescriptor())) { \ + __status = RDB_ERROR; \ + break; \ + } \ + if (!ITypesUtil::Marshal(request, ##__VA_ARGS__)) { \ + __status = RDB_ERROR; \ + break; \ + } \ + MessageOption option; \ + auto result = remote_->SendRequest((code), request, reply, option); \ + if (result != 0) { \ + __status = RDB_ERROR; \ + break; \ + } \ + \ + ITypesUtil::Unmarshal(reply, __status); \ + } while (0); \ + __status; \ +}) + RdbServiceProxy::RdbServiceProxy(const sptr &object) : IRemoteProxy(object) { + remote_ = Remote(); } void RdbServiceProxy::OnSyncComplete(uint32_t seqNum, const SyncResult &result) @@ -48,20 +74,10 @@ void RdbServiceProxy::OnDataChange(const std::string& storeName, const std::vect std::string RdbServiceProxy::ObtainDistributedTableName(const std::string &device, const std::string &table) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return ""; - } - if (!ITypesUtil::Marshal(data, device, table)) { - ZLOGE("write to message parcel failed"); - return ""; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_OBTAIN_TABLE, data, reply, option) != 0) { - ZLOGE("send request failed"); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_OBTAIN_TABLE, reply, device, table); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, device:%{public}.6s, table:%{public}s", status, device.c_str(), table.c_str()); return ""; } return reply.ReadString(); @@ -93,25 +109,13 @@ int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam& param) int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param, notifier)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_INIT_NOTIFIER, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_INIT_NOTIFIER, reply, param, notifier); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", + status, param.bundleName_.c_str(), param.storeName_.c_str()); } - - int32_t res = RDB_ERROR; - return reply.ReadInt32(res) ? res : RDB_ERROR; + return status; } uint32_t RdbServiceProxy::GetSeqNum() @@ -122,21 +126,12 @@ uint32_t RdbServiceProxy::GetSeqNum() int32_t RdbServiceProxy::DoSync(const RdbSyncerParam& param, const SyncOption &option, const RdbPredicates &predicates, SyncResult& result) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param, option, predicates)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption opt; - if (Remote()->SendRequest(RDB_SERVICE_CMD_SYNC, data, reply, opt) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_SYNC, reply, param, option, predicates); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", + status, param.bundleName_.c_str(), param.storeName_.c_str()); + return status; } if (!ITypesUtil::Unmarshal(reply, result)) { @@ -165,25 +160,13 @@ int32_t RdbServiceProxy::DoSync(const RdbSyncerParam& param, const SyncOption &o int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption &option, const RdbPredicates &predicates) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param, seqNum, option, predicates)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption opt; - if (Remote()->SendRequest(RDB_SERVICE_CMD_ASYNC, data, reply, opt) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_ASYNC, reply, param, seqNum, option, predicates); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, seqNum:%{public}u", + status, param.bundleName_.c_str(), param.storeName_.c_str(), seqNum); } - - int32_t res = RDB_ERROR; - return reply.ReadInt32(res) ? res : RDB_ERROR; + return status; } int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const SyncOption &option, @@ -208,25 +191,13 @@ int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const SyncOption & int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const std::vector &tables) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param, tables)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_SET_DIST_TABLE, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_SET_DIST_TABLE, reply, param, tables); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", + status, param.bundleName_.c_str(), param.storeName_.c_str()); } - - int32_t res = RDB_ERROR; - return reply.ReadInt32(res) ? res : RDB_ERROR; + return status; } int32_t RdbServiceProxy::Sync(const RdbSyncerParam& param, const SyncOption &option, @@ -276,25 +247,13 @@ int32_t RdbServiceProxy::Subscribe(const RdbSyncerParam ¶m, const SubscribeO int32_t RdbServiceProxy::DoSubscribe(const RdbSyncerParam ¶m) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_SUBSCRIBE, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_SUBSCRIBE, reply, param); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", + status, param.bundleName_.c_str(), param.storeName_.c_str()); } - - int32_t res = RDB_ERROR; - return reply.ReadInt32(res) ? res : RDB_ERROR; + return status; } int32_t RdbServiceProxy::UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, @@ -314,50 +273,47 @@ int32_t RdbServiceProxy::UnSubscribe(const RdbSyncerParam ¶m, const Subscrib int32_t RdbServiceProxy::DoUnSubscribe(const RdbSyncerParam ¶m) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; + MessageParcel reply; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_UNSUBSCRIBE, reply, param); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", + status, param.bundleName_.c_str(), param.storeName_.c_str()); } + return status; +} +int32_t RdbServiceProxy::CloudConfig(const CloudParam ¶m, std::string &schema) +{ MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_UNSUBSCRIBE, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_CLOUD_CONFIG, reply, param); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, cloudId:%{public}.6s", + status, param.bundleName.c_str(), param.storeName.c_str(), param.cloudId.c_str()); + return status; } + ITypesUtil::Unmarshal(reply, schema); + return RDB_OK; +} - int32_t res = RDB_ERROR; - return reply.ReadInt32(res) ? res : RDB_ERROR; +int32_t RdbServiceProxy::ClearCloudConfig(const CloudParam ¶m) +{ + MessageParcel reply; + int32_t status = IPC_SEND(RDB_SERVICE_CLEAR_CLOUD_CONFIG, reply, param); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, cloudId:%{public}.6s", + status, param.bundleName.c_str(), param.storeName.c_str(), param.cloudId.c_str()); + } + return status; } int32_t RdbServiceProxy::RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param, device, sql, selectionArgs)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_REMOTE_QUERY, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; - } - - int32_t status = reply.ReadInt32(); - if (status != RdbStatus::RDB_OK) { - ZLOGE("remote query failed, server side status is %{public}d", status); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_REMOTE_QUERY, reply, param, device, sql, selectionArgs); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, device:%{public}.6s", + status, param.bundleName_.c_str(), param.storeName_.c_str(), device.c_str()); return status; } @@ -389,55 +345,25 @@ void RdbServiceProxy::ImportObservers(ObserverMap &observers) int32_t RdbServiceProxy::CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param, writePermission, readPermission)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CREATE_RDB_TABLE, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; - } - - int32_t status = reply.ReadInt32(); - if (status != RdbStatus::RDB_OK) { - ZLOGE("remote query failed, server side status is %{public}d", status); - return status; - } - return RDB_OK; + int32_t status = IPC_SEND(RDB_SERVICE_CREATE_RDB_TABLE, reply, param, writePermission, readPermission); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s," + "writePermission:%{public}.6s, readPermission:%{public}.6s", + status, param.bundleName_.c_str(), param.storeName_.c_str(), + writePermission.c_str(), readPermission.c_str()); + } + return status; } int32_t RdbServiceProxy::DestroyRDBTable(const RdbSyncerParam ¶m) { - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return RDB_ERROR; - } - if (!ITypesUtil::Marshal(data, param)) { - ZLOGE("write to message parcel failed"); - return RDB_ERROR; - } - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_DESTROY_RDB_TABLE, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; + int32_t status = IPC_SEND(RDB_SERVICE_DESTROY_RDB_TABLE, reply, param); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", + status, param.bundleName_.c_str(), param.storeName_.c_str()); } - - int32_t status = reply.ReadInt32(); - if (status != RdbStatus::RDB_OK) { - ZLOGE("remote query failed, server side status is %{public}d", status); - return status; - } - return RDB_OK; + return status; } } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp index 7a2fde1c870fd84e859f1bb85275abcf728ed8ab..d7ed7c9e826c184116f2038a8831780d37e54d41 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -98,4 +98,14 @@ bool ITypesUtil::Unmarshalling(Asset &output, MessageParcel &data) { return ITypesUtil::Unmarshal(data, output.version, output.name, output.size, output.modifyTime, output.uri); } +template<> +bool Marshalling(const CloudParam &input, MessageParcel &data) +{ + return ITypesUtil::Marshal(data, input.bundleName, input.storeName, input.cloudId, input.schemaVerion); +} +template<> +bool Unmarshalling(CloudParam &output, MessageParcel &data) +{ + return ITypesUtil::Unmarshal(data, output.bundleName, output.storeName, output.cloudId, output.schemaVerion); +} } \ No newline at end of file diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h index a72ebf72e7d766f6ecb224db65b8e7298c242dad..bffb1ac35e83970eeee4049f0a753a16241dbdbc 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -37,6 +37,8 @@ public: RDB_SERVICE_CMD_REMOTE_QUERY, RDB_SERVICE_CREATE_RDB_TABLE, RDB_SERVICE_DESTROY_RDB_TABLE, + RDB_SERVICE_CLOUD_CONFIG, + RDB_SERVICE_CLEAR_CLOUD_CONFIG, RDB_SERVICE_CMD_MAX }; virtual std::string ObtainDistributedTableName(const std::string &device, const std::string &table) = 0; @@ -71,6 +73,10 @@ protected: virtual int32_t DoSubscribe(const RdbSyncerParam ¶m) = 0; virtual int32_t DoUnSubscribe(const RdbSyncerParam ¶m) = 0; + + virtual int32_t CloudConfig(const CloudParam ¶m, std::string &schema) = 0; + + virtual int32_t ClearCloudConfig(const CloudParam ¶m) = 0; }; } } // namespace OHOS::DistributedRdb diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h index 2dfc6044fc7c4cb4b995db5a78329bef8c78662e..14934409301335a1ec2e02de11c629e1288fe0d2 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h @@ -48,6 +48,13 @@ struct RdbSyncerParam { }; }; +struct CloudParam { + std::string bundleName; + std::string storeName; + std::string cloudId; + int32_t schemaVerion; +}; + enum SyncMode { PUSH, PULL,