diff --git a/interfaces/kits/js/backup/general_callbacks.cpp b/interfaces/kits/js/backup/general_callbacks.cpp index c7253d551f221c8bfec2f8884aced18868417afc..da856fa4a987a7a72610ba6ffd4121847117033c 100644 --- a/interfaces/kits/js/backup/general_callbacks.cpp +++ b/interfaces/kits/js/backup/general_callbacks.cpp @@ -22,6 +22,32 @@ namespace OHOS::FileManagement::Backup { using namespace std; +void GeneralCallbacks::RemoveCallbackRef() +{ + HILOGI("Called RemoveCallbackRef"); + onFileReady.CleanRef(); + onBundleBegin.CleanRef(); + onBundleEnd.CleanRef(); + onAllBundlesEnd.CleanRef(); + onBackupServiceDied.CleanRef(); + onResultReport.CleanRef(); + onProcess.CleanRef(); +} + +void BackupRestoreCallback::CleanRef() +{ + HILOGI("BackupRestoreCallback CleanRef"); + if (!ctx_) { + HILOGE("BackupRestoreCallback ctx is nullptr"); + return; + } + if (!bool(ctx_->cb_)) { + HILOGE("BackupRestoreCallback ref is nullptr"); + return; + } + ctx_->cb_.DeleteJsEnv(); +} + BackupRestoreCallback::BackupRestoreCallback(napi_env env, LibN::NVal thisPtr, LibN::NVal cb) : env_(env) { ctx_ = new LibN::NAsyncContextCallback(thisPtr, cb); @@ -29,7 +55,9 @@ BackupRestoreCallback::BackupRestoreCallback(napi_env env, LibN::NVal thisPtr, L BackupRestoreCallback::~BackupRestoreCallback() { + HILOGI("BackupRestoreCallback destruct start"); if (!ctx_) { + HILOGE("BackupRestoreCallback ctx is nullptr"); return; } @@ -53,6 +81,7 @@ BackupRestoreCallback::~BackupRestoreCallback() loop, work.get(), [](uv_work_t *work) {}, [](uv_work_t *work, int status) { LibN::NAsyncContextCallback *ctx = static_cast(work->data); + HILOGI("BackupRestoreCallback destruct delete ctx"); delete ctx; delete work; }); @@ -63,6 +92,7 @@ BackupRestoreCallback::~BackupRestoreCallback() ptr.release(); work.release(); ctx_ = nullptr; + HILOGI("BackupRestoreCallback destruct end"); } BackupRestoreCallback::operator bool() const @@ -96,6 +126,10 @@ static void DoCallJsMethod(napi_env env, void *data, InputArgsParser argParser) napi_value global = nullptr; napi_get_global(env, &global); napi_value callback = ctx->cb_.Deref(env).val_; + if (!bool(ctx->cb_)) { + HILOGE("Failed to get ref."); + return; + } napi_value result = nullptr; napi_status status = napi_call_function(env, global, callback, argv.size(), argv.data(), &result); if (status != napi_ok) { diff --git a/interfaces/kits/js/backup/general_callbacks.h b/interfaces/kits/js/backup/general_callbacks.h index fdcd560aee8aa2f9b0acbce7b70ccdfb1a52faec..7f395fdfd8d96ffbb72196bc2d2527ed273c8ccb 100644 --- a/interfaces/kits/js/backup/general_callbacks.h +++ b/interfaces/kits/js/backup/general_callbacks.h @@ -30,6 +30,7 @@ public: ~BackupRestoreCallback(); void CallJsMethod(InputArgsParser argParser); explicit operator bool() const; + void CleanRef(); private: napi_env env_; LibN::NAsyncContextCallback *ctx_ = nullptr; @@ -53,7 +54,8 @@ public: onBackupServiceDied(env, thisPtr, jsCallbacks.GetProp("onBackupServiceDied")), onResultReport(env, thisPtr, jsCallbacks.GetProp("onResultReport")), onProcess(env, thisPtr, jsCallbacks.GetProp("onProcess")) {}; - +public: + void RemoveCallbackRef(); public: LibN::NAsyncWorkCallback onFileReady; LibN::NAsyncWorkCallback onBundleBegin; diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index 7f8e99ccdfa70237844a29d34dbac51c94db9f79..fcb482f77fb11c8a4f9510eb210b252b68c05cd8 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -303,6 +303,27 @@ static void OnProcess(weak_ptr pCallbacks, const BundleName na callbacks->onProcess.CallJsMethod(cbCompl); } +static bool SetSessionBackupEntity(napi_env env, NFuncArg &funcArg, std::unique_ptr backupEntity) +{ + auto finalize = [](napi_env env, void *data, void *hint) { + std::unique_ptr entity = std::unique_ptr(static_cast(data)); + if (entity == nullptr) { + HILOGE("Entity is nullptr"); + return; + } + if (entity->callbacks == nullptr) { + HILOGE("Callbacks is nullptr"); + return; + } + entity->callbacks->RemoveCallbackRef(); + }; + if (napi_wrap(env, funcArg.GetThisVar(), backupEntity.release(), finalize, nullptr, nullptr) != napi_ok) { + HILOGE("Failed to set BackupEntity entity."); + return false; + } + return true; +} + napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info cbinfo) { HILOGD("called SessionBackup::Constructor begin"); @@ -345,12 +366,10 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); return nullptr; } - if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(backupEntity))) { - HILOGE("Failed to set BackupEntity entity."); + if (!SetSessionBackupEntity(env, funcArg, std::move(backupEntity))) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to set BackupEntity entity").GetCode()).ThrowErr(env); return nullptr; } - HILOGD("called SessionBackup::Constructor end"); return funcArg.GetThisVar(); } 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 2596a6e6c4e26fcc7afc02ca26aa5f71242aeb2b..a983e927923a096f93bb048339ab818db5e6f100 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -309,6 +309,27 @@ static void OnProcess(weak_ptr pCallbacks, const BundleName na callbacks->onProcess.CallJsMethod(cbCompl); } +static bool SetIncrementalBackupEntity(napi_env env, NFuncArg &funcArg, std::unique_ptr backupEntity) +{ + auto finalize = [](napi_env env, void *data, void *hint) { + std::unique_ptr entity = std::unique_ptr(static_cast(data)); + if (entity == nullptr) { + HILOGE("Entity is nullptr"); + return; + } + if (entity->callbacks == nullptr) { + HILOGE("Callbacks is nullptr"); + return; + } + entity->callbacks->RemoveCallbackRef(); + }; + if (napi_wrap(env, funcArg.GetThisVar(), backupEntity.release(), finalize, nullptr, nullptr) != napi_ok) { + HILOGE("Failed to set BackupEntity entity."); + return false; + } + return true; +} + napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_callback_info cbinfo) { HILOGD("called SessionIncrementalBackup::Constructor begin"); @@ -352,12 +373,10 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); return nullptr; } - if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(backupEntity))) { - HILOGE("Failed to set BackupEntity entity."); + if (!SetIncrementalBackupEntity(env, funcArg, std::move(backupEntity))) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to set BackupEntity entity.").GetCode()).ThrowErr(env); return nullptr; } - HILOGD("called SessionIncrementalBackup::Constructor end"); return funcArg.GetThisVar(); } diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 0d31df98ab965a1598dc50567a6a7c0b21d40126..b39a7c264cbf4979f95acf6322f92bd58a0b825b 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -420,6 +420,27 @@ static bool VerifyNarg(napi_env env, NVal &callbacks) return false; } +static bool SetSessionRestoreEntity(napi_env env, NFuncArg &funcArg, std::unique_ptr restoreEntity) +{ + auto finalize = [](napi_env env, void *data, void *hint) { + std::unique_ptr entity = std::unique_ptr(static_cast(data)); + if (entity == nullptr) { + HILOGE("Entity is nullptr"); + return; + } + if (entity->callbacks == nullptr) { + HILOGE("Callbacks is nullptr"); + return; + } + entity->callbacks->RemoveCallbackRef(); + }; + if (napi_wrap(env, funcArg.GetThisVar(), restoreEntity.release(), finalize, nullptr, nullptr) != napi_ok) { + HILOGE("Failed to set restoreEntity entity."); + return false; + } + return true; +} + napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info cbinfo) { HILOGI("called SessionRestore::Constructor begin"); @@ -459,11 +480,10 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init restore").GetCode()).ThrowErr(env); return nullptr; } - if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(restoreEntity))) { + if (!SetSessionRestoreEntity(env, funcArg, std::move(restoreEntity))) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to set SessionRestore entity.").GetCode()).ThrowErr(env); return nullptr; } - HILOGI("called SessionRestore::Constructor end"); return funcArg.GetThisVar(); }