diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index ba6edb7ff6f139ca9e853ab90a92fa5b018331d0..d5a675bb113d9ec4f0a8c98f5a0e640568d27874 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -91,6 +91,10 @@ public: OnUnSubscribeResultEvent(NotificationConstant::SubscribeResult result) : SubscriberEvent(SubscriberEventType::ON_UNSUBSCRIBERESULT), unSubscribeResult_(result) {} + + ~OnUnSubscribeResultEvent() override + {} + NotificationConstant::SubscribeResult GetUnSubscribeResult() { return unSubscribeResult_; @@ -104,6 +108,9 @@ class OnDiedEvent : public SubscriberEvent { public: OnDiedEvent() : SubscriberEvent(SubscriberEventType::ON_DIED) {} + + ~OnDiedEvent() override + {} }; class OnUpdatedEvent : public SubscriberEvent { @@ -112,6 +119,9 @@ public: : SubscriberEvent(SubscriberEventType::ON_UPDATE), sortingMap_(sortingMap) {} + ~OnUpdatedEvent() override + {} + std::shared_ptr GetNotificationSortingMap() { return sortingMap_; @@ -127,6 +137,9 @@ public: : SubscriberEvent(SubscriberEventType::ON_DISTURBMODECHANGED), disturbMode_(disturbMode) {} + ~OnDisturbModeChangedEvent() override + {} + int GetDisturbModeChanged() { return disturbMode_; @@ -142,6 +155,9 @@ public: : SubscriberEvent(SubscriberEventType::ON_CANCELED), request_(request) {} + ~OnOnCanceledEvent() override + {} + std::shared_ptr GetRequest() { return request_; @@ -161,6 +177,9 @@ public: deleteReason_(deleteReason) {} + ~OnOnCanceledWithSortingMapAndDeleteReasonEvent() override + {} + std::shared_ptr GetRequest() { return request_; @@ -186,6 +205,9 @@ public: : SubscriberEvent(SubscriberEventType::ON_CONSUMED), request_(request) {} + ~OnConsumedEvent() override + {} + std::shared_ptr GetRequest() { return request_; @@ -204,6 +226,9 @@ public: type_ = SubscriberEventType::ON_CONSUMED_WITH_SORTINGMAP; } + ~OnConsumedWithSortingMapEvent() override + {} + std::shared_ptr GetRequest() { return request_; @@ -325,6 +350,9 @@ public: waitOnCanceledWithSortingMapAndDeleteReason_ = false; } + ~EventParser() + {} + void parse(std::list> events) { for (auto event : events) { diff --git a/frameworks/wantagent/src/pending_want.cpp b/frameworks/wantagent/src/pending_want.cpp index d5d86db34149ebe610de1c79b72029a9fab72c7c..926dbfc2d994fd94b0d7a287994321a1dfd31628 100644 --- a/frameworks/wantagent/src/pending_want.cpp +++ b/frameworks/wantagent/src/pending_want.cpp @@ -347,8 +347,8 @@ void PendingWant::UnregisterCancelListener( std::scoped_lock lock(lock_object); bool isEmpty = cancelListeners_.empty(); cancelListeners_.erase(remove_if(cancelListeners_.begin(), - cancelListeners_.end(), - [cancelListener](std::shared_ptr x) { return x == cancelListener; }), + cancelListeners_.end(), + [cancelListener](std::shared_ptr x) { return x == cancelListener; }), cancelListeners_.end()); if (cancelListeners_.empty() && !isEmpty) { AbilityManagerClient::GetInstance()->UnregisterCancelListener(target, cancelReceiver_); diff --git a/frameworks/wantagent/src/want_agent_info.cpp b/frameworks/wantagent/src/want_agent_info.cpp index b0fe01159843af11f910fdc83143b09d2cab2497..a3a69fb230f51308438005d20f2525e41abb3288 100644 --- a/frameworks/wantagent/src/want_agent_info.cpp +++ b/frameworks/wantagent/src/want_agent_info.cpp @@ -104,5 +104,4 @@ std::shared_ptr WantAgentInfo::GetExtraInfo() const { return extraInfo_; } - } // namespace OHOS::Notification::WantAgent \ No newline at end of file diff --git a/interfaces/kits/napi/ans/include/init.h b/interfaces/kits/napi/ans/include/init.h index 1690462d791838a58b4ff63c9c0d03a17b004b98..fabae8a06626e8624dda17b9c8fe957cca20971a 100644 --- a/interfaces/kits/napi/ans/include/init.h +++ b/interfaces/kits/napi/ans/include/init.h @@ -41,7 +41,7 @@ napi_module _module = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = Init, - .nm_modname = "notificationnapi", + .nm_modname = "notification", .nm_priv = ((void *)0), .reserved = {0} diff --git a/interfaces/kits/napi/wantagent/napi_want_agent.cpp b/interfaces/kits/napi/wantagent/napi_want_agent.cpp index 0f347104567df4e611070f034de13430afd674d2..42c7b80fcf54aa6611059a671e132447ef922fbe 100644 --- a/interfaces/kits/napi/wantagent/napi_want_agent.cpp +++ b/interfaces/kits/napi/wantagent/napi_want_agent.cpp @@ -19,14 +19,12 @@ #include #include #include -#include #include "hilog_wrapper.h" #include "napi_common.h" #include "want_agent_helper.h" namespace OHOS { - constexpr int32_t BUSINESS_ERROR_CODE_OK = 0; TriggerCompleteCallBack::TriggerCompleteCallBack() @@ -46,6 +44,69 @@ void TriggerCompleteCallBack::SetWantAgentInstance(const std::shared_ptrdata; + if (dataWorkerData == nullptr) { + HILOG_INFO("TriggerReceiveDataWorker instance(uv_work_t) is nullptr"); + return; + } + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value callResult = nullptr; + + result[0] = GetCallbackErrorResult(dataWorkerData->env, BUSINESS_ERROR_CODE_OK); + napi_create_object(dataWorkerData->env, &result[1]); + // wrap wantAgent + napi_value wantAgentClass = nullptr; + napi_define_class(dataWorkerData->env, + "WantAgentClass", + NAPI_AUTO_LENGTH, + [](napi_env env, napi_callback_info info) -> napi_value { + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; + }, + nullptr, + 0, + nullptr, + &wantAgentClass); + napi_value jsWantAgent = nullptr; + napi_new_instance(dataWorkerData->env, wantAgentClass, 0, nullptr, &jsWantAgent); + napi_wrap(dataWorkerData->env, + jsWantAgent, + (void *)dataWorkerData->wantAgent.get(), + [](napi_env env, void *data, void *hint) {}, + nullptr, + nullptr); + napi_set_named_property(dataWorkerData->env, result[1], "wantAgent", jsWantAgent); + // wrap want + napi_value jsWant = nullptr; + jsWant = WrapWant(dataWorkerData->env, dataWorkerData->want); + napi_set_named_property(dataWorkerData->env, result[1], "want", jsWant); + // wrap finalCode + napi_value jsFinalCode = nullptr; + napi_create_int32(dataWorkerData->env, dataWorkerData->resultCode, &jsFinalCode); + napi_set_named_property(dataWorkerData->env, result[1], "finalCode", jsFinalCode); + // wrap finalData + napi_value jsFinalData = nullptr; + napi_create_string_utf8(dataWorkerData->env, dataWorkerData->resultData.c_str(), NAPI_AUTO_LENGTH, &jsFinalData); + napi_set_named_property(dataWorkerData->env, result[1], "finalData", jsFinalCode); + // wrap extraInfo + napi_value jsExtraInfo = WrapWantParams(dataWorkerData->env, dataWorkerData->resultExtras); + napi_set_named_property(dataWorkerData->env, result[1], "extraInfo", jsExtraInfo); + + napi_get_undefined(dataWorkerData->env, &undefined); + napi_get_reference_value(dataWorkerData->env, dataWorkerData->ref, &callback); + napi_call_function(dataWorkerData->env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + + delete dataWorkerData; + dataWorkerData = nullptr; + delete work; + work = nullptr; +} + void TriggerCompleteCallBack::OnSendFinished( const AAFwk::Want &want, int resultCode, const std::string &resultData, const AAFwk::WantParams &resultExtras) { @@ -83,97 +144,44 @@ void TriggerCompleteCallBack::OnSendFinished( dataWorker->ref = triggerCompleteInfo_.ref; dataWorker->wantAgent = triggerCompleteInfo_.wantAgent; work->data = (void *)dataWorker; - uv_queue_work(loop, - work, - [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - TriggerReceiveDataWorker *dataWorkerData = (TriggerReceiveDataWorker *)work->data; - if (dataWorkerData == nullptr) { - HILOG_INFO("TriggerReceiveDataWorker instance(uv_work_t) is nullptr"); - return; - } - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(dataWorkerData->env, BUSINESS_ERROR_CODE_OK); - napi_create_object(dataWorkerData->env, &result[1]); - // wrap wantAgent - napi_value wantAgentClass = nullptr; - napi_define_class(dataWorkerData->env, - "WantAgentClass", - NAPI_AUTO_LENGTH, - [](napi_env env, napi_callback_info info) -> napi_value { - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - return thisVar; - }, - nullptr, - 0, - nullptr, - &wantAgentClass); - napi_value jsWantAgent = nullptr; - napi_new_instance(dataWorkerData->env, wantAgentClass, 0, nullptr, &jsWantAgent); - napi_wrap(dataWorkerData->env, - jsWantAgent, - (void *)dataWorkerData->wantAgent.get(), - [](napi_env env, void *data, void *hint) {}, - nullptr, - nullptr); - napi_set_named_property(dataWorkerData->env, result[1], "wantAgent", jsWantAgent); - // wrap want - napi_value jsWant; - jsWant = WrapWant(dataWorkerData->env, dataWorkerData->want); - napi_set_named_property(dataWorkerData->env, result[1], "want", jsWant); - // wrap finalCode - napi_value jsFinalCode; - napi_create_int32(dataWorkerData->env, dataWorkerData->resultCode, &jsFinalCode); - napi_set_named_property(dataWorkerData->env, result[1], "finalCode", jsFinalCode); - // wrap finalData - napi_value jsFinalData; - napi_create_string_utf8( - dataWorkerData->env, dataWorkerData->resultData.c_str(), NAPI_AUTO_LENGTH, &jsFinalData); - napi_set_named_property(dataWorkerData->env, result[1], "finalData", jsFinalCode); - // wrap extraInfo - napi_value jsExtraInfo; - jsExtraInfo = WrapWantParams(dataWorkerData->env, dataWorkerData->resultExtras); - napi_set_named_property(dataWorkerData->env, result[1], "extraInfo", jsExtraInfo); - - napi_get_undefined(dataWorkerData->env, &undefined); - napi_get_reference_value(dataWorkerData->env, dataWorkerData->ref, &callback); - napi_call_function( - dataWorkerData->env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - - delete dataWorkerData; - dataWorkerData = nullptr; + int ret = + uv_queue_work(loop, work, [](uv_work_t *work) {}, TriggerCompleteCallBack::OnSendFinishedUvAfterWorkCallbacck); + if (ret != 0) { + if (dataWorker != nullptr) { + delete dataWorker; + dataWorker = nullptr; + } + if (work != nullptr) { delete work; work = nullptr; - }); + } + } HILOG_INFO("TriggerCompleteCallBack::OnSendFinished end"); } napi_value WantAgentInit(napi_env env, napi_value exports) { HILOG_INFO("napi_moudule Init start..."); - napi_property_descriptor desc[] = {DECLARE_NAPI_FUNCTION("getBundleName", NAPI_GetBundleName), + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getBundleName", NAPI_GetBundleName), DECLARE_NAPI_FUNCTION("getUid", NAPI_GetUid), DECLARE_NAPI_FUNCTION("cancel", NAPI_Cancel), DECLARE_NAPI_FUNCTION("trigger", NAPI_Trigger), DECLARE_NAPI_FUNCTION("equal", NAPI_Equal), DECLARE_NAPI_FUNCTION("getWant", NAPI_GetWant), - DECLARE_NAPI_FUNCTION("getWantAgent", NAPI_GetWantAgent)}; + DECLARE_NAPI_FUNCTION("getWantAgent", NAPI_GetWantAgent), + }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); HILOG_INFO("napi_moudule Init end..."); return exports; } -void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName) +void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const std::string &propName) { napi_value prop = nullptr; if (napi_create_int32(env, objName, &prop) == napi_ok) { - napi_set_named_property(env, dstObj, propName, prop); + napi_set_named_property(env, dstObj, propName.c_str(), prop); } } @@ -225,79 +233,84 @@ napi_value WantAgentOperationTypeInit(napi_env env, napi_value exports) return exports; } +void NAPI_GetBundleNameExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("GetBundleName called(CallBack Mode)..."); + AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; + asyncCallbackInfo->bundleName = WantAgentHelper::GetBundleName(asyncCallbackInfo->wantAgent); +} + +void NAPI_GetBundleNameCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetBundleName compeleted(CallBack Mode)..."); + AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value callResult = nullptr; + + result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); + napi_create_string_utf8(env, asyncCallbackInfo->bundleName.c_str(), NAPI_AUTO_LENGTH, &result[1]); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + + if (asyncCallbackInfo->callback[0] != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +void NAPI_GetBundleNamePromiseCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetBundleName compeleted(Promise Mode)..."); + AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; + napi_value result; + napi_create_string_utf8(env, asyncCallbackInfo->bundleName.c_str(), NAPI_AUTO_LENGTH, &result); + napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + napi_value NAPI_GetBundleNameWrap( - napi_env env, napi_callback_info info, bool callBackMode, AsyncGetBundleNameCallbackInfo *asyncCallbackInfo) + napi_env env, napi_callback_info info, bool callBackMode, AsyncGetBundleNameCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_GetBundleNameWrap called..."); if (callBackMode) { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetBundleNameCallBack", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetBundleName called(CallBack Mode)..."); - AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; - asyncCallbackInfo->bundleName = WantAgentHelper::GetBundleName(asyncCallbackInfo->wantAgent); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetBundleName compeleted(CallBack Mode)..."); - AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); - napi_create_string_utf8(env, asyncCallbackInfo->bundleName.c_str(), NAPI_AUTO_LENGTH, &result[1]); - napi_get_undefined(env, &undefined); - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_GetBundleNameExecuteCallBack, + NAPI_GetBundleNameCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } else { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetBundleNamePromise", NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise; + napi_deferred deferred = nullptr; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo.deferred = deferred; napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetBundleName called(Promise Mode)..."); - AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; - asyncCallbackInfo->bundleName = WantAgentHelper::GetBundleName(asyncCallbackInfo->wantAgent); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetBundleName compeleted(Promise Mode)..."); - AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = (AsyncGetBundleNameCallbackInfo *)data; - napi_value result; - napi_create_string_utf8(env, asyncCallbackInfo->bundleName.c_str(), NAPI_AUTO_LENGTH, &result); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + NAPI_GetBundleNameExecuteCallBack, + NAPI_GetBundleNamePromiseCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + napi_queue_async_work(env, asyncCallbackInfo.asyncWork); return promise; } } @@ -305,11 +318,11 @@ napi_value NAPI_GetBundleNameWrap( napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) { size_t argc = NUMBER_OF_PARAMETERS_TWO; - napi_value argv[argc]; + napi_value argv[NUMBER_OF_PARAMETERS_TWO] = {}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); HILOG_INFO("argc = [%{public}zu]", argc); - napi_valuetype wantAgentType; + napi_valuetype wantAgentType = napi_valuetype::napi_null; napi_typeof(env, argv[0], &wantAgentType); NAPI_ASSERT(env, wantAgentType == napi_object, "Wrong argument type. Object expected."); @@ -322,13 +335,16 @@ napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_TWO) { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); callBackMode = true; } - AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetBundleNameCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AsyncGetBundleNameCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncGetBundleNameCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } @@ -337,7 +353,7 @@ napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) if (callBackMode) { napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_GetBundleNameWrap(env, info, callBackMode, asyncCallbackInfo); + napi_value ret = NAPI_GetBundleNameWrap(env, info, callBackMode, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -345,79 +361,84 @@ napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) return ((callBackMode) ? (NapiGetNull(env)) : (ret)); } +void NAPI_GetUidWrapExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("GetUid called(CallBack Mode)..."); + AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; + asyncCallbackInfo->uid = WantAgentHelper::GetUid(asyncCallbackInfo->wantAgent); +} + +void NAPI_GetUidWrapCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetUid compeleted(CallBack Mode)..."); + AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; + napi_value callback; + napi_value undefined; + napi_value callResult = 0; + + result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); + napi_create_int32(env, asyncCallbackInfo->uid, &result[1]); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + + if (asyncCallbackInfo->callback[0] != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +void NAPI_GetUidWrapPromiseCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetUid compeleted(Promise Mode)..."); + AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; + napi_value result; + napi_create_int32(env, asyncCallbackInfo->uid, &result); + napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + napi_value NAPI_GetUidWrap( - napi_env env, napi_callback_info info, bool callBackMode, AsyncGetUidCallbackInfo *asyncCallbackInfo) + napi_env env, napi_callback_info info, bool callBackMode, AsyncGetUidCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_GetUidWrap called..."); if (callBackMode) { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetUidCallBack", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetUid called(CallBack Mode)..."); - AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; - asyncCallbackInfo->uid = WantAgentHelper::GetUid(asyncCallbackInfo->wantAgent); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetUid compeleted(CallBack Mode)..."); - AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); - napi_create_int32(env, asyncCallbackInfo->uid, &result[1]); - napi_get_undefined(env, &undefined); - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_GetUidWrapExecuteCallBack, + NAPI_GetUidWrapCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } else { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetUidPromise", NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise; + napi_deferred deferred = nullptr; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo.deferred = deferred; napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetUid called(Promise Mode)..."); - AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; - asyncCallbackInfo->uid = WantAgentHelper::GetUid(asyncCallbackInfo->wantAgent); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetUid compeleted(Promise Mode)..."); - AsyncGetUidCallbackInfo *asyncCallbackInfo = (AsyncGetUidCallbackInfo *)data; - napi_value result; - napi_create_int32(env, asyncCallbackInfo->uid, &result); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + NAPI_GetUidWrapExecuteCallBack, + NAPI_GetUidWrapPromiseCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + napi_queue_async_work(env, asyncCallbackInfo.asyncWork); return promise; } } @@ -425,11 +446,11 @@ napi_value NAPI_GetUidWrap( napi_value NAPI_GetUid(napi_env env, napi_callback_info info) { size_t argc = NUMBER_OF_PARAMETERS_TWO; - napi_value argv[argc]; + napi_value argv[NUMBER_OF_PARAMETERS_TWO] = {}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); HILOG_INFO("argc = [%{public}zu]", argc); - napi_valuetype wantAgentType; + napi_valuetype wantAgentType = napi_valuetype::napi_null; napi_typeof(env, argv[0], &wantAgentType); NAPI_ASSERT(env, wantAgentType == napi_object, "Wrong argument type. Object expected."); @@ -441,13 +462,16 @@ napi_value NAPI_GetUid(napi_env env, napi_callback_info info) bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_TWO) { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); callBackMode = true; } - AsyncGetUidCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetUidCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AsyncGetUidCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncGetUidCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } @@ -456,7 +480,7 @@ napi_value NAPI_GetUid(napi_env env, napi_callback_info info) if (callBackMode) { napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_GetUidWrap(env, info, callBackMode, asyncCallbackInfo); + napi_value ret = NAPI_GetUidWrap(env, info, callBackMode, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -464,78 +488,82 @@ napi_value NAPI_GetUid(napi_env env, napi_callback_info info) return ((callBackMode) ? (NapiGetNull(env)) : (ret)); } +void NAPI_GetWantWrapExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("GetWant called(CallBack Mode)..."); + AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; + asyncCallbackInfo->want = WantAgentHelper::GetWant(asyncCallbackInfo->wantAgent); +} + +void NAPI_GetWantWrapCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetWant compeleted(CallBack Mode)..."); + AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {}; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value callResult = nullptr; + + result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); + result[1] = WrapWant(env, *(asyncCallbackInfo->want)); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + if (asyncCallbackInfo->callback[0] != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +void NAPI_GetWantWrapPromiseCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetWant compeleted(Promise Mode)..."); + AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; + napi_value result = WrapWant(env, *(asyncCallbackInfo->want)); + napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + napi_value NAPI_GetWantWrap( - napi_env env, napi_callback_info info, bool callBackMode, AsyncGetWantCallbackInfo *asyncCallbackInfo) + napi_env env, napi_callback_info info, bool callBackMode, AsyncGetWantCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_GetWantWrap called..."); if (callBackMode) { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetWantCallBack", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetWant called(CallBack Mode)..."); - AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; - asyncCallbackInfo->want = WantAgentHelper::GetWant(asyncCallbackInfo->wantAgent); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetWant compeleted(CallBack Mode)..."); - AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); - result[1] = WrapWant(env, *(asyncCallbackInfo->want)); - napi_get_undefined(env, &undefined); - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_GetWantWrapExecuteCallBack, + NAPI_GetWantWrapCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } else { napi_value resourceName; napi_create_string_latin1(env, "NAPI_GetWantPromise", NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise; + napi_deferred deferred = nullptr; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo.deferred = deferred; napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetWant called(Promise Mode)..."); - AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; - asyncCallbackInfo->want = WantAgentHelper::GetWant(asyncCallbackInfo->wantAgent); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetWant compeleted(Promise Mode)..."); - AsyncGetWantCallbackInfo *asyncCallbackInfo = (AsyncGetWantCallbackInfo *)data; - napi_value result; - result = WrapWant(env, *(asyncCallbackInfo->want)); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + NAPI_GetWantWrapExecuteCallBack, + NAPI_GetWantWrapPromiseCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + napi_queue_async_work(env, asyncCallbackInfo.asyncWork); return promise; } } @@ -543,11 +571,11 @@ napi_value NAPI_GetWantWrap( napi_value NAPI_GetWant(napi_env env, napi_callback_info info) { size_t argc = NUMBER_OF_PARAMETERS_TWO; - napi_value argv[argc]; + napi_value argv[NUMBER_OF_PARAMETERS_TWO] = {}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); HILOG_INFO("argc = [%{public}zu]", argc); - napi_valuetype wantAgentType; + napi_valuetype wantAgentType = napi_valuetype::napi_null; napi_typeof(env, argv[0], &wantAgentType); NAPI_ASSERT(env, wantAgentType == napi_object, "Wrong argument type. Object expected."); @@ -559,13 +587,16 @@ napi_value NAPI_GetWant(napi_env env, napi_callback_info info) bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_TWO) { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); callBackMode = true; } - AsyncGetWantCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetWantCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AsyncGetWantCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncGetWantCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } @@ -574,7 +605,7 @@ napi_value NAPI_GetWant(napi_env env, napi_callback_info info) if (callBackMode) { napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_GetWantWrap(env, info, callBackMode, asyncCallbackInfo); + napi_value ret = NAPI_GetWantWrap(env, info, callBackMode, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -598,83 +629,86 @@ void DeleteRecordByCode(const int32_t code) } } +void NAPI_CancelWrapExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("Cancel called(CallBack Mode)..."); + AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; + int32_t code = WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); + WantAgentHelper::Cancel(asyncCallbackInfo->wantAgent); + DeleteRecordByCode(code); +} + +void NAPI_CancelWrapCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("Cancel compeleted(CallBack Mode)..."); + AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {nullptr}; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value callResult = nullptr; + + result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); + napi_get_null(env, &result[1]); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + + if (asyncCallbackInfo->callback[0] != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +void NAPI_CancelWrapPromiseCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("Cancel compeleted(Promise Mode)..."); + AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; + napi_value result; + napi_get_null(env, &result); + napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + napi_value NAPI_CancelWrap( - napi_env env, napi_callback_info info, bool callBackMode, AsyncCancelCallbackInfo *asyncCallbackInfo) + napi_env env, napi_callback_info info, bool callBackMode, AsyncCancelCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_CancelWrap called..."); if (callBackMode) { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_CancelCallBack", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("Cancel called(CallBack Mode)..."); - AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; - int32_t code = WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); - WantAgentHelper::Cancel(asyncCallbackInfo->wantAgent); - DeleteRecordByCode(code); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("Cancel compeleted(CallBack Mode)..."); - AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); - napi_get_null(env, &result[1]); - napi_get_undefined(env, &undefined); - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_CancelWrapExecuteCallBack, + NAPI_CancelWrapCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } else { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_CancelPromise", NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise; + napi_deferred deferred = nullptr; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo.deferred = deferred; napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("Cancel called(Promise Mode)..."); - AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; - int32_t code = WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); - WantAgentHelper::Cancel(asyncCallbackInfo->wantAgent); - DeleteRecordByCode(code); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("Cancel compeleted(Promise Mode)..."); - AsyncCancelCallbackInfo *asyncCallbackInfo = (AsyncCancelCallbackInfo *)data; - napi_value result; - napi_get_null(env, &result); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + NAPI_CancelWrapExecuteCallBack, + NAPI_CancelWrapPromiseCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + napi_queue_async_work(env, asyncCallbackInfo.asyncWork); return promise; } } @@ -682,11 +716,11 @@ napi_value NAPI_CancelWrap( napi_value NAPI_Cancel(napi_env env, napi_callback_info info) { size_t argc = NUMBER_OF_PARAMETERS_TWO; - napi_value argv[argc]; + napi_value argv[NUMBER_OF_PARAMETERS_TWO] = {}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); HILOG_INFO("argc = [%{public}zu]", argc); - napi_valuetype wantAgentType; + napi_valuetype wantAgentType = napi_valuetype::napi_null; napi_typeof(env, argv[0], &wantAgentType); NAPI_ASSERT(env, wantAgentType == napi_object, "Wrong argument type. Object expected."); @@ -698,13 +732,16 @@ napi_value NAPI_Cancel(napi_env env, napi_callback_info info) bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_TWO) { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); callBackMode = true; } - AsyncCancelCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncCancelCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AsyncCancelCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncCancelCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } @@ -713,7 +750,7 @@ napi_value NAPI_Cancel(napi_env env, napi_callback_info info) if (callBackMode) { napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_CancelWrap(env, info, callBackMode, asyncCallbackInfo); + napi_value ret = NAPI_CancelWrap(env, info, callBackMode, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -721,61 +758,53 @@ napi_value NAPI_Cancel(napi_env env, napi_callback_info info) return ((callBackMode) ? (NapiGetNull(env)) : (ret)); } -napi_value NAPI_TriggerWrap(napi_env env, napi_callback_info info, AsyncTriggerCallbackInfo *asyncCallbackInfo) +void NAPI_TriggerWrapExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("Trigger called ..."); + AsyncTriggerCallbackInfo *asyncCallbackInfo = (AsyncTriggerCallbackInfo *)data; + asyncCallbackInfo->triggerObj->SetCallbackInfo(env, asyncCallbackInfo->callback[0]); + asyncCallbackInfo->triggerObj->SetWantAgentInstance(asyncCallbackInfo->wantAgent); + WantAgentHelper::TriggerWantAgent(asyncCallbackInfo->context, + asyncCallbackInfo->wantAgent, + asyncCallbackInfo->triggerObj, + asyncCallbackInfo->triggerInfo); +} + +void NAPI_TriggerWrapCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("Trigger compeleted ..."); + AsyncTriggerCallbackInfo *asyncCallbackInfo = (AsyncTriggerCallbackInfo *)data; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +napi_value NAPI_TriggerWrap(napi_env env, napi_callback_info info, AsyncTriggerCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_TriggerWrap called..."); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_TriggerWrap", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("Trigger called ..."); - AsyncTriggerCallbackInfo *asyncCallbackInfo = (AsyncTriggerCallbackInfo *)data; - asyncCallbackInfo->triggerObj->SetCallbackInfo(env, asyncCallbackInfo->callback[0]); - asyncCallbackInfo->triggerObj->SetWantAgentInstance(asyncCallbackInfo->wantAgent); - WantAgentHelper::TriggerWantAgent(asyncCallbackInfo->context, - asyncCallbackInfo->wantAgent, - asyncCallbackInfo->triggerObj, - asyncCallbackInfo->triggerInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("Trigger compeleted ..."); - AsyncTriggerCallbackInfo *asyncCallbackInfo = (AsyncTriggerCallbackInfo *)data; - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); + NAPI_TriggerWrapExecuteCallBack, + NAPI_TriggerWrapCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } -napi_value NAPI_Trigger(napi_env env, napi_callback_info info) +napi_value NAPI_GetTriggerInfo(napi_value argv[NUMBER_OF_PARAMETERS_THREE], size_t argc, napi_env env, + napi_callback_info info, Notification::WantAgent::TriggerInfo &triggerInfo) { - size_t argc = NUMBER_OF_PARAMETERS_THREE; - napi_value argv[argc]; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); - HILOG_INFO("argc = [%{public}zu]", argc); - - napi_valuetype wantAgentType; - napi_typeof(env, argv[0], &wantAgentType); - NAPI_ASSERT(env, wantAgentType == napi_object, "Wrong argument type. Object expected."); - - Notification::WantAgent::WantAgent *pWantAgent = nullptr; - napi_unwrap(env, argv[0], (void **)&(pWantAgent)); - if (pWantAgent == nullptr) { - return NapiGetNull(env); - } - // Get triggerInfo napi_value jsTriggerInfo = argv[1]; - napi_valuetype valueType; + napi_valuetype valueType = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, jsTriggerInfo, &valueType)); NAPI_ASSERT(env, valueType == napi_object, "param type mismatch!"); @@ -795,7 +824,7 @@ napi_value NAPI_Trigger(napi_env env, napi_callback_info info) } } // Get triggerInfo permission - std::string permission; + std::string permission = {}; UnwrapStringByPropertyName(env, jsTriggerInfo, "permission", permission); // Get triggerInfo extraInfo napi_value jsExtraInfo = nullptr; @@ -807,15 +836,37 @@ napi_value NAPI_Trigger(napi_env env, napi_callback_info info) return NapiGetNull(env); } } + + Notification::WantAgent::TriggerInfo triggerInfoData(permission, extraInfo, want, code); + triggerInfo = triggerInfoData; + return NapiGetNull(env); +} + +napi_value NAPI_Trigger(napi_env env, napi_callback_info info) +{ + size_t argc = NUMBER_OF_PARAMETERS_THREE; + napi_value argv[NUMBER_OF_PARAMETERS_THREE] = {}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + HILOG_INFO("argc = [%{public}zu]", argc); + + napi_valuetype wantAgentType = napi_valuetype::napi_null; + napi_typeof(env, argv[0], &wantAgentType); + NAPI_ASSERT(env, wantAgentType == napi_object, "Wrong argument type. Object expected."); + + Notification::WantAgent::WantAgent *pWantAgent = nullptr; + napi_unwrap(env, argv[0], (void **)&(pWantAgent)); + if (pWantAgent == nullptr) { + return NapiGetNull(env); + } + // Get context - napi_value global = 0; + napi_value global = nullptr; NAPI_CALL(env, napi_get_global(env, &global)); - napi_value abilityObj = 0; + napi_value abilityObj = nullptr; NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); Ability *ability = nullptr; NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - std::shared_ptr context = nullptr; - context = ability->GetContext(); + std::shared_ptr context = ability->GetContext(); bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_THREE) { @@ -824,14 +875,23 @@ napi_value NAPI_Trigger(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); callBackMode = true; } - AsyncTriggerCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncTriggerCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + + Notification::WantAgent::TriggerInfo triggerInfo; + napi_value ret = NAPI_GetTriggerInfo(argv, argc, env, info, triggerInfo); + if (ret == nullptr) { + return NapiGetNull(env); + } + + AsyncTriggerCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncTriggerCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } asyncCallbackInfo->wantAgent = std::make_shared(*pWantAgent); asyncCallbackInfo->context = context; - Notification::WantAgent::TriggerInfo triggerInfo(permission, extraInfo, want, code); asyncCallbackInfo->triggerInfo = triggerInfo; asyncCallbackInfo->triggerObj = nullptr; if (callBackMode) { @@ -840,7 +900,7 @@ napi_value NAPI_Trigger(napi_env env, napi_callback_info info) napi_create_reference(env, argv[NUMBER_OF_PARAMETERS_TWO], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_TriggerWrap(env, info, asyncCallbackInfo); + ret = NAPI_TriggerWrap(env, info, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -848,81 +908,85 @@ napi_value NAPI_Trigger(napi_env env, napi_callback_info info) return NapiGetNull(env); } +void NAPI_EqualWrapExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("Equal called(CallBack Mode)..."); + AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; + asyncCallbackInfo->result = + WantAgentHelper::JudgeEquality(asyncCallbackInfo->wantAgentFirst, asyncCallbackInfo->wantAgentSecond); +} + +void NAPI_EqualWrapCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("Equal compeleted(CallBack Mode)..."); + AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {}; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value callResult = nullptr; + + result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); + napi_get_boolean(env, asyncCallbackInfo->result, &result[1]); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + + if (asyncCallbackInfo->callback[0] != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +void NAPI_EqualWrapPromiseCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("Equal compeleted(Promise Mode)..."); + AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; + napi_value result; + napi_get_boolean(env, asyncCallbackInfo->result, &result); + napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + napi_value NAPI_EqualWrap( - napi_env env, napi_callback_info info, bool callBackMode, AsyncEqualCallbackInfo *asyncCallbackInfo) + napi_env env, napi_callback_info info, bool callBackMode, AsyncEqualCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_EqualWrap called..."); if (callBackMode) { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_EqualWrapCallBack", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("Equal called(CallBack Mode)..."); - AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; - asyncCallbackInfo->result = WantAgentHelper::JudgeEquality( - asyncCallbackInfo->wantAgentFirst, asyncCallbackInfo->wantAgentSecond); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("Equal compeleted(CallBack Mode)..."); - AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); - napi_get_boolean(env, asyncCallbackInfo->result, &result[1]); - napi_get_undefined(env, &undefined); - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_EqualWrapExecuteCallBack, + NAPI_EqualWrapCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } else { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_EqualPromise", NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise; + napi_deferred deferred = nullptr; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo.deferred = deferred; napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("Equal called(Promise Mode)..."); - AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; - asyncCallbackInfo->result = WantAgentHelper::JudgeEquality( - asyncCallbackInfo->wantAgentFirst, asyncCallbackInfo->wantAgentSecond); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("Equal compeleted(Promise Mode)..."); - AsyncEqualCallbackInfo *asyncCallbackInfo = (AsyncEqualCallbackInfo *)data; - napi_value result; - napi_get_boolean(env, asyncCallbackInfo->result, &result); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + NAPI_EqualWrapExecuteCallBack, + NAPI_EqualWrapPromiseCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + napi_queue_async_work(env, asyncCallbackInfo.asyncWork); return promise; } } @@ -930,11 +994,11 @@ napi_value NAPI_EqualWrap( napi_value NAPI_Equal(napi_env env, napi_callback_info info) { size_t argc = NUMBER_OF_PARAMETERS_THREE; - napi_value argv[argc]; + napi_value argv[NUMBER_OF_PARAMETERS_THREE] = {}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); HILOG_INFO("argc = [%{public}zu]", argc); - napi_valuetype wantAgentFirstType; + napi_valuetype wantAgentFirstType = napi_valuetype::napi_null; napi_typeof(env, argv[0], &wantAgentFirstType); NAPI_ASSERT(env, wantAgentFirstType == napi_object, "Wrong argument type. Object expected."); @@ -944,7 +1008,7 @@ napi_value NAPI_Equal(napi_env env, napi_callback_info info) return NapiGetNull(env); } - napi_valuetype wantAgentSecondType; + napi_valuetype wantAgentSecondType = napi_valuetype::napi_null; napi_typeof(env, argv[1], &wantAgentSecondType); NAPI_ASSERT(env, wantAgentSecondType == napi_object, "Wrong argument type. Object expected."); @@ -956,13 +1020,16 @@ napi_value NAPI_Equal(napi_env env, napi_callback_info info) bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_THREE) { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, argv[NUMBER_OF_PARAMETERS_TWO], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); callBackMode = true; } - AsyncEqualCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncEqualCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AsyncEqualCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncEqualCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } @@ -972,7 +1039,7 @@ napi_value NAPI_Equal(napi_env env, napi_callback_info info) if (callBackMode) { napi_create_reference(env, argv[NUMBER_OF_PARAMETERS_TWO], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_EqualWrap(env, info, callBackMode, asyncCallbackInfo); + napi_value ret = NAPI_EqualWrap(env, info, callBackMode, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -980,211 +1047,181 @@ napi_value NAPI_Equal(napi_env env, napi_callback_info info) return ((callBackMode) ? (NapiGetNull(env)) : (ret)); } +void NAPI_GetWantAgentWrapExecuteCallBack(napi_env env, void *data) +{ + HILOG_INFO("GetWantAgent called(CallBack Mode)..."); + AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; + Notification::WantAgent::WantAgentInfo wantAgentInfo(asyncCallbackInfo->requestCode, + asyncCallbackInfo->operationType, + asyncCallbackInfo->wantAgentFlags, + asyncCallbackInfo->wants, + asyncCallbackInfo->extraInfo); + asyncCallbackInfo->wantAgent = + Notification::WantAgent::WantAgentHelper::GetWantAgent(asyncCallbackInfo->context, wantAgentInfo); + if (asyncCallbackInfo->wantAgent == nullptr) { + HILOG_INFO("GetWantAgent instance is nullptr..."); + } + int32_t code = Notification::WantAgent::WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); + std::lock_guard guard(g_mutex); + g_WantAgentMap.emplace(asyncCallbackInfo, code); +} + +void NAPI_GetWantAgentWrapCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetWantAgent compeleted(CallBack Mode)..."); + AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; + napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value callResult = nullptr; + + result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); + + napi_value wantAgentClass = nullptr; + napi_define_class(env, + "WantAgentClass", + NAPI_AUTO_LENGTH, + [](napi_env env, napi_callback_info info) -> napi_value { + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; + }, + nullptr, + 0, + nullptr, + &wantAgentClass); + napi_new_instance(env, wantAgentClass, 0, nullptr, &result[1]); + napi_wrap(env, + result[1], + (void *)asyncCallbackInfo->wantAgent.get(), + [](napi_env env, void *data, void *hint) {}, + nullptr, + nullptr); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); + + if (asyncCallbackInfo->callback[0] != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); +} + +void NAPI_GetWantAgentWrapPromiseCompleteCallBack(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetWantAgent compeleted(Promise Mode)..."); + AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; + napi_value wantAgentClass = nullptr; + napi_define_class(env, + "WantAgentClass", + NAPI_AUTO_LENGTH, + [](napi_env env, napi_callback_info info) -> napi_value { + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; + }, + nullptr, + 0, + nullptr, + &wantAgentClass); + napi_value result = nullptr; + napi_new_instance(env, wantAgentClass, 0, nullptr, &result); + napi_wrap(env, + result, + (void *)asyncCallbackInfo->wantAgent.get(), + [](napi_env env, void *data, void *hint) {}, + nullptr, + nullptr); + napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); +} + napi_value NAPI_GetWantAgentWrap( - napi_env env, napi_callback_info info, bool callBackMode, AsyncGetWantAgentCallbackInfo *asyncCallbackInfo) + napi_env env, napi_callback_info info, bool callBackMode, AsyncGetWantAgentCallbackInfo &asyncCallbackInfo) { HILOG_INFO("NAPI_GetWantAgentWrap called..."); if (callBackMode) { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetWantAgentCallBack", NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetWantAgent called(CallBack Mode)..."); - AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; - Notification::WantAgent::WantAgentInfo wantAgentInfo(asyncCallbackInfo->requestCode, - asyncCallbackInfo->operationType, - asyncCallbackInfo->wantAgentFlags, - asyncCallbackInfo->wants, - asyncCallbackInfo->extraInfo); - asyncCallbackInfo->wantAgent = - Notification::WantAgent::WantAgentHelper::GetWantAgent(asyncCallbackInfo->context, wantAgentInfo); - if (asyncCallbackInfo->wantAgent == nullptr) { - HILOG_INFO("GetWantAgent instance is nullptr..."); - } - int32_t code = Notification::WantAgent::WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); - std::lock_guard guard(g_mutex); - g_WantAgentMap.emplace(asyncCallbackInfo, code); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetWantAgent compeleted(CallBack Mode)..."); - AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; - napi_value result[NUMBER_OF_PARAMETERS_TWO] = {0}; - napi_value callback; - napi_value undefined; - napi_value callResult = 0; - - result[0] = GetCallbackErrorResult(asyncCallbackInfo->env, BUSINESS_ERROR_CODE_OK); - - napi_value wantAgentClass = nullptr; - napi_define_class(env, - "WantAgentClass", - NAPI_AUTO_LENGTH, - [](napi_env env, napi_callback_info info) -> napi_value { - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - return thisVar; - }, - nullptr, - 0, - nullptr, - &wantAgentClass); - napi_new_instance(env, wantAgentClass, 0, nullptr, &result[1]); - napi_wrap(env, - result[1], - (void *)asyncCallbackInfo->wantAgent.get(), - [](napi_env env, void *data, void *hint) {}, - nullptr, - nullptr); - napi_get_undefined(env, &undefined); - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, NUMBER_OF_PARAMETERS_TWO, &result[0], &callResult); - - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + NAPI_GetWantAgentWrapExecuteCallBack, + NAPI_GetWantAgentWrapCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo.asyncWork)); // create reutrn - napi_value ret = 0; + napi_value ret = nullptr; NAPI_CALL(env, napi_create_int32(env, 0, &ret)); return ret; } else { - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "NAPI_GetWantAgentPromise", NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise; + napi_deferred deferred = nullptr; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo.deferred = deferred; napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetWantAgent called(Promise Mode)..."); - AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; - HILOG_INFO("GetWantAgent wants.size = [%{public}zu], wantAgentFlags.size = [%{public}zu]", - asyncCallbackInfo->wants.size(), - asyncCallbackInfo->wantAgentFlags.size()); - - Notification::WantAgent::WantAgentInfo wantAgentInfo(asyncCallbackInfo->requestCode, - asyncCallbackInfo->operationType, - asyncCallbackInfo->wantAgentFlags, - asyncCallbackInfo->wants, - asyncCallbackInfo->extraInfo); - asyncCallbackInfo->wantAgent = - Notification::WantAgent::WantAgentHelper::GetWantAgent(asyncCallbackInfo->context, wantAgentInfo); - if (asyncCallbackInfo->wantAgent == nullptr) { - HILOG_INFO("GetWantAgent instance is nullptr..."); - } - int32_t code = Notification::WantAgent::WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); - std::lock_guard guard(g_mutex); - g_WantAgentMap.emplace(asyncCallbackInfo, code); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetWantAgent compeleted(Promise Mode)..."); - AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = (AsyncGetWantAgentCallbackInfo *)data; - napi_value wantAgentClass = nullptr; - napi_define_class(env, - "WantAgentClass", - NAPI_AUTO_LENGTH, - [](napi_env env, napi_callback_info info) -> napi_value { - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - return thisVar; - }, - nullptr, - 0, - nullptr, - &wantAgentClass); - napi_value result = nullptr; - napi_new_instance(env, wantAgentClass, 0, nullptr, &result); - napi_wrap(env, - result, - (void *)asyncCallbackInfo->wantAgent.get(), - [](napi_env env, void *data, void *hint) {}, - nullptr, - nullptr); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + NAPI_GetWantAgentWrapExecuteCallBack, + NAPI_GetWantAgentWrapPromiseCompleteCallBack, + (void *)&asyncCallbackInfo, + &asyncCallbackInfo.asyncWork); + napi_queue_async_work(env, asyncCallbackInfo.asyncWork); return promise; } } -napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info) +napi_value NAPI_GetWantAgentWants(napi_env env, napi_value jsWantAgentInfo, + std::vector> &wants, int32_t &operationType, int32_t &requestCode, + std::vector &wantAgentFlags, AAFwk::WantParams &extraInfo) { - size_t argc = NUMBER_OF_PARAMETERS_TWO; - napi_value argv[argc]; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); - HILOG_INFO("argc = [%{public}zu]", argc); - - napi_valuetype jsWantAgentInfoType; - napi_value jsWantAgentInfo = argv[0]; + napi_valuetype jsWantAgentInfoType = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, jsWantAgentInfo, &jsWantAgentInfoType)); NAPI_ASSERT(env, jsWantAgentInfoType == napi_object, "param type mismatch!"); - napi_value jsWants = nullptr; - jsWants = GetPropertyValueByPropertyName(env, jsWantAgentInfo, "wants", napi_object); - if (jsWants == nullptr) { - return NapiGetNull(env); - } + napi_value jsWants = GetPropertyValueByPropertyName(env, jsWantAgentInfo, "wants", napi_object); bool isArray = false; - if (napi_is_array(env, jsWants, &isArray) != napi_ok) { - return NapiGetNull(env); - } - if (isArray == false) { + if (jsWants == nullptr || napi_is_array(env, jsWants, &isArray) != napi_ok || !isArray) { return NapiGetNull(env); } + uint32_t wantsLen = 0; napi_get_array_length(env, jsWants, &wantsLen); - if (wantsLen < 0) { - return NapiGetNull(env); - } - std::vector> wants; for (uint32_t i = 0; i < wantsLen; i++) { std::shared_ptr want = std::make_shared(); - napi_value jsWant; + napi_value jsWant = nullptr; napi_get_element(env, jsWants, i, &jsWant); if (!UnwrapWant(env, jsWant, *want)) { return NapiGetNull(env); } - HILOG_INFO("want type is [%{public}s]", want->GetType().c_str()); wants.emplace_back(want); } + // Get operationType - int32_t operationType = -1; if (!UnwrapInt32ByPropertyName(env, jsWantAgentInfo, "operationType", operationType)) { return NapiGetNull(env); } // Get requestCode - int32_t requestCode = -1; if (!UnwrapInt32ByPropertyName(env, jsWantAgentInfo, "requestCode", requestCode)) { return NapiGetNull(env); } // Get wantAgentFlags - napi_value JsWantAgentFlags = nullptr; - std::vector wantAgentFlags; - JsWantAgentFlags = GetPropertyValueByPropertyName(env, jsWantAgentInfo, "wantAgentFlags", napi_object); - HILOG_INFO("NAPI_GetWantAgent8"); + napi_value JsWantAgentFlags = GetPropertyValueByPropertyName(env, jsWantAgentInfo, "wantAgentFlags", napi_object); if (JsWantAgentFlags != nullptr) { uint32_t arrayLength = 0; NAPI_CALL(env, napi_get_array_length(env, JsWantAgentFlags, &arrayLength)); HILOG_INFO("property is array, length=%{public}d", arrayLength); for (uint32_t i = 0; i < arrayLength; i++) { - napi_value napiWantAgentFlags; + napi_value napiWantAgentFlags = nullptr; napi_get_element(env, JsWantAgentFlags, i, &napiWantAgentFlags); - napi_valuetype valuetype0; + napi_valuetype valuetype0 = napi_valuetype::napi_null; NAPI_CALL(env, napi_typeof(env, napiWantAgentFlags, &valuetype0)); NAPI_ASSERT(env, valuetype0 == napi_number, "Wrong argument type. Numbers expected."); int32_t value0 = 0; @@ -1193,14 +1230,35 @@ napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info) } } // Get extraInfo - napi_value JsExtraInfo = nullptr; - JsExtraInfo = GetPropertyValueByPropertyName(env, jsWantAgentInfo, "extraInfo", napi_object); - AAFwk::WantParams extraInfo; + napi_value JsExtraInfo = GetPropertyValueByPropertyName(env, jsWantAgentInfo, "extraInfo", napi_object); if (JsExtraInfo != nullptr) { if (!UnwrapWantParams(env, JsExtraInfo, extraInfo)) { return NapiGetNull(env); } } + return NapiGetNull(env); +} + +napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info) +{ + size_t argc = NUMBER_OF_PARAMETERS_TWO; + napi_value argv[NUMBER_OF_PARAMETERS_TWO] = {}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + HILOG_INFO("argc = [%{public}zu]", argc); + napi_value jsWantAgentInfo = argv[0]; + + // Get wants + std::vector> wants = {}; + int32_t operationType = -1; + int32_t requestCode = -1; + std::vector wantAgentFlags = {}; + AAFwk::WantParams extraInfo = {}; + napi_value ret = + NAPI_GetWantAgentWants(env, jsWantAgentInfo, wants, operationType, requestCode, wantAgentFlags, extraInfo); + if (ret == nullptr) { + return NapiGetNull(env); + } + // Get context napi_value global = 0; NAPI_CALL(env, napi_get_global(env, &global)); @@ -1208,8 +1266,7 @@ napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); Ability *ability = nullptr; NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - std::shared_ptr context = nullptr; - context = ability->GetContext(); + std::shared_ptr context = ability->GetContext(); bool callBackMode = false; if (argc >= NUMBER_OF_PARAMETERS_TWO) { @@ -1219,8 +1276,11 @@ napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info) callBackMode = true; } - AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetWantAgentCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AsyncGetWantAgentCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncGetWantAgentCallbackInfo{ + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; if (asyncCallbackInfo == nullptr) { return NapiGetNull(env); } @@ -1235,7 +1295,7 @@ napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info) if (callBackMode) { napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback[0]); } - napi_value ret = NAPI_GetWantAgentWrap(env, info, callBackMode, asyncCallbackInfo); + ret = NAPI_GetWantAgentWrap(env, info, callBackMode, *asyncCallbackInfo); if (ret == nullptr) { delete asyncCallbackInfo; asyncCallbackInfo = nullptr; @@ -1256,9 +1316,8 @@ napi_value GetCallbackErrorResult(napi_env env, int errCode) napi_value NapiGetNull(napi_env env) { - napi_value result = 0; + napi_value result = nullptr; napi_get_null(env, &result); return result; } - } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/wantagent/napi_want_agent.h b/interfaces/kits/napi/wantagent/napi_want_agent.h index e5bf33c25f0fca3a67a4c56a935a904371d2d320..e3df2bd0772587bb68b040a533223cd131e849f6 100644 --- a/interfaces/kits/napi/wantagent/napi_want_agent.h +++ b/interfaces/kits/napi/wantagent/napi_want_agent.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "napi/native_common.h" #include "napi/native_node_api.h" @@ -33,7 +34,6 @@ #include "trigger_info.h" namespace OHOS { - using namespace OHOS::AppExecFwk; using namespace OHOS::Notification; using namespace OHOS::Notification::WantAgent; @@ -151,11 +151,13 @@ public: private: CallbackInfo triggerCompleteInfo_; + + static void OnSendFinishedUvAfterWorkCallbacck(uv_work_t *work, int status); }; napi_value WantAgentInit(napi_env env, napi_value exports); -void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName); +void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const std::string &propName); napi_value WantAgentFlagsInit(napi_env env, napi_value exports); napi_value WantAgentOperationTypeInit(napi_env env, napi_value exports); @@ -173,6 +175,5 @@ napi_value NapiGetNull(napi_env env); void DeleteRecordByCode(const int32_t code); static std::map g_WantAgentMap; static std::recursive_mutex g_mutex; - } // namespace OHOS #endif // NAPI_WANT_AGENT_H diff --git a/interfaces/kits/napi/wantagent/native_module.cpp b/interfaces/kits/napi/wantagent/native_module.cpp index 11981d2a2d2dae611c5c3ecfdb5a9d8fcaf8cc20..0177c32c6cfb67d60cce620ccc660bd8da784a02 100644 --- a/interfaces/kits/napi/wantagent/native_module.cpp +++ b/interfaces/kits/napi/wantagent/native_module.cpp @@ -44,13 +44,15 @@ EXTERN_C_END /* * Module define */ -static napi_module _module = {.nm_version = 1, +static napi_module _module = { + .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = Init, .nm_modname = "wantagent", .nm_priv = ((void *)0), - .reserved = {0}}; + .reserved = {0}, +}; /* * Module register function diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index f46776fb702e180c2027b57a8c5a7c28fcd20a27..f8fb3183094504668cc0c098cba8a3b57cf9c68b 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -674,7 +674,7 @@ bool NotificationPreferencesDatabase::StoreDeathRecipient() return true; } -template +template OHOS::DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t) { @@ -1234,4 +1234,4 @@ void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd( slot->EnableBypassDnd(enable); } } // namespace Notification -} // namespace OHOS +} // namespace OHOS \ No newline at end of file