diff --git a/bundle.json b/bundle.json index 8b898570193d2f7d3acd3bb396f20d8c793bb717..5e2cb48184139c95dc0f29fdf0712c2bfe7bbde0 100644 --- a/bundle.json +++ b/bundle.json @@ -109,7 +109,8 @@ "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/rust/extension:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:build_module", "//foundation/distributeddatamgr/datamgr_service/conf:build_module", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share:build_module" + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share:build_module", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/object/asset_loader:asset_sync_manager" ], "inner_kits": [ { diff --git a/services/distributeddataservice/service/object/BUILD.gn b/services/distributeddataservice/service/object/BUILD.gn index b5bba90befa02cfc57ac579e272d1f9ac8a0f8e0..e1e6386d3370a2dd7d155ca5608aae6293341429 100644 --- a/services/distributeddataservice/service/object/BUILD.gn +++ b/services/distributeddataservice/service/object/BUILD.gn @@ -52,7 +52,10 @@ ohos_source_set("distributeddata_object") { "-Oz", ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "asset_loader", + ] configs = [ ":object_public_config" ] @@ -73,12 +76,12 @@ ohos_source_set("distributeddata_object") { external_deps = [ "access_token:libtokenid_sdk", "data_object:data_object_inner", - "dfs_service:cloudsync_asset_kit_inner", + "device_manager:devicemanagersdk", "dfs_service:distributed_file_daemon_kit_inner", "dmsfwk:distributed_sdk", "hilog:libhilog", - "ipc:ipc_single", "hisysevent:libhisysevent", + "ipc:ipc_single", "kv_store:datamgr_common", "kv_store:distributeddb", ] diff --git a/services/distributeddataservice/service/object/asset_loader/BUILD.gn b/services/distributeddataservice/service/object/asset_loader/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1823986e7fd04813e6c0b66123389b2fed2fb5f5 --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/BUILD.gn @@ -0,0 +1,62 @@ +# 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. +import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") + +config("object_public_config") { + visibility = [ ":*" ] +} + +ohos_shared_library("asset_sync_manager") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + boundary_sanitize = true + ubsan = true + } + + sources = [ "asset_sync_manager.cpp" ] + + cflags_cc = [ + "-fvisibility=hidden", + "-Oz", + ] + + include_dirs = [ + "../include", + "${data_service_path}/adapter/include/utils", + ] + + configs = [ ":object_public_config" ] + + cflags = [ + "-Werror", + "-Wno-multichar", + "-Wno-c99-designator", + "-D_LIBCPP_HAS_COND_CLOCKWAIT", + "-Oz", + ] + + deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] + + external_deps = [ + "dfs_service:cloudsync_asset_kit_inner", + "hilog:libhilog", + "kv_store:datamgr_common", + ] + + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" +} diff --git a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf22ce08cc10e2c6a5085161bf88841f0fb5d600 --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp @@ -0,0 +1,63 @@ +/* + * 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 "AssetSyncManager" +#include "asset_sync_manager.h" + +#include + +#include "block_data.h" +#include "cloud_sync_asset_manager.h" +#include "log_print.h" +#include "object_common.h" +#include "utils/anonymous.h" +#include "visibility.h" + +namespace OHOS::DistributedObject { +using namespace OHOS::FileManagement::CloudSync; +using namespace OHOS::DistributedData; +static constexpr int WAIT_TIME = 60; +bool AssetSyncManager::Transfer(const int32_t userId, const std::string &bundleName, const std::string &deviceId, + const DistributedData::Asset &asset) +{ + AssetInfo assetInfo; + assetInfo.uri = asset.uri; + assetInfo.assetName = asset.name; + ZLOGI("Start transfer, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), + Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); + auto block = std::make_shared>>(WAIT_TIME, std::tuple{ true, OBJECT_SUCCESS }); + auto res = CloudSyncAssetManager::GetInstance().DownloadFile(userId, bundleName, deviceId, assetInfo, + [block](const std::string &uri, int32_t status) { block->SetValue({ false, status }); }); + if (res != OBJECT_SUCCESS) { + ZLOGE("fail, res: %{public}d, name: %{public}s, deviceId: %{public}s, bundleName: %{public}s", res, + Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str(), bundleName.c_str()); + return false; + } + auto [timeout, status] = block->GetValue(); + if (timeout || status != OBJECT_SUCCESS) { + ZLOGE("fail, timeout: %{public}d, status: %{public}d, name: %{public}s, deviceId: %{public}s ", timeout, + status, Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str()); + return false; + } + ZLOGD("Transfer end, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), + Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); + return true; +} +} // namespace OHOS::DistributedObject + +API_EXPORT std::shared_ptr Create() asm("CreateAssetSyncManager"); +std::shared_ptr Create() +{ + return std::make_shared(); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..e034e9876899c1d222c63c804587981f12d15fce --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h @@ -0,0 +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 DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_ASSET_SYNC_MANAGER_H +#define DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_ASSET_SYNC_MANAGER_H + +#include "i_asset_sync_manager.h" + +namespace OHOS { +namespace DistributedObject { +class AssetSyncManager : public IAssetSyncManager { +public: + bool Transfer(const int32_t userId, const std::string &bundleName, const std::string &deviceId, + const DistributedData::Asset &asset) override; +}; +} // namespace DistributedObject +} // namespace OHOS +#endif // DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_ASSET_SYNC_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h b/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..d32e89bf056873b8458974cc3f629bfc7441add8 --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h @@ -0,0 +1,33 @@ +/* + * 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_SERVICE_OBJECT_ASSET_LOADER_I_ASSET_SYNC_MANAGER_H +#define DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_I_ASSET_SYNC_MANAGER_H + +#include +#include + +#include "store/general_value.h" + +namespace OHOS { +namespace DistributedObject { +class IAssetSyncManager { +public: + virtual ~IAssetSyncManager() {}; + virtual bool Transfer(const int32_t userId, const std::string &bundleName, const std::string &deviceId, + const DistributedData::Asset &asset) = 0; +}; +} // namespace DistributedObject +} // namespace OHOS +#endif // DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_I_ASSET_SYNC_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/object/include/object_asset_loader.h b/services/distributeddataservice/service/object/include/object_asset_loader.h index 160539fafb77d9cbdf4aa21ddc7c6c2470b82d14..f8863f212deddcb940d038ac884103882b85e9fd 100644 --- a/services/distributeddataservice/service/object/include/object_asset_loader.h +++ b/services/distributeddataservice/service/object/include/object_asset_loader.h @@ -16,12 +16,14 @@ #define DISTRIBUTEDDATAMGR_OBJECT_ASSET_LOADER_H #include +#include + +#include "asset/asset_send_callback_stub.h" +#include "concurrent_map.h" #include "executor_pool.h" +#include "i_asset_sync_manager.h" #include "object_types.h" #include "store/general_value.h" -#include "concurrent_map.h" -#include -#include "asset/asset_send_callback_stub.h" namespace OHOS { namespace DistributedObject { using AssetObj = Storage::DistributedFile::AssetObj; @@ -58,7 +60,7 @@ private: bool IsDownloading(const DistributedData::Asset& asset); bool IsDownloaded(const DistributedData::Asset& asset); void UpdateDownloaded(const DistributedData::Asset& asset); - static constexpr int WAIT_TIME = 60; + std::shared_ptr GetAssetSyncManager(); static constexpr int LAST_DOWNLOAD_ASSET_SIZE = 200; std::shared_ptr executors_; std::mutex mutex_; @@ -67,6 +69,8 @@ private: ConcurrentMap tasks_; ConcurrentMap downloaded_; ConcurrentMap downloading_; + std::mutex assetSyncMutex_; + std::shared_ptr assetSyncManager_ = nullptr; }; } // namespace DistributedObject } // namespace OHOS diff --git a/services/distributeddataservice/service/object/src/object_asset_loader.cpp b/services/distributeddataservice/service/object/src/object_asset_loader.cpp index efabc95ba69400754d3fd6ab6e30816568d720d8..22250ca7f121af0ce3ba7d6417d2217bd14e415f 100644 --- a/services/distributeddataservice/service/object/src/object_asset_loader.cpp +++ b/services/distributeddataservice/service/object/src/object_asset_loader.cpp @@ -16,15 +16,14 @@ #include "object_asset_loader.h" #include "block_data.h" -#include "cloud_sync_asset_manager.h" #include "log_print.h" #include "object_common.h" #include "utils/anonymous.h" #include "object_radar_reporter.h" #include "distributed_file_daemon_manager.h" namespace OHOS::DistributedObject { -using namespace OHOS::FileManagement::CloudSync; using namespace OHOS::DistributedData; +using Creater = std::shared_ptr (*)(); ObjectAssetLoader &ObjectAssetLoader::GetInstance() { static ObjectAssetLoader loader; @@ -39,30 +38,12 @@ void ObjectAssetLoader::SetThreadPool(std::shared_ptr executors) bool ObjectAssetLoader::Transfer(const int32_t userId, const std::string& bundleName, const std::string& deviceId, const DistributedData::Asset& asset) { - AssetInfo assetInfo; - assetInfo.uri = asset.uri; - assetInfo.assetName = asset.name; - ZLOGI("Start transfer, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), - Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); - auto block = std::make_shared>>(WAIT_TIME, std::tuple{ true, OBJECT_SUCCESS }); - auto res = CloudSyncAssetManager::GetInstance().DownloadFile(userId, bundleName, deviceId, assetInfo, - [block](const std::string& uri, int32_t status) { - block->SetValue({ false, status }); - }); - if (res != OBJECT_SUCCESS) { - ZLOGE("fail, res: %{public}d, name: %{public}s, deviceId: %{public}s, bundleName: %{public}s", res, - Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str(), bundleName.c_str()); + auto syncManager = GetAssetSyncManager(); + if (syncManager == nullptr) { + ZLOGW("syncManager is nullptr"); return false; } - auto [timeout, status] = block->GetValue(); - if (timeout || status != OBJECT_SUCCESS) { - ZLOGE("fail, timeout: %{public}d, status: %{public}d, name: %{public}s, deviceId: %{public}s ", timeout, - status, Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str()); - return false; - } - ZLOGD("Transfer end, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), - Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); - return true; + return syncManager->Transfer(userId, bundleName, deviceId, asset); } void ObjectAssetLoader::TransferAssetsAsync(const int32_t userId, const std::string& bundleName, @@ -189,6 +170,26 @@ int32_t ObjectAssetLoader::PushAsset(int32_t userId, const sptr &asset return status; } +std::shared_ptr ObjectAssetLoader::GetAssetSyncManager() +{ + std::lock_guard lock(assetSyncMutex_); + if (assetSyncManager_ != nullptr) { + return assetSyncManager_; + } + void *handler = dlopen("libasset_sync_manager.z.so", RTLD_LAZY); + if (handler == nullptr) { + ZLOGW("dlopen failed"); + return nullptr; + } + auto creator = reinterpret_cast(dlsym(handler, "CreateAssetSyncManager")); + if (creator == nullptr) { + ZLOGE("dlsym failed, %{public}s", dlerror()); + return nullptr; + } + assetSyncManager_ = creator(); + return assetSyncManager_; +} + int32_t ObjectAssetsSendListener::OnSendResult(const sptr &assetObj, int32_t result) { if (assetObj == nullptr) { diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 3b8f38bffd994fe8c09ab2e9d2a9c669cf26ba0c..d8c854626cf33e895bb3be64b93652e09a9e346c 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -40,6 +40,7 @@ config("module_private_config") { "${data_service_path}/service/matrix/include/", "${data_service_path}/service/kvdb", "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", "${data_service_path}/service/permission/include", "${data_service_path}/service/rdb/", "${data_service_path}/service/test/mock", @@ -789,6 +790,7 @@ ohos_unittest("ObjectAssetLoaderTest") { "../object/src/object_asset_loader.cpp", "../object/src/object_asset_machine.cpp", "../object/src/object_snapshot.cpp", + "../object/asset_loader/asset_sync_manager.cpp", "object_asset_loader_test.cpp", ] diff --git a/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn index 8d654ce30dbfb53d27aa58e48d760a2358a10283..a04639664e51721d65ad94ceba2a701588ba0904 100644 --- a/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn @@ -21,6 +21,7 @@ ohos_fuzztest("ObjectServiceImp1FuzzTest") { include_dirs = [ "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", ] fuzz_config_file = diff --git a/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn index 57ebaece81c81df0660dd472bfe8eafd4f0bdc58..62e3d4b438e7949c50ec066cdc6b4b347599ffc1 100644 --- a/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn @@ -21,6 +21,7 @@ ohos_fuzztest("ObjectServiceImp2FuzzTest") { include_dirs = [ "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", ] fuzz_config_file = diff --git a/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn index 46a6be379d214b4d608b9a3dea5c2f72be7c5ae5..1e04771ccbd5b30250b472f78a06ed7a90200046 100644 --- a/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn @@ -21,6 +21,7 @@ ohos_fuzztest("ObjectServiceImp3FuzzTest") { include_dirs = [ "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", ] fuzz_config_file = diff --git a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn index e1185088f9651ab38a12105a0e9caad50eb5393e..b899b86e2ae15f9982378d5ee5fcdbbd07f266b8 100755 --- a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn @@ -29,6 +29,7 @@ ohos_fuzztest("ObjectServiceStubFuzzTest") { "${data_service_path}/service/config/include", "${data_service_path}/service/crypto/include", "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", "${data_service_path}/service/matrix/include", "${data_service_path}/adapter/include/communicator", "${data_service_path}/adapter/include/utils", diff --git a/services/distributeddataservice/service/test/object_asset_loader_test.cpp b/services/distributeddataservice/service/test/object_asset_loader_test.cpp index dc5188fa0433ebf9318307506b1b256502ef3574..3af346eaad1cba965a291ca47265d485b1f8724e 100644 --- a/services/distributeddataservice/service/test/object_asset_loader_test.cpp +++ b/services/distributeddataservice/service/test/object_asset_loader_test.cpp @@ -22,6 +22,7 @@ #include "executor_pool.h" #include "object_common.h" #include "snapshot/machine_status.h" +#include "asset_sync_manager.h" using namespace testing::ext; using namespace OHOS::DistributedObject; @@ -264,4 +265,16 @@ HWTEST_F(ObjectAssetLoaderTest, OnSendResult001, TestSize.Level1) ret = sendCallback->OnSendResult(assetObj, result); EXPECT_EQ(ret, result); } + +/** +* @tc.name: Transfer001 +* @tc.desc: asset sync manager test. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectAssetLoaderTest, Transfer001, TestSize.Level1) +{ + AssetSyncManager assetSyncManager; + auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_EQ(result, false); +} } // namespace OHOS::Test