From 792ec76093a4bfd00ed1bcf76faf8d6d0de66b17 Mon Sep 17 00:00:00 2001 From: libo429 Date: Tue, 24 Dec 2024 09:35:57 +0800 Subject: [PATCH] add cancel api part2 Signed-off-by: libo429 --- .../src/b_incremental_backup_session.cpp | 17 ++++++ .../src/b_incremental_restore_session.cpp | 17 ++++++ .../b_incremental_session_restore_async.cpp | 17 ++++++ .../src/b_session_restore.cpp | 17 ++++++ .../src/b_session_restore_async.cpp | 17 ++++++ .../impl/b_incremental_backup_session.h | 8 +++ .../impl/b_incremental_restore_session.h | 8 +++ .../b_incremental_session_restore_async.h | 8 +++ .../backup_kit_inner/impl/b_session_restore.h | 8 +++ .../impl/b_session_restore_async.h | 8 +++ .../session_incremental_backup_n_exporter.cpp | 46 ++++++++++++++++ .../session_incremental_backup_n_exporter.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 52 +++++++++++++++++++ .../js/backup/session_restore_n_exporter.h | 1 + 14 files changed, 225 insertions(+) diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index 0d69e9ba8..60648c65b 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -140,4 +140,21 @@ ErrCode BIncrementalBackupSession::Release() return proxy->Release(); } + +ErrCode BIncrementalBackupSession::Cancel(std::string bundleName) +{ + ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK; + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Called Cancel, failed to get proxy."); + return result; + } + + ErrCode errCode = proxy->Cancel(bundleName, result); + if (errCode != 0) { + HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode); + return result; + } + return result; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp index 0999dac1d..42f9bea12 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -165,4 +165,21 @@ void BIncrementalRestoreSession::RegisterBackupServiceDied(function func deathRecipient_ = sptr(new SvcDeathRecipient(callback)); remoteObj->AddDeathRecipient(deathRecipient_); } + +ErrCode BIncrementalRestoreSession::Cancel(std::string bundleName) +{ + ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK; + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Called Cancel, failed to get proxy."); + return result; + } + + ErrCode errCode = proxy->Cancel(bundleName, result); + if (errCode != 0) { + HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode); + return result; + } + return result; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp index cb79d416c..170a50db6 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp @@ -167,4 +167,21 @@ void BIncrementalSessionRestoreAsync::RegisterBackupServiceDied(std::functionAddDeathRecipient(deathRecipient_); } + +ErrCode BIncrementalSessionRestoreAsync::Cancel(std::string bundleName) +{ + ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK; + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Called Cancel, failed to get proxy."); + return result; + } + + ErrCode errCode = proxy->Cancel(bundleName, result); + if (errCode != 0) { + HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode); + return result; + } + return result; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp index 9247c39ff..133001e33 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -176,4 +176,21 @@ void BSessionRestore::RegisterBackupServiceDied(std::function functor) deathRecipient_ = sptr(new SvcDeathRecipient(callback)); remoteObj->AddDeathRecipient(deathRecipient_); } + +ErrCode BSessionRestore::Cancel(std::string bundleName) +{ + ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK; + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Called Cancel, failed to get proxy."); + return result; + } + + ErrCode errCode = proxy->Cancel(bundleName, result); + if (errCode != 0) { + HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode); + return result; + } + return result; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp index 12b82a250..a60c56462 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp @@ -169,4 +169,21 @@ void BSessionRestoreAsync::RegisterBackupServiceDied(std::function funct deathRecipient_ = sptr(new SvcDeathRecipient(callback)); remoteObj->AddDeathRecipient(deathRecipient_); } + +ErrCode BSessionRestoreAsync::Cancel(std::string bundleName) +{ + ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK; + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Called Cancel, failed to get proxy."); + return result; + } + + ErrCode errCode = proxy->Cancel(bundleName, result); + if (errCode != 0) { + HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode); + return result; + } + return result; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 c7c40df0a..0ab71f6fd 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 @@ -73,6 +73,14 @@ public: */ ErrCode Release(); + /** + * @brief 用于结束应用的备份恢复任务 + * + * @param bundleName 要取消的应用包名 + * @return ErrCode 规范错误码 + */ + ErrCode Cancel(std::string bundleName); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * 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 3a2e2ec5f..502fd1a9c 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 @@ -104,6 +104,14 @@ public: */ ErrCode Release(); + /** + * @brief 用于结束应用的备份恢复任务 + * + * @param bundleName 要取消的应用包名 + * @return ErrCode 规范错误码 + */ + ErrCode Cancel(std::string bundleName); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * 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 fc705c6f6..50c47bf7b 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 @@ -109,6 +109,14 @@ public: */ ErrCode Release(); + /** + * @brief 用于结束应用的备份恢复任务 + * + * @param bundleName 要取消的应用包名 + * @return ErrCode 规范错误码 + */ + ErrCode Cancel(std::string bundleName); + public: explicit BIncrementalSessionRestoreAsync(Callbacks callbacks) : callbacks_(callbacks) {}; ~BIncrementalSessionRestoreAsync(); 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 cdb72d326..c831dead3 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 @@ -105,6 +105,14 @@ public: */ ErrCode Release(); + /** + * @brief 用于结束应用的备份恢复任务 + * + * @param bundleName 要取消的应用包名 + * @return ErrCode 规范错误码 + */ + ErrCode Cancel(std::string bundleName); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * 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 482c6409e..64615acb2 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 @@ -110,6 +110,14 @@ public: */ ErrCode Release(); + /** + * @brief 用于结束应用的备份恢复任务 + * + * @param bundleName 要取消的应用包名 + * @return ErrCode 规范错误码 + */ + ErrCode Cancel(std::string bundleName); + public: explicit BSessionRestoreAsync(Callbacks callbacks) : callbacks_(callbacks) {}; ~BSessionRestoreAsync(); 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 7afafda7a..b54fa973f 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -553,12 +553,58 @@ napi_value SessionIncrementalBackupNExporter::Release(napi_env env, napi_callbac return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; } +napi_value SessionIncrementalBackupNExporter::Cancel(napi_env env, napi_callback_info info) +{ + HILOGI("Called SessionIncrementalBackup::Cancel begin"); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succStr, bundle, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succStr) { + HILOGE("First argument is not bundleName."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + std::string bundleName = bundle.get(); + + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + return nullptr; + } + + int result = backupEntity->session->Cancel(bundleName); + napi_value nResult; + napi_status status = napi_create_int32(env, result, &nResult); + if (status != napi_ok) { + HILOGE("napi_create_int32 faild."); + return nullptr; + } + HILOGI("Cancel success with result: %{public}d", result); + return nResult; +} + bool SessionIncrementalBackupNExporter::Export() { HILOGD("called SessionIncrementalBackupNExporter::Export begin"); vector props = { NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("release", Release), + NVal::DeclareNapiFunction("cancel", Cancel), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h index 9508630b4..2790b9400 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h @@ -28,6 +28,7 @@ public: static napi_value Constructor(napi_env env, napi_callback_info cbinfo); static napi_value AppendBundles(napi_env env, napi_callback_info cbinfo); static napi_value Release(napi_env env, napi_callback_info cbinfo); + static napi_value Cancel(napi_env env, napi_callback_info cbinfo); SessionIncrementalBackupNExporter(napi_env env, napi_value exports); ~SessionIncrementalBackupNExporter() override; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index cb4fb6de3..ddc36c4c0 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -736,6 +736,57 @@ napi_value SessionRestoreNExporter::Release(napi_env env, napi_callback_info cbi return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; } +napi_value SessionRestoreNExporter::Cancel(napi_env env, napi_callback_info info) +{ + HILOGD("Called SessionRestore::Cancel begin"); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succStr, bundle, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succStr) { + HILOGE("First argument is not bundleName."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + std::string bundleName = bundle.get(); + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { + HILOGE("Failed to get restoreSession entity."); + return nullptr; + } + + int result = BError(BError::Codes::OK); + if (restoreEntity->sessionWhole) { + result = restoreEntity->sessionWhole->Cancel(bundleName); + } else { + result = restoreEntity->sessionSheet->Cancel(bundleName); + } + + napi_value nResult; + napi_status status = napi_create_int32(env, result, &nResult); + if (status != napi_ok) { + HILOGE("napi_create_int32 faild."); + return nullptr; + } + HILOGI("Cancel success with result: %{public}d", result); + return nResult; +} + bool SessionRestoreNExporter::Export() { HILOGD("called SessionRestoreNExporter::Export begin"); @@ -744,6 +795,7 @@ bool SessionRestoreNExporter::Export() NVal::DeclareNapiFunction("publishFile", PublishFile), NVal::DeclareNapiFunction("getFileHandle", GetFileHandle), NVal::DeclareNapiFunction("release", Release), + NVal::DeclareNapiFunction("cancel", Cancel), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h index 49e1f6388..422269447 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -33,6 +33,7 @@ public: static napi_value PublishFile(napi_env env, napi_callback_info cbinfo); static napi_value GetFileHandle(napi_env env, napi_callback_info cbinfo); static napi_value Release(napi_env env, napi_callback_info cbinfo); + static napi_value Cancel(napi_env env, napi_callback_info cbinfo); SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override; -- Gitee