diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e1762314c5a5d45a336b34126ddd65774697ea1a..283085c8781acd80f1f9ce10be491f402bfba785 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -120,6 +120,7 @@ ohos_shared_library("backup") { "${path_backup_js}/backup/module.cpp", "${path_backup_js}/backup/prop_n_exporter.cpp", "${path_backup_js}/backup/session_backup_n_exporter.cpp", + "${path_backup_js}/backup/session_incremental_backup_n_exporter.cpp", "${path_backup_js}/backup/session_restore_n_exporter.cpp", ] diff --git a/interfaces/kits/js/backup/local_capabilities.cpp b/interfaces/kits/js/backup/local_capabilities.cpp index d3b9e9eb3b5de4c610dcbfc1d6ac5b7e8fa4fdc2..404f1ccac21fbe39c3f6669165a05431040c3b32 100644 --- a/interfaces/kits/js/backup/local_capabilities.cpp +++ b/interfaces/kits/js/backup/local_capabilities.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -14,31 +14,28 @@ */ #include "local_capabilities.h" +#include "b_error/b_error.h" +#include "b_incremental_data.h" #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" +#include "incremental_backup_data.h" #include "service_proxy.h" namespace OHOS::FileManagement::Backup { using namespace std; using namespace LibN; -napi_value LocalCapabilities::Async(napi_env env, napi_callback_info info) +static napi_value AsyncCallback(napi_env env, const NFuncArg& funcArg) { - HILOGI("called LocalCapabilities::Async begin"); - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } + HILOGI("called LocalCapabilities::AsyncCallback begin"); auto fd = make_shared(); auto cbExec = [fd]() -> NError { - HILOGI("called LocalCapabilities::Async cbExec"); + HILOGI("called LocalCapabilities::AsyncCallback cbExec"); ServiceProxy::InvaildInstance(); auto proxy = ServiceProxy::GetInstance(); if (!proxy) { - HILOGI("called LocalCapabilities::Async cbExec, failed to get proxy"); + HILOGI("called LocalCapabilities::AsyncCallback cbExec, failed to get proxy"); return NError(errno); } *fd = proxy->GetLocalCapabilities(); @@ -63,4 +60,121 @@ napi_value LocalCapabilities::Async(napi_env env, napi_callback_info info) return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_; } } + +static bool CheckDataList(const LibN::NVal &data) +{ + LibN::NVal name = data.GetProp("bundleName"); + if (name.val_ == nullptr) { + return false; + } + auto [succ, str, ignore] = name.ToUTF8String(); + if (!succ) { + return false; + } + + LibN::NVal time = data.GetProp("lastIncrementalTime"); + if (time.val_ == nullptr) { + return false; + } + tie(succ, ignore) = time.ToInt64(); + if (!succ) { + return false; + } + return true; +} + +static std::tuple> ParseDataList(napi_env env, const napi_value& value) +{ + uint32_t size = 0; + napi_status status = napi_get_array_length(env, value, &size); + if (status != napi_ok) { + HILOGE("Get array length failed!"); + return {false, {}}; + } + if (size == 0) { + HILOGI("array length is zero!"); + return {true, {}}; + } + + napi_value result; + std::vector backupData; + for (uint32_t i = 0; i < size; i++) { + status = napi_get_element(env, value, i, &result); + if (status != napi_ok) { + HILOGE("Get element failed! index is :%{public}u", i); + return {false, {}}; + } else { + NVal element(env, result); + if (!CheckDataList(element)) { + HILOGE("bundles are invalid!"); + return {false, {}}; + } + IncrementalBackupData data(element); + backupData.emplace_back(data.bundleName, + data.lastIncrementalTime, + data.manifestFd, + data.parameters, + data.priority); + } + } + return {true, backupData}; +} + +static napi_value AsyncDataList(napi_env env, const NFuncArg& funcArg) +{ + HILOGI("called LocalCapabilities::AsyncDataList begin"); + + auto [succ, bundles] = ParseDataList(env, funcArg[NARG_POS::FIRST]); + if (!succ) { + HILOGE("bundles array invalid."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "bundles array invalid.").GetCode()).ThrowErr(env); + return nullptr; + } + + auto fd = make_shared(); + auto cbExec = [fd, bundles { move(bundles) }]() -> NError { + HILOGI("called LocalCapabilities::AsyncDataList cbExec"); + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (!proxy) { + HILOGI("called LocalCapabilities::AsyncDataList cbExec, failed to get proxy"); + return NError(errno); + } + *fd = proxy->GetLocalCapabilitiesIncremental(bundles); + return NError(ERRNO_NOERR); + }; + auto cbCompl = [fd](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + NVal obj = NVal::CreateObject(env); + obj.AddProp({NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)}); + return {obj}; + }; + + HILOGI("called LocalCapabilities::Async::promise"); + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_; +} + +napi_value LocalCapabilities::Async(napi_env env, napi_callback_info info) +{ + HILOGI("called LocalCapabilities::Async begin"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return nullptr; + } + + if (funcArg.GetArgc() == 1) { + bool result = 0; + napi_status status = napi_is_array(env, funcArg[NARG_POS::FIRST], &result); + if (status == napi_ok && result) { + return AsyncDataList(env, funcArg); + } + } + + return AsyncCallback(env, funcArg); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/module.cpp b/interfaces/kits/js/backup/module.cpp index 59f88491b7e918250b1709822f054d4fc5562803..7dccab264943c461cdc36417a292b28c62765799 100644 --- a/interfaces/kits/js/backup/module.cpp +++ b/interfaces/kits/js/backup/module.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -20,6 +20,7 @@ #include "filemgmt_libn.h" #include "prop_n_exporter.h" #include "session_backup_n_exporter.h" +#include "session_incremental_backup_n_exporter.h" #include "session_restore_n_exporter.h" namespace OHOS::FileManagement::Backup { @@ -32,6 +33,7 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); for (auto &&product : products) { if (!product->Export()) { HILOGE("INNER BUG. Failed to export class %{public}s for module file.backup", diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index 4aa1b42966730e2daee7355c24a80201f9015d76..8b802d3b0ed5b2c7ca4f77f44ac0dd6d3dbc6008 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -289,10 +289,46 @@ napi_value SessionBackupNExporter::AppendBundles(napi_env env, napi_callback_inf } } +napi_value SessionBackupNExporter::Release(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup::Release begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return nullptr; + } + + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get backupSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {backupEntity->session.get()}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").GetCode()); + } + return NError(session->Release()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGI("Called SessionBackup::Release end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionBackupNExporter::Export() { HILOGI("called SessionBackupNExporter::Export begin"); - vector props = {NVal::DeclareNapiFunction("appendBundles", AppendBundles)}; + vector props = { + NVal::DeclareNapiFunction("appendBundles", AppendBundles), + NVal::DeclareNapiFunction("release", Release), + }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); if (!succ) { diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index afa3c96b2fe3306c795f9795f65250e48bca747f..1c95b7df63cb25ccfaa1a30de635f35a7cd66377 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -27,6 +27,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); SessionBackupNExporter(napi_env env, napi_value exports); ~SessionBackupNExporter() override; 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 74b9af776b0247000277985692db403d3f67f3ca..5d9ce0d518c9bebeb1c90845691caf7e2d2dfdc6 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -263,7 +263,7 @@ static bool CheckDataList(const LibN::NVal &data) if (time.val_ == nullptr) { return false; } - auto [succ, tm] = time.ToInt64(); + tie(succ, ignore) = time.ToInt64(); if (!succ) { return false; } diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 30b97578f83101a812c2b6abcd5291ace09016bb..6cd9712d719de47344c976b6877a480a4a3756af 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -20,6 +20,8 @@ #include "b_error/b_error.h" #include "b_filesystem/b_dir.h" #include "b_filesystem/b_file.h" +#include "b_incremental_restore_session.h" +#include "b_ohos/startup/backup_para.h" #include "b_resources/b_constants.h" #include "b_session_restore.h" #include "backup_kit_inner.h" @@ -32,11 +34,12 @@ using namespace std; using namespace LibN; struct RestoreEntity { - unique_ptr session; + unique_ptr sessionWhole; + unique_ptr sessionSheet; shared_ptr callbacks; }; -static void OnFileReady(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd) +static void OnFileReadyWhole(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd) { if (pCallbacks.expired()) { HILOGI("callbacks is unbound"); @@ -68,6 +71,44 @@ static void OnFileReady(weak_ptr pCallbacks, const BFileInfo & callbacks->onFileReady.ThreadSafeSchedule(cbCompl); } +static void OnFileReadySheet(weak_ptr pCallbacks, + const BFileInfo &fileInfo, + UniqueFd fd, + UniqueFd manifestFd) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onFileReady has already been released"); + return; + } + if (!bool(callbacks->onFileReady)) { + HILOGI("callback function onFileReady is undefined"); + return; + } + + auto cbCompl = [bundleName {fileInfo.owner}, + fileName {fileInfo.fileName}, + fd {make_shared(fd.Release())}, + manifestFd {make_shared(manifestFd.Release())}](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + NVal obj = NVal::CreateObject(env); + obj.AddProp({NVal::DeclareNapiProperty("bundleName", NVal::CreateUTF8String(env, bundleName).val_), + NVal::DeclareNapiProperty("uri", NVal::CreateUTF8String(env, fileName).val_), + NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_), + NVal::DeclareNapiProperty("manifestFd", NVal::CreateInt32(env, manifestFd->Release()).val_)}); + + return {obj}; + }; + + callbacks->onFileReady.ThreadSafeSchedule(cbCompl); +} + static void onBundleBegin(weak_ptr pCallbacks, ErrCode err, const BundleName name) { if (pCallbacks.expired()) { @@ -221,15 +262,28 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info } NVal ptr(env, funcArg.GetThisVar()); + bool bSheet = BackupPara().GetBackupOverrideIncrementalRestore(); auto restoreEntity = std::make_unique(); restoreEntity->callbacks = make_shared(env, ptr, callbacks); - restoreEntity->session = BSessionRestore::Init(BSessionRestore::Callbacks { - .onFileReady = bind(OnFileReady, restoreEntity->callbacks, placeholders::_1, placeholders::_2), - .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), - .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); - if (!restoreEntity->session) { + if (bSheet) { + restoreEntity->sessionWhole = nullptr; + restoreEntity->sessionSheet = BIncrementalRestoreSession::Init(BIncrementalRestoreSession::Callbacks { + .onFileReady = + bind(OnFileReadySheet, restoreEntity->callbacks, placeholders::_1, placeholders::_2, placeholders::_3), + .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), + .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); + } else { + restoreEntity->sessionSheet = nullptr; + restoreEntity->sessionWhole = BSessionRestore::Init(BSessionRestore::Callbacks { + .onFileReady = bind(OnFileReadyWhole, restoreEntity->callbacks, placeholders::_1, placeholders::_2), + .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), + .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); + } + if (!restoreEntity->sessionWhole && !restoreEntity->sessionSheet) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init restore").GetCode()).ThrowErr(env); return nullptr; } @@ -271,17 +325,20 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!(restoreEntity && restoreEntity->session)) { + if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { HILOGE("Failed to get RestoreSession entity."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get RestoreSession entity.").GetCode()).ThrowErr(env); return nullptr; } - auto cbExec = [session {restoreEntity->session.get()}, fd {fd}, bundles {bundles}]() -> NError { - if (!session) { + auto cbExec = [entity {restoreEntity}, fd {fd}, bundles {bundles}]() -> NError { + if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); } - return NError(session->AppendBundles(UniqueFd(fd), bundles)); + if (entity->sessionWhole) { + return NError(entity->sessionWhole->AppendBundles(UniqueFd(fd), bundles)); + } + return NError(entity->sessionSheet->AppendBundles(UniqueFd(fd), bundles)); }; auto cbCompl = [](napi_env env, NError err) -> NVal { return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); @@ -344,19 +401,22 @@ napi_value SessionRestoreNExporter::PublishFile(napi_env env, napi_callback_info } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!(restoreEntity && restoreEntity->session)) { + if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { HILOGE("Failed to get RestoreSession entity."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get RestoreSession entity.").GetCode()).ThrowErr(env); return nullptr; } - auto cbExec = [session {restoreEntity->session.get()}, bundleName {string(bundleName.get())}, - fileName {string(fileName.get())}]() -> NError { - if (!session) { + auto cbExec = [entity {restoreEntity}, bundleName {string(bundleName.get())}, + fileName {string(fileName.get())}]() -> NError { + if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); } BFileInfo fileInfo(bundleName, fileName, 0); - return NError(session->PublishFile(fileInfo)); + if (entity->sessionWhole) { + return NError(entity->sessionWhole->PublishFile(fileInfo)); + } + return NError(entity->sessionSheet->PublishFile(fileInfo)); }; auto cbCompl = [](napi_env env, NError err) -> NVal { return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); @@ -398,20 +458,23 @@ napi_value SessionRestoreNExporter::GetFileHandle(napi_env env, napi_callback_in } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!(restoreEntity && restoreEntity->session)) { + if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { HILOGE("Failed to get RestoreSession entity."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get RestoreSession entity.").GetCode()).ThrowErr(env); return nullptr; } - auto cbExec = [session {restoreEntity->session.get()}, bundleName {string(bundleName.get())}, - fileName {string(fileName.get())}]() -> NError { - if (!session) { + auto cbExec = [entity {restoreEntity}, bundleName {string(bundleName.get())}, + fileName {string(fileName.get())}]() -> NError { + if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); } string bundle = bundleName; string file = fileName; - return NError(session->GetFileHandle(bundle, file)); + if (entity->sessionWhole) { + return NError(entity->sessionWhole->GetFileHandle(bundle, file)); + } + return NError(entity->sessionSheet->GetFileHandle(bundle, file)); }; auto cbCompl = [](napi_env env, NError err) -> NVal { return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); @@ -428,6 +491,42 @@ napi_value SessionRestoreNExporter::GetFileHandle(napi_env env, napi_callback_in } } +napi_value SessionRestoreNExporter::Release(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::Release begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { + HILOGE("Failed to get RestoreSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get RestoreSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + + auto cbExec = [entity {restoreEntity}]() -> NError { + if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); + } + if (entity->sessionWhole) { + return NError(entity->sessionWhole->Release()); + } + return NError(entity->sessionSheet->Release()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGI("Called SessionRestore::Release end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionRestoreNExporter::Export() { HILOGI("called SessionRestoreNExporter::Export begin"); @@ -435,6 +534,7 @@ bool SessionRestoreNExporter::Export() NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("publishFile", PublishFile), NVal::DeclareNapiFunction("getFileHandle", GetFileHandle), + NVal::DeclareNapiFunction("release", Release), }; 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 b389b55f5685c0563b0054167586a5141185c7c8..49e1f638885e4753b5d4123b5bdafbdb76d5eedb 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -32,6 +32,7 @@ public: static napi_value AppendBundles(napi_env env, napi_callback_info cbinfo); 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); SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override;