From 0e5fa45395cb2fd3370922eb173676dc8140f1ca Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Sun, 11 Sep 2022 15:23:55 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E3=80=90FA=E6=8E=A5=E5=8F=A3=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E3=80=91Napi=E6=94=B9=E9=80=A0Context=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xinxin13 --- frameworks/js/napi/featureAbility/BUILD.gn | 1 + .../js/napi/featureAbility/napi_context.cpp | 997 ++++++++++++++++++ .../js/napi/featureAbility/napi_context.h | 10 + frameworks/js/napi/inner/napi_common/BUILD.gn | 6 +- .../napi_common/js_napi_common_ability.h | 124 +++ .../inner/napi_common/napi_common_ability.cpp | 855 +++++++++++++++ frameworks/js/napi/particleAbility/BUILD.gn | 2 + frameworks/native/ability/native/BUILD.gn | 1 + 8 files changed, 1995 insertions(+), 1 deletion(-) create mode 100644 frameworks/js/napi/inner/napi_common/js_napi_common_ability.h diff --git a/frameworks/js/napi/featureAbility/BUILD.gn b/frameworks/js/napi/featureAbility/BUILD.gn index 32b8225e081..fcf3661ccc6 100644 --- a/frameworks/js/napi/featureAbility/BUILD.gn +++ b/frameworks/js/napi/featureAbility/BUILD.gn @@ -38,6 +38,7 @@ ohos_shared_library("featureability") { deps = [ "${ability_runtime_innerkits_path}/dataobs_manager:dataobs_manager", "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_native_path}/appkit:appkit_native", "//third_party/libuv:uv", ] diff --git a/frameworks/js/napi/featureAbility/napi_context.cpp b/frameworks/js/napi/featureAbility/napi_context.cpp index 5b973b7fd2b..e33abc889c0 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -25,9 +25,11 @@ #include "file_ex.h" #include "hilog_wrapper.h" #include "securec.h" +#include "js_napi_common_ability.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; namespace OHOS { namespace AppExecFwk { @@ -2802,5 +2804,1000 @@ napi_value NAPI_SetWakeUpScreen(napi_env env, napi_callback_info info) return nullptr; #endif } + +class NapiJsContext : public JsNapiCommon { +public: + NapiJsContext() = default; + virtual ~NapiJsContext() = default; + + static void Finalizer(NativeEngine *engine, void *data, void *hint) + { + HILOG_DEBUG("called."); + std::unique_ptr(static_cast(data)); + }; + + static NativeValue* JsRequestPermissionsFromUser(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetBundleName(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsVerifyPermission(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetApplicationInfo(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetProcessInfo(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetElementName(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetProcessName(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetCallingBundle(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetOrCreateLocalDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetFilesDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsIsUpdatingConfigurations(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsPrintDrawnCompleted(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetCacheDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetCtxAppType(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetCtxHapModuleInfo(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetAppVersionInfo(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetCtxAbilityInfo(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsSetShowOnLockScreen(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetOrCreateDistributedDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsSetWakeUpScreen(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsSetDisplayOrientation(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* JsGetDisplayOrientation(NativeEngine *engine, NativeCallbackInfo *info); + + bool DataInit(NativeEngine &engine); + +private: + NativeValue* OnRequestPermissionsFromUser(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetBundleName(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnVerifyPermission(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetApplicationInfo(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetProcessInfo(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetElementName(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetProcessName(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetCallingBundle(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetOrCreateLocalDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnSetShowOnLockScreen(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnSetWakeUpScreen(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnSetDisplayOrientation(NativeEngine &engine, NativeCallbackInfo &info); +}; + +static bool BindNapiJSContextFunction(NativeEngine &engine, NativeObject* object) +{ + HILOG_DEBUG("called"); + if (object == nullptr) { + HILOG_ERROR("input params error"); + return false; + } + + const char* moduleName = "context"; + BindNativeFunction( + engine, *object, "requestPermissionsFromUser", moduleName, NapiJsContext::JsRequestPermissionsFromUser); + BindNativeFunction(engine, *object, "getBundleName", moduleName, NapiJsContext::JsGetBundleName); + BindNativeFunction(engine, *object, "verifyPermission", moduleName, NapiJsContext::JsVerifyPermission); + BindNativeFunction(engine, *object, "getApplicationInfo", moduleName, NapiJsContext::JsGetApplicationInfo); + BindNativeFunction(engine, *object, "getProcessInfo", moduleName, NapiJsContext::JsGetProcessInfo); + BindNativeFunction(engine, *object, "getElementName", moduleName, NapiJsContext::JsGetElementName); + BindNativeFunction(engine, *object, "getProcessName", moduleName, NapiJsContext::JsGetProcessName); + BindNativeFunction(engine, *object, "getCallingBundle", moduleName, NapiJsContext::JsGetCallingBundle); + BindNativeFunction(engine, *object, "getOrCreateLocalDir", moduleName, NapiJsContext::JsGetOrCreateLocalDir); + BindNativeFunction(engine, *object, "getFilesDir", moduleName, NapiJsContext::JsGetFilesDir); + BindNativeFunction( + engine, *object, "isUpdatingConfigurations", moduleName, NapiJsContext::JsIsUpdatingConfigurations); + BindNativeFunction(engine, *object, "printDrawnCompleted", moduleName, NapiJsContext::JsPrintDrawnCompleted); + BindNativeFunction(engine, *object, "getCacheDir", moduleName, NapiJsContext::JsGetCacheDir); + BindNativeFunction(engine, *object, "getAppType", moduleName, NapiJsContext::JsGetCtxAppType); + BindNativeFunction(engine, *object, "getHapModuleInfo", moduleName, NapiJsContext::JsGetCtxHapModuleInfo); + BindNativeFunction(engine, *object, "getAppVersionInfo", moduleName, NapiJsContext::JsGetAppVersionInfo); + BindNativeFunction(engine, *object, "getApplicationContext", moduleName, NapiJsContext::JsGetApplicationContext); + BindNativeFunction(engine, *object, "getAbilityInfo", moduleName, NapiJsContext::JsGetCtxAbilityInfo); + BindNativeFunction(engine, *object, "setShowOnLockScreen", moduleName, NapiJsContext::JsSetShowOnLockScreen); + BindNativeFunction( + engine, *object, "getOrCreateDistributedDir", moduleName, NapiJsContext::JsGetOrCreateDistributedDir); + BindNativeFunction( + engine, *object, "setWakeUpScreen", moduleName, NapiJsContext::JsSetWakeUpScreen); + BindNativeFunction(engine, *object, "setDisplayOrientation", moduleName, NapiJsContext::JsSetDisplayOrientation); + BindNativeFunction(engine, *object, "getDisplayOrientation", moduleName, NapiJsContext::JsGetDisplayOrientation); + + return true; +} + +static NativeValue* ConstructNapiJSContext(NativeEngine &engine) +{ + HILOG_DEBUG("called"); + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return nullptr; + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return nullptr; + } + + auto jsCalss = std::make_unique(); + if (jsCalss == nullptr) { + HILOG_ERROR("new NapiJsContext failed"); + return nullptr; + } + + if (!jsCalss->DataInit(engine)) { + HILOG_ERROR("NapiJsContext init failed"); + return nullptr; + } + + object->SetNativePointer(jsCalss.release(), NapiJsContext::Finalizer, nullptr); + object->SetProperty("stageMode", engine.CreateBoolean(false)); + + if (!BindNapiJSContextFunction(engine, object)) { + HILOG_ERROR("bind func failed"); + return nullptr; + } + + return objContext; +} + +NativeValue* CreateNapiJSContext(NativeEngine &engine) +{ + HILOG_DEBUG("called"); + auto jsObj = ConstructNapiJSContext(engine); + if (jsObj == nullptr) { + HILOG_ERROR("Construct Context failed"); + return engine.CreateUndefined(); + } + + return jsObj; +} + +NativeValue* NapiJsContext::JsRequestPermissionsFromUser(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnRequestPermissionsFromUser(*engine, *info); +} +NativeValue* NapiJsContext::JsGetBundleName(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetBundleName(*engine, *info); +} +NativeValue* NapiJsContext::JsVerifyPermission(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnVerifyPermission(*engine, *info); +} +NativeValue* NapiJsContext::JsGetApplicationInfo(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetApplicationInfo(*engine, *info); +} +NativeValue* NapiJsContext::JsGetProcessInfo(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetProcessInfo(*engine, *info); +} +NativeValue* NapiJsContext::JsGetElementName(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetElementName(*engine, *info); +} +NativeValue* NapiJsContext::JsGetProcessName(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetProcessName(*engine, *info); +} +NativeValue* NapiJsContext::JsGetCallingBundle(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetCallingBundle(*engine, *info); +} +NativeValue* NapiJsContext::JsGetOrCreateLocalDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnGetOrCreateLocalDir(*engine, *info); +} +NativeValue* NapiJsContext::JsGetFilesDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetFilesDir(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsIsUpdatingConfigurations(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsIsUpdatingConfigurations(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsPrintDrawnCompleted(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsPrintDrawnCompleted(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsGetCacheDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetCacheDir(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsGetCtxAppType(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetCtxAppType(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsGetCtxHapModuleInfo(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetCtxHapModuleInfo(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsGetAppVersionInfo(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetAppVersionInfo(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsGetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetApplicationContext(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsGetCtxAbilityInfo(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetCtxAbilityInfo(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsSetShowOnLockScreen(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnSetShowOnLockScreen(*engine, *info); +} +NativeValue* NapiJsContext::JsGetOrCreateDistributedDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetOrCreateDistributedDir(*engine, *info, AbilityType::PAGE); +} +NativeValue* NapiJsContext::JsSetWakeUpScreen(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnSetWakeUpScreen(*engine, *info); +} +NativeValue* NapiJsContext::JsSetDisplayOrientation(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->OnSetDisplayOrientation(*engine, *info); +} +NativeValue* NapiJsContext::JsGetDisplayOrientation(NativeEngine *engine, NativeCallbackInfo *info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_ERROR("but input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info")); + return nullptr; + } + + auto object = CheckParamsAndGetThis(engine, info); + if (object == nullptr) { + HILOG_ERROR("CheckParamsAndGetThis return nullptr"); + return engine->CreateUndefined(); + } + + return object->JsNapiCommon::JsGetDisplayOrientation(*engine, *info, AbilityType::PAGE); +} + +bool NapiJsContext::DataInit(NativeEngine &engine) +{ + HILOG_DEBUG("called"); + value_ = 0; + napi_value global = 0; + napi_value abilityObj = 0; + auto env = reinterpret_cast(&engine); + HILOG_INFO("Get Ability to start"); + NAPI_CALL_BASE(env, napi_get_global(env, &global), false); + NAPI_CALL_BASE(env, napi_get_named_property(env, global, "ability", &abilityObj), false); + NAPI_CALL_BASE(env, napi_get_value_external(env, abilityObj, (void **)&ability_), false); + HILOG_INFO("Get Ability to done"); + + return true; +} + + +NativeValue* NapiJsContext::OnRequestPermissionsFromUser(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc == ARGS_ZERO || info.argc > ARGS_THREE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = 0; + std::vector permissionList; + if(!GetStringsValue(engine, info.argv[PARAM0], permissionList)) { + HILOG_ERROR("input params string error"); + return engine.CreateUndefined(); + } + int32_t value = 0; + if(!ConvertFromJsValue(engine, info.argv[PARAM1], value)) { + HILOG_ERROR("input params int error"); + return engine.CreateUndefined(); + } + CallbackInfo callbackInfo; + auto execute = [obj = this, permissionList, value, cbInfo = callbackInfo] () { + if (permissionList.empty()) { + obj->value_ = static_cast(NAPI_ERR_PARAM_INVALID); + return; + } + CallAbilityPermissionParam permissionParam; + permissionParam.requestCode = value; + permissionParam.permission_list = permissionList; + auto processInstance = AbilityProcess::GetInstance(); + if (processInstance != nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + return; + } + processInstance->RequestPermissionsFromUser(obj->ability_, permissionParam, cbInfo); + }; + auto complete = [obj = this] (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != 0) { + task.Reject(engine, CreateJsError(engine, obj->value_, obj->ConvertErrorCode(obj->value_))); + return; + } + task.Resolve(engine, CreateJsValue(engine, obj->value_)); + }; + + auto callback = info.argc == ARGS_THREE ? info.argv[PARAM2] : nullptr; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnRequestPermissionsFromUser", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + return result; +} + +NativeValue* NapiJsContext::OnGetBundleName(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateNull(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr bundleName = std::make_shared(); + auto execute = [obj = this, name = bundleName] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (name == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, name is nullptr"); + return; + } + name->name = obj->ability_->GetBundleName(); + }; + auto complete = [obj = this, name = bundleName] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { + auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, name->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetBundleName", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnVerifyPermission(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc == ARGS_ZERO || info.argc > ARGS_THREE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateNull(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::string permission(""); + if (!ConvertFromJsValue(engine, info.argv[PARAM0], permission)) { + HILOG_ERROR("input params string error"); + return engine.CreateNull(); + } + JsPermissionOptions options; + bool flagCall = UnwarpVerifyPermissionParams(engine, info, options); + auto execute = [obj = this, permission, options] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (options.uidFlag) { + obj->value_ = obj->ability_->VerifyPermission(permission, options.pid, options.uid); + } else { + obj->value_ = obj->ability_->VerifySelfPermission(permission); + } + }; + auto complete = [obj = this] (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ == static_cast(NAPI_ERR_ACE_ABILITY)) { + task.Reject(engine, CreateJsError( engine, obj->value_,obj->ConvertErrorCode(obj->value_))); + return; + } + task.Resolve(engine, CreateJsValue(engine, obj->value_)); + }; + + auto callback = flagCall ? + ((info.argc == ARGS_TWO) ? info.argv[PARAM1] : info.argv[PARAM2]) : + nullptr; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetBundleName", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnGetApplicationInfo(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, info = infoData] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + auto getInfo = obj->ability_->GetApplicationInfo(); + if (getInfo != nullptr && info != nullptr) { + info->appInfo = *getInfo; + } else { + HILOG_ERROR("GetApplicationInfo return nullptr"); + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, obj->CreateAppInfo(engine, info->appInfo)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetApplicationInfo", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnGetProcessInfo(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr processInfo = std::make_shared(); + auto execute = [obj = this, data = processInfo] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + auto getInfo = obj->ability_->GetProcessInfo(); + if (getInfo != nullptr && data != nullptr) { + data->processName = getInfo->GetProcessName(); + data->pid = getInfo->GetPid(); + } else { + HILOG_ERROR("GetProcessInfo return nullptr"); + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = processInfo] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? (NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, obj->CreateProcessInfo(engine, info)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetProcessInfo", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnGetElementName(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr elementName = std::make_shared(); + auto execute = [obj = this, data = elementName] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + auto elementName = obj->ability_->GetElementName(); + if (elementName != nullptr && data != nullptr) { + data->deviceId = elementName->GetDeviceID(); + data->bundleName = elementName->GetBundleName(); + data->abilityName = elementName->GetAbilityName(); + data->uri = obj->ability_->GetWant()->GetUriString(); + data->shortName = ""; + } else { + HILOG_ERROR("GetElementName return nullptr"); + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, ename = elementName] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || ename == nullptr) { + auto ecode = ename == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, obj->CreateElementName(engine, ename)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetElementName", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnGetProcessName(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr processName = std::make_shared(); + auto execute = [obj = this, name = processName] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (name == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, name is nullptr"); + return; + } + name->name = obj->ability_->GetProcessName(); + }; + auto complete = [obj = this, name = processName] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { + auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, name->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetProcessName", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} +NativeValue* NapiJsContext::OnGetCallingBundle(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr callingBundleName = std::make_shared(); + auto execute = [obj = this, name = callingBundleName] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (name == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, name is nullptr"); + return; + } + name->name = obj->ability_->GetCallingBundle(); + }; + auto complete = [obj = this, name = callingBundleName] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { + auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, name->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetCallingBundle", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnGetOrCreateLocalDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr createDir = std::make_shared(); + auto execute = [obj = this, dir = createDir] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr || dir == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, the abilitycontext is nullptr"); + return; + } + dir->name = context->GetBaseDir(); + if (!OHOS::FileExists(dir->name)) { + HILOG_INFO("dir is not exits, create dir."); + OHOS::ForceCreateDirectory(dir->name); + OHOS::ChangeModeDirectory(dir->name, MODE); + } + }; + auto complete = [obj = this, dir = createDir] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, dir->name)); + }; + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnGetOrCreateLocalDir", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnSetShowOnLockScreen(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc == ARGS_ZERO || info.argc > ARGS_TWO) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + bool isShow = false; + if (!ConvertFromJsValue(engine, info.argv[PARAM0], isShow)) { + HILOG_ERROR("input params int error"); + return engine.CreateUndefined(); + } + auto complete = [obj = this, isShow] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->ability_ == nullptr) { + task.Reject(engine, + CreateJsError(engine, static_cast(NAPI_ERR_ACE_ABILITY), "get ability error")); + return; + } + obj->ability_->SetShowOnLockScreen(isShow); + task.Resolve(engine, engine.CreateNull()); + }; + + auto callback = info.argc == ARGS_ONE ? nullptr : info.argv[PARAM1]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnSetShowOnLockScreen", + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); + + return result; +} + +NativeValue* NapiJsContext::OnSetWakeUpScreen(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc == ARGS_ZERO || info.argc > ARGS_TWO) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + bool wakeUp = false; + if (!ConvertFromJsValue(engine, info.argv[PARAM0], wakeUp)) { + HILOG_ERROR("input params int error"); + return engine.CreateUndefined(); + } + auto complete = [obj = this, wakeUp] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->ability_ == nullptr) { + task.Reject(engine, + CreateJsError(engine, static_cast(NAPI_ERR_ACE_ABILITY), "get ability error")); + return; + } + obj->ability_->SetWakeUpScreen(wakeUp); + task.Resolve(engine, engine.CreateNull()); + }; + + auto callback = info.argc == ARGS_ONE ? nullptr : info.argv[PARAM1]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::OnSetWakeUpScreen", + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); + + return result; +} +NativeValue* NapiJsContext::OnSetDisplayOrientation(NativeEngine &engine, NativeCallbackInfo &info) +{ + HILOG_DEBUG("called"); + if (info.argc == ARGS_ZERO || info.argc > ARGS_TWO) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + int32_t orientation = 0; + if (!ConvertFromJsValue(engine, info.argv[PARAM0], orientation)) { + HILOG_ERROR("input params int error"); + return engine.CreateUndefined(); + } + auto complete = [obj = this, orientation] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->ability_ == nullptr) { + task.Reject(engine, + CreateJsError(engine, static_cast(NAPI_ERR_ACE_ABILITY), "get ability error")); + return; + } + obj->ability_->SetDisplayOrientation(orientation); + task.Resolve(engine, CreateJsValue(engine, 1)); + }; + + auto callback = info.argc == ARGS_ONE ? nullptr : info.argv[PARAM1]; + NativeValue *result = nullptr; + AsyncTask::Schedule("NapiJsContext::SetDisplayOrientation", + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); + + return result; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/js/napi/featureAbility/napi_context.h b/frameworks/js/napi/featureAbility/napi_context.h index c9175c67ff3..e396390b830 100644 --- a/frameworks/js/napi/featureAbility/napi_context.h +++ b/frameworks/js/napi/featureAbility/napi_context.h @@ -20,6 +20,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "js_runtime_utils.h" using Ability = OHOS::AppExecFwk::Ability; #define MODE 0771 @@ -197,6 +198,15 @@ void SetDisplayOrientationExecuteCallbackWork(napi_env env, void *data); * @return The return value from NAPI C++ to JS for the module. */ napi_value NAPI_GetDisplayOrientation(napi_env env, napi_callback_info info); + +/** + * @brief Get the application context + * + * @param engine Native JS engine. + * + * @return The return value from C++ to JS for the module. + */ +NativeValue* CreateNapiJSContext(NativeEngine &engine); } // namespace AppExecFwk } // namespace OHOS #endif /* OHOS_ABILITY_RUNTIME_NAPI_CONTEXT_H */ diff --git a/frameworks/js/napi/inner/napi_common/BUILD.gn b/frameworks/js/napi/inner/napi_common/BUILD.gn index 8b3034de90e..32e1618c17a 100644 --- a/frameworks/js/napi/inner/napi_common/BUILD.gn +++ b/frameworks/js/napi/inner/napi_common/BUILD.gn @@ -19,7 +19,10 @@ config("napi_common_public_config") { } ohos_shared_library("napi_common") { - include_dirs = [ "//third_party/node/src" ] + include_dirs = [ + "${ability_runtime_napi_path}/featureAbility", + "//third_party/node/src", + ] public_configs = [ ":napi_common_public_config" ] @@ -40,6 +43,7 @@ ohos_shared_library("napi_common") { "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", "ability_runtime:napi_base_context", + "ability_runtime:runtime", "ability_runtime:wantagent_innerkits", "bundle_framework:appexecfwk_base", "c_utils:utils", diff --git a/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h b/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h new file mode 100644 index 00000000000..572570c69e0 --- /dev/null +++ b/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2021 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_ABILITY_RUNTIME_JS_NAPI_COMMON_ABILITY_H +#define OHOS_ABILITY_RUNTIME_JS_NAPI_COMMON_ABILITY_H +#include "ability_connect_callback_stub.h" +#include "ability_info.h" +#include "ability_manager_errors.h" +#include "application_info.h" +#include "feature_ability_common.h" +#include "js_runtime_utils.h" + +namespace OHOS { +namespace AppExecFwk { +class JsNapiCommon { +public: + JsNapiCommon(); + virtual ~JsNapiCommon() = default; + + struct JsPermissionOptions { + bool uidFlag = false; + bool pidFlag = false; + int32_t uid = 0; + int32_t pid = 0; + }; + + struct JsApplicationInfo { + ApplicationInfo appInfo; + }; + + struct JsBundleName { + std::string name = ""; + }; + typedef JsBundleName JsProcessName; + typedef JsBundleName JsCallingBundleName; + typedef JsBundleName JsOrCreateLocalDir; + typedef JsBundleName JsFilesDir; + typedef JsBundleName JsCacheDir; + typedef JsBundleName JsCtxAppType; + typedef JsBundleName JsOrCreateDistributedDir; + typedef JsBundleName JsAppType; + + struct JsElementName { + std::string deviceId = ""; + std::string bundleName = ""; + std::string abilityName = ""; + std::string uri = ""; + std::string shortName = ""; + }; + + struct JsProcessInfo { + std::string processName = ""; + pid_t pid = 0; + }; + + struct JsConfigurations { + bool status; + }; + typedef JsConfigurations JsDrawnCompleted; + + struct JsHapModuleInfo { + HapModuleInfo hapModInfo; + }; + + struct JsAbilityInfoInfo { + AbilityInfo abilityInfo; + }; + + struct JsWant { + Want want; + }; + + NativeValue* JsGetContext(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsIsUpdatingConfigurations( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsPrintDrawnCompleted(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetCtxAppType(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetCtxHapModuleInfo(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetAppVersionInfo(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetApplicationContext( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetCtxAbilityInfo(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetOrCreateDistributedDir( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + NativeValue* JsGetDisplayOrientation( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + + NativeValue* CreateProcessInfo(NativeEngine &engine, const std::shared_ptr &processInfo); + NativeValue* CreateElementName(NativeEngine &engine, const std::shared_ptr &elementName); + NativeValue* CreateHapModuleInfo(NativeEngine &engine, const std::shared_ptr &hapModInfo); + NativeValue* CreateModuleInfo(NativeEngine &engine, const ModuleInfo &modInfo); + NativeValue* CreateModuleInfos(NativeEngine &engine, const std::vector &moduleInfos); + NativeValue* CreateAppInfo(NativeEngine &engine, const ApplicationInfo &appInfo); + NativeValue* CreateAppInfo(NativeEngine &engine, const std::shared_ptr &appInfo); + NativeValue* CreateAbilityInfo(NativeEngine &engine, const AbilityInfo &abilityInfo); + NativeValue* CreateAbilityInfo(NativeEngine &engine, const std::shared_ptr &abilityInfo); + NativeValue* CreateAbilityInfos(NativeEngine &engine, const std::vector &abilityInfos); + NativeValue* CreateAppVersionInfo(NativeEngine &engine, const std::shared_ptr &appInfo); + bool CheckAbilityType(const AbilityType typeWant); + bool UnwarpVerifyPermissionParams(NativeEngine &engine, NativeCallbackInfo &info, JsPermissionOptions &options); + bool GetStringsValue(NativeEngine &engine, NativeValue *object, std::vector &strList); + bool GetPermissionOptions(NativeEngine &engine, NativeValue *object, JsPermissionOptions &strVal); + std::string ConvertErrorCode(int32_t errCode); + + Ability *ability_; + int32_t value_; +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_JS_NAPI_COMMON_ABILITY_H \ No newline at end of file diff --git a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp index fc2c9a69202..d09695eef69 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp @@ -18,11 +18,15 @@ #include #include +#include "js_runtime_utils.h" +#include "js_napi_common_ability.h" #include "hilog_wrapper.h" #include "napi_common_util.h" #include "napi_base_context.h" #include "napi_remote_object.h" #include "securec.h" +#include "napi_context.h" +using namespace OHOS::AbilityRuntime; namespace OHOS { namespace AppExecFwk { @@ -4802,5 +4806,856 @@ napi_value NAPI_TerminateAbilityCommon(napi_env env, napi_callback_info info) HILOG_INFO("%{public}s,end", __func__); return ret; } + +JsNapiCommon::JsNapiCommon() : ability_(nullptr), value_(0) +{} + +NativeValue* JsNapiCommon::JsGetContext(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + if (!CheckAbilityType(abilityType)) { + HILOG_ERROR("ability type error"); + return engine.CreateUndefined(); + } + + return CreateNapiJSContext(engine); +} + +NativeValue* JsNapiCommon::JsGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr filesDir = std::make_shared(); + auto execute = [obj = this, dir = filesDir, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, the abilitycontext is nullptr"); + return; + } + dir->name = context->GetFilesDir(); + }; + auto complete = [obj = this, dir = filesDir] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, dir->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetFilesDir", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + return result; +} + +NativeValue* JsNapiCommon::JsIsUpdatingConfigurations( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr config = std::make_shared(); + auto execute = [obj = this, data = config, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + if (data == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, param is nullptr"); + return; + } + data->status = obj->ability_->IsUpdatingConfigurations(); + }; + auto complete = [obj = this, info = config] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, engine.CreateBoolean(info->status)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsIsUpdatingConfigurations", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsPrintDrawnCompleted( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr drawComplete = std::make_shared(); + auto execute = [obj = this, data = drawComplete, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + if (data == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, name is nullptr"); + return; + } + data->status = obj->ability_->PrintDrawnCompleted(); + }; + auto complete = [obj = this, draw = drawComplete] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || draw == nullptr) { + auto ecode = draw == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, engine.CreateNull()); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsPrintDrawnCompleted", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr cacheDir = std::make_shared(); + auto execute = [obj = this, dir = cacheDir, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, the abilitycontext is nullptr"); + return; + } + dir->name = context->GetCacheDir(); + }; + auto complete = [obj = this, dir = cacheDir] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, dir->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetCacheDir", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetCtxAppType( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr type = std::make_shared(); + auto execute = [obj = this, apptype = type, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + if (apptype == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + return; + } + apptype->name = obj->ability_->GetAppType(); + }; + auto complete = [obj = this, apptype = type] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || apptype == nullptr) { + auto ecode = apptype == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, apptype->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetCtxAppType", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetCtxHapModuleInfo( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, hapMod = infoData, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto getInfo = obj->ability_->GetHapModuleInfo(); + if (getInfo != nullptr && hapMod != nullptr) { + hapMod->hapModInfo = *getInfo; + } else { + HILOG_ERROR("GetHapModuleInfo return nullptr"); + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, obj->CreateHapModuleInfo(engine, info)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetCtxHapModuleInfo", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetAppVersionInfo( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, appInfo = infoData, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto getInfo = obj->ability_->GetApplicationInfo(); + if (getInfo != nullptr && appInfo != nullptr) { + appInfo->appInfo = *getInfo; + } else { + HILOG_ERROR("GetApplicationInfo return nullptr"); + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, obj->CreateAppVersionInfo(engine, info)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetAppVersionInfo", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetApplicationContext( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + if (!CheckAbilityType(abilityType)) { + HILOG_ERROR("ability type error"); + return engine.CreateUndefined(); + } + + return CreateNapiJSContext(engine); +} + +NativeValue* JsNapiCommon::JsGetCtxAbilityInfo( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, abilityInfo = infoData, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto getInfo = obj->ability_->GetAbilityInfo(); + if (getInfo != nullptr && abilityInfo != nullptr) { + abilityInfo->abilityInfo = *getInfo; + } else { + HILOG_ERROR("GetAbilityInfo return nullptr"); + obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, obj->CreateAbilityInfo(engine, info)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetCtxAbilityInfo", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetOrCreateDistributedDir( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + std::shared_ptr orCreateDistributedDir = std::make_shared(); + auto execute = [obj = this, dir = orCreateDistributedDir, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the abilitycontext is nullptr"); + return; + } + dir->name = context->GetDistributedFilesDir(); + }; + + auto complete = [obj = this, dir = orCreateDistributedDir] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); + return; + } + task.Resolve(engine, CreateJsValue(engine, dir->name)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetOrCreateDistributedDir", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::JsGetDisplayOrientation( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) +{ + HILOG_DEBUG("called"); + if (info.argc > ARGS_ONE) { + HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); + return engine.CreateUndefined(); + } + + value_ = static_cast(NAPI_ERR_NO_ERROR); + auto execute = [obj = this, abilityType] () { + if (obj->ability_ == nullptr) { + obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + obj->value_ = obj->ability_->GetDisplayOrientation(); + }; + auto complete = [obj = this] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (obj->value_ == NAPI_ERR_NO_ERROR) { + task.Reject(engine, CreateJsError(engine, NAPI_ERR_ACE_ABILITY, "ability is nullptr")); + } + + task.Resolve(engine, CreateJsValue(engine, obj->value_)); + }; + + auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; + NativeValue *result = nullptr; + AsyncTask::Schedule("JsNapiCommon::JsGetDisplayOrientation", + engine, CreateAsyncTaskWithLastParam(engine, callback, std::move(execute), std::move(complete), &result)); + + return result; +} + +NativeValue* JsNapiCommon::CreateProcessInfo(NativeEngine &engine, const std::shared_ptr &processInfo) +{ + HILOG_DEBUG("called"); + if (processInfo == nullptr) { + HILOG_ERROR("input params error"); + return engine.CreateUndefined(); + } + + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("processName", CreateJsValue(engine, processInfo->processName)); + object->SetProperty("pid", CreateJsValue(engine, processInfo->pid)); + + return objContext; +} + +NativeValue* JsNapiCommon::CreateElementName(NativeEngine &engine, const std::shared_ptr &elementName) +{ + HILOG_DEBUG("called"); + if (elementName == nullptr) { + HILOG_ERROR("input params error"); + return engine.CreateUndefined(); + } + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("deviceId", CreateJsValue(engine, elementName->deviceId)); + object->SetProperty("bundleName", CreateJsValue(engine, elementName->bundleName)); + object->SetProperty("abilityName", CreateJsValue(engine, elementName->abilityName)); + object->SetProperty("uri", CreateJsValue(engine, elementName->uri)); + object->SetProperty("shortName", CreateJsValue(engine, elementName->shortName)); + + return objContext; +} + +NativeValue* JsNapiCommon::CreateHapModuleInfo( + NativeEngine &engine, const std::shared_ptr &hapModInfo) +{ + HILOG_DEBUG("called"); + if (hapModInfo == nullptr) { + HILOG_ERROR("input params error"); + return engine.CreateUndefined(); + } + + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("name", CreateJsValue(engine, hapModInfo->hapModInfo.name)); + object->SetProperty("description", CreateJsValue(engine, hapModInfo->hapModInfo.description)); + object->SetProperty("icon", CreateJsValue(engine, hapModInfo->hapModInfo.iconPath)); + object->SetProperty("label", CreateJsValue(engine, hapModInfo->hapModInfo.label)); + object->SetProperty("backgroundImg", CreateJsValue(engine, hapModInfo->hapModInfo.backgroundImg)); + object->SetProperty("moduleName", CreateJsValue(engine, hapModInfo->hapModInfo.moduleName)); + object->SetProperty("mainAbilityName", CreateJsValue(engine, hapModInfo->hapModInfo.mainAbility)); + object->SetProperty("supportedModes", CreateJsValue(engine, hapModInfo->hapModInfo.supportedModes)); + object->SetProperty("descriptionId", CreateJsValue(engine, hapModInfo->hapModInfo.descriptionId)); + object->SetProperty("labelId", CreateJsValue(engine, hapModInfo->hapModInfo.labelId)); + object->SetProperty("iconId", CreateJsValue(engine, hapModInfo->hapModInfo.iconId)); + object->SetProperty("installationFree", engine.CreateBoolean(hapModInfo->hapModInfo.installationFree)); + object->SetProperty("reqCapabilities", CreateNativeArray(engine, hapModInfo->hapModInfo.reqCapabilities)); + object->SetProperty("deviceTypes", CreateNativeArray(engine, hapModInfo->hapModInfo.deviceTypes)); + object->SetProperty("abilityInfo", CreateAbilityInfos(engine, hapModInfo->hapModInfo.abilityInfos)); + + return objContext; +} + +NativeValue* JsNapiCommon::CreateModuleInfo(NativeEngine &engine, const ModuleInfo &modInfo) +{ + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("moduleName", CreateJsValue(engine, modInfo.moduleName)); + object->SetProperty("moduleSourceDir", CreateJsValue(engine, modInfo.moduleSourceDir)); + + return objContext; +} + +NativeValue* JsNapiCommon::CreateModuleInfos(NativeEngine &engine, const std::vector &moduleInfos) +{ + auto arrayValue = engine.CreateArray(moduleInfos.size()); + auto array = ConvertNativeValueTo(arrayValue); + if (array == nullptr) { + HILOG_ERROR("CreateArray failed"); + return engine.CreateUndefined(); + } + + for (uint32_t i = 0; i < moduleInfos.size(); i++) { + array->SetElement(i, CreateModuleInfo(engine, moduleInfos.at(i))); + } + return arrayValue; +} + +NativeValue* JsNapiCommon::CreateAppInfo(NativeEngine &engine, const ApplicationInfo &appInfo) +{ + HILOG_DEBUG("called"); + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("name", CreateJsValue(engine, appInfo.name)); + object->SetProperty("description", CreateJsValue(engine, appInfo.description)); + object->SetProperty("descriptionId", CreateJsValue(engine, appInfo.descriptionId)); + object->SetProperty("systemApp", CreateJsValue(engine, appInfo.isSystemApp)); + object->SetProperty("enabled", CreateJsValue(engine, appInfo.enabled)); + object->SetProperty("label", CreateJsValue(engine, appInfo.label)); + object->SetProperty("labelId", CreateJsValue(engine, std::to_string(appInfo.labelId))); + object->SetProperty("icon", CreateJsValue(engine, appInfo.iconPath)); + object->SetProperty("iconId", CreateJsValue(engine, std::to_string(appInfo.iconId))); + object->SetProperty("process", CreateJsValue(engine, appInfo.process)); + object->SetProperty("entryDir", CreateJsValue(engine, appInfo.entryDir)); + object->SetProperty("supportedModes", CreateJsValue(engine, appInfo.supportedModes)); + object->SetProperty("moduleSourceDirs", CreateNativeArray(engine, appInfo.moduleSourceDirs)); + object->SetProperty("permissions", CreateNativeArray(engine, appInfo.permissions)); + object->SetProperty("moduleInfos", CreateModuleInfos(engine, appInfo.moduleInfos)); + + return objContext; +} + +NativeValue* JsNapiCommon::CreateAppInfo(NativeEngine &engine, const std::shared_ptr &appInfo) +{ + if (appInfo == nullptr) { + HILOG_ERROR("input param error"); + return engine.CreateUndefined(); + } + + return CreateAppInfo(engine, appInfo->appInfo); +} + +NativeValue* JsNapiCommon::CreateAbilityInfo(NativeEngine &engine, const AbilityInfo &abilityInfo) +{ + HILOG_DEBUG("called"); + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("bundleName", CreateJsValue(engine, abilityInfo.bundleName)); + object->SetProperty("name", CreateJsValue(engine, abilityInfo.name)); + object->SetProperty("label", CreateJsValue(engine, abilityInfo.label)); + object->SetProperty("description", CreateJsValue(engine, abilityInfo.description)); + object->SetProperty("icon", CreateJsValue(engine, abilityInfo.iconPath)); + object->SetProperty("moduleName", CreateJsValue(engine, abilityInfo.moduleName)); + object->SetProperty("process", CreateJsValue(engine, abilityInfo.process)); + object->SetProperty("uri", CreateJsValue(engine, abilityInfo.uri)); + object->SetProperty("readPermission", CreateJsValue(engine, abilityInfo.readPermission)); + object->SetProperty("writePermission", CreateJsValue(engine, abilityInfo.writePermission)); + object->SetProperty("targetAbility", CreateJsValue(engine, abilityInfo.targetAbility)); + object->SetProperty("type", CreateJsValue(engine, static_cast(abilityInfo.type))); + object->SetProperty("orientation", CreateJsValue(engine, static_cast(abilityInfo.orientation))); + object->SetProperty("launchMode", CreateJsValue(engine, static_cast(abilityInfo.launchMode))); + object->SetProperty("labelId", CreateJsValue(engine, abilityInfo.labelId)); + object->SetProperty("descriptionId", CreateJsValue(engine, abilityInfo.descriptionId)); + object->SetProperty("iconId", CreateJsValue(engine, abilityInfo.iconId)); + object->SetProperty("formEntity", CreateJsValue(engine, abilityInfo.formEntity)); + object->SetProperty("minFormHeight", CreateJsValue(engine, abilityInfo.minFormHeight)); + object->SetProperty("defaultFormHeight", CreateJsValue(engine, abilityInfo.defaultFormHeight)); + object->SetProperty("minFormWidth", CreateJsValue(engine, abilityInfo.minFormWidth)); + object->SetProperty("defaultFormWidth", CreateJsValue(engine, abilityInfo.defaultFormWidth)); + object->SetProperty("backgroundModes", CreateJsValue(engine, abilityInfo.backgroundModes)); + object->SetProperty("subType", CreateJsValue(engine, static_cast(abilityInfo.subType))); + object->SetProperty("isVisible", CreateJsValue(engine, abilityInfo.visible)); + object->SetProperty("formEnabled", CreateJsValue(engine, abilityInfo.formEnabled)); + object->SetProperty("permissions", CreateNativeArray(engine, abilityInfo.permissions)); + object->SetProperty("deviceCapabilities", CreateNativeArray(engine, abilityInfo.deviceCapabilities)); + object->SetProperty("deviceTypes", CreateNativeArray(engine, abilityInfo.deviceTypes)); + object->SetProperty("applicationInfo", CreateAppInfo(engine, abilityInfo.applicationInfo)); + return objContext; +} + +NativeValue* JsNapiCommon::CreateAbilityInfo( + NativeEngine &engine, const std::shared_ptr &abilityInfo) +{ + HILOG_DEBUG("called"); + if (abilityInfo == nullptr) { + HILOG_ERROR("called"); + return engine.CreateUndefined(); + } + + return CreateAbilityInfo(engine, abilityInfo->abilityInfo); +} + +NativeValue* JsNapiCommon::CreateAbilityInfos(NativeEngine &engine, const std::vector &abilityInfos) +{ + auto arrayValue = engine.CreateArray(abilityInfos.size()); + auto array = ConvertNativeValueTo(arrayValue); + if (array == nullptr) { + HILOG_ERROR("CreateArray failed"); + return engine.CreateUndefined(); + } + + for (uint32_t i = 0; i < abilityInfos.size(); i++) { + array->SetElement(i, CreateAbilityInfo(engine, abilityInfos.at(i))); + } + return arrayValue; +} + +bool JsNapiCommon::CheckAbilityType(const AbilityType typeWant) +{ + if (ability_ == nullptr) { + HILOG_ERROR("input params int error"); + return false; + } + + const std::shared_ptr info = ability_->GetAbilityInfo(); + if (info == nullptr) { + HILOG_ERROR("get ability info error"); + return false; + } + + switch (typeWant) { + case AbilityType::PAGE: + if (static_cast(info->type) == AbilityType::PAGE || + static_cast(info->type) == AbilityType::DATA) { + return true; + } + return false; + default: + return static_cast(info->type) != AbilityType::PAGE; + } + return false; +} + +NativeValue* JsNapiCommon::CreateAppVersionInfo( + NativeEngine &engine, const std::shared_ptr &appInfo) +{ + HILOG_DEBUG("called"); + if (appInfo == nullptr) { + HILOG_ERROR("input params error"); + return engine.CreateUndefined(); + } + + auto objContext = engine.CreateObject(); + if (objContext == nullptr) { + HILOG_ERROR("CreateObject failed"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objContext); + if (object == nullptr) { + HILOG_ERROR("ConvertNativeValueTo object failed"); + return engine.CreateUndefined(); + } + + object->SetProperty("appName", CreateJsValue(engine, appInfo->appInfo.name)); + object->SetProperty("versionName", CreateJsValue(engine, appInfo->appInfo.versionName)); + object->SetProperty("versionCode", CreateJsValue(engine, static_cast(appInfo->appInfo.versionCode))); + + return objContext; +} + +bool JsNapiCommon::UnwarpVerifyPermissionParams( + NativeEngine &engine, NativeCallbackInfo &info, JsPermissionOptions &options) +{ + bool flagCall = true; + if (info.argc == ARGS_ONE) { + flagCall = false; + } else if (info.argc == ARGS_TWO && info.argv[PARAM1]->TypeOf() != NATIVE_FUNCTION) { + if (!GetPermissionOptions(engine, info.argv[PARAM1], options)) { + HILOG_WARN("input params string error"); + } + flagCall = false; + } else if (info.argc == ARGS_THREE) { + if (!GetPermissionOptions(engine, info.argv[PARAM1], options)) { + HILOG_WARN("input params string error"); + } + } + + return flagCall; +} + +bool JsNapiCommon::GetStringsValue(NativeEngine &engine, NativeValue *object, std::vector &strList) +{ + auto array = ConvertNativeValueTo(object); + if (array == nullptr) { + HILOG_ERROR("input params error"); + return false; + } + + for (uint32_t i = 0; i < array->GetLength(); i++) { + std::string itemStr(""); + if (!ConvertFromJsValue(engine, array->GetElement(i), itemStr)) { + HILOG_ERROR("GetElement from to array [%{public}u] error", i); + return false; + } + strList.push_back(itemStr); + } + + return true; +} + +bool JsNapiCommon::GetPermissionOptions(NativeEngine &engine, NativeValue *object, JsPermissionOptions &options) +{ + auto obj = ConvertNativeValueTo(object); + if (obj == nullptr) { + HILOG_ERROR("input params error"); + return false; + } + + options.uidFlag = ConvertFromJsValue(engine, obj->GetProperty("uid"), options.uid); + options.pidFlag = ConvertFromJsValue(engine, obj->GetProperty("pid"), options.pid); + + return true; +} + +std::string JsNapiCommon::ConvertErrorCode(int32_t errCode) +{ + static std::map errMap = { + { static_cast(NAPI_ERR_ACE_ABILITY), std::string("get ability error") }, + { static_cast(NAPI_ERR_ABILITY_CALL_INVALID), std::string("ability call error") }, + { static_cast(NAPI_ERR_PARAM_INVALID), std::string("input param error") }, + { static_cast(NAPI_ERR_ABILITY_TYPE_INVALID), std::string("ability type error") } + }; + + auto findECode = errMap.find(errCode); + if (findECode == errMap.end()) { + HILOG_ERROR("convert error code failed"); + return std::string("execution failed"); + } + + return findECode->second; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/js/napi/particleAbility/BUILD.gn b/frameworks/js/napi/particleAbility/BUILD.gn index d56f13b5423..3abe4dfe183 100644 --- a/frameworks/js/napi/particleAbility/BUILD.gn +++ b/frameworks/js/napi/particleAbility/BUILD.gn @@ -41,6 +41,7 @@ ohos_shared_library("particleability") { deps = [ "${ability_runtime_innerkits_path}/dataobs_manager:dataobs_manager", "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_native_path}/appkit:appkit_native", "//third_party/jsoncpp:jsoncpp", "//third_party/libuv:uv", ] @@ -52,6 +53,7 @@ ohos_shared_library("particleability") { "ability_runtime:abilitykit_native", "ability_runtime:data_ability_helper", "ability_runtime:napi_base_context", + "ability_runtime:runtime", "bundle_framework:appexecfwk_base", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", diff --git a/frameworks/native/ability/native/BUILD.gn b/frameworks/native/ability/native/BUILD.gn index 99564d1e41c..579acecf863 100644 --- a/frameworks/native/ability/native/BUILD.gn +++ b/frameworks/native/ability/native/BUILD.gn @@ -131,6 +131,7 @@ ohos_shared_library("abilitykit_native") { sources = [ "${ability_runtime_innerkits_path}/app_manager/src/appmgr/process_info.cpp", + "${ability_runtime_napi_path}/featureAbility/napi_context.cpp", "${ability_runtime_napi_path}/inner/napi_common/napi_common_ability.cpp", "${ability_runtime_napi_path}/inner/napi_common/napi_common_configuration.cpp", "${ability_runtime_napi_path}/inner/napi_common/napi_common_start_options.cpp", -- Gitee From 3541b1e5a4b197fff457ada7919aeb0734817c0e Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Tue, 13 Sep 2022 10:33:57 +0800 Subject: [PATCH 2/6] fix review Code Signed-off-by: xinxin13 --- .../js/napi/featureAbility/napi_context.cpp | 171 +++++++++------- .../napi_common/js_napi_common_ability.h | 3 +- .../inner/napi_common/napi_common_ability.cpp | 182 +++++++++--------- 3 files changed, 184 insertions(+), 172 deletions(-) diff --git a/frameworks/js/napi/featureAbility/napi_context.cpp b/frameworks/js/napi/featureAbility/napi_context.cpp index e33abc889c0..74edc6bb1f2 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -2961,6 +2961,7 @@ NativeValue* NapiJsContext::JsRequestPermissionsFromUser(NativeEngine *engine, N return object->OnRequestPermissionsFromUser(*engine, *info); } + NativeValue* NapiJsContext::JsGetBundleName(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -2976,6 +2977,7 @@ NativeValue* NapiJsContext::JsGetBundleName(NativeEngine *engine, NativeCallback return object->OnGetBundleName(*engine, *info); } + NativeValue* NapiJsContext::JsVerifyPermission(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -2991,6 +2993,7 @@ NativeValue* NapiJsContext::JsVerifyPermission(NativeEngine *engine, NativeCallb return object->OnVerifyPermission(*engine, *info); } + NativeValue* NapiJsContext::JsGetApplicationInfo(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3006,6 +3009,7 @@ NativeValue* NapiJsContext::JsGetApplicationInfo(NativeEngine *engine, NativeCal return object->OnGetApplicationInfo(*engine, *info); } + NativeValue* NapiJsContext::JsGetProcessInfo(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3021,6 +3025,7 @@ NativeValue* NapiJsContext::JsGetProcessInfo(NativeEngine *engine, NativeCallbac return object->OnGetProcessInfo(*engine, *info); } + NativeValue* NapiJsContext::JsGetElementName(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3036,6 +3041,7 @@ NativeValue* NapiJsContext::JsGetElementName(NativeEngine *engine, NativeCallbac return object->OnGetElementName(*engine, *info); } + NativeValue* NapiJsContext::JsGetProcessName(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3051,6 +3057,7 @@ NativeValue* NapiJsContext::JsGetProcessName(NativeEngine *engine, NativeCallbac return object->OnGetProcessName(*engine, *info); } + NativeValue* NapiJsContext::JsGetCallingBundle(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3066,6 +3073,7 @@ NativeValue* NapiJsContext::JsGetCallingBundle(NativeEngine *engine, NativeCallb return object->OnGetCallingBundle(*engine, *info); } + NativeValue* NapiJsContext::JsGetOrCreateLocalDir(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3081,6 +3089,7 @@ NativeValue* NapiJsContext::JsGetOrCreateLocalDir(NativeEngine *engine, NativeCa return object->OnGetOrCreateLocalDir(*engine, *info); } + NativeValue* NapiJsContext::JsGetFilesDir(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3096,6 +3105,7 @@ NativeValue* NapiJsContext::JsGetFilesDir(NativeEngine *engine, NativeCallbackIn return object->JsNapiCommon::JsGetFilesDir(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsIsUpdatingConfigurations(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3111,6 +3121,7 @@ NativeValue* NapiJsContext::JsIsUpdatingConfigurations(NativeEngine *engine, Nat return object->JsNapiCommon::JsIsUpdatingConfigurations(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsPrintDrawnCompleted(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3126,6 +3137,7 @@ NativeValue* NapiJsContext::JsPrintDrawnCompleted(NativeEngine *engine, NativeCa return object->JsNapiCommon::JsPrintDrawnCompleted(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsGetCacheDir(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3141,6 +3153,7 @@ NativeValue* NapiJsContext::JsGetCacheDir(NativeEngine *engine, NativeCallbackIn return object->JsNapiCommon::JsGetCacheDir(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsGetCtxAppType(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3156,6 +3169,7 @@ NativeValue* NapiJsContext::JsGetCtxAppType(NativeEngine *engine, NativeCallback return object->JsNapiCommon::JsGetCtxAppType(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsGetCtxHapModuleInfo(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3171,6 +3185,7 @@ NativeValue* NapiJsContext::JsGetCtxHapModuleInfo(NativeEngine *engine, NativeCa return object->JsNapiCommon::JsGetCtxHapModuleInfo(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsGetAppVersionInfo(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3186,6 +3201,7 @@ NativeValue* NapiJsContext::JsGetAppVersionInfo(NativeEngine *engine, NativeCall return object->JsNapiCommon::JsGetAppVersionInfo(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsGetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3199,8 +3215,9 @@ NativeValue* NapiJsContext::JsGetApplicationContext(NativeEngine *engine, Native return engine->CreateUndefined(); } - return object->JsNapiCommon::JsGetApplicationContext(*engine, *info, AbilityType::PAGE); + return object->JsNapiCommon::JsGetContext(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsGetCtxAbilityInfo(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3216,6 +3233,7 @@ NativeValue* NapiJsContext::JsGetCtxAbilityInfo(NativeEngine *engine, NativeCall return object->JsNapiCommon::JsGetCtxAbilityInfo(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsSetShowOnLockScreen(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3231,6 +3249,7 @@ NativeValue* NapiJsContext::JsSetShowOnLockScreen(NativeEngine *engine, NativeCa return object->OnSetShowOnLockScreen(*engine, *info); } + NativeValue* NapiJsContext::JsGetOrCreateDistributedDir(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3246,6 +3265,7 @@ NativeValue* NapiJsContext::JsGetOrCreateDistributedDir(NativeEngine *engine, Na return object->JsNapiCommon::JsGetOrCreateDistributedDir(*engine, *info, AbilityType::PAGE); } + NativeValue* NapiJsContext::JsSetWakeUpScreen(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3261,6 +3281,7 @@ NativeValue* NapiJsContext::JsSetWakeUpScreen(NativeEngine *engine, NativeCallba return object->OnSetWakeUpScreen(*engine, *info); } + NativeValue* NapiJsContext::JsSetDisplayOrientation(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3276,6 +3297,7 @@ NativeValue* NapiJsContext::JsSetDisplayOrientation(NativeEngine *engine, Native return object->OnSetDisplayOrientation(*engine, *info); } + NativeValue* NapiJsContext::JsGetDisplayOrientation(NativeEngine *engine, NativeCallbackInfo *info) { if (engine == nullptr || info == nullptr) { @@ -3295,7 +3317,6 @@ NativeValue* NapiJsContext::JsGetDisplayOrientation(NativeEngine *engine, Native bool NapiJsContext::DataInit(NativeEngine &engine) { HILOG_DEBUG("called"); - value_ = 0; napi_value global = 0; napi_value abilityObj = 0; auto env = reinterpret_cast(&engine); @@ -3317,39 +3338,39 @@ NativeValue* NapiJsContext::OnRequestPermissionsFromUser(NativeEngine &engine, N return engine.CreateUndefined(); } - value_ = 0; + auto errorVal = std::make_shared(0); std::vector permissionList; if(!GetStringsValue(engine, info.argv[PARAM0], permissionList)) { HILOG_ERROR("input params string error"); return engine.CreateUndefined(); } - int32_t value = 0; - if(!ConvertFromJsValue(engine, info.argv[PARAM1], value)) { + int32_t code = 0; + if(!ConvertFromJsValue(engine, info.argv[PARAM1], code)) { HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } CallbackInfo callbackInfo; - auto execute = [obj = this, permissionList, value, cbInfo = callbackInfo] () { + auto execute = [obj = this, permissionList, code, cbInfo = callbackInfo, value = errorVal] () { if (permissionList.empty()) { - obj->value_ = static_cast(NAPI_ERR_PARAM_INVALID); + *value = static_cast(NAPI_ERR_PARAM_INVALID); return; } CallAbilityPermissionParam permissionParam; - permissionParam.requestCode = value; + permissionParam.requestCode = code; permissionParam.permission_list = permissionList; auto processInstance = AbilityProcess::GetInstance(); if (processInstance != nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); return; } processInstance->RequestPermissionsFromUser(obj->ability_, permissionParam, cbInfo); }; - auto complete = [obj = this] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != 0) { - task.Reject(engine, CreateJsError(engine, obj->value_, obj->ConvertErrorCode(obj->value_))); + auto complete = [obj = this, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (*value != 0) { + task.Reject(engine, CreateJsError(engine, *value, obj->ConvertErrorCode(*value))); return; } - task.Resolve(engine, CreateJsValue(engine, obj->value_)); + task.Resolve(engine, CreateJsValue(engine, *value)); }; auto callback = info.argc == ARGS_THREE ? info.argv[PARAM2] : nullptr; @@ -3367,25 +3388,25 @@ NativeValue* NapiJsContext::OnGetBundleName(NativeEngine &engine, NativeCallback return engine.CreateNull(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr bundleName = std::make_shared(); - auto execute = [obj = this, name = bundleName] () { + auto execute = [obj = this, name = bundleName, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (name == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, name is nullptr"); return; } name->name = obj->ability_->GetBundleName(); }; - auto complete = [obj = this, name = bundleName] + auto complete = [obj = this, name = bundleName, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { - auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { + auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3408,7 +3429,7 @@ NativeValue* NapiJsContext::OnVerifyPermission(NativeEngine &engine, NativeCallb return engine.CreateNull(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::string permission(""); if (!ConvertFromJsValue(engine, info.argv[PARAM0], permission)) { HILOG_ERROR("input params string error"); @@ -3416,24 +3437,24 @@ NativeValue* NapiJsContext::OnVerifyPermission(NativeEngine &engine, NativeCallb } JsPermissionOptions options; bool flagCall = UnwarpVerifyPermissionParams(engine, info, options); - auto execute = [obj = this, permission, options] () { + auto execute = [obj = this, permission, options, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (options.uidFlag) { - obj->value_ = obj->ability_->VerifyPermission(permission, options.pid, options.uid); + *value = obj->ability_->VerifyPermission(permission, options.pid, options.uid); } else { - obj->value_ = obj->ability_->VerifySelfPermission(permission); + *value = obj->ability_->VerifySelfPermission(permission); } }; auto complete = [obj = this] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ == static_cast(NAPI_ERR_ACE_ABILITY)) { - task.Reject(engine, CreateJsError( engine, obj->value_,obj->ConvertErrorCode(obj->value_))); + if (*value == static_cast(NAPI_ERR_ACE_ABILITY)) { + task.Reject(engine, CreateJsError( engine, *value,obj->ConvertErrorCode(*value))); return; } - task.Resolve(engine, CreateJsValue(engine, obj->value_)); + task.Resolve(engine, CreateJsValue(engine, *value)); }; auto callback = flagCall ? @@ -3454,11 +3475,11 @@ NativeValue* NapiJsContext::OnGetApplicationInfo(NativeEngine &engine, NativeCal return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr infoData = std::make_shared(); - auto execute = [obj = this, info = infoData] () { + auto execute = [obj = this, info = infoData, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } @@ -3467,13 +3488,13 @@ NativeValue* NapiJsContext::OnGetApplicationInfo(NativeEngine &engine, NativeCal info->appInfo = *getInfo; } else { HILOG_ERROR("GetApplicationInfo return nullptr"); - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); } }; - auto complete = [obj = this, info = infoData] + auto complete = [obj = this, info = infoData, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { - auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3496,11 +3517,11 @@ NativeValue* NapiJsContext::OnGetProcessInfo(NativeEngine &engine, NativeCallbac return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr processInfo = std::make_shared(); - auto execute = [obj = this, data = processInfo] () { + auto execute = [obj = this, data = processInfo, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } @@ -3510,13 +3531,13 @@ NativeValue* NapiJsContext::OnGetProcessInfo(NativeEngine &engine, NativeCallbac data->pid = getInfo->GetPid(); } else { HILOG_ERROR("GetProcessInfo return nullptr"); - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); } }; - auto complete = [obj = this, info = processInfo] + auto complete = [obj = this, info = processInfo, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { - auto ecode = info == nullptr ? (NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? (NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3539,11 +3560,11 @@ NativeValue* NapiJsContext::OnGetElementName(NativeEngine &engine, NativeCallbac return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr elementName = std::make_shared(); - auto execute = [obj = this, data = elementName] () { + auto execute = [obj = this, data = elementName, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } @@ -3556,13 +3577,13 @@ NativeValue* NapiJsContext::OnGetElementName(NativeEngine &engine, NativeCallbac data->shortName = ""; } else { HILOG_ERROR("GetElementName return nullptr"); - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); } }; - auto complete = [obj = this, ename = elementName] + auto complete = [obj = this, ename = elementName, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || ename == nullptr) { - auto ecode = ename == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || ename == nullptr) { + auto ecode = ename == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3585,25 +3606,25 @@ NativeValue* NapiJsContext::OnGetProcessName(NativeEngine &engine, NativeCallbac return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr processName = std::make_shared(); - auto execute = [obj = this, name = processName] () { + auto execute = [obj = this, name = processName, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (name == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, name is nullptr"); return; } name->name = obj->ability_->GetProcessName(); }; - auto complete = [obj = this, name = processName] + auto complete = [obj = this, name = processName, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { - auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { + auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3617,6 +3638,7 @@ NativeValue* NapiJsContext::OnGetProcessName(NativeEngine &engine, NativeCallbac return result; } + NativeValue* NapiJsContext::OnGetCallingBundle(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_DEBUG("called"); @@ -3625,25 +3647,25 @@ NativeValue* NapiJsContext::OnGetCallingBundle(NativeEngine &engine, NativeCallb return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr callingBundleName = std::make_shared(); - auto execute = [obj = this, name = callingBundleName] () { + auto execute = [obj = this, name = callingBundleName, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (name == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, name is nullptr"); return; } name->name = obj->ability_->GetCallingBundle(); }; - auto complete = [obj = this, name = callingBundleName] + auto complete = [obj = this, name = callingBundleName, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { - auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || name == nullptr) { + auto ecode = name == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3666,17 +3688,17 @@ NativeValue* NapiJsContext::OnGetOrCreateLocalDir(NativeEngine &engine, NativeCa return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr createDir = std::make_shared(); - auto execute = [obj = this, dir = createDir] () { + auto execute = [obj = this, dir = createDir, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } auto context = obj->ability_->GetAbilityContext(); if (context == nullptr || dir == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, the abilitycontext is nullptr"); return; } @@ -3687,10 +3709,10 @@ NativeValue* NapiJsContext::OnGetOrCreateLocalDir(NativeEngine &engine, NativeCa OHOS::ChangeModeDirectory(dir->name, MODE); } }; - auto complete = [obj = this, dir = createDir] + auto complete = [obj = this, dir = createDir, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { - auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -3712,13 +3734,13 @@ NativeValue* NapiJsContext::OnSetShowOnLockScreen(NativeEngine &engine, NativeCa return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); bool isShow = false; if (!ConvertFromJsValue(engine, info.argv[PARAM0], isShow)) { HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } - auto complete = [obj = this, isShow] + auto complete = [obj = this, isShow, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (obj->ability_ == nullptr) { task.Reject(engine, @@ -3750,7 +3772,7 @@ NativeValue* NapiJsContext::OnSetWakeUpScreen(NativeEngine &engine, NativeCallba HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } - auto complete = [obj = this, wakeUp] + auto complete = [obj = this, wakeUp, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (obj->ability_ == nullptr) { task.Reject(engine, @@ -3768,6 +3790,7 @@ NativeValue* NapiJsContext::OnSetWakeUpScreen(NativeEngine &engine, NativeCallba return result; } + NativeValue* NapiJsContext::OnSetDisplayOrientation(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_DEBUG("called"); @@ -3781,7 +3804,7 @@ NativeValue* NapiJsContext::OnSetDisplayOrientation(NativeEngine &engine, Native HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } - auto complete = [obj = this, orientation] + auto complete = [obj = this, orientation, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (obj->ability_ == nullptr) { task.Reject(engine, diff --git a/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h b/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h index 572570c69e0..2534de09713 100644 --- a/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h +++ b/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -117,7 +117,6 @@ public: std::string ConvertErrorCode(int32_t errCode); Ability *ability_; - int32_t value_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp index d09695eef69..ed5aceeab4d 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp @@ -18,14 +18,15 @@ #include #include -#include "js_runtime_utils.h" -#include "js_napi_common_ability.h" #include "hilog_wrapper.h" #include "napi_common_util.h" +#include "napi_context.h" #include "napi_base_context.h" #include "napi_remote_object.h" +#include "js_napi_common_ability.h" +#include "js_runtime_utils.h" #include "securec.h" -#include "napi_context.h" + using namespace OHOS::AbilityRuntime; namespace OHOS { @@ -4807,7 +4808,7 @@ napi_value NAPI_TerminateAbilityCommon(napi_env env, napi_callback_info info) return ret; } -JsNapiCommon::JsNapiCommon() : ability_(nullptr), value_(0) +JsNapiCommon::JsNapiCommon() : ability_(nullptr) {} NativeValue* JsNapiCommon::JsGetContext(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) @@ -4828,30 +4829,30 @@ NativeValue* JsNapiCommon::JsGetFilesDir(NativeEngine &engine, NativeCallbackInf return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr filesDir = std::make_shared(); - auto execute = [obj = this, dir = filesDir, abilityType] () { + auto execute = [obj = this, dir = filesDir, abilityType, value = errorVal] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } auto context = obj->ability_->GetAbilityContext(); if (context == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, the abilitycontext is nullptr"); return; } dir->name = context->GetFilesDir(); }; - auto complete = [obj = this, dir = filesDir] + auto complete = [obj = this, dir = filesDir, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { - auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -4874,29 +4875,29 @@ NativeValue* JsNapiCommon::JsIsUpdatingConfigurations( return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr config = std::make_shared(); - auto execute = [obj = this, data = config, abilityType] () { + auto execute = [obj = this, data = config, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } if (data == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, param is nullptr"); return; } data->status = obj->ability_->IsUpdatingConfigurations(); }; - auto complete = [obj = this, info = config] + auto complete = [obj = this, info = config, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { - auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -4919,29 +4920,29 @@ NativeValue* JsNapiCommon::JsPrintDrawnCompleted( HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr drawComplete = std::make_shared(); - auto execute = [obj = this, data = drawComplete, abilityType] () { + auto execute = [obj = this, data = drawComplete, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } if (data == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); - HILOG_ERROR("task execute error, name is nullptr"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + HILOG_ERROR("task execute error, data is nullptr"); return; } data->status = obj->ability_->PrintDrawnCompleted(); }; - auto complete = [obj = this, draw = drawComplete] + auto complete = [obj = this, draw = drawComplete, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || draw == nullptr) { - auto ecode = draw == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || draw == nullptr) { + auto ecode = draw == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -4963,30 +4964,30 @@ NativeValue* JsNapiCommon::JsGetCacheDir(NativeEngine &engine, NativeCallbackInf HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr cacheDir = std::make_shared(); - auto execute = [obj = this, dir = cacheDir, abilityType] () { + auto execute = [obj = this, dir = cacheDir, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } auto context = obj->ability_->GetAbilityContext(); if (context == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); HILOG_ERROR("task execute error, the abilitycontext is nullptr"); return; } dir->name = context->GetCacheDir(); }; - auto complete = [obj = this, dir = cacheDir] + auto complete = [obj = this, dir = cacheDir, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { - auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -5010,28 +5011,28 @@ NativeValue* JsNapiCommon::JsGetCtxAppType( return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr type = std::make_shared(); - auto execute = [obj = this, apptype = type, abilityType] () { + auto execute = [obj = this, apptype = type, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } if (apptype == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); return; } apptype->name = obj->ability_->GetAppType(); }; - auto complete = [obj = this, apptype = type] + auto complete = [obj = this, apptype = type, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || apptype == nullptr) { - auto ecode = apptype == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || apptype == nullptr) { + auto ecode = apptype == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -5054,16 +5055,16 @@ NativeValue* JsNapiCommon::JsGetCtxHapModuleInfo( HILOG_ERROR("input params count error, argc=%{public}zu", info.argc); return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr infoData = std::make_shared(); - auto execute = [obj = this, hapMod = infoData, abilityType] () { + auto execute = [obj = this, hapMod = infoData, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } auto getInfo = obj->ability_->GetHapModuleInfo(); @@ -5071,13 +5072,13 @@ NativeValue* JsNapiCommon::JsGetCtxHapModuleInfo( hapMod->hapModInfo = *getInfo; } else { HILOG_ERROR("GetHapModuleInfo return nullptr"); - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); } }; - auto complete = [obj = this, info = infoData] + auto complete = [obj = this, info = infoData, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { - auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -5101,16 +5102,16 @@ NativeValue* JsNapiCommon::JsGetAppVersionInfo( return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr infoData = std::make_shared(); - auto execute = [obj = this, appInfo = infoData, abilityType] () { + auto execute = [obj = this, appInfo = infoData, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } auto getInfo = obj->ability_->GetApplicationInfo(); @@ -5118,13 +5119,13 @@ NativeValue* JsNapiCommon::JsGetAppVersionInfo( appInfo->appInfo = *getInfo; } else { HILOG_ERROR("GetApplicationInfo return nullptr"); - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); } }; - auto complete = [obj = this, info = infoData] + auto complete = [obj = this, info = infoData, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { - auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -5139,17 +5140,6 @@ NativeValue* JsNapiCommon::JsGetAppVersionInfo( return result; } -NativeValue* JsNapiCommon::JsGetApplicationContext( - NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) -{ - if (!CheckAbilityType(abilityType)) { - HILOG_ERROR("ability type error"); - return engine.CreateUndefined(); - } - - return CreateNapiJSContext(engine); -} - NativeValue* JsNapiCommon::JsGetCtxAbilityInfo( NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType) { @@ -5159,16 +5149,16 @@ NativeValue* JsNapiCommon::JsGetCtxAbilityInfo( return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr infoData = std::make_shared(); - auto execute = [obj = this, abilityInfo = infoData, abilityType] () { + auto execute = [obj = this, abilityInfo = infoData, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } auto getInfo = obj->ability_->GetAbilityInfo(); @@ -5176,13 +5166,13 @@ NativeValue* JsNapiCommon::JsGetCtxAbilityInfo( abilityInfo->abilityInfo = *getInfo; } else { HILOG_ERROR("GetAbilityInfo return nullptr"); - obj->value_ = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); } }; - auto complete = [obj = this, info = infoData] + auto complete = [obj = this, info = infoData, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { - auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || info == nullptr) { + auto ecode = info == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -5206,31 +5196,31 @@ NativeValue* JsNapiCommon::JsGetOrCreateDistributedDir( return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); std::shared_ptr orCreateDistributedDir = std::make_shared(); - auto execute = [obj = this, dir = orCreateDistributedDir, abilityType] () { + auto execute = [obj = this, dir = orCreateDistributedDir, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } auto context = obj->ability_->GetAbilityContext(); if (context == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the abilitycontext is nullptr"); return; } dir->name = context->GetDistributedFilesDir(); }; - auto complete = [obj = this, dir = orCreateDistributedDir] + auto complete = [obj = this, dir = orCreateDistributedDir, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { - auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : obj->value_; + if (*value != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { + auto ecode = dir == nullptr ? static_cast(NAPI_ERR_ABILITY_CALL_INVALID) : *value; task.Reject(engine, CreateJsError(engine, ecode, obj->ConvertErrorCode(ecode))); return; } @@ -5254,26 +5244,26 @@ NativeValue* JsNapiCommon::JsGetDisplayOrientation( return engine.CreateUndefined(); } - value_ = static_cast(NAPI_ERR_NO_ERROR); - auto execute = [obj = this, abilityType] () { + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + auto execute = [obj = this, value = errorVal, abilityType] () { if (obj->ability_ == nullptr) { - obj->value_ = static_cast(NAPI_ERR_ACE_ABILITY); + *value = static_cast(NAPI_ERR_ACE_ABILITY); HILOG_ERROR("task execute error, the ability is nullptr"); return; } if (!obj->CheckAbilityType(abilityType)) { - obj->value_ = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); return; } - obj->value_ = obj->ability_->GetDisplayOrientation(); + *value = obj->ability_->GetDisplayOrientation(); }; - auto complete = [obj = this] + auto complete = [obj = this, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (obj->value_ == NAPI_ERR_NO_ERROR) { + if (*value == NAPI_ERR_NO_ERROR) { task.Reject(engine, CreateJsError(engine, NAPI_ERR_ACE_ABILITY, "ability is nullptr")); } - task.Resolve(engine, CreateJsValue(engine, obj->value_)); + task.Resolve(engine, CreateJsValue(engine, *value)); }; auto callback = info.argc == ARGS_ZERO ? nullptr : info.argv[PARAM0]; -- Gitee From cce951e0f47a5cb31de8165ef140f6b7bb044c20 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Tue, 13 Sep 2022 10:45:33 +0800 Subject: [PATCH 3/6] fix review Code Signed-off-by: xinxin13 --- frameworks/js/napi/featureAbility/napi_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/featureAbility/napi_context.cpp b/frameworks/js/napi/featureAbility/napi_context.cpp index 74edc6bb1f2..09b33b5ea7e 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -24,8 +24,8 @@ #include "feature_ability_common.h" #include "file_ex.h" #include "hilog_wrapper.h" -#include "securec.h" #include "js_napi_common_ability.h" +#include "securec.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; -- Gitee From f1269954c3a469aedc2cd5c8ce642a0b36c6d73b Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Tue, 13 Sep 2022 14:21:53 +0800 Subject: [PATCH 4/6] fix build error Signed-off-by: xinxin13 --- frameworks/js/napi/featureAbility/napi_context.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/featureAbility/napi_context.cpp b/frameworks/js/napi/featureAbility/napi_context.cpp index 09b33b5ea7e..f0fe0943609 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -3449,7 +3449,7 @@ NativeValue* NapiJsContext::OnVerifyPermission(NativeEngine &engine, NativeCallb *value = obj->ability_->VerifySelfPermission(permission); } }; - auto complete = [obj = this] (NativeEngine &engine, AsyncTask &task, int32_t status) { + auto complete = [obj = this, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (*value == static_cast(NAPI_ERR_ACE_ABILITY)) { task.Reject(engine, CreateJsError( engine, *value,obj->ConvertErrorCode(*value))); return; @@ -3772,7 +3772,7 @@ NativeValue* NapiJsContext::OnSetWakeUpScreen(NativeEngine &engine, NativeCallba HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } - auto complete = [obj = this, wakeUp, value = errorVal] + auto complete = [obj = this, wakeUp] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (obj->ability_ == nullptr) { task.Reject(engine, @@ -3804,7 +3804,7 @@ NativeValue* NapiJsContext::OnSetDisplayOrientation(NativeEngine &engine, Native HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } - auto complete = [obj = this, orientation, value = errorVal] + auto complete = [obj = this, orientation] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (obj->ability_ == nullptr) { task.Reject(engine, -- Gitee From a7992d81c51f3654506754a4041e9785ded3edd4 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Sat, 17 Sep 2022 14:17:09 +0800 Subject: [PATCH 5/6] remove empty lines Signed-off-by: xinxin13 --- .../js/napi/featureAbility/napi_context.cpp | 6 ------ .../inner/napi_common/napi_common_ability.cpp | 18 ++---------------- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/frameworks/js/napi/featureAbility/napi_context.cpp b/frameworks/js/napi/featureAbility/napi_context.cpp index f0fe0943609..3a62d86c853 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -2864,7 +2864,6 @@ static bool BindNapiJSContextFunction(NativeEngine &engine, NativeObject* object HILOG_ERROR("input params error"); return false; } - const char* moduleName = "context"; BindNativeFunction( engine, *object, "requestPermissionsFromUser", moduleName, NapiJsContext::JsRequestPermissionsFromUser); @@ -2905,27 +2904,22 @@ static NativeValue* ConstructNapiJSContext(NativeEngine &engine) HILOG_ERROR("CreateObject failed"); return nullptr; } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); return nullptr; } - auto jsCalss = std::make_unique(); if (jsCalss == nullptr) { HILOG_ERROR("new NapiJsContext failed"); return nullptr; } - if (!jsCalss->DataInit(engine)) { HILOG_ERROR("NapiJsContext init failed"); return nullptr; } - object->SetNativePointer(jsCalss.release(), NapiJsContext::Finalizer, nullptr); object->SetProperty("stageMode", engine.CreateBoolean(false)); - if (!BindNapiJSContextFunction(engine, object)) { HILOG_ERROR("bind func failed"); return nullptr; diff --git a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp index ed5aceeab4d..f92fc76cb60 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp @@ -5216,7 +5216,6 @@ NativeValue* JsNapiCommon::JsGetOrCreateDistributedDir( } dir->name = context->GetDistributedFilesDir(); }; - auto complete = [obj = this, dir = orCreateDistributedDir, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (*value != static_cast(NAPI_ERR_NO_ERROR) || dir == nullptr) { @@ -5281,13 +5280,11 @@ NativeValue* JsNapiCommon::CreateProcessInfo(NativeEngine &engine, const std::sh HILOG_ERROR("input params error"); return engine.CreateUndefined(); } - auto objContext = engine.CreateObject(); if (objContext == nullptr) { HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5312,7 +5309,6 @@ NativeValue* JsNapiCommon::CreateElementName(NativeEngine &engine, const std::sh HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5336,13 +5332,11 @@ NativeValue* JsNapiCommon::CreateHapModuleInfo( HILOG_ERROR("input params error"); return engine.CreateUndefined(); } - auto objContext = engine.CreateObject(); if (objContext == nullptr) { HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5375,7 +5369,6 @@ NativeValue* JsNapiCommon::CreateModuleInfo(NativeEngine &engine, const ModuleIn HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5396,10 +5389,10 @@ NativeValue* JsNapiCommon::CreateModuleInfos(NativeEngine &engine, const std::ve HILOG_ERROR("CreateArray failed"); return engine.CreateUndefined(); } - for (uint32_t i = 0; i < moduleInfos.size(); i++) { array->SetElement(i, CreateModuleInfo(engine, moduleInfos.at(i))); } + return arrayValue; } @@ -5411,7 +5404,6 @@ NativeValue* JsNapiCommon::CreateAppInfo(NativeEngine &engine, const Application HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5455,7 +5447,6 @@ NativeValue* JsNapiCommon::CreateAbilityInfo(NativeEngine &engine, const Ability HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5515,10 +5506,10 @@ NativeValue* JsNapiCommon::CreateAbilityInfos(NativeEngine &engine, const std::v HILOG_ERROR("CreateArray failed"); return engine.CreateUndefined(); } - for (uint32_t i = 0; i < abilityInfos.size(); i++) { array->SetElement(i, CreateAbilityInfo(engine, abilityInfos.at(i))); } + return arrayValue; } @@ -5528,7 +5519,6 @@ bool JsNapiCommon::CheckAbilityType(const AbilityType typeWant) HILOG_ERROR("input params int error"); return false; } - const std::shared_ptr info = ability_->GetAbilityInfo(); if (info == nullptr) { HILOG_ERROR("get ability info error"); @@ -5556,13 +5546,11 @@ NativeValue* JsNapiCommon::CreateAppVersionInfo( HILOG_ERROR("input params error"); return engine.CreateUndefined(); } - auto objContext = engine.CreateObject(); if (objContext == nullptr) { HILOG_ERROR("CreateObject failed"); return engine.CreateUndefined(); } - auto object = ConvertNativeValueTo(objContext); if (object == nullptr) { HILOG_ERROR("ConvertNativeValueTo object failed"); @@ -5603,7 +5591,6 @@ bool JsNapiCommon::GetStringsValue(NativeEngine &engine, NativeValue *object, st HILOG_ERROR("input params error"); return false; } - for (uint32_t i = 0; i < array->GetLength(); i++) { std::string itemStr(""); if (!ConvertFromJsValue(engine, array->GetElement(i), itemStr)) { @@ -5638,7 +5625,6 @@ std::string JsNapiCommon::ConvertErrorCode(int32_t errCode) { static_cast(NAPI_ERR_PARAM_INVALID), std::string("input param error") }, { static_cast(NAPI_ERR_ABILITY_TYPE_INVALID), std::string("ability type error") } }; - auto findECode = errMap.find(errCode); if (findECode == errMap.end()) { HILOG_ERROR("convert error code failed"); -- Gitee From fdedf3d8a0c1d56b176b4d102901f0ff94eb01b6 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Wed, 28 Sep 2022 13:57:08 +0800 Subject: [PATCH 6/6] Codex Signed-off-by: xinxin13 --- frameworks/js/napi/featureAbility/napi_context.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/featureAbility/napi_context.cpp b/frameworks/js/napi/featureAbility/napi_context.cpp index 3a62d86c853..3c1becf9b57 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -3334,12 +3334,12 @@ NativeValue* NapiJsContext::OnRequestPermissionsFromUser(NativeEngine &engine, N auto errorVal = std::make_shared(0); std::vector permissionList; - if(!GetStringsValue(engine, info.argv[PARAM0], permissionList)) { + if (!GetStringsValue(engine, info.argv[PARAM0], permissionList)) { HILOG_ERROR("input params string error"); return engine.CreateUndefined(); } int32_t code = 0; - if(!ConvertFromJsValue(engine, info.argv[PARAM1], code)) { + if (!ConvertFromJsValue(engine, info.argv[PARAM1], code)) { HILOG_ERROR("input params int error"); return engine.CreateUndefined(); } @@ -3445,7 +3445,7 @@ NativeValue* NapiJsContext::OnVerifyPermission(NativeEngine &engine, NativeCallb }; auto complete = [obj = this, value = errorVal] (NativeEngine &engine, AsyncTask &task, int32_t status) { if (*value == static_cast(NAPI_ERR_ACE_ABILITY)) { - task.Reject(engine, CreateJsError( engine, *value,obj->ConvertErrorCode(*value))); + task.Reject(engine, CreateJsError( engine, *value, obj->ConvertErrorCode(*value))); return; } task.Resolve(engine, CreateJsValue(engine, *value)); -- Gitee