From 712f95749995f54ecaafa6f1c593c2dceb474286 Mon Sep 17 00:00:00 2001 From: huruitao Date: Fri, 15 Nov 2024 17:25:50 +0800 Subject: [PATCH 01/12] add napi js;sink:setDiscoverable,source:startDiscovery Signed-off-by: huruitao --- BUILD.gn | 1 + .../js/wfd/native_module_ohos_wfd.cpp | 37 ++ frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp | 540 ++++++++++++++++++ .../kitsimpl/js/wfd/wfd_napi_source.cpp | 445 +++++++++++++++ interfaces/kits/js/wfd/BUILD.gn | 93 +++ .../js/wfd/include/native_module_ohos_wfd.h | 7 + .../kits/js/wfd/include/wfd_callback_napi.h | 120 ++++ .../kits/js/wfd/include/wfd_enum_napi.h | 66 +++ .../kits/js/wfd/include/wfd_js_result.h | 71 +++ .../kits/js/wfd/include/wfd_napi_sink.h | 64 +++ .../kits/js/wfd/include/wfd_napi_source.h | 59 ++ 11 files changed, 1503 insertions(+) create mode 100755 frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp create mode 100755 frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp create mode 100755 frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp create mode 100755 interfaces/kits/js/wfd/BUILD.gn create mode 100755 interfaces/kits/js/wfd/include/native_module_ohos_wfd.h create mode 100755 interfaces/kits/js/wfd/include/wfd_callback_napi.h create mode 100755 interfaces/kits/js/wfd/include/wfd_enum_napi.h create mode 100755 interfaces/kits/js/wfd/include/wfd_js_result.h create mode 100755 interfaces/kits/js/wfd/include/wfd_napi_sink.h create mode 100755 interfaces/kits/js/wfd/include/wfd_napi_source.h diff --git a/BUILD.gn b/BUILD.gn index d65af6e..4188a41 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -16,6 +16,7 @@ import("//build/ohos.gni") group("sharing_packages") { deps = [ "//foundation/CastEngine/castengine_wifi_display/interfaces/innerkits/native/wfd:sharingwfd_client", + "//foundation/CastEngine/castengine_wifi_display/interfaces/kits/js/wfd:sharingwfd_napi", "//foundation/CastEngine/castengine_wifi_display/sa_profile:sharing_sa_profile", "//foundation/CastEngine/castengine_wifi_display/services:sharing_services_package", "//foundation/CastEngine/castengine_wifi_display/services/etc:sharing_service.rc", diff --git a/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp b/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp new file mode 100755 index 0000000..6e167d6 --- /dev/null +++ b/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp @@ -0,0 +1,37 @@ +#include "native_module_ohos_wfd.h" + +namespace OHOS { +namespace Sharing { +/* + * Function registering all props and functions of ohos.camera module + */ +static napi_value Export(napi_env env, napi_value exports) +{ + SHARING_LOGI("called() WfdSinkNapi::Init()."); + InitEnums(env, exports); + WfdSinkNapi::Init(env, exports); + return exports; +} + +/* + * module define + */ +static napi_module g_module = {.nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Export, + .nm_modname = "multimedia.SharingWfd", + .nm_priv = ((void *)0), + .reserved = {0}}; + +/* + * module register + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + SHARING_LOGI("called() multimedia.SharingWfd."); + napi_module_register(&g_module); +} + +} // namespace Sharing +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp new file mode 100755 index 0000000..f48a767 --- /dev/null +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp @@ -0,0 +1,540 @@ +#include "wfd_napi.h" +#include "ability_manager/include/ability_manager_client.h" +#include "common/sharing_log.h" +#include "surface_utils.h" +#include "utils.h" +#include "wfd.h" + +namespace OHOS { +namespace Sharing { +napi_ref WfdSinkNapi::constructor_ = nullptr; +const std::string BUNDLE_NAME = "com.example.player"; +const std::string ABILITY_NAME = "MainAbility"; +const std::string CLASS_NAME = "WfdSinkImpl"; +const int32_t ARGS_ONE = 1; +const int32_t ARGS_TWO = 2; +const int32_t ARGS_THREE = 3; +const int32_t ARGS_FOUR = 4; +const int32_t STRING_MAX_SIZE = 255; + +WfdSinkNapi::WfdSinkNapi() +{ + SHARING_LOGI("ctor %{public}p.", this); +} + +WfdSinkNapi::~WfdSinkNapi() +{ + SHARING_LOGI("dtor %{public}p.", this); + CancelCallbackReference(); + nativeWfdSink_.reset(); +} + +napi_value WfdSinkNapi::Init(napi_env env, napi_value exports) +{ + SHARING_LOGD("trace."); + napi_property_descriptor staticProperty[] = {DECLARE_NAPI_STATIC_FUNCTION("createSink", CreateSink)}; + + napi_property_descriptor properties[] = {DECLARE_NAPI_FUNCTION("start", Start), + DECLARE_NAPI_FUNCTION("setDiscoverable", SetDiscoverable), + DECLARE_NAPI_FUNCTION("stop", Stop), + + napi_value constructor = nullptr; + napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, + sizeof(properties) / sizeof(properties[0]), properties, &constructor); + + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define WfdSinkClient class."); + + status = napi_create_reference(env, constructor, 1, &constructor_); + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to create reference of constructor."); + + status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor); + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set constructor."); + + status = napi_define_properties(env, exports, sizeof(staticProperty) / sizeof(staticProperty[0]), staticProperty); + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define static function."); + + SHARING_LOGD("success."); + return exports; +} + +void WfdSinkNapi::CancelCallbackReference() +{ + SHARING_LOGD("trace."); + std::lock_guard lock(refMutex_); + if (jsCallback_ != nullptr) { + jsCallback_->ClearCallbackReference(); + jsCallback_.reset(); + } + + refMap_.clear(); +} + +napi_value WfdSinkNapi::Constructor(napi_env env, napi_callback_info info) +{ + SHARING_LOGD("trace."); + napi_status status; + napi_value result = nullptr; + napi_value jsThis = nullptr; + + auto finalize = [](napi_env env, void *data, void *hint) { + SHARING_LOGD("Destructor in."); + auto *wfdSinkNapi = reinterpret_cast(data); + wfdSinkNapi->CancelCallbackReference(); + + delete wfdSinkNapi; + SHARING_LOGD("Destructor out."); + }; + + status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + napi_get_undefined(env, &result); + SHARING_LOGE("Failed to retrieve details about the call."); + return result; + } + + WfdSinkNapi *jsCast = new (std::nothrow) WfdSinkNapi(); + SHARING_CHECK_AND_RETURN_RET_LOG(jsCast != nullptr, result, "No memory."); + + jsCast->env_ = env; + + std::vector outInfo; + AAFwk::AbilityManagerClient::GetInstance()->GetAbilityRunningInfos(outInfo); + + if (outInfo.size() != 1) { + SHARING_LOGE("get ability size error: %{public}zu.", outInfo.size()); + for (auto &item : outInfo) { + SHARING_LOGW("get app, bundle: %{public}s, ability: %{public}s.", item.ability.GetBundleName().c_str(), + item.ability.GetAbilityName().c_str()); + } + + jsCast->bundleName_ = BUNDLE_NAME; + jsCast->abilityName_ = ABILITY_NAME; + } else { + jsCast->bundleName_ = outInfo[0].ability.GetBundleName(); + jsCast->abilityName_ = outInfo[0].ability.GetAbilityName(); + SHARING_LOGI("get app, bundle: %{public}s, ability: %{public}s.", jsCast->bundleName_.c_str(), + jsCast->abilityName_.c_str()); + } + + DmKit::InitDeviceManager(); + if (DmKit::GetTrustedDevicesInfo().size() > 0) { + for (auto &item : DmKit::GetTrustedDevicesInfo()) { + SHARING_LOGI("remote trusted device: %{public}s.", item.deviceId); + } + } + + RpcKeyParser parser; + jsCast->localKey_ = + parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); + + jsCast->nativeWfdSink_ = WfdSinkFactory::CreateSink(0, jsCast->localKey_); + SHARING_CHECK_AND_RETURN_RET_LOG(jsCast->nativeWfdSink_ != nullptr, result, "failed to WfdSinkImpl."); + + jsCast->jsCallback_ = std::make_shared(env); + jsCast->jsCallback_->SetWfdSinkNapi(jsCast); + jsCast->nativeWfdSink_->SetListener(jsCast->jsCallback_); + + status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, nullptr, nullptr); + if (status != napi_ok) { + napi_get_undefined(env, &result); + delete jsCast; + SHARING_LOGE("Fail to wrapping js to native napi."); + return result; + } + + SHARING_LOGD("success."); + return jsThis; +} + +napi_value WfdSinkNapi::CreateSink(napi_env env, napi_callback_info info) +{ + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value constructor = nullptr; + + napi_status status = napi_get_reference_value(env, constructor_, &constructor); + if (status != napi_ok) { + SHARING_LOGE("Fail to get the representation of constructor object."); + napi_get_undefined(env, &result); + return result; + } + + status = napi_new_instance(env, constructor, 0, nullptr, &result); + if (status != napi_ok) { + SHARING_LOGE("Fail to instantiate js cast instance."); + napi_get_undefined(env, &result); + return result; + } + + SHARING_LOGD("success."); + return result; +} + +napi_value WfdSinkNapi::SetDiscoverable(napi_env env, napi_callback_info info) +{ + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value jsThis = nullptr; + napi_value args[ARGS_TWO] = {nullptr}; + size_t argCount = ARGS_TWO; + + napi_get_undefined(env, &result); + auto asyncContext = std::make_unique(env); + asyncContext->asyncWorkType = AsyncWorkType::ASYNC_WORK_SET_DISCOVERABLE; + + napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + SHARING_LOGE("Failed to retrieve details about the call."); + asyncContext->SignError(SharingErrorCode::ERR_BAD_PARAMETER, "Failed to retrieve details about the call"); + } + + bool isDiscoverable = false; + if (!napi_get_value_bool(env, args[0], &isDiscoverable)) { + SHARING_LOGE("Failed to get isDiscoverable."); + asyncContext->SignError(SharingErrorCode::ERR_BAD_PARAMETER, "Failed to get isDiscoverable"); + } + + napi_valuetype valueType = napi_undefined; + if (args[1] == nullptr && napi_typeof(env, args[1], &valueType) != napi_ok && valueType != napi_function) { + SHARING_LOGW("no callback here."); + } + + napi_create_reference(env, args[1], 1, &(asyncContext->callbackRef)); + + if (asyncContext->callbackRef == nullptr) { + SHARING_LOGD("napi_create_promise."); + napi_create_promise(env, &(asyncContext->deferred), &result); + } + + (void)napi_unwrap(env, jsThis, reinterpret_cast(&asyncContext->napi)); + SHARING_CHECK_AND_RETURN_RET_LOG(asyncContext->napi != nullptr, result, "failed to GetJsInstance."); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetDiscoverable", NAPI_AUTO_LENGTH, &resource); + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSinkNapi::AsyncWork, WfdSinkNapi::CompleteCallback, + static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + + asyncContext.release(); + SHARING_LOGD("success."); + return result; +} + +napi_value WfdSinkNapi::Start(napi_env env, napi_callback_info info) +{ + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value jsThis = nullptr; + napi_value args[ARGS_ONE] = {nullptr}; + size_t argCount = ARGS_ONE; + + napi_get_undefined(env, &result); + auto asyncContext = std::make_unique(env); + asyncContext->asyncWorkType = AsyncWorkType::ASYNC_WORK_START; + + napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + SHARING_LOGE("Failed to retrieve details about the call."); + asyncContext->SignError(SharingErrorCode::ERR_BAD_PARAMETER, "Failed to retrieve details about the call"); + } + + napi_valuetype valueType = napi_undefined; + if (args[0] == nullptr && napi_typeof(env, args[0], &valueType) != napi_ok && valueType != napi_function) { + SHARING_LOGW("no callback here."); + } + + napi_create_reference(env, args[0], 1, &(asyncContext->callbackRef)); + + if (asyncContext->callbackRef == nullptr) { + SHARING_LOGD("napi_create_promise."); + napi_create_promise(env, &(asyncContext->deferred), &result); + } + + (void)napi_unwrap(env, jsThis, reinterpret_cast(&asyncContext->napi)); + SHARING_CHECK_AND_RETURN_RET_LOG(asyncContext->napi != nullptr, result, "failed to GetJsInstance."); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "Start", NAPI_AUTO_LENGTH, &resource); + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSinkNapi::AsyncWork, WfdSinkNapi::CompleteCallback, + static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + + asyncContext.release(); + SHARING_LOGD("success."); + return result; +} + +napi_value WfdSinkNapi::Stop(napi_env env, napi_callback_info info) +{ + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value jsThis = nullptr; + napi_value args[ARGS_ONE] = {nullptr}; + size_t argCount = ARGS_ONE; + + napi_get_undefined(env, &result); + auto asyncContext = std::make_unique(env); + asyncContext->asyncWorkType = AsyncWorkType::ASYNC_WORK_STOP; + + napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + SHARING_LOGE("Failed to retrieve details about the call."); + asyncContext->SignError(SharingErrorCode::ERR_BAD_PARAMETER, "Failed to retrieve details about the call"); + } + + napi_valuetype valueType = napi_undefined; + if (args[0] == nullptr && napi_typeof(env, args[0], &valueType) != napi_ok && valueType != napi_function) { + SHARING_LOGW("no callback here."); + } + + napi_create_reference(env, args[0], 1, &(asyncContext->callbackRef)); + + if (asyncContext->callbackRef == nullptr) { + SHARING_LOGD("napi_create_promise."); + napi_create_promise(env, &(asyncContext->deferred), &result); + } + + (void)napi_unwrap(env, jsThis, reinterpret_cast(&asyncContext->napi)); + SHARING_CHECK_AND_RETURN_RET_LOG(asyncContext->napi != nullptr, result, "failed to GetJsInstance."); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "Stop", NAPI_AUTO_LENGTH, &resource); + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSinkNapi::AsyncWork, WfdSinkNapi::CompleteCallback, + static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + + asyncContext.release(); + SHARING_LOGD("success."); + return result; +} + +void WfdSinkNapi::AsyncWork(napi_env env, void *data) +{ + SHARING_LOGD("trace."); + auto asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + SHARING_LOGE("WfdSinkAsyncContext is nullptr."); + return; + } + + SHARING_LOGD("workType: %{public}d.", asyncContext->asyncWorkType); + if (asyncContext->napi == nullptr || asyncContext->napi->nativeWfdSink_ == nullptr) { + if (asyncContext->napi == nullptr) { + SHARING_LOGE("WfdSinkAsyncContext->napi is nullptr."); + } else { + SHARING_LOGE("WfdSinkAsyncContext->nativeWfdSink_ is nullptr."); + } + + asyncContext->SignError(CommonErrorCode, "native instance is null"); + return; + } + + auto jsCast = asyncContext->napi; + auto nativeWfdSink = jsCast->nativeWfdSink_; + int32_t ret = 0; + + switch (asyncContext->asyncWorkType) { + case AsyncWorkType::ASYNC_WORK_SET_DISCOVERABLE: { + if (!asyncContext->errFlag) { + bool isDiscoverable = *static_cast(asyncContext->data); + if (isDiscoverable) { + ret = nativeWfdSink->Start(); + } else { + ret = nativeWfdSink->Stop(); + } + if (ret < 0) { + SHARING_LOGE("nativeWfdSink->SetDiscoverable error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance SetDiscoverable error"); + return; + } + } else { + ret = asyncContext->errCode; + } + + asyncContext->JsResult = std::make_unique(ret); + break; + } + case AsyncWorkType::ASYNC_WORK_START: { + if (!asyncContext->errFlag) { + ret = nativeWfdSink->Start(); + if (ret < 0) { + SHARING_LOGE("nativeWfdSink->Start error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance Start error"); + return; + } + } else { + ret = asyncContext->errCode; + } + + asyncContext->JsResult = std::make_unique(ret); + break; + } + case AsyncWorkType::ASYNC_WORK_STOP: { + if (!asyncContext->errFlag) { + ret = nativeWfdSink->Stop(); + if (ret < 0) { + SHARING_LOGE("nativeWfdSink->Stop error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance Stop error"); + return; + } + } else { + ret = asyncContext->errCode; + } + + asyncContext->JsResult = std::make_unique(ret); + break; + } + default: + SHARING_LOGE("error unknown operation (%{public}d).", (int32_t)asyncContext->asyncWorkType); + break; + } + + SHARING_LOGD("workType: %{public}d.", asyncContext->asyncWorkType); + return; +} + +void WfdSinkNapi::CompleteCallback(napi_env env, napi_status status, void *data) +{ + SHARING_LOGD("trace."); + auto asyncContext = reinterpret_cast(data); + SHARING_CHECK_AND_RETURN_LOG(asyncContext != nullptr, "asyncContext is nullptr!"); + + if (status != napi_ok) { + asyncContext->SignError(CommonErrorCode, "napi_create_async_work status != napi_ok"); + } + + napi_value result = nullptr; + napi_get_undefined(env, &result); + + napi_value args[2] = {nullptr}; + napi_get_undefined(env, &args[0]); + napi_get_undefined(env, &args[1]); + + if (asyncContext->errFlag) { + SHARING_LOGE("async callback failed."); + CreateError(env, asyncContext->errCode, asyncContext->errMessage, result); + args[0] = result; + } else { + SHARING_LOGD("async callback out."); + if (asyncContext->JsResult != nullptr) { + auto res = asyncContext->JsResult->GetJsResult(env, result); + if (res == napi_ok) { + args[1] = result; + } else { + SHARING_LOGE("asyncContext->JsResult->GetJsResult error, %{public}d.", (int32_t)res); + asyncContext->SignError(CommonErrorCode, "failed to get return data"); + CreateError(env, asyncContext->errCode, asyncContext->errMessage, result); + args[0] = result; + } + } + } + + if (asyncContext->deferred) { + if (asyncContext->errFlag) { + SHARING_LOGD("napi_reject_deferred."); + napi_reject_deferred(env, asyncContext->deferred, args[0]); + } else { + SHARING_LOGD("napi_resolve_deferred."); + napi_resolve_deferred(env, asyncContext->deferred, args[1]); + } + } else { + SHARING_LOGD("napi_call_function callback."); + napi_value callback = nullptr; + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); + + constexpr size_t argCount = 2; + napi_value retVal; + napi_get_undefined(env, &retVal); + napi_call_function(env, nullptr, callback, argCount, args, &retVal); + napi_delete_reference(env, asyncContext->callbackRef); + } + + napi_delete_async_work(env, asyncContext->work); + + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + SHARING_LOGD("success."); +} + +napi_status WfdSinkNapi::CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal) +{ + SHARING_LOGD("trace."); + napi_get_undefined(env, &errVal); + + napi_value msgValStr = nullptr; + napi_status nstatus = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr); + if (nstatus != napi_ok || msgValStr == nullptr) { + SHARING_LOGE("create error message str fail."); + return napi_invalid_arg; + } + + nstatus = napi_create_error(env, nullptr, msgValStr, &errVal); + if (nstatus != napi_ok || errVal == nullptr) { + SHARING_LOGE("create error fail."); + return napi_invalid_arg; + } + + napi_value codeStr = nullptr; + nstatus = napi_create_string_utf8(env, "code", NAPI_AUTO_LENGTH, &codeStr); + if (nstatus != napi_ok || codeStr == nullptr) { + SHARING_LOGE("create code str fail."); + return napi_invalid_arg; + } + + napi_value errCodeVal = nullptr; + nstatus = napi_create_int32(env, errCode, &errCodeVal); + if (nstatus != napi_ok || errCodeVal == nullptr) { + SHARING_LOGE("create error code number val fail."); + return napi_invalid_arg; + } + + nstatus = napi_set_property(env, errVal, codeStr, errCodeVal); + if (nstatus != napi_ok) { + SHARING_LOGE("set error code property fail."); + return napi_invalid_arg; + } + + napi_value msgStr = nullptr; + nstatus = napi_create_string_utf8(env, "msg", NAPI_AUTO_LENGTH, &msgStr); + if (nstatus != napi_ok || msgStr == nullptr) { + SHARING_LOGE("create msg str fail."); + return napi_invalid_arg; + } + + nstatus = napi_set_property(env, errVal, msgStr, msgValStr); + if (nstatus != napi_ok) { + SHARING_LOGE("set error msg property fail."); + return napi_invalid_arg; + } + + napi_value nameStr = nullptr; + nstatus = napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &nameStr); + if (nstatus != napi_ok || nameStr == nullptr) { + SHARING_LOGE("create name str fail."); + return napi_invalid_arg; + } + + napi_value errNameVal = nullptr; + nstatus = napi_create_string_utf8(env, "BusinessError", NAPI_AUTO_LENGTH, &errNameVal); + if (nstatus != napi_ok || errNameVal == nullptr) { + SHARING_LOGE("create BusinessError str fail."); + return napi_invalid_arg; + } + + nstatus = napi_set_property(env, errVal, nameStr, errNameVal); + if (nstatus != napi_ok) { + SHARING_LOGE("set error name property fail."); + return napi_invalid_arg; + } + + return napi_ok; +} + +} // namespace Sharing +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp new file mode 100755 index 0000000..6005fc7 --- /dev/null +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -0,0 +1,445 @@ +// wfd_source_napi.cpp +#include "wfd_source_napi.h" +#include "ability_manager/include/ability_manager_client.h" +#include "common/sharing_log.h" +#include "surface_utils.h" +#include "utils.h" +#include "wfd.h" + +namespace OHOS { +namespace Sharing { + +napi_ref WfdSourceNapi::constructor_ = nullptr; +const std::string BUNDLE_NAME = "com.example.player"; +const std::string ABILITY_NAME = "MainAbility"; +const std::string CLASS_NAME = "WfdSourceImpl"; +const int32_t ARGS_ONE = 1; +const int32_t ARGS_TWO = 2; +const int32_t STRING_MAX_SIZE = 255; + +WfdSourceNapi::WfdSourceNapi() { + SHARING_LOGI("ctor %{public}p.", this); +} + +WfdSourceNapi::~WfdSourceNapi() { + SHARING_LOGI("dtor %{public}p.", this); + CancelCallbackReference(); + nativeWfdSource_.reset(); +} + +napi_value WfdSourceNapi::Init(napi_env env, napi_value exports) { + SHARING_LOGD("trace."); + napi_property_descriptor staticProperty[] = {DECLARE_NAPI_STATIC_FUNCTION("createSource", CreateSource)}; + + napi_property_descriptor properties[] = {DECLARE_NAPI_FUNCTION("startDiscovery", StartDiscovery), + DECLARE_NAPI_FUNCTION("stopDiscovery", StopDiscovery)}; + + napi_value constructor = nullptr; + napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, + sizeof(properties) / sizeof(properties[0]), properties, &constructor); + + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define WfdSourceClient class."); + + status = napi_create_reference(env, constructor, 1, &constructor_); + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to create reference of constructor."); + + status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor); + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set constructor."); + + status = napi_define_properties(env, exports, sizeof(staticProperty) / sizeof(staticProperty[0]), staticProperty); + SHARING_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define static function."); + + SHARING_LOGD("success."); + return exports; +} + +void WfdSourceNapi::CancelCallbackReference() { + SHARING_LOGD("trace."); + std::lock_guard lock(refMutex_); + if (jsCallback_ != nullptr) { + jsCallback_->ClearCallbackReference(); + jsCallback_.reset(); + } + + refMap_.clear(); +} + +napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) { + SHARING_LOGD("trace."); + napi_status status; + napi_value result = nullptr; + napi_value jsThis = nullptr; + + auto finalize = [](napi_env env, void *data, void *hint) { + SHARING_LOGD("Destructor in."); + auto *wfdSourceNapi = reinterpret_cast(data); + wfdSourceNapi->CancelCallbackReference(); + + delete wfdSourceNapi; + SHARING_LOGD("Destructor out."); + }; + + status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + napi_get_undefined(env, &result); + SHARING_LOGE("Failed to retrieve details about the call."); + return result; + } + + WfdSourceNapi *jsCast = new (std::nothrow) WfdSourceNapi(); + SHARING_CHECK_AND_RETURN_RET_LOG(jsCast != nullptr, result, "No memory."); + + jsCast->env_ = env; + + std::vector outInfo; + AAFwk::AbilityManagerClient::GetInstance()->GetAbilityRunningInfos(outInfo); + + if (outInfo.size() != 1) { + SHARING_LOGE("get ability size error: %{public}zu.", outInfo.size()); + for (auto &item : outInfo) { + SHARING_LOGW("get app, bundle: %{public}s, ability: %{public}s.", item.ability.GetBundleName().c_str(), + item.ability.GetAbilityName().c_str()); + } + + jsCast->bundleName_ = BUNDLE_NAME; + jsCast->abilityName_ = ABILITY_NAME; + } else { + jsCast->bundleName_ = outInfo[0].ability.GetBundleName(); + jsCast->abilityName_ = outInfo[0].ability.GetAbilityName(); + SHARING_LOGI("get app, bundle: %{public}s, ability: %{public}s.", jsCast->bundleName_.c_str(), + jsCast->abilityName_.c_str()); + } + + DmKit::InitDeviceManager(); + if (DmKit::GetTrustedDevicesInfo().size() > 0) { + for (auto &item : DmKit::GetTrustedDevicesInfo()) { + SHARING_LOGI("remote trusted device: %{public}s.", item.deviceId); + } + } + + RpcKeyParser parser; + jsCast->localKey_ = + parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); + + jsCast->nativeWfdSource_ = WfdSourceFactory::CreateSource(0, jsCast->localKey_); + SHARING_CHECK_AND_RETURN_RET_LOG(jsCast->nativeWfdSource_ != nullptr, result, "failed to WfdSourceImpl."); + + jsCast->jsCallback_ = std::make_shared(env); + jsCast->jsCallback_->SetWfdSourceNapi(jsCast); + jsCast->nativeWfdSource_->SetListener(jsCast->jsCallback_); + +status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, nullptr, nullptr); + if (status != napi_ok) { + napi_get_undefined(env, &result); + delete jsCast; + SHARING_LOGE("Fail to wrapping js to native napi."); + return result; + } + + SHARING_LOGD("success."); + return jsThis; +} + +napi_value WfdSourceNapi::CreateSource(napi_env env, napi_callback_info info) { + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value constructor = nullptr; + + napi_status status = napi_get_reference_value(env, constructor_, &constructor); + if (status != napi_ok) { + SHARING_LOGE("Fail to get the representation of constructor object."); + napi_get_undefined(env, &result); + return result; + } + + status = napi_new_instance(env, constructor, 0, nullptr, &result); + if (status != napi_ok) { + SHARING_LOGE("Fail to instantiate js cast instance."); + napi_get_undefined(env, &result); + return result; + } + + SHARING_LOGD("success."); + return result; +} + +napi_value WfdSourceNapi::StartDiscovery(napi_env env, napi_callback_info info) { + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value jsThis = nullptr; + napi_value args[ARGS_ONE] = {nullptr}; + size_t argCount = ARGS_ONE; + + napi_get_undefined(env, &result); + auto asyncContext = std::make_unique(env); + asyncContext->asyncWorkType = AsyncWorkType::ASYNC_WORK_START_DISCOVERY; + + napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + SHARING_LOGE("Failed to retrieve details about the call."); + asyncContext->SignError(SharingErrorCode::ERR_BAD_PARAMETER, "Failed to retrieve details about the call"); + } + + napi_valuetype valueType = napi_undefined; + if (args[0] == nullptr && napi_typeof(env, args[0], &valueType) != napi_ok && valueType != napi_function) { + SHARING_LOGW("no callback here."); + } + + napi_create_reference(env, args[0], 1, &(asyncContext->callbackRef)); + + if (asyncContext->callbackRef == nullptr) { + SHARING_LOGD("napi_create_promise."); + napi_create_promise(env, &(asyncContext->deferred), &result); + } + + (void)napi_unwrap(env, jsThis, reinterpret_cast(&asyncContext->napi)); + SHARING_CHECK_AND_RETURN_RET_LOG(asyncContext->napi != nullptr, result, "failed to GetJsInstance."); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "StartDiscovery", NAPI_AUTO_LENGTH, &resource); + + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, WfdSourceNapi::CompleteCallback, + static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + + asyncContext.release(); + SHARING_LOGD("success."); + return result; +} + +napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) { + SHARING_LOGD("trace."); + napi_value result = nullptr; + napi_value jsThis = nullptr; + napi_value args[ARGS_ONE] = {nullptr}; + size_t argCount = ARGS_ONE; + + napi_get_undefined(env, &result); + auto asyncContext = std::make_unique(env); + asyncContext->asyncWorkType = AsyncWorkType::ASYNC_WORK_STOP_DISCOVERY; + + napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr); + if (status != napi_ok || jsThis == nullptr) { + SHARING_LOGE("Failed to retrieve details about the call."); + asyncContext->SignError(SharingErrorCode::ERR_BAD_PARAMETER, "Failed to retrieve details about the call"); + } + + napi_valuetype valueType = napi_undefined; + if (args[0] == nullptr && napi_typeof(env, args[0], &valueType) != napi_ok && valueType != napi_function) { + SHARING_LOGW("no callback here."); + } + + napi_create_reference(env, args[0], 1, &(asyncContext->callbackRef)); + + if (asyncContext->callbackRef == nullptr) { + SHARING_LOGD("napi_create_promise."); + napi_create_promise(env, &(asyncContext->deferred), &result); + } + + (void)napi_unwrap(env, jsThis, reinterpret_cast(&asyncContext->napi)); + SHARING_CHECK_AND_RETURN_RET_LOG(asyncContext->napi != nullptr, result, "failed to GetJsInstance."); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "StopDiscovery", NAPI_AUTO_LENGTH, &resource); + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, WfdSourceNapi::CompleteCallback, + static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + + asyncContext.release(); + SHARING_LOGD("success."); + return result; +} + +void WfdSourceNapi::AsyncWork(napi_env env, void *data) { + SHARING_LOGD("trace."); + auto asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + SHARING_LOGE("WfdSourceAsyncContext is nullptr."); + return; + } + + SHARING_LOGD("workType: %{public}d.", asyncContext->asyncWorkType); + if (asyncContext->napi == nullptr || asyncContext->napi->nativeWfdSource_ == nullptr) { + if (asyncContext->napi == nullptr) { + SHARING_LOGE("WfdSourceAsyncContext->napi is nullptr."); + } else { + SHARING_LOGE("WfdSourceAsyncContext->nativeWfdSource_ is nullptr."); + } + + asyncContext->SignError(CommonErrorCode, "native instance is null"); + return; + } + + auto jsCast = asyncContext->napi; + auto nativeWfdSource = jsCast->nativeWfdSource_; + int32_t ret = 0; + + switch (asyncContext->asyncWorkType) { + case AsyncWorkType::ASYNC_WORK_START_DISCOVERY: { + ret = nativeWfdSource->StartDiscover(); + if (ret < 0) { + SHARING_LOGE("nativeWfdSource->StartDiscovery error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance StartDiscovery error"); + return; + } + asyncContext->JsResult = std::make_unique(ret); + break; + } + case AsyncWorkType::ASYNC_WORK_STOP_DISCOVERY: { + ret = nativeWfdSource->StopDiscover(); + if (ret < 0) { + SHARING_LOGE("nativeWfdSource->StopDiscovery error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance StopDiscovery error"); + return; + } + asyncContext->JsResult = std::make_unique(ret); + break; + } + default: + SHARING_LOGE("error unknown operation (%{public}d).", (int32_t)asyncContext->asyncWorkType); + break; + } +} + +void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *data) { + SHARING_LOGD("trace."); + auto asyncContext = reinterpret_cast(data); + SHARING_CHECK_AND_RETURN_LOG(asyncContext != nullptr, "asyncContext is nullptr!"); + + if (status != napi_ok) { + asyncContext->SignError(CommonErrorCode, "napi_create_async_work status != napi_ok"); + } + + napi_value result = nullptr; + napi_get_undefined(env, &result); + + napi_value args[2] = {nullptr}; + napi_get_undefined(env, &args[0]); + napi_get_undefined(env, &args[1]); + + if (asyncContext->errFlag) { + SHARING_LOGE("async callback failed."); + CreateError(env, asyncContext->errCode, asyncContext->errMessage, result); + args[0] = result; + } else { + SHARING_LOGD("async callback out."); + if (asyncContext->JsResult != nullptr) { + auto res = asyncContext->JsResult->GetJsResult(env, result); + if (res == napi_ok) { + args[1] = result; + } else { + SHARING_LOGE("asyncContext->JsResult->GetJsResult error, %{public}d.", (int32_t)res); + asyncContext->SignError(CommonErrorCode, "failed to get return data"); + CreateError(env, asyncContext->errCode, asyncContext->errMessage, result); + args[0] = result; + } + } + } + + if (asyncContext->deferred) { + if (asyncContext->errFlag) { + SHARING_LOGD("napi_reject_deferred."); + napi_reject_deferred(env, asyncContext->deferred, args[0]); + } else { + SHARING_LOGD("napi_resolve_deferred."); + napi_resolve_deferred(env, asyncContext->deferred, args[1]); + } + } else { + SHARING_LOGD("napi_call_function callback."); + napi_value callback = nullptr; + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); + + constexpr size_t argCount = 2; + napi_value retVal; + napi_get_undefined(env, &retVal); + napi_call_function(env, nullptr, callback, argCount, args, &retVal); + napi_delete_reference(env, asyncContext->callbackRef); + } + + napi_delete_async_work(env, asyncContext->work); + + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + SHARING_LOGD("success."); +} + +napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal) { + SHARING_LOGD("trace."); + napi_get_undefined(env, &errVal); + + napi_value msgValStr = nullptr; + napi_status nstatus = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr); + if (nstatus != napi_ok || msgValStr == nullptr) { + SHARING_LOGE("create error message str fail."); + return napi_invalid_arg; + } + + nstatus = napi_create_error(env, nullptr, msgValStr, &errVal); + if (nstatus != napi_ok || errVal == nullptr) { + SHARING_LOGE("create error fail."); + return napi_invalid_arg; + } + + napi_value codeStr = nullptr; + nstatus = napi_create_string_utf8(env, "code", NAPI_AUTO_LENGTH, &codeStr); + if (nstatus != napi_ok || codeStr == nullptr) { + SHARING_LOGE("create code str fail."); + return napi_invalid_arg; + } + + napi_value errCodeVal = nullptr; + nstatus = napi_create_int32(env, errCode, &errCodeVal); + if (nstatus != napi_ok || errCodeVal == nullptr) { + SHARING_LOGE("create error code number val fail."); + return napi_invalid_arg; + } + + nstatus = napi_set_property(env, errVal, codeStr, errCodeVal); + if (nstatus != napi_ok) { + SHARING_LOGE("set error code property fail."); + return napi_invalid_arg; + } + + napi_value msgStr = nullptr; + nstatus = napi_create_string_utf8(env, "msg", NAPI_AUTO_LENGTH, &msgStr); + if (nstatus != napi_ok || msgStr == nullptr) { + SHARING_LOGE("create msg str fail."); + return napi_invalid_arg; + } + + nstatus = napi_set_property(env, errVal, msgStr, msgValStr); + if (nstatus != napi_ok) { + SHARING_LOGE("set error msg property fail."); + return napi_invalid_arg; + } + + napi_value nameStr = nullptr; + nstatus = napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &nameStr); + if (nstatus != napi_ok || nameStr == nullptr) { + SHARING_LOGE("create name str fail."); + return napi_invalid_arg; + } + + napi_value errNameVal = nullptr; + nstatus = napi_create_string_utf8(env, "BusinessError", NAPI_AUTO_LENGTH, &errNameVal); + if (nstatus != napi_ok || errNameVal == nullptr) { + SHARING_LOGE("create BusinessError str fail."); + return napi_invalid_arg; + } + + nstatus = napi_set_property(env, errVal, nameStr, errNameVal); + if (nstatus != napi_ok) { + SHARING_LOGE("set error name property fail."); + return napi_invalid_arg; + } + + return napi_ok; +} + +} // namespace Sharing +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/wfd/BUILD.gn b/interfaces/kits/js/wfd/BUILD.gn new file mode 100755 index 0000000..7c1a112 --- /dev/null +++ b/interfaces/kits/js/wfd/BUILD.gn @@ -0,0 +1,93 @@ +# Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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. + +import("//build/ohos.gni") +import("//foundation/CastEngine/castengine_wifi_display/config.gni") + +config("sharing_service_config") { + include_dirs = [ + "$SHARING_ROOT_DIR/services", + "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", + "$SHARING_ROOT_DIR/services/interaction/ipc_codec", + "$SHARING_ROOT_DIR/services/interaction/device_kit", + "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include/", + "//foundation/communication/ipc/ipc/native/c/ipc/include", + "$SHARING_ROOT_DIR/services/extend/magic_enum", + "//utils/native/base/include", + ] + + cflags = [ "-Wno-c++20-extensions" ] +} + +ohos_shared_library("sharingwfd_napi") { + install_enable = true + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "$SHARING_ROOT_DIR", + "$SHARING_ROOT_DIR/services/utils", + "$SHARING_ROOT_DIR/interfaces/kits/js/wfd/include", + "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", + "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/ability/ability_runtime/interfaces/inner_api", + "//foundation/ability/ability_runtime/interfaces/kits/native", + "//foundation/ability/ability_base/interfaces/inner_api", + "//foundation/ability/ability_base/interfaces/kits/native", + ] + + sources = [ + "$SHARING_ROOT_DIR/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp", + "$SHARING_ROOT_DIR/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp", + ] + + public_configs = [ + ":sharing_service_config", + "$SHARING_ROOT_DIR/tests:coverage_flags", + ] + + deps = [ + "$SHARING_ROOT_DIR/services/utils:sharing_utils", + "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", + "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd:sharingwfd_client", + "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", + "//foundation/ability/ability_runtime/frameworks/native/ability/native:abilitykit_native", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:app_manager", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "device_manager:devicemanagersdk", + "c_utils:utils", + "graphic_standard:surface", + "hilog_native:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + ] + + subsystem_name = "castplus" + part_name = "sharing_framework" +} diff --git a/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h b/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h new file mode 100755 index 0000000..45ae284 --- /dev/null +++ b/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h @@ -0,0 +1,7 @@ +#ifndef OHOS_SHARING_NATIVE_MODULE_OHOS_SHARINGWFD_H +#define OHOS_SHARING_NATIVE_MODULE_OHOS_SHARINGWFD_H + +#include "wfd_enum_napi.h" +#include "wfd_napi.h" + +#endif \ No newline at end of file diff --git a/interfaces/kits/js/wfd/include/wfd_callback_napi.h b/interfaces/kits/js/wfd/include/wfd_callback_napi.h new file mode 100755 index 0000000..7758f05 --- /dev/null +++ b/interfaces/kits/js/wfd/include/wfd_callback_napi.h @@ -0,0 +1,120 @@ +#ifndef OHOS_SHARING_WFD_CALLBACK_NAPI_H +#define OHOS_SHARING_WFD_CALLBACK_NAPI_H + +#include "napi/native_api.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "services/impl/scene/wfd/wfd_def.h" +#include "uv.h" +#include "wfd.h" +#include "wfd_js_result.h" +#include "services/common/sharing_hisysevent.h" + +namespace OHOS { +namespace Sharing { + +enum class AsyncWorkType { + ASYNC_WORK_INVALID = 0, + ASYNC_WORK_START, + ASYNC_WORK_STOP, + ASYNC_WORK_START_DISCOVERY, + ASYNC_WORK_STOP_DISCOVERY +}; + +class WfdSinkNapi; + +struct WfdSinkAsyncContext { + explicit WfdSinkAsyncContext(napi_env env1) : env(env1) {} + + void SignError(int32_t code, std::string message) + { + SHARING_LOGD("trace."); + errFlag = true; + errCode = code; + errMessage = message; + } + + bool errFlag = false; + + int32_t errCode; + int32_t videoFormat; + + float volume; + + std::string deviceId; + std::string surfaceId; + std::string errMessage; + + std::unique_ptr JsResult; + + napi_env env; + napi_async_work work = nullptr; + napi_ref callbackRef = nullptr; + napi_deferred deferred = nullptr; + + SceneType sceneType; + CodecAttr videoAttr; + CodecAttr audioAttr; + + WfdSinkNapi *napi = nullptr; + AsyncWorkType asyncWorkType = AsyncWorkType::ASYNC_WORK_INVALID; +}; + +struct AutoRef { + AutoRef(napi_env env, napi_ref cb) : env_(env), cb_(cb) {} + ~AutoRef() + { + SHARING_LOGD("trace."); + if (env_ != nullptr && cb_ != nullptr) { + SHARING_LOGD("~Wfd AutoRef dtor."); + (void)napi_delete_reference(env_, cb_); + } + } + + napi_env env_; + + napi_ref cb_; +}; + +class WfdSinkCallbackNapi : public IWfdSinkListener { +public: + WfdSinkCallbackNapi(napi_env env) : env_(env) {} + + void SetWfdSinkNapi(WfdSinkNapi *napi) + { + SHARING_LOGD("trace."); + napi_ = napi; + } + +public: + void OnInfo(std::shared_ptr &info) override; + + void OnAccelerationDone(std::string surfaceId); + void OnDeviceStateChanged(const ConnectionInfo &info); + void OnError(std::string deviceId, int32_t errorCode, std::string msg); + + void ClearCallbackReference(); + void SaveCallbackReference(const std::string &name, std::weak_ptr ref); + +protected: + struct WfdJsCallback { + std::string callbackName = "unknown"; + + std::weak_ptr callback; + std::unique_ptr jsResult; + }; + + void OnJsCallback(WfdJsCallback *jsCb); + +protected: + std::unordered_map> refMap_; + +private: + std::mutex mutex_; + napi_env env_ = nullptr; + WfdSinkNapi *napi_ = nullptr; +}; + +} // namespace Sharing +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/wfd/include/wfd_enum_napi.h b/interfaces/kits/js/wfd/include/wfd_enum_napi.h new file mode 100755 index 0000000..e5053dc --- /dev/null +++ b/interfaces/kits/js/wfd/include/wfd_enum_napi.h @@ -0,0 +1,66 @@ +#ifndef OHOS_SHARING_WFD_ENUM_NAPI_H +#define OHOS_SHARING_WFD_ENUM_NAPI_H + +#include +#include "common/sharing_log.h" +#include "napi/native_api.h" +#include "wfd_js_result.h" + +namespace OHOS { +namespace Sharing { + +static const std::unordered_map mapVideoFormatId = { + {"VIDEO_640x480_60", VideoFormat::VIDEO_640x480_60}, + {"VIDEO_1280x720_25", VideoFormat::VIDEO_1280x720_25}, + {"VIDEO_1280x720_30", VideoFormat::VIDEO_1280x720_30}, + {"VIDEO_1920x1080_25", VideoFormat::VIDEO_1920x1080_25}, + {"VIDEO_1920x1080_30", VideoFormat::VIDEO_1920x1080_30}}; + +static const std::unordered_map mapAudioFormatId = { + {"AUDIO_48000_16_2", AudioFormat::AUDIO_48000_16_2}, {"AUDIO_48000_16_4", AudioFormat::AUDIO_48000_16_4}}; + +static const std::unordered_map mapDeviceState = { + {"CONNECTED", DeviceState::CONNECTED}, + {"DISCONNECTED", DeviceState::DISCONNECTED}, + {"INIT", DeviceState::INIT}, + {"READY", DeviceState::READY}, + {"PLAYING", DeviceState::PLAYING}, + {"PAUSED", DeviceState::PAUSED}, + {"STOPPED", DeviceState::STOPPED},}; + +constexpr int32_t CommonErrorCode = 18800001; +static const std::unordered_map mapErrorCode = {{"ERR_OK", 0}, + {"ERR_GENERAL_ERROR", CommonErrorCode}, + {"ERR_BAD_PARAMETER", 18800002}, + {"ERR_INVALID_ID", 18800003}, + {"ERR_SERVICE_LIMIT", 18800004}, + {"ERR_STATE_EXCEPTION", 18800005}, + {"ERR_CONNECTION_FAILURE", 18800006}, + {"ERR_INTERACTION_FAILURE", 18800007}, + {"ERR_CONNECTION_TIMEOUT", 18800008}, + {"ERR_CONNECTION_REFUSED", 18800009}, + {"ERR_NETWORK_ERROR ", 18800011}, + {"ERR_INVALID_SURFACE_ID", 18800101}, + {"ERR_SURFACE_FAILURE", 18800102}, + {"ERR_DECODE_ERROR", 18800601}, + {"ERR_DECODE_FORMAT", 18800602}, + {"ERR_DECODE_SCARCE_CAPACITY", 18800603}, + {"ERR_PLAY_START", 18800608}, + {"ERR_PLAY_STOP", 18800609}, + {"ERR_RECEIVING_LIMIT", 18802101}}; + +static const std::unordered_map mapSceneType = {{"FOREGROUND", SceneType::FOREGROUND}, + {"BACKGROUND", SceneType::BACKGROUND}}; + +static const std::unordered_map mapCodecType = { + {"CODEC_H264", CodecId::CODEC_H264}, {"CODEC_H265", CodecId::CODEC_H265}, {"CODEC_AAC", CodecId::CODEC_AAC}, + {"CODEC_G711A", CodecId::CODEC_G711A}, {"CODEC_G711U", CodecId::CODEC_G711U}, {"CODEC_Opus", CodecId::CODEC_Opus}, + {"CODEC_L16", CodecId::CODEC_L16}, {"CODEC_VP8", CodecId::CODEC_VP8}, {"CODEC_VP9", CodecId::CODEC_VP9}, + {"CODEC_AV1", CodecId::CODEC_AV1}, {"CODEC_PCM", CodecId::CODEC_PCM}, +}; + +napi_status InitEnums(napi_env env, napi_value exports); + +} // namespace Sharing +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/wfd/include/wfd_js_result.h b/interfaces/kits/js/wfd/include/wfd_js_result.h new file mode 100755 index 0000000..2145e5d --- /dev/null +++ b/interfaces/kits/js/wfd/include/wfd_js_result.h @@ -0,0 +1,71 @@ +#ifndef OHOS_SHARING_WFD_JS_RESULT_H +#define OHOS_SHARING_WFD_JS_RESULT_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "wfd.h" + +namespace OHOS { +namespace Sharing { + +class WfdJsResult { +public: + WfdJsResult(int32_t ret) : ret_(ret) {} + virtual ~WfdJsResult() = default; + + virtual napi_status GetJsResult(napi_env env, napi_value &result); + +protected: + int32_t ret_; +}; + +class WfdSinkConfigJsResult : public WfdJsResult { +public: + WfdSinkConfigJsResult(SinkConfig &config) : WfdJsResult(0), config_(config) {} + ~WfdSinkConfigJsResult() = default; + + napi_status GetJsResult(napi_env env, napi_value &result) override; + +private: + SinkConfig config_; +}; + +class WfdSinkErrorJsResult : public WfdJsResult { +public: + WfdSinkErrorJsResult(int32_t code, std::string msg, std::string deviceId) + : WfdJsResult(0), code_(code), msg_(std::move(msg)), deviceId_(std::move(deviceId)) {} + ~WfdSinkErrorJsResult() = default; + + napi_status GetJsResult(napi_env env, napi_value &result) override; + +private: + int32_t code_; + std::string msg_; + std::string deviceId_; +}; + +class WfdSinkConnectionJsResult : public WfdJsResult { +public: + WfdSinkConnectionJsResult(ConnectionInfo info) : WfdJsResult(0), info_(info) {} + ~WfdSinkConnectionJsResult() = default; + + napi_status GetJsResult(napi_env env, napi_value &result) override; + +private: + ConnectionInfo info_; +}; + +class WfdSinkAccelerationJsResult : public WfdJsResult { +public: + WfdSinkAccelerationJsResult(std::string surfaceId) : WfdJsResult(0), surfaceId_(surfaceId) {} + ~WfdSinkAccelerationJsResult() = default; + + napi_status GetJsResult(napi_env env, napi_value &result) override; + +private: + std::string surfaceId_; +}; + +} // namespace Sharing +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/wfd/include/wfd_napi_sink.h b/interfaces/kits/js/wfd/include/wfd_napi_sink.h new file mode 100755 index 0000000..897a140 --- /dev/null +++ b/interfaces/kits/js/wfd/include/wfd_napi_sink.h @@ -0,0 +1,64 @@ +#ifndef OHOS_SHARING_WFD_NAPI_SINK_H +#define OHOS_SHARING_WFD_NAPI_SINK_H + +#include +#include +#include +#include "interaction/device_kit/dm_kit.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "wfd_callback_napi.h" +#include "wfd_enum_napi.h" +#include "wfd_impl.h" +#include "wfd_js_result.h" + +namespace OHOS { +namespace Sharing { + +class WfdSinkNapi { +public: + WfdSinkNapi(); + ~WfdSinkNapi(); + + static napi_value Init(napi_env env, napi_value exports); + + void CancelCallbackReference(); + static napi_value SetDiscoverable(napi_env env, napi_callback_info info); + +private: + static napi_value CreateSink(napi_env env, napi_callback_info info); + static napi_value Constructor(napi_env env, napi_callback_info info); + + static napi_value Stop(napi_env env, napi_callback_info info); + static napi_value Play(napi_env env, napi_callback_info info); + + static napi_value On(napi_env env, napi_callback_info info); + static napi_value Release(napi_env env, napi_callback_info info); + + static void AsyncWork(napi_env env, void *data); + static void CompleteCallback(napi_env env, napi_status status, void *data); + + static napi_status CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal); + +private: + static napi_ref constructor_; + + std::string localKey_; + std::string bundleName_; + std::string abilityName_; + + std::mutex refMutex_; + std::mutex devicesMutex_; + + std::shared_ptr nativeWfdSink_ = nullptr; + std::shared_ptr jsCallback_ = nullptr; + + std::unordered_set deviceIds_; + std::unordered_map> refMap_; + + napi_env env_ = nullptr; +}; + +} // namespace Sharing +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/wfd/include/wfd_napi_source.h b/interfaces/kits/js/wfd/include/wfd_napi_source.h new file mode 100755 index 0000000..966550c --- /dev/null +++ b/interfaces/kits/js/wfd/include/wfd_napi_source.h @@ -0,0 +1,59 @@ +#ifndef OHOS_SHARING_WFD_NAPI_SOURCE_H +#define OHOS_SHARING_WFD_NAPI_SOURCE_H + +#include +#include +#include +#include "interaction/device_kit/dm_kit.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "wfd_callback_napi.h" +#include "wfd_enum_napi.h" +#include "wfd_impl.h" +#include "wfd_js_result.h" + +namespace OHOS { +namespace Sharing { + +class WfdSourceNapi { +public: + WfdSourceNapi(); + ~WfdSourceNapi(); + + static napi_value Init(napi_env env, napi_value exports); + + void CancelCallbackReference(); + static napi_value StartDiscovery(napi_env env, napi_callback_info info); + static napi_value StopDiscovery(napi_env env, napi_callback_info info); + +private: + static napi_value CreateSource(napi_env env, napi_callback_info info); + static napi_value Constructor(napi_env env, napi_callback_info info); + + static void AsyncWork(napi_env env, void *data); + static void CompleteCallback(napi_env env, napi_status status, void *data); + + static napi_status CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal); + +private: + static napi_ref constructor_; + + std::string localKey_; + std::string bundleName_; + std::string abilityName_; + + std::mutex refMutex_; + std::mutex devicesMutex_; + + std::shared_ptr nativeWfdSource_ = nullptr; + std::shared_ptr jsCallback_ = nullptr; + + std::unordered_set deviceIds_; + std::unordered_map> refMap_; + + napi_env env_ = nullptr; +}; + +} // namespace Sharing +} // namespace OHOS +#endif \ No newline at end of file -- Gitee From ccfc19055189d6db31fb840ae5db27a240d276d0 Mon Sep 17 00:00:00 2001 From: huruitao Date: Fri, 15 Nov 2024 17:36:07 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huruitao --- .../kitsimpl/js/wfd/native_module_ohos_wfd.cpp | 15 +++++++++++++++ frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp | 15 +++++++++++++++ frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp | 16 +++++++++++++++- .../kits/js/wfd/include/native_module_ohos_wfd.h | 15 +++++++++++++++ .../kits/js/wfd/include/wfd_callback_napi.h | 15 +++++++++++++++ interfaces/kits/js/wfd/include/wfd_enum_napi.h | 15 +++++++++++++++ interfaces/kits/js/wfd/include/wfd_js_result.h | 15 +++++++++++++++ interfaces/kits/js/wfd/include/wfd_napi_sink.h | 15 +++++++++++++++ interfaces/kits/js/wfd/include/wfd_napi_source.h | 15 +++++++++++++++ 9 files changed, 135 insertions(+), 1 deletion(-) diff --git a/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp b/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp index 6e167d6..df2d714 100755 --- a/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp +++ b/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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. + */ + #include "native_module_ohos_wfd.h" namespace OHOS { diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp index f48a767..b7adf03 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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. + */ + #include "wfd_napi.h" #include "ability_manager/include/ability_manager_client.h" #include "common/sharing_log.h" diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index 6005fc7..70fdcf5 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -1,4 +1,18 @@ -// wfd_source_napi.cpp +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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. + */ + #include "wfd_source_napi.h" #include "ability_manager/include/ability_manager_client.h" #include "common/sharing_log.h" diff --git a/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h b/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h index 45ae284..348b09f 100755 --- a/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h +++ b/interfaces/kits/js/wfd/include/native_module_ohos_wfd.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_NATIVE_MODULE_OHOS_SHARINGWFD_H #define OHOS_SHARING_NATIVE_MODULE_OHOS_SHARINGWFD_H diff --git a/interfaces/kits/js/wfd/include/wfd_callback_napi.h b/interfaces/kits/js/wfd/include/wfd_callback_napi.h index 7758f05..12768ff 100755 --- a/interfaces/kits/js/wfd/include/wfd_callback_napi.h +++ b/interfaces/kits/js/wfd/include/wfd_callback_napi.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_WFD_CALLBACK_NAPI_H #define OHOS_SHARING_WFD_CALLBACK_NAPI_H diff --git a/interfaces/kits/js/wfd/include/wfd_enum_napi.h b/interfaces/kits/js/wfd/include/wfd_enum_napi.h index e5053dc..e827227 100755 --- a/interfaces/kits/js/wfd/include/wfd_enum_napi.h +++ b/interfaces/kits/js/wfd/include/wfd_enum_napi.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_WFD_ENUM_NAPI_H #define OHOS_SHARING_WFD_ENUM_NAPI_H diff --git a/interfaces/kits/js/wfd/include/wfd_js_result.h b/interfaces/kits/js/wfd/include/wfd_js_result.h index 2145e5d..0cbd712 100755 --- a/interfaces/kits/js/wfd/include/wfd_js_result.h +++ b/interfaces/kits/js/wfd/include/wfd_js_result.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_WFD_JS_RESULT_H #define OHOS_SHARING_WFD_JS_RESULT_H diff --git a/interfaces/kits/js/wfd/include/wfd_napi_sink.h b/interfaces/kits/js/wfd/include/wfd_napi_sink.h index 897a140..1b8ff91 100755 --- a/interfaces/kits/js/wfd/include/wfd_napi_sink.h +++ b/interfaces/kits/js/wfd/include/wfd_napi_sink.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_WFD_NAPI_SINK_H #define OHOS_SHARING_WFD_NAPI_SINK_H diff --git a/interfaces/kits/js/wfd/include/wfd_napi_source.h b/interfaces/kits/js/wfd/include/wfd_napi_source.h index 966550c..5a131ea 100755 --- a/interfaces/kits/js/wfd/include/wfd_napi_source.h +++ b/interfaces/kits/js/wfd/include/wfd_napi_source.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_WFD_NAPI_SOURCE_H #define OHOS_SHARING_WFD_NAPI_SOURCE_H -- Gitee From 834f84ee093aae6016feb4e6dbc223ca71259c2b Mon Sep 17 00:00:00 2001 From: huruitao Date: Fri, 15 Nov 2024 17:54:01 +0800 Subject: [PATCH 03/12] codecheck build.gn Signed-off-by: huruitao --- interfaces/kits/js/wfd/BUILD.gn | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/wfd/BUILD.gn b/interfaces/kits/js/wfd/BUILD.gn index 7c1a112..d6cc575 100755 --- a/interfaces/kits/js/wfd/BUILD.gn +++ b/interfaces/kits/js/wfd/BUILD.gn @@ -39,7 +39,7 @@ ohos_shared_library("sharingwfd_napi") { cfi_cross_dso = true debug = false } - + include_dirs = [ "$SHARING_ROOT_DIR", "$SHARING_ROOT_DIR/services/utils", @@ -67,11 +67,11 @@ ohos_shared_library("sharingwfd_napi") { ] deps = [ - "$SHARING_ROOT_DIR/services/utils:sharing_utils", - "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd:sharingwfd_client", - "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", + "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", + "$SHARING_ROOT_DIR/services/utils:sharing_utils", "//foundation/ability/ability_runtime/frameworks/native/ability/native:abilitykit_native", + "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", ] external_deps = [ @@ -80,8 +80,8 @@ ohos_shared_library("sharingwfd_napi") { "ability_runtime:app_manager", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", - "device_manager:devicemanagersdk", "c_utils:utils", + "device_manager:devicemanagersdk", "graphic_standard:surface", "hilog_native:libhilog", "ipc:ipc_core", -- Gitee From 55415efbb993607f0aeb2c7d9053a38835dabd1f Mon Sep 17 00:00:00 2001 From: huruitao Date: Mon, 18 Nov 2024 09:31:12 +0800 Subject: [PATCH 04/12] fix codecheck Signed-off-by: huruitao --- interfaces/kits/js/wfd/BUILD.gn | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/interfaces/kits/js/wfd/BUILD.gn b/interfaces/kits/js/wfd/BUILD.gn index d6cc575..954e073 100755 --- a/interfaces/kits/js/wfd/BUILD.gn +++ b/interfaces/kits/js/wfd/BUILD.gn @@ -20,12 +20,15 @@ config("sharing_service_config") { "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", "$SHARING_ROOT_DIR/services/interaction/ipc_codec", "$SHARING_ROOT_DIR/services/interaction/device_kit", - "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + + #"//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", - "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include/", - "//foundation/communication/ipc/ipc/native/c/ipc/include", + + #"//foundation/communication/ipc/interfaces/innerkits/ipc_core/include/", + #"//foundation/communication/ipc/ipc/native/c/ipc/include", "$SHARING_ROOT_DIR/services/extend/magic_enum", - "//utils/native/base/include", + + #"//utils/native/base/include", ] cflags = [ "-Wno-c++20-extensions" ] @@ -47,13 +50,14 @@ ohos_shared_library("sharingwfd_napi") { "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", "//third_party/node/src", - "//foundation/ace/napi/interfaces/kits", - "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", - "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", - "//foundation/ability/ability_runtime/interfaces/inner_api", - "//foundation/ability/ability_runtime/interfaces/kits/native", - "//foundation/ability/ability_base/interfaces/inner_api", - "//foundation/ability/ability_base/interfaces/kits/native", + + #"//foundation/ace/napi/interfaces/kits", + #"//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + #"//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + #"//foundation/ability/ability_runtime/interfaces/inner_api", + #"//foundation/ability/ability_runtime/interfaces/kits/native", + #"//foundation/ability/ability_base/interfaces/inner_api", + #"//foundation/ability/ability_base/interfaces/kits/native", ] sources = [ @@ -70,8 +74,9 @@ ohos_shared_library("sharingwfd_napi") { "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd:sharingwfd_client", "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", "$SHARING_ROOT_DIR/services/utils:sharing_utils", - "//foundation/ability/ability_runtime/frameworks/native/ability/native:abilitykit_native", - "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", + + #"//foundation/ability/ability_runtime/frameworks/native/ability/native:abilitykit_native", + #"//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", ] external_deps = [ -- Gitee From f65ddc973c405b8d500a1c8fb8a65f2113a38a2f Mon Sep 17 00:00:00 2001 From: huruitao Date: Mon, 18 Nov 2024 09:55:18 +0800 Subject: [PATCH 05/12] fix build.gn Signed-off-by: huruitao --- interfaces/kits/js/wfd/BUILD.gn | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/interfaces/kits/js/wfd/BUILD.gn b/interfaces/kits/js/wfd/BUILD.gn index 954e073..df9a5c1 100755 --- a/interfaces/kits/js/wfd/BUILD.gn +++ b/interfaces/kits/js/wfd/BUILD.gn @@ -20,15 +20,8 @@ config("sharing_service_config") { "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", "$SHARING_ROOT_DIR/services/interaction/ipc_codec", "$SHARING_ROOT_DIR/services/interaction/device_kit", - - #"//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", - - #"//foundation/communication/ipc/interfaces/innerkits/ipc_core/include/", - #"//foundation/communication/ipc/ipc/native/c/ipc/include", "$SHARING_ROOT_DIR/services/extend/magic_enum", - - #"//utils/native/base/include", ] cflags = [ "-Wno-c++20-extensions" ] @@ -50,14 +43,6 @@ ohos_shared_library("sharingwfd_napi") { "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", "//third_party/node/src", - - #"//foundation/ace/napi/interfaces/kits", - #"//base/hiviewdfx/hilog/interfaces/native/innerkits/include", - #"//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", - #"//foundation/ability/ability_runtime/interfaces/inner_api", - #"//foundation/ability/ability_runtime/interfaces/kits/native", - #"//foundation/ability/ability_base/interfaces/inner_api", - #"//foundation/ability/ability_base/interfaces/kits/native", ] sources = [ @@ -74,9 +59,6 @@ ohos_shared_library("sharingwfd_napi") { "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd:sharingwfd_client", "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", "$SHARING_ROOT_DIR/services/utils:sharing_utils", - - #"//foundation/ability/ability_runtime/frameworks/native/ability/native:abilitykit_native", - #"//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", ] external_deps = [ -- Gitee From 7732755d7c911753efe4b76ba58b672e08bf2c6d Mon Sep 17 00:00:00 2001 From: huruitao Date: Mon, 18 Nov 2024 10:17:13 +0800 Subject: [PATCH 06/12] fix build.gn Signed-off-by: huruitao --- interfaces/kits/js/wfd/BUILD.gn | 160 ++++++++++++++++---------------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/interfaces/kits/js/wfd/BUILD.gn b/interfaces/kits/js/wfd/BUILD.gn index df9a5c1..012b710 100755 --- a/interfaces/kits/js/wfd/BUILD.gn +++ b/interfaces/kits/js/wfd/BUILD.gn @@ -1,80 +1,80 @@ -# Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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. - -import("//build/ohos.gni") -import("//foundation/CastEngine/castengine_wifi_display/config.gni") - -config("sharing_service_config") { - include_dirs = [ - "$SHARING_ROOT_DIR/services", - "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", - "$SHARING_ROOT_DIR/services/interaction/ipc_codec", - "$SHARING_ROOT_DIR/services/interaction/device_kit", - "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", - "$SHARING_ROOT_DIR/services/extend/magic_enum", - ] - - cflags = [ "-Wno-c++20-extensions" ] -} - -ohos_shared_library("sharingwfd_napi") { - install_enable = true - - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false - } - - include_dirs = [ - "$SHARING_ROOT_DIR", - "$SHARING_ROOT_DIR/services/utils", - "$SHARING_ROOT_DIR/interfaces/kits/js/wfd/include", - "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", - "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", - "//third_party/node/src", - ] - - sources = [ - "$SHARING_ROOT_DIR/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp", - "$SHARING_ROOT_DIR/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp", - ] - - public_configs = [ - ":sharing_service_config", - "$SHARING_ROOT_DIR/tests:coverage_flags", - ] - - deps = [ - "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd:sharingwfd_client", - "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", - "$SHARING_ROOT_DIR/services/utils:sharing_utils", - ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "ability_runtime:app_manager", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "device_manager:devicemanagersdk", - "graphic_standard:surface", - "hilog_native:libhilog", - "ipc:ipc_core", - "napi:ace_napi", - ] - - subsystem_name = "castplus" - part_name = "sharing_framework" -} +# Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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. + +import("//build/ohos.gni") +import("//foundation/CastEngine/castengine_wifi_display/config.gni") + +config("sharing_service_config") { + include_dirs = [ + "$SHARING_ROOT_DIR/services", + "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", + "$SHARING_ROOT_DIR/services/interaction/ipc_codec", + "$SHARING_ROOT_DIR/services/interaction/device_kit", + "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", + "$SHARING_ROOT_DIR/services/extend/magic_enum", + ] + + cflags = [ "-Wno-c++20-extensions" ] +} + +ohos_shared_library("sharingwfd_napi") { + install_enable = true + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "$SHARING_ROOT_DIR", + "$SHARING_ROOT_DIR/services/utils", + "$SHARING_ROOT_DIR/interfaces/kits/js/wfd/include", + "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd/include", + "$SHARING_ROOT_DIR/frameworks/innerkitsimpl/native/wfd", + "//third_party/node/src", + ] + + sources = [ + "$SHARING_ROOT_DIR/frameworks/kitsimpl/js/wfd/native_module_ohos_wfd.cpp", + "$SHARING_ROOT_DIR/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp", + ] + + public_configs = [ + ":sharing_service_config", + "$SHARING_ROOT_DIR/tests:coverage_flags", + ] + + deps = [ + "$SHARING_ROOT_DIR/interfaces/innerkits/native/wfd:sharingwfd_client", + "$SHARING_ROOT_DIR/services/interaction/device_kit:dmkit", + "$SHARING_ROOT_DIR/services/utils:sharing_utils", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:app_manager", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "device_manager:devicemanagersdk", + "graphic_standard:surface", + "hilog_native:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + ] + + subsystem_name = "castplus" + part_name = "sharing_framework" +} -- Gitee From b7222512b6d000ceeabfb2b22b5e61a0eed003e3 Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 19 Nov 2024 14:17:23 +0800 Subject: [PATCH 07/12] fix codecheck Signed-off-by: huruitao --- frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp | 1 - .../kitsimpl/js/wfd/wfd_napi_source.cpp | 47 ++++++++++--------- .../kits/js/wfd/include/wfd_enum_napi.h | 2 +- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp index b7adf03..d9066f6 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp @@ -547,7 +547,6 @@ napi_status WfdSinkNapi::CreateError(napi_env env, int32_t errCode, const std::s SHARING_LOGE("set error name property fail."); return napi_invalid_arg; } - return napi_ok; } diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index 70fdcf5..d5512ab 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -41,12 +41,13 @@ WfdSourceNapi::~WfdSourceNapi() { nativeWfdSource_.reset(); } -napi_value WfdSourceNapi::Init(napi_env env, napi_value exports) { +napi_value WfdSourceNapi::Init(napi_env env, napi_value exports) +{ SHARING_LOGD("trace."); napi_property_descriptor staticProperty[] = {DECLARE_NAPI_STATIC_FUNCTION("createSource", CreateSource)}; napi_property_descriptor properties[] = {DECLARE_NAPI_FUNCTION("startDiscovery", StartDiscovery), - DECLARE_NAPI_FUNCTION("stopDiscovery", StopDiscovery)}; + DECLARE_NAPI_FUNCTION("stopDiscovery", StopDiscovery)}; napi_value constructor = nullptr; napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, @@ -67,7 +68,8 @@ napi_value WfdSourceNapi::Init(napi_env env, napi_value exports) { return exports; } -void WfdSourceNapi::CancelCallbackReference() { +void WfdSourceNapi::CancelCallbackReference() +{ SHARING_LOGD("trace."); std::lock_guard lock(refMutex_); if (jsCallback_ != nullptr) { @@ -78,7 +80,8 @@ void WfdSourceNapi::CancelCallbackReference() { refMap_.clear(); } -napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) { +napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) +{ SHARING_LOGD("trace."); napi_status status; napi_value result = nullptr; @@ -142,7 +145,7 @@ napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) { jsCast->jsCallback_->SetWfdSourceNapi(jsCast); jsCast->nativeWfdSource_->SetListener(jsCast->jsCallback_); -status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, nullptr, nullptr); + status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, nullptr, nullptr); if (status != napi_ok) { napi_get_undefined(env, &result); delete jsCast; @@ -154,7 +157,8 @@ status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, null return jsThis; } -napi_value WfdSourceNapi::CreateSource(napi_env env, napi_callback_info info) { +napi_value WfdSourceNapi::CreateSource(napi_env env, napi_callback_info info) +{ SHARING_LOGD("trace."); napi_value result = nullptr; napi_value constructor = nullptr; @@ -177,7 +181,8 @@ napi_value WfdSourceNapi::CreateSource(napi_env env, napi_callback_info info) { return result; } -napi_value WfdSourceNapi::StartDiscovery(napi_env env, napi_callback_info info) { +napi_value WfdSourceNapi::StartDiscovery(napi_env env, napi_callback_info info) +{ SHARING_LOGD("trace."); napi_value result = nullptr; napi_value jsThis = nullptr; @@ -212,9 +217,9 @@ napi_value WfdSourceNapi::StartDiscovery(napi_env env, napi_callback_info info) napi_value resource = nullptr; napi_create_string_utf8(env, "StartDiscovery", NAPI_AUTO_LENGTH, &resource); - - NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, WfdSourceNapi::CompleteCallback, - static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, + WfdSourceNapi::CompleteCallback, static_cast(asyncContext.get()), + &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); asyncContext.release(); @@ -222,7 +227,8 @@ napi_value WfdSourceNapi::StartDiscovery(napi_env env, napi_callback_info info) return result; } -napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) { +napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) +{ SHARING_LOGD("trace."); napi_value result = nullptr; napi_value jsThis = nullptr; @@ -257,8 +263,9 @@ napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) { napi_value resource = nullptr; napi_create_string_utf8(env, "StopDiscovery", NAPI_AUTO_LENGTH, &resource); - NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, WfdSourceNapi::CompleteCallback, - static_cast(asyncContext.get()), &asyncContext->work)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, + WfdSourceNapi::CompleteCallback,static_cast(asyncContext.get()), + &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); asyncContext.release(); @@ -266,7 +273,8 @@ napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) { return result; } -void WfdSourceNapi::AsyncWork(napi_env env, void *data) { +void WfdSourceNapi::AsyncWork(napi_env env, void *data) +{ SHARING_LOGD("trace."); auto asyncContext = reinterpret_cast(data); if (asyncContext == nullptr) { @@ -317,7 +325,8 @@ void WfdSourceNapi::AsyncWork(napi_env env, void *data) { } } -void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *data) { +void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *data) +{ SHARING_LOGD("trace."); auto asyncContext = reinterpret_cast(data); SHARING_CHECK_AND_RETURN_LOG(asyncContext != nullptr, "asyncContext is nullptr!"); @@ -328,7 +337,6 @@ void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *dat napi_value result = nullptr; napi_get_undefined(env, &result); - napi_value args[2] = {nullptr}; napi_get_undefined(env, &args[0]); napi_get_undefined(env, &args[1]); @@ -365,16 +373,13 @@ void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *dat napi_value callback = nullptr; napi_get_reference_value(env, asyncContext->callbackRef, &callback); SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); - constexpr size_t argCount = 2; napi_value retVal; napi_get_undefined(env, &retVal); napi_call_function(env, nullptr, callback, argCount, args, &retVal); napi_delete_reference(env, asyncContext->callbackRef); } - napi_delete_async_work(env, asyncContext->work); - if (asyncContext) { delete asyncContext; asyncContext = nullptr; @@ -382,7 +387,8 @@ void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *dat SHARING_LOGD("success."); } -napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal) { +napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal) +{ SHARING_LOGD("trace."); napi_get_undefined(env, &errVal); @@ -451,7 +457,6 @@ napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std: SHARING_LOGE("set error name property fail."); return napi_invalid_arg; } - return napi_ok; } diff --git a/interfaces/kits/js/wfd/include/wfd_enum_napi.h b/interfaces/kits/js/wfd/include/wfd_enum_napi.h index e827227..29cb422 100755 --- a/interfaces/kits/js/wfd/include/wfd_enum_napi.h +++ b/interfaces/kits/js/wfd/include/wfd_enum_napi.h @@ -41,7 +41,7 @@ static const std::unordered_map mapDeviceState = { {"READY", DeviceState::READY}, {"PLAYING", DeviceState::PLAYING}, {"PAUSED", DeviceState::PAUSED}, - {"STOPPED", DeviceState::STOPPED},}; + {"STOPPED", DeviceState::STOPPED}}; constexpr int32_t CommonErrorCode = 18800001; static const std::unordered_map mapErrorCode = {{"ERR_OK", 0}, -- Gitee From 378131b52aa7bae2e054348c802e2265d543e14e Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 19 Nov 2024 15:38:16 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huruitao --- frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp | 265 +++++++++--------- .../kitsimpl/js/wfd/wfd_napi_source.cpp | 188 ++++++------- 2 files changed, 220 insertions(+), 233 deletions(-) diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp index d9066f6..ec23c7d 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp @@ -51,7 +51,7 @@ napi_value WfdSinkNapi::Init(napi_env env, napi_value exports) napi_property_descriptor properties[] = {DECLARE_NAPI_FUNCTION("start", Start), DECLARE_NAPI_FUNCTION("setDiscoverable", SetDiscoverable), - DECLARE_NAPI_FUNCTION("stop", Stop), + DECLARE_NAPI_FUNCTION("stop", Stop)} napi_value constructor = nullptr; napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, @@ -91,15 +91,6 @@ napi_value WfdSinkNapi::Constructor(napi_env env, napi_callback_info info) napi_value result = nullptr; napi_value jsThis = nullptr; - auto finalize = [](napi_env env, void *data, void *hint) { - SHARING_LOGD("Destructor in."); - auto *wfdSinkNapi = reinterpret_cast(data); - wfdSinkNapi->CancelCallbackReference(); - - delete wfdSinkNapi; - SHARING_LOGD("Destructor out."); - }; - status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); if (status != napi_ok || jsThis == nullptr) { napi_get_undefined(env, &result); @@ -111,7 +102,22 @@ napi_value WfdSinkNapi::Constructor(napi_env env, napi_callback_info info) SHARING_CHECK_AND_RETURN_RET_LOG(jsCast != nullptr, result, "No memory."); jsCast->env_ = env; + InitializeWfdSink(jsCast); + + status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), FinalizeCallback, nullptr, nullptr); + if (status != napi_ok) { + napi_get_undefined(env, &result); + delete jsCast; + SHARING_LOGE("Fail to wrapping js to native napi."); + return result; + } + + SHARING_LOGD("success."); + return jsThis; +} +void InitializeWfdSink(WfdSinkNapi *jsCast) +{ std::vector outInfo; AAFwk::AbilityManagerClient::GetInstance()->GetAbilityRunningInfos(outInfo); @@ -143,22 +149,21 @@ napi_value WfdSinkNapi::Constructor(napi_env env, napi_callback_info info) parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); jsCast->nativeWfdSink_ = WfdSinkFactory::CreateSink(0, jsCast->localKey_); - SHARING_CHECK_AND_RETURN_RET_LOG(jsCast->nativeWfdSink_ != nullptr, result, "failed to WfdSinkImpl."); + SHARING_CHECK_AND_RETURN_LOG(jsCast->nativeWfdSink_ != nullptr, "failed to WfdSinkImpl."); - jsCast->jsCallback_ = std::make_shared(env); + jsCast->jsCallback_ = std::make_shared(jsCast->env_); jsCast->jsCallback_->SetWfdSinkNapi(jsCast); jsCast->nativeWfdSink_->SetListener(jsCast->jsCallback_); +} - status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, nullptr, nullptr); - if (status != napi_ok) { - napi_get_undefined(env, &result); - delete jsCast; - SHARING_LOGE("Fail to wrapping js to native napi."); - return result; - } +void FinalizeCallback(napi_env env, void *data, void * /*hint*/) +{ + SHARING_LOGD("Destructor in."); + auto *wfdSinkNapi = reinterpret_cast(data); + wfdSinkNapi->CancelCallbackReference(); - SHARING_LOGD("success."); - return jsThis; + delete wfdSinkNapi; + SHARING_LOGD("Destructor out."); } napi_value WfdSinkNapi::CreateSink(napi_env env, napi_callback_info info) @@ -226,11 +231,9 @@ napi_value WfdSinkNapi::SetDiscoverable(napi_env env, napi_callback_info info) napi_value resource = nullptr; napi_create_string_utf8(env, "SetDiscoverable", NAPI_AUTO_LENGTH, &resource); - NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSinkNapi::AsyncWork, WfdSinkNapi::CompleteCallback, static_cast(asyncContext.get()), &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); - asyncContext.release(); SHARING_LOGD("success."); return result; @@ -347,68 +350,84 @@ void WfdSinkNapi::AsyncWork(napi_env env, void *data) return; } - auto jsCast = asyncContext->napi; - auto nativeWfdSink = jsCast->nativeWfdSink_; - int32_t ret = 0; - switch (asyncContext->asyncWorkType) { - case AsyncWorkType::ASYNC_WORK_SET_DISCOVERABLE: { - if (!asyncContext->errFlag) { - bool isDiscoverable = *static_cast(asyncContext->data); - if (isDiscoverable) { - ret = nativeWfdSink->Start(); - } else { - ret = nativeWfdSink->Stop(); - } - if (ret < 0) { - SHARING_LOGE("nativeWfdSink->SetDiscoverable error, %{public}d.", ret); - asyncContext->SignError(CommonErrorCode, "native instance SetDiscoverable error"); - return; - } - } else { - ret = asyncContext->errCode; - } - - asyncContext->JsResult = std::make_unique(ret); + case AsyncWorkType::ASYNC_WORK_SET_DISCOVERABLE: + HandleSetDiscoverable(asyncContext); break; - } - case AsyncWorkType::ASYNC_WORK_START: { - if (!asyncContext->errFlag) { - ret = nativeWfdSink->Start(); - if (ret < 0) { - SHARING_LOGE("nativeWfdSink->Start error, %{public}d.", ret); - asyncContext->SignError(CommonErrorCode, "native instance Start error"); - return; - } - } else { - ret = asyncContext->errCode; - } - - asyncContext->JsResult = std::make_unique(ret); + case AsyncWorkType::ASYNC_WORK_START: + HandleStart(asyncContext); break; - } - case AsyncWorkType::ASYNC_WORK_STOP: { - if (!asyncContext->errFlag) { - ret = nativeWfdSink->Stop(); - if (ret < 0) { - SHARING_LOGE("nativeWfdSink->Stop error, %{public}d.", ret); - asyncContext->SignError(CommonErrorCode, "native instance Stop error"); - return; - } - } else { - ret = asyncContext->errCode; - } - - asyncContext->JsResult = std::make_unique(ret); + case AsyncWorkType::ASYNC_WORK_STOP: + HandleStop(asyncContext); break; - } default: SHARING_LOGE("error unknown operation (%{public}d).", (int32_t)asyncContext->asyncWorkType); break; } SHARING_LOGD("workType: %{public}d.", asyncContext->asyncWorkType); - return; +} + +void HandleSetDiscoverable(WfdSinkAsyncContext *asyncContext) +{ + auto nativeWfdSink = asyncContext->napi->nativeWfdSink_; + int32_t ret = 0; + + if (!asyncContext->errFlag) { + bool isDiscoverable = *static_cast(asyncContext->data); + if (isDiscoverable) { + ret = nativeWfdSink->Start(); + } else { + ret = nativeWfdSink->Stop(); + } + if (ret < 0) { + SHARING_LOGE("nativeWfdSink->SetDiscoverable error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance SetDiscoverable error"); + return; + } + } else { + ret = asyncContext->errCode; + } + + asyncContext->JsResult = std::make_unique(ret); +} + +void HandleStart(WfdSinkAsyncContext *asyncContext) +{ + auto nativeWfdSink = asyncContext->napi->nativeWfdSink_; + int32_t ret = 0; + + if (!asyncContext->errFlag) { + ret = nativeWfdSink->Start(); + if (ret < 0) { + SHARING_LOGE("nativeWfdSink->Start error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance Start error"); + return; + } + } else { + ret = asyncContext->errCode; + } + + asyncContext->JsResult = std::make_unique(ret); +} + +void HandleStop(WfdSinkAsyncContext *asyncContext) +{ + auto nativeWfdSink = asyncContext->napi->nativeWfdSink_; + int32_t ret = 0; + + if (!asyncContext->errFlag) { + ret = nativeWfdSink->Stop(); + if (ret < 0) { + SHARING_LOGE("nativeWfdSink->Stop error, %{public}d.", ret); + asyncContext->SignError(CommonErrorCode, "native instance Stop error"); + return; + } + } else { + ret = asyncContext->errCode; + } + + asyncContext->JsResult = std::make_unique(ret); } void WfdSinkNapi::CompleteCallback(napi_env env, napi_status status, void *data) @@ -420,20 +439,16 @@ void WfdSinkNapi::CompleteCallback(napi_env env, napi_status status, void *data) if (status != napi_ok) { asyncContext->SignError(CommonErrorCode, "napi_create_async_work status != napi_ok"); } - - napi_value result = nullptr; - napi_get_undefined(env, &result); - - napi_value args[2] = {nullptr}; - napi_get_undefined(env, &args[0]); - napi_get_undefined(env, &args[1]); + napi_value undefined; + napi_get_undefined(env, &undefined); + napi_value result = undefined; + napi_value args[2] = {undefined, undefined}; if (asyncContext->errFlag) { SHARING_LOGE("async callback failed."); CreateError(env, asyncContext->errCode, asyncContext->errMessage, result); args[0] = result; } else { - SHARING_LOGD("async callback out."); if (asyncContext->JsResult != nullptr) { auto res = asyncContext->JsResult->GetJsResult(env, result); if (res == napi_ok) { @@ -449,22 +464,16 @@ void WfdSinkNapi::CompleteCallback(napi_env env, napi_status status, void *data) if (asyncContext->deferred) { if (asyncContext->errFlag) { - SHARING_LOGD("napi_reject_deferred."); napi_reject_deferred(env, asyncContext->deferred, args[0]); } else { - SHARING_LOGD("napi_resolve_deferred."); napi_resolve_deferred(env, asyncContext->deferred, args[1]); } } else { - SHARING_LOGD("napi_call_function callback."); napi_value callback = nullptr; napi_get_reference_value(env, asyncContext->callbackRef, &callback); SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); - - constexpr size_t argCount = 2; - napi_value retVal; - napi_get_undefined(env, &retVal); - napi_call_function(env, nullptr, callback, argCount, args, &retVal); + napi_value retVal = undefined; + napi_call_function(env, nullptr, callback, 2, args, &retVal); napi_delete_reference(env, asyncContext->callbackRef); } @@ -480,75 +489,71 @@ void WfdSinkNapi::CompleteCallback(napi_env env, napi_status status, void *data) napi_status WfdSinkNapi::CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal) { SHARING_LOGD("trace."); - napi_get_undefined(env, &errVal); - napi_value msgValStr = nullptr; - napi_status nstatus = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr); - if (nstatus != napi_ok || msgValStr == nullptr) { - SHARING_LOGE("create error message str fail."); + napi_status nstatus = CreateString(env, errMsg.c_str(), &msgValStr); + if (HandleError(env, "create error message str fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } nstatus = napi_create_error(env, nullptr, msgValStr, &errVal); - if (nstatus != napi_ok || errVal == nullptr) { - SHARING_LOGE("create error fail."); - return napi_invalid_arg; - } - - napi_value codeStr = nullptr; - nstatus = napi_create_string_utf8(env, "code", NAPI_AUTO_LENGTH, &codeStr); - if (nstatus != napi_ok || codeStr == nullptr) { - SHARING_LOGE("create code str fail."); + if (HandleError(env, "create error fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } napi_value errCodeVal = nullptr; nstatus = napi_create_int32(env, errCode, &errCodeVal); - if (nstatus != napi_ok || errCodeVal == nullptr) { - SHARING_LOGE("create error code number val fail."); + if (HandleError(env, "create error code number val fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = napi_set_property(env, errVal, codeStr, errCodeVal); - if (nstatus != napi_ok) { - SHARING_LOGE("set error code property fail."); + nstatus = SetProperty(env, errVal, kCodeKey, errCodeVal); + if (HandleError(env, "set error code property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - napi_value msgStr = nullptr; - nstatus = napi_create_string_utf8(env, "msg", NAPI_AUTO_LENGTH, &msgStr); - if (nstatus != napi_ok || msgStr == nullptr) { - SHARING_LOGE("create msg str fail."); + nstatus = SetProperty(env, errVal, kMsgKey, msgValStr); + if (HandleError(env, "set error msg property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = napi_set_property(env, errVal, msgStr, msgValStr); - if (nstatus != napi_ok) { - SHARING_LOGE("set error msg property fail."); + napi_value errNameVal = nullptr; + nstatus = CreateString(env, kErrorName, &errNameVal); + if (HandleError(env, "create BusinessError str fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - napi_value nameStr = nullptr; - nstatus = napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &nameStr); - if (nstatus != napi_ok || nameStr == nullptr) { - SHARING_LOGE("create name str fail."); + nstatus = SetProperty(env, errVal, kNameKey, errNameVal); + if (HandleError(env, "set error name property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } + SHARING_LOGD("success."); + return napi_ok; +} - napi_value errNameVal = nullptr; - nstatus = napi_create_string_utf8(env, "BusinessError", NAPI_AUTO_LENGTH, &errNameVal); - if (nstatus != napi_ok || errNameVal == nullptr) { - SHARING_LOGE("create BusinessError str fail."); - return napi_invalid_arg; +static napi_status CreateString(napi_env env, const char* str, napi_value* result){ + return napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, result); +} + +static napi_status SetProperty(napi_env env, napi_value object, const char* key, napi_value value) +{ + napi_value keyVal; + napi_status status = CreateString(env, key, &keyVal); + if (status != napi_ok) { + return status; } + return napi_set_property(env, object, keyVal, value); +} - nstatus = napi_set_property(env, errVal, nameStr, errNameVal); - if (nstatus != napi_ok) { - SHARING_LOGE("set error name property fail."); +static napi_status HandleError(napi_env env, const char* message, napi_status status, napi_value* errVal) +{ + if (status != napi_ok) { + SHARING_LOGE("%s: %d", message, status); + if (errVal != nullptr) { + *errVal = nullptr; + } return napi_invalid_arg; } return napi_ok; } - } // namespace Sharing } // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index d5512ab..9c1a554 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -30,12 +30,18 @@ const std::string CLASS_NAME = "WfdSourceImpl"; const int32_t ARGS_ONE = 1; const int32_t ARGS_TWO = 2; const int32_t STRING_MAX_SIZE = 255; +const char* kCodeKey = "code"; +const char* kMsgKey = "msg"; +const char* kNameKey = "name"; +const char* kErrorName = "BusinessError"; -WfdSourceNapi::WfdSourceNapi() { +WfdSourceNapi::WfdSourceNapi() +{ SHARING_LOGI("ctor %{public}p.", this); } -WfdSourceNapi::~WfdSourceNapi() { +WfdSourceNapi::~WfdSourceNapi() +{ SHARING_LOGI("dtor %{public}p.", this); CancelCallbackReference(); nativeWfdSource_.reset(); @@ -68,7 +74,7 @@ napi_value WfdSourceNapi::Init(napi_env env, napi_value exports) return exports; } -void WfdSourceNapi::CancelCallbackReference() +void WfdSourceNapi::CancelCallbackReference() { SHARING_LOGD("trace."); std::lock_guard lock(refMutex_); @@ -80,22 +86,12 @@ void WfdSourceNapi::CancelCallbackReference() refMap_.clear(); } -napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) -{ +napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) { SHARING_LOGD("trace."); napi_status status; napi_value result = nullptr; napi_value jsThis = nullptr; - auto finalize = [](napi_env env, void *data, void *hint) { - SHARING_LOGD("Destructor in."); - auto *wfdSourceNapi = reinterpret_cast(data); - wfdSourceNapi->CancelCallbackReference(); - - delete wfdSourceNapi; - SHARING_LOGD("Destructor out."); - }; - status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); if (status != napi_ok || jsThis == nullptr) { napi_get_undefined(env, &result); @@ -107,54 +103,56 @@ napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) SHARING_CHECK_AND_RETURN_RET_LOG(jsCast != nullptr, result, "No memory."); jsCast->env_ = env; + InitializeWfdSource(jsCast); + + status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), FinalizeCallback, nullptr, nullptr); + if (status != napi_ok) { + napi_get_undefined(env, &result); + delete jsCast; + SHARING_LOGE("Fail to wrapping js to native napi."); + return result; + } + + SHARING_LOGD("success."); + return jsThis; +} +void InitializeWfdSource(WfdSourceNapi *jsCast) { std::vector outInfo; AAFwk::AbilityManagerClient::GetInstance()->GetAbilityRunningInfos(outInfo); - if (outInfo.size() != 1) { - SHARING_LOGE("get ability size error: %{public}zu.", outInfo.size()); - for (auto &item : outInfo) { - SHARING_LOGW("get app, bundle: %{public}s, ability: %{public}s.", item.ability.GetBundleName().c_str(), - item.ability.GetAbilityName().c_str()); - } - + if (outInfo.empty() || outInfo.size() > 1) { + SHARING_LOGE("get ability size error: %zu.", outInfo.size()); jsCast->bundleName_ = BUNDLE_NAME; jsCast->abilityName_ = ABILITY_NAME; } else { jsCast->bundleName_ = outInfo[0].ability.GetBundleName(); jsCast->abilityName_ = outInfo[0].ability.GetAbilityName(); - SHARING_LOGI("get app, bundle: %{public}s, ability: %{public}s.", jsCast->bundleName_.c_str(), - jsCast->abilityName_.c_str()); + SHARING_LOGI("get app, bundle: %s, ability: %s.", jsCast->bundleName_.c_str(), jsCast->abilityName_.c_str()); } DmKit::InitDeviceManager(); - if (DmKit::GetTrustedDevicesInfo().size() > 0) { - for (auto &item : DmKit::GetTrustedDevicesInfo()) { - SHARING_LOGI("remote trusted device: %{public}s.", item.deviceId); - } + for (const auto &item : DmKit::GetTrustedDevicesInfo()) { + SHARING_LOGI("remote trusted device: %s.", item.deviceId); } RpcKeyParser parser; - jsCast->localKey_ = - parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); + jsCast->localKey_ = parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); jsCast->nativeWfdSource_ = WfdSourceFactory::CreateSource(0, jsCast->localKey_); - SHARING_CHECK_AND_RETURN_RET_LOG(jsCast->nativeWfdSource_ != nullptr, result, "failed to WfdSourceImpl."); + SHARING_CHECK_AND_RETURN_LOG(jsCast->nativeWfdSource_ != nullptr, "failed to WfdSourceImpl."); - jsCast->jsCallback_ = std::make_shared(env); + jsCast->jsCallback_ = std::make_shared(jsCast->env_); jsCast->jsCallback_->SetWfdSourceNapi(jsCast); jsCast->nativeWfdSource_->SetListener(jsCast->jsCallback_); +} - status = napi_wrap(env, jsThis, reinterpret_cast(jsCast), finalize, nullptr, nullptr); - if (status != napi_ok) { - napi_get_undefined(env, &result); - delete jsCast; - SHARING_LOGE("Fail to wrapping js to native napi."); - return result; - } - - SHARING_LOGD("success."); - return jsThis; +void FinalizeCallback(napi_env env, void *data, void * /*hint*/) { + SHARING_LOGD("Destructor in."); + auto *wfdSourceNapi = reinterpret_cast(data); + wfdSourceNapi->CancelCallbackReference(); + delete wfdSourceNapi; + SHARING_LOGD("Destructor out."); } napi_value WfdSourceNapi::CreateSource(napi_env env, napi_callback_info info) @@ -263,8 +261,8 @@ napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) napi_value resource = nullptr; napi_create_string_utf8(env, "StopDiscovery", NAPI_AUTO_LENGTH, &resource); - NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, - WfdSourceNapi::CompleteCallback,static_cast(asyncContext.get()), + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, + WfdSourceNapi::CompleteCallback,static_cast(asyncContext.get()), &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); @@ -281,7 +279,6 @@ void WfdSourceNapi::AsyncWork(napi_env env, void *data) SHARING_LOGE("WfdSourceAsyncContext is nullptr."); return; } - SHARING_LOGD("workType: %{public}d.", asyncContext->asyncWorkType); if (asyncContext->napi == nullptr || asyncContext->napi->nativeWfdSource_ == nullptr) { if (asyncContext->napi == nullptr) { @@ -328,25 +325,22 @@ void WfdSourceNapi::AsyncWork(napi_env env, void *data) void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *data) { SHARING_LOGD("trace."); - auto asyncContext = reinterpret_cast(data); + auto asyncContext = static_cast(data); SHARING_CHECK_AND_RETURN_LOG(asyncContext != nullptr, "asyncContext is nullptr!"); if (status != napi_ok) { asyncContext->SignError(CommonErrorCode, "napi_create_async_work status != napi_ok"); } - - napi_value result = nullptr; - napi_get_undefined(env, &result); - napi_value args[2] = {nullptr}; - napi_get_undefined(env, &args[0]); - napi_get_undefined(env, &args[1]); + napi_value undefined; + napi_get_undefined(env, &undefined); + napi_value result = undefined; + napi_value args[2] = {undefined, undefined}; if (asyncContext->errFlag) { SHARING_LOGE("async callback failed."); CreateError(env, asyncContext->errCode, asyncContext->errMessage, result); args[0] = result; } else { - SHARING_LOGD("async callback out."); if (asyncContext->JsResult != nullptr) { auto res = asyncContext->JsResult->GetJsResult(env, result); if (res == napi_ok) { @@ -362,103 +356,91 @@ void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *dat if (asyncContext->deferred) { if (asyncContext->errFlag) { - SHARING_LOGD("napi_reject_deferred."); napi_reject_deferred(env, asyncContext->deferred, args[0]); } else { - SHARING_LOGD("napi_resolve_deferred."); napi_resolve_deferred(env, asyncContext->deferred, args[1]); } } else { - SHARING_LOGD("napi_call_function callback."); - napi_value callback = nullptr; + napi_value callback; napi_get_reference_value(env, asyncContext->callbackRef, &callback); SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); - constexpr size_t argCount = 2; - napi_value retVal; - napi_get_undefined(env, &retVal); - napi_call_function(env, nullptr, callback, argCount, args, &retVal); + napi_value retVal = undefined; + napi_call_function(env, nullptr, callback, 2, args, &retVal); napi_delete_reference(env, asyncContext->callbackRef); } napi_delete_async_work(env, asyncContext->work); - if (asyncContext) { - delete asyncContext; - asyncContext = nullptr; - } + delete asyncContext; SHARING_LOGD("success."); } napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std::string &errMsg, napi_value &errVal) { SHARING_LOGD("trace."); - napi_get_undefined(env, &errVal); - napi_value msgValStr = nullptr; - napi_status nstatus = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr); - if (nstatus != napi_ok || msgValStr == nullptr) { - SHARING_LOGE("create error message str fail."); + napi_status nstatus = CreateString(env, errMsg.c_str(), &msgValStr); + if (HandleError(env, "create error message str fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } nstatus = napi_create_error(env, nullptr, msgValStr, &errVal); - if (nstatus != napi_ok || errVal == nullptr) { - SHARING_LOGE("create error fail."); - return napi_invalid_arg; - } - - napi_value codeStr = nullptr; - nstatus = napi_create_string_utf8(env, "code", NAPI_AUTO_LENGTH, &codeStr); - if (nstatus != napi_ok || codeStr == nullptr) { - SHARING_LOGE("create code str fail."); + if (HandleError(env, "create error fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } napi_value errCodeVal = nullptr; nstatus = napi_create_int32(env, errCode, &errCodeVal); - if (nstatus != napi_ok || errCodeVal == nullptr) { - SHARING_LOGE("create error code number val fail."); + if (HandleError(env, "create error code number val fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = napi_set_property(env, errVal, codeStr, errCodeVal); - if (nstatus != napi_ok) { - SHARING_LOGE("set error code property fail."); + nstatus = SetProperty(env, errVal, kCodeKey, errCodeVal); + if (HandleError(env, "set error code property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - napi_value msgStr = nullptr; - nstatus = napi_create_string_utf8(env, "msg", NAPI_AUTO_LENGTH, &msgStr); - if (nstatus != napi_ok || msgStr == nullptr) { - SHARING_LOGE("create msg str fail."); + nstatus = SetProperty(env, errVal, kMsgKey, msgValStr); + if (HandleError(env, "set error msg property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = napi_set_property(env, errVal, msgStr, msgValStr); - if (nstatus != napi_ok) { - SHARING_LOGE("set error msg property fail."); + napi_value errNameVal = nullptr; + nstatus = CreateString(env, kErrorName, &errNameVal); + if (HandleError(env, "create BusinessError str fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - napi_value nameStr = nullptr; - nstatus = napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &nameStr); - if (nstatus != napi_ok || nameStr == nullptr) { - SHARING_LOGE("create name str fail."); + nstatus = SetProperty(env, errVal, kNameKey, errNameVal); + if (HandleError(env, "set error name property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } + SHARING_LOGD("success."); + return napi_ok; +} - napi_value errNameVal = nullptr; - nstatus = napi_create_string_utf8(env, "BusinessError", NAPI_AUTO_LENGTH, &errNameVal); - if (nstatus != napi_ok || errNameVal == nullptr) { - SHARING_LOGE("create BusinessError str fail."); - return napi_invalid_arg; +static napi_status CreateString(napi_env env, const char* str, napi_value* result){ + return napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, result); +} + +static napi_status SetProperty(napi_env env, napi_value object, const char* key, napi_value value) +{ + napi_value keyVal; + napi_status status = CreateString(env, key, &keyVal); + if (status != napi_ok) { + return status; } + return napi_set_property(env, object, keyVal, value); +} - nstatus = napi_set_property(env, errVal, nameStr, errNameVal); - if (nstatus != napi_ok) { - SHARING_LOGE("set error name property fail."); +static napi_status HandleError(napi_env env, const char* message, napi_status status, napi_value* errVal) +{ + if (status != napi_ok) { + SHARING_LOGE("%s: %d", message, status); + if (errVal != nullptr) { + *errVal = nullptr; + } return napi_invalid_arg; } return napi_ok; } - } // namespace Sharing } // namespace OHOS \ No newline at end of file -- Gitee From 7acb1cdeb7ea09e3bc05e9e773ea8629f1db0035 Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 19 Nov 2024 16:00:22 +0800 Subject: [PATCH 09/12] fix codecheck Signed-off-by: huruitao --- frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp | 17 ++++++------ .../kitsimpl/js/wfd/wfd_napi_source.cpp | 26 ++++++++++++------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp index ec23c7d..f984c1a 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp @@ -31,6 +31,10 @@ const int32_t ARGS_TWO = 2; const int32_t ARGS_THREE = 3; const int32_t ARGS_FOUR = 4; const int32_t STRING_MAX_SIZE = 255; +const char* CODE "code"; +const char* MSG_KEY = "msg"; +const char* NAME_KEY = "name"; +const char* ERROR_NAME = "BusinessError"; WfdSinkNapi::WfdSinkNapi() { @@ -473,16 +477,12 @@ void WfdSinkNapi::CompleteCallback(napi_env env, napi_status status, void *data) napi_get_reference_value(env, asyncContext->callbackRef, &callback); SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); napi_value retVal = undefined; - napi_call_function(env, nullptr, callback, 2, args, &retVal); + constexpr size_t argCount = ARGS_TWO; + napi_call_function(env, nullptr, callback, argCount, args, &retVal); napi_delete_reference(env, asyncContext->callbackRef); } - napi_delete_async_work(env, asyncContext->work); - - if (asyncContext) { - delete asyncContext; - asyncContext = nullptr; - } + delete asyncContext; SHARING_LOGD("success."); } @@ -530,7 +530,8 @@ napi_status WfdSinkNapi::CreateError(napi_env env, int32_t errCode, const std::s return napi_ok; } -static napi_status CreateString(napi_env env, const char* str, napi_value* result){ +static napi_status CreateString(napi_env env, const char* str, napi_value* result) +{ return napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, result); } diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index 9c1a554..532aed1 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -30,10 +30,10 @@ const std::string CLASS_NAME = "WfdSourceImpl"; const int32_t ARGS_ONE = 1; const int32_t ARGS_TWO = 2; const int32_t STRING_MAX_SIZE = 255; -const char* kCodeKey = "code"; -const char* kMsgKey = "msg"; -const char* kNameKey = "name"; -const char* kErrorName = "BusinessError"; +const char* CODE "code"; +const char* MSG_KEY = "msg"; +const char* NAME_KEY = "name"; +const char* ERROR_NAME = "BusinessError"; WfdSourceNapi::WfdSourceNapi() { @@ -86,7 +86,8 @@ void WfdSourceNapi::CancelCallbackReference() refMap_.clear(); } -napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) { +napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) +{ SHARING_LOGD("trace."); napi_status status; napi_value result = nullptr; @@ -117,7 +118,8 @@ napi_value WfdSourceNapi::Constructor(napi_env env, napi_callback_info info) { return jsThis; } -void InitializeWfdSource(WfdSourceNapi *jsCast) { +void InitializeWfdSource(WfdSourceNapi *jsCast) +{ std::vector outInfo; AAFwk::AbilityManagerClient::GetInstance()->GetAbilityRunningInfos(outInfo); @@ -137,7 +139,8 @@ void InitializeWfdSource(WfdSourceNapi *jsCast) { } RpcKeyParser parser; - jsCast->localKey_ = parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); + jsCast->localKey_ = + parser.GetRpcKey(jsCast->bundleName_, jsCast->abilityName_, DmKit::GetLocalDevicesInfo().deviceId, CLASS_NAME); jsCast->nativeWfdSource_ = WfdSourceFactory::CreateSource(0, jsCast->localKey_); SHARING_CHECK_AND_RETURN_LOG(jsCast->nativeWfdSource_ != nullptr, "failed to WfdSourceImpl."); @@ -147,7 +150,8 @@ void InitializeWfdSource(WfdSourceNapi *jsCast) { jsCast->nativeWfdSource_->SetListener(jsCast->jsCallback_); } -void FinalizeCallback(napi_env env, void *data, void * /*hint*/) { +void FinalizeCallback(napi_env env, void *data, void * /*hint*/) +{ SHARING_LOGD("Destructor in."); auto *wfdSourceNapi = reinterpret_cast(data); wfdSourceNapi->CancelCallbackReference(); @@ -365,7 +369,8 @@ void WfdSourceNapi::CompleteCallback(napi_env env, napi_status status, void *dat napi_get_reference_value(env, asyncContext->callbackRef, &callback); SHARING_CHECK_AND_RETURN_LOG(callback != nullptr, "callbackRef is nullptr!"); napi_value retVal = undefined; - napi_call_function(env, nullptr, callback, 2, args, &retVal); + constexpr size_t argCount = ARGS_TWO; + napi_call_function(env, nullptr, callback, argCount, args, &retVal); napi_delete_reference(env, asyncContext->callbackRef); } napi_delete_async_work(env, asyncContext->work); @@ -417,7 +422,8 @@ napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std: return napi_ok; } -static napi_status CreateString(napi_env env, const char* str, napi_value* result){ +static napi_status CreateString(napi_env env, const char* str, napi_value* result) +{ return napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, result); } -- Gitee From a2bfa21d73f5072abfc1fb577a4e906e036f1133 Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 19 Nov 2024 16:07:18 +0800 Subject: [PATCH 10/12] fix constName Signed-off-by: huruitao --- frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp | 8 ++++---- frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp index f984c1a..48fadbf 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_sink.cpp @@ -506,23 +506,23 @@ napi_status WfdSinkNapi::CreateError(napi_env env, int32_t errCode, const std::s return napi_invalid_arg; } - nstatus = SetProperty(env, errVal, kCodeKey, errCodeVal); + nstatus = SetProperty(env, errVal, CODE, errCodeVal); if (HandleError(env, "set error code property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = SetProperty(env, errVal, kMsgKey, msgValStr); + nstatus = SetProperty(env, errVal, MSG_KEY, msgValStr); if (HandleError(env, "set error msg property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } napi_value errNameVal = nullptr; - nstatus = CreateString(env, kErrorName, &errNameVal); + nstatus = CreateString(env, ERROR_NAME, &errNameVal); if (HandleError(env, "create BusinessError str fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = SetProperty(env, errVal, kNameKey, errNameVal); + nstatus = SetProperty(env, errVal, NAME_KEY, errNameVal); if (HandleError(env, "set error name property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index 532aed1..42dd9d5 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -398,23 +398,23 @@ napi_status WfdSourceNapi::CreateError(napi_env env, int32_t errCode, const std: return napi_invalid_arg; } - nstatus = SetProperty(env, errVal, kCodeKey, errCodeVal); + nstatus = SetProperty(env, errVal, CODE, errCodeVal); if (HandleError(env, "set error code property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = SetProperty(env, errVal, kMsgKey, msgValStr); + nstatus = SetProperty(env, errVal, MSG_KEY, msgValStr); if (HandleError(env, "set error msg property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } napi_value errNameVal = nullptr; - nstatus = CreateString(env, kErrorName, &errNameVal); + nstatus = CreateString(env, ERROR_NAME, &errNameVal); if (HandleError(env, "create BusinessError str fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } - nstatus = SetProperty(env, errVal, kNameKey, errNameVal); + nstatus = SetProperty(env, errVal, NAME_KEY, errNameVal); if (HandleError(env, "set error name property fail.", nstatus, &errVal) != napi_ok) { return napi_invalid_arg; } -- Gitee From 3ff22e307ddf5fc7ae50938b27c0caf8c140906c Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 19 Nov 2024 16:10:00 +0800 Subject: [PATCH 11/12] fix codecheck Signed-off-by: huruitao --- frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index 42dd9d5..738191a 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -266,8 +266,7 @@ napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "StopDiscovery", NAPI_AUTO_LENGTH, &resource); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, - WfdSourceNapi::CompleteCallback,static_cast(asyncContext.get()), - &asyncContext->work)); + WfdSourceNapi::CompleteCallback,static_cast(asyncContext.get()), &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); asyncContext.release(); -- Gitee From c3391af520ee90f1e06a690746f7e29632360bb5 Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 19 Nov 2024 16:29:17 +0800 Subject: [PATCH 12/12] codecheck fix, Signed-off-by: huruitao --- frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp index 738191a..472012d 100755 --- a/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp +++ b/frameworks/kitsimpl/js/wfd/wfd_napi_source.cpp @@ -266,7 +266,7 @@ napi_value WfdSourceNapi::StopDiscovery(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "StopDiscovery", NAPI_AUTO_LENGTH, &resource); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, WfdSourceNapi::AsyncWork, - WfdSourceNapi::CompleteCallback,static_cast(asyncContext.get()), &asyncContext->work)); + WfdSourceNapi::CompleteCallback, static_cast(asyncContext.get()), &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); asyncContext.release(); -- Gitee