回退 'Pull Request !5271 : 冗余代码删除'

This commit is contained in:
jerry
2023-04-20 08:31:44 +00:00
committed by Gitee
parent b541ad1980
commit 0860d2abd7
3 changed files with 69 additions and 2 deletions
@@ -54,4 +54,5 @@ errormanager("errormanager_napi") {
errormanager("errormanager") {
relative_install_dir = "module/app/ability"
defines = [ "ENABLE_ERRCODE" ]
}
@@ -17,6 +17,7 @@
#include "native_engine/native_engine.h"
extern "C" __attribute__((constructor))
#ifdef ENABLE_ERRCODE
void NAPI_app_ability_ErrorManager_AutoRegister()
{
NativeModule newModuleInfo = {
@@ -28,3 +29,16 @@ void NAPI_app_ability_ErrorManager_AutoRegister()
auto moduleManager = NativeModuleManager::GetInstance();
moduleManager->Register(&newModuleInfo);
}
#else
void NAPI_application_ErrorManager_AutoRegister()
{
NativeModule newModuleInfo = {
.name = "application.errorManager",
.fileName = "application/errormanager_napi.so/error_manager.js",
.registerCallback = OHOS::AbilityRuntime::JsErrorManagerInit,
};
auto moduleManager = NativeModuleManager::GetInstance();
moduleManager->Register(&newModuleInfo);
}
#endif
@@ -29,11 +29,19 @@
namespace OHOS {
namespace AbilityRuntime {
namespace {
#ifndef ENABLE_ERRCODE
constexpr int32_t ERROR_CODE = -1;
constexpr int32_t INVALID_PARAM = -2;
#endif
constexpr int32_t INDEX_ZERO = 0;
constexpr int32_t INDEX_ONE = 1;
constexpr size_t ARGC_TWO = 2;
#ifdef ENABLE_ERRCODE
constexpr int32_t INDEX_TWO = 2;
constexpr size_t ARGC_THREE = 3;
#else
constexpr size_t ARGC_ONE = 1;
#endif
class JsErrorManager final {
public:
@@ -42,7 +50,7 @@ public:
static void Finalizer(NativeEngine* engine, void* data, void* hint)
{
HILOG_DEBUG("JsErrorManager Finalizer is called");
HILOG_INFO("JsErrorManager Finalizer is called");
std::unique_ptr<JsErrorManager>(static_cast<JsErrorManager*>(data));
}
@@ -62,6 +70,7 @@ private:
NativeValue* OnRegisterErrorObserver(NativeEngine& engine, const NativeCallbackInfo& info)
{
HILOG_INFO("Register errorObserver is called.");
#ifdef ENABLE_ERRCODE
if (info.argc != ARGC_TWO) {
HILOG_ERROR("The param is invalid, observers need.");
ThrowTooFewParametersError(engine);
@@ -74,7 +83,13 @@ private:
ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
return engine.CreateUndefined();
}
#else
// only support one
if (info.argc != ARGC_ONE) {
HILOG_ERROR("The param is invalid, observers need.");
return engine.CreateUndefined();
}
#endif
int32_t observerId = serialNumber_;
if (serialNumber_ < INT32_MAX) {
serialNumber_++;
@@ -87,13 +102,18 @@ private:
observer_ = std::make_shared<JsErrorObserver>(engine);
AppExecFwk::ApplicationDataManager::GetInstance().AddErrorObserver(observer_);
}
#ifdef ENABLE_ERRCODE
observer_->AddJsObserverObject(observerId, info.argv[INDEX_ONE]);
#else
observer_->AddJsObserverObject(observerId, info.argv[INDEX_ZERO]);
#endif
return engine.CreateNumber(observerId);
}
NativeValue* OnUnregisterErrorObserver(NativeEngine& engine, NativeCallbackInfo& info)
{
int32_t observerId = -1;
#ifdef ENABLE_ERRCODE
if (info.argc != ARGC_TWO && info.argc != ARGC_THREE) {
ThrowTooFewParametersError(engine);
HILOG_ERROR("unregister errorObserver error, not enough params.");
@@ -109,6 +129,16 @@ private:
ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
return engine.CreateUndefined();
}
#else
// only support one or two params
if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
HILOG_ERROR("unregister errorObserver error, not enough params.");
} else {
napi_get_value_int32(reinterpret_cast<napi_env>(&engine),
reinterpret_cast<napi_value>(info.argv[INDEX_ZERO]), &observerId);
HILOG_INFO("unregister errorObserver called, observer:%{public}d", observerId);
}
#endif
std::weak_ptr<JsErrorObserver> observerWptr(observer_);
AsyncTask::CompleteCallback complete =
@@ -116,22 +146,38 @@ private:
NativeEngine& engine, AsyncTask& task, int32_t status) {
HILOG_INFO("Unregister errorObserver called.");
if (observerId == -1) {
#ifdef ENABLE_ERRCODE
task.Reject(engine, CreateJsError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
#else
task.Reject(engine, CreateJsError(engine, INVALID_PARAM, "param is invalid!"));
#endif
return;
}
auto observer = observerWptr.lock();
bool isEmpty = false;
if (observer && observer->RemoveJsObserverObject(observerId, isEmpty)) {
#ifdef ENABLE_ERRCODE
task.ResolveWithNoError(engine, engine.CreateUndefined());
#else
task.Resolve(engine, engine.CreateUndefined());
#endif
} else {
#ifdef ENABLE_ERRCODE
task.Reject(engine, CreateJsError(engine, AbilityErrorCode::ERROR_CODE_INVALID_ID));
#else
task.Reject(engine, CreateJsError(engine, ERROR_CODE, "observer is not exist!"));
#endif
}
if (isEmpty) {
AppExecFwk::ApplicationDataManager::GetInstance().RemoveErrorObserver();
}
};
#ifdef ENABLE_ERRCODE
NativeValue* lastParam = (info.argc <= ARGC_TWO) ? nullptr : info.argv[INDEX_TWO];
#else
NativeValue* lastParam = (info.argc <= ARGC_ONE) ? nullptr : info.argv[INDEX_ONE];
#endif
NativeValue* result = nullptr;
AsyncTask::Schedule("JSErrorManager::OnUnregisterErrorObserver",
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
@@ -162,8 +208,14 @@ NativeValue* JsErrorManagerInit(NativeEngine* engine, NativeValue* exportObj)
HILOG_INFO("JsErrorManager BindNativeFunction called");
const char *moduleName = "JsErrorManager";
#ifdef ENABLE_ERRCODE
BindNativeFunction(*engine, *object, "on", moduleName, JsErrorManager::RegisterErrorObserver);
BindNativeFunction(*engine, *object, "off", moduleName, JsErrorManager::UnregisterErrorObserver);
#else
BindNativeFunction(*engine, *object, "registerErrorObserver", moduleName, JsErrorManager::RegisterErrorObserver);
BindNativeFunction(*engine, *object, "unregisterErrorObserver",
moduleName, JsErrorManager::UnregisterErrorObserver);
#endif
return engine->CreateUndefined();
}
} // namespace AbilityRuntime