diff --git a/data_share/frameworks/native/consumer/src/datashare_helper.cpp b/data_share/frameworks/native/consumer/src/datashare_helper.cpp index 312e4e9e75623f5f457da82c2bec9077da5e2cb9..b254599c854e331b396f93e9f17312efd5c68a7d 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" @@ -42,8 +42,8 @@ private: std::shared_ptr dataShareObserver_; }; -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; @@ -66,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) { @@ -87,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) { @@ -129,11 +129,11 @@ std::shared_ptr DataShareHelper::Creator(const sptr DataShareHelper::GetFileTypes(Uri &uri, const std::string &mimeTypeFilter) { std::vector matchedMIMEs; @@ -172,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; @@ -204,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; @@ -237,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; @@ -266,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; @@ -296,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; @@ -325,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) { @@ -361,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; @@ -391,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; @@ -420,11 +420,11 @@ int DataShareHelper::BatchInsert(Uri &uri, const std::vector &dataObserver) { LOG_INFO("Start"); @@ -459,11 +459,11 @@ void DataShareHelper::RegisterObserver(const Uri &uri, const sptr &dataObserver) { LOG_INFO("Start"); @@ -493,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) { @@ -549,11 +549,11 @@ void DataShareHelper::RegisterObserverExt(const Uri &uri, std::shared_ptr dataObserver) { LOG_INFO("Start"); @@ -579,10 +579,10 @@ void DataShareHelper::UnregisterObserverExt(const Uri &uri, 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) { @@ -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); diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp index 66e58e6d2f4e7e3e31c6b141c828435ac1dc6acc..5dd21727a524fed3d0f7e9667fc82e4cbd7ff7f7 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,18 +16,49 @@ #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, + 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()) +{ +} + +//int32_t CloudEvent::GetUser() const +//{ +// return user_; +//} + +void CloudEvent::SetSchemaKey(std::string schemaKey) +{ + schemaKey_ = schemaKey; +} -int32_t CloudEvent::GetUser() const +std::string CloudEvent::GetSchemaKey() const { - return user_; + 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; } + +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 4d022a2f65cf10b26ced407a04f13e536315b0c6..e8d83c18a3f71ebf3af7291800e507b2a85f8c32 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 2ef233a75f3f7caead70ee711282e02408314e79..64493501719fb313b3cd97f3b723269abe8cd405 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,29 @@ class CloudEvent : public Event { public: enum : int32_t { CLOUD_RDB_FEATURE_INIT = EVT_CLOUD, + CLOUD_RDB_OPEN_STORE, + CLOUD_RDB_NEED_CREATE, CLOUD_BUTT }; - CloudEvent(int32_t evtId, int32_t user); + 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; +// 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; private: - int32_t user_; +// int32_t user_; + std::string bundleName_; + uint32_t tokenId_; + std::string storeName_; + std::string schemaKey_; + }; } // 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 baddad9574f5ef13db212b176c4f62f889ef1648..f87c1d0374ea0e63bc0a1bcb85205e9d5d302ce8 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 8de4f2cf23863bc705ee14705b45e5d295aa6ed3..35d6ab11cd510d7f007dc8c7f06d0138abea18e0 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,45 @@ 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; + std::string schemaKey = cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()); + 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) { + if (database.name != rdbEvent.GetStoreName() /* ||TODO:不需要同步*/) { + continue; + } + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetTokenId(), database); + //TODO:同步 + } + }); } int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) @@ -146,7 +178,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 +205,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 +252,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 +318,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 +330,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 +352,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 afb98b0c17e7484b5681368a0859ec2b2d4e261e..0df774ea64211fdcd38c6fe9a1c2387de6b1c518 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 ba7fe7c77c0d444e21b87f268f06eedd746e9cbf..f5c2fbacb45a4622ae6bd6ad913f74dbe25640d6 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 cf142a29218a95a7e0520839c1a48d98d141feca..a7118b315cf65a8d94dbd690c844cc81bb6f0ca5 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 430d5e307f7ffd8a86ce9ab2b7cd1c0293efda74..54f6dcba97b8bb800c0e8225db04221528bb2580 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; @@ -79,6 +80,13 @@ RdbServiceImpl::RdbServiceImpl() [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); + + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_RDB_NEED_CREATE, [this](const Event &event) { + auto &cloudEvent = static_cast(event); + DistributedData::SchemaMeta schemaMeta; + MetaDataManager::GetInstance().LoadMeta(cloudEvent.GetSchemaKey(),schemaMeta); + //CreateDatabase(schemaMeta)TODO:根据schema创建表和trigger + }); } int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) @@ -138,13 +146,13 @@ void RdbServiceImpl::OnClientDied(pid_t pid) }); } -bool RdbServiceImpl::CheckAccess(const RdbSyncerParam ¶m) +bool RdbServiceImpl::CheckAccess(const std::string& bundleName, const std::string& storeName) { CheckerManager::StoreInfo storeInfo; storeInfo.uid = IPCSkeleton::GetCallingUid(); storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.bundleName = param.bundleName_; - storeInfo.storeId = RdbSyncer::RemoveSuffix(param.storeName_); + storeInfo.bundleName = bundleName; + storeInfo.storeId = RdbSyncer::RemoveSuffix(storeName); auto instanceId = RdbSyncer::GetInstIndex(storeInfo.tokenId, storeInfo.bundleName); if (instanceId != 0) { return false; @@ -163,9 +171,9 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam& param, const sptr notifier) +int32_t RdbServiceImpl::InitNotifier(const std::string& bundleName, const std::string& storeName,const sptr notifier) { - if (!CheckAccess(param)) { + if (!CheckAccess(bundleName, storeName)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -283,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)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -297,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)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -320,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)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -384,7 +392,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_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -399,7 +407,7 @@ int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::stri int32_t RdbServiceImpl::CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -427,7 +435,7 @@ int32_t RdbServiceImpl::CreateRDBTable( int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -460,4 +468,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 3d2324bd92b5a19179f3ac629381cae03a6aa12b..de9a76dcdb4b6b13c28609aa041cd7feb5d53261 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,9 @@ 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 std::string& storeName, const sptr notifier) override; + + int32_t OpenStore(const CloudParam &cloudParam) override; int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; @@ -72,7 +74,7 @@ protected: private: std::string GenIdentifier(const RdbSyncerParam& param); - bool CheckAccess(const RdbSyncerParam& param); + 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 137a05983ba9fd8a9bbd86b3ba4300e6acaec18b..6438028b3072b6c0a060122a55ed9d27f15a0e45 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; + auto status = InitNotifier(param.bundleName_, notifier); + 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 901a7d6e779d013968eb098a7bba2ddc7b2b5eaf..0758ad9c8c87c4924f5a4c4d9943cb167b471987 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/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp index 816599452d640acaf89afececda98c9261cb695f..2644cc25fa7fa489ea19195d0408f943379e8704 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp @@ -78,7 +78,7 @@ void DmDeathCallback::OnRemoteDied() DevManager::DevManager(const std::string &pkgName) : PKG_NAME(pkgName + PKG_NAME_EX) { - RegisterDevCallback(); +// RegisterDevCallback(); } int32_t DevManager::Init() @@ -222,7 +222,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; }); @@ -237,7 +237,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 d293e93b60eed3bd25b6c50e68dfab58707a4e48..4ce4e13c00c50b45aba268615d8b3ea0ea7bc96b 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/js/napi/rdb/src/napi_rdb_store_helper.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp index 7f1742ae06a04afccb6462d0c2eaca70319826ed..ed233346dfd598813722f45c5289474ac2e926e8 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_manager_impl.h b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h index 13e5ea0be3880aafbdf7a64f9953d32f8d166043..197f439d5cd83d9dbcde5febcc1e214bace0b0ac 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 b7166e2385b161aa07f7e0d3e557920d775ff01b..8fda347edd5ebf33d6a9d29e65afc3a633f3566c 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -33,8 +33,9 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const RdbSyncerParam& param); - int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; + int32_t OpenStore(const CloudParam &cloudParam) 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/include/rdb_types_util.h b/relational_store/frameworks/native/rdb/include/rdb_types_util.h index 71dcf135fc400e1f983ba4b668c0a2e2d91c7c93..ca4465ae55441990d7ac586aa1cd28d85bd5dbfb 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_manager.cpp b/relational_store/frameworks/native/rdb/src/rdb_manager.cpp index 261e420f6c9357bdeddff25a7c297708a63a266a..a9b52b27157fc868ea82b3b2031d589d55bae900 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 208c8c2170fefef72145562f25cb59ddd6943db9..cfc07cc5221146b75cd09934a6af0e776e1febdc 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 760709adb35a521ff13a72a85016beb3cd449d91..5c0b93dcd35d68fd61fb6adb7c87704e4b4380ff 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; } @@ -342,4 +341,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_config.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp index 23b6117484f055c9586b7b2e1cf4748f0662c2db..ad764efd9d8d3934a4c44a3848d7d24cc7336e6a 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 179e4de90afe78b9f2f3e8da9023c2f080b21bc7..f866243c858fff944f5eb01ad693c846007a6f8a 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 86b5137376e02de94cfdea810f663cd4aee967b6..c496a61fd3222a0035a4b2117be9ba5f1950e088 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 a45c56c22993609f3a1a3c28673e460ef5fad051..bc423e6f09a9c01e6d462aba897eec921577e13b 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 a72ebf72e7d766f6ecb224db65b8e7298c242dad..d549cb3aa97b3a5a21117319bf2abefb9912f09c 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -37,6 +37,7 @@ 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; @@ -59,7 +60,9 @@ 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 std::string& storeName, const sptr notifier) = 0; + + virtual int32_t OpenStore(const CloudParam &cloudParam) = 0; 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 0d7e67094c2e0dfca6fd18b46501e4b94cc76b72..6bcc799c05407b1d94444e00f7f095171aeee540 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