diff --git a/frameworks/js/napi/featureAbility/BUILD.gn b/frameworks/js/napi/featureAbility/BUILD.gn index dfd66003a3..2ed32d7ee5 100644 --- a/frameworks/js/napi/featureAbility/BUILD.gn +++ b/frameworks/js/napi/featureAbility/BUILD.gn @@ -51,6 +51,7 @@ ohos_shared_library("featureability") { "ability_runtime:app_manager", "ability_runtime:data_ability_helper", "ability_runtime:napi_base_context", + "ability_runtime:runtime", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "eventhandler:libeventhandler", @@ -65,6 +66,11 @@ ohos_shared_library("featureability") { ] if (ability_runtime_graphics) { + include_dirs += [ + "${windowmanager_path}/interfaces/kits/napi/window_runtime/window_napi", + ] + deps += [ "${windowmanager_path}/interfaces/kits/napi/window_runtime:window_native_kit" ] + external_deps += [ "window_manager:libwm" ] defines = [ "SUPPORT_GRAPHICS" ] } diff --git a/frameworks/js/napi/featureAbility/feature_ability.cpp b/frameworks/js/napi/featureAbility/feature_ability.cpp index ae78532181..6aca30f91b 100644 --- a/frameworks/js/napi/featureAbility/feature_ability.cpp +++ b/frameworks/js/napi/featureAbility/feature_ability.cpp @@ -22,6 +22,9 @@ #include "ability_process.h" #include "element_name.h" #include "hilog_wrapper.h" +#ifdef SUPPORT_GRAPHICS +#include "js_window.h" +#endif #include "napi_context.h" #include "napi_data_ability_helper.h" #include "securec.h" @@ -69,6 +72,7 @@ napi_value FeatureAbilityInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("disconnectAbility", NAPI_FADisConnectAbility), DECLARE_NAPI_FUNCTION("continueAbility", NAPI_FAContinueAbility), DECLARE_NAPI_FUNCTION("getWantSync", NAPI_GetWantSync), + DECLARE_NAPI_FUNCTION("getWindow", NAPI_GetWindow), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties)); @@ -1041,6 +1045,222 @@ napi_value UnwrapAbilityResult(CallAbilityParam ¶m, napi_env env, napi_value return result; } +#ifdef SUPPORT_GRAPHICS +napi_value GetWindowWrapAsync( + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback)); + } + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + HILOG_INFO("GetWindowWrapAsync, worker pool thread execute."); + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + if (asyncCallbackInfo->ability != nullptr) { + asyncCallbackInfo->window = asyncCallbackInfo->ability->GetWindow(); + auto engine = reinterpret_cast(env); + OHOS::Rosen::CreateJsWindowObject(*engine, asyncCallbackInfo->window); + } else { + HILOG_ERROR("GetWindowWrapAsync, ability == nullptr"); + } + HILOG_INFO("GetWindowWrapAsync, worker pool thread execute end."); + }, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("GetWindowWrapAsync, main event thread complete."); + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + napi_value callback = 0; + napi_value undefined = 0; + napi_value result[ARGS_TWO] = {0}; + napi_value callResult = 0; + napi_get_undefined(env, &undefined); + result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); + napi_get_null(env, &result[PARAM1]); + napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); + napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + HILOG_INFO("GetWindowWrapAsync, main event thread complete end."); + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +napi_value GetWindowWrapPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncCallbackInfo->deferred = deferred; + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + HILOG_INFO("GetWindowWrapPromise, worker pool thread execute."); + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + if (asyncCallbackInfo->ability != nullptr) { + asyncCallbackInfo->window = asyncCallbackInfo->ability->GetWindow(); + auto engine = reinterpret_cast(env); + OHOS::Rosen::CreateJsWindowObject(*engine, asyncCallbackInfo->window); + } else { + HILOG_INFO("GetWindowWrapPromise, ability == nullptr"); + } + HILOG_INFO("GetWindowWrapPromise, worker pool thread execute end."); + }, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("GetWindowWrapPromise, main event thread complete."); + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + napi_value result = 0; + napi_get_null(env, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + HILOG_INFO("GetWindowWrapPromise, main event thread complete end."); + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetWindowWrap processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetWindowWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, asyncCallbackInfo == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetWindowWrapAsync(env, args, 0, asyncCallbackInfo); + } else { + ret = GetWindowWrapPromise(env, asyncCallbackInfo); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Get window. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetWindow(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + return WrapVoidToJS(env); + } + + napi_value ret = GetWindowWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + HILOG_ERROR("%{public}s,ret == nullptr", __func__); + if (asyncCallbackInfo != nullptr) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + ret = WrapVoidToJS(env); + } else { + HILOG_INFO("%{public}s, end.", __func__); + } + return ret; +} +#else +napi_value GetWindowWrapAsync( + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) +{ + return nullptr; +} + +napi_value GetWindowWrapPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) +{ + return nullptr; +} + +/** + * @brief GetWindowWrap processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetWindowWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +{ + return nullptr; +} + +/** + * @brief Get window. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetWindow(napi_env env, napi_callback_info info) +{ + return nullptr; +} +#endif + /** * @brief GetWantSyncWrap processing function. * diff --git a/frameworks/js/napi/featureAbility/feature_ability.h b/frameworks/js/napi/featureAbility/feature_ability.h index 2713b85ccf..1ef0424ae4 100644 --- a/frameworks/js/napi/featureAbility/feature_ability.h +++ b/frameworks/js/napi/featureAbility/feature_ability.h @@ -104,6 +104,16 @@ napi_value NAPI_HasWindowFocus(napi_env env, napi_callback_info info); */ napi_value NAPI_GetContext(napi_env env, napi_callback_info info); +/** + * @brief Get window. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetWindow(napi_env env, napi_callback_info info); + /** * @brief Get want. * diff --git a/frameworks/js/napi/inner/napi_common/feature_ability_common.h b/frameworks/js/napi/inner/napi_common/feature_ability_common.h index e3aa23cc53..2421d03c58 100644 --- a/frameworks/js/napi/inner/napi_common/feature_ability_common.h +++ b/frameworks/js/napi/inner/napi_common/feature_ability_common.h @@ -69,6 +69,9 @@ struct AsyncCallbackInfo { AbilityType abilityType = AbilityType::UNKNOWN; int errCode = 0; ContinueAbilityOptionsInfo optionInfo; +#ifdef SUPPORT_GRAPHICS + sptr window; +#endif }; struct CBBase {