diff --git a/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp b/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp index fb05fa24524b6ff073dd911ce856546bdcc6b50e..a6d27a66da453c002cb4313bf31c32b5e8cfaf64 100644 --- a/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp +++ b/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp @@ -47,7 +47,7 @@ void ServiceReverse::IncrementalBackupOnResultReport(std::string result, std::st HILOGI("Error scenario or callback is nullptr"); return; } - callbacksIncrementalBackup_.onResultReport(result); + callbacksIncrementalBackup_.onResultReport(bundleName, result); } void ServiceReverse::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) @@ -116,7 +116,7 @@ void ServiceReverse::IncrementalRestoreOnResultReport(std::string result, std::s HILOGI("Error scenario or callback is nullptr"); return; } - callbacksIncrementalRestore_.onResultReport(result); + callbacksIncrementalRestore_.onResultReport(bundleName, result); if (scenario_ != Scenario::RESTORE || !callbacksIncrementalRestore_.onBundleFinished) { HILOGI("Error scenario or callback is nullptr"); return; diff --git a/frameworks/native/backup_kit_inner/src/service_reverse.cpp b/frameworks/native/backup_kit_inner/src/service_reverse.cpp index 18f0510e9e6f6079e425a35c8e16416df46a8062..98942bd6ba6fd0edc66e47ee8d77ec6670e3a3f0 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse.cpp @@ -46,7 +46,7 @@ void ServiceReverse::BackupOnResultReport(std::string result, std::string bundle HILOGI("Error scenario or callback is nullptr"); return; } - callbacksBackup_.onResultReport(result); + callbacksBackup_.onResultReport(bundleName, result); } void ServiceReverse::BackupOnBundleFinished(int32_t errCode, string bundleName) @@ -116,7 +116,7 @@ void ServiceReverse::RestoreOnResultReport(string result, std::string bundleName HILOGI("Error scenario or callback is nullptr"); return; } - callbacksRestore_.onResultReport(result); + callbacksRestore_.onResultReport(bundleName, result); if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onBundleFinished) { HILOGI("Error scenario or callback is nullptr"); return; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h index 7f3eade3eeec1cafd03c63911fa839ccfcf056e1..f37aa190e9b9fc802ee1d7d6f11db5d092e020dc 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h @@ -35,7 +35,7 @@ public: std::function onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个备份流程结束或意外中止时执行的回调函数 - std::function onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数 + std::function onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h index 6606c87273f907ec3e2b25e060afc75beee8557d..54b3a25b16ef5b18ca1a56511184024ebc2565b9 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h @@ -35,7 +35,7 @@ public: std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 - std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h index 3cec4806bf5cc9ff1b82829b31b27f0f8ae2c09c..8405b9b88aa67fa16d707b336eb6ca0a2e902dcb 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h @@ -37,7 +37,7 @@ public: std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 - std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h index 68f56264edde23dd3c3dccb9b562d65a4686cf19..46a6e24b1be69d4be5433a7b94e012190ea4ae66 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h @@ -33,7 +33,7 @@ public: std::function onBundleStarted; // 当启动某个应用的备份流程结束时执行的回调函数 std::function onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个备份流程结束或意外中止时执行的回调函数 - std::function onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数 + std::function onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h index ae916947471f647cfb35d0c59d07a166fb8a1c0b..06e8332303fa170d02170f96cab0b6a78843015d 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h @@ -33,7 +33,7 @@ public: std::function onBundleStarted; // 当启动某个应用的恢复流程结束时执行的回调函数 std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 - std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h index 405b659e84da7ef0e4129da36db848c330202ea5..4d4cdad10cbf52f889485deaddc81c1286fc35b2 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h @@ -37,7 +37,7 @@ public: std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 - std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d1c52b8a852d4a099894ef476eb4f17888bf5380..57417adbf2f489f51b28af8caba30d08bc64c6a3 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -141,6 +141,7 @@ ohos_shared_library("backup") { ] sources = [ + "${path_backup_js}/backup/general_callbacks.cpp", "${path_backup_js}/backup/module.cpp", "${path_backup_js}/backup/prop_n_exporter.cpp", "${path_backup_js}/backup/prop_n_operation.cpp", diff --git a/interfaces/kits/js/backup/general_callbacks.cpp b/interfaces/kits/js/backup/general_callbacks.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed667125e06ea9644249cea205d4eb68064e40e3 --- /dev/null +++ b/interfaces/kits/js/backup/general_callbacks.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2024-2024 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 "uv.h" +#include "native_reference.h" +#include "native_value.h" +#include "b_error/b_error.h" +#include "general_callbacks.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +BackupRestoreCallback::BackupRestoreCallback(napi_env env, LibN::NVal thisPtr, LibN::NVal cb) : env_(env) +{ + ctx_ = new LibN::NAsyncContextCallback(thisPtr, cb); +} + +BackupRestoreCallback::~BackupRestoreCallback() +{ + if (!ctx_) { + return; + } + + unique_ptr ptr(ctx_); + uv_loop_s *loop = nullptr; + napi_status status = napi_get_uv_event_loop(env_, &loop); + if (status != napi_ok) { + HILOGE("Failed to get uv event loop"); + return; + } + + auto work = make_unique(); + if (work == nullptr) { + HILOGE("Failed to new uv_work_t"); + return; + } + work->data = static_cast(ctx_); + + int ret = uv_queue_work( + loop, work.get(), [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + LibN::NAsyncContextCallback *ctx = static_cast(work->data); + delete ctx; + delete work; + }); + if (ret) { + HILOGE("Failed to call uv_queue_work %{public}d", status); + return; + } + ptr.release(); + work.release(); + ctx_ = nullptr; +} + +BackupRestoreCallback::operator bool() const +{ + return bool(ctx_->cb_); +} + +static void DoCallJsMethod(napi_env env, void *data, InputArgsParser argParser) +{ + HILOGI("Start execute DoCallJsMethod"); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + if (scope == nullptr) { + HILOGE("scope is nullptr"); + return; + } + auto ctx = static_cast(data); + if (ctx == nullptr) { + HILOGE("This pointer address is empty"); + napi_close_handle_scope(env, scope); + return; + } + vector argv = {}; + if (argParser != nullptr) { + if (!argParser(env, argv)) { + HILOGE("failed to get params."); + napi_close_handle_scope(env, scope); + return; + } + } + napi_value global = nullptr; + napi_get_global(env, &global); + napi_value callback = ctx->cb_.Deref(env).val_; + napi_value result = nullptr; + napi_status status = napi_call_function(env, global, callback, argv.size(), argv.data(), &result); + if (status != napi_ok) { + HILOGE("Failed to call function for %{public}d.", status); + } + napi_close_handle_scope(env, scope); + HILOGI("End execute DoCallJsMethod"); +} + +void BackupRestoreCallback::CallJsMethod(InputArgsParser argParser) +{ + HILOGI("call BackupRestoreCallback CallJsMethod begin."); + uv_loop_s *loop = nullptr; + napi_status status = napi_get_uv_event_loop(env_, &loop); + if (status != napi_ok) { + HILOGE("failed to get uv event loop."); + return; + } + struct WorkArgs { + BackupRestoreCallback *ptr = nullptr; + InputArgsParser argParser; + }; + auto workArgs = make_unique(); + auto work = make_unique(); + if (workArgs == nullptr || work == nullptr) { + HILOGE("failed to new workArgs or uv_work_t."); + return; + } + workArgs->ptr = this; + workArgs->argParser = argParser; + work->data = reinterpret_cast(workArgs.get()); + HILOGI("Will execute current js method"); + int ret = uv_queue_work( + loop, work.get(), [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + auto workArgs = reinterpret_cast(work->data); + do { + if (workArgs == nullptr) { + HILOGE("failed to get CallJsParam."); + break; + } + DoCallJsMethod(workArgs->ptr->env_, workArgs->ptr->ctx_, workArgs->argParser); + } while (false); + delete workArgs; + delete work; + }); + if (ret != 0) { + HILOGE("failed to exec uv_queue_work."); + workArgs.reset(); + work.reset(); + return; + } + workArgs.release(); + work.release(); + HILOGI("call BackupRestoreCallback CallJsMethod end."); +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/general_callbacks.h b/interfaces/kits/js/backup/general_callbacks.h index d5d0e1a2e9c105004325697d0495f0c00c2e8f19..04956ac668675c339e731d5df5a1c92f88c68792 100644 --- a/interfaces/kits/js/backup/general_callbacks.h +++ b/interfaces/kits/js/backup/general_callbacks.h @@ -22,6 +22,19 @@ #include "filemgmt_libn.h" namespace OHOS::FileManagement::Backup { +using InputArgsParser = std::function &)>; + +class BackupRestoreCallback { +public: + BackupRestoreCallback(napi_env env, LibN::NVal thisPtr, LibN::NVal cb); + ~BackupRestoreCallback(); + void CallJsMethod(InputArgsParser argParser); + explicit operator bool() const; +private: + napi_env env_; + LibN::NAsyncContextCallback *ctx_ = nullptr; +}; + class GeneralCallbacks { public: GeneralCallbacks(const napi_env &env, const LibN::NVal &thisPtr, const LibN::NVal &jsCallbacks) @@ -38,7 +51,7 @@ public: LibN::NAsyncWorkCallback onBundleEnd; LibN::NAsyncWorkCallback onAllBundlesEnd; LibN::NAsyncWorkCallback onBackupServiceDied; - LibN::NAsyncWorkCallback onResultReport; + BackupRestoreCallback onResultReport; }; } // namespace OHOS::FileManagement::Backup #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H \ No newline at end of file diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index e81151e53d7e1252ed99d478d6d7d4865b0206c7..06606cf569e19f2ad10780c7544a07cc2a41fb24 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -187,7 +187,8 @@ static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) callbacks->onAllBundlesEnd.ThreadSafeSchedule(cbCompl); } -static void OnResultReport(weak_ptr pCallbacks, const std::string result) +static void OnResultReport(weak_ptr pCallbacks, const std::string bundleName, + const std::string result) { HILOGI("callback function onResultReport begin"); if (pCallbacks.expired()) { @@ -203,11 +204,16 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str HILOGI("callback function onResultReport is undefined"); return; } - auto cbCompl = [res {result}](napi_env env, NError err) -> NVal { - NVal str = NVal::CreateUTF8String(env, res); - return str; + auto cbCompl = [bName {bundleName}, res {result}](napi_env env, vector &argv) -> bool { + napi_value napi_bName = nullptr; + napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName); + argv.push_back(napi_bName); + napi_value napi_res = nullptr; + napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res); + argv.push_back(napi_res); + return true; }; - callbacks->onResultReport.ThreadSafeSchedule(cbCompl); + callbacks->onResultReport.CallJsMethod(cbCompl); } static void OnBackupServiceDied(weak_ptr pCallbacks) @@ -258,7 +264,7 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info .onBundleStarted = bind(onBundleBegin, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, backupEntity->callbacks, placeholders::_1), - .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks)}); if (!backupEntity->session) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index 588373ed3ece0e640d6e6793a2c9c3c929352e15..3152cae0bc29a0a063cd44a7bcb6507e26cf3323 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -193,7 +193,8 @@ static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) callbacks->onAllBundlesEnd.ThreadSafeSchedule(cbCompl); } -static void OnResultReport(weak_ptr pCallbacks, const std::string result) +static void OnResultReport(weak_ptr pCallbacks, const std::string bundleName, + const std::string result) { HILOGI("callback function onResultReport begin"); if (pCallbacks.expired()) { @@ -209,11 +210,16 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str HILOGI("callback function onResultReport is undefined"); return; } - auto cbCompl = [res {result}](napi_env env, NError err) -> NVal { - NVal str = NVal::CreateUTF8String(env, res); - return str; + auto cbCompl = [bName {bundleName}, res {result}](napi_env env, vector &argv) -> bool { + napi_value napi_bName = nullptr; + napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName); + argv.push_back(napi_bName); + napi_value napi_res = nullptr; + napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res); + argv.push_back(napi_res); + return true; }; - callbacks->onResultReport.ThreadSafeSchedule(cbCompl); + callbacks->onResultReport.CallJsMethod(cbCompl); } static void OnBackupServiceDied(weak_ptr pCallbacks) @@ -265,7 +271,7 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal .onBundleStarted = bind(onBundleBegin, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, backupEntity->callbacks, placeholders::_1), - .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks)}); if (!backupEntity->session) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 7d4c83d921ddf4de45cfeb8f568a57ea15d6e0ed..3f175e937be877f1b2f1f9195a3e63179d6b21d7 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -262,7 +262,8 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) callbacks->onBackupServiceDied.ThreadSafeSchedule(cbCompl); } -static void OnResultReport(weak_ptr pCallbacks, const std::string result) +static void OnResultReport(weak_ptr pCallbacks, const std::string bundleName, + const std::string result) { HILOGD("callback function onResultReport begin."); if (pCallbacks.expired()) { @@ -278,11 +279,16 @@ static void OnResultReport(weak_ptr pCallbacks, const std::str HILOGI("callback function onResultReport is undefined"); return; } - auto cbCompl = [res {result}](napi_env env, NError err) -> NVal { - NVal str = NVal::CreateUTF8String(env, res); - return str; + auto cbCompl = [bName {bundleName}, res {result}](napi_env env, vector &argv) -> bool { + napi_value napi_bName = nullptr; + napi_create_string_utf8(env, bName.c_str(), bName.size(), &napi_bName); + argv.push_back(napi_bName); + napi_value napi_res = nullptr; + napi_create_string_utf8(env, res.c_str(), res.size(), &napi_res); + argv.push_back(napi_res); + return true; }; - callbacks->onResultReport.ThreadSafeSchedule(cbCompl); + callbacks->onResultReport.CallJsMethod(cbCompl); } static bool VerifyAppendBundlesParam(NFuncArg &funcArg, int32_t &fd, std::vector &bundleNames, @@ -403,7 +409,7 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info .onBundleStarted = bind(onBundleBegin, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, restoreEntity->callbacks, placeholders::_1), - .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); } else { restoreEntity->sessionSheet = nullptr; @@ -413,7 +419,7 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info .onBundleStarted = bind(onBundleBegin, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, restoreEntity->callbacks, placeholders::_1), - .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); } if (!restoreEntity->sessionWhole && !restoreEntity->sessionSheet) { diff --git a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp index 89d28b3f3986217ceb5973ba024bc2f4ce8d7eea..dd71f134c5a737fb93400b6073b8aa5b7b292854 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp @@ -87,7 +87,7 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, vectorRestoreOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by RestoreOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_RestoreOnResultReport_0101"; +} + +/** + * @tc.number: SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0100 + * @tc.desc: 测试 IncrementalRestoreOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0100"; + try { + IncrementalInit(IServiceReverse::Scenario::RESTORE); + std::string resultReport = "result_report"; + std::string bundleName = BUNDLE_NAME; + service_->IncrementalRestoreOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by IncrementalRestoreOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0100"; +} + +/** + * @tc.number: SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0101 + * @tc.name: SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0101 + * @tc.desc: 测试 IncrementalRestoreOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0101, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0101"; + try { + IncrementalInit(IServiceReverse::Scenario::RESTORE, 1); + std::string resultReport = "result_report"; + std::string bundleName = BUNDLE_NAME; + service_->IncrementalRestoreOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by IncrementalRestoreOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_IncrementalRestoreOnResultReport_0101"; +} + +/** + * @tc.number: SUB_backup_ServiceReverse_BackupOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverse_BackupOnResultReport_0100 + * @tc.desc: 测试 BackupOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_BackupOnResultReport_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_BackupOnResultReport_0100"; + try { + Init(IServiceReverse::Scenario::BACKUP); + std::string resultReport = "result_report"; + std::string bundleName = BUNDLE_NAME; + service_->BackupOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by BackupOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_BackupOnResultReport_0100"; +} + +/** + * @tc.number: SUB_backup_ServiceReverse_BackupOnResultReport_0101 + * @tc.name: SUB_backup_ServiceReverse_BackupOnResultReport_0101 + * @tc.desc: 测试 BackupOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_BackupOnResultReport_0101, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_BackupOnResultReport_0101"; + try { + Init(IServiceReverse::Scenario::BACKUP, 1); + std::string resultReport = "result_report"; + std::string bundleName = BUNDLE_NAME; + service_->BackupOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by BackupOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_BackupOnResultReport_0101"; +} + +/** + * @tc.number: SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0100 + * @tc.desc: 测试 IncrementalBackupOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0100"; + try { + IncrementalInit(IServiceReverse::Scenario::BACKUP); + std::string resultReport = "result_report"; + std::string bundleName = BUNDLE_NAME; + service_->IncrementalBackupOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by IncrementalBackupOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0100"; +} + +/** + * @tc.number: SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0101 + * @tc.name: SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0101 + * @tc.desc: 测试 IncrementalBackupOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0101, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0101"; + try { + IncrementalInit(IServiceReverse::Scenario::BACKUP, 1); + std::string resultReport = "result_report"; + std::string bundleName = BUNDLE_NAME; + service_->IncrementalBackupOnResultReport(resultReport, bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by IncrementalBackupOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_IncrementalBackupOnResultReport_0101"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_tools/backup_tool/tools_op_restore_async_test.cpp b/tests/unittests/backup_tools/backup_tool/tools_op_restore_async_test.cpp index 6cdfe706146bbf24cea13486c65d334293c9ebe7..109d465f18d8c9880b952d5b67455dfbea2e9586 100644 --- a/tests/unittests/backup_tools/backup_tool/tools_op_restore_async_test.cpp +++ b/tests/unittests/backup_tools/backup_tool/tools_op_restore_async_test.cpp @@ -326,8 +326,9 @@ HWTEST_F(ToolsOpRestoreAsyncTest, tools_op_restore_async_0700, testing::ext::Tes GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-begin tools_op_restore_async_0700"; try { auto ctx = make_shared(); + std::string bundleName = "com.example.app2backup"; std::string resultInfo = "test result info"; - OnResultReport(ctx, resultInfo); + OnResultReport(ctx, bundleName, resultInfo); EXPECT_TRUE(true); } catch (...) { EXPECT_TRUE(false); diff --git a/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp b/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp index a35d5107293800c9273862d4e1a07be8ed8dfba6..3d7220db85a358fa0c4f93d12d617b5967018527 100644 --- a/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp +++ b/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp @@ -446,8 +446,9 @@ HWTEST_F(ToolsOpRestoreTest, tools_op_restore_OOnResultReport_0800, testing::ext GTEST_LOG_(INFO) << "ToolsOpRestoreTest-begin tools_op_restore_OOnResultReport_0800"; try { auto ctx = make_shared(); + std::string bundleName = "com.example.app2backup"; std::string resultInfo = "result info"; - OnResultReport(ctx, resultInfo); + OnResultReport(ctx, bundleName, resultInfo); EXPECT_TRUE(true); } catch (...) { EXPECT_TRUE(false); diff --git a/tools/backup_tool/src/tools_op_backup.cpp b/tools/backup_tool/src/tools_op_backup.cpp index 93959f3be1d82d4061b61c0a325e3ea48a45d810..fcf892a4b20ff9e9d17e5dbf09020e5ff751ad0b 100644 --- a/tools/backup_tool/src/tools_op_backup.cpp +++ b/tools/backup_tool/src/tools_op_backup.cpp @@ -158,9 +158,9 @@ static void OnBundleStarted(shared_ptr ctx, ErrCode err, const BundleNa } } -static void OnResultReport(shared_ptr ctx, const std::string result) +static void OnResultReport(shared_ptr ctx, const std::string bundleName, const std::string result) { - printf("OnResultReport result is = %s\n", result.c_str()); + printf("OnResultReport bundleName = %s, result = %s\n", bundleName.c_str(), result.c_str()); } static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleName name) @@ -248,7 +248,7 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init backup\n"); diff --git a/tools/backup_tool/src/tools_op_incremental_backup.cpp b/tools/backup_tool/src/tools_op_incremental_backup.cpp index 6b429c0d170baec8ea07e5e2d9a83003ae92e511..2be218c800ada8a4224c0b62cb7dfe62708eb175 100644 --- a/tools/backup_tool/src/tools_op_incremental_backup.cpp +++ b/tools/backup_tool/src/tools_op_incremental_backup.cpp @@ -213,9 +213,9 @@ static void OnAllBundlesFinished(shared_ptr ctx, ErrCode err) ctx->TryNotify(); } -static void OnResultReport(shared_ptr ctx, const std::string &resultInfo) +static void OnResultReport(shared_ptr ctx, const std::string &bundleName, const std::string &resultInfo) { - printf("OnResultReport, resultInfo = %s\n", resultInfo.c_str()); + printf("OnResultReport bundleName = %s, resultInfo = %s\n", bundleName.c_str(), resultInfo.c_str()); } static void OnBackupServiceDied(shared_ptr ctx) @@ -307,7 +307,7 @@ static int32_t Init(const string &pathCapFile, const vector& bundleNames .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init backup\n"); diff --git a/tools/backup_tool/src/tools_op_incremental_restore.cpp b/tools/backup_tool/src/tools_op_incremental_restore.cpp index 7eaf331e7337e38b591412728c05e6905800c541..a713c36424df282d5aa18b0fb886f271e0606250 100644 --- a/tools/backup_tool/src/tools_op_incremental_restore.cpp +++ b/tools/backup_tool/src/tools_op_incremental_restore.cpp @@ -214,9 +214,9 @@ static void OnBackupServiceDied(shared_ptr ctx) ctx->TryNotify(true); } -static void OnResultReport(shared_ptr ctx, const std::string &resultInfo) +static void OnResultReport(shared_ptr ctx, const std::string &bundleName, const std::string &resultInfo) { - printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + printf("OnResultReport bundleName = %s, resultInfo = %s\n", bundleName.c_str(), resultInfo.c_str()); } static void RestoreApp(shared_ptr restore) @@ -271,7 +271,7 @@ static int32_t InitRestoreSession(shared_ptr ctx, .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/tools/backup_tool/src/tools_op_incremental_restore_async.cpp b/tools/backup_tool/src/tools_op_incremental_restore_async.cpp index c7a56463093c32b5c245966bf43758d3d97c926a..a6ff9cf412a300179ff3b5d241b8f8da41fa676e 100644 --- a/tools/backup_tool/src/tools_op_incremental_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_incremental_restore_async.cpp @@ -240,9 +240,10 @@ static void AdapteCloneOptimize(const string &path) close(cachedEntity.GetFd().Release()); } -static void OnResultReport(shared_ptr ctx, const std::string &resultInfo) +static void OnResultReport(shared_ptr ctx, const std::string &bundleName, + const std::string &resultInfo) { - printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + printf("OnResultReport bundleName = %s, resultInfo = %s\n", bundleName.c_str(), resultInfo.c_str()); } static void RestoreApp(shared_ptr restore, vector &bundleNames) @@ -398,7 +399,7 @@ static int32_t InitArg(const string &pathCapFile, .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index ff51241acea51ef6c18f5fc19cd953305d187bf2..e9c172426397c364c216df94fb40f2e90cbb1b24 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -173,9 +173,9 @@ static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleN ctx->TryNotify(); } -static void OnResultReport(shared_ptr ctx, const std::string &resultInfo) +static void OnResultReport(shared_ptr ctx, const std::string &bundleName, const std::string &resultInfo) { - printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + printf("OnResultReport bundleName = %s, resultInfo = %s\n", bundleName.c_str(), resultInfo.c_str()); } static void OnAllBundlesFinished(shared_ptr ctx, ErrCode err) @@ -256,7 +256,7 @@ static int32_t InitRestoreSession(shared_ptr ctx) .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/tools/backup_tool/src/tools_op_restore_async.cpp b/tools/backup_tool/src/tools_op_restore_async.cpp index 2e6679767e177d99c5a96aae9fd3757abb849aba..b937cec5139b1f9760156bc5035b91dc8961eee4 100644 --- a/tools/backup_tool/src/tools_op_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_restore_async.cpp @@ -159,9 +159,9 @@ static void OnAllBundlesFinished(shared_ptr ctx, ErrCode err) ctx->TryNotify(true); } -static void OnResultReport(shared_ptr ctx, const std::string &resultInfo) +static void OnResultReport(shared_ptr ctx, const std::string &bundleName, const std::string &resultInfo) { - printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + printf("OnResultReport bundleName = %s, resultInfo = %s\n", bundleName.c_str(), resultInfo.c_str()); } static void OnBackupServiceDied(shared_ptr ctx) @@ -382,7 +382,7 @@ static int32_t InitArg(const string &pathCapFile, .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n");