1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_keyboard_delegate_setting.h"
17
18 #include "input_method_ability.h"
19 #include "js_keyboard_controller_engine.h"
20 #include "js_text_input_client_engine.h"
21 #include "js_utils.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24
25 namespace OHOS {
26 namespace MiscServices {
27 constexpr size_t ARGC_ZERO = 0;
28 constexpr size_t ARGC_ONE = 1;
29 constexpr size_t ARGC_TWO = 2;
30 constexpr size_t ARGC_THREE = 3;
31 constexpr size_t ARGC_FOUR = 4;
32 const std::string JsKeyboardDelegateSetting::KDS_CLASS_NAME = "KeyboardDelegate";
33 thread_local napi_ref JsKeyboardDelegateSetting::KDSRef_ = nullptr;
34
35 std::mutex JsKeyboardDelegateSetting::keyboardMutex_;
36 std::shared_ptr<JsKeyboardDelegateSetting> JsKeyboardDelegateSetting::keyboardDelegate_ { nullptr };
37
Init(napi_env env,napi_value exports)38 napi_value JsKeyboardDelegateSetting::Init(napi_env env, napi_value exports)
39 {
40 napi_property_descriptor descriptor[] = {
41 DECLARE_NAPI_PROPERTY("OPTION_ASCII", GetJsConstProperty(env, static_cast<uint32_t>(20))),
42 DECLARE_NAPI_PROPERTY("OPTION_NONE", GetJsConstProperty(env, static_cast<uint32_t>(0))),
43 DECLARE_NAPI_PROPERTY("OPTION_AUTO_CAP_CHARACTERS", GetJsConstProperty(env, static_cast<uint32_t>(2))),
44 DECLARE_NAPI_PROPERTY("OPTION_AUTO_CAP_SENTENCES", GetJsConstProperty(env, static_cast<uint32_t>(8))),
45 DECLARE_NAPI_PROPERTY("OPTION_AUTO_WORDS", GetJsConstProperty(env, static_cast<uint32_t>(4))),
46 DECLARE_NAPI_PROPERTY("OPTION_MULTI_LINE", GetJsConstProperty(env, static_cast<uint32_t>(1))),
47 DECLARE_NAPI_PROPERTY("OPTION_NO_FULLSCREEN", GetJsConstProperty(env, static_cast<uint32_t>(10))),
48 DECLARE_NAPI_PROPERTY("CURSOR_UP", GetJsConstProperty(env, static_cast<uint32_t>(1))),
49 DECLARE_NAPI_PROPERTY("CURSOR_DOWN", GetJsConstProperty(env, static_cast<uint32_t>(2))),
50 DECLARE_NAPI_PROPERTY("CURSOR_LEFT", GetJsConstProperty(env, static_cast<uint32_t>(3))),
51 DECLARE_NAPI_PROPERTY("CURSOR_RIGHT", GetJsConstProperty(env, static_cast<uint32_t>(4))),
52
53 DECLARE_NAPI_PROPERTY("FLAG_SELECTING", GetJsConstProperty(env, static_cast<uint32_t>(2))),
54 DECLARE_NAPI_PROPERTY("FLAG_SINGLE_LINE", GetJsConstProperty(env, static_cast<uint32_t>(1))),
55
56 DECLARE_NAPI_PROPERTY("DISPLAY_MODE_PART", GetJsConstProperty(env, static_cast<uint32_t>(0))),
57 DECLARE_NAPI_PROPERTY("DISPLAY_MODE_FULL", GetJsConstProperty(env, static_cast<uint32_t>(1))),
58 DECLARE_NAPI_PROPERTY("WINDOW_TYPE_INPUT_METHOD_FLOAT", GetJsConstProperty(env, static_cast<uint32_t>(2105))),
59
60 DECLARE_NAPI_FUNCTION("createKeyboardDelegate", CreateKeyboardDelegate),
61 DECLARE_NAPI_FUNCTION("getKeyboardDelegate", GetKeyboardDelegate),
62 };
63 NAPI_CALL(
64 env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
65
66 napi_property_descriptor properties[] = {
67 DECLARE_NAPI_FUNCTION("on", Subscribe),
68 DECLARE_NAPI_FUNCTION("off", UnSubscribe),
69 };
70 napi_value cons = nullptr;
71 NAPI_CALL(env, napi_define_class(env, KDS_CLASS_NAME.c_str(), KDS_CLASS_NAME.size(), JsConstructor, nullptr,
72 sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
73 NAPI_CALL(env, napi_create_reference(env, cons, 1, &KDSRef_));
74 NAPI_CALL(env, napi_set_named_property(env, exports, KDS_CLASS_NAME.c_str(), cons));
75 return exports;
76 };
77
GetJsConstProperty(napi_env env,uint32_t num)78 napi_value JsKeyboardDelegateSetting::GetJsConstProperty(napi_env env, uint32_t num)
79 {
80 napi_value jsNumber = nullptr;
81 napi_create_int32(env, num, &jsNumber);
82 return jsNumber;
83 };
84
GetKeyboardDelegateSetting()85 std::shared_ptr<JsKeyboardDelegateSetting> JsKeyboardDelegateSetting::GetKeyboardDelegateSetting()
86 {
87 if (keyboardDelegate_ == nullptr) {
88 std::lock_guard<std::mutex> lock(keyboardMutex_);
89 if (keyboardDelegate_ == nullptr) {
90 auto delegate = std::make_shared<JsKeyboardDelegateSetting>();
91 if (delegate == nullptr) {
92 IMSA_HILOGE("keyboard delegate nullptr");
93 return nullptr;
94 }
95 keyboardDelegate_ = delegate;
96 }
97 }
98 return keyboardDelegate_;
99 }
100
InitKeyboardDelegate()101 bool JsKeyboardDelegateSetting::InitKeyboardDelegate()
102 {
103 if (InputMethodAbility::GetInstance()->SetCoreAndAgent() != ErrorCode::NO_ERROR) {
104 return false;
105 }
106 auto delegate = GetKeyboardDelegateSetting();
107 if (delegate == nullptr) {
108 return false;
109 }
110 InputMethodAbility::GetInstance()->setKdListener(delegate);
111 return true;
112 }
113
JsConstructor(napi_env env,napi_callback_info info)114 napi_value JsKeyboardDelegateSetting::JsConstructor(napi_env env, napi_callback_info info)
115 {
116 IMSA_HILOGI("run in JsConstructor");
117 napi_value thisVar = nullptr;
118 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
119 auto delegate = GetKeyboardDelegateSetting();
120 if (delegate == nullptr || !InitKeyboardDelegate()) {
121 IMSA_HILOGE("get delegate nullptr");
122 napi_value result = nullptr;
123 napi_get_null(env, &result);
124 return result;
125 }
126 napi_status status = napi_wrap(
127 env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr);
128 if (status != napi_ok) {
129 IMSA_HILOGE("JsKeyboardDelegateSetting napi_wrap failed: %{public}d", status);
130 return nullptr;
131 }
132 if (delegate->loop_ == nullptr) {
133 napi_get_uv_event_loop(env, &delegate->loop_);
134 }
135 return thisVar;
136 };
137
CreateKeyboardDelegate(napi_env env,napi_callback_info info)138 napi_value JsKeyboardDelegateSetting::CreateKeyboardDelegate(napi_env env, napi_callback_info info)
139 {
140 return GetKDInstance(env, info);
141 }
142
GetKeyboardDelegate(napi_env env,napi_callback_info info)143 napi_value JsKeyboardDelegateSetting::GetKeyboardDelegate(napi_env env, napi_callback_info info)
144 {
145 return GetKDInstance(env, info);
146 }
147
GetKDInstance(napi_env env,napi_callback_info info)148 napi_value JsKeyboardDelegateSetting::GetKDInstance(napi_env env, napi_callback_info info)
149 {
150 napi_value instance = nullptr;
151 napi_value cons = nullptr;
152 if (napi_get_reference_value(env, KDSRef_, &cons) != napi_ok) {
153 IMSA_HILOGE("failed to get reference value");
154 return nullptr;
155 }
156 if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
157 IMSA_HILOGE("failed to new instance");
158 return nullptr;
159 }
160 return instance;
161 }
162
GetStringProperty(napi_env env,napi_value jsString)163 std::string JsKeyboardDelegateSetting::GetStringProperty(napi_env env, napi_value jsString)
164 {
165 char propValue[MAX_VALUE_LEN] = { 0 };
166 size_t propLen;
167 if (napi_get_value_string_utf8(env, jsString, propValue, MAX_VALUE_LEN, &propLen) != napi_ok) {
168 IMSA_HILOGE("GetStringProperty error");
169 }
170 return std::string(propValue);
171 }
172
RegisterListener(napi_value callback,std::string type,std::shared_ptr<JSCallbackObject> callbackObj)173 void JsKeyboardDelegateSetting::RegisterListener(
174 napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj)
175 {
176 IMSA_HILOGI("RegisterListener %{public}s", type.c_str());
177 std::lock_guard<std::recursive_mutex> lock(mutex_);
178 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
179 IMSA_HILOGE("methodName %{public}s not registered!", type.c_str());
180 }
181 auto callbacks = jsCbMap_[type];
182 bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr<JSCallbackObject> cb) {
183 return Equals(cb->env_, callback, cb->callback_, cb->threadId_);
184 });
185 if (ret) {
186 IMSA_HILOGE("JsKeyboardDelegateSetting::RegisterListener callback already registered!");
187 return;
188 }
189
190 IMSA_HILOGI("Add %{public}s callbackObj into jsCbMap_", type.c_str());
191 jsCbMap_[type].push_back(std::move(callbackObj));
192 }
193
UnRegisterListener(napi_value callback,std::string type)194 void JsKeyboardDelegateSetting::UnRegisterListener(napi_value callback, std::string type)
195 {
196 IMSA_HILOGI("UnRegisterListener %{public}s", type.c_str());
197 std::lock_guard<std::recursive_mutex> lock(mutex_);
198 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
199 IMSA_HILOGE("methodName %{public}s not unRegisterted!", type.c_str());
200 return;
201 }
202
203 if (callback == nullptr) {
204 jsCbMap_.erase(type);
205 IMSA_HILOGE("callback is nullptr");
206 return;
207 }
208
209 for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
210 if ((callback != nullptr) && (Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_))) {
211 jsCbMap_[type].erase(item);
212 break;
213 }
214 }
215 if (jsCbMap_[type].empty()) {
216 jsCbMap_.erase(type);
217 }
218 }
219
GetNative(napi_env env,napi_callback_info info)220 JsKeyboardDelegateSetting *JsKeyboardDelegateSetting::GetNative(napi_env env, napi_callback_info info)
221 {
222 size_t argc = AsyncCall::ARGC_MAX;
223 void *native = nullptr;
224 napi_value self = nullptr;
225 napi_value argv[AsyncCall::ARGC_MAX] = { nullptr };
226 napi_status status = napi_invalid_arg;
227 status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
228 if (self == nullptr && argc >= AsyncCall::ARGC_MAX) {
229 IMSA_HILOGE("napi_get_cb_info failed");
230 return nullptr;
231 }
232
233 status = napi_unwrap(env, self, &native);
234 NAPI_ASSERT(env, (status == napi_ok && native != nullptr), "napi_unwrap failed!");
235 return reinterpret_cast<JsKeyboardDelegateSetting *>(native);
236 }
237
Subscribe(napi_env env,napi_callback_info info)238 napi_value JsKeyboardDelegateSetting::Subscribe(napi_env env, napi_callback_info info)
239 {
240 size_t argc = ARGC_TWO;
241 napi_value argv[ARGC_TWO] = { nullptr };
242 napi_value thisVar = nullptr;
243 void *data = nullptr;
244 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
245 if (argc != ARGC_TWO) {
246 JsUtils::ThrowException(
247 env, IMFErrorCode::EXCEPTION_PARAMCHECK, "Wrong number of arguments, requires 2", TypeCode::TYPE_NONE);
248 }
249
250 napi_valuetype valuetype;
251 NAPI_CALL(env, napi_typeof(env, argv[ARGC_ZERO], &valuetype));
252 if (valuetype != napi_string) {
253 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'type'", TypeCode::TYPE_STRING);
254 return nullptr;
255 }
256 std::string type = GetStringProperty(env, argv[ARGC_ZERO]);
257 IMSA_HILOGE("event type is: %{public}s", type.c_str());
258
259 valuetype = napi_undefined;
260 napi_typeof(env, argv[ARGC_ONE], &valuetype);
261 if (valuetype != napi_function) {
262 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'callback'", TypeCode::TYPE_FUNCTION);
263 return nullptr;
264 }
265
266 auto engine = GetNative(env, info);
267 if (engine == nullptr) {
268 return nullptr;
269 }
270 std::shared_ptr<JSCallbackObject> callback =
271 std::make_shared<JSCallbackObject>(env, argv[1], std::this_thread::get_id());
272 engine->RegisterListener(argv[ARGC_ONE], type, callback);
273
274 napi_value result = nullptr;
275 napi_get_null(env, &result);
276 return result;
277 }
278
UnSubscribe(napi_env env,napi_callback_info info)279 napi_value JsKeyboardDelegateSetting::UnSubscribe(napi_env env, napi_callback_info info)
280 {
281 size_t argc = ARGC_TWO;
282 napi_value argv[ARGC_TWO] = { nullptr };
283 napi_value thisVar = nullptr;
284 void *data = nullptr;
285 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
286 if (argc != ARGC_ONE && argc != ARGC_TWO) {
287 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "Wrong number of arguments, requires 1 or 2",
288 TypeCode::TYPE_NONE);
289 return nullptr;
290 }
291
292 napi_valuetype valuetype;
293 NAPI_CALL(env, napi_typeof(env, argv[ARGC_ZERO], &valuetype));
294 if (valuetype != napi_string) {
295 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'type'", TypeCode::TYPE_STRING);
296 return nullptr;
297 }
298 std::string type = GetStringProperty(env, argv[ARGC_ZERO]);
299
300 auto delegate = GetNative(env, info);
301 if (delegate == nullptr) {
302 return nullptr;
303 }
304
305 if (argc == ARGC_TWO) {
306 valuetype = napi_undefined;
307 napi_typeof(env, argv[ARGC_ONE], &valuetype);
308 if (valuetype != napi_function) {
309 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'callback'", TypeCode::TYPE_FUNCTION);
310 return nullptr;
311 }
312 }
313 delegate->UnRegisterListener(argv[ARGC_ONE], type);
314 napi_value result = nullptr;
315 napi_get_null(env, &result);
316 return result;
317 }
318
Equals(napi_env env,napi_value value,napi_ref copy,std::thread::id threadId)319 bool JsKeyboardDelegateSetting::Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId)
320 {
321 if (copy == nullptr) {
322 return (value == nullptr);
323 }
324
325 if (threadId != std::this_thread::get_id()) {
326 IMSA_HILOGD("napi_value can not be compared");
327 return false;
328 }
329
330 napi_value copyValue = nullptr;
331 napi_get_reference_value(env, copy, ©Value);
332
333 bool isEquals = false;
334 napi_strict_equals(env, value, copyValue, &isEquals);
335 IMSA_HILOGD("value compare result: %{public}d", isEquals);
336 return isEquals;
337 }
338
GetResultOnKeyEvent(napi_env env,int32_t keyCode,int32_t keyStatus)339 napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t keyCode, int32_t keyStatus)
340 {
341 napi_value KeyboardDelegate = nullptr;
342 napi_create_object(env, &KeyboardDelegate);
343
344 napi_value jsKeyCode = nullptr;
345 napi_create_int32(env, keyCode, &jsKeyCode);
346 napi_set_named_property(env, KeyboardDelegate, "keyCode", jsKeyCode);
347
348 napi_value jsKeyAction = nullptr;
349 napi_create_int32(env, keyStatus, &jsKeyAction);
350 napi_set_named_property(env, KeyboardDelegate, "keyAction", jsKeyAction);
351
352 return KeyboardDelegate;
353 }
354
OnKeyEvent(int32_t keyCode,int32_t keyStatus)355 bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus)
356 {
357 IMSA_HILOGD("run in");
358 KeyEventPara para{ keyCode, keyStatus, false };
359 std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp");
360 auto isDone = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
361 uv_work_t *work = GetUVwork(type, [¶, isDone](UvEntry &entry) {
362 entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent };
363 entry.isDone = isDone;
364 });
365 if (work == nullptr) {
366 IMSA_HILOGE("failed to get uv work");
367 return false;
368 }
369 uv_queue_work(
370 loop_, work, [](uv_work_t *work) {},
371 [](uv_work_t *work, int status) {
372 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
373 delete data;
374 delete work;
375 });
376 auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc,
377 std::shared_ptr<JSCallbackObject> item) -> bool {
378 if (argc == 0) {
379 return false;
380 }
381 napi_value jsObject =
382 GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus);
383 if (jsObject == nullptr) {
384 IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr");
385 return false;
386 }
387 args[ARGC_ZERO] = { jsObject };
388 return true;
389 };
390 bool isOnKeyEvent = JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getKeyEventProperty);
391 entry->isDone->SetValue(isOnKeyEvent);
392 });
393 return isDone->GetValue();
394 }
395
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)396 void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
397 {
398 IMSA_HILOGD("run in");
399 CursorPara para{ positionX, positionY, height };
400 std::string type = "cursorContextChange";
401 uv_work_t *work = GetUVwork(type, [¶](UvEntry &entry) {
402 entry.curPara.positionX = para.positionX;
403 entry.curPara.positionY = para.positionY;
404 entry.curPara.height = para.height;
405 });
406 if (work == nullptr) {
407 IMSA_HILOGD("failed to get uv entry");
408 return;
409 }
410 uv_queue_work(
411 loop_, work, [](uv_work_t *work) {},
412 [](uv_work_t *work, int status) {
413 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
414 delete data;
415 delete work;
416 });
417
418 auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc,
419 std::shared_ptr<JSCallbackObject> item) -> bool {
420 if (argc < 3) {
421 return false;
422 }
423 napi_create_int32(item->env_, entry->curPara.positionX, &args[ARGC_ZERO]);
424 napi_create_int32(item->env_, entry->curPara.positionY, &args[ARGC_ONE]);
425 napi_create_int32(item->env_, entry->curPara.height, &args[ARGC_TWO]);
426 return true;
427 };
428 JsUtils::TraverseCallback(entry->vecCopy, ARGC_THREE, getCursorUpdateProperty);
429 });
430 }
431
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)432 void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
433 {
434 IMSA_HILOGD("run in");
435 SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd };
436 std::string type = "selectionChange";
437 uv_work_t *work = GetUVwork(type, [¶](UvEntry &entry) {
438 entry.selPara.oldBegin = para.oldBegin;
439 entry.selPara.oldEnd = para.oldEnd;
440 entry.selPara.newBegin = para.newBegin;
441 entry.selPara.newEnd = para.newEnd;
442 });
443 if (work == nullptr) {
444 IMSA_HILOGD("failed to get uv entry");
445 return;
446 }
447 uv_queue_work(
448 loop_, work, [](uv_work_t *work) {},
449 [](uv_work_t *work, int status) {
450 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
451 delete data;
452 delete work;
453 });
454
455 auto getSelectionChangeProperty = [entry](napi_value *args, uint8_t argc,
456 std::shared_ptr<JSCallbackObject> item) -> bool {
457 if (argc < 4) {
458 return false;
459 }
460 napi_create_int32(item->env_, entry->selPara.oldBegin, &args[ARGC_ZERO]);
461 napi_create_int32(item->env_, entry->selPara.oldEnd, &args[ARGC_ONE]);
462 napi_create_int32(item->env_, entry->selPara.newBegin, &args[ARGC_TWO]);
463 napi_create_int32(item->env_, entry->selPara.newEnd, &args[ARGC_THREE]);
464 return true;
465 };
466 JsUtils::TraverseCallback(entry->vecCopy, ARGC_FOUR, getSelectionChangeProperty);
467 });
468 }
469
OnTextChange(const std::string & text)470 void JsKeyboardDelegateSetting::OnTextChange(const std::string &text)
471 {
472 IMSA_HILOGD("run in");
473 std::string type = "textChange";
474 uv_work_t *work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; });
475 if (work == nullptr) {
476 IMSA_HILOGD("failed to get uv entry");
477 return;
478 }
479 uv_queue_work(
480 loop_, work, [](uv_work_t *work) {},
481 [](uv_work_t *work, int status) {
482 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
483 delete data;
484 delete work;
485 });
486
487 auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc,
488 std::shared_ptr<JSCallbackObject> item) -> bool {
489 if (argc == 0) {
490 return false;
491 }
492 napi_create_string_utf8(item->env_, entry->text.c_str(), NAPI_AUTO_LENGTH, &args[ARGC_ZERO]);
493 return true;
494 };
495 JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getTextChangeProperty);
496 });
497 }
498
GetUVwork(const std::string & type,EntrySetter entrySetter)499 uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
500 {
501 IMSA_HILOGD("run in, type: %{public}s", type.c_str());
502 UvEntry *entry = nullptr;
503 {
504 std::lock_guard<std::recursive_mutex> lock(mutex_);
505
506 if (jsCbMap_[type].empty()) {
507 IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
508 return nullptr;
509 }
510 entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
511 if (entry == nullptr) {
512 IMSA_HILOGE("entry ptr is nullptr!");
513 return nullptr;
514 }
515 if (entrySetter != nullptr) {
516 entrySetter(*entry);
517 }
518 }
519 uv_work_t *work = new (std::nothrow) uv_work_t;
520 if (work == nullptr) {
521 IMSA_HILOGE("entry ptr is nullptr!");
522 return nullptr;
523 }
524 work->data = entry;
525 return work;
526 }
527 } // namespace MiscServices
528 } // namespace OHOS