diff --git a/conf/config.json b/conf/config.json index 69b0995a96bc73e33b971878ea0070267e213d83..045d6840fb1ebb584b0568259485c836ef11deed 100644 --- a/conf/config.json +++ b/conf/config.json @@ -100,5 +100,12 @@ "schedularInternal" : 1800, "backupInternal" : 36000, "backupNumber" : 20 - } + }, + "autoSyncApps": [ + { + "appId": "", + "bundleName": "", + "version": 3 + } + ] } \ No newline at end of file diff --git a/services/distributeddataservice/adapter/schema_helper/get_schema_helper.cpp b/services/distributeddataservice/adapter/schema_helper/get_schema_helper.cpp index 25ce22999748e3d43a7d21e921602a98dd3102b8..ce4064d7043e87d2d66c39fe1b467a2e695dc499 100644 --- a/services/distributeddataservice/adapter/schema_helper/get_schema_helper.cpp +++ b/services/distributeddataservice/adapter/schema_helper/get_schema_helper.cpp @@ -16,7 +16,7 @@ #define LOG_TAG "SchemaHelper" #include "get_schema_helper.h" -#include "bundle_mgr_interface.h" +#include "bundlemgr/bundle_mgr_interface.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "log_print.h" diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index a9be1e0d004acdc9e5765239141de4eda6bb26fb..9e8319c83b4fdcc4fac3de9b43082c2e76d48904 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -383,7 +383,7 @@ void KvStoreDataService::LoadConfigs() Bootstrap::GetInstance().LoadBackup(executors_); Bootstrap::GetInstance().LoadCloud(); Bootstrap::GetInstance().LoadAppIdMappings(); - Bootstrap::GetInstance().LoadDeviceSyncAppWhiteLists(); + Bootstrap::GetInstance().LoadAutoSyncApps(); Bootstrap::GetInstance().LoadSyncTrustedApp(); } @@ -905,7 +905,7 @@ void KvStoreDataService::AccountEventChanged(const AccountEventInfo &eventInfo) g_kvStoreAccountEventStatus = 1; // delete all kvstore meta belong to this user std::vector metaData; - MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({""}), metaData, true); + MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({}), metaData, true); for (const auto &meta : metaData) { if (meta.user != eventInfo.userId) { continue; diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index c02ccc84c0faab89d26ced1b5d4fa9598a96e858..3f6eea65051603fd36bec3201fc71102ee9049ab 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -71,7 +71,6 @@ ohos_shared_library("distributeddatasvcfwk") { "communication/connect_manager.cpp", "crypto/crypto_manager.cpp", "device_manager/device_manager_delegate.cpp", - "device_sync_app/device_sync_app_manager.cpp", "dfx/reporter.cpp", "directory/directory_manager.cpp", "dump/dump_manager.cpp", @@ -90,6 +89,7 @@ ohos_shared_library("distributeddatasvcfwk") { "metadata/meta_data_manager.cpp", "metadata/object_user_meta_data.cpp", "metadata/secret_key_meta_data.cpp", + "metadata/special_channel_data.cpp", "metadata/store_debug_info.cpp", "metadata/store_meta_data.cpp", "metadata/store_meta_data_local.cpp", @@ -103,6 +103,7 @@ ohos_shared_library("distributeddatasvcfwk") { "snapshot/bind_event.cpp", "snapshot/snapshot.cpp", "store/auto_cache.cpp", + "sync_mgr/sync_mgr.cpp", "thread/thread_manager.cpp", "utils/anonymous.cpp", "utils/base64_utils.cpp", diff --git a/services/distributeddataservice/framework/include/metadata/special_channel_data.h b/services/distributeddataservice/framework/include/metadata/special_channel_data.h new file mode 100644 index 0000000000000000000000000000000000000000..c35290f0b15ee6984a962df39c7224de7b8b6e9c --- /dev/null +++ b/services/distributeddataservice/framework/include/metadata/special_channel_data.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_SPECIAL_CHANNEL_DATA_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_SPECIAL_CHANNEL_DATA_H +#include + +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +struct API_EXPORT SpecialChannelData final : public Serializable { + std::vector devices; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + API_EXPORT static std::string GetKey(); + +private: + static constexpr const char *KEY_PREFIX = "SpecialChannel"; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_SPECIAL_CHANNEL_DATA_H diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h b/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h old mode 100755 new mode 100644 diff --git a/services/distributeddataservice/framework/include/device_sync_app/device_sync_app_manager.h b/services/distributeddataservice/framework/include/sync_mgr/sync_mgr.h similarity index 56% rename from services/distributeddataservice/framework/include/device_sync_app/device_sync_app_manager.h rename to services/distributeddataservice/framework/include/sync_mgr/sync_mgr.h index 664883bce41c938e7644226cef7392cd7daf0a88..5907e8594b734d676a670774da40ecc55fe3332c 100644 --- a/services/distributeddataservice/framework/include/device_sync_app/device_sync_app_manager.h +++ b/services/distributeddataservice/framework/include/sync_mgr/sync_mgr.h @@ -1,39 +1,45 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISTRIBUTEDDATAMGR_DEVICE_SYNC_APP_MANAGER_H -#define DISTRIBUTEDDATAMGR_DEVICE_SYNC_APP_MANAGER_H - -#include -#include -#include "visibility.h" -namespace OHOS::DistributedData { -class DeviceSyncAppManager { -public: - struct WhiteList { - std::string appId; - std::string bundleName; - uint32_t version; - }; - API_EXPORT static DeviceSyncAppManager &GetInstance(); - API_EXPORT void Initialize(const std::vector &lists); - API_EXPORT bool Check(const WhiteList &whiteList); - -private: - DeviceSyncAppManager(); - std::vector whiteLists_; -}; -} // namespace OHOS::DistributedData +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTEDDATAMGR_DEVICE_SYNC_APP_MANAGER_H +#define DISTRIBUTEDDATAMGR_DEVICE_SYNC_APP_MANAGER_H + +#include +#include +#include +#include "visibility.h" +namespace OHOS::DistributedData { +class SyncManager { +public: + struct AutoSyncInfo { + uint32_t version = 0; + std::string appId; + std::string bundleName; + std::vector storeIds; + }; + API_EXPORT static SyncManager &GetInstance(); + API_EXPORT void Initialize(const std::vector &autoSyncApps); + API_EXPORT void SetAutoSyncAppInfo(const AutoSyncInfo &autoSyncApp); + API_EXPORT bool IsAutoSyncApp(const std::string &bundleName, const std::string &appId); + API_EXPORT bool IsAutoSyncStore(const std::string &bundleName, const std::string &appId, + const std::string &store); + API_EXPORT bool NeedForceReplaceSchema(const AutoSyncInfo &autoSyncApp); + +private: + SyncManager(); + std::map autoSyncApps_; +}; +} // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_DEVICE_SYNC_APP_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/device_sync_app/device_sync_app_manager.cpp b/services/distributeddataservice/framework/metadata/special_channel_data.cpp similarity index 46% rename from services/distributeddataservice/framework/device_sync_app/device_sync_app_manager.cpp rename to services/distributeddataservice/framework/metadata/special_channel_data.cpp index e20ee46ef181dd261371ee36788e70cd6f83cc79..6c8866008e6df70a4e9c972cbcaaec141fd30b95 100644 --- a/services/distributeddataservice/framework/device_sync_app/device_sync_app_manager.cpp +++ b/services/distributeddataservice/framework/metadata/special_channel_data.cpp @@ -1,47 +1,35 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#define LOG_TAG "DeviceSyncAppManager" -#include "device_sync_app/device_sync_app_manager.h" - -namespace OHOS::DistributedData { -DeviceSyncAppManager::DeviceSyncAppManager() -{ -} - -DeviceSyncAppManager &DeviceSyncAppManager::GetInstance() -{ - static DeviceSyncAppManager instance; - return instance; -} - -void DeviceSyncAppManager::Initialize(const std::vector &lists) -{ - for (const auto &list : lists) { - whiteLists_.push_back(list); - } -} - -bool DeviceSyncAppManager::Check(const WhiteList &whiteList) -{ - for (const auto &info : whiteLists_) { - if (info.appId == whiteList.appId && (info.bundleName == whiteList.bundleName) && - (info.version == whiteList.version)) { - return true; - } - } - return false; -} - -} // namespace OHOS::DistributedData \ No newline at end of file +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "metadata/special_channel_data.h" +#include "utils/constant.h" +namespace OHOS::DistributedData { +bool SpecialChannelData::Marshal(json &node) const +{ + SetValue(node[GET_NAME(devices)], devices); + return true; +} + +bool SpecialChannelData::Unmarshal(const json &node) +{ + GetValue(node, GET_NAME(devices), devices); + return true; +} + +std::string SpecialChannelData::GetKey() +{ + return std::string(KEY_PREFIX) + Constant::KEY_SEPARATOR; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp b/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp old mode 100755 new mode 100644 diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index 4253619e56be45d32ff38c87bcd8ead55530fc25..d65c9dee579e8a3a497c96f5b399b5fbf6ed06a1 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -63,10 +63,10 @@ AutoCache::~AutoCache() std::string AutoCache::GenerateKey(const std::string &path, const std::string &storeId) const { - std::string key = ""; - if (path.empty() || storeId.empty()) { - return key; + if (path.empty()) { + return storeId; } + std::string key = ""; return key.append(path).append(KEY_SEPARATOR).append(storeId); } diff --git a/services/distributeddataservice/framework/sync_mgr/sync_mgr.cpp b/services/distributeddataservice/framework/sync_mgr/sync_mgr.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b7d91f0fb6884e10d23136fee0cf0dab444a2b0 --- /dev/null +++ b/services/distributeddataservice/framework/sync_mgr/sync_mgr.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "SyncManager" +#include "sync_mgr/sync_mgr.h" + +namespace OHOS::DistributedData { +SyncManager::SyncManager() +{ +} + +SyncManager &SyncManager::GetInstance() +{ + static SyncManager instance; + return instance; +} + +void SyncManager::Initialize(const std::vector &autoSyncApps) +{ + for (const auto &app : autoSyncApps) { + SetAutoSyncAppInfo(app); + } +} + +void SyncManager::SetAutoSyncAppInfo(const AutoSyncInfo &autoSyncApp) +{ + autoSyncApps_[autoSyncApp.bundleName] = autoSyncApp; +} + +bool SyncManager::IsAutoSyncApp(const std::string &bundleName, const std::string &appId) +{ + auto it = autoSyncApps_.find(bundleName); + if (it == autoSyncApps_.end()) { + return false; + } + return it->second.appId == appId; +} + +bool SyncManager::IsAutoSyncStore(const std::string &bundleName, const std::string &appId, const std::string &store) +{ + auto it = autoSyncApps_.find(bundleName); + if (it == autoSyncApps_.end()) { + return false; + } + if (it->second.appId != appId) { + return false; + } + + for (auto &storeId : it->second.storeIds) { + if (storeId == store) { + return true; + } + } + return false; +} + +bool SyncManager::NeedForceReplaceSchema(const AutoSyncInfo &autoSyncApp) +{ + auto it = autoSyncApps_.find(autoSyncApp.bundleName); + if (it == autoSyncApps_.end()) { + return false; + } + return ((it->second.version == autoSyncApp.version) && (it->second.appId == autoSyncApp.appId)); +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 37a5bafddd07269758be7cb49b3601c027d7f709..d0c27991f6abbed33feec9b5e392c8f797b7aad4 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -400,9 +400,9 @@ ohos_unittest("ConnectManagerTest") { external_deps = [ "kv_store:datamgr_common" ] } -ohos_unittest("DeviceSyncAppManagerTest") { +ohos_unittest("SyncManagerTest") { module_out_path = module_output_path - sources = [ "device_sync_app_manager_test.cpp" ] + sources = [ "sync_manager_test.cpp" ] configs = [ ":module_private_config" ] deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] external_deps = [ "kv_store:datamgr_common" ] @@ -423,7 +423,6 @@ group("unittest") { ":ConnectManagerTest", ":ConstantTest", ":CryptoTest", - ":DeviceSyncAppManagerTest", ":EventCenterTest", ":EventTest", ":FeatureTest", @@ -436,6 +435,7 @@ group("unittest") { ":StoreMetaDataLocalTest", ":StoreTest", ":SubscriptionTest", + ":SyncManagerTest", ] } ############################################################################### diff --git a/services/distributeddataservice/framework/test/device_sync_app_manager_test.cpp b/services/distributeddataservice/framework/test/device_sync_app_manager_test.cpp deleted file mode 100644 index 6ae441b9f46f540bfcf026c3265783a4347eec9f..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/framework/test/device_sync_app_manager_test.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* -* Copyright (c) 2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -#include "device_sync_app/device_sync_app_manager.h" -#include - -using namespace testing::ext; -using namespace OHOS::DistributedData; - -namespace OHOS::Test { -class DeviceSyncAppManagerTest : public testing::Test { -public: - void SetUp() - { - whiteList1 = {"appId1", "bundleName1", 1}; - whiteList2 = {"appId2", "bundleName2", 2}; - whiteList3 = {"appId3", "bundleName3", 3}; - - std::vector lists = {whiteList1, whiteList2, whiteList3}; - DeviceSyncAppManager::GetInstance().Initialize(lists); - } - - DeviceSyncAppManager::WhiteList whiteList1; - DeviceSyncAppManager::WhiteList whiteList2; - DeviceSyncAppManager::WhiteList whiteList3; -}; - -/** -* @tc.name: Check001 -* @tc.desc: Checks that the given WhiteList object exists in whiteLists_ list. -* @tc.type: FUNC -*/ -HWTEST_F(DeviceSyncAppManagerTest, Check001, TestSize.Level1) -{ - DeviceSyncAppManager::WhiteList whiteList = {"appId1", "bundleName1", 1}; - bool result = DeviceSyncAppManager::GetInstance().Check(whiteList); - EXPECT_TRUE(result); -} - -/** -* @tc.name: Check002 -* @tc.desc: Check that the given appId object does not match. -* @tc.type: FUNC -*/ -HWTEST_F(DeviceSyncAppManagerTest, Check002, TestSize.Level1) -{ - DeviceSyncAppManager::WhiteList testList = {"appId2", "bundleName1", 1}; - bool result = DeviceSyncAppManager::GetInstance().Check(testList); - EXPECT_FALSE(result); -} - -/** -* @tc.name: Check003 -* @tc.desc: Check that the given bundleName object does not match. -* @tc.type: FUNC -*/ -HWTEST_F(DeviceSyncAppManagerTest, Check003, TestSize.Level1) -{ - DeviceSyncAppManager::WhiteList testList = {"appId1", "bundleName2", 1}; - bool result = DeviceSyncAppManager::GetInstance().Check(testList); - EXPECT_FALSE(result); -} - -/** -* @tc.name: Check004 -* @tc.desc: Check that the given version object does not match. -* @tc.type: FUNC -*/ -HWTEST_F(DeviceSyncAppManagerTest, Check004, TestSize.Level1) -{ - DeviceSyncAppManager::WhiteList testList = {"appId1", "bundleName1", 2}; - bool result = DeviceSyncAppManager::GetInstance().Check(testList); - EXPECT_FALSE(result); -} - -/** -* @tc.name: Check005 -* @tc.desc: Checks that the given WhiteList object does not exist in whiteLists_ list. -* @tc.type: FUNC -*/ -HWTEST_F(DeviceSyncAppManagerTest, Check005, TestSize.Level1) -{ - DeviceSyncAppManager::WhiteList whiteList = {"appId4", "bundleName4", 4}; - bool result = DeviceSyncAppManager::GetInstance().Check(whiteList); - EXPECT_FALSE(result); -} -} // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/framework/test/sync_manager_test.cpp b/services/distributeddataservice/framework/test/sync_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36c1cb1814d19caeb2fc8d56bd0050f28c896495 --- /dev/null +++ b/services/distributeddataservice/framework/test/sync_manager_test.cpp @@ -0,0 +1,142 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include + +#include "sync_mgr/sync_mgr.h" + +using namespace testing::ext; +using namespace OHOS::DistributedData; + +namespace OHOS::Test { +class SyncManagerTest : public testing::Test { +public: + void SetUp() + { + SyncManager::AutoSyncInfo syncInfo1 = { 1, "appId1", "bundleName1" }; + SyncManager::AutoSyncInfo syncInfo2 = { 2, "appId2", "bundleName2" }; + SyncManager::AutoSyncInfo syncInfo3 = { 3, "appId3", "bundleName3" }; + SyncManager::AutoSyncInfo syncInfo4 = { 3, "appId13", "bundleName13", { "storeId1", "storeId2" } }; + SyncManager::GetInstance().Initialize({ syncInfo1, syncInfo2, syncInfo3, syncInfo4 }); + } +}; + +/** +* @tc.name: NeedForceReplaceSchema001 +* @tc.desc: the app need force replace the sync schema. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, NeedForceReplaceSchema001, TestSize.Level1) +{ + SyncManager::AutoSyncInfo autoSyncInfo = { 1, "appId1", "bundleName1" }; + bool result = SyncManager::GetInstance().NeedForceReplaceSchema(autoSyncInfo); + EXPECT_TRUE(result); +} + +/** +* @tc.name: NeedForceReplaceSchema002 +* @tc.desc: the app need not force replace the sync schema. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, NeedForceReplaceSchema002, TestSize.Level1) +{ + SyncManager::AutoSyncInfo autoSyncInfo = { 1, "appId2", "bundleName1" }; + bool result = SyncManager::GetInstance().NeedForceReplaceSchema(autoSyncInfo); + EXPECT_FALSE(result); +} + +/** +* @tc.name: NeedForceReplaceSchema003 +* @tc.desc: the app need not force replace the sync schema. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, NeedForceReplaceSchema003, TestSize.Level1) +{ + SyncManager::AutoSyncInfo autoSyncInfo = { 1, "appId1", "bundleName2" }; + bool result = SyncManager::GetInstance().NeedForceReplaceSchema(autoSyncInfo); + EXPECT_FALSE(result); +} + +/** +* @tc.name: NeedForceReplaceSchema004 +* @tc.desc: the app need not force replace the sync schema. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, NeedForceReplaceSchema004, TestSize.Level1) +{ + SyncManager::AutoSyncInfo autoSyncInfo = { 2, "appId1", "bundleName2" }; + bool result = SyncManager::GetInstance().NeedForceReplaceSchema(autoSyncInfo); + EXPECT_FALSE(result); +} + +/** +* @tc.name: NeedForceReplaceSchema005 +* @tc.desc: the app need not force replace the sync schema. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, NeedForceReplaceSchema005, TestSize.Level1) +{ + SyncManager::AutoSyncInfo autoSyncInfo = { 4, "appId4", "bundleName4" }; + bool result = SyncManager::GetInstance().NeedForceReplaceSchema(autoSyncInfo); + EXPECT_FALSE(result); +} + +/** +* @tc.name: IsAutoSyncApp001 +* @tc.desc: the app is auto sync system app. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, IsAutoSyncApp001, TestSize.Level1) +{ + // valid bundleName and valid appid + bool autoSync = SyncManager::GetInstance().IsAutoSyncApp("bundleName2", "appId2"); + EXPECT_TRUE(autoSync); + // invalid bundleName and invalid appid + autoSync = SyncManager::GetInstance().IsAutoSyncApp("bundleName4", "appId4"); + EXPECT_FALSE(autoSync); + // valid bundleName and invalid appid + autoSync = SyncManager::GetInstance().IsAutoSyncApp("bundleName1", "appId5"); + EXPECT_FALSE(autoSync); +} + +/** +* @tc.name: IsAutoSyncStore001 +* @tc.desc: the bundleName is equal to the sync system app, but the appId is another app. +* @tc.type: FUNC +*/ +HWTEST_F(SyncManagerTest, IsAutoSyncStore001, TestSize.Level1) +{ + // valid bundleName, valid appid, valid storeId, + bool autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName13", "appId13", "storeId1"); + EXPECT_TRUE(autoSyncStore); + // valid bundleName, valid appid, another valid storeId, + autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName13", "appId13", "storeId2"); + EXPECT_TRUE(autoSyncStore); + // valid bundleName, valid appid, invalid storeId, + autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName13", "appId13", "storeId3"); + EXPECT_FALSE(autoSyncStore); + // invalid bundleName, valid appid, invalid storeId, + autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName1", "appId13", "storeId3"); + EXPECT_FALSE(autoSyncStore); + // invalid bundleName, invalid appid, invalid storeId, + autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName1", "appId1", "storeId3"); + EXPECT_FALSE(autoSyncStore); + // invalid bundleName, invalid appid, valid storeId, + autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName1", "appId1", "storeId1"); + EXPECT_FALSE(autoSyncStore); + // invalid bundleName, valid appid, valid storeId, + autoSyncStore = SyncManager::GetInstance().IsAutoSyncStore("bundleName1", "appId13", "storeId1"); + EXPECT_FALSE(autoSyncStore); +} +} // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/framework/utils/anonymous.cpp b/services/distributeddataservice/framework/utils/anonymous.cpp index 2cf8434a145c4ec222195ea739bde5ec18f00f01..de87b5b241c3ccd70f76c79170aa4122a4778e92 100644 --- a/services/distributeddataservice/framework/utils/anonymous.cpp +++ b/services/distributeddataservice/framework/utils/anonymous.cpp @@ -14,6 +14,7 @@ */ #include "utils/anonymous.h" +#include namespace OHOS { namespace DistributedData { diff --git a/services/distributeddataservice/framework/utils/time_utils.cpp b/services/distributeddataservice/framework/utils/time_utils.cpp index a85d147f4338f77c5750e05610b5763828049bd5..e133f6f20680139db46a32d88d5687befa30a4f4 100644 --- a/services/distributeddataservice/framework/utils/time_utils.cpp +++ b/services/distributeddataservice/framework/utils/time_utils.cpp @@ -14,7 +14,7 @@ */ #define LOG_TAG "TimeUtils" -#include "time_utils.h" +#include "utils/time_utils.h" #include #include diff --git a/services/distributeddataservice/service/bootstrap/include/bootstrap.h b/services/distributeddataservice/service/bootstrap/include/bootstrap.h index eaf7672446ef568faff65715ef6c64451097de04..eb94790ba16ff2b04a7bf132ba299df0468abd39 100644 --- a/services/distributeddataservice/service/bootstrap/include/bootstrap.h +++ b/services/distributeddataservice/service/bootstrap/include/bootstrap.h @@ -33,7 +33,7 @@ public: API_EXPORT void LoadBackup(std::shared_ptr executors); API_EXPORT void LoadAppIdMappings(); API_EXPORT void LoadThread(); - API_EXPORT void LoadDeviceSyncAppWhiteLists(); + API_EXPORT void LoadAutoSyncApps(); API_EXPORT void LoadSyncTrustedApp(); private: static constexpr const char *DEFAULT_LABEL = "distributeddata"; diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 106363c184ef1411ddb50a9513a811abe7538f7c..a241ef80b5a70acd7be22127e5dbfd7e7349a200 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -24,9 +24,9 @@ #include "checker/checker_manager.h" #include "cloud/cloud_config_manager.h" #include "config_factory.h" -#include "device_sync_app/device_sync_app_manager.h" #include "directory/directory_manager.h" #include "log_print.h" +#include "sync_mgr/sync_mgr.h" #include "thread/thread_manager.h" namespace OHOS { namespace DistributedData { @@ -199,17 +199,15 @@ void Bootstrap::LoadThread() ThreadManager::GetInstance().Initialize(config->minThreadNum, config->maxThreadNum, config->ipcThreadNum); } -void Bootstrap::LoadDeviceSyncAppWhiteLists() +void Bootstrap::LoadAutoSyncApps() { - auto *deviceSyncAppWhiteLists = ConfigFactory::GetInstance().GetDeviceSyncAppWhiteListConfig(); - if (deviceSyncAppWhiteLists == nullptr) { + auto *autoSyncApps = ConfigFactory::GetInstance().GetAutoSyncAppConfig(); + if (autoSyncApps == nullptr) { return; } - std::vector infos; - for (const auto &info : deviceSyncAppWhiteLists->whiteLists) { - infos.push_back({ info.appId, info.bundleName, info.version }); + for (auto &autoSyncApp : *autoSyncApps) { + SyncManager::GetInstance().SetAutoSyncAppInfo(autoSyncApp); } - DeviceSyncAppManager::GetInstance().Initialize(infos); } void Bootstrap::LoadSyncTrustedApp() diff --git a/services/distributeddataservice/service/config/BUILD.gn b/services/distributeddataservice/service/config/BUILD.gn index 99550fc4ad8c842d13eb27cb5aef5678a9e923d4..3b36840999f077d61d6908d8ff729e001317f482 100644 --- a/services/distributeddataservice/service/config/BUILD.gn +++ b/services/distributeddataservice/service/config/BUILD.gn @@ -26,12 +26,12 @@ ohos_source_set("distributeddata_config") { "src/config_factory.cpp", "src/model/app_access_check_config.cpp", "src/model/app_id_mapping_config.cpp", + "src/model/auto_sync_app_config.cpp", "src/model/backup_config.cpp", "src/model/checker_config.cpp", "src/model/cloud_config.cpp", "src/model/component_config.cpp", "src/model/datashare_config.cpp", - "src/model/device_sync_app_white_list_config.cpp", "src/model/directory_config.cpp", "src/model/global_config.cpp", "src/model/network_config.cpp", diff --git a/services/distributeddataservice/service/config/include/config_factory.h b/services/distributeddataservice/service/config/include/config_factory.h index 87fcb7e693fefc00a13d6a381dc446471cba8ab8..448eec27a7d810922ec2a7a7ce92b542a0795620 100644 --- a/services/distributeddataservice/service/config/include/config_factory.h +++ b/services/distributeddataservice/service/config/include/config_factory.h @@ -34,7 +34,7 @@ public: API_EXPORT std::vector *GetAppIdMappingConfig(); API_EXPORT ThreadConfig *GetThreadConfig(); API_EXPORT DataShareConfig *GetDataShareConfig(); - API_EXPORT DeviceSyncAppWhiteListConfig *GetDeviceSyncAppWhiteListConfig(); + API_EXPORT std::vector *GetAutoSyncAppConfig(); API_EXPORT AppAccessCheckConfig *GetSyncAppsConfig(); private: static constexpr const char *CONF_PATH = "/system/etc/distributeddata/conf"; diff --git a/services/distributeddataservice/service/config/include/model/device_sync_app_white_list_config.h b/services/distributeddataservice/service/config/include/model/auto_sync_app_config.h similarity index 57% rename from services/distributeddataservice/service/config/include/model/device_sync_app_white_list_config.h rename to services/distributeddataservice/service/config/include/model/auto_sync_app_config.h index 03729b9c77c52dbdf74d6498087f7856f1e36953..59531c4420e41939707f6f044df1f54a4f028a1e 100644 --- a/services/distributeddataservice/service/config/include/model/device_sync_app_white_list_config.h +++ b/services/distributeddataservice/service/config/include/model/auto_sync_app_config.h @@ -1,37 +1,29 @@ -/* -* Copyright (c) 2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DEVICE_SYNC_APP_WHITE_LIST_CONFIG_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DEVICE_SYNC_APP_WHITE_LIST_CONFIG_H - -#include "serializable/serializable.h" -namespace OHOS { -namespace DistributedData { -class DeviceSyncAppWhiteListConfig final : public Serializable { -public: - struct WhiteList final : public Serializable { - std::string appId; - std::string bundleName; - uint32_t version; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - }; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - - std::vector whiteLists; -}; -} // namespace DistributedData -} // namespace OHOS -#endif //OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DEVICE_SYNC_APP_WHITE_LIST_CONFIG_H +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_AUTO_SYNC_APP_CONFIG_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_AUTO_SYNC_APP_CONFIG_H + +#include "sync_mgr/sync_mgr.h" +#include "serializable/serializable.h" +namespace OHOS { +namespace DistributedData { +struct AutoSyncAppConfig final : public Serializable, public SyncManager::AutoSyncInfo { +public: + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; +}; +} // namespace DistributedData +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_AUTO_SYNC_APP_CONFIG_H diff --git a/services/distributeddataservice/service/config/include/model/global_config.h b/services/distributeddataservice/service/config/include/model/global_config.h index 4d7a9e531c067edd42a59e07e66385f3e07afc4c..da3c907d41e05e8d468cb3234b1c504baaf2b0de 100644 --- a/services/distributeddataservice/service/config/include/model/global_config.h +++ b/services/distributeddataservice/service/config/include/model/global_config.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_GLOBAL_CONFIG_H #include "model/app_access_check_config.h" #include "model/app_id_mapping_config.h" +#include "model/auto_sync_app_config.h" #include "model/backup_config.h" #include "model/checker_config.h" #include "model/cloud_config.h" @@ -25,7 +26,6 @@ #include "model/directory_config.h" #include "model/network_config.h" #include "model/thread_config.h" -#include "model/device_sync_app_white_list_config.h" #include "serializable/serializable.h" namespace OHOS { namespace DistributedData { @@ -44,7 +44,7 @@ public: std::vector *appIdMapping = nullptr; ThreadConfig *thread = nullptr; DataShareConfig *dataShare = nullptr; - DeviceSyncAppWhiteListConfig *deviceSyncAppWhiteList = nullptr; + std::vector *autoSyncApps = nullptr; AppAccessCheckConfig *syncAppList = nullptr; ~GlobalConfig(); bool Marshal(json &node) const override; diff --git a/services/distributeddataservice/service/config/src/config_factory.cpp b/services/distributeddataservice/service/config/src/config_factory.cpp index 0f91cb6aaad97d053323787ce4f517d6ffff764e..9c7c7cdd32ad042448939540a22a631d546429c1 100644 --- a/services/distributeddataservice/service/config/src/config_factory.cpp +++ b/services/distributeddataservice/service/config/src/config_factory.cpp @@ -97,9 +97,9 @@ DataShareConfig *ConfigFactory::GetDataShareConfig() return config_.dataShare; } -DeviceSyncAppWhiteListConfig *ConfigFactory::GetDeviceSyncAppWhiteListConfig() +std::vector *ConfigFactory::GetAutoSyncAppConfig() { - return config_.deviceSyncAppWhiteList; + return config_.autoSyncApps; } AppAccessCheckConfig *ConfigFactory::GetSyncAppsConfig() diff --git a/services/distributeddataservice/service/config/src/model/device_sync_app_white_list_config.cpp b/services/distributeddataservice/service/config/src/model/auto_sync_app_config.cpp similarity index 66% rename from services/distributeddataservice/service/config/src/model/device_sync_app_white_list_config.cpp rename to services/distributeddataservice/service/config/src/model/auto_sync_app_config.cpp index 594d3f2ae9159af93ae90a2e67edc9eabcc821ba..17d627ddb66824b08ea50c250c63a91b1560fa49 100644 --- a/services/distributeddataservice/service/config/src/model/device_sync_app_white_list_config.cpp +++ b/services/distributeddataservice/service/config/src/model/auto_sync_app_config.cpp @@ -1,45 +1,36 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "model/device_sync_app_white_list_config.h" -namespace OHOS { -namespace DistributedData { -bool DeviceSyncAppWhiteListConfig::Marshal(Serializable::json &node) const -{ - SetValue(node[GET_NAME(whiteLists)], whiteLists); - return true; -} -bool DeviceSyncAppWhiteListConfig::Unmarshal(const Serializable::json &node) -{ - GetValue(node, GET_NAME(whiteLists), whiteLists); - return true; -} - -bool DeviceSyncAppWhiteListConfig::WhiteList::Marshal(Serializable::json &node) const -{ - SetValue(node[GET_NAME(appId)], appId); - SetValue(node[GET_NAME(bundleName)], bundleName); - SetValue(node[GET_NAME(version)], version); - return true; -} -bool DeviceSyncAppWhiteListConfig::WhiteList::Unmarshal(const Serializable::json &node) -{ - GetValue(node, GET_NAME(appId), appId); - GetValue(node, GET_NAME(bundleName), bundleName); - GetValue(node, GET_NAME(version), version); - return true; -} -} // namespace DistributedData -} // namespace OHOS +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "model/auto_sync_app_config.h" +namespace OHOS { +namespace DistributedData { +bool AutoSyncAppConfig::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(version)], version); + SetValue(node[GET_NAME(appId)], appId); + SetValue(node[GET_NAME(bundleName)], bundleName); + SetValue(node[GET_NAME(storeIds)], storeIds); + return true; +} +bool AutoSyncAppConfig::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(version), version); + GetValue(node, GET_NAME(appId), appId); + GetValue(node, GET_NAME(bundleName), bundleName); + GetValue(node, GET_NAME(storeIds), storeIds); + return true; +} +} // namespace DistributedData +} // namespace OHOS diff --git a/services/distributeddataservice/service/config/src/model/global_config.cpp b/services/distributeddataservice/service/config/src/model/global_config.cpp index 51b12d61e8bab3ab1fc3a63fd4ee875e27d1c763..09891bc37e9d54061037476e132d6923b9105fdc 100644 --- a/services/distributeddataservice/service/config/src/model/global_config.cpp +++ b/services/distributeddataservice/service/config/src/model/global_config.cpp @@ -31,7 +31,7 @@ bool GlobalConfig::Marshal(json &node) const SetValue(node[GET_NAME(appIdMapping)], appIdMapping); SetValue(node[GET_NAME(thread)], thread); SetValue(node[GET_NAME(dataShare)], dataShare); - SetValue(node[GET_NAME(deviceSyncAppWhiteList)], deviceSyncAppWhiteList); + SetValue(node[GET_NAME(autoSyncApps)], autoSyncApps); SetValue(node[GET_NAME(syncAppList)], syncAppList); return true; } @@ -51,7 +51,7 @@ bool GlobalConfig::Unmarshal(const json &node) GetValue(node, GET_NAME(appIdMapping), appIdMapping); GetValue(node, GET_NAME(thread), thread); GetValue(node, GET_NAME(dataShare), dataShare); - GetValue(node, GET_NAME(deviceSyncAppWhiteList), deviceSyncAppWhiteList); + GetValue(node, GET_NAME(autoSyncApps), autoSyncApps); GetValue(node, GET_NAME(syncAppList), syncAppList); return true; } @@ -67,7 +67,7 @@ GlobalConfig::~GlobalConfig() delete appIdMapping; delete thread; delete dataShare; - delete deviceSyncAppWhiteList; + delete autoSyncApps; delete syncAppList; } } // namespace DistributedData diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index d6b43d5beeecb7ad9944735e2aebfe4885c17e1f..94f13725d766142acf1cbc8784c2d87532c8be88 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -26,7 +26,7 @@ #include "cloud/cloud_mark.h" #include "cloud/cloud_store_types.h" #include "cloud/schema_meta.h" -#include "device_sync_app/device_sync_app_manager.h" +#include "sync_mgr/sync_mgr.h" #include "cloud_service.h" #include "commonevent/data_sync_event.h" #include "crypto/crypto_manager.h" @@ -961,8 +961,8 @@ int32_t RdbGeneralStore::SetDistributedTables(const std::vector &ta } auto [exist, database] = GetDistributedSchema(observer_.meta_); if (exist && type == DistributedTableType::DISTRIBUTED_DEVICE) { - auto force = DeviceSyncAppManager::GetInstance().Check( - {observer_.meta_.appId, observer_.meta_.bundleName, database.version}); + auto force = SyncManager::GetInstance().NeedForceReplaceSchema( + {database.version, observer_.meta_.appId, observer_.meta_.bundleName, {}}); delegate_->SetDistributedSchema(GetGaussDistributedSchema(database), force); } CloudMark metaData(storeInfo_); diff --git a/services/distributeddataservice/service/rdb/rdb_query.cpp b/services/distributeddataservice/service/rdb/rdb_query.cpp index 994a98a458a6520f08113e88e5b85259630d29b8..ec716bbcc1eee2b2f4e43f4691ee2fed7e68a81f 100644 --- a/services/distributeddataservice/service/rdb/rdb_query.cpp +++ b/services/distributeddataservice/service/rdb/rdb_query.cpp @@ -70,9 +70,9 @@ void RdbQuery::MakeQuery(const PredicatesMemo &predicates) tables_ = predicates.tables_; } -void RdbQuery::MakeQuery(const std::string &table) +void RdbQuery::MakeDeviceQuery(const std::vector &tables) { - query_ = DistributedDB::Query::Select(table); + query_ = DistributedDB::Query::Select().FromTable(tables); } void RdbQuery::MakeCloudQuery(const PredicatesMemo& predicates) diff --git a/services/distributeddataservice/service/rdb/rdb_query.h b/services/distributeddataservice/service/rdb/rdb_query.h index 94d9460f233deea470bf3cec07a79a668d75d3a5..cf80b032a9210ba56acb9a4ba1d3e1f33b5943e1 100644 --- a/services/distributeddataservice/service/rdb/rdb_query.h +++ b/services/distributeddataservice/service/rdb/rdb_query.h @@ -41,7 +41,7 @@ public: bool IsRemoteQuery(); bool IsPriority(); void MakeQuery(const PredicatesMemo &predicates); - void MakeQuery(const std::string &table); + void MakeDeviceQuery(const std::vector &tables); void MakeRemoteQuery(const std::string &devices, const std::string &sql, DistributedData::Values &&args); void MakeCloudQuery(const PredicatesMemo &predicates); diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index c580433a9d669eda56a3feba7be36edca5b10389..62f6aec8a9beb5c356b744547514db03745573bf 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -38,6 +38,7 @@ #include "metadata/auto_launch_meta_data.h" #include "metadata/capability_meta_data.h" #include "metadata/meta_data_manager.h" +#include "metadata/special_channel_data.h" #include "metadata/store_debug_info.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" @@ -50,18 +51,19 @@ #include "rdb_schema_config.h" #include "rdb_watcher.h" #include "store/general_store.h" +#include "sync_mgr/sync_mgr.h" #include "tokenid_kit.h" #include "types_export.h" #include "utils/anonymous.h" #include "utils/constant.h" #include "utils/converter.h" +#include "utils/crypto.h" #include "xcollie.h" using OHOS::DistributedData::AccountDelegate; using OHOS::DistributedData::Anonymous; using OHOS::DistributedData::CheckerManager; using OHOS::DistributedData::MetaDataManager; using OHOS::DistributedData::StoreMetaData; -using OHOS::DistributedData::AccountDelegate; using namespace OHOS::DistributedData; using namespace OHOS::Security::AccessToken; using DistributedDB::RelationalStoreManager; @@ -72,6 +74,7 @@ using system_clock = std::chrono::system_clock; constexpr uint32_t ITERATE_TIMES = 10000; constexpr uint32_t ALLOW_ONLINE_AUTO_SYNC = 8; +constexpr uint32_t ALLOW_AUTO_SYNC_DEVICE = 10; constexpr int32_t VALID_PARAM_LENGTH = 2; const size_t KEY_COUNT = 2; namespace OHOS::DistributedRdb { @@ -104,7 +107,7 @@ RdbServiceImpl::RdbServiceImpl() { ZLOGI("construct"); DistributedDB::RelationalStoreManager::SetAutoLaunchRequestCallback( - [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { + [this](const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); RegisterEvent(); @@ -121,7 +124,7 @@ int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, Distrib return false; } ZLOGI("size=%{public}d", static_cast(entries.size())); - for (const auto& entry : entries) { + for (const auto &entry : entries) { if (entry.storeType != RDB_DEVICE_COLLABORATION) { continue; } @@ -209,7 +212,7 @@ int32_t RdbServiceImpl::OnFeatureExit(pid_t uid, pid_t pid, uint32_t tokenId, co return E_OK; } -bool RdbServiceImpl::IsValidAccess(const std::string& bundleName, const std::string& storeName) +bool RdbServiceImpl::IsValidAccess(const std::string &bundleName, const std::string &storeName) { CheckerManager::StoreInfo storeInfo; storeInfo.uid = IPCSkeleton::GetCallingUid(); @@ -292,11 +295,12 @@ void RdbServiceImpl::UpdateMeta(const StoreMetaData &meta, const StoreMetaData & syncMeta.isEncrypt, meta.isEncrypt, syncMeta.area, meta.area); MetaDataManager::GetInstance().SaveMeta(meta.GetKeyWithoutPath(), localMeta); } + bool isAutoSync = SyncManager::GetInstance().IsAutoSyncApp(meta.bundleName, meta.appId); Database dataBase; - if (RdbSchemaConfig::GetDistributedSchema(localMeta, dataBase) && !dataBase.name.empty() && + if (isAutoSync && RdbSchemaConfig::GetDistributedSchema(localMeta, dataBase) && !dataBase.name.empty() && !dataBase.bundleName.empty()) { MetaDataManager::GetInstance().SaveMeta(dataBase.GetKey(), dataBase, true); - store->SetConfig({false, GeneralStore::DistributedTableMode::COLLABORATION}); + store->SetConfig({ false, GeneralStore::DistributedTableMode::COLLABORATION }); } } @@ -318,7 +322,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const bool isCreatedLocal = MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), localMeta, true); if (!isCreatedLocal) { ZLOGE("no meta. bundleName:%{public}s, storeName:%{public}s. GetStore failed", param.bundleName_.c_str(), - Anonymous::Change(param.storeName_).c_str()); + Anonymous::Change(param.storeName_).c_str()); return RDB_ERROR; } auto store = GetStore(meta); @@ -334,9 +338,9 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const } else if (type == DistributedTableType::DISTRIBUTED_CLOUD) { if (localMeta.asyncDownloadAsset != param.asyncDownloadAsset_ || localMeta.enableCloud != param.enableCloud_) { ZLOGI("update meta, bundleName:%{public}s, storeName:%{public}s, asyncDownloadAsset? [%{public}d -> " - "%{public}d],enableCloud? [%{public}d -> %{public}d]", param.bundleName_.c_str(), - Anonymous::Change(param.storeName_).c_str(), localMeta.asyncDownloadAsset, param.asyncDownloadAsset_, - localMeta.enableCloud, param.enableCloud_); + "%{public}d],enableCloud? [%{public}d -> %{public}d]", + param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), localMeta.asyncDownloadAsset, + param.asyncDownloadAsset_, localMeta.enableCloud, param.enableCloud_); localMeta.asyncDownloadAsset = param.asyncDownloadAsset_; localMeta.enableCloud = param.enableCloud_; MetaDataManager::GetInstance().SaveMeta(localMeta.GetKey(), localMeta, true); @@ -378,7 +382,7 @@ std::string RdbServiceImpl::TransferStringToHex(const std::string &origStr) std::string tmp; for (auto item : origStr) { auto currentByte = static_cast(item); - tmp.push_back(hex[currentByte >> 4]); // high 4 bit to one hex. + tmp.push_back(hex[currentByte >> 4]); // high 4 bit to one hex. tmp.push_back(hex[currentByte & 0x0F]); // low 4 bit to one hex. } return tmp; @@ -421,8 +425,8 @@ RdbServiceImpl::DetailAsync RdbServiceImpl::GetCallbacks(uint32_t tokenId, const }; } -std::pair> RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, - const std::string& device, const std::string& sql, const std::vector& selectionArgs) +std::pair> RdbServiceImpl::RemoteQuery(const RdbSyncerParam ¶m, + const std::string &device, const std::string &sql, const std::vector &selectionArgs) { if (!IsValidParam(param) || !IsValidAccess(param.bundleName_, param.storeName_)) { ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), @@ -437,8 +441,7 @@ std::pair> RdbServiceImpl::R return { RDB_ERROR, nullptr }; } std::vector devices = { DmAdapter::GetInstance().ToUUID(device) }; - if (IsNeedMetaSync(meta, devices) && !MetaDataManager::GetInstance().Sync( - devices, [](auto &results) {}, true)) { + if (IsNeedMetaSync(meta, devices) && !MetaDataManager::GetInstance().Sync(devices, [](auto &results) {}, true)) { ZLOGW("bundleName:%{public}s, storeName:%{public}s. meta sync failed", param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str()); } @@ -452,7 +455,7 @@ std::pair> RdbServiceImpl::R } int32_t RdbServiceImpl::Sync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, - const AsyncDetail &async) + const AsyncDetail &async) { if (!IsValidParam(param) || !IsValidAccess(param.bundleName_, param.storeName_)) { ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), @@ -467,18 +470,56 @@ int32_t RdbServiceImpl::Sync(const RdbSyncerParam ¶m, const Option &option, return DoSync(param, option, predicates, async); } +void RdbServiceImpl::SaveAutoSyncDeviceId(const StoreMetaData &meta, const std::vector &devices) +{ + DistributedData::SpecialChannelData specialDevices; + if (devices.empty()) { + ZLOGE("There is no deviceId in predicates"); + return; + } + + MetaDataManager::GetInstance().LoadMeta(specialDevices.GetKey(), specialDevices, true); + auto &specialUUIDs = specialDevices.devices; + for (auto &device : devices) { + auto sha256UUID = Crypto::Sha256(device); + auto iter = std::find(specialUUIDs.begin(), specialUUIDs.end(), sha256UUID); + if (iter != specialUUIDs.end() && specialUUIDs.size() == 1) { + continue; + } + if (iter != specialUUIDs.end()) { + std::swap(*iter, specialUUIDs.back()); + } else { + specialUUIDs.push_back(sha256UUID); + } + } + if (specialUUIDs.size() > ALLOW_AUTO_SYNC_DEVICE) { + auto index = specialUUIDs.size() - ALLOW_AUTO_SYNC_DEVICE; + specialUUIDs.erase(specialUUIDs.begin(), specialUUIDs.begin() + index - 1); + } + MetaDataManager::GetInstance().SaveMeta(specialDevices.GetKey(), specialDevices, true); +} + int RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const RdbService::Option &option, const PredicatesMemo &predicates, const AsyncDetail &async) { StoreMetaData meta = GetStoreMetaData(param); - auto store = GetStore(meta); - if (store == nullptr) { + if (meta.instanceId != 0) { return RDB_ERROR; } + RdbQuery rdbQuery; rdbQuery.MakeQuery(predicates); auto devices = rdbQuery.GetDevices().empty() ? DmAdapter::ToUUID(DmAdapter::GetInstance().GetRemoteDevices()) : DmAdapter::ToUUID(rdbQuery.GetDevices()); + bool isAutoSync = SyncManager::GetInstance().IsAutoSyncApp(meta.bundleName, meta.appId); + if (isAutoSync && !rdbQuery.GetDevices().empty()) { + SaveAutoSyncDeviceId(meta, devices); + } + auto store = GetStore(meta); + if (store == nullptr) { + return RDB_ERROR; + } + auto pid = IPCSkeleton::GetCallingPid(); SyncParam syncParam = { option.mode, 0, option.isCompensation }; auto tokenId = IPCSkeleton::GetCallingTokenID(); @@ -548,7 +589,7 @@ RdbServiceImpl::SyncResult RdbServiceImpl::ProcessResult(const std::map({ ValueProxy::Convert(std::move(value)) })); } auto memo = predicates.GetDistributedPredicates(); @@ -600,8 +641,8 @@ void RdbServiceImpl::DoCloudSync(const RdbSyncerParam ¶m, const RdbService:: } }; auto highMode = (!predicates.tables_.empty() && option.mode == DistributedData::GeneralStore::CLOUD_CLOUD_FIRST) - ? GeneralStore::ASSETS_SYNC_MODE - : (option.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE); + ? GeneralStore::ASSETS_SYNC_MODE + : (option.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE); auto mixMode = static_cast(GeneralStore::MixMode(option.mode, highMode)); SyncParam syncParam = { mixMode, (option.isAsync ? 0 : static_cast(WAIT_TIME)), option.isCompensation }; syncParam.asyncDownloadAsset = param.asyncDownloadAsset_; @@ -636,8 +677,8 @@ int32_t RdbServiceImpl::Subscribe(const RdbSyncerParam ¶m, const SubscribeOp return true; }); if (isCreate) { - AutoCache::GetInstance().SetObserver(tokenId, GetWatchers(tokenId, param.storeName_), - GetPath(param), RemoveSuffix(param.storeName_)); + AutoCache::GetInstance().SetObserver(tokenId, GetWatchers(tokenId, param.storeName_), GetPath(param), + RemoveSuffix(param.storeName_)); } return RDB_OK; } @@ -665,13 +706,13 @@ int32_t RdbServiceImpl::UnSubscribe(const RdbSyncerParam ¶m, const Subscribe return true; }); if (destroyed) { - AutoCache::GetInstance().SetObserver(tokenId, GetWatchers(tokenId, param.storeName_), - GetPath(param), RemoveSuffix(param.storeName_)); + AutoCache::GetInstance().SetObserver(tokenId, GetWatchers(tokenId, param.storeName_), GetPath(param), + RemoveSuffix(param.storeName_)); } return RDB_OK; } -int32_t RdbServiceImpl::RegisterAutoSyncCallback(const RdbSyncerParam& param, +int32_t RdbServiceImpl::RegisterAutoSyncCallback(const RdbSyncerParam ¶m, std::shared_ptr observer) { pid_t pid = IPCSkeleton::GetCallingPid(); @@ -691,7 +732,7 @@ int32_t RdbServiceImpl::RegisterAutoSyncCallback(const RdbSyncerParam& param, return RDB_OK; } -int32_t RdbServiceImpl::UnregisterAutoSyncCallback(const RdbSyncerParam& param, +int32_t RdbServiceImpl::UnregisterAutoSyncCallback(const RdbSyncerParam ¶m, std::shared_ptr observer) { auto tokenId = IPCSkeleton::GetCallingTokenID(); @@ -791,7 +832,7 @@ std::pair> RdbServiceImpl::Query return { RDB_OK, std::make_shared(cursor) }; } -std::pair> RdbServiceImpl::AllocResource(StoreInfo& storeInfo, +std::pair> RdbServiceImpl::AllocResource(StoreInfo &storeInfo, std::shared_ptr rdbQuery) { std::pair> result; @@ -1022,7 +1063,7 @@ bool RdbServiceImpl::SaveAppIDMeta(const StoreMetaData &meta, const StoreMetaDat return true; } -int32_t RdbServiceImpl::ReportStatistic(const RdbSyncerParam& param, const RdbStatEvent &statEvent) +int32_t RdbServiceImpl::ReportStatistic(const RdbSyncerParam ¶m, const RdbStatEvent &statEvent) { if (!IsValidAccess(param.bundleName_, param.storeName_)) { ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), @@ -1040,7 +1081,7 @@ void RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.bundleName = param.bundleName_; storeInfo.storeName = RemoveSuffix(param.storeName_); - auto [instanceId, user]= GetInstIndexAndUser(storeInfo.tokenId, param.bundleName_); + auto [instanceId, user] = GetInstIndexAndUser(storeInfo.tokenId, param.bundleName_); storeInfo.instanceId = instanceId; storeInfo.user = user; storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; @@ -1078,7 +1119,7 @@ std::pair RdbServiceImpl::LoadStoreMetaData(const RdbSyncer metaData.customDir = param.customDir_; metaData.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData) + "/" + param.storeName_; auto exist = MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData, true); - return {exist, metaData}; + return { exist, metaData }; } StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) @@ -1130,7 +1171,7 @@ int32_t RdbServiceImpl::Upgrade(const RdbSyncerParam ¶m, const StoreMetaData Details RdbServiceImpl::HandleGenDetails(const GenDetails &details) { Details dbDetails; - for (const auto& [id, detail] : details) { + for (const auto &[id, detail] : details) { auto &dbDetail = dbDetails[id]; dbDetail.progress = detail.progress; dbDetail.code = detail.code; @@ -1142,7 +1183,7 @@ Details RdbServiceImpl::HandleGenDetails(const GenDetails &details) return dbDetails; } -std::string RdbServiceImpl::RemoveSuffix(const std::string& name) +std::string RdbServiceImpl::RemoveSuffix(const std::string &name) { std::string suffix(".db"); auto pos = name.rfind(suffix); @@ -1218,47 +1259,56 @@ std::vector RdbServiceImpl::GetReuseDevice(const std::vector &devices, const Database &dataBase, std::vector tables) +int RdbServiceImpl::DoAutoSync(const std::vector &devices, const StoreMetaData &storeMetaData, + const std::vector &tables) { - StoreMetaData storeMetaData = GetStoreMetaData(dataBase); - auto store = GetStore(storeMetaData); - if (store == nullptr) { - ZLOGE("autosync store null, storeId:%{public}s", storeMetaData.GetStoreAlias().c_str()); + if (storeMetaData.instanceId != 0) { return RDB_ERROR; } - if (executors_ == nullptr) { - ZLOGE("autosync executors_ null, storeId:%{public}s", storeMetaData.GetStoreAlias().c_str()); + DistributedData::SpecialChannelData specialDevices; + if (!MetaDataManager::GetInstance().LoadMeta(specialDevices.GetKey(), specialDevices, true) || + specialDevices.devices.empty()) { + ZLOGE("There is no auto sync devices, store:%{public}s.", Anonymous::Change(storeMetaData.storeId).c_str()); + return RDB_ERROR; + } + auto &specialUUIDs = specialDevices.devices; + std::vector syncDevices; + for (auto &device : devices) { + auto sha256UUID = Crypto::Sha256(device); + if (std::find(specialUUIDs.begin(), specialUUIDs.end(), sha256UUID) != specialUUIDs.end()) { + syncDevices.push_back(device); + } + } + auto store = GetStore(storeMetaData); + if (syncDevices.empty() || executors_ == nullptr || store == nullptr) { + ZLOGE("Designated sync device not in cache storeId:%{public}s", storeMetaData.GetStoreAlias().c_str()); return RDB_ERROR; } SyncParam syncParam = { 0, 0 }; DetailAsync async; - for (auto &table : tables) { - executors_->Execute([this, table, store, syncParam, async, devices, storeMetaData]() { - RdbQuery rdbQuery; - rdbQuery.MakeQuery(table); - std::vector onDevices = GetReuseDevice(devices, storeMetaData); - if (onDevices.empty()) { - ZLOGE("autosync ondevices null, storeId:%{public}s", storeMetaData.GetStoreAlias().c_str()); - return; - } - auto complete = [this, rdbQuery, store, syncParam, async]( - const auto &results) mutable { - auto ret = ProcessResult(results); - store->Sync(ret.first, rdbQuery, async, syncParam); - }; - if (IsNeedMetaSync(storeMetaData, onDevices)) { - MetaDataManager::GetInstance().Sync(onDevices, complete); - return; - } - (void)store->Sync(onDevices, rdbQuery, async, syncParam).first; + executors_->Execute([this, tables, store, syncParam, async, syncDevices, storeMetaData]() { + RdbQuery rdbQuery; + rdbQuery.MakeDeviceQuery(tables); + std::vector onDevices = GetReuseDevice(syncDevices, storeMetaData); + if (onDevices.empty()) { + ZLOGE("autosync device null, storeId:%{public}s", storeMetaData.GetStoreAlias().c_str()); return; - }); - } + } + auto complete = [rdbQuery, store, syncParam, async](const auto &results) mutable { + auto ret = ProcessResult(results); + store->Sync(ret.first, rdbQuery, async, syncParam); + }; + if (IsNeedMetaSync(storeMetaData, onDevices)) { + MetaDataManager::GetInstance().Sync(onDevices, complete); + return; + } + (void)store->Sync(onDevices, rdbQuery, async, syncParam).first; + return; + }); return RDB_OK; } -int RdbServiceImpl::DoOnlineSync(const std::vector &devices, const Database &dataBase) +int RdbServiceImpl::DoOnlineSync(const std::string &device, const Database &dataBase) { std::vector tableNames; for (auto &table : dataBase.tables) { @@ -1266,11 +1316,15 @@ int RdbServiceImpl::DoOnlineSync(const std::vector &devices, const tableNames.push_back(table.name); } } - return DoAutoSync(devices, dataBase, tableNames); + StoreMetaData storeMetaData = GetStoreMetaData(dataBase); + return DoAutoSync({ device }, storeMetaData, tableNames); } int32_t RdbServiceImpl::OnReady(const std::string &device) { + if (device.empty()) { + return 0; + } int index = ALLOW_ONLINE_AUTO_SYNC; Database dataBase; std::string prefix = dataBase.GetPrefix({}); @@ -1278,12 +1332,11 @@ int32_t RdbServiceImpl::OnReady(const std::string &device) if (!MetaDataManager::GetInstance().LoadMeta(prefix, dataBases, true)) { return 0; } - for (auto dataBase : dataBases) { + for (auto &dataBase : dataBases) { if ((dataBase.autoSyncType == AutoSyncType::SYNC_ON_READY || dataBase.autoSyncType == AutoSyncType::SYNC_ON_CHANGE_READY) && index > 0) { - std::vector devices = {device}; - if (DoOnlineSync(devices, dataBase) != RDB_OK) { + if (DoOnlineSync(device, dataBase) != RDB_OK) { ZLOGE("store online sync fail, storeId:%{public}s", Anonymous::Change(dataBase.name).c_str()); } index--; @@ -1348,7 +1401,7 @@ int32_t RdbServiceImpl::RdbStatic::CloseStore(const std::string &bundleName, int int32_t RdbServiceImpl::RdbStatic::OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) { - std::string prefix = Database::GetPrefix({std::to_string(user), "default", bundleName}); + std::string prefix = Database::GetPrefix({ std::to_string(user), "default", bundleName }); std::vector dataBase; if (MetaDataManager::GetInstance().LoadMeta(prefix, dataBase, true)) { for (const auto &dataBase : dataBase) { @@ -1360,7 +1413,7 @@ int32_t RdbServiceImpl::RdbStatic::OnAppUninstall(const std::string &bundleName, int32_t RdbServiceImpl::RdbStatic::OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index) { - std::string prefix = Database::GetPrefix({std::to_string(user), "default", bundleName}); + std::string prefix = Database::GetPrefix({ std::to_string(user), "default", bundleName }); std::vector dataBase; if (MetaDataManager::GetInstance().LoadMeta(prefix, dataBase, true)) { for (const auto &database : dataBase) { @@ -1437,19 +1490,19 @@ int RdbServiceImpl::DoDataChangeSync(const StoreInfo &storeInfo, const RdbChange tableNames.push_back(key); } } - if (MetaDataManager::GetInstance().LoadMeta(dataBase.GetKey(), dataBase, true)) { - std::vector devices = DmAdapter::ToUUID(DmAdapter::GetInstance().GetRemoteDevices()); - if ((dataBase.autoSyncType == AutoSyncType::SYNC_ON_CHANGE || - dataBase.autoSyncType == AutoSyncType::SYNC_ON_CHANGE_READY) && - !devices.empty()) { - return DoAutoSync(devices, dataBase, tableNames); - } + if (!MetaDataManager::GetInstance().LoadMeta(dataBase.GetKey(), dataBase, true)) { + return RDB_OK; + } + StoreMetaData storeMetaData = GetStoreMetaData(dataBase); + if ((dataBase.autoSyncType == AutoSyncType::SYNC_ON_CHANGE || + dataBase.autoSyncType == AutoSyncType::SYNC_ON_CHANGE_READY)) { + return DoAutoSync(DmAdapter::ToUUID(DmAdapter::GetInstance().GetRemoteDevices()), storeMetaData, tableNames); } return RDB_OK; } -int32_t RdbServiceImpl::NotifyDataChange( - const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData, const RdbNotifyConfig &rdbNotifyConfig) +int32_t RdbServiceImpl::NotifyDataChange(const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData, + const RdbNotifyConfig &rdbNotifyConfig) { XCollie xcollie(__FUNCTION__, XCollie::XCOLLIE_LOG | XCollie::XCOLLIE_RECOVERY); if (!IsValidParam(param) || !IsValidAccess(param.bundleName_, param.storeName_)) { @@ -1473,7 +1526,7 @@ int32_t RdbServiceImpl::NotifyDataChange( } for (const auto &[key, value] : rdbChangedData.tableData) { if (value.isTrackedDataChange) { - DataChangeEvent::TableChangeProperties tableChangeProperties = {value.isTrackedDataChange}; + DataChangeEvent::TableChangeProperties tableChangeProperties = { value.isTrackedDataChange }; eventInfo.tableProperties.insert_or_assign(key, std::move(tableChangeProperties)); } } @@ -1521,7 +1574,7 @@ bool RdbServiceImpl::IsPostImmediately(const int32_t callingPid, const RdbNotify return postImmediately; } -int32_t RdbServiceImpl::SetSearchable(const RdbSyncerParam& param, bool isSearchable) +int32_t RdbServiceImpl::SetSearchable(const RdbSyncerParam ¶m, bool isSearchable) { XCollie xcollie(__FUNCTION__, XCollie::XCOLLIE_LOG | XCollie::XCOLLIE_RECOVERY); if (!IsValidAccess(param.bundleName_, param.storeName_)) { @@ -1626,10 +1679,10 @@ StoreInfo RdbServiceImpl::GetStoreInfo(const RdbSyncerParam ¶m) std::pair RdbServiceImpl::LockCloudContainer(const RdbSyncerParam ¶m) { - std::pair result { RDB_ERROR, 0 }; + std::pair result{ RDB_ERROR, 0 }; if (!IsValidAccess(param.bundleName_, param.storeName_)) { ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), - Anonymous::Change(param.storeName_).c_str()); + Anonymous::Change(param.storeName_).c_str()); return result; } ZLOGI("start to lock cloud db: bundleName:%{public}s, storeName:%{public}s", param.bundleName_.c_str(), @@ -1651,7 +1704,7 @@ int32_t RdbServiceImpl::UnlockCloudContainer(const RdbSyncerParam ¶m) int32_t result = RDB_ERROR; if (!IsValidAccess(param.bundleName_, param.storeName_)) { ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), - Anonymous::Change(param.storeName_).c_str()); + Anonymous::Change(param.storeName_).c_str()); return result; } ZLOGI("start to unlock cloud db: bundleName:%{public}s, storeName:%{public}s", param.bundleName_.c_str(), @@ -1715,7 +1768,7 @@ int32_t RdbServiceImpl::SaveDebugInfo(const StoreMetaData &metaData, const RdbSy fileInfo.mode = info.mode_; fileInfo.uid = info.uid_; fileInfo.gid = info.gid_; - debugMeta.fileInfos.insert(std::pair{name, fileInfo}); + debugMeta.fileInfos.insert(std::pair{ name, fileInfo }); } MetaDataManager::GetInstance().SaveMeta(metaData.GetDebugInfoKey(), debugMeta, true); return RDB_OK; @@ -1803,7 +1856,7 @@ int32_t RdbServiceImpl::VerifyPromiseInfo(const RdbSyncerParam ¶m) bool isPromise = std::any_of(localMeta.promiseInfo.permissionNames.begin(), localMeta.promiseInfo.permissionNames.end(), [tokenId](const std::string &permissionName) { return PermitDelegate::VerifyPermission(permissionName, tokenId); - }); + }); if (tokenIdRet == localMeta.promiseInfo.tokenIds.end() && uidRet == localMeta.promiseInfo.uids.end() && !isPromise) { return RDB_ERROR; @@ -1833,8 +1886,7 @@ std::string RdbServiceImpl::GetSubUser(const int32_t subUser) return userId; } -bool RdbServiceImpl::TryUpdateDeviceId(const RdbSyncerParam ¶m, const StoreMetaData &oldMeta, - StoreMetaData &meta) +bool RdbServiceImpl::TryUpdateDeviceId(const RdbSyncerParam ¶m, const StoreMetaData &oldMeta, StoreMetaData &meta) { StoreMetaData syncMeta; if (oldMeta.isNeedUpdateDeviceId && oldMeta.storeType >= StoreMetaData::StoreType::STORE_RELATIONAL_BEGIN && diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 6db6dfa4b0a31a6556a5bb8a1525fa40125a1f0d..ea3b9aa74a2ff47fdf3deb3f6a1a748536f4df3b 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "cloud/cloud_event.h" #include "commonevent/data_change_event.h" @@ -28,6 +29,7 @@ #include "feature/static_acts.h" #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" +#include "process_communicator_impl.h" #include "rdb_notifier_proxy.h" #include "rdb_query.h" #include "rdb_service_stub.h" @@ -38,7 +40,6 @@ #include "store/general_value.h" #include "store_observer.h" #include "visibility.h" -#include "process_communicator_impl.h" namespace OHOS::DistributedRdb { using namespace OHOS::AppDistributedKv; @@ -180,15 +181,17 @@ private: const AsyncDetail &async); void DoCompensateSync(const DistributedData::BindEvent& event); + + void SaveAutoSyncDeviceId(const StoreMetaData &meta, const std::vector &devices); int DoSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, const AsyncDetail &async); - int DoAutoSync( - const std::vector &devices, const Database &dataBase, std::vector tableNames); + int DoAutoSync(const std::vector &devices, const StoreMetaData &storeMetaData, + const std::vector &tables); std::vector GetReuseDevice(const std::vector &devices, const StoreMetaData &metaData); - int DoOnlineSync(const std::vector &devices, const Database &dataBase); + int DoOnlineSync(const std::string &device, const Database &dataBase); int DoDataChangeSync(const StoreInfo &storeInfo, const RdbChangedData &rdbChangedData); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 3b8f38bffd994fe8c09ab2e9d2a9c669cf26ba0c..810c25bb82c3c4c702d5c152b993afd1847c7673 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1328,12 +1328,12 @@ ohos_unittest("DataShareServiceImplMockTest") { "${data_service_path}/service/common/xcollie.cpp", "${data_service_path}/service/config/src/model/app_access_check_config.cpp", "${data_service_path}/service/config/src/model/app_id_mapping_config.cpp", + "${data_service_path}/service/config/src/model/auto_sync_app_config.cpp", "${data_service_path}/service/config/src/model/backup_config.cpp", "${data_service_path}/service/config/src/model/checker_config.cpp", "${data_service_path}/service/config/src/model/cloud_config.cpp", "${data_service_path}/service/config/src/model/component_config.cpp", "${data_service_path}/service/config/src/model/datashare_config.cpp", - "${data_service_path}/service/config/src/model/device_sync_app_white_list_config.cpp", "${data_service_path}/service/config/src/model/directory_config.cpp", "${data_service_path}/service/config/src/model/global_config.cpp", "${data_service_path}/service/config/src/model/network_config.cpp", @@ -2139,12 +2139,12 @@ ohos_unittest("BootStrapMockTest") { "${data_service_path}/service/bootstrap/src/bootstrap.cpp", "${data_service_path}/service/config/src/model/app_access_check_config.cpp", "${data_service_path}/service/config/src/model/app_id_mapping_config.cpp", + "${data_service_path}/service/config/src/model/auto_sync_app_config.cpp", "${data_service_path}/service/config/src/model/backup_config.cpp", "${data_service_path}/service/config/src/model/checker_config.cpp", "${data_service_path}/service/config/src/model/cloud_config.cpp", "${data_service_path}/service/config/src/model/component_config.cpp", "${data_service_path}/service/config/src/model/datashare_config.cpp", - "${data_service_path}/service/config/src/model/device_sync_app_white_list_config.cpp", "${data_service_path}/service/config/src/model/directory_config.cpp", "${data_service_path}/service/config/src/model/global_config.cpp", "${data_service_path}/service/config/src/model/network_config.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn index 6e09939e73e7604e5e485e5dd8ae57965cb2430a..0d63875b560cef4b809f3ffb74e865e5d411d36a 100644 --- a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn @@ -48,6 +48,7 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { ] sources = [ + "${data_service_path}/service/config/src/model/auto_sync_app_config.cpp", "${data_service_path}/app/src/checker/bundle_checker.cpp", "${data_service_path}/app/src/checker/system_checker.cpp", "${data_service_path}/service/backup/src/backup_manager.cpp", @@ -61,7 +62,6 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/service/config/src/model/cloud_config.cpp", "${data_service_path}/service/config/src/model/component_config.cpp", "${data_service_path}/service/config/src/model/datashare_config.cpp", - "${data_service_path}/service/config/src/model/device_sync_app_white_list_config.cpp", "${data_service_path}/service/config/src/model/directory_config.cpp", "${data_service_path}/service/config/src/model/global_config.cpp", "${data_service_path}/service/config/src/model/network_config.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn index e1185088f9651ab38a12105a0e9caad50eb5393e..f5c048957c04a77ae2f0c29997886210c0a68c74 100755 --- a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn @@ -45,6 +45,7 @@ ohos_fuzztest("ObjectServiceStubFuzzTest") { ] sources = [ + "${data_service_path}/service/config/src/model/auto_sync_app_config.cpp", "${data_service_path}/service/backup/src/backup_manager.cpp", "${data_service_path}/service/bootstrap/src/bootstrap.cpp", "${data_service_path}/service/common/common_types_utils.cpp", @@ -57,7 +58,6 @@ ohos_fuzztest("ObjectServiceStubFuzzTest") { "${data_service_path}/service/config/src/model/cloud_config.cpp", "${data_service_path}/service/config/src/model/component_config.cpp", "${data_service_path}/service/config/src/model/datashare_config.cpp", - "${data_service_path}/service/config/src/model/device_sync_app_white_list_config.cpp", "${data_service_path}/service/config/src/model/directory_config.cpp", "${data_service_path}/service/config/src/model/global_config.cpp", "${data_service_path}/service/config/src/model/network_config.cpp", diff --git a/services/distributeddataservice/service/test/rdb_service_impl_test.cpp b/services/distributeddataservice/service/test/rdb_service_impl_test.cpp index f144db0ff33f5d7a7eaf7b850f10085b2a48ac81..fe4501ad8f77fe9b840dc79afef30f6f02370c06 100644 --- a/services/distributeddataservice/service/test/rdb_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/rdb_service_impl_test.cpp @@ -640,10 +640,10 @@ HWTEST_F(RdbServiceImplTest, DoAutoSync001, TestSize.Level0) { RdbServiceImpl service; std::vector devices = {"device1"}; - DistributedData::Database dataBase; + StoreMetaData meta; std::vector tables = {"table1"}; - auto result = service.DoAutoSync(devices, dataBase, tables); + auto result = service.DoAutoSync(devices, meta, tables); EXPECT_EQ(result, RDB_ERROR); } @@ -662,13 +662,14 @@ HWTEST_F(RdbServiceImplTest, DoAutoSync002, TestSize.Level0) metaMapping.devicePath = "path1"; EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true); RdbServiceImpl service; + StoreMetaData meta; std::vector devices = {"device1"}; DistributedData::Database dataBase; std::vector tables = {"table1"}; - dataBase.bundleName = "bundleName"; - dataBase.name = "storeName"; - dataBase.user = "100"; - auto result = service.DoAutoSync(devices, dataBase, tables); + meta.bundleName = "bundleName"; + meta.storeId= "storeName"; + meta.user = "100"; + auto result = service.DoAutoSync(devices, meta, tables); EXPECT_EQ(result, RDB_ERROR); } @@ -682,7 +683,6 @@ HWTEST_F(RdbServiceImplTest, DoAutoSync002, TestSize.Level0) HWTEST_F(RdbServiceImplTest, DoOnlineSync001, TestSize.Level0) { RdbServiceImpl service; - std::vector devices = {"device1"}; DistributedData::Database dataBase; dataBase.name = TEST_STORE; @@ -695,7 +695,7 @@ HWTEST_F(RdbServiceImplTest, DoOnlineSync001, TestSize.Level0) dataBase.tables = {table1, table2}; - auto result = service.DoOnlineSync(devices, dataBase); + auto result = service.DoOnlineSync("device1", dataBase); EXPECT_EQ(result, RDB_ERROR); }