From 8a0a135673a0b0e8e27087a545810e5a70cfcf4f Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 19 Apr 2023 20:48:33 +0800 Subject: [PATCH 1/3] update Signed-off-by: htt1997 --- .../framework/cloud/cloud_event.cpp | 18 ++- .../framework/cloud/cloud_info.cpp | 7 +- .../framework/include/cloud/cloud_event.h | 13 +- .../framework/include/cloud/cloud_info.h | 1 + .../service/cloud/cloud_service_impl.cpp | 66 +++++--- .../service/cloud/cloud_service_impl.h | 1 + .../data_share/data_share_service_stub.cpp | 50 +++--- .../service/object/object_service_stub.cpp | 53 ++++--- .../service/rdb/rdb_service_impl.cpp | 7 + .../service/rdb/rdb_service_impl.h | 2 + .../service/rdb/rdb_service_stub.cpp | 146 ++++++++++++------ .../service/rdb/rdb_service_stub.h | 5 +- .../js/napi/rdb/src/napi_rdb_store_helper.cpp | 1 + .../native/rdb/include/rdb_service_proxy.h | 1 + .../native/rdb/include/rdb_types_util.h | 5 + .../native/rdb/src/rdb_service_proxy.cpp | 11 ++ .../native/rdb/src/rdb_store_manager.cpp | 1 + .../native/rdb/src/rdb_types_util.cpp | 9 ++ .../inner_api/rdb/include/rdb_service.h | 3 + 19 files changed, 282 insertions(+), 118 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp index 66e58e6d..7ab8cbe7 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,18 +16,28 @@ #include "cloud/cloud_event.h" namespace OHOS::DistributedData { -CloudEvent::CloudEvent(int32_t evtId, int32_t user) - : Event(evtId), user_(user) +CloudEvent::CloudEvent(int32_t evtId, uint32_t tokenId, std::string storeName, std::string bundleName) + : Event(evtId), tokenId_(tokenId), storeName_(storeName), bundleName_(bundleName) { } -int32_t CloudEvent::GetUser() const +//int32_t CloudEvent::GetUser() const +//{ +// return user_; +//} + +std::string CloudEvent::GetBundleName() const { - return user_; + return bundleName_; } bool CloudEvent::Equals(const Event &event) const { return false; } + +uint32_t CloudEvent::GetTokenId() const +{ + return tokenId_; +} } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index 4d022a2f..e8d83c18 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -72,6 +72,11 @@ std::map CloudInfo::GetSchemaKey() const return keys; } +std::string CloudInfo::GetSchemaKey(std::string bundleName) const +{ + return GetKey(SCHEMA_PREFIX, { std::to_string(user), id, bundleName }); +} + bool CloudInfo::IsValid() const { return !id.empty(); @@ -90,7 +95,7 @@ bool CloudInfo::IsExist(const std::string &bundleName) const void CloudInfo::DelApp(const std::string &bundleName) { for (auto it = apps.begin(); it != apps.end();) { - if ((*it).bundleName == bundleName) { + if (it->bundleName == bundleName) { it = apps.erase(it); break; } diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index 2ef233a7..ebb1004b 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -24,16 +24,23 @@ class CloudEvent : public Event { public: enum : int32_t { CLOUD_RDB_FEATURE_INIT = EVT_CLOUD, + CLOUD_RDB_OPEN_STORE, CLOUD_BUTT }; - CloudEvent(int32_t evtId, int32_t user); + CloudEvent(int32_t evtId, uint32_t tokenId, std::string storeName, std::string bundleName); ~CloudEvent() = default; - int32_t GetUser() const; +// int32_t GetUser() const; + std::string GetBundleName() const; + uint32_t GetTokenId() const; bool Equals(const DistributedData::Event &event) const override; private: - int32_t user_; +// int32_t user_; + std::string bundleName_; + uint32_t tokenId_; + std::string storeName_ + }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_EVENT_H 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 baddad95..f87c1d03 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -37,6 +37,7 @@ public: std::string GetKey() const; std::map GetSchemaKey() const; + std::string GetSchemaKey(std::string bundleName) const; bool IsValid() const; bool IsExist(const std::string &bundleName) const; void DelApp(const std::string &bundleName); 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 8de4f2cf..82a266a4 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -19,20 +19,21 @@ #include "account/account_delegate.h" #include "checker/checker_manager.h" -#include "cloud_syncer.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" +#include "cloud_syncer.h" #include "eventcenter/event_center.h" #include "feature/feature_system.h" #include "ipc_skeleton.h" -#include "utils/anonymous.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "utils/anonymous.h" namespace OHOS::CloudData { using namespace DistributedData; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; -CloudServiceImpl::Factory::Factory() { +CloudServiceImpl::Factory::Factory() +{ FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { if (product_ == nullptr) { product_ = std::make_shared(); @@ -48,14 +49,42 @@ CloudServiceImpl::CloudServiceImpl() EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_RDB_FEATURE_INIT, [this](const Event &event) { auto &rdbEvent = static_cast(event); CloudInfo cloudInfo; - cloudInfo.user = rdbEvent.GetUser(); + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); if (GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", rdbEvent.GetUser()); + ZLOGE("failed, user:%{public}d", cloudInfo.user); return; } UpdateCloudInfo(cloudInfo); AddSchema(cloudInfo); }); + + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_RDB_OPEN_STORE, [this](const Event &event) { + auto &rdbEvent = static_cast(event); + CloudInfo cloudInfo; + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); + if (GetServerInfo(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", cloudInfo.user); + return; + } + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + return; + } + SchemaMeta schemaMeta; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()), schemaMeta, + true)) { + schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetBundleName()); + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()), schemaMeta, true); + } + + for (auto &database : schemaMeta.databases) { + if (false) { //TODO:不需要同步 + continue; + } + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetTokenId(), database); + //TODO:同步 + } + }); } int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) @@ -146,7 +175,7 @@ int32_t CloudServiceImpl::Config(const std::string &id, const StoreInfo &storeIn auto tokenId = IPCSkeleton::GetCallingTokenID(); CloudInfo cloudInfo; cloudInfo.id = id; - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId);; + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); if (ConfigCloudInfo(storeInfo, cloudInfo) != SUCCESS) { return ERROR; } @@ -173,8 +202,7 @@ int32_t CloudServiceImpl::ConfigCloudInfo(const StoreInfo &storeInfo, CloudInfo return SUCCESS; } -int32_t CloudServiceImpl::GetStoreSchema( - const CloudInfo &cloudInfo, const StoreInfo &storeInfo, std::string &schema) +int32_t CloudServiceImpl::GetStoreSchema(const CloudInfo &cloudInfo, const StoreInfo &storeInfo, std::string &schema) { if (!cloudInfo.IsExist(storeInfo.bundleName)) { ZLOGE("no exist bundleName:%{public}s", storeInfo.bundleName.c_str()); @@ -221,8 +249,8 @@ int32_t CloudServiceImpl::GetCloudInfo(CloudInfo &cloudInfo) auto tokenId = IPCSkeleton::GetCallingTokenID(); cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); 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); + ZLOGE("invalid argument id:%{public}s, user:%{public}d", Anonymous::Change(cloudInfo.id).c_str(), + cloudInfo.user); return ERROR; } return SUCCESS; @@ -287,8 +315,7 @@ void CloudServiceImpl::AddSchema(CloudInfo &cloudInfo) } } -int32_t CloudServiceImpl::GetAppSchema( - int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta) +int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta) { auto instance = CloudServer::GetInstance(); if (instance == nullptr) { @@ -300,11 +327,14 @@ int32_t CloudServiceImpl::GetAppSchema( void CloudServiceImpl::UpdateSchema(CloudInfo &cloudInfo, const StoreInfo &storeInfo, int32_t version) { - auto keys = cloudInfo.GetSchemaKey(); SchemaMeta schemaMeta; - if (!MetaDataManager::GetInstance().LoadMeta(keys[storeInfo.bundleName], schemaMeta, true)) { - AddSchema(cloudInfo); - return; + auto key = cloudInfo.GetSchemaKey(storeInfo.bundleName); + if (!MetaDataManager::GetInstance().LoadMeta(key, schemaMeta, true)) { + if (GetAppSchema(cloudInfo.user, storeInfo.bundleName, schemaMeta) != SUCCESS) { + ZLOGI("get schema meta fail"); + return; + } + MetaDataManager::GetInstance().SaveMeta(key, schemaMeta, true); } if (version <= schemaMeta.version) { ZLOGI("input ver:%{public}d, meta ver:%{public}d", version, schemaMeta.version); @@ -319,6 +349,6 @@ void CloudServiceImpl::UpdateSchema(CloudInfo &cloudInfo, const StoreInfo &store ZLOGI("input ver:%{public}d, server ver:%{public}d", version, serverMeta.version); return; } - MetaDataManager::GetInstance().SaveMeta(keys[storeInfo.bundleName], serverMeta, true); + MetaDataManager::GetInstance().SaveMeta(key, serverMeta, true); } -} // OHOS::CloudData \ No newline at end of file +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index afb98b0c..0df774ea 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -58,6 +58,7 @@ private: std::string GetAppId(const std::string &bundleName); bool CheckAccess(const std::string &bundleName); std::mutex mutex_; + ConcurrentMap clousInfos_; }; } // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index ba7fe7c7..f5c2fbac 100644 --- a/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -21,6 +21,7 @@ #include "ishared_result_set.h" #include "itypes_util.h" #include "log_print.h" +#include "utils/anonymous.h" namespace OHOS { namespace DataShare { @@ -40,13 +41,14 @@ int32_t DataShareServiceStub::OnRemoteInsert(MessageParcel &data, MessageParcel std::string uri; DataShareValuesBucket bucket; if (!ITypesUtil::Unmarshal(data, uri, bucket.valuesMap)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(), + bucket.valuesMap.size()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Insert(uri, bucket); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnRemoteInsert fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -57,13 +59,14 @@ int32_t DataShareServiceStub::OnRemoteUpdate(MessageParcel &data, MessageParcel DataSharePredicates predicate; DataShareValuesBucket bucket; if (!ITypesUtil::Unmarshal(data, uri, predicate, bucket.valuesMap)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(), + bucket.valuesMap.size()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Update(uri, predicate, bucket); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnRemoteUpdate fail %d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -73,13 +76,13 @@ int32_t DataShareServiceStub::OnRemoteDelete(MessageParcel &data, MessageParcel std::string uri; DataSharePredicates predicate; if (!ITypesUtil::Unmarshal(data, uri, predicate)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s", DistributedData::Anonymous::Change(uri).c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Delete(uri, predicate); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnRemoteDelete fail %d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -90,15 +93,16 @@ int32_t DataShareServiceStub::OnRemoteQuery(MessageParcel &data, MessageParcel & DataSharePredicates predicate; std::vector columns; if (!ITypesUtil::Unmarshal(data, uri, predicate, columns)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s columns size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(), + columns.size()); + return IPC_STUB_INVALID_DATA_ERR; } - int errCode = 0; - auto result = ISharedResultSet::WriteToParcel(Query(uri, predicate, columns, errCode), reply); - reply.WriteInt32(errCode); - if (result == nullptr) { - ZLOGW("!resultSet->Marshalling(reply)"); - return -1; + int status = 0; + auto result = ISharedResultSet::WriteToParcel(Query(uri, predicate, columns, status), reply); + reply.WriteInt32(status); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } diff --git a/datamgr_service/services/distributeddataservice/service/object/object_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/object/object_service_stub.cpp index cf142a29..a7118b31 100644 --- a/datamgr_service/services/distributeddataservice/service/object/object_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/object/object_service_stub.cpp @@ -21,6 +21,7 @@ #include "itypes_util.h" #include "log_print.h" +#include "utils/anonymous.h" namespace OHOS::DistributedObject { using namespace DistributedKv; @@ -32,17 +33,19 @@ int32_t ObjectServiceStub::ObjectStoreSaveOnRemote(MessageParcel &data, MessageP std::map> objectData; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId, objectData, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s objectData size:%{public}zu", + sessionId.c_str(), bundleName.c_str(), DistributedData::Anonymous::Change(deviceId).c_str(), + objectData.size()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = ObjectStoreSave(bundleName, sessionId, deviceId, objectData, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("ObjectStoreSaveOnRemote fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -53,17 +56,17 @@ int32_t ObjectServiceStub::ObjectStoreRevokeSaveOnRemote(MessageParcel &data, Me std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = ObjectStoreRevokeSave(bundleName, sessionId, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("ObjectStoreRevokeSaveOnRemote fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -74,17 +77,17 @@ int32_t ObjectServiceStub::ObjectStoreRetrieveOnRemote(MessageParcel &data, Mess std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = ObjectStoreRetrieve(bundleName, sessionId, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("ObjectStoreRetrieveOnRemote fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -95,17 +98,17 @@ int32_t ObjectServiceStub::OnSubscribeRequest(MessageParcel &data, MessageParcel std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = RegisterDataObserver(bundleName, sessionId, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnSubscribeRequest fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -115,13 +118,13 @@ int32_t ObjectServiceStub::OnUnsubscribeRequest(MessageParcel &data, MessageParc std::string sessionId; std::string bundleName; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = UnregisterDataChangeObserver(bundleName, sessionId); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnSubscribeRequest fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } 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 a0a29d01..29ecbddc 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -159,6 +159,13 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } +int32_t OpenStore(const CloudParam &cloudParam) +{ + auto event = + std::make_unique(CloudEvent::CLOUD_RDB_OPEN_STORE, IPCSkeleton::GetCallingTokenID(), cloudParam.storeName, cloudParam.bundleName); + EventCenter::GetInstance().PostEvent(move(event)); +} + int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam& param, const sptr notifier) { if (!CheckAccess(param)) { 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 0961337a..7203622e 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -43,6 +43,8 @@ public: int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; + int32_t OpenStore(const CloudParam &cloudParam) override; + int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, 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 137a0598..67885150 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -19,6 +19,7 @@ #include #include "log_print.h" #include "itypes_util.h" +#include "utils/anonymous.h" namespace OHOS::DistributedRdb { int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, MessageParcel &reply) @@ -26,12 +27,30 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, std::string device; std::string table; if (!ITypesUtil::Unmarshal(data, device, table)) { - ZLOGE("read from message parcel failed"); - reply.WriteString(""); - return RDB_OK; + ZLOGE("Unmarshal device:%{public}s table:%{public}s", device.c_str(), table.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } - reply.WriteString(ObtainDistributedTableName(device, table)); + std::string distributedTableName = ObtainDistributedTableName(device, table); + if (!ITypesUtil::Marshal(reply, distributedTableName)) { + ZLOGE("Marshal distributedTableName:%{public}s", distributedTableName.c_str()); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; +} + +int32_t RdbServiceStub::OnOpenStore(MessageParcel &data, MessageParcel &reply) { + CloudParam cloudParam; + if (!ITypesUtil::Unmarshal(data, cloudParam)) { + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", cloudParam.bundleName.c_str(), + cloudParam.storeName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = OpenStore(cloudParam); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } return RDB_OK; } @@ -39,14 +58,17 @@ int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel { RdbSyncerParam param; sptr notifier; - if (!ITypesUtil::Unmarshal(data, param, notifier)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) { + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } - auto status = InitNotifier(param, notifier); - return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply) @@ -54,13 +76,17 @@ int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, Messag RdbSyncerParam param; std::vector tables; if (!ITypesUtil::Unmarshal(data, param, tables)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables size:%{public}zu", + param.bundleName_.c_str(), param.storeName_.c_str(), tables.size()); + return IPC_STUB_INVALID_DATA_ERR; } auto status = SetDistributedTables(param, tables); - return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply) @@ -69,14 +95,18 @@ int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply SyncOption option {}; RdbPredicates predicates; if (!ITypesUtil::Unmarshal(data, param, option, predicates)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str(), predicates.table_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } SyncResult result; auto status = DoSync(param, option, predicates, result); - return ITypesUtil::Marshal(reply, status, result) ? RDB_OK : RDB_ERROR; + if (!ITypesUtil::Marshal(reply, status, result)) { + ZLOGE("Marshal status:0x%{public}x result size:%{public}zu", status, result.size()); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply) @@ -86,24 +116,33 @@ int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &repl SyncOption option {}; RdbPredicates predicates; if (!ITypesUtil::Unmarshal(data, param, seqNum, option, predicates)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s seqNum:%{public}u tables:%{public}s", + param.bundleName_.c_str(), param.storeName_.c_str(), seqNum, predicates.table_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } auto status = DoAsync(param, seqNum, option, predicates); - return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; if (!ITypesUtil::Unmarshal(data, param)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + + auto status = DoSubscribe(param); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(DoSubscribe(param)); return RDB_OK; } @@ -111,11 +150,16 @@ int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel { RdbSyncerParam param; if (!ITypesUtil::Unmarshal(data, param)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + + auto status = DoUnSubscribe(param); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(DoUnSubscribe(param)); return RDB_OK; } @@ -126,14 +170,20 @@ int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel std::string sql; std::vector selectionArgs; if (!ITypesUtil::Unmarshal(data, param, device, sql, selectionArgs)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s device:%{public}s sql:%{public}s " + "selectionArgs size:%{public}zu", param.bundleName_.c_str(), param.storeName_.c_str(), + DistributedData::Anonymous::Change(device).c_str(), + DistributedData::Anonymous::Change(sql).c_str(), selectionArgs.size()); + return IPC_STUB_INVALID_DATA_ERR; } sptr resultSet; - int32_t status = RemoteQuery(param, device, sql, selectionArgs, resultSet); - return ITypesUtil::Marshal(reply, status, resultSet) ? RDB_OK : RDB_ERROR; + auto status = RemoteQuery(param, device, sql, selectionArgs, resultSet); + if (!ITypesUtil::Marshal(reply, status, resultSet)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data) @@ -165,25 +215,35 @@ int32_t RdbServiceStub::OnRemoteDoCreateTable(MessageParcel &data, MessageParcel std::string writePermission; std::string readPermission; if (!ITypesUtil::Unmarshal(data, param, writePermission, readPermission)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s writePermission:%{public}s " + "readPermission:%{public}s", param.bundleName_.c_str(), param.storeName_.c_str(), + DistributedData::Anonymous::Change(writePermission).c_str(), + DistributedData::Anonymous::Change(readPermission).c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = CreateRDBTable(param, writePermission, readPermission); - return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } int32_t RdbServiceStub::OnRemoteDoDestroyTable(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; if (!ITypesUtil::Unmarshal(data, param)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = DestroyRDBTable(param); - return ITypesUtil::Marshal(reply, status) ? RDB_OK : RDB_ERROR; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; } } // 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 901a7d6e..0758ad9c 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -50,6 +50,8 @@ private: int32_t OnRemoteObtainDistributedTableName(MessageParcel& data, MessageParcel& reply); + int32_t OnOpenStore(MessageParcel&data, MessageParcel& reply); + int32_t OnRemoteInitNotifier(MessageParcel&data, MessageParcel& reply); int32_t OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply); @@ -79,7 +81,8 @@ 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_CMD_OPEN_STORE] = &RdbServiceStub::OnOpenStore }; }; } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp index 7f1742ae..ed233346 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp @@ -451,6 +451,7 @@ napi_value InnerGetRdbStore(napi_env env, napi_callback_info info, std::shared_p int errCode = OK; DefaultOpenCallback callback; context->proxy = RdbHelper::GetRdbStore(context->config, context->version, callback, errCode); + std::shared_ptr dbInvalidError = std::make_shared(); RDB_CHECK_RETURN_CALL_RESULT(errCode == E_OK && context->proxy != nullptr, context->SetError(dbInvalidError)); return (errCode == E_OK) ? OK : ERR; 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 b7166e23..50a20253 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -33,6 +33,7 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; + int32_t OpenStore(const CloudParam &cloudParam) override; int32_t InitNotifier(const RdbSyncerParam& param); int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; 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 71dcf135..ca4465ae 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_types_util.h +++ b/relational_store/frameworks/native/rdb/include/rdb_types_util.h @@ -22,6 +22,7 @@ #include "rdb_visibility.h" namespace OHOS::ITypesUtil { using SyncerParam = DistributedRdb::RdbSyncerParam; +using CloudParam = DistributedRdb::CloudParam; using SyncOption = DistributedRdb::SyncOption; using RdbPredicates = DistributedRdb::RdbPredicates; using RdbOperation = DistributedRdb::RdbPredicateOperation; @@ -33,6 +34,10 @@ RDB_API_EXPORT bool Marshalling(const SyncerParam &input, MessageParcel &data); template<> RDB_API_EXPORT bool Unmarshalling(SyncerParam &output, MessageParcel &data); template<> +RDB_API_EXPORT bool Marshalling(const CloudParam &input, MessageParcel &data); +template<> +RDB_API_EXPORT bool Unmarshalling(CloudParam &output, MessageParcel &data); +template<> RDB_API_EXPORT bool Marshalling(const SyncOption &input, MessageParcel &data); template<> RDB_API_EXPORT bool Unmarshalling(SyncOption &output, MessageParcel &data); 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 760709ad..1b0cb411 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -342,4 +342,15 @@ int32_t RdbServiceProxy::DestroyRDBTable(const RdbSyncerParam ¶m) } return status; } + +int32_t RdbServiceProxy::OpenStore(const CloudParam &cloudParam) +{ + MessageParcel reply; + int32_t status = IPC_SEND(RDB_SERVICE_CMD_OPEN_STORE, reply, cloudParam); + if (status != RDB_OK) { + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, cloudParam.bundleName.c_str(), + cloudParam.storeName.c_str()); + } + return status; +} } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp index a5e8bb47..c8e2e8e5 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp @@ -24,6 +24,7 @@ #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) #include "rdb_security_manager.h" #include "security_policy.h" +#include #endif namespace OHOS { 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 a45c56c2..bc423e6f 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -28,6 +28,15 @@ bool Unmarshalling(SyncerParam &output, MessageParcel &data) output.level_, output.type_, output.isAutoSync_, output.isEncrypt_, output.password_); } +template<> bool Marshalling(const CloudParam &input, MessageParcel &data) +{ + return ITypesUtil::Marshal(data, input.bundleName, input.storeName); +} +template<> bool Unmarshalling(CloudParam &output, MessageParcel &data) +{ + return ITypesUtil::Unmarshal(data, output.bundleName, output.storeName); +} + template<> bool Marshalling(const SyncOption &input, MessageParcel &data) { 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 a72ebf72..fe89a896 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -37,10 +37,13 @@ public: RDB_SERVICE_CMD_REMOTE_QUERY, RDB_SERVICE_CREATE_RDB_TABLE, RDB_SERVICE_DESTROY_RDB_TABLE, + RDB_SERVICE_CMD_OPEN_STORE, RDB_SERVICE_CMD_MAX }; virtual std::string ObtainDistributedTableName(const std::string &device, const std::string &table) = 0; + virtual int32_t OpenStore(const CloudParam &cloudParam) = 0; + virtual int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) = 0; virtual int32_t Sync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, -- Gitee From 8106315e4043431189a134f0ed71e07245c38973 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 21 Apr 2023 10:01:30 +0800 Subject: [PATCH 2/3] update Signed-off-by: htt1997 --- .../native/consumer/src/datashare_helper.cpp | 29 +++++++++-- .../src/mediadatashare_unit_test.cpp | 42 ++++++++++++++-- .../framework/cloud/cloud_event.cpp | 25 +++++++++- .../framework/include/cloud/cloud_event.h | 10 +++- .../service/cloud/cloud_service_impl.cpp | 12 +++-- .../service/rdb/rdb_service_impl.cpp | 48 ++++++++++++------- .../service/rdb/rdb_service_impl.h | 4 +- .../innerkitsimpl/kvdb/src/dev_manager.cpp | 6 +-- .../distributedkvstore/src/js_util.cpp | 2 +- .../native/rdb/include/rdb_manager_impl.h | 2 +- .../native/rdb/include/rdb_service_proxy.h | 4 +- .../frameworks/native/rdb/src/rdb_manager.cpp | 2 +- .../native/rdb/src/rdb_manager_impl.cpp | 8 ++-- .../native/rdb/src/rdb_service_proxy.cpp | 11 ++--- .../native/rdb/src/rdb_store_config.cpp | 21 ++++++++ .../native/rdb/src/rdb_store_impl.cpp | 17 +++++++ .../native/rdb/src/rdb_store_manager.cpp | 2 +- .../inner_api/rdb/include/rdb_service.h | 8 ++-- .../inner_api/rdb/include/rdb_store_config.h | 26 ++++++++++ 19 files changed, 222 insertions(+), 57 deletions(-) diff --git a/data_share/frameworks/native/consumer/src/datashare_helper.cpp b/data_share/frameworks/native/consumer/src/datashare_helper.cpp index 312e4e9e..d606dfeb 100644 --- a/data_share/frameworks/native/consumer/src/datashare_helper.cpp +++ b/data_share/frameworks/native/consumer/src/datashare_helper.cpp @@ -21,6 +21,7 @@ #include "dataobs_mgr_client.h" #include "datashare_log.h" #include "datashare_result_set.h" +#include "concurrent_map.h" namespace OHOS { namespace DataShare { @@ -31,16 +32,20 @@ constexpr int INVALID_VALUE = -1; } // namespace class ObserverImpl : public AAFwk::DataAbilityObserverStub { public: + ObserverImpl() : dataShareObserver_(nullptr){}; explicit ObserverImpl(const std::shared_ptr dataShareObserver) : dataShareObserver_(dataShareObserver){}; void OnChange(); void OnChangeExt(const ChangeInfo &info); static DataShareObserver::ChangeInfo ConvertInfo(const AAFwk::ChangeInfo &info); static AAFwk::ChangeInfo ConvertInfo(const DataShareObserver::ChangeInfo &info); + static sptr GetInstance(const std::shared_ptr dataShareObserver, bool remove = false); private: std::shared_ptr dataShareObserver_; + static ConcurrentMap> obs_; }; +ConcurrentMap> ObserverImpl::obs_; DataShareHelper::DataShareHelper(const sptr &token, const Uri &uri, std::shared_ptr dataShareConnection) @@ -536,9 +541,10 @@ void DataShareHelper::RegisterObserverExt(const Uri &uri, std::shared_ptr obs(new (std::nothrow) ObserverImpl(dataObserver)); + auto obs = ObserverImpl::GetInstance(dataObserver); +// sptr obs(new (std::nothrow) ObserverImpl(dataObserver)); if (obs == nullptr) { - LOG_ERROR("new ObserverImpl failed"); + LOG_ERROR("get ObserverImpl failed"); return; } ErrCode ret = obsMgrClient->RegisterObserverExt(uri, obs, isDescendants); @@ -566,9 +572,9 @@ void DataShareHelper::UnregisterObserverExt(const Uri &uri, std::shared_ptr obs(new (std::nothrow) ObserverImpl(dataObserver)); + auto obs = ObserverImpl::GetInstance(dataObserver, true); if (obs == nullptr) { - LOG_ERROR("new ObserverImpl failed"); + LOG_ERROR("get ObserverImpl failed"); return; } ErrCode ret = obsMgrClient->UnregisterObserverExt(uri, obs); @@ -752,5 +758,20 @@ AAFwk::ChangeInfo ObserverImpl::ConvertInfo(const DataShareObserver::ChangeInfo changeInfo.size_ = info.size_; return changeInfo; } +sptr ObserverImpl::GetInstance(const std::shared_ptr dataShareObserver, bool remove) +{ + sptr result = nullptr; + obs_.Compute(dataShareObserver.get(), [&result, remove, dataShareObserver](const auto &key, auto &value) { + if (value == nullptr) { + sptr obs(new (std::nothrow) ObserverImpl(dataShareObserver)); + value = obs; + } + result = value; + return !remove && result != nullptr; + }); + + return result; +} + } // namespace DataShare } // namespace OHOS \ No newline at end of file diff --git a/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp b/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp index ad447fde..fe4c5966 100644 --- a/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp +++ b/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp @@ -1170,9 +1170,9 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Observer_001, TestSize.Level0) LOG_INFO("MediaDataShare_Observer_001 end"); } -HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ObserverExt_001, TestSize.Level0) +HWTEST_F(MediaDataShareUnitTest, MediaDataShare_RegisterObserverExt_001, TestSize.Level0) { - LOG_INFO("MediaDataShare_ObserverExt_001 start"); + LOG_INFO("MediaDataShare_RegisterObserverExt_001 start"); std::shared_ptr helper = g_mediaDataShareHelper; ASSERT_TRUE(helper != nullptr); Uri uri(MEDIALIBRARY_DATA_URI); @@ -1230,7 +1230,43 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ObserverExt_001, TestSize.Level0 } helper->UnregisterObserverExt(uri, dataObserver); - LOG_INFO("MediaDataShare_ObserverExt_001 end"); + LOG_INFO("MediaDataShare_RegisterObserverExt_001 end"); +} + +HWTEST_F(MediaDataShareUnitTest, MediaDataShare_UnregisterObserverExt_001, TestSize.Level0) +{ + LOG_INFO("MediaDataShare_UnregisterObserverExt_001 start"); + std::shared_ptr helper = g_mediaDataShareHelper; + ASSERT_TRUE(helper != nullptr); + Uri uri(MEDIALIBRARY_DATA_URI); + std::shared_ptr dataObserver = std::make_shared(); + helper->RegisterObserverExt(uri, dataObserver, true); + + DataShare::DataShareValuesBucket valuesBucket; + valuesBucket.Put(MEDIA_DATA_DB_TITLE, "Datashare_Observer_Test001"); + int retVal = helper->Insert(uri, valuesBucket); + EXPECT_EQ((retVal > 0), true); + helper->NotifyChangeExt({ DataShareObserver::ChangeType::INSERT, { uri } }); + + { + unique_lock lock(dataObserver->mutex_); + if (dataObserver->condition_.wait_for(lock, 2s) == std::cv_status::no_timeout) { + EXPECT_TRUE(true); + } else { + EXPECT_TRUE(false); + } + } + helper->UnregisterObserverExt(uri, dataObserver); + helper->NotifyChangeExt({ DataShareObserver::ChangeType::DELETE, { uri } }); + { + unique_lock lock(dataObserver->mutex_); + if (dataObserver->condition_.wait_for(lock, 2s) == std::cv_status::no_timeout) { + EXPECT_TRUE(false); + } else { + EXPECT_TRUE(true); + } + } + LOG_INFO("MediaDataShare_UnregisterObserverExt_001 end"); } } // namespace Media } // namespace OHOS \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp index 7ab8cbe7..5dd21727 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,8 +16,14 @@ #include "cloud/cloud_event.h" namespace OHOS::DistributedData { -CloudEvent::CloudEvent(int32_t evtId, uint32_t tokenId, std::string storeName, std::string bundleName) - : Event(evtId), tokenId_(tokenId), storeName_(storeName), bundleName_(bundleName) +CloudEvent::CloudEvent(int32_t evtId, uint32_t tokenId, std::string storeName, std::string bundleName, + std::string schemaKey) + : Event(evtId), tokenId_(tokenId), storeName_(storeName), bundleName_(bundleName), schemaKey_(schemaKey) +{ +} +CloudEvent::CloudEvent(int32_t evtId, const CloudEvent &cloudEvent) + : Event(evtId), tokenId_(cloudEvent.GetTokenId()), storeName_(cloudEvent.GetStoreName()), + bundleName_(cloudEvent.GetBundleName()) { } @@ -26,11 +32,26 @@ CloudEvent::CloudEvent(int32_t evtId, uint32_t tokenId, std::string storeName, s // return user_; //} +void CloudEvent::SetSchemaKey(std::string schemaKey) +{ + schemaKey_ = schemaKey; +} + +std::string CloudEvent::GetSchemaKey() const +{ + return schemaKey_; +} + std::string CloudEvent::GetBundleName() const { return bundleName_; } +std::string CloudEvent::GetStoreName() const +{ + return storeName_; +} + bool CloudEvent::Equals(const Event &event) const { return false; diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index ebb1004b..d0d4295b 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -25,13 +25,18 @@ public: enum : int32_t { CLOUD_RDB_FEATURE_INIT = EVT_CLOUD, CLOUD_RDB_OPEN_STORE, + CLOUD_RDB_GET_SCHEMA_FINISHED, CLOUD_BUTT }; - CloudEvent(int32_t evtId, uint32_t tokenId, std::string storeName, std::string bundleName); + CloudEvent(int32_t evtId, uint32_t tokenId = 0, std::string storeName = "", std::string bundleName = "", std::string schemaKey = ""); + CloudEvent(int32_t evtId, const CloudEvent &cloudEvent); ~CloudEvent() = default; // int32_t GetUser() const; + void SetSchemaKey(std::string bundleName); + std::string GetSchemaKey() const; std::string GetBundleName() const; + std::string GetStoreName() const; uint32_t GetTokenId() const; bool Equals(const DistributedData::Event &event) const override; @@ -39,7 +44,8 @@ private: // int32_t user_; std::string bundleName_; uint32_t tokenId_; - std::string storeName_ + std::string storeName_; + std::string schemaKey_; }; } // namespace OHOS::DistributedData 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 82a266a4..740a1ba7 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -71,19 +71,23 @@ CloudServiceImpl::CloudServiceImpl() return; } SchemaMeta schemaMeta; - if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()), schemaMeta, - true)) { + std::string schemaKey = cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()); + if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetBundleName()); - MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()), schemaMeta, true); + MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); } for (auto &database : schemaMeta.databases) { - if (false) { //TODO:不需要同步 + if (database.name != rdbEvent.GetStoreName() /* ||TODO:不需要同步*/) { continue; } auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetTokenId(), database); //TODO:同步 } + + auto finishedEvent = std::make_unique(CloudEvent::CLOUD_RDB_GET_SCHEMA_FINISHED, rdbEvent); + finishedEvent->SetSchemaKey(schemaKey); + EventCenter::GetInstance().PostEvent(move(finishedEvent)); }); } 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 29ecbddc..839da7c0 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -79,6 +79,11 @@ RdbServiceImpl::RdbServiceImpl() [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); + + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_RDB_GET_SCHEMA_FINISHED, [this](const Event &event) { + auto &cloudEvent = static_cast(event); + MetaDataManager::GetInstance().LoadMeta() + }); } int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) @@ -138,14 +143,14 @@ void RdbServiceImpl::OnClientDied(pid_t pid) }); } -bool RdbServiceImpl::CheckAccess(const RdbSyncerParam ¶m) +bool RdbServiceImpl::CheckAccess(const std::string& bundleName) { auto tokenId = IPCSkeleton::GetCallingTokenID(); - auto instanceId = RdbSyncer::GetInstIndex(tokenId, param.bundleName_); + auto instanceId = RdbSyncer::GetInstIndex(tokenId, bundleName); if (instanceId != 0) { return false; } - return !GetAppId(param.bundleName_).empty(); + return !GetAppId(bundleName).empty(); } std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device, const std::string &table) @@ -159,16 +164,9 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -int32_t OpenStore(const CloudParam &cloudParam) -{ - auto event = - std::make_unique(CloudEvent::CLOUD_RDB_OPEN_STORE, IPCSkeleton::GetCallingTokenID(), cloudParam.storeName, cloudParam.bundleName); - EventCenter::GetInstance().PostEvent(move(event)); -} - -int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam& param, const sptr notifier) +int32_t RdbServiceImpl::InitNotifier(const std::string& bundleName, const sptr notifier) { - if (!CheckAccess(param)) { + if (!CheckAccess(bundleName)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -286,7 +284,7 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) { ZLOGI("enter"); - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -300,7 +298,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const int32_t RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -323,7 +321,7 @@ void RdbServiceImpl::OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResul int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, const SyncOption &option, const RdbPredicates &predicates) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -384,7 +382,7 @@ int32_t RdbServiceImpl::DoUnSubscribe(const RdbSyncerParam& param) int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -408,7 +406,7 @@ std::string RdbServiceImpl::GetAppId(const std::string &bundleName) int32_t RdbServiceImpl::CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -436,7 +434,7 @@ int32_t RdbServiceImpl::CreateRDBTable( int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -469,4 +467,18 @@ int32_t RdbServiceImpl::OnInitialize() EventCenter::GetInstance().PostEvent(std::move(initEvt)); return RDB_OK; } + +int32_t RdbServiceImpl::OpenStore(const CloudParam &cloudParam) +{ + if (!CheckAccess(cloudParam.bundleName)) { + ZLOGE("permission error"); + return RDB_ERROR; + } + auto event = std::make_unique(CloudEvent::CLOUD_RDB_OPEN_STORE, IPCSkeleton::GetCallingTokenID(), + cloudParam.storeName, cloudParam.bundleName); + EventCenter::GetInstance().PostEvent(move(event)); + + return 0; +} + } // namespace OHOS::DistributedRdb 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 7203622e..8d943031 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -41,7 +41,7 @@ public: /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; + int32_t InitNotifier(const std::string& bundleName, const sptr notifier) override; int32_t OpenStore(const CloudParam &cloudParam) override; @@ -74,7 +74,7 @@ protected: private: std::string GenIdentifier(const RdbSyncerParam& param); - bool CheckAccess(const RdbSyncerParam& param); + bool CheckAccess(const std::string& bundleName); void SyncerTimeout(std::shared_ptr syncer); diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp index 76199f4b..93309353 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp @@ -77,7 +77,7 @@ void DmDeathCallback::OnRemoteDied() DevManager::DevManager(const std::string &pkgName) : PKG_NAME(pkgName + PKG_NAME_EX) { - RegisterDevCallback(); +// RegisterDevCallback(); } int32_t DevManager::Init() @@ -215,7 +215,7 @@ void DevManager::Offline(const std::string &networkId) deviceInfos_.Delete(deviceInfo.uuid); } ZLOGI("%{public}s observers:%{public}zu", StoreUtil::Anonymous(networkId).c_str(), observers_.Size()); - observers_.ForEach([&networkId](const auto &key, auto &value) { + observers_.ForEachCopies([&networkId](const auto &key, auto &value) { value->Offline(networkId); return false; }); @@ -230,7 +230,7 @@ void DevManager::OnChanged(const std::string &networkId) void DevManager::OnReady(const std::string &networkId) { ZLOGI("%{public}s observers:%{public}zu", StoreUtil::Anonymous(networkId).c_str(), observers_.Size()); - observers_.ForEach([&networkId](const auto &key, auto &value) { + observers_.ForEachCopies([&networkId](const auto &key, auto &value) { value->Online(networkId); return false; }); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp index d293e93b..4ce4e13c 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp @@ -310,7 +310,7 @@ JSUtil::StatusMsg JSUtil::SetValue(napi_env env, const JSUtil::KvStoreVariant& i return SetValue(env, *dblValue, out); } - ZLOGE("napi_value <- KvStoreVariant INVALID value type"); + ZLOGE("napi_value <- KvStoreVariant INVALID value type"); return napi_invalid_arg; } diff --git a/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h index 13e5ea0b..197f439d 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h @@ -39,7 +39,7 @@ public: static RdbManagerImpl &GetInstance(); - int GetRdbService(const RdbSyncerParam& param, std::shared_ptr &service); + int GetRdbService(const std::string& bundleName, std::shared_ptr &service); void OnRemoteDied(); 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 50a20253..8fda347e 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -34,8 +34,8 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; int32_t OpenStore(const CloudParam &cloudParam) override; - int32_t InitNotifier(const RdbSyncerParam& param); - int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; + int32_t InitNotifier(const std::string& bundleName); + int32_t InitNotifier(const std::string& bundleName, const sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; diff --git a/relational_store/frameworks/native/rdb/src/rdb_manager.cpp b/relational_store/frameworks/native/rdb/src/rdb_manager.cpp index 261e420f..a9b52b27 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_manager.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_manager.cpp @@ -19,6 +19,6 @@ namespace OHOS::DistributedRdb { int RdbManager::GetRdbService(const RdbSyncerParam& param, std::shared_ptr &service) { - return RdbManagerImpl::GetInstance().GetRdbService(param, service); + return RdbManagerImpl::GetInstance().GetRdbService(param.bundleName_, service); } } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp index 208c8c21..cfc07cc5 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp @@ -80,7 +80,7 @@ RdbManagerImpl& RdbManagerImpl::GetInstance() return manager; } -int RdbManagerImpl::GetRdbService(const RdbSyncerParam& param, std::shared_ptr &service) +int RdbManagerImpl::GetRdbService(const std::string& bundleName, std::shared_ptr &service) { std::lock_guard lock(mutex_); if (rdbService_ != nullptr) { @@ -101,7 +101,7 @@ int RdbManagerImpl::GetRdbService(const RdbSyncerParam& param, std::shared_ptr serviceProxy = iface_cast(remote); - if (serviceProxy->InitNotifier(param) != RDB_OK) { + if (serviceProxy->InitNotifier(bundleName) != RDB_OK) { ZLOGE("init notifier failed"); return E_ERROR; } @@ -111,7 +111,7 @@ int RdbManagerImpl::GetRdbService(const RdbSyncerParam& param, std::shared_ptr service = nullptr; - int errCode = GetRdbService(param, service); + int errCode = GetRdbService(bundleName_, service); if (errCode != E_OK) { return; } 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 1b0cb411..5c0b93dc 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -83,7 +83,7 @@ std::string RdbServiceProxy::ObtainDistributedTableName(const std::string &devic return reply.ReadString(); } -int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam& param) +int32_t RdbServiceProxy::InitNotifier(const std::string& bundleName) { notifier_ = new (std::nothrow) RdbNotifierStub( [this] (uint32_t seqNum, const SyncResult& result) { @@ -98,7 +98,7 @@ int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam& param) return RDB_ERROR; } - if (InitNotifier(param, notifier_->AsObject().GetRefPtr()) != RDB_OK) { + if (InitNotifier(bundleName, notifier_->AsObject().GetRefPtr()) != RDB_OK) { notifier_ = nullptr; return RDB_ERROR; } @@ -107,13 +107,12 @@ int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam& param) return RDB_OK; } -int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) +int32_t RdbServiceProxy::InitNotifier(const std::string& bundleName, const sptr notifier) { MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_INIT_NOTIFIER, reply, param, notifier); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_INIT_NOTIFIER, reply, bundleName, notifier); if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", - status, param.bundleName_.c_str(), param.storeName_.c_str()); + ZLOGE("status:%{public}d, bundleName:%{public}s", status, bundleName.c_str()); } return status; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp index 23b61174..ad764efd 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp @@ -391,4 +391,25 @@ void RdbStoreConfig::ClearEncryptKey() { encryptKey_.assign(encryptKey_.size(), 0); } + +void RdbStoreConfig::SetVersion(int32_t version) +{ + schemaVerion_ = version; + +} + +int32_t RdbStoreConfig::GetVersion() const +{ + return schemaVerion_; +} + +void RdbStoreConfig::SetCloudId(std::string cloudId) +{ + cloudId_ = cloudId; +} + +std::string RdbStoreConfig::GetCloudId() const +{ + return cloudId_; +} } // namespace OHOS::NativeRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 68d48dc0..483be8ce 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -89,6 +89,7 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) syncerParam_.isEncrypt_ = config.IsEncrypt(); syncerParam_.password_ = {}; // open uri share + if (!config.GetUri().empty()) { std::shared_ptr service = nullptr; errCode = DistributedRdb::RdbManager::GetRdbService(syncerParam_, service); @@ -103,6 +104,22 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) } isShared_ = true; } + + if(config.GetVersion() >= 0){ + std::shared_ptr service = nullptr; + errCode = DistributedRdb::RdbManager::GetRdbService(syncerParam_, service); + if (errCode != E_OK) { + LOG_ERROR("RdbStoreImpl::InnerOpen get service failed, err is %{public}d.", errCode); + return E_OK; + } + DistributedRdb::CloudParam cloudParam = { config.GetBundleName(), config.GetName(), config.GetCloudId(), + config.GetVersion() }; + errCode = service->OpenStore(cloudParam); + if (errCode != E_OK) { + LOG_ERROR("RdbStoreImpl::InnerOpen OpenStore failed, err is %{public}d.", errCode); + return E_OK; + } + } #endif return E_OK; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp index c8e2e8e5..e904bed0 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp @@ -24,7 +24,7 @@ #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) #include "rdb_security_manager.h" #include "security_policy.h" -#include +//#include #endif namespace OHOS { 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 fe89a896..2caa3017 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -42,8 +42,6 @@ public: }; virtual std::string ObtainDistributedTableName(const std::string &device, const std::string &table) = 0; - virtual int32_t OpenStore(const CloudParam &cloudParam) = 0; - virtual int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) = 0; virtual int32_t Sync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, @@ -62,7 +60,11 @@ public: virtual int32_t DestroyRDBTable(const RdbSyncerParam ¶m) = 0; - virtual int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) = 0; + virtual int32_t InitNotifier(const std::string& bundleName, const sptr notifier) = 0; + + virtual int32_t OpenStore(const CloudParam &cloudParam) = 0; + + virtual protected: virtual int32_t DoSync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h index 0d7e6709..6bcc799c 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h @@ -426,6 +426,27 @@ public: * @brief Obtains the encrypt key in this {@code StoreConfig} object. */ std::vector GetEncryptKey() const; + + /** + * @brief Sets the version for the object. + */ + void SetVersion(int32_t version); + + /** + * @brief Obtains the version in this {@code StoreConfig} object. + */ + int32_t GetVersion() const; + + /** + * @brief Sets the cloudId for the object. + */ + void SetCloudId(std::string cloudId); + + /** + * @brief Obtains the cloudId in this {@code StoreConfig} object. + */ + std::string GetCloudId() const; + private: void ClearEncryptKey(); @@ -457,6 +478,11 @@ private: int pageSize; int readConSize_ = 4; std::string encryptAlgo; + + //cloud rdb + std::string cloudId_; + int32_t schemaVerion_ = -1; + }; } // namespace OHOS::NativeRdb -- Gitee From 3593cb6bfbb561fb96ad7ac479eb806240cfe53a Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 21 Apr 2023 11:20:05 +0800 Subject: [PATCH 3/3] update Signed-off-by: htt1997 --- .../native/consumer/src/datashare_helper.cpp | 355 +++++++++--------- .../src/mediadatashare_unit_test.cpp | 96 ++--- .../framework/include/cloud/cloud_event.h | 2 +- .../service/cloud/cloud_service_impl.cpp | 7 +- .../service/rdb/rdb_service_impl.cpp | 35 +- .../service/rdb/rdb_service_impl.h | 4 +- .../service/rdb/rdb_service_stub.cpp | 2 +- .../inner_api/rdb/include/rdb_service.h | 4 +- 8 files changed, 229 insertions(+), 276 deletions(-) diff --git a/data_share/frameworks/native/consumer/src/datashare_helper.cpp b/data_share/frameworks/native/consumer/src/datashare_helper.cpp index d606dfeb..b254599c 100644 --- a/data_share/frameworks/native/consumer/src/datashare_helper.cpp +++ b/data_share/frameworks/native/consumer/src/datashare_helper.cpp @@ -1,17 +1,17 @@ /* - * Copyright (c) 2022 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. - */ +* Copyright (c) 2022 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. +*/ #include "datashare_helper.h" @@ -21,7 +21,6 @@ #include "dataobs_mgr_client.h" #include "datashare_log.h" #include "datashare_result_set.h" -#include "concurrent_map.h" namespace OHOS { namespace DataShare { @@ -32,23 +31,19 @@ constexpr int INVALID_VALUE = -1; } // namespace class ObserverImpl : public AAFwk::DataAbilityObserverStub { public: - ObserverImpl() : dataShareObserver_(nullptr){}; explicit ObserverImpl(const std::shared_ptr dataShareObserver) : dataShareObserver_(dataShareObserver){}; void OnChange(); void OnChangeExt(const ChangeInfo &info); static DataShareObserver::ChangeInfo ConvertInfo(const AAFwk::ChangeInfo &info); static AAFwk::ChangeInfo ConvertInfo(const DataShareObserver::ChangeInfo &info); - static sptr GetInstance(const std::shared_ptr dataShareObserver, bool remove = false); private: std::shared_ptr dataShareObserver_; - static ConcurrentMap> obs_; }; -ConcurrentMap> ObserverImpl::obs_; -DataShareHelper::DataShareHelper(const sptr &token, const Uri &uri, - std::shared_ptr dataShareConnection) +DataShareHelper::DataShareHelper( + const sptr &token, const Uri &uri, std::shared_ptr dataShareConnection) { LOG_INFO("DataShareHelper::DataShareHelper start"); token_ = token; @@ -71,15 +66,15 @@ DataShareHelper::~DataShareHelper() } /** - * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship - * between the ability using the Data template (data share for short) and the associated client process in - * a DataShareHelper instance. - * - * @param context Indicates the Context object on OHOS. - * @param strUri Indicates the database table or disk file to operate. - * - * @return Returns the created DataShareHelper instance. - */ +* @brief You can use this method to specify the Uri of the data to operate and set the binding relationship +* between the ability using the Data template (data share for short) and the associated client process in +* a DataShareHelper instance. +* +* @param context Indicates the Context object on OHOS. +* @param strUri Indicates the database table or disk file to operate. +* +* @return Returns the created DataShareHelper instance. +*/ std::shared_ptr DataShareHelper::Creator( const std::shared_ptr &context, const std::string &strUri) { @@ -92,15 +87,15 @@ std::shared_ptr DataShareHelper::Creator( } /** - * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship - * between the ability using the Data template (data share for short) and the associated client process in - * a DataShareHelper instance. - * - * @param token Indicates the System token. - * @param strUri Indicates the database table or disk file to operate. - * - * @return Returns the created DataShareHelper instance. - */ +* @brief You can use this method to specify the Uri of the data to operate and set the binding relationship +* between the ability using the Data template (data share for short) and the associated client process in +* a DataShareHelper instance. +* +* @param token Indicates the System token. +* @param strUri Indicates the database table or disk file to operate. +* +* @return Returns the created DataShareHelper instance. +*/ std::shared_ptr DataShareHelper::Creator(const sptr &token, const std::string &strUri) { if (token == nullptr) { @@ -134,11 +129,11 @@ std::shared_ptr DataShareHelper::Creator(const sptr DataShareHelper::GetFileTypes(Uri &uri, const std::string &mimeTypeFilter) { std::vector matchedMIMEs; @@ -177,16 +172,16 @@ std::vector DataShareHelper::GetFileTypes(Uri &uri, const std::stri } /** - * @brief Opens a file in a specified remote path. - * - * @param uri Indicates the path of the file to open. - * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access - * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, - * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, - * or "rwt" for read and write access that truncates any existing file. - * - * @return Returns the file descriptor. - */ +* @brief Opens a file in a specified remote path. +* +* @param uri Indicates the path of the file to open. +* @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access +* (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, +* "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, +* or "rwt" for read and write access that truncates any existing file. +* +* @return Returns the file descriptor. +*/ int DataShareHelper::OpenFile(Uri &uri, const std::string &mode) { int fd = INVALID_VALUE; @@ -209,17 +204,17 @@ int DataShareHelper::OpenFile(Uri &uri, const std::string &mode) } /** - * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets - * inside of their .hap. - * - * @param uri Indicates the path of the file to open. - * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access - * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, - * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing - * data, or "rwt" for read and write access that truncates any existing file. - * - * @return Returns the RawFileDescriptor object containing file descriptor. - */ +* @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets +* inside of their .hap. +* +* @param uri Indicates the path of the file to open. +* @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access +* (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, +* "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing +* data, or "rwt" for read and write access that truncates any existing file. +* +* @return Returns the RawFileDescriptor object containing file descriptor. +*/ int DataShareHelper::OpenRawFile(Uri &uri, const std::string &mode) { int fd = INVALID_VALUE; @@ -242,13 +237,13 @@ int DataShareHelper::OpenRawFile(Uri &uri, const std::string &mode) } /** - * @brief Inserts a single data record into the database. - * - * @param uri Indicates the path of the data to operate. - * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. - * - * @return Returns the index of the inserted data record. - */ +* @brief Inserts a single data record into the database. +* +* @param uri Indicates the path of the data to operate. +* @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. +* +* @return Returns the index of the inserted data record. +*/ int DataShareHelper::Insert(Uri &uri, const DataShareValuesBucket &value) { int index = INVALID_VALUE; @@ -271,14 +266,14 @@ int DataShareHelper::Insert(Uri &uri, const DataShareValuesBucket &value) } /** - * @brief Updates data records in the database. - * - * @param uri Indicates the path of data to update. - * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. - * @param value Indicates the data to update. This parameter can be null. - * - * @return Returns the number of data records updated. - */ +* @brief Updates data records in the database. +* +* @param uri Indicates the path of data to update. +* @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. +* @param value Indicates the data to update. This parameter can be null. +* +* @return Returns the number of data records updated. +*/ int DataShareHelper::Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) { int index = INVALID_VALUE; @@ -301,13 +296,13 @@ int DataShareHelper::Update(Uri &uri, const DataSharePredicates &predicates, con } /** - * @brief Deletes one or more data records from the database. - * - * @param uri Indicates the path of the data to operate. - * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. - * - * @return Returns the number of data records deleted. - */ +* @brief Deletes one or more data records from the database. +* +* @param uri Indicates the path of the data to operate. +* @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. +* +* @return Returns the number of data records deleted. +*/ int DataShareHelper::Delete(Uri &uri, const DataSharePredicates &predicates) { int index = INVALID_VALUE; @@ -330,14 +325,14 @@ int DataShareHelper::Delete(Uri &uri, const DataSharePredicates &predicates) } /** - * @brief Deletes one or more data records from the database. - * - * @param uri Indicates the path of data to query. - * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. - * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. - * - * @return Returns the query result. - */ +* @brief Deletes one or more data records from the database. +* +* @param uri Indicates the path of data to query. +* @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. +* @param columns Indicates the columns to query. If this parameter is null, all columns are queried. +* +* @return Returns the query result. +*/ std::shared_ptr DataShareHelper::Query(Uri &uri, const DataSharePredicates &predicates, std::vector &columns, DatashareBusinessError *businessError) { @@ -366,13 +361,13 @@ std::shared_ptr DataShareHelper::Query(Uri &uri, const DataS } /** - * @brief Obtains the MIME type matching the data specified by the URI of the data share. This method should be - * implemented by a data share. Data abilities supports general data types, including text, HTML, and JPEG. - * - * @param uri Indicates the URI of the data. - * - * @return Returns the MIME type that matches the data specified by uri. - */ +* @brief Obtains the MIME type matching the data specified by the URI of the data share. This method should be +* implemented by a data share. Data abilities supports general data types, including text, HTML, and JPEG. +* +* @param uri Indicates the URI of the data. +* +* @return Returns the MIME type that matches the data specified by uri. +*/ std::string DataShareHelper::GetType(Uri &uri) { std::string type; @@ -396,13 +391,13 @@ std::string DataShareHelper::GetType(Uri &uri) } /** - * @brief Inserts multiple data records into the database. - * - * @param uri Indicates the path of the data to operate. - * @param values Indicates the data records to insert. - * - * @return Returns the number of data records inserted. - */ +* @brief Inserts multiple data records into the database. +* +* @param uri Indicates the path of the data to operate. +* @param values Indicates the data records to insert. +* +* @return Returns the number of data records inserted. +*/ int DataShareHelper::BatchInsert(Uri &uri, const std::vector &values) { int ret = INVALID_VALUE; @@ -425,11 +420,11 @@ int DataShareHelper::BatchInsert(Uri &uri, const std::vector &dataObserver) { LOG_INFO("Start"); @@ -464,11 +459,11 @@ void DataShareHelper::RegisterObserver(const Uri &uri, const sptr &dataObserver) { LOG_INFO("Start"); @@ -498,10 +493,10 @@ void DataShareHelper::UnregisterObserver(const Uri &uri, const sptr dataObserver, - bool isDescendants) +* Registers an observer to DataObsMgr specified by the given Uri. +* +* @param uri, Indicates the path of the data to operate. +* @param dataObserver, Indicates the DataShareObserver object. +* @param isDescendants, Indicates the Whether to note the change of descendants. +*/ +void DataShareHelper::RegisterObserverExt( + const Uri &uri, std::shared_ptr dataObserver, bool isDescendants) { LOG_INFO("Start"); if (dataObserver == nullptr) { @@ -541,10 +536,9 @@ void DataShareHelper::RegisterObserverExt(const Uri &uri, std::shared_ptr obs(new (std::nothrow) ObserverImpl(dataObserver)); + sptr obs(new (std::nothrow) ObserverImpl(dataObserver)); if (obs == nullptr) { - LOG_ERROR("get ObserverImpl failed"); + LOG_ERROR("new ObserverImpl failed"); return; } ErrCode ret = obsMgrClient->RegisterObserverExt(uri, obs, isDescendants); @@ -555,11 +549,11 @@ void DataShareHelper::RegisterObserverExt(const Uri &uri, std::shared_ptr dataObserver) { LOG_INFO("Start"); @@ -572,9 +566,9 @@ void DataShareHelper::UnregisterObserverExt(const Uri &uri, std::shared_ptr obs(new (std::nothrow) ObserverImpl(dataObserver)); if (obs == nullptr) { - LOG_ERROR("get ObserverImpl failed"); + LOG_ERROR("new ObserverImpl failed"); return; } ErrCode ret = obsMgrClient->UnregisterObserverExt(uri, obs); @@ -585,10 +579,10 @@ void DataShareHelper::UnregisterObserverExt(const Uri &uri, std::shared_ptr ObserverImpl::GetInstance(const std::shared_ptr dataShareObserver, bool remove) -{ - sptr result = nullptr; - obs_.Compute(dataShareObserver.get(), [&result, remove, dataShareObserver](const auto &key, auto &value) { - if (value == nullptr) { - sptr obs(new (std::nothrow) ObserverImpl(dataShareObserver)); - value = obs; - } - result = value; - return !remove && result != nullptr; - }); - - return result; -} - } // namespace DataShare } // namespace OHOS \ No newline at end of file diff --git a/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp b/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp index fe4c5966..7c1569a1 100644 --- a/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp +++ b/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp @@ -1,26 +1,26 @@ /* - * Copyright (C) 2021-2022 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. - */ +* Copyright (C) 2021-2022 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 MLOG_TAG "DataShareUnitTest" #include "mediadatashare_unit_test.h" #include "datashare_helper.h" +#include "datashare_log.h" #include "fetch_result.h" #include "get_self_permissions.h" #include "iservice_registry.h" -#include "datashare_log.h" #include "media_file_utils.h" #include "media_library_manager.h" #include "medialibrary_errno.h" @@ -41,13 +41,13 @@ std::shared_ptr CreateDataShareHelper(int32_t system auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (saManager == nullptr) { LOG_ERROR("CreateDataShareHelper::CreateFileExtHelper Get system ability " - "mgr failed."); + "mgr failed."); return nullptr; } auto remoteObj = saManager->GetSystemAbility(systemAbilityId); while (remoteObj == nullptr) { LOG_ERROR("CreateDataShareHelper::CreateFileExtHelper GetSystemAbility " - "Service Failed."); + "Service Failed."); return nullptr; } return DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI); @@ -202,8 +202,12 @@ void MediaDataShareUnitTest::TearDownTestCase(void) LOG_INFO("TearDownTestCase end"); } -void MediaDataShareUnitTest::SetUp(void) {} -void MediaDataShareUnitTest::TearDown(void) {} +void MediaDataShareUnitTest::SetUp(void) +{ +} +void MediaDataShareUnitTest::TearDown(void) +{ +} HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_001, TestSize.Level0) { @@ -300,7 +304,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_006, TestSize.Le vector columns; Uri uri(MEDIALIBRARY_DATA_URI); auto resultSet = helper->Query(uri, predicates, columns); - int result = 0; + int result = -1; if (resultSet != nullptr) { resultSet->GetRowCount(result); } @@ -555,7 +559,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_021, TestSize.Le LOG_INFO("MediaDataShare_Predicates_Test_021::Start"); DataShare::DataSharePredicates predicates; predicates.EqualTo(MEDIA_DATA_DB_TITLE, "dataShareTest003"); - + std::vector operationItems = predicates.GetOperationList(); DataShare::OperationItem operationItem = operationItems[0]; EXPECT_EQ(operationItem.operation, DataShare::OperationType::EQUAL_TO); @@ -594,7 +598,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_024, TestSize.Le { LOG_INFO("MediaDataShare_Predicates_Test_024::Start"); DataShare::DataSharePredicates predicates; - int res = predicates.SetWhereArgs(std::vector { "-5" }); + int res = predicates.SetWhereArgs(std::vector{ "-5" }); EXPECT_EQ(res, 0); vector args = predicates.GetWhereArgs(); EXPECT_EQ(args[0], "-5"); @@ -887,7 +891,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ResultSet_Test_003, TestSize.Lev auto resultSet = helper->Query(uri, predicates, columns); AppDataFwk::SharedBlock *block = nullptr; ASSERT_TRUE(resultSet != nullptr); - + bool hasBlock = resultSet->HasBlock(); EXPECT_EQ(hasBlock, true); block = resultSet->GetBlock(); @@ -1071,7 +1075,7 @@ HWTEST_F(MediaDataShareUnitTest, Insert_ConnectionNull_Test_001, TestSize.Level0 valuesBucket.Put(MEDIA_DATA_DB_TITLE, "dataShareTest006"); int value4 = 998; valuesBucket.Put(MEDIA_DATA_DB_PARENT_ID, value4); - auto resultInsert= helper->Insert(uri, valuesBucket); + auto resultInsert = helper->Insert(uri, valuesBucket); EXPECT_EQ(resultInsert, -1); auto resultGetType = helper->GetType(uri); @@ -1124,7 +1128,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_NotImplPredicates_Test_001, Test inColumn.push_back("dataShare_Test_001"); inColumn.push_back("dataShare_Test_002"); predicates.In(MEDIA_DATA_DB_TITLE, inColumn); - + vector notInColumn; notInColumn.push_back("dataShare_Test_003"); notInColumn.push_back("dataShare_Test_004"); @@ -1153,7 +1157,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Observer_001, TestSize.Level0) sptr dataObserver; EXPECT_EQ(dataObserver, nullptr); helper->RegisterObserver(uri, dataObserver); - + DataShare::DataShareValuesBucket valuesBucket; valuesBucket.Put(MEDIA_DATA_DB_TITLE, "Datashare_Observer_Test001"); int retVal = helper->Insert(uri, valuesBucket); @@ -1170,9 +1174,9 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Observer_001, TestSize.Level0) LOG_INFO("MediaDataShare_Observer_001 end"); } -HWTEST_F(MediaDataShareUnitTest, MediaDataShare_RegisterObserverExt_001, TestSize.Level0) +HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ObserverExt_001, TestSize.Level0) { - LOG_INFO("MediaDataShare_RegisterObserverExt_001 start"); + LOG_INFO("MediaDataShare_ObserverExt_001 start"); std::shared_ptr helper = g_mediaDataShareHelper; ASSERT_TRUE(helper != nullptr); Uri uri(MEDIALIBRARY_DATA_URI); @@ -1230,43 +1234,7 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_RegisterObserverExt_001, TestSiz } helper->UnregisterObserverExt(uri, dataObserver); - LOG_INFO("MediaDataShare_RegisterObserverExt_001 end"); -} - -HWTEST_F(MediaDataShareUnitTest, MediaDataShare_UnregisterObserverExt_001, TestSize.Level0) -{ - LOG_INFO("MediaDataShare_UnregisterObserverExt_001 start"); - std::shared_ptr helper = g_mediaDataShareHelper; - ASSERT_TRUE(helper != nullptr); - Uri uri(MEDIALIBRARY_DATA_URI); - std::shared_ptr dataObserver = std::make_shared(); - helper->RegisterObserverExt(uri, dataObserver, true); - - DataShare::DataShareValuesBucket valuesBucket; - valuesBucket.Put(MEDIA_DATA_DB_TITLE, "Datashare_Observer_Test001"); - int retVal = helper->Insert(uri, valuesBucket); - EXPECT_EQ((retVal > 0), true); - helper->NotifyChangeExt({ DataShareObserver::ChangeType::INSERT, { uri } }); - - { - unique_lock lock(dataObserver->mutex_); - if (dataObserver->condition_.wait_for(lock, 2s) == std::cv_status::no_timeout) { - EXPECT_TRUE(true); - } else { - EXPECT_TRUE(false); - } - } - helper->UnregisterObserverExt(uri, dataObserver); - helper->NotifyChangeExt({ DataShareObserver::ChangeType::DELETE, { uri } }); - { - unique_lock lock(dataObserver->mutex_); - if (dataObserver->condition_.wait_for(lock, 2s) == std::cv_status::no_timeout) { - EXPECT_TRUE(false); - } else { - EXPECT_TRUE(true); - } - } - LOG_INFO("MediaDataShare_UnregisterObserverExt_001 end"); + LOG_INFO("MediaDataShare_ObserverExt_001 end"); } } // namespace Media } // namespace OHOS \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index d0d4295b..64493501 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -25,7 +25,7 @@ public: enum : int32_t { CLOUD_RDB_FEATURE_INIT = EVT_CLOUD, CLOUD_RDB_OPEN_STORE, - CLOUD_RDB_GET_SCHEMA_FINISHED, + CLOUD_RDB_NEED_CREATE, CLOUD_BUTT }; 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 740a1ba7..35d6ab11 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -75,6 +75,9 @@ CloudServiceImpl::CloudServiceImpl() if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetBundleName()); MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); + auto finishedEvent = std::make_unique(CloudEvent::CLOUD_RDB_NEED_CREATE, rdbEvent); + finishedEvent->SetSchemaKey(schemaKey); + EventCenter::GetInstance().PostEvent(move(finishedEvent)); } for (auto &database : schemaMeta.databases) { @@ -84,10 +87,6 @@ CloudServiceImpl::CloudServiceImpl() auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetTokenId(), database); //TODO:同步 } - - auto finishedEvent = std::make_unique(CloudEvent::CLOUD_RDB_GET_SCHEMA_FINISHED, rdbEvent); - finishedEvent->SetSchemaKey(schemaKey); - EventCenter::GetInstance().PostEvent(move(finishedEvent)); }); } 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 839da7c0..223a7a09 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -29,6 +29,7 @@ #include "rdb_notifier_proxy.h" #include "types_export.h" #include "utils/anonymous.h" +#include "cloud/schema_meta.h" using OHOS::DistributedKv::AccountDelegate; using OHOS::DistributedData::CheckerManager; using OHOS::DistributedData::MetaDataManager; @@ -80,9 +81,11 @@ RdbServiceImpl::RdbServiceImpl() return ResolveAutoLaunch(identifier, param); }); - EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_RDB_GET_SCHEMA_FINISHED, [this](const Event &event) { + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_RDB_NEED_CREATE, [this](const Event &event) { auto &cloudEvent = static_cast(event); - MetaDataManager::GetInstance().LoadMeta() + DistributedData::SchemaMeta schemaMeta; + MetaDataManager::GetInstance().LoadMeta(cloudEvent.GetSchemaKey(),schemaMeta); + //CreateDatabase(schemaMeta)TODO:根据schema创建表和trigger }); } @@ -143,14 +146,18 @@ void RdbServiceImpl::OnClientDied(pid_t pid) }); } -bool RdbServiceImpl::CheckAccess(const std::string& bundleName) +bool RdbServiceImpl::CheckAccess(const std::string& bundleName, const std::string& storeName) { - auto tokenId = IPCSkeleton::GetCallingTokenID(); - auto instanceId = RdbSyncer::GetInstIndex(tokenId, bundleName); + CheckerManager::StoreInfo storeInfo; + storeInfo.uid = IPCSkeleton::GetCallingUid(); + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = bundleName; + storeInfo.storeId = RdbSyncer::RemoveSuffix(storeName); + auto instanceId = RdbSyncer::GetInstIndex(storeInfo.tokenId, storeInfo.bundleName); if (instanceId != 0) { return false; } - return !GetAppId(bundleName).empty(); + return !CheckerManager::GetInstance().GetAppId(storeInfo).empty(); } std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device, const std::string &table) @@ -164,9 +171,9 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -int32_t RdbServiceImpl::InitNotifier(const std::string& bundleName, const sptr notifier) +int32_t RdbServiceImpl::InitNotifier(const std::string& bundleName, const std::string& storeName,const sptr notifier) { - if (!CheckAccess(bundleName)) { + if (!CheckAccess(bundleName, storeName)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -284,7 +291,7 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) { ZLOGI("enter"); - if (!CheckAccess(param.bundleName_)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -298,7 +305,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const int32_t RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) { - if (!CheckAccess(param.bundleName_)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -321,7 +328,7 @@ void RdbServiceImpl::OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResul int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, const SyncOption &option, const RdbPredicates &predicates) { - if (!CheckAccess(param.bundleName_)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -382,7 +389,7 @@ int32_t RdbServiceImpl::DoUnSubscribe(const RdbSyncerParam& param) int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) { - if (!CheckAccess(param.bundleName_)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -406,7 +413,7 @@ std::string RdbServiceImpl::GetAppId(const std::string &bundleName) int32_t RdbServiceImpl::CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) { - if (!CheckAccess(param.bundleName_)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -434,7 +441,7 @@ int32_t RdbServiceImpl::CreateRDBTable( int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) { - if (!CheckAccess(param.bundleName_)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } 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 8d943031..a1b4acc0 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -41,7 +41,7 @@ public: /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const std::string& bundleName, const sptr notifier) override; + int32_t InitNotifier(const std::string& bundleName, const std::string& storeName, const sptr notifier) override; int32_t OpenStore(const CloudParam &cloudParam) override; @@ -74,7 +74,7 @@ protected: private: std::string GenIdentifier(const RdbSyncerParam& param); - bool CheckAccess(const std::string& bundleName); + bool CheckAccess(const std::string& bundleName, const std::string& storeName); void SyncerTimeout(std::shared_ptr syncer); 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 67885150..6438028b 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -63,7 +63,7 @@ int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel param.storeName_.c_str()); return IPC_STUB_INVALID_DATA_ERR; } - auto status = InitNotifier(param, notifier); + auto status = InitNotifier(param.bundleName_, notifier); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; 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 2caa3017..d549cb3a 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -60,12 +60,10 @@ public: virtual int32_t DestroyRDBTable(const RdbSyncerParam ¶m) = 0; - virtual int32_t InitNotifier(const std::string& bundleName, const sptr notifier) = 0; + virtual int32_t InitNotifier(const std::string& bundleName, const std::string& storeName, const sptr notifier) = 0; virtual int32_t OpenStore(const CloudParam &cloudParam) = 0; - virtual - protected: virtual int32_t DoSync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) = 0; -- Gitee