diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp b/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp index 19eddfdf..6c7a6bd6 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp +++ b/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp @@ -20,6 +20,7 @@ #include "cloud_sync_manager.h" #include "dfs_error.h" #include "utils_log.h" +#include "async_work.h" #include "uv.h" namespace OHOS::FileManagement::CloudSync { @@ -78,19 +79,9 @@ napi_value CloudFileDownloadNapi::Start(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env); }; - NVal thisVar(env, funcArg.GetThisVar()); string procedureName = "cloudFileDownload"; - if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_; } CloudDownloadCallbackImpl::CloudDownloadCallbackImpl(napi_env env, napi_value fun) : env_(env) @@ -283,19 +274,9 @@ napi_value CloudFileDownloadNapi::Stop(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env); }; - NVal thisVar(env, funcArg.GetThisVar()); string procedureName = "cloudFileDownload"; - if (funcArg.GetArgc() == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_; } bool CloudFileDownloadNapi::Export() diff --git a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp index b892024c..4a23c6f4 100644 --- a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp +++ b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp @@ -20,6 +20,7 @@ #include "cloud_sync_manager.h" #include "dfs_error.h" #include "utils_log.h" +#include "async_work.h" #include "uv.h" namespace OHOS::FileManagement::CloudSync { @@ -164,7 +165,7 @@ napi_value GallerySyncNapi::OnCallback(napi_env env, napi_callback_info info) napi_value GallerySyncNapi::OffCallback(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE,NARG_CNT::TWO)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); LOGE("OffCallback Number of arguments unmatched"); return nullptr; @@ -220,18 +221,8 @@ napi_value GallerySyncNapi::Start(napi_env env, napi_callback_info info) }; std::string PROCEDURE_NAME = "Start"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; } napi_value GallerySyncNapi::Stop(napi_env env, napi_callback_info info) @@ -259,18 +250,8 @@ napi_value GallerySyncNapi::Stop(napi_env env, napi_callback_info info) }; std::string PROCEDURE_NAME = "Stop"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; } std::string GallerySyncNapi::GetClassName() diff --git a/interfaces/kits/js/cloudsyncmanager/cloud_sync_manager_n_exporter.cpp b/interfaces/kits/js/cloudsyncmanager/cloud_sync_manager_n_exporter.cpp index 3c07705c..0878dc13 100644 --- a/interfaces/kits/js/cloudsyncmanager/cloud_sync_manager_n_exporter.cpp +++ b/interfaces/kits/js/cloudsyncmanager/cloud_sync_manager_n_exporter.cpp @@ -23,6 +23,7 @@ #include "cloud_sync_manager.h" #include "dfs_error.h" #include "utils_log.h" +#include "async_work.h" namespace OHOS::FileManagement::CloudSync { using namespace FileManagement::LibN; @@ -79,17 +80,8 @@ napi_value ChangeAppCloudSwitch(napi_env env, napi_callback_info info) std::string procedureName = "ChangeAppCloudSwitch"; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == (uint)NARG_CNT::THREE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[(int)NARG_POS::FOURTH]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::FOUR)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; } napi_value NotifyDataChange(napi_env env, napi_callback_info info) @@ -135,18 +127,8 @@ napi_value NotifyDataChange(napi_env env, napi_callback_info info) }; std::string procedureName = "NotifyDataChange"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == (uint)NARG_CNT::TWO) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[(int)NARG_POS::THIRD]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::THREE)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; } napi_value DisableCloud(napi_env env, napi_callback_info info) @@ -185,18 +167,8 @@ napi_value DisableCloud(napi_env env, napi_callback_info info) }; std::string procedureName = "DisableCloud"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[(int)NARG_POS::SECOND]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; } @@ -277,18 +249,8 @@ napi_value EnableCloud(napi_env env, napi_callback_info info) }; std::string procedureName = "EnableCloud"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == (uint)NARG_CNT::TWO) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[(int)NARG_POS::THIRD]); - if (!NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::THREE)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; } bool ParseAppActions(napi_env env, napi_value object, CleanOptions &cleanOptions) @@ -364,18 +326,8 @@ napi_value Clean(napi_env env, napi_callback_info info) }; std::string procedureName = "Clean"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == (uint)NARG_CNT::TWO) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[(int)NARG_POS::THIRD]); - if (!cb.TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::THREE)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; } } // namespace OHOS::FileManagement::CloudSync diff --git a/utils/BUILD.gn b/utils/BUILD.gn index ee6b8537..0d1bf3b9 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -47,6 +47,7 @@ config("compiler_configs") { config("utils_public_config") { include_dirs = [ + "work/include", "log/include", "preference/include", "system/include", @@ -63,6 +64,7 @@ ohos_shared_library("libdistributedfileutils") { "system/src/dfsu_memory_guard.cpp", "system/src/dfsu_mount_argument_descriptors.cpp", "system/src/utils_directory.cpp", + "work/src/async_work.cpp", ] configs = [ "//build/config/compiler:exceptions" ] @@ -84,8 +86,11 @@ ohos_shared_library("libdistributedfileutils") { "access_token:libtokenid_sdk", "c_utils:utils", "c_utils:utilsbase", + "file_api:filemgmt_libhilog", + "file_api:filemgmt_libn", "hilog:libhilog", "ipc:ipc_core", + "napi:ace_napi", "preferences:native_preferences", ] diff --git a/utils/work/include/async_work.h b/utils/work/include/async_work.h new file mode 100644 index 00000000..76f0b400 --- /dev/null +++ b/utils/work/include/async_work.h @@ -0,0 +1,30 @@ +/* + * 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 OHOS_FILEMGMT_ASYNC_WORK_H +#define OHOS_FILEMGMT_ASYNC_WORK_H + +#include "filemgmt_libn.h" +#include "n_func_arg.h" +#include "n_async_context.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { + std::unique_ptr GetPromiseOrCallBackWork(napi_env env, const NFuncArg& funcArg, size_t maxArgSize); +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS +#endif // OHOS_FILEMGMT_ASYNC_WORK_H \ No newline at end of file diff --git a/utils/work/src/async_work.cpp b/utils/work/src/async_work.cpp new file mode 100644 index 00000000..1707fc58 --- /dev/null +++ b/utils/work/src/async_work.cpp @@ -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. + */ + +#include "async_work.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { + +std::unique_ptr GetPromiseOrCallBackWork(napi_env env, const NFuncArg &funcArg, size_t maxArgSize) +{ + std::unique_ptr asyncWork = nullptr; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() != maxArgSize) { + asyncWork = std::make_unique(env, thisVar); + } else { + NVal cb(env, funcArg[(int)maxArgSize - 1]); + if (cb.TypeIs(napi_function)) { + asyncWork = std::make_unique(env, thisVar, cb); + } else { + LOGE("Argument type mismatch"); + NError(E_PARAMS).ThrowErr(env); + } + } + return asyncWork; +} +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file