diff --git a/frameworks/js/napi/include/manager/napi_push.h b/frameworks/js/napi/include/manager/napi_push.h index 3de12621e65fa3a8fcd1797b0800dc75c1206949..a0acc30cdfd0bde5038d81301191e74d7f7069d7 100644 --- a/frameworks/js/napi/include/manager/napi_push.h +++ b/frameworks/js/napi/include/manager/napi_push.h @@ -27,16 +27,14 @@ class NapiPush { public: NapiPush() = default; ~NapiPush() = default; - static void Finalizer(NativeEngine *engine, void *data, void *hint); - static NativeValue *RegisterPushCallback(NativeEngine *engine, NativeCallbackInfo *info); - static NativeValue *UnregisterPushCallback(NativeEngine *engine, NativeCallbackInfo *info); - + static void Finalizer(napi_env env, void *data, void *hint); + static napi_value RegisterPushCallback(napi_env env, napi_callback_info info); + static napi_value UnregisterPushCallback(napi_env env, napi_callback_info info); private: - NativeValue *OnRegisterPushCallback(NativeEngine &engine, const NativeCallbackInfo &info); - NativeValue *OnUnregisterPushCallback(NativeEngine &engine, const NativeCallbackInfo &info); - bool CheckCallerIsSystemApp(); - sptr jsPushCallBack_; + napi_value OnRegisterPushCallback(napi_env env, const napi_callback_info info); + napi_value OnUnregisterPushCallback(napi_env env, const napi_callback_info info); + bool CheckCallerIsSystemApp(); }; } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/manager/napi_push_callback.h b/frameworks/js/napi/include/manager/napi_push_callback.h index a94393cb9664502c7bbbe0ad509439ce7aea3589..5cb4b1dd5695c7719eb78acd9807766740c72e2b 100644 --- a/frameworks/js/napi/include/manager/napi_push_callback.h +++ b/frameworks/js/napi/include/manager/napi_push_callback.h @@ -34,18 +34,18 @@ namespace Notification { */ class JSPushCallBack : public PushCallBackStub { public: - JSPushCallBack(NativeEngine &engine); + JSPushCallBack(napi_env env); virtual ~JSPushCallBack(); bool OnCheckNotification(const std::string ¬ificationData); - void SetJsPushCallBackObject(NativeValue *pushCallBackObject); - bool IsEqualPushCallBackObject(NativeValue *pushCallBackObject); + void SetJsPushCallBackObject(napi_value pushCallBackObject); + bool IsEqualPushCallBackObject(napi_value pushCallBackObject); private: - bool ConvertFunctionResult(NativeValue *funcResult); + bool ConvertFunctionResult(napi_value funcResult); void ConvertJsonStringToValue( const std::string ¬ificationData, std::string &pkgName, int32_t ¬ifyId, int32_t &contentType); - NativeEngine &engine_; - std::unique_ptr pushCallBackObject_; + napi_env env_ = nullptr; + napi_ref pushCallBackObject_ = nullptr; std::mutex mutexlock; }; } // namespace Notification diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index 42f6d39160b5b5cf8a2dfa49ed776ea26ee0604a..232d2d689013626ec192cbdcdfad3ef30e80b449 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -37,26 +37,20 @@ using namespace OHOS::Notification; EXTERN_C_START -static NativeValue *NapiPushInit(NativeEngine *engine, NativeValue *exports) +static napi_value NapiPushInit(napi_env env, napi_value exports) { ANS_LOGD("called"); - if (engine == nullptr || exports == nullptr) { + if (env == nullptr || exports == nullptr) { ANS_LOGE("Invalid input parameters"); return nullptr; } - NativeObject *object = OHOS::AbilityRuntime::ConvertNativeValueTo(exports); - if (object == nullptr) { - ANS_LOGE("object is nullptr"); - return nullptr; - } - std::unique_ptr napiPush = std::make_unique(); - object->SetNativePointer(napiPush.release(), NapiPush::Finalizer, nullptr); + napi_wrap(env, exports, napiPush.release(), NapiPush::Finalizer, nullptr, nullptr); const char *moduleName = "NapiPush"; - OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "on", moduleName, NapiPush::RegisterPushCallback); - OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "off", moduleName, NapiPush::UnregisterPushCallback); + OHOS::AbilityRuntime::BindNativeFunction(env, exports, "on", moduleName, NapiPush::RegisterPushCallback); + OHOS::AbilityRuntime::BindNativeFunction(env, exports, "off", moduleName, NapiPush::UnregisterPushCallback); return exports; } @@ -111,8 +105,7 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - return reinterpret_cast( - NapiPushInit(reinterpret_cast(env), reinterpret_cast(exports))); + return NapiPushInit(env, exports); } /* diff --git a/frameworks/js/napi/src/manager/napi_push.cpp b/frameworks/js/napi/src/manager/napi_push.cpp index b55c4dfaed991c40ae54d6d159c30b382bb2012d..01e433888cfbba92b512c38abc25e46fbecca85d 100644 --- a/frameworks/js/napi/src/manager/napi_push.cpp +++ b/frameworks/js/napi/src/manager/napi_push.cpp @@ -30,100 +30,132 @@ constexpr int32_t INDEX_ONE = 1; } // namespace using namespace OHOS::AbilityRuntime; -void NapiPush::Finalizer(NativeEngine *engine, void *data, void *hint) +void NapiPush::Finalizer(napi_env env, void *data, void *hint) { ANS_LOGD("called"); delete static_cast(data); } -NativeValue *NapiPush::RegisterPushCallback(NativeEngine *engine, NativeCallbackInfo *info) +napi_value NapiPush::RegisterPushCallback(napi_env env, napi_callback_info info) { - NapiPush *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnRegisterPushCallback(*engine, *info) : nullptr; + NapiPush *me = CheckParamsAndGetThis(env, info); + return (me != nullptr) ? me->OnRegisterPushCallback(env, info) : nullptr; } -NativeValue *NapiPush::UnregisterPushCallback(NativeEngine *engine, NativeCallbackInfo *info) +napi_value NapiPush::UnregisterPushCallback(napi_env env, napi_callback_info info) { - NapiPush *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnUnregisterPushCallback(*engine, *info) : nullptr; + NapiPush *me = CheckParamsAndGetThis(env, info); + return (me != nullptr) ? me->OnUnregisterPushCallback(env, info) : nullptr; } -NativeValue *NapiPush::OnRegisterPushCallback(NativeEngine &engine, const NativeCallbackInfo &info) +napi_value NapiPush::OnRegisterPushCallback(napi_env env, const napi_callback_info info) { ANS_LOGD("called"); - - if (info.argc < ARGC_TWO) { + napi_value undefined = nullptr; + napi_get_undefined(env, &undefined); + + size_t argc = ARGC_TWO; + napi_value argv[ARGC_TWO] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < ARGC_TWO) { ANS_LOGE("The param is invalid."); - ThrowTooFewParametersError(engine); - return engine.CreateUndefined(); + ThrowTooFewParametersError(env); + return undefined; } - std::string type; - if (!ConvertFromJsValue(engine, info.argv[INDEX_ZERO], type) || type != "checkNotification") { + napi_valuetype valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[INDEX_ZERO], &valueType)); + if (valueType != napi_string) { ANS_LOGE("Parse type failed"); - ThrowError(engine, ERROR_PARAM_INVALID); - return engine.CreateUndefined(); + ThrowError(env, ERROR_PARAM_INVALID); + return undefined; + } + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[INDEX_ZERO], str, STR_MAX_SIZE - 1, &strLen)); + std::string type = str; + if (type != "checkNotification") { + ANS_LOGE("The type is not checkNotification"); + ThrowError(env, ERROR_PARAM_INVALID); + return undefined; } if (!CheckCallerIsSystemApp()) { - ThrowError(engine, ERROR_NOT_SYSTEM_APP); - return engine.CreateUndefined(); + ThrowError(env, ERROR_NOT_SYSTEM_APP); + return undefined; } if (jsPushCallBack_ == nullptr) { - jsPushCallBack_ = new (std::nothrow) OHOS::Notification::JSPushCallBack(engine); + jsPushCallBack_ = new (std::nothrow) OHOS::Notification::JSPushCallBack(env); if (jsPushCallBack_ == nullptr) { ANS_LOGE("new JSPushCallBack failed"); - ThrowError(engine, ERROR_INTERNAL_ERROR); - return engine.CreateUndefined(); + ThrowError(env, ERROR_INTERNAL_ERROR); + return undefined; } } - jsPushCallBack_->SetJsPushCallBackObject(info.argv[INDEX_ONE]); + jsPushCallBack_->SetJsPushCallBackObject(argv[INDEX_ONE]); NotificationHelper::RegisterPushCallback(jsPushCallBack_->AsObject()); - return engine.CreateUndefined(); + return undefined; } -NativeValue *NapiPush::OnUnregisterPushCallback(NativeEngine &engine, const NativeCallbackInfo &info) +napi_value NapiPush::OnUnregisterPushCallback(napi_env env, const napi_callback_info info) { ANS_LOGD("called"); - - if (info.argc < ARGC_ONE) { + napi_value undefined = nullptr; + napi_get_undefined(env, &undefined); + + size_t argc = ARGC_TWO; + napi_value argv[ARGC_TWO] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < ARGC_ONE) { ANS_LOGE("The param is invalid."); - ThrowTooFewParametersError(engine); - return engine.CreateUndefined(); + ThrowTooFewParametersError(env); + return undefined; } - std::string type; - if (!ConvertFromJsValue(engine, info.argv[INDEX_ZERO], type) || type != "checkNotification") { + napi_valuetype valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[INDEX_ZERO], &valueType)); + if (valueType != napi_string) { ANS_LOGE("Failed to parse type."); - ThrowError(engine, ERROR_PARAM_INVALID); - return engine.CreateUndefined(); + ThrowError(env, ERROR_PARAM_INVALID); + return undefined; + } + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[INDEX_ZERO], str, STR_MAX_SIZE - 1, &strLen)); + std::string type = str; + if (type != "checkNotification") { + ANS_LOGE("The type is not checkNotification"); + ThrowError(env, ERROR_PARAM_INVALID); + return undefined; } if (!CheckCallerIsSystemApp()) { - ThrowError(engine, ERROR_NOT_SYSTEM_APP); - return engine.CreateUndefined(); + ThrowError(env, ERROR_NOT_SYSTEM_APP); + return undefined; } if (jsPushCallBack_ == nullptr) { - ThrowError(engine, ERROR_INTERNAL_ERROR); + ThrowError(env, ERROR_INTERNAL_ERROR); ANS_LOGE("Never registered."); - return engine.CreateUndefined(); + return undefined; } - if (info.argc == ARGC_TWO) { - if (!jsPushCallBack_->IsEqualPushCallBackObject(info.argv[INDEX_ONE])) { + if (argc == ARGC_TWO) { + if (!jsPushCallBack_->IsEqualPushCallBackObject(argv[INDEX_ONE])) { ANS_LOGE("inconsistent with existing callback"); - ThrowError(engine, ERROR_PARAM_INVALID); - return engine.CreateUndefined(); + ThrowError(env, ERROR_PARAM_INVALID); + return undefined; } } NotificationHelper::UnregisterPushCallback(); delete jsPushCallBack_; jsPushCallBack_ = nullptr; - return engine.CreateUndefined(); + return undefined; } bool NapiPush::CheckCallerIsSystemApp() diff --git a/frameworks/js/napi/src/manager/napi_push_callback.cpp b/frameworks/js/napi/src/manager/napi_push_callback.cpp index 219e8dd35efa90be5b882374085ab9f1acdd586d..de49a182b05036002d2898e5e22b5ec48c76a7d0 100644 --- a/frameworks/js/napi/src/manager/napi_push_callback.cpp +++ b/frameworks/js/napi/src/manager/napi_push_callback.cpp @@ -25,40 +25,43 @@ namespace Notification { namespace { constexpr size_t ARGC_ONE = 1; } // namespace -JSPushCallBack::JSPushCallBack(NativeEngine &engine) : engine_(engine) {} +JSPushCallBack::JSPushCallBack(napi_env env) : env_(env) {} JSPushCallBack::~JSPushCallBack() {} -void JSPushCallBack::SetJsPushCallBackObject(NativeValue *pushCallBackObject) +void JSPushCallBack::SetJsPushCallBackObject(napi_value pushCallBackObject) { - pushCallBackObject_ = std::unique_ptr(engine_.CreateReference(pushCallBackObject, 1)); + napi_create_reference(env_, pushCallBackObject, 1, &pushCallBackObject_); } -bool JSPushCallBack::IsEqualPushCallBackObject(NativeValue *pushCallBackObject) +bool JSPushCallBack::IsEqualPushCallBackObject(napi_value pushCallBackObject) { if (pushCallBackObject_ == nullptr) { ANS_LOGE("pushCallBackObject_ nullptr"); return false; } - - NativeValue *value = pushCallBackObject_->Get(); + napi_value value = nullptr; + napi_get_reference_value(env_, pushCallBackObject_, &value); if (value == nullptr) { ANS_LOGE("Failed to get value"); return false; } - return value->StrictEquals(pushCallBackObject); + bool isEquals = false; + napi_strict_equals(env_, value, pushCallBackObject, &isEquals); + return isEquals; } bool JSPushCallBack::OnCheckNotification(const std::string ¬ificationData) { - AbilityRuntime::HandleEscape handleEscape(engine_); + AbilityRuntime::HandleEscape handleEscape(env_); if (pushCallBackObject_ == nullptr) { ANS_LOGE("pushCallBackObject_ nullptr"); return false; } - NativeValue *value = pushCallBackObject_->Get(); + napi_value value = nullptr; + napi_get_reference_value(env_, pushCallBackObject_, &value); if (value == nullptr) { ANS_LOGE("Failed to get value"); return false; @@ -70,15 +73,36 @@ bool JSPushCallBack::OnCheckNotification(const std::string ¬ificationData) ANS_LOGI( "pkgName=%{public}s, notifyId=%{public}d, contentType=%{public}d ", pkgName.c_str(), notifyId, contentType); - NativeValue *jsResult = engine_.CreateObject(); - NativeObject *result = AbilityRuntime::ConvertNativeValueTo(jsResult); - result->SetProperty("bundleName", AbilityRuntime::CreateJsValue(engine_, pkgName)); - result->SetProperty("notificationId", AbilityRuntime::CreateJsValue(engine_, notifyId)); - result->SetProperty("contentType", AbilityRuntime::CreateJsValue(engine_, contentType)); - - NativeValue *funcResult; - NativeValue *argv[] = { jsResult }; - funcResult = handleEscape.Escape(engine_.CallFunction(value, value, argv, ARGC_ONE)); + napi_value jsResult = nullptr; + napi_create_object(env_, &jsResult); + + std::string bundleName = "bundleName"; + napi_value bundleNameNapiValue = nullptr; + napi_value pkgNameNapiValue = nullptr; + napi_create_string_utf8(env_, bundleName.c_str(), bundleName.length(), &bundleNameNapiValue); + napi_create_string_utf8(env_, pkgName.c_str(), pkgName.length(), &pkgNameNapiValue); + napi_set_property(env_, jsResult, bundleNameNapiValue, pkgNameNapiValue); + + std::string notificationId = "notificationId"; + napi_value notifyIdNapiValue = nullptr; + napi_value notificationIdNapiValue = nullptr; + napi_create_string_utf8(env_, notificationId.c_str(), notificationId.length(), ¬ificationIdNapiValue); + napi_create_int32(env_, notifyId, ¬ifyIdNapiValue); + napi_set_property(env_, jsResult, notificationIdNapiValue, notifyIdNapiValue); + + std::string contentTypeName = "contentType"; + napi_value contentTypeValueNapiValue = nullptr; + napi_value contentTypeNameNapiValue = nullptr; + napi_create_string_utf8(env_, contentTypeName.c_str(), contentTypeName.length(), &contentTypeNameNapiValue); + napi_create_int32(env_, contentType, &contentTypeValueNapiValue); + napi_set_property(env_, jsResult, contentTypeNameNapiValue, contentTypeValueNapiValue); + + napi_value funcResult; + napi_value argv[] = { jsResult }; + + napi_value resultOut = nullptr; + napi_call_function(env_, value, value, ARGC_ONE, &argv[0], &resultOut); + funcResult = handleEscape.Escape(resultOut); return ConvertFunctionResult(funcResult); } @@ -116,39 +140,61 @@ void JSPushCallBack::ConvertJsonStringToValue( } } -bool JSPushCallBack::ConvertFunctionResult(NativeValue *funcResult) +bool JSPushCallBack::ConvertFunctionResult(napi_value funcResult) { - if (funcResult == nullptr || funcResult->TypeOf() != NativeValueType::NATIVE_OBJECT) { - ANS_LOGE("funcResult TypeOf error."); + if (funcResult == nullptr) { + ANS_LOGE("The funcResult is error."); return false; } - - NativeObject *obj = AbilityRuntime::ConvertNativeValueTo(funcResult); - if (obj == nullptr) { - ANS_LOGE("obj is nullptr."); + napi_valuetype valueType = napi_undefined; + NAPI_CALL_BASE(env_, napi_typeof(env_, funcResult, &valueType), false); + if (valueType != napi_object) { + ANS_LOGE("The funcResult is not napi_object."); return false; } - - auto codeJsvalue = obj->GetProperty("code"); - if (codeJsvalue == nullptr || codeJsvalue->TypeOf() != NativeValueType::NATIVE_NUMBER) { + bool hasProperty = false; + NAPI_CALL_BASE(env_, napi_has_named_property(env_, funcResult, "code", &hasProperty), false); + if (!hasProperty) { ANS_LOGE("GetProperty code failed."); return false; } + napi_value codeValue = nullptr; + napi_valuetype codeType = napi_undefined; + napi_get_named_property(env_, funcResult, "code", &codeValue); + NAPI_CALL_BASE(env_, napi_typeof(env_, codeValue, &codeType), false); + + if (codeType != napi_number) { + ANS_LOGE("GetProperty code failed. Number expected."); + return false; + } int32_t code = -1; - if (!AbilityRuntime::ConvertFromJsValue(engine_, codeJsvalue, code)) { + if (!AbilityRuntime::ConvertFromJsValue(env_, codeValue, code)) { ANS_LOGE("Parse code failed."); return false; } - auto messageJsvalue = obj->GetProperty("message"); - if (messageJsvalue == nullptr || messageJsvalue->TypeOf() != NativeValueType::NATIVE_STRING) { - ANS_LOGE("GetProperty message failed."); + bool hasMessageProperty = false; + NAPI_CALL_BASE(env_, napi_has_named_property(env_, funcResult, "message", &hasMessageProperty), false); + if (!hasMessageProperty) { + ANS_LOGE("Property message expected."); + return false; + } + + napi_value messageValue = nullptr; + napi_valuetype messageType = napi_undefined; + napi_get_named_property(env_, funcResult, "message", &messageValue); + NAPI_CALL_BASE(env_, napi_typeof(env_, messageValue, &messageType), false); + + if (messageType != napi_string) { + ANS_LOGE("GetProperty message failed. String expected."); + return false; } std::string message; - if (!AbilityRuntime::ConvertFromJsValue(engine_, messageJsvalue, message)) { + if (!AbilityRuntime::ConvertFromJsValue(env_, messageValue, message)) { ANS_LOGE("Parse message failed."); + return false; } ANS_LOGI("code : %{public}d ,message : %{public}s", code, message.c_str());