diff --git a/BUILD.gn b/BUILD.gn index 4233fa9353c8c11a1ea4c800e610e7d7f1877f27..54caeef415214a154d7c2b173c304e5a8c0376c8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -20,3 +20,7 @@ group("libremotefileshare") { "//foundation/filemanagement/app_file_service/interfaces/kits/js:remotefileshare", ] } + +group("tgt_backup_js") { + deps = [ "//foundation/filemanagement/app_file_service/interfaces/kits/js:backup" ] +} diff --git a/bundle.json b/bundle.json index fa6637b891cf0bf23d8f0ce7946480a6d549cfff..3e27ef5e1327bda7220f100041b45d30c83d3eb2 100644 --- a/bundle.json +++ b/bundle.json @@ -27,10 +27,14 @@ "fwk_group": [ "//foundation/filemanagement/app_file_service:libremotefileshare", "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri" + "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri", + "//foundation/filemanagement/app_file_service/interfaces/kits/js:backup" ], "service_group": [] }, + "sub_component": [ + "//foundation/filemanagement/app_file_service:tgt_backup_js" + ], "inner_kits": [ { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share:fileshare_native", diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 6e4a29c48c76bbce4aeef73cc91fe74c2bedd4fd..78f25931b83b3b5e4462a18f197b75324412d3b8 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -101,3 +101,28 @@ ohos_shared_library("fileuri") { part_name = "app_file_service" subsystem_name = "filemanagement" } + +ohos_shared_library("backup") { + relative_install_dir = "module" + subsystem_name = "filemanagement" + part_name = "app_file_service" + + include_dirs = [ "${path_napi}/interfaces/kits" ] + + sources = [ + "${path_backup_js}/backup/local_capabilities.cpp", + "${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_restore_n_exporter.cpp", + ] + + deps = [ + "${path_file_api}/utils/filemgmt_libn:filemgmt_libn", + "${path_napi}:ace_napi", + ] + + external_deps = ["app_file_service:backup_kit_inner"] + + public_deps = ["${path_file_api}/utils/filemgmt_libhilog:filemgmt_libhilog"] +} diff --git a/interfaces/kits/js/backup/general_callbacks.h b/interfaces/kits/js/backup/general_callbacks.h new file mode 100644 index 0000000000000000000000000000000000000000..e79e9cbc295e820ae2fc1a139c02851750834abe --- /dev/null +++ b/interfaces/kits/js/backup/general_callbacks.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 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. + */ +#ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H +#define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H + +#include +#include +#include + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::Backup { +class GeneralCallbacks { +public: + GeneralCallbacks(const napi_env &env, const LibN::NVal &thisPtr, const LibN::NVal &jsCallbacks) + : onFileReady(env, thisPtr, jsCallbacks.GetProp("onFileReady")), + onBundleStarted(env, thisPtr, jsCallbacks.GetProp("onBundleStarted")), + onBundleFinished(env, thisPtr, jsCallbacks.GetProp("onBundleFinished")), + onAllBundlesFinished(env, thisPtr, jsCallbacks.GetProp("onAllBundlesFinished")), + onBackupServiceDied(env, thisPtr, jsCallbacks.GetProp("onBackupServiceDied")) {}; + +public: + LibN::NAsyncWorkCallback onFileReady; + LibN::NAsyncWorkCallback onBundleStarted; + LibN::NAsyncWorkCallback onBundleFinished; + LibN::NAsyncWorkCallback onAllBundlesFinished; + LibN::NAsyncWorkCallback onBackupServiceDied; +}; +} // 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/local_capabilities.cpp b/interfaces/kits/js/backup/local_capabilities.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c8ea23e693f730e3b9851f5ed0f70e0bd2bfec4 --- /dev/null +++ b/interfaces/kits/js/backup/local_capabilities.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 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 "local_capabilities.h" + +#include "filemgmt_libhilog.h" +#include "filemgmt_libn.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) +{ + 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; + } + + auto fd = make_shared(); + auto cbExec = [fd]() -> NError { + HILOGI("called LocalCapabilities::Async cbExec"); + auto proxy = ServiceProxy::GetInstance(); + if (!proxy) { + HILOGI("called LocalCapabilities::Async cbExec, failed to get proxy"); + return NError(errno); + } + *fd = proxy->GetLocalCapabilities(); + 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}; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + HILOGI("called LocalCapabilities::Async::promise"); + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_; + } else { + HILOGI("called LocalCapabilities::Async::callback"); + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_; + } +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/local_capabilities.h b/interfaces/kits/js/backup/local_capabilities.h new file mode 100644 index 0000000000000000000000000000000000000000..716b5d3dfb01a20ae323aa3e6e96604443b222ef --- /dev/null +++ b/interfaces/kits/js/backup/local_capabilities.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 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. + */ +#ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_LOCAL_CAPABILITIES_H +#define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_LOCAL_CAPABILITIES_H + +#include +#include + +namespace OHOS::FileManagement::Backup { +class LocalCapabilities final { +public: + static napi_value Async(napi_env env, napi_callback_info info); +}; + +const std::string PROCEDURE_LOCALCAPABILITIES_NAME = "getLocalCapalities"; +} // namespace OHOS::FileManagement::Backup +#endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_LOCAL_CAPABILITIES_H \ No newline at end of file diff --git a/interfaces/kits/js/backup/module.cpp b/interfaces/kits/js/backup/module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a0cc1ec14d1ea4a5bc9b04b5fb1d4e5fc716e19 --- /dev/null +++ b/interfaces/kits/js/backup/module.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 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 +#include + +#include "filemgmt_libhilog.h" +#include "filemgmt_libn.h" +#include "prop_n_exporter.h" +#include "session_backup_n_exporter.h" +#include "session_restore_n_exporter.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace LibN; + +static napi_value Export(napi_env env, napi_value exports) +{ + std::vector> products; + 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 backup", product->GetClassName().c_str()); + return nullptr; + } else { + HILOGI("Class %{public}s for module fileio has been exported", product->GetClassName().c_str()); + } + } + return exports; +} + +NAPI_MODULE(backup, Export) +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/prop_n_exporter.cpp b/interfaces/kits/js/backup/prop_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..572a46db4bf77529bc9caedf7f2c073f983398d5 --- /dev/null +++ b/interfaces/kits/js/backup/prop_n_exporter.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 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 "prop_n_exporter.h" + +#include "local_capabilities.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace LibN; + +bool PropNExporter::Export() +{ + return exports_.AddProp({ + NVal::DeclareNapiFunction("getLocalCapabilities", LocalCapabilities::Async), + }); +} + +string PropNExporter::GetClassName() +{ + return PropNExporter::className; +} + +PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +PropNExporter::~PropNExporter() {} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/prop_n_exporter.h b/interfaces/kits/js/backup/prop_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..defb4c78ac796430264493eef8384fb1a801b471 --- /dev/null +++ b/interfaces/kits/js/backup/prop_n_exporter.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 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. + */ +#ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_PROP_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_PROP_N_EXPORTER_H + +#include + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::Backup { +class PropNExporter final : public LibN::NExporter { +public: + inline static const std::string className = "LocalCapabilities"; + + bool Export() override; + std::string GetClassName() override; + + PropNExporter(napi_env env, napi_value exports); + ~PropNExporter() override; +}; +} // namespace OHOS::FileManagement::Backup +#endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_PROP_N_EXPORTER_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 new file mode 100644 index 0000000000000000000000000000000000000000..f3d926d547c383af73c50926258e039c8d30e81b --- /dev/null +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -0,0 +1,359 @@ +/* + * Copyright (c) 2023 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 "session_backup_n_exporter.h" + +#include +#include + +#include "b_error/b_error.h" +#include "b_filesystem/b_file.h" +#include "b_resources/b_constants.h" +#include "b_session_backup.h" +#include "backup_kit_inner.h" +#include "directory_ex.h" +#include "filemgmt_libhilog.h" +#include "general_callbacks.h" +#include "service_proxy.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace LibN; + +struct BackupEntity { + unique_ptr session; + shared_ptr callbacks; +}; + +static void OnFileReady(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd) +{ + 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())}](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("fileName", NVal::CreateUTF8String(env, fileName).val_), + NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)}); + + return {obj}; + }; + + callbacks->onFileReady.ThreadSafeSchedule(cbCompl); +} + +static void OnBundleStarted(weak_ptr pCallbacks, ErrCode err, const BundleName name) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onBundleStarted has already been released"); + return; + } + if (bool(callbacks->onBundleStarted)) { + HILOGI("callback function onBundleStarted is undefined"); + return; + } + + auto cbCompl = [name {name}](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUTF8String(env, name); + }; + + callbacks->onBundleStarted.ThreadSafeSchedule(cbCompl); +} + +static void OnBundleFinished(weak_ptr pCallbacks, ErrCode err, const BundleName name) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onBundleFinished has already been released"); + return; + } + if (bool(callbacks->onBundleFinished)) { + HILOGI("callback function onBundleFinished is undefined"); + return; + } + + auto cbCompl = [name {name}](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUTF8String(env, name); + }; + + callbacks->onBundleFinished.ThreadSafeSchedule(cbCompl); +} + +static void OnAllBundlesFinished(weak_ptr pCallbacks, ErrCode err) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onAllBundlesFinished has already been released"); + return; + } + if (bool(callbacks->onAllBundlesFinished)) { + HILOGI("callback function onAllBundlesFinished is undefined"); + return; + } + + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + callbacks->onAllBundlesFinished.ThreadSafeSchedule(cbCompl); +} + +static void OnBackupServiceDied(weak_ptr pCallbacks) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("js callback function onBackupServiceDied has already been released"); + return; + } + if (bool(callbacks->onBackupServiceDied)) { + HILOGI("callback function onBackupServiceDied is undefined"); + return; + } + + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + callbacks->onBackupServiceDied.ThreadSafeSchedule(cbCompl); +} + +napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup::Constructor begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal callbacks(env, funcArg[NARG_POS::FIRST]); + if (!callbacks.TypeIs(napi_object)) { + HILOGE("First argument is not an object."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal ptr(env, funcArg.GetThisVar()); + auto backupEntity = std::make_unique(); + backupEntity->callbacks = make_shared(env, ptr, callbacks); + backupEntity->session = BSessionBackup::Init(BSessionBackup::Callbacks { + .onFileReady = bind(OnFileReady, backupEntity->callbacks, placeholders::_1, placeholders::_2), + .onBundleStarted = bind(OnBundleStarted, backupEntity->callbacks, placeholders::_1, placeholders::_2), + .onBundleFinished = bind(OnBundleFinished, backupEntity->callbacks, placeholders::_1, placeholders::_2), + .onAllBundlesFinished = bind(OnAllBundlesFinished, backupEntity->callbacks, placeholders::_1), + .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks)}); + if (!backupEntity->session) { + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").Normalize()).ThrowErr(env); + return nullptr; + } + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(backupEntity))) { + HILOGE("Failed to set BackupEntity entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + HILOGI("called SessionBackup::Constructor end"); + return funcArg.GetThisVar(); +} + +napi_value SessionBackupNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup::AppendBundles begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal jsBundles(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundles, ignore] = jsBundles.ToStringArray(); + if (!succ) { + HILOGE("First argument is not bundles array."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {backupEntity->session.get()}, bundles {bundles}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").Normalize()); + } + return NError(session->AppendBundles(bundles)); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionBackup::AppendBundles end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +napi_value SessionBackupNExporter::Start(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup::Start begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + NError(EPERM).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").Normalize()); + } + return NError(session->Start()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionBackup::Start end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +napi_value SessionBackupNExporter::Finish(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup::Finish begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backup session entity."); + NError(EPERM).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").Normalize()); + } + return NError(session->Finish()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionBackup::Finish end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +bool SessionBackupNExporter::Export() +{ + HILOGI("called SessionBackupNExporter::Export begin"); + vector props = { + NVal::DeclareNapiFunction("appendBundles", AppendBundles), + NVal::DeclareNapiFunction("start", Start), + NVal::DeclareNapiFunction("finish", Finish), + }; + + auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); + if (!succ) { + HILOGE("Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOGE("Failed to save class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + HILOGI("called SessionBackupNExporter::Export end"); + return exports_.AddProp(className, classValue); +} + +string SessionBackupNExporter::GetClassName() +{ + return SessionBackupNExporter::className; +} + +SessionBackupNExporter::SessionBackupNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +SessionBackupNExporter::~SessionBackupNExporter() {} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..a5c57302dded79c804610c9199aa7cd81300e1c7 --- /dev/null +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 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. + */ +#ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_BACKUP_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_BACKUP_N_EXPORTER_H + +#include "n_exporter.h" + +namespace OHOS::FileManagement::Backup { +class SessionBackupNExporter final : public LibN::NExporter { +public: + inline static const std::string className = "SessionBackup"; + + bool Export() override; + std::string GetClassName() override; + + 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 Start(napi_env env, napi_callback_info cbinfo); + static napi_value Finish(napi_env env, napi_callback_info cbinfo); + + SessionBackupNExporter(napi_env env, napi_value exports); + ~SessionBackupNExporter() override; +}; +} // namespace OHOS::FileManagement::Backup +#endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_BACKUP_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc0e95358c54ef0d8c8e72864d1364c90a9f7092 --- /dev/null +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -0,0 +1,494 @@ +/* + * Copyright (c) 2023 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 "session_restore_n_exporter.h" + +#include +#include + +#include "b_error/b_error.h" +#include "b_filesystem/b_dir.h" +#include "b_filesystem/b_file.h" +#include "b_resources/b_constants.h" +#include "b_session_restore.h" +#include "backup_kit_inner.h" +#include "filemgmt_libhilog.h" +#include "general_callbacks.h" +#include "service_proxy.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace LibN; + +struct RestoreEntity { + unique_ptr session; + shared_ptr callbacks; +}; + +static void OnFileReady(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd) +{ + 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())}](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("fileName", NVal::CreateUTF8String(env, fileName).val_), + NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)}); + + return {obj}; + }; + + callbacks->onFileReady.ThreadSafeSchedule(cbCompl); +} + +static void OnBundleStarted(weak_ptr pCallbacks, ErrCode err, const BundleName name) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onBundleStarted has already been released"); + return; + } + if (bool(callbacks->onBundleStarted)) { + HILOGI("callback function onBundleStarted is undefined"); + return; + } + + auto cbCompl = [name {name}](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUTF8String(env, name); + }; + + callbacks->onBundleStarted.ThreadSafeSchedule(cbCompl); +} + +static void OnBundleFinished(weak_ptr pCallbacks, ErrCode err, const BundleName name) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onBundleFinished has already been released"); + return; + } + if (bool(callbacks->onBundleFinished)) { + HILOGI("callback function onBundleFinished is undefined"); + return; + } + + auto cbCompl = [name {name}](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUTF8String(env, name); + }; + + callbacks->onBundleFinished.ThreadSafeSchedule(cbCompl); +} + +static void OnAllBundlesFinished(weak_ptr pCallbacks, ErrCode err) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onAllBundlesFinished has already been released"); + return; + } + if (bool(callbacks->onAllBundlesFinished)) { + HILOGI("callback function onAllBundlesFinished is undefined"); + return; + } + + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + callbacks->onAllBundlesFinished.ThreadSafeSchedule(cbCompl); +} + +static void OnBackupServiceDied(weak_ptr pCallbacks) +{ + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("js callback function onBackupServiceDied has already been released"); + return; + } + if (bool(callbacks->onBackupServiceDied)) { + HILOGI("callback function onBackupServiceDied is undefined"); + return; + } + + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + callbacks->onBackupServiceDied.ThreadSafeSchedule(cbCompl); +} + +napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::Constructor begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal callbacks(env, funcArg[NARG_POS::FIRST]); + if (!callbacks.TypeIs(napi_object)) { + HILOGE("First argument is not an object."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal ptr(env, funcArg.GetThisVar()); + 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(OnBundleStarted, restoreEntity->callbacks, placeholders::_1, placeholders::_2), + .onBundleFinished = bind(OnBundleFinished, restoreEntity->callbacks, placeholders::_1, placeholders::_2), + .onAllBundlesFinished = bind(OnAllBundlesFinished, restoreEntity->callbacks, placeholders::_1), + .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); + if (!restoreEntity->session) { + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init restore").Normalize()).ThrowErr(env); + return nullptr; + } + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(restoreEntity))) { + HILOGE("Failed to set SessionRestore entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + HILOGI("called SessionRestore::Constructor end"); + return funcArg.GetThisVar(); +} + +napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::AppendBundles begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal remoteCap(env, funcArg[NARG_POS::FIRST]); + auto [err, fd] = remoteCap.ToInt32(); + if (err) { + HILOGE("First argument is not remote capabilitily file number."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal jsBundles(env, funcArg[NARG_POS::SECOND]); + auto [succ, bundles, ignore] = jsBundles.ToStringArray(); + if (!succ) { + HILOGE("First argument is not bundles array."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && restoreEntity->session)) { + HILOGE("Failed to get RestoreSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {restoreEntity->session.get()}, fd {fd}, bundles {bundles}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").Normalize()); + } + return NError(session->AppendBundles(UniqueFd(fd), bundles)); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionRestore::AppendBundles end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +napi_value SessionRestoreNExporter::PublishFile(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::PublishFile begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal fileMeta(env, funcArg[NARG_POS::FIRST]); + if (!fileMeta.TypeIs(napi_object)) { + HILOGE("First arguments is not an object"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, bundleName, size] = fileMeta.GetProp("bundleName").ToUTF8String(); + if (!succ) { + HILOGE("First argument is not have property bundle name."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr fileName = nullptr; + tie(succ, fileName, ignore) = fileMeta.GetProp("fileName").ToUTF8String(); + if (!succ) { + HILOGE("First argument is not have property file name."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && restoreEntity->session)) { + HILOGE("Failed to get RestoreSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {restoreEntity->session.get()}, bundleName {string(bundleName.get())}, + fileName {string(fileName.get())}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").Normalize()); + } + BFileInfo fileInfo(bundleName, fileName, 0); + return NError(session->PublishFile(fileInfo)); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionRestore::PublishFile end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +napi_value SessionRestoreNExporter::GetFileHandle(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::GetFileHandle begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + NVal fileMeta(env, funcArg[NARG_POS::FIRST]); + if (!fileMeta.TypeIs(napi_object)) { + HILOGE("First arguments is not an object"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, bundleName, ignore] = fileMeta.GetProp("bundleName").ToUTF8String(); + if (!succ) { + HILOGE("First argument is not have property bundle name."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr fileName = nullptr; + tie(succ, fileName, ignore) = fileMeta.GetProp("fileName").ToUTF8String(); + if (!succ) { + HILOGE("First argument is not have property file name."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && restoreEntity->session)) { + HILOGE("Failed to get RestoreSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {restoreEntity->session.get()}, bundleName {string(bundleName.get())}, + fileName {string(fileName.get())}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").Normalize()); + } + string bundle = bundleName; + string file = fileName; + return NError(session->GetFileHandle(bundle, file)); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionRestore::GetFileHandle end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +napi_value SessionRestoreNExporter::Start(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::Start begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && restoreEntity->session)) { + HILOGE("Failed to get RestoreSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {restoreEntity->session.get()}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").Normalize()); + } + return NError(session->Start()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionRestore::Start end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +napi_value SessionRestoreNExporter::Finish(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::Finish begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && restoreEntity->session)) { + HILOGE("Failed to get RestoreSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {restoreEntity->session.get()}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").Normalize()); + } + return NError(session->Finish()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionRestore::Finish end."); + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } +} + +bool SessionRestoreNExporter::Export() +{ + HILOGI("called SessionRestoreNExporter::Export begin"); + vector props = { + NVal::DeclareNapiFunction("appendBundles", AppendBundles), + NVal::DeclareNapiFunction("publishFile", PublishFile), + NVal::DeclareNapiFunction("getFileHandle", GetFileHandle), + NVal::DeclareNapiFunction("start", Start), + NVal::DeclareNapiFunction("finish", Finish), + }; + + auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); + if (!succ) { + HILOGE("Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOGE("Failed to save class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + HILOGI("called SessionRestoreNExporter::Export end"); + return exports_.AddProp(className, classValue); +} + +string SessionRestoreNExporter::GetClassName() +{ + return SessionRestoreNExporter::className; +} + +SessionRestoreNExporter::SessionRestoreNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +SessionRestoreNExporter::~SessionRestoreNExporter() {} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..0e6f6a48ef1f272aabfc638d69d057170a6f3837 --- /dev/null +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 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. + */ +#ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_RESTORE_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_RESTORE_N_EXPORTER_H + +#include +#include + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::Backup { +class SessionRestoreNExporter final : public LibN::NExporter { +public: + inline static const std::string className = "SessionRestore"; + + bool Export() override; + std::string GetClassName() override; + + 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 PublishFile(napi_env env, napi_callback_info cbinfo); + static napi_value GetFileHandle(napi_env env, napi_callback_info cbinfo); + static napi_value Start(napi_env env, napi_callback_info cbinfo); + static napi_value Finish(napi_env env, napi_callback_info cbinfo); + + SessionRestoreNExporter(napi_env env, napi_value exports); + ~SessionRestoreNExporter() override; +}; +} // namespace OHOS::FileManagement::Backup +#endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_RESTORE_N_EXPORTER_H \ No newline at end of file