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 0d69e9ba8def6d05197e461e7c378f78d7a40858..60648c65b7df2b4fda05859d89a0e6833dd1ff4f 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 0999dac1d385f0ed6add68593f2faf7d8274a6a7..42f9bea125e5fdbef2e5ccb35e32f8e806cbf1db 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 cb79d416cd0e807b4111af0f65cd331bfadb3ce0..170a50db637afb7c6345fced6b262b52658855dd 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 9247c39ffe785d68ca37b484e4841a384dfce082..133001e339da5028947452b87e648475695e1265 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 12b82a250b54e5f06c6c6889b241bb1a83daf8cf..a60c564623d7e5bf79367356e579270ffb4b0fff 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 c7c40df0a3ea6bb0c478ab667a438a074c85c3ef..0ab71f6fd4c3ab26cbff31114803b241e6843858 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 3a2e2ec5fd4d3bd87fffdde7ecc9a4da70e0286e..502fd1a9c39a382a51c9b6965c97a12216ab2ccb 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 fc705c6f63a2996afbae716e4c8405fe689dbeac..50c47bf7b38d46e8f391efa7fcccd4c2e43fdc68 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 cdb72d3260090ecf549b68aded023a952bed12ef..c831dead301507326356a5c3dedc3a7e42fec58d 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 482c6409e6b488d3f4c1e7539c5a36f9249a6f93..64615acb28408e875245881ac140d6710036b6c6 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 d33e0c5e805f51aa4684d2d39e5a9721dc7ec6fb..8c1ff5f915806d7291e768e1315136617af9cf94 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -547,12 +547,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 9508630b4c57d34a394d0e99bdba2c7536ccd512..2790b9400f6f69109cfddb5a158aa0a844b343ba 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 6257637efce94b1c4743174f9bd3eb5cee7c4880..93fc3e6c9886ea16a7ffbf2f174f1836d9da3fa7 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -729,6 +729,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"); @@ -737,6 +788,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 49e1f638885e4753b5d4123b5bdafbdb76d5eedb..4222694477aa3f472e35105248c65e0a77537dc6 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;