diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 344f08426e82264a07c93bb58f10960859853b68..a1ba24c673c5f1e5afa188435adda31e5515fbbd 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -92,6 +92,7 @@ ohos_shared_library("distributeddatasvcfwk") { "metadata/matrix_meta_data.cpp", "metadata/meta_data.cpp", "metadata/meta_data_manager.cpp", + "metadata/object_user_meta_data.cpp", "metadata/secret_key_meta_data.cpp", "metadata/store_debug_info.cpp", "metadata/store_meta_data.cpp", diff --git a/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h b/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h new file mode 100644 index 0000000000000000000000000000000000000000..cd2dd38e44135a2569b93ff5821c623d3c014e77 --- /dev/null +++ b/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h @@ -0,0 +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. + */ + +#ifndef DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H +#define DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H + +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +class API_EXPORT ObjectUserMetaData final : public Serializable { +public: + std::string userId; + + API_EXPORT bool Marshal(json &node) const override; + API_EXPORT bool Unmarshal(const json &node) override; + API_EXPORT ObjectUserMetaData(); + + API_EXPORT static std::string GetKey(); + +private: + static constexpr const char *KEY_PREFIX = "ObjectUserMetaData"; +}; +} // namespace OHOS::DistributedData +#endif // DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5d5d9e073ae8cd22c4799b1ca85bd8eeabf340f --- /dev/null +++ b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp @@ -0,0 +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. + */ +#include "metadata/object_user_meta_data.h" + +namespace OHOS::DistributedData { +ObjectUserMetaData::ObjectUserMetaData() +{ +} +bool ObjectUserMetaData::Marshal(json &node) const +{ + return SetValue(node[GET_NAME(userId)], userId); +} + +bool ObjectUserMetaData::Unmarshal(const json &node) +{ + return GetValue(node, GET_NAME(userId), userId); +} + +std::string ObjectUserMetaData::GetKey() +{ + return KEY_PREFIX; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/framework/test/meta_data_test.cpp b/services/distributeddataservice/framework/test/meta_data_test.cpp index 952990503b4304dce12d361fe9acd52980f6b0e7..58359051ec6464c24a2a9c25a60d52f552f63bca 100644 --- a/services/distributeddataservice/framework/test/meta_data_test.cpp +++ b/services/distributeddataservice/framework/test/meta_data_test.cpp @@ -25,6 +25,7 @@ #include "metadata/matrix_meta_data.h" #include "metadata/meta_data.h" #include "metadata/meta_data_manager.h" +#include "metadata/object_user_meta_data.h" #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" @@ -994,4 +995,17 @@ HWTEST_F(ServiceMetaDataTest, GetKey, TestSize.Level1) std::string prefix = metaData.GetKey(); EXPECT_EQ(prefix, expectedPrefix); } + +/** +* @tc.name: ObjectUserMetaDataGetKey +* @tc.desc: ObjectUserMetaDataGetKey +* @tc.type: FUNC +*/ +HWTEST_F(ServiceMetaDataTest, ObjectUserMetaDataGetKey, TestSize.Level1) +{ + ObjectUserMetaData metaData; + std::string expectedPrefix = "ObjectUserMetaData"; + std::string prefix = metaData.GetKey(); + EXPECT_EQ(prefix, expectedPrefix); +} } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index 8fdaea70822984a0457bd80f2b64e1d2fc2d3747..158b5c50a01490a53088d2e1f63dc64cc64e144c 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -89,6 +89,7 @@ public: sptr callback, uint32_t tokenId); void SetData(const std::string &dataDir, const std::string &userId); int32_t Clear(); + int32_t InitUserMeta(); int32_t DeleteByAppId(const std::string &appId, int32_t user); void RegisterRemoteCallback(const std::string &bundleName, const std::string &sessionId, pid_t pid, uint32_t tokenId, diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index c86d28e1bf4bab1213499e0eb1b6f13c7ce05b88..51635cf04016c7d369f3f105499797f29c7460c2 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -29,6 +29,7 @@ #include "kvstore_utils.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "metadata/object_user_meta_data.h" #include "metadata/store_meta_data.h" #include "object_dms_handler.h" #include "object_radar_reporter.h" @@ -291,29 +292,25 @@ int32_t ObjectStoreManager::Retrieve( int32_t ObjectStoreManager::Clear() { ZLOGI("enter"); + DistributedData::ObjectUserMetaData userMeta; + if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { + ZLOGE("load meta error"); + return OBJECT_INNER_ERROR; + } + if (userMeta.userId.empty()) { + ZLOGI("no object user meta, don't need clean"); + return OBJECT_SUCCESS; + } std::string userId = GetCurrentUser(); if (userId.empty()) { + ZLOGE("no user error"); return OBJECT_INNER_ERROR; } - std::vector metaData; - std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - std::string metaKey = GetMetaUserIdKey(userId, appId); - if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(metaKey, metaData, true)) { - ZLOGE("no store of %{public}s", appId.c_str()); - return OBJECT_STORE_NOT_FOUND; - } - for (const auto &storeMeta : metaData) { - if (storeMeta.storeType < StoreMetaData::StoreType::STORE_OBJECT_BEGIN - || storeMeta.storeType > StoreMetaData::StoreType::STORE_OBJECT_END) { - continue; - } - if (storeMeta.user == userId) { - ZLOGI("user is same, not need to change, mate user:%{public}s::user:%{public}s.", - storeMeta.user.c_str(), userId.c_str()); - return OBJECT_SUCCESS; - } + if (userMeta.userId == userId) { + ZLOGI("user is same, don't need clear, user:%{public}s.", userId.c_str()); + return OBJECT_SUCCESS; } - ZLOGI("user changed, need to clear, userId:%{public}s", userId.c_str()); + ZLOGI("user changed, need clear, userId:%{public}s", userId.c_str()); int32_t result = Open(); if (result != OBJECT_SUCCESS) { ZLOGE("Open failed, errCode = %{public}d", result); @@ -324,6 +321,33 @@ int32_t ObjectStoreManager::Clear() return result; } +int32_t ObjectStoreManager::InitUserMeta() +{ + ObjectUserMetaData userMeta; + if (DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { + ZLOGI("userId has been set, don't need clean"); + return OBJECT_SUCCESS; + } + std::string userId = GetCurrentUser(); + if (userId.empty()) { + ZLOGI("get userId error"); + return OBJECT_INNER_ERROR; + } + userMeta.userId = userId; + std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + std::string metaKey = GetMetaUserIdKey(userId, appId); + if (!DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true)) { + ZLOGE("delete old meta error, userId:%{public}s", userId.c_str()); + return OBJECT_INNER_ERROR; + } + if (!DistributedData::MetaDataManager::GetInstance().SaveMeta(DistributedData::ObjectUserMetaData::GetKey(), + userMeta, true)) { + ZLOGE("save meta error, userId:%{public}s", userId.c_str()); + return OBJECT_INNER_ERROR; + } + return OBJECT_SUCCESS; +} + int32_t ObjectStoreManager::DeleteByAppId(const std::string &appId, int32_t user) { int32_t result = Open(); @@ -338,12 +362,6 @@ int32_t ObjectStoreManager::DeleteByAppId(const std::string &appId, int32_t user appId.c_str(), user); } Close(); - std::string userId = std::to_string(user); - std::string metaKey = GetMetaUserIdKey(userId, appId); - auto status = DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true); - if (!status) { - ZLOGE("Delete meta failed, userId: %{public}s, appId: %{public}s", userId.c_str(), appId.c_str()); - } return result; } @@ -1085,8 +1103,8 @@ std::vector ObjectStoreManager::SplitEntryKey(const std::string &ke std::string ObjectStoreManager::GetCurrentUser() { std::vector users; - AccountDelegate::GetInstance()->QueryUsers(users); - if (users.empty()) { + if (!AccountDelegate::GetInstance()->QueryUsers(users)) { + ZLOGE("QueryUsers failed."); return ""; } return std::to_string(users[0]); @@ -1100,20 +1118,20 @@ void ObjectStoreManager::SaveUserToMeta() return; } std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - StoreMetaData userMeta; - userMeta.storeId = DistributedObject::ObjectCommon::OBJECTSTORE_DB_STOREID; - userMeta.user = userId; - userMeta.storeType = ObjectDistributedType::OBJECT_SINGLE_VERSION; - std::string userMetaKey = GetMetaUserIdKey(userId, appId); - auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta(userMetaKey, userMeta, true); + DistributedData::ObjectUserMetaData userMeta; + userMeta.userId = userId; + auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta( + DistributedData::ObjectUserMetaData::GetKey(), userMeta, true); if (!saved) { - ZLOGE("userMeta save failed"); + ZLOGE("userMeta save failed, userId:%{public}s", userId.c_str()); } } void ObjectStoreManager::CloseAfterMinute() { - executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&ObjectStoreManager::Close, this)); + executors_->Schedule(std::chrono::minutes(INTERVAL), [this]() { + Close(); + }); } void ObjectStoreManager::SetThreadPool(std::shared_ptr executors) diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index c09b5828667db68037b33b28aea29012f7cc6b7e..71f6195e7a2c4182f769693e996f633a94bc5875 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -139,6 +139,7 @@ int32_t ObjectServiceImpl::OnInitialize() StoreMetaData saveMeta; SaveMetaData(saveMeta, std::to_string(userId), accountId); ObjectStoreManager::GetInstance()->SetData(saveMeta.dataDir, std::to_string(userId)); + ObjectStoreManager::GetInstance()->InitUserMeta(); RegisterObjectServiceInfo(); RegisterHandler(); ObjectDmsHandler::GetInstance().RegisterDmsEvent(); @@ -382,8 +383,9 @@ void ObjectServiceImpl::RegisterObjectServiceInfo() void ObjectServiceImpl::RegisterHandler() { - Handler handler = - std::bind(&ObjectServiceImpl::DumpObjectServiceInfo, this, std::placeholders::_1, std::placeholders::_2); + Handler handler = [this](int fd, std::map> ¶ms) { + DumpObjectServiceInfo(fd, params); + }; DumpManager::GetInstance().AddHandler("FEATURE_INFO", uintptr_t(this), handler); } diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 9c0b1d91a577352a96d5a8eba8dc1d48df724044..9b1b757b39906f38630c6f64c88de3c6c3bc36d3 100755 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -828,6 +828,7 @@ ohos_unittest("ObjectDmsHandlerTest") { ohos_unittest("ObjectManagerTest") { module_out_path = module_output_path sources = [ + "${data_service_path}/app/src/kvstore_meta_manager.cpp", "${data_service_path}/service/common/common_types_utils.cpp", "${data_service_path}/service/common/value_proxy.cpp", "../object/src/object_asset_loader.cpp", @@ -849,6 +850,7 @@ ohos_unittest("ObjectManagerTest") { include_dirs = [ "${dataobject_path}/frameworks/innerkitsimpl/include", + "${data_service_path}/app/src", "${data_service_path}/service/common", "${dataobject_path}/frameworks/innerkitsimpl/include/common", "${dataobject_path}/interfaces/innerkits", @@ -864,6 +866,7 @@ ohos_unittest("ObjectManagerTest") { "access_token:libtokenid_sdk", "c_utils:utils", "data_object:distributeddataobject_impl", + "dataclassification:data_transit_mgr", "dfs_service:cloudsync_asset_kit_inner", "dfs_service:distributed_file_daemon_kit_inner", "dmsfwk:distributed_sdk", @@ -874,6 +877,7 @@ ohos_unittest("ObjectManagerTest") { "ipc:ipc_core", "json:nlohmann_json_static", "kv_store:distributeddata_inner", + "kv_store:distributeddata_mgr", "kv_store:distributeddb", "relational_store:native_rdb", ] diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index dfc4bf24a800747807cbf9df602bfda1075c930c..4250b4c64b8b6b8c32f3102783047d34c415a954 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -20,14 +20,17 @@ #include #include +#include "bootstrap.h" #include "device_manager_adapter_mock.h" #include "executor_pool.h" +#include "kvstore_meta_manager.h" #include "kv_store_nb_delegate_mock.h" #include "object_types.h" #include "snapshot/machine_status.h" using namespace testing::ext; using namespace OHOS::DistributedObject; +using namespace OHOS::DistributedData; using namespace std; using namespace testing; using AssetValue = OHOS::CommonType::AssetValue; @@ -101,6 +104,12 @@ void ObjectManagerTest::SetUpTestCase(void) { devMgrAdapterMock = make_shared(); BDeviceManagerAdapter::deviceManagerAdapter = devMgrAdapterMock; + std::shared_ptr executors = std::make_shared(1, 0); + Bootstrap::GetInstance().LoadDirectory(); + Bootstrap::GetInstance().LoadCheckers(); + DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors); + DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter(); + DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener(); } void ObjectManagerTest::TearDownTestCase(void) @@ -184,7 +193,7 @@ HWTEST_F(ObjectManagerTest, Clear001, TestSize.Level0) { auto manager = ObjectStoreManager::GetInstance(); auto result = manager->Clear(); - ASSERT_EQ(result, OHOS::DistributedObject::OBJECT_STORE_NOT_FOUND); + ASSERT_EQ(result, OHOS::DistributedObject::OBJECT_SUCCESS); } /** @@ -1086,4 +1095,16 @@ HWTEST_F(ObjectManagerTest, GetObjectData002, TestSize.Level1) EXPECT_NE(saveInfo.bundleName, bundleName); EXPECT_FALSE(hasAsset); } + +/** +* @tc.name: InitUserMeta001 +* @tc.desc: test clear old user meta. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, InitUserMeta001, TestSize.Level1) +{ + auto manager = ObjectStoreManager::GetInstance(); + auto status = manager->InitUserMeta(); + ASSERT_EQ(status, DistributedObject::OBJECT_SUCCESS); +} } // namespace OHOS::Test diff --git a/services/distributeddataservice/service/test/object_service_impl_test.cpp b/services/distributeddataservice/service/test/object_service_impl_test.cpp index e67f493ab070d8e8f2db8e014700ac400055d52b..4a0a0b2f3cd273cd88fbc0901649e59f781ea3d2 100644 --- a/services/distributeddataservice/service/test/object_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/object_service_impl_test.cpp @@ -149,6 +149,6 @@ HWTEST_F(ObjectServiceImplTest, ResolveAutoLaunch001, TestSize.Level1) std::string identifier = "identifier"; std::shared_ptr objectServiceImpl = std::make_shared(); int32_t ret = objectServiceImpl->ResolveAutoLaunch(identifier, param); - EXPECT_EQ(ret, OBJECT_STORE_NOT_FOUND); + EXPECT_EQ(ret, OBJECT_SUCCESS); } } \ No newline at end of file