消除静态检查告警,分解大函数

Signed-off-by: Luyao Wang <wangluyao35@huawei.com>
Change-Id: I39cfd2e1a137f65ba41455bc942dd6bc92ce8f03
This commit is contained in:
Luyao Wang 2023-09-20 17:04:24 +08:00
parent 2f4f170abb
commit 0d88df6097
6 changed files with 99 additions and 108 deletions

View File

@ -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<size_t>(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<size_t>(NARG_CNT::TWO));
return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_;
}
bool CloudFileDownloadNapi::Export()

View File

@ -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 {
@ -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<size_t>(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<size_t>(NARG_CNT::TWO));
return asyncWork == nullptr ? nullptr : asyncWork->Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_;
}
std::string GallerySyncNapi::GetClassName()

View File

@ -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<size_t>(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<size_t>(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<size_t>(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<size_t>(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<size_t>(NARG_CNT::THREE));
return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_;
}
} // namespace OHOS::FileManagement::CloudSync

View File

@ -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",
]

View File

@ -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<NAsyncWork> GetPromiseOrCallBackWork(napi_env env, const NFuncArg& funcArg, size_t maxArgSize);
} // namespace LibN
} // namespace FileManagement
} // namespace OHOS
#endif // OHOS_FILEMGMT_ASYNC_WORK_H

View File

@ -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<NAsyncWork> GetPromiseOrCallBackWork(napi_env env, const NFuncArg &funcArg, size_t maxArgSize)
{
std::unique_ptr<NAsyncWork> asyncWork = nullptr;
NVal thisVar(env, funcArg.GetThisVar());
if (funcArg.GetArgc() != maxArgSize) {
asyncWork = std::make_unique<NAsyncWorkPromise>(env, thisVar);
} else {
NVal cb(env, funcArg[(int)maxArgSize - 1]);
if (cb.TypeIs(napi_function)) {
asyncWork = std::make_unique<NAsyncWorkCallback>(env, thisVar, cb);
} else {
LOGE("Argument type mismatch");
NError(E_PARAMS).ThrowErr(env);
}
}
return asyncWork;
}
} // namespace LibN
} // namespace FileManagement
} // namespace OHOS