From 102dacccad5da2c6cd58ddb94d8f6096ddf8ad71 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Sat, 10 Sep 2022 18:47:36 +0800 Subject: [PATCH 1/3] napi context retrofit commit Signed-off-by: xinxin13 --- frameworks/js/napi/featureAbility/BUILD.gn | 1 + .../js/napi/featureAbility/napi_context.cpp | 460 ++++++++++++++++++ .../js/napi/featureAbility/napi_context.h | 10 + frameworks/js/napi/inner/napi_common/BUILD.gn | 6 +- .../napi_common/js_napi_common_ability.h | 94 ++++ .../inner/napi_common/napi_common_ability.cpp | 46 ++ frameworks/js/napi/particleAbility/BUILD.gn | 2 + frameworks/native/ability/native/BUILD.gn | 1 + 8 files changed, 619 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..f612c40e95b 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,463 @@ 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* JsSetShowOnLockScreen(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* 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, "setShowOnLockScreen", moduleName, NapiJsContext::JsSetShowOnLockScreen); + 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::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::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::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..cf5a9cf8e52 --- /dev/null +++ b/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h @@ -0,0 +1,94 @@ +/* + * 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* JsGetDisplayOrientation( + NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + 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..26015cb6952 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,47 @@ 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::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; +} } // 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 645ecfd74b79302f5f8ac5c827d03d215b989a73 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Sun, 11 Sep 2022 09:37:21 +0800 Subject: [PATCH 2/3] fix build error --- .../napi_common/js_napi_common_ability.h | 3 + .../inner/napi_common/napi_common_ability.cpp | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+) 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 cf5a9cf8e52..b7b98e82b3e 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 @@ -84,6 +84,9 @@ public: NativeValue* JsGetDisplayOrientation( NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + bool CheckAbilityType(const AbilityType typeWant); + bool UnwarpVerifyPermissionParams(NativeEngine &engine, NativeCallbackInfo &info, JsPermissionOptions &options); + bool GetStringsValue(NativeEngine &engine, NativeValue *object, std::vector &strList); std::string ConvertErrorCode(int32_t errCode); Ability *ability_; 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 26015cb6952..ca6ead2b3ba 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp @@ -4848,5 +4848,104 @@ NativeValue* JsNapiCommon::JsGetDisplayOrientation( return result; } + +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; +} + + +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 -- Gitee From 1f85168928b6d3fa269672f3f628a19e50fcd25e Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Sun, 11 Sep 2022 09:37:21 +0800 Subject: [PATCH 3/3] fix build error Signed-off-by: xinxin13 --- .../napi_common/js_napi_common_ability.h | 3 + .../inner/napi_common/napi_common_ability.cpp | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+) 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 cf5a9cf8e52..b7b98e82b3e 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 @@ -84,6 +84,9 @@ public: NativeValue* JsGetDisplayOrientation( NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType); + bool CheckAbilityType(const AbilityType typeWant); + bool UnwarpVerifyPermissionParams(NativeEngine &engine, NativeCallbackInfo &info, JsPermissionOptions &options); + bool GetStringsValue(NativeEngine &engine, NativeValue *object, std::vector &strList); std::string ConvertErrorCode(int32_t errCode); Ability *ability_; 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 26015cb6952..ca6ead2b3ba 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp @@ -4848,5 +4848,104 @@ NativeValue* JsNapiCommon::JsGetDisplayOrientation( return result; } + +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; +} + + +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 -- Gitee