Napi 整改

issue: https://gitee.com/openharmony/ability_dmsfwk/issues/I81C7Y

Signed-off-by: hunili <lihucheng2@huawei.com>
This commit is contained in:
hunili 2023-09-16 15:50:57 +08:00
parent e5d8e28462
commit c96bd53ffa
5 changed files with 427 additions and 362 deletions

View File

@ -16,14 +16,13 @@
#include "native_engine/native_engine.h"
#include "js_continuation_manager.h"
static napi_module _module = {
.nm_modname = "continuation.continuationManager",
.nm_filename = "continuation/libcontinuationmanager_napi.so/continuationmanager.js",
.nm_register_func = OHOS::DistributedSchedule::JsContinuationManagerInit,
};
__attribute__((constructor)) static void NAPI_application_continuationmanager_AutoRegister()
{
auto moduleManager = NativeModuleManager::GetInstance();
NativeModule newModuleInfo = {
.name = "continuation.continuationManager",
.fileName = "continuation/libcontinuationmanager_napi.so/continuationmanager.js",
.registerCallback = OHOS::DistributedSchedule::JsContinuationManagerInit,
};
moduleManager->Register(&newModuleInfo);
napi_module_register(&_module);
}

View File

@ -33,59 +33,63 @@ public:
JsContinuationManager() = default;
~JsContinuationManager() = default;
static void Finalizer(NativeEngine* engine, void* data, void* hint);
static NativeValue* Register(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* Unregister(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* RegisterDeviceSelectionCallback(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* UnregisterDeviceSelectionCallback(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* UpdateConnectStatus(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* StartDeviceManager(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* InitDeviceConnectStateObject(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* InitContinuationModeObject(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* RegisterContinuation(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* UnregisterContinuation(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* UpdateContinuationState(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* StartContinuationDeviceManager(NativeEngine* engine, NativeCallbackInfo* info);
static void Finalizer(napi_env env, void* data, void* hint);
static napi_value Register(napi_env env, napi_callback_info info);
static napi_value Unregister(napi_env env, napi_callback_info info);
static napi_value RegisterDeviceSelectionCallback(napi_env env, napi_callback_info info);
static napi_value UnregisterDeviceSelectionCallback(napi_env env, napi_callback_info info);
static napi_value UpdateConnectStatus(napi_env env, napi_callback_info info);
static napi_value StartDeviceManager(napi_env env, napi_callback_info info);
static napi_value InitDeviceConnectStateObject(napi_env env, napi_callback_info info);
static napi_value InitContinuationModeObject(napi_env env, napi_callback_info info);
static napi_value RegisterContinuation(napi_env env, napi_callback_info info);
static napi_value UnregisterContinuation(napi_env env, napi_callback_info info);
static napi_value UpdateContinuationState(napi_env env, napi_callback_info info);
static napi_value StartContinuationDeviceManager(napi_env env, napi_callback_info info);
private:
using CallbackPair = std::pair<std::unique_ptr<NativeReference>, sptr<JsDeviceSelectionListener>>;
NativeValue* OnRegister(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnUnregister(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnRegisterDeviceSelectionCallback(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnUnregisterDeviceSelectionCallback(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnUpdateConnectStatus(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnStartDeviceManager(NativeEngine &engine, NativeCallbackInfo &info);
int32_t CheckParamAndGetToken(NativeEngine &engine, NativeCallbackInfo &info, int32_t &token);
NativeValue* OnInitDeviceConnectStateObject(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnInitContinuationModeObject(NativeEngine &engine, NativeCallbackInfo &info);
static napi_status SetEnumItem(const napi_env& env, napi_value object, const char* name, int32_t value);
NativeValue* OnRegisterContinuation(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnUnregisterContinuation(NativeEngine &engine, NativeCallbackInfo &info);
NativeValue* OnUpdateContinuationState(NativeEngine &engine, NativeCallbackInfo &info);
std::string GetErrorInfo(NativeEngine &engine, NativeCallbackInfo &info, int32_t &token,
napi_value OnRegister(napi_env env, napi_callback_info info);
napi_value OnUnregister(napi_env env, napi_callback_info info);
napi_value OnRegisterDeviceSelectionCallback(napi_env env, napi_callback_info info);
napi_value OnUnregisterDeviceSelectionCallback(napi_env env, napi_callback_info info);
napi_value OnUpdateConnectStatus(napi_env env, napi_callback_info info);
int32_t GetInfoForUpdateConnectStatus(napi_env env,
napi_value *argv,int32_t &token, std::string &deviceId, DeviceConnectStatus &deviceConnectStatus);
napi_value OnStartDeviceManager(napi_env env, napi_callback_info info);
int32_t CheckParamAndGetToken(napi_env env, size_t argc, napi_value *argv, int32_t &token);
napi_value OnInitDeviceConnectStateObject(napi_env env, napi_callback_info info);
napi_value OnInitContinuationModeObject(napi_env env, napi_callback_info info);
static napi_status SetEnumItem(const napi_env &env, napi_value object, const char* name, int32_t value);
napi_value OnRegisterContinuation(napi_env env, napi_callback_info info);
napi_value OnUnregisterContinuation(napi_env env, napi_callback_info info);
napi_value OnUpdateContinuationState(napi_env env, napi_callback_info info);
std::string GetErrorInfo(napi_env env, napi_callback_info info, int32_t &token,
std::string &deviceId, DeviceConnectStatus &deviceConnectStatus);
std::string GetErrorForStartContinuation(NativeEngine &engine, NativeCallbackInfo &info,
std::string GetErrorForStartContinuation(napi_env env, napi_callback_info info,
int32_t &token, int32_t &unwrapArgc, std::shared_ptr<ContinuationExtraParams> &continuationExtraParams);
NativeValue* OnStartContinuationDeviceManager(NativeEngine &engine, NativeCallbackInfo &info);
std::string GetErrorInforForRegisterContination(napi_env env, napi_callback_info info,
size_t &unwrapArgc, std::shared_ptr<ContinuationExtraParams> &continuationExtraParams);
napi_value OnStartContinuationDeviceManager(napi_env env, napi_callback_info info);
static bool IsCallbackValid(NativeValue* listenerObj);
static bool IsCallbackValid(napi_env env, napi_value listenerObj);
bool IsCallbackRegistered(int32_t token, const std::string& cbType);
bool UnWrapContinuationExtraParams(const napi_env& env, const napi_value& options,
bool UnWrapContinuationExtraParams(const napi_env &env, const napi_value& options,
std::shared_ptr<ContinuationExtraParams>& continuationExtraParams);
bool UnwrapJsonByPropertyName(const napi_env& env, const napi_value& param,
bool UnwrapJsonByPropertyName(const napi_env &env, const napi_value& param,
const std::string& field, nlohmann::json& jsonObj);
static bool PraseJson(const napi_env& env, const napi_value& jsonField, const napi_value& jsProNameList,
static bool PraseJson(const napi_env &env, const napi_value& jsonField, const napi_value& jsProNameList,
uint32_t jsProCount, nlohmann::json& jsonObj);
static int32_t ErrorCodeReturn(int32_t code);
static std::string ErrorMessageReturn(int32_t code);
static std::string OnRegisterDeviceSelectionCallbackParameterCheck(NativeEngine &engine,
NativeCallbackInfo &info, std::string &cbType, int32_t &token, NativeValue** jsListenerObj);
static std::string OnRegisterDeviceSelectionCallbackParameterCheck(napi_env env,
napi_callback_info info, std::string &cbType, int32_t &token, napi_value *jsListenerObj);
napi_value GenerateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
std::mutex jsCbMapMutex_;
std::map<int32_t, std::map<std::string, CallbackPair>> jsCbMap_;
};
NativeValue* JsContinuationManagerInit(NativeEngine* engine, NativeValue* exportObj);
napi_value JsContinuationManagerInit(napi_env env, napi_value exportObj);
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_JS_CONTINUATION_MANAGER_H

View File

@ -38,16 +38,17 @@ void JsDeviceSelectionListener::OnDeviceDisconnect(const std::vector<Continuatio
CallJsMethod(EVENT_DISCONNECT, continuationResults);
}
void JsDeviceSelectionListener::AddCallback(const std::string& cbType, NativeValue* jsListenerObj)
void JsDeviceSelectionListener::AddCallback(const std::string& cbType, napi_value jsListenerObj)
{
HILOGD("called.");
napi_ref tempRef = nullptr;
std::unique_ptr<NativeReference> callbackRef;
if (engine_ == nullptr) {
HILOGE("engine_ is nullptr");
return;
}
callbackRef.reset(engine_->CreateReference(jsListenerObj, 1));
napi_create_reference(engine_, jsListenerObj, 1, &tempRef);
callbackRef.reset(reinterpret_cast<NativeReference *>(tempRef));
std::lock_guard<std::mutex> jsCallBackMapLock(jsCallBackMapMutex_);
jsCallBackMap_[cbType] = std::move(callbackRef);
HILOGD("jsCallBackMap_ cbType: %{public}s, size: %{public}u!",
@ -72,36 +73,36 @@ void JsDeviceSelectionListener::CallJsMethod(const std::string& methodName,
return;
}
// js callback should run in js thread
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback>
std::unique_ptr<NapiAsyncTask::CompleteCallback> complete = std::make_unique<NapiAsyncTask::CompleteCallback>
([this, methodName, continuationResults]
(NativeEngine &engine, AsyncTask &task, int32_t status) {
(napi_env env, NapiAsyncTask &task, int32_t status) {
napi_handle_scope scope = nullptr;
napi_open_handle_scope(reinterpret_cast<napi_env>(&engine), &scope);
napi_open_handle_scope(env, &scope);
if (scope == nullptr) {
return;
}
CallJsMethodInner(methodName, continuationResults);
napi_close_handle_scope(reinterpret_cast<napi_env>(&engine), scope);
napi_close_handle_scope(env, scope);
});
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule("JsDeviceSelectionListener::OnDeviceConnect",
*engine_, std::make_unique<AsyncTask>(callback, std::move(execute), std::move(complete)));
napi_ref callback = nullptr;
std::unique_ptr<NapiAsyncTask::ExecuteCallback> execute = nullptr;
NapiAsyncTask::Schedule("JsDeviceSelectionListener::OnDeviceConnect",
engine_, std::make_unique<NapiAsyncTask>(callback, std::move(execute), std::move(complete)));
}
void JsDeviceSelectionListener::CallJsMethodInner(const std::string& methodName,
const std::vector<ContinuationResult>& continuationResults)
{
std::lock_guard<std::mutex> jsCallBackMapLock(jsCallBackMapMutex_);
NativeValue* method = jsCallBackMap_[methodName]->Get();
napi_value method = jsCallBackMap_[methodName]->GetNapiValue();
if (method == nullptr) {
HILOGE("Failed to get %{public}s from object", methodName.c_str());
return;
}
NativeValue* argv[] = { WrapContinuationResultArray(*engine_, continuationResults) };
engine_->CallFunction(engine_->CreateUndefined(), method, argv, ArraySize(argv));
napi_value argv[] = { WrapContinuationResultArray(engine_, continuationResults) };
napi_call_function(engine_, CreateJsUndefined(engine_), method, ArraySize(argv), argv, nullptr);
}
void JsDeviceSelectionListener::CallJsMethod(const std::string& methodName, const std::vector<std::string>& deviceIds)
@ -112,71 +113,79 @@ void JsDeviceSelectionListener::CallJsMethod(const std::string& methodName, cons
return;
}
// js callback should run in js thread
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback>
std::unique_ptr<NapiAsyncTask::CompleteCallback> complete = std::make_unique<NapiAsyncTask::CompleteCallback>
([this, methodName, deviceIds]
(NativeEngine &engine, AsyncTask &task, int32_t status) {
(napi_env env, NapiAsyncTask &task, int32_t status) {
napi_handle_scope scope = nullptr;
napi_open_handle_scope(reinterpret_cast<napi_env>(&engine), &scope);
napi_open_handle_scope(env, &scope);
if (scope == nullptr) {
return;
}
CallJsMethodInner(methodName, deviceIds);
napi_close_handle_scope(reinterpret_cast<napi_env>(&engine), scope);
napi_close_handle_scope(env, scope);
});
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule("JsDeviceSelectionListener::OnDeviceDisconnect",
*engine_, std::make_unique<AsyncTask>(callback, std::move(execute), std::move(complete)));
napi_ref callback = nullptr;
std::unique_ptr<NapiAsyncTask::ExecuteCallback> execute = nullptr;
NapiAsyncTask::Schedule("JsDeviceSelectionListener::OnDeviceDisconnect",
engine_, std::make_unique<NapiAsyncTask>(callback, std::move(execute), std::move(complete)));
}
void JsDeviceSelectionListener::CallJsMethodInner(const std::string& methodName,
const std::vector<std::string>& deviceIds)
{
std::lock_guard<std::mutex> jsCallBackMapLock(jsCallBackMapMutex_);
NativeValue* method = jsCallBackMap_[methodName]->Get();
napi_value method = jsCallBackMap_[methodName]->GetNapiValue();
if (method == nullptr) {
HILOGE("Failed to get %{public}s from object", methodName.c_str());
return;
}
NativeValue* argv[] = { WrapDeviceIdArray(*engine_, deviceIds) };
engine_->CallFunction(engine_->CreateUndefined(), method, argv, ArraySize(argv));
napi_value argv[] = { WrapDeviceIdArray(engine_, deviceIds) };
napi_call_function(engine_, CreateJsUndefined(engine_), method, ArraySize(argv), argv, nullptr);
}
NativeValue* JsDeviceSelectionListener::WrapContinuationResult(NativeEngine& engine,
napi_value JsDeviceSelectionListener::WrapContinuationResult(napi_env env,
const ContinuationResult& continuationResult)
{
NativeValue* objValue = engine.CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
object->SetProperty("id", CreateJsValue(engine, continuationResult.GetDeviceId()));
object->SetProperty("type", CreateJsValue(engine, continuationResult.GetDeviceType()));
object->SetProperty("name", CreateJsValue(engine, continuationResult.GetDeviceName()));
napi_value objValue;
napi_create_object(env, &objValue);
SetKeyValue(env, objValue, "id", continuationResult.GetDeviceId());
SetKeyValue(env, objValue, "type", continuationResult.GetDeviceType());
SetKeyValue(env, objValue, "name", continuationResult.GetDeviceName());
return objValue;
}
NativeValue* JsDeviceSelectionListener::WrapContinuationResultArray(NativeEngine& engine,
napi_value JsDeviceSelectionListener::WrapContinuationResultArray(napi_env env,
const std::vector<ContinuationResult>& continuationResults)
{
NativeValue* arrayValue = engine.CreateArray(continuationResults.size());
NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
napi_value arrayValue;
napi_create_array_with_length(env, continuationResults.size(), &arrayValue);
uint32_t index = 0;
for (const auto& continuationResult : continuationResults) {
array->SetElement(index++, WrapContinuationResult(engine, continuationResult));
napi_set_element(env,
arrayValue, index++, WrapContinuationResult(env, continuationResult));
}
return arrayValue;
}
NativeValue* JsDeviceSelectionListener::WrapDeviceIdArray(NativeEngine& engine,
napi_value JsDeviceSelectionListener::WrapDeviceIdArray(napi_env env,
const std::vector<std::string>& deviceIds)
{
NativeValue* arrayValue = engine.CreateArray(deviceIds.size());
NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
napi_value arrayValue;
napi_create_array_with_length(env, deviceIds.size(), &arrayValue);
uint32_t index = 0;
for (const auto& deviceId : deviceIds) {
array->SetElement(index++, CreateJsValue(engine, deviceId));
napi_set_element(env, arrayValue, index++, CreateJsValue(env, deviceId));
}
return arrayValue;
}
void JsDeviceSelectionListener::SetKeyValue(napi_env env,
const napi_value object, const std::string &strKey, const std::string &strValue) const
{
napi_value attrValue = nullptr;
napi_create_string_utf8(env, strValue.c_str(), NAPI_AUTO_LENGTH, &attrValue);
napi_set_named_property(env, object, strKey.c_str(), attrValue);
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -26,13 +26,13 @@ namespace OHOS {
namespace DistributedSchedule {
class JsDeviceSelectionListener : public DeviceSelectionNotifierStub {
public:
explicit JsDeviceSelectionListener(NativeEngine* engine) : engine_(engine) {}
explicit JsDeviceSelectionListener(napi_env env) : engine_(env) {}
virtual ~JsDeviceSelectionListener() = default;
void OnDeviceConnect(const std::vector<ContinuationResult>& continuationResults) override;
void OnDeviceDisconnect(const std::vector<ContinuationResult>& continuationResults) override;
void AddCallback(const std::string& cbType, NativeValue* jsListenerObj);
void AddCallback(const std::string& cbType, napi_value jsListenerObj);
void RemoveCallback(const std::string& cbType);
private:
@ -40,12 +40,14 @@ private:
void CallJsMethod(const std::string& methodName, const std::vector<std::string>& deviceIds);
void CallJsMethodInner(const std::string& methodName, const std::vector<ContinuationResult>& continuationResults);
void CallJsMethodInner(const std::string& methodName, const std::vector<std::string>& deviceIds);
NativeValue* WrapContinuationResult(NativeEngine& engine, const ContinuationResult& continuationResult);
NativeValue* WrapContinuationResultArray(NativeEngine& engine,
napi_value WrapContinuationResult(napi_env env, const ContinuationResult& continuationResult);
napi_value WrapContinuationResultArray(napi_env env,
const std::vector<ContinuationResult>& continuationResults);
NativeValue* WrapDeviceIdArray(NativeEngine& engine, const std::vector<std::string>& deviceIds);
napi_value WrapDeviceIdArray(napi_env env, const std::vector<std::string>& deviceIds);
void SetKeyValue(napi_env env,
const napi_value object, const std::string &strKey, const std::string &strValue) const;
NativeEngine* engine_ = nullptr;
napi_env engine_ = nullptr;
std::mutex jsCallBackMapMutex_;
std::map<std::string, std::unique_ptr<NativeReference>> jsCallBackMap_;
};