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_input_method_engine_setting.h"
17
18 #include <thread>
19
20 #include "input_method_ability.h"
21 #include "input_method_property.h"
22 #include "input_method_utils.h"
23 #include "js_keyboard_controller_engine.h"
24 #include "js_text_input_client_engine.h"
25 #include "js_utils.h"
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28
29 namespace OHOS {
30 namespace MiscServices {
31 constexpr size_t ARGC_ZERO = 0;
32 constexpr size_t ARGC_ONE = 1;
33 constexpr size_t ARGC_TWO = 2;
34 constexpr size_t ARGC_MAX = 6;
35 constexpr int32_t V9_FLAG = 1;
36 constexpr int32_t ORIGINAL_FLAG = 2;
37 const std::string JsInputMethodEngineSetting::IMES_CLASS_NAME = "InputMethodEngine";
38 thread_local napi_ref JsInputMethodEngineSetting::IMESRef_ = nullptr;
39
40 std::mutex JsInputMethodEngineSetting::engineMutex_;
41 std::shared_ptr<JsInputMethodEngineSetting> JsInputMethodEngineSetting::inputMethodEngine_ { nullptr };
42
Init(napi_env env,napi_value exports)43 napi_value JsInputMethodEngineSetting::Init(napi_env env, napi_value exports)
44 {
45 napi_property_descriptor descriptor[] = {
46 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_UNSPECIFIED",
47 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::UNSPECIFIED))),
48 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_GO",
49 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::GO))),
50 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_SEARCH",
51 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::SEARCH))),
52 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_SEND",
53 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::SEND))),
54 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_NEXT",
55 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::NEXT))),
56 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_DONE",
57 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::DONE))),
58 DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_PREVIOUS",
59 GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::PREVIOUS))),
60
61 DECLARE_NAPI_PROPERTY("PATTERN_NULL",
62 GetIntJsConstProperty(env, static_cast<int32_t>(TextInputType::NONE))),
63 DECLARE_NAPI_PROPERTY("PATTERN_TEXT",
64 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::TEXT))),
65 DECLARE_NAPI_PROPERTY("PATTERN_NUMBER",
66 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::NUMBER))),
67 DECLARE_NAPI_PROPERTY("PATTERN_PHONE",
68 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::PHONE))),
69 DECLARE_NAPI_PROPERTY("PATTERN_DATETIME",
70 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::DATETIME))),
71 DECLARE_NAPI_PROPERTY("PATTERN_EMAIL",
72 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::EMAIL_ADDRESS))),
73 DECLARE_NAPI_PROPERTY("PATTERN_URI",
74 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::URL))),
75 DECLARE_NAPI_PROPERTY("PATTERN_PASSWORD",
76 GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::VISIBLE_PASSWORD))),
77
78 DECLARE_NAPI_FUNCTION("MoveCursor", MoveCursor),
79 DECLARE_NAPI_FUNCTION("getInputMethodEngine", GetInputMethodEngine),
80 DECLARE_NAPI_FUNCTION("getInputMethodAbility", GetInputMethodAbility),
81 };
82 NAPI_CALL(
83 env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
84
85 napi_property_descriptor properties[] = {
86 DECLARE_NAPI_FUNCTION("on", Subscribe),
87 DECLARE_NAPI_FUNCTION("off", UnSubscribe),
88 };
89 napi_value cons = nullptr;
90 NAPI_CALL(env, napi_define_class(env, IMES_CLASS_NAME.c_str(), IMES_CLASS_NAME.size(),
91 JsConstructor, nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
92 NAPI_CALL(env, napi_create_reference(env, cons, 1, &IMESRef_));
93 NAPI_CALL(env, napi_set_named_property(env, exports, IMES_CLASS_NAME.c_str(), cons));
94 return exports;
95 };
96
GetJsConstProperty(napi_env env,uint32_t num)97 napi_value JsInputMethodEngineSetting::GetJsConstProperty(napi_env env, uint32_t num)
98 {
99 napi_value jsNumber = nullptr;
100 napi_create_uint32(env, num, &jsNumber);
101 return jsNumber;
102 }
103
GetIntJsConstProperty(napi_env env,int32_t num)104 napi_value JsInputMethodEngineSetting::GetIntJsConstProperty(napi_env env, int32_t num)
105 {
106 napi_value jsNumber = nullptr;
107 napi_create_int32(env, num, &jsNumber);
108 return jsNumber;
109 }
110
GetInputMethodEngineSetting()111 std::shared_ptr<JsInputMethodEngineSetting> JsInputMethodEngineSetting::GetInputMethodEngineSetting()
112 {
113 if (inputMethodEngine_ == nullptr) {
114 std::lock_guard<std::mutex> lock(engineMutex_);
115 if (inputMethodEngine_ == nullptr) {
116 auto engine = std::make_shared<JsInputMethodEngineSetting>();
117 if (engine == nullptr) {
118 IMSA_HILOGE("input method engine nullptr");
119 return nullptr;
120 }
121 inputMethodEngine_ = engine;
122 InputMethodAbility::GetInstance()->setImeListener(inputMethodEngine_);
123 }
124 }
125 return inputMethodEngine_;
126 }
127
JsConstructor(napi_env env,napi_callback_info info)128 napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback_info info)
129 {
130 IMSA_HILOGI("run in JsConstructor");
131 napi_value thisVar = nullptr;
132 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
133 auto delegate = GetInputMethodEngineSetting();
134 if (delegate == nullptr) {
135 IMSA_HILOGE("get delegate nullptr");
136 JsUtils::ThrowException(
137 env, static_cast<int32_t>(IMFErrorCode::EXCEPTION_IMENGINE), "get delegate nullptr", TypeCode::TYPE_NONE);
138 napi_value result = nullptr;
139 napi_get_null(env, &result);
140 return result;
141 }
142 napi_status status = napi_wrap(
143 env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr);
144 if (status != napi_ok) {
145 IMSA_HILOGE("JsInputMethodEngineSetting napi_wrap failed: %{public}d", status);
146 return nullptr;
147 }
148 if (delegate->loop_ == nullptr) {
149 napi_get_uv_event_loop(env, &delegate->loop_);
150 }
151 return thisVar;
152 };
153
GetInputMethodAbility(napi_env env,napi_callback_info info)154 napi_value JsInputMethodEngineSetting::GetInputMethodAbility(napi_env env, napi_callback_info info)
155 {
156 return GetIMEInstance(env, info, V9_FLAG);
157 }
158
GetInputMethodEngine(napi_env env,napi_callback_info info)159 napi_value JsInputMethodEngineSetting::GetInputMethodEngine(napi_env env, napi_callback_info info)
160 {
161 return GetIMEInstance(env, info, ORIGINAL_FLAG);
162 }
163
GetIMEInstance(napi_env env,napi_callback_info info,int flag)164 napi_value JsInputMethodEngineSetting::GetIMEInstance(napi_env env, napi_callback_info info, int flag)
165 {
166 napi_value instance = nullptr;
167 napi_value cons = nullptr;
168
169 if (flag == V9_FLAG) {
170 size_t argc = ARGC_MAX;
171 napi_value argv[ARGC_MAX] = { nullptr };
172
173 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
174 if (argc != ARGC_ZERO) {
175 JsUtils::ThrowException(
176 env, IMFErrorCode::EXCEPTION_PARAMCHECK, "Wrong number of arguments, requires 0", TypeCode::TYPE_NONE);
177 return nullptr;
178 }
179 }
180
181 if (napi_get_reference_value(env, IMESRef_, &cons) != napi_ok) {
182 return nullptr;
183 }
184
185 if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
186 return nullptr;
187 }
188
189 return instance;
190 }
191
MoveCursor(napi_env env,napi_callback_info info)192 napi_value JsInputMethodEngineSetting::MoveCursor(napi_env env, napi_callback_info info)
193 {
194 size_t argc = ARGC_MAX;
195 napi_value argv[ARGC_MAX] = { nullptr };
196 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
197 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments, requires 1");
198
199 napi_valuetype valuetype;
200 NAPI_CALL(env, napi_typeof(env, argv[ARGC_ZERO], &valuetype));
201 NAPI_ASSERT(env, valuetype == napi_number, "type is not a number");
202
203 int32_t number;
204 if (napi_get_value_int32(env, argv[ARGC_ZERO], &number) != napi_ok) {
205 IMSA_HILOGE("GetNumberProperty error");
206 }
207
208 InputMethodAbility::GetInstance()->MoveCursor(number);
209
210 napi_value result = nullptr;
211 napi_get_null(env, &result);
212 return result;
213 }
214
GetStringProperty(napi_env env,napi_value jsString)215 std::string JsInputMethodEngineSetting::GetStringProperty(napi_env env, napi_value jsString)
216 {
217 char propValue[MAX_VALUE_LEN] = {0};
218 size_t propLen;
219 if (napi_get_value_string_utf8(env, jsString, propValue, MAX_VALUE_LEN, &propLen) != napi_ok) {
220 IMSA_HILOGE("GetStringProperty error");
221 return "";
222 }
223 return std::string(propValue);
224 }
225
RegisterListener(napi_value callback,std::string type,std::shared_ptr<JSCallbackObject> callbackObj)226 void JsInputMethodEngineSetting::RegisterListener(
227 napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj)
228 {
229 IMSA_HILOGI("RegisterListener %{public}s", type.c_str());
230 std::lock_guard<std::recursive_mutex> lock(mutex_);
231 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
232 IMSA_HILOGE("methodName: %{public}s not registertd!", type.c_str());
233 }
234 auto callbacks = jsCbMap_[type];
235 bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr<JSCallbackObject> cb) {
236 return Equals(cb->env_, callback, cb->callback_, cb->threadId_);
237 });
238 if (ret) {
239 IMSA_HILOGE("JsInputMethodEngineListener::RegisterListener callback already registered!");
240 return;
241 }
242
243 IMSA_HILOGI("Add %{public}s callbackObj into jsCbMap_", type.c_str());
244 jsCbMap_[type].push_back(std::move(callbackObj));
245 }
246
UnRegisterListener(napi_value callback,std::string type)247 void JsInputMethodEngineSetting::UnRegisterListener(napi_value callback, std::string type)
248 {
249 IMSA_HILOGI("UnRegisterListener %{public}s", type.c_str());
250 std::lock_guard<std::recursive_mutex> lock(mutex_);
251 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
252 IMSA_HILOGE("methodName: %{public}s already unRegisterted!", type.c_str());
253 return;
254 }
255
256 if (callback == nullptr) {
257 jsCbMap_.erase(type);
258 IMSA_HILOGE("callback is nullptr");
259 return;
260 }
261
262 for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
263 if (Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_)) {
264 jsCbMap_[type].erase(item);
265 break;
266 }
267 }
268
269 if (jsCbMap_[type].empty()) {
270 jsCbMap_.erase(type);
271 }
272 }
273
GetNative(napi_env env,napi_callback_info info)274 JsInputMethodEngineSetting *JsInputMethodEngineSetting::GetNative(napi_env env, napi_callback_info info)
275 {
276 size_t argc = ARGC_MAX;
277 void *native = nullptr;
278 napi_value self = nullptr;
279 napi_value argv[ARGC_MAX] = { nullptr };
280 napi_status status = napi_invalid_arg;
281 status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
282 if (self == nullptr && argc >= ARGC_MAX) {
283 IMSA_HILOGE("napi_get_cb_info failed");
284 return nullptr;
285 }
286
287 status = napi_unwrap(env, self, &native);
288 NAPI_ASSERT(env, (status == napi_ok && native != nullptr), "napi_unwrap failed!");
289 return reinterpret_cast<JsInputMethodEngineSetting*>(native);
290 }
291
Subscribe(napi_env env,napi_callback_info info)292 napi_value JsInputMethodEngineSetting::Subscribe(napi_env env, napi_callback_info info)
293 {
294 size_t argc = ARGC_TWO;
295 napi_value argv[ARGC_TWO] = { nullptr };
296 napi_value thisVar = nullptr;
297 void *data = nullptr;
298 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
299 if (argc != ARGC_TWO) {
300 JsUtils::ThrowException(
301 env, IMFErrorCode::EXCEPTION_PARAMCHECK, "Wrong number of arguments, requires 2", TypeCode::TYPE_NONE);
302 return nullptr;
303 }
304
305 napi_valuetype valuetype;
306 NAPI_CALL(env, napi_typeof(env, argv[ARGC_ZERO], &valuetype));
307 if (valuetype != napi_string) {
308 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'type'", TypeCode::TYPE_STRING);
309 return nullptr;
310 }
311 std::string type = GetStringProperty(env, argv[ARGC_ZERO]);
312 IMSA_HILOGE("event type is: %{public}s", type.c_str());
313
314 valuetype = napi_undefined;
315 napi_typeof(env, argv[ARGC_ONE], &valuetype);
316 if (valuetype != napi_function) {
317 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'callback'", TypeCode::TYPE_FUNCTION);
318 return nullptr;
319 }
320
321 auto engine = GetNative(env, info);
322 if (engine == nullptr) {
323 return nullptr;
324 }
325 std::shared_ptr<JSCallbackObject> callback =
326 std::make_shared<JSCallbackObject>(env, argv[ARGC_ONE], std::this_thread::get_id());
327 engine->RegisterListener(argv[ARGC_ONE], type, callback);
328
329 napi_value result = nullptr;
330 napi_get_null(env, &result);
331 return result;
332 }
333
UnSubscribe(napi_env env,napi_callback_info info)334 napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_info info)
335 {
336 size_t argc = ARGC_TWO;
337 napi_value argv[ARGC_TWO] = {nullptr};
338 napi_value thisVar = nullptr;
339 void *data = nullptr;
340 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
341 if (argc != ARGC_ONE && argc != ARGC_TWO) {
342 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "Wrong number of arguments, requires 1 or 2",
343 TypeCode::TYPE_NONE);
344 return nullptr;
345 }
346
347 napi_valuetype valuetype;
348 NAPI_CALL(env, napi_typeof(env, argv[ARGC_ZERO], &valuetype));
349 if (valuetype != napi_string) {
350 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'type'", TypeCode::TYPE_STRING);
351 return nullptr;
352 }
353 std::string type = GetStringProperty(env, argv[ARGC_ZERO]);
354 IMSA_HILOGE("event type is: %{public}s", type.c_str());
355
356 auto engine = GetNative(env, info);
357 if (engine == nullptr) {
358 return nullptr;
359 }
360
361 if (argc == ARGC_TWO) {
362 valuetype = napi_undefined;
363 napi_typeof(env, argv[ARGC_ONE], &valuetype);
364 if (valuetype != napi_function) {
365 JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "'callback'", TypeCode::TYPE_FUNCTION);
366 return nullptr;
367 }
368 }
369 engine->UnRegisterListener(argv[ARGC_ONE], type);
370 napi_value result = nullptr;
371 napi_get_null(env, &result);
372 return result;
373 }
374
Equals(napi_env env,napi_value value,napi_ref copy,std::thread::id threadId)375 bool JsInputMethodEngineSetting::Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId)
376 {
377 if (copy == nullptr) {
378 return (value == nullptr);
379 }
380
381 if (threadId != std::this_thread::get_id()) {
382 IMSA_HILOGD("napi_value can not be compared");
383 return false;
384 }
385
386 napi_value copyValue = nullptr;
387 napi_get_reference_value(env, copy, ©Value);
388
389 bool isEquals = false;
390 napi_strict_equals(env, value, copyValue, &isEquals);
391 IMSA_HILOGD("value compare result: %{public}d", isEquals);
392 return isEquals;
393 }
394
GetResultOnSetSubtype(napi_env env,const SubProperty & property)395 napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property)
396 {
397 napi_value subType = nullptr;
398 napi_create_object(env, &subType);
399
400 napi_value label = nullptr;
401 napi_create_string_utf8(env, property.label.c_str(), property.name.size(), &label);
402 napi_set_named_property(env, subType, "label", label);
403
404 napi_value name = nullptr;
405 napi_create_string_utf8(env, property.name.c_str(), property.name.size(), &name);
406 napi_set_named_property(env, subType, "name", name);
407
408 napi_value id = nullptr;
409 napi_create_string_utf8(env, property.id.c_str(), property.id.size(), &id);
410 napi_set_named_property(env, subType, "id", id);
411
412 napi_value mode = nullptr;
413 napi_create_string_utf8(env, property.mode.c_str(), property.mode.size(), &mode);
414 napi_set_named_property(env, subType, "mode", mode);
415
416 napi_value locale = nullptr;
417 napi_create_string_utf8(env, property.locale.c_str(), property.locale.size(), &locale);
418 napi_set_named_property(env, subType, "locale", locale);
419
420 napi_value language = nullptr;
421 napi_create_string_utf8(env, property.language.c_str(), property.language.size(), &language);
422 napi_set_named_property(env, subType, "language", language);
423
424 napi_value icon = nullptr;
425 napi_create_string_utf8(env, property.icon.c_str(), property.icon.size(), &icon);
426 napi_set_named_property(env, subType, "icon", icon);
427
428 napi_value iconId = nullptr;
429 napi_create_int32(env, property.iconId, &iconId);
430 napi_set_named_property(env, subType, "iconId", iconId);
431
432 napi_value extra = nullptr;
433 napi_create_object(env, &extra);
434 napi_set_named_property(env, subType, "extra", extra);
435
436 return subType;
437 }
438
OnInputStart()439 void JsInputMethodEngineSetting::OnInputStart()
440 {
441 IMSA_HILOGD("run in");
442 std::string type = "inputStart";
443 uv_work_t *work = GetUVwork(type);
444 if (work == nullptr) {
445 IMSA_HILOGD("failed to get uv entry");
446 return;
447 }
448 uv_queue_work(
449 loop_, work, [](uv_work_t *work) {},
450 [](uv_work_t *work, int status) {
451 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
452 delete data;
453 delete work;
454 });
455 if (entry == nullptr) {
456 IMSA_HILOGE("OnInputStart:: entryptr is null");
457 return;
458 }
459
460 auto getInputStartProperty = [](napi_value *args, uint8_t argc,
461 std::shared_ptr<JSCallbackObject> item) -> bool {
462 if (argc < 2) {
463 return false;
464 }
465 napi_value textInput = JsTextInputClientEngine::GetTextInputClientInstance(item->env_);
466 napi_value keyBoardController = JsKeyboardControllerEngine::GetKeyboardControllerInstance(item->env_);
467 if (keyBoardController == nullptr || textInput == nullptr) {
468 IMSA_HILOGE("get KBCins or TICins failed:");
469 return false;
470 }
471 args[ARGC_ZERO] = keyBoardController;
472 args[ARGC_ONE] = textInput;
473 return true;
474 };
475 JsUtils::TraverseCallback(entry->vecCopy, ARGC_TWO, getInputStartProperty);
476 });
477 }
478
OnKeyboardStatus(bool isShow)479 void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
480 {
481 std::string type = isShow ? "keyboardShow" : "keyboardHide";
482 IMSA_HILOGD("run in, %{public}s", type.c_str());
483 uv_work_t *work = GetUVwork(type);
484 if (work == nullptr) {
485 IMSA_HILOGD("failed to get uv entry");
486 return;
487 }
488 uv_queue_work(
489 loop_, work, [](uv_work_t *work) {},
490 [](uv_work_t *work, int status) {
491 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
492 delete data;
493 delete work;
494 });
495
496 auto getKeyboardStatusProperty = [](napi_value *args, uint8_t argc,
497 std::shared_ptr<JSCallbackObject> item) -> bool {
498 if (argc == 0) {
499 return false;
500 }
501 args[ARGC_ZERO] = nullptr;
502 return true;
503 };
504 JsUtils::TraverseCallback(entry->vecCopy, ARGC_ZERO, getKeyboardStatusProperty);
505 });
506 }
507
OnInputStop(const std::string & imeId)508 void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId)
509 {
510 IMSA_HILOGD("run in");
511 std::string type = "inputStop";
512 uv_work_t *work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; });
513 if (work == nullptr) {
514 IMSA_HILOGD("failed to get uv entry");
515 return;
516 }
517 uv_queue_work(
518 loop_, work, [](uv_work_t *work) {},
519 [](uv_work_t *work, int status) {
520 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
521 delete data;
522 delete work;
523 });
524 if (entry == nullptr) {
525 IMSA_HILOGE("OnInputStop:: entryptr is null");
526 return;
527 }
528
529 auto getInputStopProperty = [entry](napi_value *args, uint8_t argc,
530 std::shared_ptr<JSCallbackObject> item) -> bool {
531 if (argc == 0) {
532 return false;
533 }
534 napi_create_string_utf8(item->env_, entry->imeid.c_str(), NAPI_AUTO_LENGTH, &args[ARGC_ZERO]);
535 return true;
536 };
537 JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getInputStopProperty);
538 });
539 }
540
OnSetCallingWindow(uint32_t windowId)541 void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
542 {
543 IMSA_HILOGD("run in");
544 std::string type = "setCallingWindow";
545 uv_work_t *work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; });
546 if (work == nullptr) {
547 IMSA_HILOGD("failed to get uv entry");
548 return;
549 }
550 uv_queue_work(
551 loop_, work, [](uv_work_t *work) {},
552 [](uv_work_t *work, int status) {
553 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
554 delete data;
555 delete work;
556 });
557 if (entry == nullptr) {
558 IMSA_HILOGE("setCallingWindow:: entryptr is null");
559 return;
560 }
561
562 auto getCallingWindowProperty = [entry](napi_value *args, uint8_t argc,
563 std::shared_ptr<JSCallbackObject> item) -> bool {
564 if (argc == 0) {
565 return false;
566 }
567 napi_create_int32(item->env_, entry->windowid, &args[ARGC_ZERO]);
568 return true;
569 };
570 JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getCallingWindowProperty);
571 });
572 }
573
OnSetSubtype(const SubProperty & property)574 void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
575 {
576 IMSA_HILOGD("run in");
577 std::string type = "setSubtype";
578 uv_work_t *work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; });
579 if (work == nullptr) {
580 IMSA_HILOGD("failed to get uv entry");
581 return;
582 }
583 uv_queue_work(
584 loop_, work, [](uv_work_t *work) {},
585 [](uv_work_t *work, int status) {
586 std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
587 delete data;
588 delete work;
589 });
590 if (entry == nullptr) {
591 IMSA_HILOGE("OnSetSubtype:: entryptr is null");
592 return;
593 }
594
595 auto getSubtypeProperty = [entry](napi_value *args, uint8_t argc,
596 std::shared_ptr<JSCallbackObject> item) -> bool {
597 if (argc == 0) {
598 return false;
599 }
600 napi_value jsObject = GetResultOnSetSubtype(item->env_, entry->subProperty);
601 if (jsObject == nullptr) {
602 IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr");
603 return false;
604 }
605 args[ARGC_ZERO] = { jsObject };
606 return true;
607 };
608 JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getSubtypeProperty);
609 });
610 }
611
GetUVwork(const std::string & type,EntrySetter entrySetter)612 uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
613 {
614 IMSA_HILOGD("run in, type: %{public}s", type.c_str());
615 UvEntry *entry = nullptr;
616 {
617 std::lock_guard<std::recursive_mutex> lock(mutex_);
618
619 if (jsCbMap_[type].empty()) {
620 IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
621 return nullptr;
622 }
623 entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
624 if (entry == nullptr) {
625 IMSA_HILOGE("entry ptr is nullptr!");
626 return nullptr;
627 }
628 if (entrySetter != nullptr) {
629 entrySetter(*entry);
630 }
631 }
632 uv_work_t *work = new (std::nothrow) uv_work_t;
633 if (work == nullptr) {
634 IMSA_HILOGE("entry ptr is nullptr!");
635 return nullptr;
636 }
637 work->data = entry;
638 return work;
639 }
640 } // namespace MiscServices
641 } // namespace OHOS
642