diff --git a/frameworks/js/napi/featureAbility/BUILD.gn b/frameworks/js/napi/featureAbility/BUILD.gn index 32b8225e0813ed01fa1e9f9d4d3e5e74a75e7d44..fcf3661ccc6d4866a80a79148e2a0ec15fe8af6b 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 5b973b7fd2b07775fcad12d389708adb7520284d..3c1becf9b57c69eaf0cfa48b0b7f5440c7432594 100644 --- a/frameworks/js/napi/featureAbility/napi_context.cpp +++ b/frameworks/js/napi/featureAbility/napi_context.cpp @@ -24,10 +24,12 @@ #include "feature_ability_common.h" #include "file_ex.h" #include "hilog_wrapper.h" +#include "js_napi_common_ability.h" #include "securec.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; namespace OHOS { namespace AppExecFwk { @@ -2802,5 +2804,1017 @@ 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::JsGetContext(*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"); + 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(); + } + + 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 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, code, cbInfo = callbackInfo, value = errorVal] () { + if (permissionList.empty()) { + *value = static_cast(NAPI_ERR_PARAM_INVALID); + return; + } + CallAbilityPermissionParam permissionParam; + permissionParam.requestCode = code; + permissionParam.permission_list = permissionList; + auto processInstance = AbilityProcess::GetInstance(); + if (processInstance != nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + return; + } + processInstance->RequestPermissionsFromUser(obj->ability_, permissionParam, cbInfo); + }; + 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, *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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr bundleName = std::make_shared(); + auto execute = [obj = this, name = bundleName, value = errorVal] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (name == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + 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"); + return engine.CreateNull(); + } + JsPermissionOptions options; + bool flagCall = UnwarpVerifyPermissionParams(engine, info, options); + auto execute = [obj = this, permission, options, value = errorVal] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (options.uidFlag) { + *value = obj->ability_->VerifyPermission(permission, options.pid, options.uid); + } else { + *value = obj->ability_->VerifySelfPermission(permission); + } + }; + 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; + } + task.Resolve(engine, CreateJsValue(engine, *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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, info = infoData, value = errorVal] () { + if (obj->ability_ == nullptr) { + *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"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr processInfo = std::make_shared(); + auto execute = [obj = this, data = processInfo, value = errorVal] () { + if (obj->ability_ == nullptr) { + *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"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = processInfo, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr elementName = std::make_shared(); + auto execute = [obj = this, data = elementName, value = errorVal] () { + if (obj->ability_ == nullptr) { + *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"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, ename = elementName, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr processName = std::make_shared(); + auto execute = [obj = this, name = processName, value = errorVal] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (name == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr callingBundleName = std::make_shared(); + auto execute = [obj = this, name = callingBundleName, value = errorVal] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (name == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr createDir = std::make_shared(); + auto execute = [obj = this, dir = createDir, value = errorVal] () { + if (obj->ability_ == nullptr) { + *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) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + 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, value = errorVal] + (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 c9175c67ff310b5b8bb03e5d836d9196db817951..e396390b830401854e6980848698f4c5d251be07 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 8b3034de90ef183c83e39839c285e9d45f7a9d7b..32e1618c17a59cf1d951623c20ae2afaf3b20adf 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 0000000000000000000000000000000000000000..2534de097132c62ee194dd54f03944298222e561 --- /dev/null +++ b/frameworks/js/napi/inner/napi_common/js_napi_common_ability.h @@ -0,0 +1,123 @@ +/* + * 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 + * + * 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_; +}; +} // 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 fc2c9a692022051599cddad5eb8434c70ae3362d..f92fc76cb603437d64af81fdecc873b7339e016a 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_ability.cpp @@ -20,10 +20,15 @@ #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" +using namespace OHOS::AbilityRuntime; + namespace OHOS { namespace AppExecFwk { napi_ref thread_local g_dataAbilityHelper = nullptr; @@ -4802,5 +4807,831 @@ napi_value NAPI_TerminateAbilityCommon(napi_env env, napi_callback_info info) HILOG_INFO("%{public}s,end", __func__); return ret; } + +JsNapiCommon::JsNapiCommon() : ability_(nullptr) +{} + +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(); + } + + 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, value = errorVal] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr config = std::make_shared(); + auto execute = [obj = this, data = config, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + if (data == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr drawComplete = std::make_shared(); + auto execute = [obj = this, data = drawComplete, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + if (data == 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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr cacheDir = std::make_shared(); + auto execute = [obj = this, dir = cacheDir, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr type = std::make_shared(); + auto execute = [obj = this, apptype = type, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + if (apptype == nullptr) { + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + return; + } + apptype->name = obj->ability_->GetAppType(); + }; + auto complete = [obj = this, apptype = type, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, hapMod = infoData, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *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"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, appInfo = infoData, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *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"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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::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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr infoData = std::make_shared(); + auto execute = [obj = this, abilityInfo = infoData, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *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"); + *value = static_cast(NAPI_ERR_ABILITY_CALL_INVALID); + } + }; + auto complete = [obj = this, info = infoData, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + std::shared_ptr orCreateDistributedDir = std::make_shared(); + auto execute = [obj = this, dir = orCreateDistributedDir, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + auto context = obj->ability_->GetAbilityContext(); + if (context == nullptr) { + *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, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + 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; + } + 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(); + } + + auto errorVal = std::make_shared(static_cast(NAPI_ERR_NO_ERROR)); + auto execute = [obj = this, value = errorVal, abilityType] () { + if (obj->ability_ == nullptr) { + *value = static_cast(NAPI_ERR_ACE_ABILITY); + HILOG_ERROR("task execute error, the ability is nullptr"); + return; + } + if (!obj->CheckAbilityType(abilityType)) { + *value = static_cast(NAPI_ERR_ABILITY_TYPE_INVALID); + return; + } + *value = obj->ability_->GetDisplayOrientation(); + }; + auto complete = [obj = this, value = errorVal] + (NativeEngine &engine, AsyncTask &task, int32_t status) { + if (*value == NAPI_ERR_NO_ERROR) { + task.Reject(engine, CreateJsError(engine, NAPI_ERR_ACE_ABILITY, "ability is nullptr")); + } + + task.Resolve(engine, CreateJsValue(engine, *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 d56f13b542310e33145e888aeda5859ca8e92eb4..3abe4dfe1839cb59c3c1869a4be9511653e672a7 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 99564d1e41c54dea24728dfb9cff35756f2a9676..579acecf86393da3da4e3c1279024fb7350273a9 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",