From f9898006bef8461a99d7a76beb4c658d92dc78fb Mon Sep 17 00:00:00 2001 From: yuanyulu Date: Tue, 26 Sep 2023 15:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E6=94=B9Native=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=BANapi=E6=8E=A5=E5=8F=A3=20Signed-off-by:yuanyulu@huawei?= =?UTF-8?q?.com?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanyulu --- .../extension/include/js_print_callback.h | 19 +- .../extension/include/js_print_extension.h | 6 +- .../include/js_print_extension_connection.h | 9 +- .../include/js_print_extension_context.h | 7 +- .../kits/extension/src/js_print_callback.cpp | 24 +- .../kits/extension/src/js_print_extension.cpp | 154 ++++---- .../src/js_print_extension_connection.cpp | 60 +-- .../src/js_print_extension_context.cpp | 373 +++++++++--------- 8 files changed, 321 insertions(+), 331 deletions(-) diff --git a/frameworks/kits/extension/include/js_print_callback.h b/frameworks/kits/extension/include/js_print_callback.h index f492ebb3..5ab10d20 100644 --- a/frameworks/kits/extension/include/js_print_callback.h +++ b/frameworks/kits/extension/include/js_print_callback.h @@ -23,9 +23,6 @@ #include "napi/native_api.h" -class NativeValue; -class NativeEngine; - namespace OHOS { namespace AbilityRuntime { class JsRuntime; @@ -33,23 +30,23 @@ class JsPrintCallback : public std::enable_shared_from_this { public: explicit JsPrintCallback(JsRuntime &jsRutime); ~JsPrintCallback() = default; - NativeValue *Exec(NativeValue *jsObj, const std::string &name, NativeValue *const *argv = nullptr, size_t argc = 0, + napi_value Exec(napi_value jsObj, const std::string &name, napi_value const *argv = nullptr, size_t argc = 0, bool isSync = true); private: uv_loop_s *GetJsLoop(JsRuntime &jsRuntime); - bool BuildJsWorker(NativeValue *jsObj, const std::string &name, - NativeValue *const *argv, size_t argc, bool isSync); + bool BuildJsWorker(napi_value jsObj, const std::string &name, + napi_value const *argv, size_t argc, bool isSync); private: struct JsWorkParam { std::shared_ptr self; - NativeEngine *nativeEngine; - NativeValue *jsObj; - NativeValue *jsMethod; - NativeValue *const *argv; + napi_env nativeEngine; + napi_value jsObj; + napi_value jsMethod; + napi_value const *argv; size_t argc; - NativeValue *jsResult; + napi_value jsResult; bool isSync; bool isCompleted; JsWorkParam() diff --git a/frameworks/kits/extension/include/js_print_extension.h b/frameworks/kits/extension/include/js_print_extension.h index 954ab2f6..128f8548 100644 --- a/frameworks/kits/extension/include/js_print_extension.h +++ b/frameworks/kits/extension/include/js_print_extension.h @@ -21,8 +21,6 @@ #include "print_job.h" class NativeReference; -class NativeValue; -class NativeObject; namespace OHOS { namespace AbilityRuntime { @@ -107,9 +105,9 @@ public: virtual void OnStop() override; private: - NativeValue *CallObjectMethod(const char *name, NativeValue *const *argv = nullptr, size_t argc = 0); + napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0); bool InitExtensionObj(JsRuntime &jsRuntime); - bool InitContextObj(JsRuntime &jsRuntime, NativeObject *&extObj, std::string &extensionId); + bool InitContextObj(JsRuntime &jsRuntime, napi_value &extObj, std::string &extensionId); void RegisterDiscoveryCb(); void RegisterConnectionCb(); void RegisterPrintJobCb(); diff --git a/frameworks/kits/extension/include/js_print_extension_connection.h b/frameworks/kits/extension/include/js_print_extension_connection.h index c17f3b80..56e11371 100644 --- a/frameworks/kits/extension/include/js_print_extension_connection.h +++ b/frameworks/kits/extension/include/js_print_extension_connection.h @@ -21,16 +21,15 @@ #include "ability_connect_callback.h" #include "event_handler.h" #include "print_extension_context.h" +#include "napi/native_api.h" -class NativeEngine; -class NativeValue; class NativeReference; namespace OHOS { namespace AbilityRuntime { class JSPrintExtensionConnection : public AbilityConnectCallback { public: - explicit JSPrintExtensionConnection(NativeEngine& engine); + explicit JSPrintExtensionConnection(napi_env engine); ~JSPrintExtensionConnection(); void OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; @@ -38,10 +37,10 @@ public: void HandleOnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode); void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode); - void SetJsConnectionObject(NativeValue* jsConnectionObject); + void SetJsConnectionObject(napi_value jsConnectionObject); void CallJsFailed(int32_t errorCode); private: - NativeEngine& engine_; + napi_env engine_; std::unique_ptr jsConnectionObject_ = nullptr; }; diff --git a/frameworks/kits/extension/include/js_print_extension_context.h b/frameworks/kits/extension/include/js_print_extension_context.h index 556b8959..57af7e38 100644 --- a/frameworks/kits/extension/include/js_print_extension_context.h +++ b/frameworks/kits/extension/include/js_print_extension_context.h @@ -21,14 +21,11 @@ #include "ability_connect_callback.h" #include "event_handler.h" #include "print_extension_context.h" - -class NativeEngine; -class NativeValue; -class NativeReference; +#include "napi/native_api.h" namespace OHOS { namespace AbilityRuntime { -NativeValue *CreateJsPrintExtensionContext(NativeEngine &engine, +napi_value CreateJsPrintExtensionContext(napi_env engine, std::shared_ptr context, std::string &extensionId); } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/kits/extension/src/js_print_callback.cpp b/frameworks/kits/extension/src/js_print_callback.cpp index 0cee1bd7..327d5c29 100644 --- a/frameworks/kits/extension/src/js_print_callback.cpp +++ b/frameworks/kits/extension/src/js_print_callback.cpp @@ -37,25 +37,27 @@ JsPrintCallback::JsPrintCallback(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime), uv_loop_s* JsPrintCallback::GetJsLoop(JsRuntime &jsRuntime) { - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); + napi_env env = jsRuntime_.GetNapiEnv(); uv_loop_s* loop = nullptr; - napi_get_uv_event_loop(reinterpret_cast(nativeEngine), &loop); + napi_get_uv_event_loop(env, &loop); if (loop == nullptr) { return nullptr; } return loop; } -bool JsPrintCallback::BuildJsWorker(NativeValue *jsObj, const std::string &name, - NativeValue *const *argv, size_t argc, bool isSync) +bool JsPrintCallback::BuildJsWorker(napi_value jsObj, const std::string &name, + napi_value const *argv, size_t argc, bool isSync) { - NativeObject *obj = ConvertNativeValueTo(jsObj); + napi_value obj = jsObj; if (obj == nullptr) { PRINT_HILOGE("Failed to get PrintExtension object"); return false; } - NativeValue *method = obj->GetProperty(name.c_str()); + napi_env env = jsRuntime_.GetNapiEnv(); + napi_value method = nullptr; + napi_get_named_property(env, obj, name.c_str(), &method); if (method == nullptr) { PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name.c_str()); return false; @@ -68,7 +70,7 @@ bool JsPrintCallback::BuildJsWorker(NativeValue *jsObj, const std::string &name, } jsParam_.self = shared_from_this(); - jsParam_.nativeEngine = &jsRuntime_.GetNativeEngine(); + jsParam_.nativeEngine = jsRuntime_.GetNapiEnv(); jsParam_.jsObj = jsObj; jsParam_.jsMethod = method; jsParam_.argv = argv; @@ -80,8 +82,8 @@ bool JsPrintCallback::BuildJsWorker(NativeValue *jsObj, const std::string &name, return true; } -NativeValue *JsPrintCallback::Exec( - NativeValue *jsObj, const std::string &name, NativeValue *const *argv, size_t argc, bool isSync) +napi_value JsPrintCallback::Exec( + napi_value jsObj, const std::string &name, napi_value const *argv, size_t argc, bool isSync) { PRINT_HILOGD("%{public}s callback in", name.c_str()); HandleScope handleScope(jsRuntime_); @@ -99,8 +101,8 @@ NativeValue *JsPrintCallback::Exec( [](uv_work_t *work, int statusInt) { auto jsWorkParam = reinterpret_cast(work->data); if (jsWorkParam != nullptr) { - jsWorkParam->jsResult = jsWorkParam->nativeEngine->CallFunction( - jsWorkParam->jsObj, jsWorkParam->jsMethod, jsWorkParam->argv, jsWorkParam->argc); + napi_call_function(jsWorkParam->nativeEngine, jsWorkParam->jsObj, + jsWorkParam->jsMethod, jsWorkParam->argc, jsWorkParam->argv, &(jsWorkParam->jsResult)); jsWorkParam->isCompleted = true; if (jsWorkParam->isSync) { jsWorkParam->self = nullptr; diff --git a/frameworks/kits/extension/src/js_print_extension.cpp b/frameworks/kits/extension/src/js_print_extension.cpp index 52e89c42..b3e3ee43 100644 --- a/frameworks/kits/extension/src/js_print_extension.cpp +++ b/frameworks/kits/extension/src/js_print_extension.cpp @@ -62,8 +62,7 @@ void JsPrintExtension::Init(const std::shared_ptr &record, return; } - PRINT_HILOGD("JsPrintExtension::Init ConvertNativeValueTo."); - NativeObject *obj = ConvertNativeValueTo(jsObj_->Get()); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get JsPrintExtension object"); return; @@ -99,7 +98,7 @@ bool JsPrintExtension::InitExtensionObj(JsRuntime &jsRuntime) return true; } -bool JsPrintExtension::InitContextObj(JsRuntime &jsRuntime, NativeObject *&extObj, std::string &extensionId) +bool JsPrintExtension::InitContextObj(JsRuntime &jsRuntime, napi_value &extObj, std::string &extensionId) { auto context = GetContext(); if (context == nullptr) { @@ -107,28 +106,25 @@ bool JsPrintExtension::InitContextObj(JsRuntime &jsRuntime, NativeObject *&extOb return false; } PRINT_HILOGD("CreateJsPrintExtensionContext."); - auto &engine = jsRuntime.GetNativeEngine(); - NativeValue *contextObj = CreateJsPrintExtensionContext(engine, context, extensionId); + napi_env engine = jsRuntime.GetNapiEnv(); + napi_value contextObj = CreateJsPrintExtensionContext(engine, context, extensionId); auto shellContextRef = jsRuntime.LoadSystemModule("PrintExtensionContext", &contextObj, NapiPrintUtils::ARGC_ONE); - contextObj = shellContextRef->Get(); + contextObj = shellContextRef->GetNapiValue(); PRINT_HILOGD("JsPrintExtension::Init Bind."); context->Bind(jsRuntime, shellContextRef.release()); - PRINT_HILOGD("JsPrintExtension::SetProperty."); - extObj->SetProperty("context", contextObj); - - auto nativeObj = ConvertNativeValueTo(contextObj); - if (nativeObj == nullptr) { + PRINT_HILOGD("JsPrintExtension::napi_set_named_property."); + napi_set_named_property(engine, extObj, "context", contextObj); + if (contextObj == nullptr) { PRINT_HILOGE("Failed to get Print extension native object"); return false; } - nativeObj->SetNativePointer( - new std::weak_ptr(context), - [](NativeEngine *, void *data, void *) { + napi_wrap(engine, contextObj, new std::weak_ptr(context), + [](napi_env, void *data, void *) { PRINT_HILOGD("Finalizer for weak_ptr Print extension context is called"); delete static_cast *>(data); }, - nullptr); + nullptr, nullptr); return true; } @@ -137,10 +133,9 @@ void JsPrintExtension::OnStart(const AAFwk::Want &want) Extension::OnStart(want); PRINT_HILOGD("jws JsPrintExtension OnStart begin.."); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); - NativeValue *argv[] = { nativeWant }; + napi_env nativeEngine = jsRuntime_.GetNapiEnv(); + napi_value nativeWant = OHOS::AppExecFwk::WrapWant(nativeEngine, want); + napi_value argv[] = { nativeWant }; CallObjectMethod("onCreate", argv, NapiPrintUtils::ARGC_ONE); RegisterCb(); PrintManagerClient::GetInstance()->LoadExtSuccess(extensionId_); @@ -177,34 +172,33 @@ sptr JsPrintExtension::OnConnect(const AAFwk::Want &want) Extension::OnConnect(want); PRINT_HILOGD("%{public}s begin.", __func__); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); - NativeValue *argv[] = { nativeWant }; + napi_env nativeEngine = jsRuntime_.GetNapiEnv(); + napi_value nativeWant = OHOS::AppExecFwk::WrapWant(nativeEngine, want); + napi_value argv[] = { nativeWant }; if (!jsObj_) { PRINT_HILOGW("Not found PrintExtension.js"); return nullptr; } - NativeValue *value = jsObj_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get PrintExtension object"); return nullptr; } - NativeValue *method = obj->GetProperty("onConnect"); + napi_value method = nullptr; + napi_get_named_property(nativeEngine, obj, "onConnect", &method); if (method == nullptr) { PRINT_HILOGE("Failed to get onConnect from PrintExtension object"); return nullptr; } - PRINT_HILOGD("JsPrintExtension::CallFunction onConnect, success"); - NativeValue *remoteNative = nativeEngine->CallFunction(value, method, argv, NapiPrintUtils::ARGC_ONE); + PRINT_HILOGD("JsPrintExtension::napi_call_function onConnect, success"); + napi_value remoteNative = nullptr; + napi_call_function(nativeEngine, obj, method, NapiPrintUtils::ARGC_ONE, argv, &remoteNative); if (remoteNative == nullptr) { PRINT_HILOGE("remoteNative nullptr."); } - auto remoteObj = NAPI_ohos_rpc_getNativeRemoteObject( - reinterpret_cast(nativeEngine), reinterpret_cast(remoteNative)); + auto remoteObj = NAPI_ohos_rpc_getNativeRemoteObject(nativeEngine, remoteNative); if (remoteObj == nullptr) { PRINT_HILOGE("remoteObj nullptr."); } @@ -217,28 +211,28 @@ void JsPrintExtension::OnDisconnect(const AAFwk::Want &want) Extension::OnDisconnect(want); PRINT_HILOGD("%{public}s begin.", __func__); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); - NativeValue *argv[] = { nativeWant }; + napi_env nativeEngine = jsRuntime_.GetNapiEnv(); + napi_value nativeWant = OHOS::AppExecFwk::WrapWant(nativeEngine, want); + napi_value argv[] = { nativeWant }; if (!jsObj_) { PRINT_HILOGW("Not found PrintExtension.js"); return; } - NativeValue *value = jsObj_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get PrintExtension object"); return; } - NativeValue *method = obj->GetProperty("onDisconnect"); + napi_value method = nullptr; + napi_get_named_property(nativeEngine, obj, "onDisconnect", &method); if (method == nullptr) { PRINT_HILOGE("Failed to get onDisconnect from PrintExtension object"); return; } - nativeEngine->CallFunction(value, method, argv, NapiPrintUtils::ARGC_ONE); + napi_value callResult = nullptr; + napi_call_function(nativeEngine, obj, method, NapiPrintUtils::ARGC_ONE, argv, &callResult); PRINT_HILOGD("%{public}s end.", __func__); } @@ -256,7 +250,7 @@ void JsPrintExtension::OnCommand(const AAFwk::Want &want, bool restart, int star PRINT_HILOGD("%{public}s end.", __func__); } -NativeValue *JsPrintExtension::CallObjectMethod(const char *name, NativeValue *const *argv, size_t argc) +napi_value JsPrintExtension::CallObjectMethod(const char *name, napi_value const *argv, size_t argc) { PRINT_HILOGD("jws JsPrintExtension::CallObjectMethod(%{public}s), begin", name); @@ -266,22 +260,24 @@ NativeValue *JsPrintExtension::CallObjectMethod(const char *name, NativeValue *c } HandleScope handleScope(jsRuntime_); - auto &nativeEngine = jsRuntime_.GetNativeEngine(); + napi_env nativeEngine = jsRuntime_.GetNapiEnv(); - NativeValue *value = jsObj_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get PrintExtension object"); return nullptr; } - NativeValue *method = obj->GetProperty(name); + napi_value method = nullptr; + napi_get_named_property(nativeEngine, obj, name, &method); if (method == nullptr) { PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name); return nullptr; } - PRINT_HILOGD("JsPrintExtension::CallFunction(%{public}s), success", name); - return nativeEngine.CallFunction(value, method, argv, argc); + PRINT_HILOGD("JsPrintExtension::napi_call_function(%{public}s), success", name); + napi_value callResult = nullptr; + napi_call_function(nativeEngine, obj, method, argc, argv, &callResult); + return callResult; } void JsPrintExtension::GetSrcPath(std::string &srcPath) @@ -314,7 +310,7 @@ void JsPrintExtension::RegisterDiscoveryCb() PRINT_HILOGD("Start Print Discovery"); HandleScope handleScope(jsExtension_->jsRuntime_); auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onStartDiscoverPrinter"); return true; }); @@ -323,7 +319,7 @@ void JsPrintExtension::RegisterDiscoveryCb() PRINT_HILOGD("Stop Print Discovery"); HandleScope handleScope(jsExtension_->jsRuntime_); auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onStopDiscoverPrinter"); return true; }); @@ -336,13 +332,12 @@ void JsPrintExtension::RegisterConnectionCb() PRINT_HILOGD("Connect Printer"); std::string realPrinterId = PrintUtils::GetLocalId(printId, jsExtension_->extensionId_); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jsPrintId = - OHOS::AppExecFwk::WrapStringToJS(reinterpret_cast(nativeEng), realPrinterId); - NativeValue *nativePrintId = reinterpret_cast(jsPrintId); - NativeValue *arg[] = { nativePrintId }; + napi_env nativeEng = (jsExtension_->jsRuntime_).GetNapiEnv(); + napi_value nativePrintId = + OHOS::AppExecFwk::WrapStringToJS(nativeEng, realPrinterId); + napi_value arg[] = { nativePrintId }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onConnectPrinter", arg, NapiPrintUtils::ARGC_ONE); return true; }); @@ -351,13 +346,12 @@ void JsPrintExtension::RegisterConnectionCb() PRINT_HILOGD("Disconnect Printer"); std::string realPrinterId = PrintUtils::GetLocalId(printId, jsExtension_->extensionId_); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jsPrintId = - OHOS::AppExecFwk::WrapStringToJS(reinterpret_cast(nativeEng), realPrinterId); - NativeValue *nativePrintId = reinterpret_cast(jsPrintId); - NativeValue *arg[] = { nativePrintId }; + napi_env nativeEng = (jsExtension_->jsRuntime_).GetNapiEnv(); + napi_value nativePrintId = + OHOS::AppExecFwk::WrapStringToJS(nativeEng, realPrinterId); + napi_value arg[] = { nativePrintId }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onDisconnectPrinter", arg, NapiPrintUtils::ARGC_ONE); return true; }); @@ -369,13 +363,12 @@ void JsPrintExtension::RegisterPrintJobCb() [](const PrintJob &job) -> bool { PRINT_HILOGD("Start Print Job"); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jobObject = PrintJobHelper::MakeJsObject(reinterpret_cast(nativeEng), job); - NativeValue *nativeJob = reinterpret_cast(jobObject); - NativeValue *arg[] = { nativeJob }; + napi_env nativeEng = (jsExtension_->jsRuntime_).GetNapiEnv(); + napi_value nativeJob = PrintJobHelper::MakeJsObject(nativeEng, job); + napi_value arg[] = { nativeJob }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onStartPrintJob", arg, NapiPrintUtils::ARGC_ONE); return true; }); @@ -383,13 +376,12 @@ void JsPrintExtension::RegisterPrintJobCb() [](const PrintJob &job) -> bool { PRINT_HILOGD("Cancel Print Job"); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jobObject = PrintJobHelper::MakeJsObject(reinterpret_cast(nativeEng), job); - NativeValue *nativeJob = reinterpret_cast(jobObject); - NativeValue *arg[] = { nativeJob }; + napi_env nativeEng = (jsExtension_->jsRuntime_).GetNapiEnv(); + napi_value nativeJob = PrintJobHelper::MakeJsObject(nativeEng, job); + napi_value arg[] = { nativeJob }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onCancelPrintJob", arg, NapiPrintUtils::ARGC_ONE); return true; }); @@ -401,13 +393,12 @@ void JsPrintExtension::RegisterPreviewCb() [](const PrintJob &job) -> bool { PRINT_HILOGD("Requet preview"); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jobObject = PrintJobHelper::MakeJsObject(reinterpret_cast(nativeEng), job); - NativeValue *nativeJob = reinterpret_cast(jobObject); - NativeValue *arg[] = { nativeJob }; + napi_env nativeEng = (jsExtension_->jsRuntime_).GetNapiEnv(); + napi_value nativeJob = PrintJobHelper::MakeJsObject(nativeEng, job); + napi_value arg[] = { nativeJob }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onRequestPreview", arg, NapiPrintUtils::ARGC_ONE); return true; }); @@ -420,13 +411,12 @@ void JsPrintExtension::RegisterQueryCapCb() PRINT_HILOGD("Request Capability"); std::string realPrinterId = PrintUtils::GetLocalId(printId, jsExtension_->extensionId_); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jsPrintId = - OHOS::AppExecFwk::WrapStringToJS(reinterpret_cast(nativeEng), realPrinterId); - NativeValue *nativePrintId = reinterpret_cast(jsPrintId); - NativeValue *arg[] = { nativePrintId }; + napi_env nativeEng = (jsExtension_->jsRuntime_).GetNapiEnv(); + napi_value nativePrintId = + OHOS::AppExecFwk::WrapStringToJS(nativeEng, realPrinterId); + napi_value arg[] = { nativePrintId }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onRequestPrinterCapability", arg, NapiPrintUtils::ARGC_ONE); PRINT_HILOGD("Request Capability Success"); return true; @@ -439,9 +429,9 @@ void JsPrintExtension::RegisterExtensionCb() []() -> bool { PRINT_HILOGD("Stop Extension"); HandleScope handleScope(jsExtension_->jsRuntime_); - NativeValue *arg[] = { }; + napi_value arg[] = { }; auto callback = std::make_shared(jsExtension_->jsRuntime_); - NativeValue *value = jsExtension_->jsObj_->Get(); + napi_value value = jsExtension_->jsObj_->GetNapiValue(); callback->Exec(value, "onDestroy", arg, NapiPrintUtils::ARGC_ZERO); jsExtension_->hasDestroyed_ = true; PRINT_HILOGD("Destroy Extension Success"); diff --git a/frameworks/kits/extension/src/js_print_extension_connection.cpp b/frameworks/kits/extension/src/js_print_extension_connection.cpp index a33cbaeb..9c5460e9 100644 --- a/frameworks/kits/extension/src/js_print_extension_connection.cpp +++ b/frameworks/kits/extension/src/js_print_extension_connection.cpp @@ -31,7 +31,7 @@ using namespace OHOS::Print; namespace OHOS { namespace AbilityRuntime { -JSPrintExtensionConnection::JSPrintExtensionConnection(NativeEngine& engine) : engine_(engine) {} +JSPrintExtensionConnection::JSPrintExtensionConnection(napi_env engine) : engine_(engine) {} JSPrintExtensionConnection::~JSPrintExtensionConnection() = default; @@ -60,32 +60,30 @@ void JSPrintExtensionConnection::HandleOnAbilityConnectDone(const AppExecFwk::El { PRINT_HILOGD("HandleOnAbilityConnectDone begin, resultCode:%{public}d", resultCode); // wrap ElementName - napi_value napiElementName = OHOS::AppExecFwk::WrapElementName(reinterpret_cast(&engine_), element); - NativeValue* nativeElementName = reinterpret_cast(napiElementName); + napi_value nativeElementName = OHOS::AppExecFwk::WrapElementName(engine_, element); // wrap RemoteObject PRINT_HILOGD("OnAbilityConnectDone begin NAPI_ohos_rpc_CreateJsRemoteObject"); - napi_value napiRemoteObject = NAPI_ohos_rpc_CreateJsRemoteObject( - reinterpret_cast(&engine_), remoteObject); - NativeValue* nativeRemoteObject = reinterpret_cast(napiRemoteObject); - NativeValue* argv[] = {nativeElementName, nativeRemoteObject}; + napi_value nativeRemoteObject = NAPI_ohos_rpc_CreateJsRemoteObject(engine_, remoteObject); + napi_value argv[] = {nativeElementName, nativeRemoteObject}; if (jsConnectionObject_ == nullptr) { PRINT_HILOGE("jsConnectionObject_ nullptr"); return; } - NativeValue* value = jsConnectionObject_->Get(); - NativeObject* obj = ConvertNativeValueTo(value); + napi_value obj = jsConnectionObject_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get object"); return; } - NativeValue* methodOnConnect = obj->GetProperty("onConnect"); + napi_value methodOnConnect = nullptr; + napi_get_named_property(engine_, obj, "onConnect", &methodOnConnect); if (methodOnConnect == nullptr) { PRINT_HILOGE("Failed to get onConnect from object"); return; } - PRINT_HILOGD("JSPrintExtensionConnection::CallFunction onConnect, success"); - engine_.CallFunction(value, methodOnConnect, argv, NapiPrintUtils::ARGC_TWO); + PRINT_HILOGD("JSPrintExtensionConnection::napi_call_function onConnect, success"); + napi_value callResult = nullptr; + napi_call_function(engine_, obj, methodOnConnect, NapiPrintUtils::ARGC_TWO, argv, &callResult); PRINT_HILOGD("OnAbilityConnectDone end"); } @@ -112,21 +110,20 @@ void JSPrintExtensionConnection::HandleOnAbilityDisconnectDone(const AppExecFwk: int resultCode) { PRINT_HILOGD("HandleOnAbilityDisconnectDone begin, resultCode:%{public}d", resultCode); - napi_value napiElementName = OHOS::AppExecFwk::WrapElementName(reinterpret_cast(&engine_), element); - NativeValue* nativeElementName = reinterpret_cast(napiElementName); - NativeValue* argv[] = {nativeElementName}; + napi_value nativeElementName = OHOS::AppExecFwk::WrapElementName(engine_, element); + napi_value argv[] = {nativeElementName}; if (jsConnectionObject_ == nullptr) { PRINT_HILOGE("jsConnectionObject_ nullptr"); return; } - NativeValue* value = jsConnectionObject_->Get(); - NativeObject* obj = ConvertNativeValueTo(value); + napi_value obj = jsConnectionObject_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get object"); return; } - NativeValue* method = obj->GetProperty("onDisconnect"); + napi_value method = nullptr; + napi_get_named_property(engine_, obj, "onDisconnect", &method); if (method == nullptr) { PRINT_HILOGE("Failed to get onDisconnect from object"); return; @@ -148,13 +145,16 @@ void JSPrintExtensionConnection::HandleOnAbilityDisconnectDone(const AppExecFwk: connects_.erase(item); PRINT_HILOGD("OnAbilityDisconnectDone erase connects_.size:%{public}zu", connects_.size()); } - PRINT_HILOGD("OnAbilityDisconnectDone CallFunction success"); - engine_.CallFunction(value, method, argv, NapiPrintUtils::ARGC_ONE); + PRINT_HILOGD("OnAbilityDisconnectDone napi_call_function success"); + napi_value callResult = nullptr; + napi_call_function(engine_, obj, method, NapiPrintUtils::ARGC_ONE, argv, &callResult); } -void JSPrintExtensionConnection::SetJsConnectionObject(NativeValue* jsConnectionObject) +void JSPrintExtensionConnection::SetJsConnectionObject(napi_value jsConnectionObject) { - jsConnectionObject_ = std::unique_ptr(engine_.CreateReference(jsConnectionObject, 1)); + napi_ref jsRef = nullptr; + napi_create_reference(engine_, jsConnectionObject, 1, &jsRef); + jsConnectionObject_.reset(reinterpret_cast(jsRef)); } void JSPrintExtensionConnection::CallJsFailed(int32_t errorCode) @@ -164,21 +164,25 @@ void JSPrintExtensionConnection::CallJsFailed(int32_t errorCode) PRINT_HILOGE("jsConnectionObject_ nullptr"); return; } - NativeValue* value = jsConnectionObject_->Get(); - NativeObject* obj = ConvertNativeValueTo(value); + napi_value obj = jsConnectionObject_->GetNapiValue(); if (obj == nullptr) { PRINT_HILOGE("Failed to get object"); return; } - NativeValue* method = obj->GetProperty("onFailed"); + napi_value method = nullptr; + napi_get_named_property(engine_, obj, "onFailed", &method); if (method == nullptr) { PRINT_HILOGE("Failed to get onFailed from object"); return; } - NativeValue* argv[] = {engine_.CreateNumber(errorCode)}; - PRINT_HILOGD("CallJsFailed CallFunction success"); - engine_.CallFunction(value, method, argv, NapiPrintUtils::ARGC_ONE); + + napi_value result = nullptr; + napi_create_int32(engine_, errorCode, &result); + napi_value argv[] = { result }; + PRINT_HILOGD("CallJsFailed napi_call_function success"); + napi_value callResult = nullptr; + napi_call_function(engine_, obj, method, NapiPrintUtils::ARGC_ONE, argv, &callResult); PRINT_HILOGD("CallJsFailed end"); } } // namespace AbilityRuntime diff --git a/frameworks/kits/extension/src/js_print_extension_context.cpp b/frameworks/kits/extension/src/js_print_extension_context.cpp index 0ae6c767..baa201fa 100644 --- a/frameworks/kits/extension/src/js_print_extension_context.cpp +++ b/frameworks/kits/extension/src/js_print_extension_context.cpp @@ -42,79 +42,93 @@ public: explicit JsPrintExtensionContext(const std::shared_ptr& context) : context_(context) {} ~JsPrintExtensionContext() = default; - static void Finalizer(NativeEngine *engine, noneType data, noneType hint) + static void Finalizer(napi_env engine, noneType data, noneType hint) { PRINT_HILOGD("JsAbilityContext::Finalizer is called"); std::unique_ptr(static_cast(data)); } - static NativeValue *StartAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value StartAbility(napi_env engine, napi_callback_info info) { JsPrintExtensionContext* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnStartAbility(*engine, *info) : nullptr; + return (me != nullptr) ? me->OnStartAbility(engine, info) : nullptr; } - static NativeValue *StartAbilityWithAccount(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value StartAbilityWithAccount(napi_env engine, napi_callback_info info) { JsPrintExtensionContext* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnStartAbilityWithAccount(*engine, *info) : nullptr; + return (me != nullptr) ? me->OnStartAbilityWithAccount(engine, info) : nullptr; } - static NativeValue *ConnectAbilityWithAccount(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value ConnectAbilityWithAccount(napi_env engine, napi_callback_info info) { JsPrintExtensionContext* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnConnectAbilityWithAccount(*engine, *info) : nullptr; + return (me != nullptr) ? me->OnConnectAbilityWithAccount(engine, info) : nullptr; } - static NativeValue *TerminateAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value TerminateAbility(napi_env engine, napi_callback_info info) { JsPrintExtensionContext* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnTerminateAbility(*engine, *info) : nullptr; + return (me != nullptr) ? me->OnTerminateAbility(engine, info) : nullptr; } - static NativeValue *ConnectAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value ConnectAbility(napi_env engine, napi_callback_info info) { JsPrintExtensionContext* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnConnectAbility(*engine, *info) : nullptr; + return (me != nullptr) ? me->OnConnectAbility(engine, info) : nullptr; } - static NativeValue *DisconnectAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value DisconnectAbility(napi_env engine, napi_callback_info info) { JsPrintExtensionContext* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnDisconnectAbility(*engine, *info) : nullptr; + return (me != nullptr) ? me->OnDisconnectAbility(engine, info) : nullptr; } private: std::weak_ptr context_; - NativeValue *OnStartAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value GetUndefinedValue(napi_env &engine) + { + PRINT_HILOGE("params count or value is error"); + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + return undefineResult; + } + + napi_valuetype GetNapiValueType(napi_env &engine, napi_value &argv) + { + napi_valuetype valueType = napi_undefined; + napi_typeof(engine, argv, &valueType); + return valueType; + } + + napi_value OnStartAbility(napi_env &engine, napi_callback_info &info) { PRINT_HILOGD("OnStartAbility is called"); - // only support one or two or three params - if (info.argc != NapiPrintUtils::ARGC_ONE && info.argc != NapiPrintUtils::ARGC_TWO && - info.argc != NapiPrintUtils::ARGC_THREE) { - PRINT_HILOGE("Not enough params"); - return engine.CreateUndefined(); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_get_cb_info(engine, info, &argc, argv, nullptr, nullptr); + if (argc != NapiPrintUtils::ARGC_ONE && argc != NapiPrintUtils::ARGC_TWO && + argc != NapiPrintUtils::ARGC_THREE) { + return GetUndefinedValue(engine); } - decltype(info.argc) unwrapArgc = 0; + decltype(argc) unwrapArgc = 0; AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant(reinterpret_cast(&engine), - reinterpret_cast(info.argv[NapiPrintUtils::INDEX_ZERO]), want); + OHOS::AppExecFwk::UnwrapWant(engine, argv[NapiPrintUtils::INDEX_ZERO], want); PRINT_HILOGD("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); unwrapArgc++; AAFwk::StartOptions startOptions; - if (info.argc > NapiPrintUtils::ARGC_ONE && info.argv[1]->TypeOf() == NATIVE_OBJECT) { + if (argc > NapiPrintUtils::ARGC_ONE && GetNapiValueType(engine, argv[1]) == napi_object) { PRINT_HILOGD("OnStartAbility start options is used."); - AppExecFwk::UnwrapStartOptions(reinterpret_cast(&engine), - reinterpret_cast(info.argv[1]), startOptions); + AppExecFwk::UnwrapStartOptions(engine, argv[1], startOptions); unwrapArgc++; } - AsyncTask::CompleteCallback complete = [weak = context_, want, startOptions, unwrapArgc]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, startOptions, unwrapArgc]( + napi_env engine, NapiAsyncTask &task, int32_t status) { PRINT_HILOGD("startAbility begin"); auto context = weak.lock(); if (!context) { @@ -127,98 +141,91 @@ private: (unwrapArgc == 1) ? errcode = context->StartAbility(want) : errcode = context->StartAbility(want, startOptions); if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + task.Resolve(engine, undefineResult); } else { task.Reject(engine, CreateJsError(engine, errcode, "Start Ability failed.")); } }; - NativeValue *lastParam = (info.argc == unwrapArgc) ? nullptr : info.argv[unwrapArgc]; - NativeValue *result = nullptr; - AsyncTask::Schedule("PrintExtensionContext::OnStartAbility", engine, + napi_value lastParam = (argc == unwrapArgc) ? nullptr : argv[unwrapArgc]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("PrintExtensionContext::OnStartAbility", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); return result; } - NativeValue *CheckInfo(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnStartAbilityWithAccount(napi_env &engine, napi_callback_info &info) { - PRINT_HILOGD("OnStartAbilityWithAccount is called"); - // only support two or three or four params - if (info.argc != NapiPrintUtils::ARGC_TWO && info.argc != NapiPrintUtils::ARGC_THREE && - info.argc != NapiPrintUtils::ARGC_FOUR) { - PRINT_HILOGE("Not enough params"); - return engine.CreateUndefined(); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_get_cb_info(engine, info, &argc, argv, nullptr, nullptr); + if (argc != NapiPrintUtils::ARGC_TWO && argc != NapiPrintUtils::ARGC_THREE && + argc != NapiPrintUtils::ARGC_FOUR) { + return GetUndefinedValue(engine); } - return nullptr; - } - NativeValue *OnStartAbilityWithAccount(NativeEngine &engine, NativeCallbackInfo &info) - { - CheckInfo(engine, info); - decltype(info.argc) unwrapArgc = 0; + decltype(argc) unwrapArgc = 0; AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant(reinterpret_cast(&engine), - reinterpret_cast(info.argv[0]), want); + OHOS::AppExecFwk::UnwrapWant(engine, argv[0], want); PRINT_HILOGD("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); unwrapArgc++; int32_t accountId = 0; - if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(reinterpret_cast(&engine), - reinterpret_cast(info.argv[1]), accountId)) { - PRINT_HILOGD("%{public}s called, the second parameter is invalid.", __func__); - return engine.CreateUndefined(); + if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(engine, argv[1], accountId)) { + return GetUndefinedValue(engine); } PRINT_HILOGD("%{public}d accountId:", accountId); unwrapArgc++; AAFwk::StartOptions startOptions; - if (info.argc > NapiPrintUtils::ARGC_TWO && info.argv[NapiPrintUtils::INDEX_TWO]->TypeOf() == NATIVE_OBJECT) { - PRINT_HILOGD("OnStartAbilityWithAccount start options is used."); - AppExecFwk::UnwrapStartOptions(reinterpret_cast(&engine), - reinterpret_cast(info.argv[NapiPrintUtils::INDEX_TWO]), startOptions); + if (static_cast(argc) > NapiPrintUtils::INDEX_TWO && + GetNapiValueType(engine, argv[NapiPrintUtils::INDEX_TWO]) == napi_object) { + AppExecFwk::UnwrapStartOptions(engine, argv[NapiPrintUtils::INDEX_TWO], startOptions); unwrapArgc++; } - AsyncTask::CompleteCallback complete = [weak = context_, want, accountId, startOptions, unwrapArgc]( - NativeEngine &engine, AsyncTask &task, int32_t status) { - PRINT_HILOGD("startAbility begin"); + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, accountId, startOptions, unwrapArgc]( + napi_env engine, NapiAsyncTask &task, int32_t status) { auto context = weak.lock(); if (!context) { - PRINT_HILOGW("context is released"); task.Reject(engine, CreateJsError(engine, E_PRINT_INVALID_CONTEXT, "Context is released")); return; } ErrCode errcode = ERR_OK; - (unwrapArgc == NapiPrintUtils::ARGC_TWO) - ? errcode = context->StartAbilityWithAccount(want, accountId) + (unwrapArgc == NapiPrintUtils::ARGC_TWO) ? errcode = context->StartAbilityWithAccount(want, accountId) : errcode = context->StartAbilityWithAccount(want, accountId, startOptions); if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + task.Resolve(engine, undefineResult); } else { task.Reject(engine, CreateJsError(engine, errcode, "Start Ability failed.")); } }; - NativeValue *lastParam = (info.argc == unwrapArgc) ? nullptr : info.argv[unwrapArgc]; - NativeValue *result = nullptr; - AsyncTask::Schedule("PrintExtensionContext::OnStartAbilityWithAccount", engine, + napi_value lastParam = (argc == unwrapArgc) ? nullptr : argv[unwrapArgc]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("PrintExtensionContext::OnStartAbilityWithAccount", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); return result; } - NativeValue *OnTerminateAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnTerminateAbility(napi_env &engine, napi_callback_info &info) { PRINT_HILOGD("OnTerminateAbility is called"); - // only support one or zero params - if (info.argc != NapiPrintUtils::ARGC_ZERO && info.argc != NapiPrintUtils::ARGC_ONE) { - PRINT_HILOGE("Not enough params"); - return engine.CreateUndefined(); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_get_cb_info(engine, info, &argc, argv, nullptr, nullptr); + if (argc != NapiPrintUtils::ARGC_ZERO && argc != NapiPrintUtils::ARGC_ONE) { + return GetUndefinedValue(engine); } - AsyncTask::CompleteCallback complete = [weak = context_]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_]( + napi_env engine, NapiAsyncTask &task, int32_t status) { PRINT_HILOGD("TerminateAbility begin"); auto context = weak.lock(); if (!context) { @@ -229,51 +236,46 @@ private: auto errcode = context->TerminateAbility(); if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + task.Resolve(engine, undefineResult); } else { task.Reject(engine, CreateJsError(engine, errcode, "Terminate Ability failed.")); } }; - NativeValue *lastParam = - (info.argc == NapiPrintUtils::ARGC_ZERO) ? nullptr : info.argv[0]; - NativeValue *result = nullptr; - AsyncTask::Schedule("PrintExtensionContext::OnTerminateAbility", engine, + napi_value lastParam = (argc == NapiPrintUtils::ARGC_ZERO) ? nullptr : argv[0]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("PrintExtensionContext::OnTerminateAbility", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); return result; } - NativeValue *OnConnectAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnConnectAbility(napi_env &engine, napi_callback_info &info) { PRINT_HILOGD("OnConnectAbility is called"); - // only support two params - if (info.argc != NapiPrintUtils::ARGC_TWO) { - PRINT_HILOGE("Not enough params"); - return engine.CreateUndefined(); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_get_cb_info(engine, info, &argc, argv, nullptr, nullptr); + if (argc != NapiPrintUtils::ARGC_TWO) { + return GetUndefinedValue(engine); } - // unwrap want AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant( - reinterpret_cast(&engine), reinterpret_cast( - info.argv[0]), want); + OHOS::AppExecFwk::UnwrapWant(engine, argv[0], want); PRINT_HILOGD("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); - // unwarp connection + sptr connection = new JSPrintExtensionConnection(engine); - connection->SetJsConnectionObject(info.argv[1]); + connection->SetJsConnectionObject(argv[1]); int64_t connectId = serialNumber_; ConnecttionKey key; key.id = serialNumber_; key.want = want; connects_.emplace(key, connection); - if (serialNumber_ < INT64_MAX) { - serialNumber_++; - } else { - serialNumber_ = 0; - } - AsyncTask::CompleteCallback complete = [weak = context_, want, connection, connectId]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + (serialNumber_ < INT64_MAX) ? serialNumber_++ : serialNumber_ = 0; + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, connection, connectId]( + napi_env engine, NapiAsyncTask &task, int32_t status) { PRINT_HILOGD("OnConnectAbility begin"); auto context = weak.lock(); if (!context) { @@ -285,52 +287,49 @@ private: if (!context->ConnectAbility(want, connection)) { connection->CallJsFailed(E_PRINT_INVALID_CONTEXT); } - task.Resolve(engine, engine.CreateUndefined()); + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + task.Resolve(engine, undefineResult); }; - NativeValue *result = nullptr; - AsyncTask::Schedule("PrintExtensionContext::OnConnectAbility", engine, + napi_value result = nullptr; + NapiAsyncTask::Schedule("PrintExtensionContext::OnConnectAbility", engine, CreateAsyncTaskWithLastParam(engine, nullptr, nullptr, std::move(complete), &result)); - return engine.CreateNumber(connectId); + napi_value numberResult = nullptr; + napi_create_double(engine, connectId, &numberResult); + return numberResult; } - NativeValue *OnConnectAbilityWithAccount(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnConnectAbilityWithAccount(napi_env &engine, napi_callback_info &info) { PRINT_HILOGD("OnConnectAbilityWithAccount is called"); - // only support three params - if (info.argc != NapiPrintUtils::ARGC_THREE) { - PRINT_HILOGE("Not enough params"); - return engine.CreateUndefined(); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_get_cb_info(engine, info, &argc, argv, nullptr, nullptr); + if (argc != NapiPrintUtils::ARGC_THREE) { + return GetUndefinedValue(engine); } - // unwrap want AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant(reinterpret_cast(&engine), - reinterpret_cast(info.argv[0]), want); + OHOS::AppExecFwk::UnwrapWant(engine, argv[0], want); PRINT_HILOGD("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); int32_t accountId = 0; - if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(reinterpret_cast(&engine), - reinterpret_cast(info.argv[1]), accountId)) { + if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(engine, argv[1], accountId)) { PRINT_HILOGD("%{public}s called, the second parameter is invalid.", __func__); - return engine.CreateUndefined(); + return GetUndefinedValue(engine); } - // unwarp connection sptr connection = new JSPrintExtensionConnection(engine); - connection->SetJsConnectionObject(info.argv[1]); + connection->SetJsConnectionObject(argv[1]); int64_t connectId = serialNumber_; ConnecttionKey key; key.id = serialNumber_; key.want = want; connects_.emplace(key, connection); - if (serialNumber_ < INT64_MAX) { - serialNumber_++; - } else { - serialNumber_ = 0; - } - AsyncTask::CompleteCallback complete = [weak = context_, want, accountId, connection, connectId]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + (serialNumber_ < INT64_MAX) ? serialNumber_++ : serialNumber_ = 0; + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, accountId, connection, connectId]( + napi_env engine, NapiAsyncTask &task, int32_t status) { PRINT_HILOGD("OnConnectAbilityWithAccount begin"); auto context = weak.lock(); if (!context) { @@ -342,30 +341,32 @@ private: if (!context->ConnectAbilityWithAccount(want, accountId, connection)) { connection->CallJsFailed(E_PRINT_INVALID_CONTEXT); } - task.Resolve(engine, engine.CreateUndefined()); + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + task.Resolve(engine, undefineResult); }; - NativeValue *result = nullptr; - AsyncTask::Schedule("PrintExtensionContext::OnConnectAbilityWithAccount", engine, + napi_value result = nullptr; + NapiAsyncTask::Schedule("PrintExtensionContext::OnConnectAbilityWithAccount", engine, CreateAsyncTaskWithLastParam(engine, nullptr, nullptr, std::move(complete), &result)); - return engine.CreateNumber(connectId); + napi_value numberResult = nullptr; + napi_create_double(engine, connectId, &numberResult); + return numberResult; } - NativeValue *OnDisconnectAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnDisconnectAbility(napi_env &engine, napi_callback_info &info) { PRINT_HILOGD("OnDisconnectAbility is called"); - // only support one or two params - if (info.argc != NapiPrintUtils::ARGC_ONE && info.argc != NapiPrintUtils::ARGC_TWO) { - PRINT_HILOGE("Not enough params"); - return engine.CreateUndefined(); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_get_cb_info(engine, info, &argc, argv, nullptr, nullptr); + if (argc != NapiPrintUtils::ARGC_ONE && argc != NapiPrintUtils::ARGC_TWO) { + return GetUndefinedValue(engine); } - // unwrap want AAFwk::Want want; - // unwrap connectId int64_t connectId = -1; sptr connection = nullptr; - napi_get_value_int64(reinterpret_cast(&engine), - reinterpret_cast(info.argv[NapiPrintUtils::INDEX_ZERO]), &connectId); + napi_get_value_int64(engine, argv[NapiPrintUtils::INDEX_ZERO], &connectId); PRINT_HILOGD("OnDisconnectAbility connection:%{public}d", (int32_t)connectId); auto item = std::find_if(connects_.begin(), connects_.end(), [&connectId](const std::map>::value_type &obj) { @@ -378,9 +379,8 @@ private: } else { PRINT_HILOGD("%{public}s not find conn exist.", __func__); } - // begin disconnect - AsyncTask::CompleteCallback complete = [weak = context_, want, connection]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, connection]( + napi_env engine, NapiAsyncTask &task, int32_t status) { PRINT_HILOGD("OnDisconnectAbility begin"); auto context = weak.lock(); if (!context) { @@ -395,94 +395,96 @@ private: } PRINT_HILOGD("context->DisconnectAbility"); auto errcode = context->DisconnectAbility(want, connection); - errcode == 0 ? task.Resolve(engine, engine.CreateUndefined()) + napi_value undefineResult = nullptr; + napi_get_undefined(engine, &undefineResult); + errcode == 0 ? task.Resolve(engine, undefineResult) : task.Reject(engine, CreateJsError(engine, errcode, "Disconnect Ability failed.")); }; - NativeValue *lastParam = - (info.argc == NapiPrintUtils::ARGC_ONE) ? nullptr : info.argv[1]; - NativeValue *result = nullptr; - AsyncTask::Schedule("PrintExtensionContext::OnDisconnectAbility", engine, + napi_value lastParam = (argc == NapiPrintUtils::ARGC_ONE) ? nullptr : argv[1]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("PrintExtensionContext::OnDisconnectAbility", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); return result; } }; -NativeValue *CreateJsMetadata(NativeEngine &engine, const AppExecFwk::Metadata &Info) +napi_value CreateJsMetadata(napi_env &engine, const AppExecFwk::Metadata &Info) { PRINT_HILOGD("CreateJsMetadata"); - NativeValue *objValue = engine.CreateObject(); - NativeObject *object = ConvertNativeValueTo(objValue); + napi_value object = nullptr; + napi_create_object(engine, &object); - object->SetProperty("name", CreateJsValue(engine, Info.name)); - object->SetProperty("value", CreateJsValue(engine, Info.value)); - object->SetProperty("resource", CreateJsValue(engine, Info.resource)); - return objValue; + napi_set_named_property(engine, object, "name", CreateJsValue(engine, Info.name)); + napi_set_named_property(engine, object, "value", CreateJsValue(engine, Info.value)); + napi_set_named_property(engine, object, "resource", CreateJsValue(engine, Info.resource)); + return object; } -NativeValue *CreateJsMetadataArray(NativeEngine &engine, const std::vector &info) +napi_value CreateJsMetadataArray(napi_env &engine, const std::vector &info) { PRINT_HILOGD("CreateJsMetadataArray"); - NativeValue *arrayValue = engine.CreateArray(info.size()); - NativeArray *array = ConvertNativeValueTo(arrayValue); + napi_value arrayValue = nullptr; + napi_create_array_with_length(engine, info.size(), &arrayValue); uint32_t index = 0; for (const auto &item : info) { - array->SetElement(index++, CreateJsMetadata(engine, item)); + napi_set_element(engine, arrayValue, index++, CreateJsMetadata(engine, item)); } return arrayValue; } -NativeValue *CreateJsExtensionAbilityInfo(NativeEngine &engine, const AppExecFwk::ExtensionAbilityInfo &info) +napi_value CreateJsExtensionAbilityInfoMessage(napi_env &engine, const AppExecFwk::ExtensionAbilityInfo &info) { - PRINT_HILOGD("CreateJsExtensionAbilityInfo"); - NativeValue *objValue = engine.CreateObject(); - NativeObject *object = ConvertNativeValueTo(objValue); - object->SetProperty("bundleName", CreateJsValue(engine, info.bundleName)); - object->SetProperty("moduleName", CreateJsValue(engine, info.moduleName)); - object->SetProperty("name", CreateJsValue(engine, info.name)); - object->SetProperty("labelId", CreateJsValue(engine, info.labelId)); - object->SetProperty("descriptionId", CreateJsValue(engine, info.descriptionId)); - object->SetProperty("iconId", CreateJsValue(engine, info.iconId)); - object->SetProperty("isVisible", CreateJsValue(engine, info.visible)); - object->SetProperty("extensionAbilityType", CreateJsValue(engine, info.type)); - NativeValue *permissionArrayValue = engine.CreateArray(info.permissions.size()); - NativeArray *permissionArray = ConvertNativeValueTo(permissionArrayValue); - if (permissionArray != nullptr) { - int index = 0; + PRINT_HILOGD("CreateJsExtensionAbilityInfoMessage"); + napi_value object = nullptr; + napi_create_object(engine, &object); + napi_set_named_property(engine, object, "bundleName", CreateJsValue(engine, info.bundleName)); + napi_set_named_property(engine, object, "moduleName", CreateJsValue(engine, info.moduleName)); + napi_set_named_property(engine, object, "name", CreateJsValue(engine, info.name)); + napi_set_named_property(engine, object, "labelId", CreateJsValue(engine, info.labelId)); + napi_set_named_property(engine, object, "descriptionId", CreateJsValue(engine, info.descriptionId)); + napi_set_named_property(engine, object, "iconId", CreateJsValue(engine, info.iconId)); + napi_set_named_property(engine, object, "isVisible", CreateJsValue(engine, info.visible)); + napi_set_named_property(engine, object, "extensionAbilityType", CreateJsValue(engine, info.type)); + + napi_value permissionArrayValue = nullptr; + napi_create_array_with_length(engine, info.permissions.size(), &permissionArrayValue); + if (permissionArrayValue != nullptr) { + uint32_t index = 0; for (auto permission : info.permissions) { - permissionArray->SetElement(index++, CreateJsValue(engine, permission)); + napi_set_element(engine, permissionArrayValue, index++, CreateJsValue(engine, permission)); } } - object->SetProperty("permissions", permissionArrayValue); - object->SetProperty("applicationInfo", CreateJsApplicationInfo(engine, info.applicationInfo)); - object->SetProperty("metadata", CreateJsMetadataArray(engine, info.metadata)); - object->SetProperty("enabled", CreateJsValue(engine, info.enabled)); - object->SetProperty("readPermission", CreateJsValue(engine, info.readPermission)); - object->SetProperty("writePermission", CreateJsValue(engine, info.writePermission)); - return objValue; + napi_set_named_property(engine, object, "permissions", permissionArrayValue); + napi_set_named_property(engine, object, "applicationInfo", CreateJsApplicationInfo(engine, info.applicationInfo)); + napi_set_named_property(engine, object, "metadata", CreateJsMetadataArray(engine, info.metadata)); + napi_set_named_property(engine, object, "enabled", CreateJsValue(engine, info.enabled)); + napi_set_named_property(engine, object, "readPermission", CreateJsValue(engine, info.readPermission)); + napi_set_named_property(engine, object, "writePermission", CreateJsValue(engine, info.writePermission)); + return object; } -NativeValue *CreateJsPrintExtensionContext(NativeEngine &engine, +napi_value CreateJsPrintExtensionContext(napi_env engine, std::shared_ptr context, std::string &extensionId) { PRINT_HILOGD("CreateJsPrintExtensionContext begin"); - NativeValue *objValue = CreateJsExtensionContext(engine, context); - NativeObject *object = ConvertNativeValueTo(objValue); + napi_value objValue = CreateJsExtensionContext(engine, context); + napi_value object = objValue; std::unique_ptr jsContext = std::make_unique(context); - object->SetNativePointer(jsContext.release(), JsPrintExtensionContext::Finalizer, nullptr); + napi_wrap(engine, object, jsContext.release(), JsPrintExtensionContext::Finalizer, nullptr, nullptr); // make handler handler_ = std::make_shared(AppExecFwk::EventRunner::GetMainEventRunner()); const char *moduleName = "JsPrintExtensionContext"; - BindNativeFunction(engine, *object, "startAbility", moduleName, JsPrintExtensionContext::StartAbility); - BindNativeFunction(engine, *object, "terminateSelf", moduleName, JsPrintExtensionContext::TerminateAbility); - BindNativeFunction(engine, *object, "connectAbility", moduleName, JsPrintExtensionContext::ConnectAbility); - BindNativeFunction(engine, *object, "disconnectAbility", moduleName, JsPrintExtensionContext::DisconnectAbility); + BindNativeFunction(engine, object, "startAbility", moduleName, JsPrintExtensionContext::StartAbility); + BindNativeFunction(engine, object, "terminateSelf", moduleName, JsPrintExtensionContext::TerminateAbility); + BindNativeFunction(engine, object, "connectAbility", moduleName, JsPrintExtensionContext::ConnectAbility); + BindNativeFunction(engine, object, "disconnectAbility", moduleName, JsPrintExtensionContext::DisconnectAbility); BindNativeFunction( - engine, *object, "startAbilityWithAccount", moduleName, JsPrintExtensionContext::StartAbilityWithAccount); + engine, object, "startAbilityWithAccount", moduleName, JsPrintExtensionContext::StartAbilityWithAccount); BindNativeFunction( - engine, *object, "connectAbilityWithAccount", moduleName, JsPrintExtensionContext::ConnectAbilityWithAccount); + engine, object, "connectAbilityWithAccount", moduleName, JsPrintExtensionContext::ConnectAbilityWithAccount); if (context) { PRINT_HILOGD("Set ExtensionAbilityInfo Property"); auto abilityInfo = context->GetAbilityInfo(); @@ -500,7 +502,8 @@ NativeValue *CreateJsPrintExtensionContext(NativeEngine &engine, PRINT_HILOGD("Get target fail."); return objValue; } - object->SetProperty("extensionAbilityInfo", CreateJsExtensionAbilityInfo(engine, *infoIter)); + napi_set_named_property(engine, object, + "extensionAbilityInfo", CreateJsExtensionAbilityInfoMessage(engine, *infoIter)); } } -- Gitee