!558 【内存问题修复】修复输入法框架内存泄漏问题

Merge pull request !558 from 赵凌岚/master
This commit is contained in:
openharmony_ci 2023-02-03 07:10:48 +00:00 committed by Gitee
commit 00a908ff69
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
15 changed files with 235 additions and 340 deletions

View File

@ -133,10 +133,12 @@ napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback
napi_get_null(env, &result);
return result;
}
napi_wrap(
env, thisVar, delegate.get(),
[](napi_env env, void *nativeObject, void *hint) { IMSA_HILOGE("delete JsInputMethodEngineSetting"); },
nullptr, nullptr);
napi_status status = napi_wrap(
env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr);
if (status != napi_ok) {
IMSA_HILOGE("JsInputMethodEngineSetting napi_wrap failed: %{public}d", status);
return nullptr;
}
if (delegate->loop_ == nullptr) {
napi_get_uv_event_loop(env, &delegate->loop_);
}
@ -384,113 +386,6 @@ bool JsInputMethodEngineSetting::Equals(napi_env env, napi_value value, napi_ref
return isEquals;
}
uv_work_t *JsInputMethodEngineSetting::GetUVwork(std::string type)
{
IMSA_HILOGI("run in GetUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
uv_work_t *JsInputMethodEngineSetting::GetStopInputUVwork(std::string type, std::string imeId)
{
IMSA_HILOGI("run in GetStopInputUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->imeid = imeId;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
uv_work_t *JsInputMethodEngineSetting::GetWindowIDUVwork(std::string type, uint32_t windowid)
{
IMSA_HILOGI("run in GetWindowIDUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->windowid = windowid;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
uv_work_t *JsInputMethodEngineSetting::GetSubtypeUVwork(std::string type, const SubProperty &property)
{
IMSA_HILOGI("run in GetSubtypeUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->subProperty = property;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property)
{
napi_value subType = nullptr;
@ -537,10 +432,11 @@ napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const
void JsInputMethodEngineSetting::OnInputStart()
{
IMSA_HILOGI("run in OnInputStart");
IMSA_HILOGD("run in");
std::string type = "inputStart";
uv_work_t *work = GetUVwork(type);
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -556,7 +452,7 @@ void JsInputMethodEngineSetting::OnInputStart()
}
auto getInputStartProperty = [](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc < 2) {
return false;
}
@ -577,9 +473,10 @@ void JsInputMethodEngineSetting::OnInputStart()
void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
{
std::string type = isShow ? "keyboardShow" : "keyboardHide";
IMSA_HILOGI("run in OnKeyboardStatus: %{public}s", type.c_str());
IMSA_HILOGD("run in, %{public}s", type.c_str());
uv_work_t *work = GetUVwork(type);
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -591,7 +488,7 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
});
auto getKeyboardStatusProperty = [](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc == 0) {
return false;
}
@ -604,10 +501,11 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId)
{
IMSA_HILOGI("run in OnInputStop");
IMSA_HILOGD("run in");
std::string type = "inputStop";
uv_work_t *work = GetStopInputUVwork(type, imeId);
uv_work_t *work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; });
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -623,7 +521,7 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId)
}
auto getInputStopProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc == 0) {
return false;
}
@ -636,10 +534,11 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId)
void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
{
IMSA_HILOGI("run in OnSetCallingWindow");
IMSA_HILOGD("run in");
std::string type = "setCallingWindow";
uv_work_t *work = GetWindowIDUVwork(type, windowId);
uv_work_t *work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; });
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -655,7 +554,7 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
}
auto getCallingWindowProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc == 0) {
return false;
}
@ -668,10 +567,11 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
{
IMSA_HILOGI("run in OnSetSubtype");
IMSA_HILOGD("run in");
std::string type = "setSubtype";
uv_work_t *work = GetSubtypeUVwork(type, property);
uv_work_t *work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; });
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -687,7 +587,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
}
auto getSubtypeProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc == 0) {
return false;
}
@ -696,11 +596,40 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr");
return false;
}
args[ARGC_ZERO] = {jsObject};
args[ARGC_ZERO] = { jsObject };
return true;
};
JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getSubtypeProperty);
});
}
uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
{
IMSA_HILOGD("run in, type: %{public}s", type.c_str());
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
if (entrySetter != nullptr) {
entrySetter(*entry);
}
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -57,10 +57,6 @@ private:
static napi_value GetIMEInstance(napi_env env, napi_callback_info info, int flag);
void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj);
void UnRegisterListener(napi_value callback, std::string type);
uv_work_t *GetUVwork(std::string type);
uv_work_t *GetStopInputUVwork(std::string type, std::string imeId);
uv_work_t *GetWindowIDUVwork(std::string type, uint32_t windowid);
uv_work_t *GetSubtypeUVwork(std::string type, const SubProperty &property);
static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property);
static std::string GetStringProperty(napi_env env, napi_value jsString);
static constexpr int32_t MAX_VALUE_LEN = 1024;
@ -76,6 +72,8 @@ private:
{
}
};
using EntrySetter = std::function<void(UvEntry &)>;
uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr);
uv_loop_s *loop_ = nullptr;
std::recursive_mutex mutex_;
std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_;

View File

@ -51,15 +51,19 @@ napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback
napi_get_null(env, &result);
return result;
}
napi_wrap(
env, thisVar, controllerObject,
[](napi_env env, void *data, void *hint) {
auto *objInfo = reinterpret_cast<JsKeyboardControllerEngine *>(data);
if (objInfo != nullptr) {
delete objInfo;
}
},
nullptr, nullptr);
auto finalize = [](napi_env env, void *data, void *hint) {
IMSA_HILOGE("JsKeyboardControllerEngine finalize");
auto *objInfo = reinterpret_cast<JsKeyboardControllerEngine *>(data);
if (objInfo != nullptr) {
delete objInfo;
}
};
napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr);
if (status != napi_ok) {
IMSA_HILOGE("JsKeyboardControllerEngine napi_wrap failed: %{public}d", status);
delete controllerObject;
return nullptr;
}
return thisVar;
}

View File

@ -33,6 +33,7 @@ struct HideKeyboardContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -52,6 +53,7 @@ struct HideContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);

View File

@ -113,10 +113,12 @@ napi_value JsKeyboardDelegateSetting::JsConstructor(napi_env env, napi_callback_
napi_get_null(env, &result);
return result;
}
napi_wrap(
env, thisVar, delegate.get(),
[](napi_env env, void *nativeObject, void *hint) { IMSA_HILOGE("delete JsKeyboardDelegateSetting"); }, nullptr,
nullptr);
napi_status status = napi_wrap(
env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr);
if (status != napi_ok) {
IMSA_HILOGE("JsKeyboardDelegateSetting napi_wrap failed: %{public}d", status);
return nullptr;
}
if (delegate->loop_ == nullptr) {
napi_get_uv_event_loop(env, &delegate->loop_);
}
@ -355,13 +357,16 @@ napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t
bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus)
{
IMSA_HILOGI("run in OnKeyEvent");
IMSA_HILOGD("run in");
KeyEventPara para{ keyCode, keyStatus, false };
std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp");
auto isDone = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
uv_work_t *work = GetKeyEventUVwork(type, para, isDone);
uv_work_t *work = GetUVwork(type, [&para, isDone](UvEntry &entry) {
entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent };
entry.isDone = isDone;
});
if (work == nullptr) {
IMSA_HILOGE("GetKeyEventUVwork nullptr");
IMSA_HILOGE("failed to get uv work");
return false;
}
uv_queue_work(
@ -372,12 +377,12 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus)
delete work;
});
auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc == 0) {
return false;
}
napi_value jsObject = GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode,
entry->keyEventPara.keyStatus);
napi_value jsObject =
GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus);
if (jsObject == nullptr) {
IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr");
return false;
@ -391,129 +396,18 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus)
return isDone->GetValue();
}
uv_work_t *JsKeyboardDelegateSetting::GetKeyEventUVwork(
std::string type, KeyEventPara para, std::shared_ptr<BlockData<bool>> &isDone)
{
IMSA_HILOGI("run in GetKeyEventUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent };
entry->isDone = isDone;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
delete entry;
return nullptr;
}
work->data = entry;
return work;
}
uv_work_t *JsKeyboardDelegateSetting::GetCursorUVwork(std::string type, CursorPara para)
{
IMSA_HILOGI("run in GetCursorUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->curPara.positionX = para.positionX;
entry->curPara.positionY = para.positionY;
entry->curPara.height = para.height;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
uv_work_t *JsKeyboardDelegateSetting::GetSelectionUVwork(std::string type, SelectionPara para)
{
IMSA_HILOGI("run in GetSelectionUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->selPara.oldBegin = para.oldBegin;
entry->selPara.oldEnd = para.oldEnd;
entry->selPara.newBegin = para.newBegin;
entry->selPara.newEnd = para.newEnd;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
uv_work_t *JsKeyboardDelegateSetting::GetTextUVwork(std::string type, std::string text)
{
IMSA_HILOGI("run in GetTextUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->text = text;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
{
IMSA_HILOGI("run in OnCursorUpdate");
IMSA_HILOGD("run in");
CursorPara para{ positionX, positionY, height };
std::string type = "cursorContextChange";
uv_work_t *work = GetCursorUVwork(type, para);
uv_work_t *work = GetUVwork(type, [&para](UvEntry &entry) {
entry.curPara.positionX = para.positionX;
entry.curPara.positionY = para.positionY;
entry.curPara.height = para.height;
});
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -525,7 +419,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi
});
auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc < 3) {
return false;
}
@ -540,11 +434,17 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi
void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
{
IMSA_HILOGI("run in OnSelectionChange");
IMSA_HILOGD("run in");
SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd };
std::string type = "selectionChange";
uv_work_t *work = GetSelectionUVwork(type, para);
uv_work_t *work = GetUVwork(type, [&para](UvEntry &entry) {
entry.selPara.oldBegin = para.oldBegin;
entry.selPara.oldEnd = para.oldEnd;
entry.selPara.newBegin = para.newBegin;
entry.selPara.newEnd = para.newEnd;
});
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -556,7 +456,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE
});
auto getSelectionChangeProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc < 4) {
return false;
}
@ -572,10 +472,11 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE
void JsKeyboardDelegateSetting::OnTextChange(const std::string &text)
{
IMSA_HILOGI("run in OnTextChange");
IMSA_HILOGD("run in");
std::string type = "textChange";
uv_work_t *work = GetTextUVwork(type, text);
uv_work_t *work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; });
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -587,7 +488,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text)
});
auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc == 0) {
return false;
}
@ -597,5 +498,34 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text)
JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getTextChangeProperty);
});
}
uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
{
IMSA_HILOGD("run in, type: %{public}s", type.c_str());
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
if (entrySetter != nullptr) {
entrySetter(*entry);
}
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -130,10 +130,8 @@ private:
{
}
};
uv_work_t *GetCursorUVwork(std::string type, CursorPara para);
uv_work_t *GetSelectionUVwork(std::string type, SelectionPara para);
uv_work_t *GetTextUVwork(std::string type, std::string text);
uv_work_t *GetKeyEventUVwork(std::string type, KeyEventPara para, std::shared_ptr<BlockData<bool>> &isDone);
using EntrySetter = std::function<void(UvEntry &)>;
uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr);
uv_loop_s *loop_ = nullptr;
std::recursive_mutex mutex_;
std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_;

View File

@ -93,16 +93,19 @@ napi_value JsTextInputClientEngine::JsConstructor(napi_env env, napi_callback_in
napi_get_null(env, &result);
return result;
}
napi_wrap(
env, thisVar, clientObject,
[](napi_env env, void *data, void *hint) {
auto *objInfo = reinterpret_cast<JsTextInputClientEngine *>(data);
if (objInfo != nullptr) {
delete objInfo;
}
},
nullptr, nullptr);
auto finalize = [](napi_env env, void *data, void *hint) {
IMSA_HILOGE("JsTextInputClientEngine finalize");
auto *objInfo = reinterpret_cast<JsTextInputClientEngine *>(data);
if (objInfo != nullptr) {
delete objInfo;
}
};
napi_status status = napi_wrap(env, thisVar, clientObject, finalize, nullptr, nullptr);
if (status != napi_ok) {
IMSA_HILOGE("JsTextInputClientEngine napi_wrap failed: %{public}d", status);
delete clientObject;
return nullptr;
}
return thisVar;
}

View File

@ -38,6 +38,7 @@ struct SendKeyFunctionContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -58,6 +59,7 @@ struct MoveCursorContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -79,6 +81,7 @@ struct DeleteForwardContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -100,6 +103,7 @@ struct DeleteBackwardContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -121,6 +125,7 @@ struct InsertTextContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -142,6 +147,7 @@ struct GetForwardContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -163,6 +169,7 @@ struct GetBackwardContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -184,6 +191,7 @@ struct GetEditorAttributeContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);

View File

@ -58,7 +58,9 @@ public:
if (input_ == nullptr) {
return napi_ok;
}
return input_(env, argc, argv, self);
auto ret = input_(env, argc, argv, self);
input_ = nullptr;
return ret;
}
virtual napi_status operator()(napi_env env, napi_value *result)
@ -67,7 +69,9 @@ public:
*result = nullptr;
return napi_ok;
}
return output_(env, result);
auto ret = output_(env, result);
output_ = nullptr;
return ret;
}
virtual void Exec()
@ -76,6 +80,7 @@ public:
return;
}
exec_(this);
exec_ = nullptr;
};
protected:

View File

@ -59,16 +59,20 @@ napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback
napi_get_null(env, &result);
return result;
}
napi_wrap(
env, thisVar, controllerObject,
[](napi_env env, void *data, void *hint) {
auto *objInfo = reinterpret_cast<JsGetInputMethodController *>(data);
if (objInfo != nullptr) {
IMSA_HILOGE("objInfo is nullptr");
delete objInfo;
}
},
nullptr, nullptr);
auto finalize = [](napi_env env, void *data, void *hint) {
IMSA_HILOGE("JsGetInputMethodController finalize");
auto *objInfo = reinterpret_cast<JsGetInputMethodController *>(data);
if (objInfo != nullptr) {
IMSA_HILOGE("objInfo is nullptr");
delete objInfo;
}
};
napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr);
if (status != napi_ok) {
IMSA_HILOGE("JsGetInputMethodController napi_wrap failed:%{public}d", status);
delete controllerObject;
return nullptr;
}
return thisVar;
}

View File

@ -35,6 +35,7 @@ struct HandleContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);

View File

@ -79,9 +79,12 @@ napi_value JsGetInputMethodSetting::JsConstructor(napi_env env, napi_callback_in
napi_get_null(env, &result);
return result;
}
napi_wrap(
env, thisVar, delegate.get(),
[](napi_env env, void *data, void *hint) { IMSA_HILOGE("delete JsInputMethodSetting"); }, nullptr, nullptr);
napi_status status = napi_wrap(
env, thisVar, delegate.get(), [](napi_env env, void *data, void *hint) {}, nullptr, nullptr);
if (status != napi_ok) {
IMSA_HILOGE("JsGetInputMethodSetting napi_wrap failed: %{public}d", status);
return nullptr;
}
if (delegate->loop_ == nullptr) {
napi_get_uv_event_loop(env, &delegate->loop_);
}
@ -511,41 +514,16 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info
return result;
}
uv_work_t *JsGetInputMethodSetting::GetImeChangeUVwork(
std::string type, const Property &property, const SubProperty &subProperty)
{
IMSA_HILOGI("run in GetImeChangeUVwork");
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
entry->property = property;
entry->subProperty = subProperty;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty)
{
IMSA_HILOGI("run in OnImeChange");
IMSA_HILOGD("run in");
std::string type = "imeChange";
uv_work_t *work = GetImeChangeUVwork(type, property, subProperty);
uv_work_t *work = GetUVwork(type, [&property, &subProperty](UvEntry &entry) {
entry.property = property;
entry.subProperty = subProperty;
});
if (work == nullptr) {
IMSA_HILOGD("failed to get uv entry");
return;
}
uv_queue_work(
@ -560,7 +538,7 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro
return;
}
auto getImeChangeProperty = [entry](napi_value *args, uint8_t argc,
std::shared_ptr<JSCallbackObject> item) -> bool {
std::shared_ptr<JSCallbackObject> item) -> bool {
if (argc < 2) {
return false;
}
@ -577,5 +555,34 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro
JsUtils::TraverseCallback(entry->vecCopy, ARGC_TWO, getImeChangeProperty);
});
}
uv_work_t *JsGetInputMethodSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
{
IMSA_HILOGD("run in, type: %{public}s", type.c_str());
UvEntry *entry = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (jsCbMap_[type].empty()) {
IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
return nullptr;
}
entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
if (entry == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
if (entrySetter != nullptr) {
entrySetter(*entry);
}
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
IMSA_HILOGE("entry ptr is nullptr!");
return nullptr;
}
work->data = entry;
return work;
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -42,6 +42,7 @@ struct ListInputContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -63,6 +64,7 @@ struct DisplayOptionalInputMethodContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -84,6 +86,7 @@ struct GetInputMethodControllerContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);
@ -111,7 +114,6 @@ public:
private:
static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr<ListInputContext> ctxt);
static JsGetInputMethodSetting *GetNative(napi_env env, napi_callback_info info);
uv_work_t *GetImeChangeUVwork(std::string type, const Property &property, const SubProperty &subProperty);
static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
static napi_value GetJsConstProperty(napi_env env, uint32_t num);
static napi_value DisplayInputMethod(napi_env env, napi_callback_info info, bool needThrowException);
@ -128,6 +130,8 @@ private:
{
}
};
using EntrySetter = std::function<void(UvEntry &)>;
uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr);
static const std::string IMS_CLASS_NAME;
static thread_local napi_ref IMSRef_;
uv_loop_s *loop_ = nullptr;

View File

@ -41,6 +41,7 @@ struct SwitchInputMethodContext : public AsyncCall::Context {
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
output_ = nullptr;
return status;
}
return Context::operator()(env, result);

View File

@ -150,15 +150,17 @@ bool JsUtils::TraverseCallback(const std::vector<std::shared_ptr<JSCallbackObjec
bool isResult = false;
bool isOnKeyEvent = false;
for (const auto &item : vecCopy) {
napi_handle_scope scope = nullptr;
napi_open_handle_scope(item->env_, &scope);
if (item->threadId_ != std::this_thread::get_id()) {
napi_close_handle_scope(item->env_, scope);
continue;
}
napi_value args[MAX_ARGMENT_COUNT];
if (!argsProvider(args, MAX_ARGMENT_COUNT, item)) {
napi_close_handle_scope(item->env_, scope);
continue;
}
napi_value callback = nullptr;
napi_value global = nullptr;
napi_value result = nullptr;
@ -168,12 +170,10 @@ bool JsUtils::TraverseCallback(const std::vector<std::shared_ptr<JSCallbackObjec
napi_get_global(item->env_, &global);
napi_status callStatus = napi_call_function(item->env_, global, callback, paramNum, args, &result);
if (callStatus != napi_ok) {
IMSA_HILOGE(
"notify data change failed callStatus:%{public}d", callStatus);
IMSA_HILOGE("notify data change failed callStatus:%{public}d", callStatus);
result = nullptr;
}
}
if (result != nullptr && !isOnKeyEvent) {
napi_valuetype valueType = napi_undefined;
napi_typeof(item->env_, result, &valueType);
@ -185,6 +185,7 @@ bool JsUtils::TraverseCallback(const std::vector<std::shared_ptr<JSCallbackObjec
isOnKeyEvent = true;
}
}
napi_close_handle_scope(item->env_, scope);
}
return isOnKeyEvent;
}