• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "js_callback_handler.h"
21 #include "event_checker.h"
22 #include "input_method_ability.h"
23 #include "input_method_property.h"
24 #include "input_method_utils.h"
25 #include "js_keyboard_controller_engine.h"
26 #include "js_runtime_utils.h"
27 #include "js_text_input_client_engine.h"
28 #include "js_util.h"
29 #include "napi/native_api.h"
30 #include "napi/native_node_api.h"
31 #include "napi_base_context.h"
32 
33 namespace OHOS {
34 namespace MiscServices {
35 constexpr size_t ARGC_ONE = 1;
36 constexpr size_t ARGC_TWO = 2;
37 constexpr size_t ARGC_MAX = 6;
38 const std::string JsInputMethodEngineSetting::IMES_CLASS_NAME = "InputMethodEngine";
39 thread_local napi_ref JsInputMethodEngineSetting::IMESRef_ = nullptr;
40 
41 std::mutex JsInputMethodEngineSetting::engineMutex_;
42 std::shared_ptr<JsInputMethodEngineSetting> JsInputMethodEngineSetting::inputMethodEngine_{ nullptr };
43 
Init(napi_env env,napi_value exports)44 napi_value JsInputMethodEngineSetting::Init(napi_env env, napi_value exports)
45 {
46     napi_property_descriptor descriptor[] = {
47         DECLARE_NAPI_PROPERTY(
48             "ENTER_KEY_TYPE_UNSPECIFIED", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::UNSPECIFIED))),
49         DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_GO", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::GO))),
50         DECLARE_NAPI_PROPERTY(
51             "ENTER_KEY_TYPE_SEARCH", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::SEARCH))),
52         DECLARE_NAPI_PROPERTY(
53             "ENTER_KEY_TYPE_SEND", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::SEND))),
54         DECLARE_NAPI_PROPERTY(
55             "ENTER_KEY_TYPE_NEXT", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::NEXT))),
56         DECLARE_NAPI_PROPERTY(
57             "ENTER_KEY_TYPE_DONE", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::DONE))),
58         DECLARE_NAPI_PROPERTY(
59             "ENTER_KEY_TYPE_PREVIOUS", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::PREVIOUS))),
60 
61         DECLARE_NAPI_PROPERTY("PATTERN_NULL", GetIntJsConstProperty(env, static_cast<int32_t>(TextInputType::NONE))),
62         DECLARE_NAPI_PROPERTY("PATTERN_TEXT", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::TEXT))),
63         DECLARE_NAPI_PROPERTY("PATTERN_NUMBER", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::NUMBER))),
64         DECLARE_NAPI_PROPERTY("PATTERN_PHONE", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::PHONE))),
65         DECLARE_NAPI_PROPERTY(
66             "PATTERN_DATETIME", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::DATETIME))),
67         DECLARE_NAPI_PROPERTY(
68             "PATTERN_EMAIL", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::EMAIL_ADDRESS))),
69         DECLARE_NAPI_PROPERTY("PATTERN_URI", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::URL))),
70         DECLARE_NAPI_PROPERTY(
71             "PATTERN_PASSWORD", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::VISIBLE_PASSWORD))),
72 
73         DECLARE_NAPI_FUNCTION("getInputMethodEngine", GetInputMethodEngine),
74         DECLARE_NAPI_FUNCTION("getInputMethodAbility", GetInputMethodAbility),
75         DECLARE_NAPI_STATIC_PROPERTY("PanelType", GetJsPanelTypeProperty(env)),
76         DECLARE_NAPI_STATIC_PROPERTY("PanelFlag", GetJsPanelFlagProperty(env)),
77         DECLARE_NAPI_STATIC_PROPERTY("Direction", GetJsDirectionProperty(env)),
78         DECLARE_NAPI_STATIC_PROPERTY("ExtendAction", GetJsExtendActionProperty(env)),
79     };
80     NAPI_CALL(
81         env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
82 
83     napi_property_descriptor properties[] = {
84         DECLARE_NAPI_FUNCTION("on", Subscribe),
85         DECLARE_NAPI_FUNCTION("off", UnSubscribe),
86         DECLARE_NAPI_FUNCTION("createPanel", CreatePanel),
87         DECLARE_NAPI_FUNCTION("destroyPanel", DestroyPanel),
88     };
89     napi_value cons = nullptr;
90     NAPI_CALL(env, napi_define_class(env, IMES_CLASS_NAME.c_str(), IMES_CLASS_NAME.size(), JsConstructor, nullptr,
91                        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 
GetJsPanelTypeProperty(napi_env env)111 napi_value JsInputMethodEngineSetting::GetJsPanelTypeProperty(napi_env env)
112 {
113     napi_value panelType = nullptr;
114     napi_value typeSoftKeyboard = nullptr;
115     napi_value typeStatusBar = nullptr;
116     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelType::SOFT_KEYBOARD), &typeSoftKeyboard));
117     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelType::STATUS_BAR), &typeStatusBar));
118     NAPI_CALL(env, napi_create_object(env, &panelType));
119     NAPI_CALL(env, napi_set_named_property(env, panelType, "SOFT_KEYBOARD", typeSoftKeyboard));
120     NAPI_CALL(env, napi_set_named_property(env, panelType, "STATUS_BAR", typeStatusBar));
121     return panelType;
122 }
123 
GetJsPanelFlagProperty(napi_env env)124 napi_value JsInputMethodEngineSetting::GetJsPanelFlagProperty(napi_env env)
125 {
126     napi_value panelFlag = nullptr;
127     napi_value flagFixed = nullptr;
128     napi_value flagFloating = nullptr;
129     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelFlag::FLG_FIXED), &flagFixed));
130     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelFlag::FLG_FLOATING), &flagFloating));
131     NAPI_CALL(env, napi_create_object(env, &panelFlag));
132     NAPI_CALL(env, napi_set_named_property(env, panelFlag, "FLG_FIXED", flagFixed));
133     NAPI_CALL(env, napi_set_named_property(env, panelFlag, "FLG_FLOATING", flagFloating));
134     return panelFlag;
135 }
136 
GetJsDirectionProperty(napi_env env)137 napi_value JsInputMethodEngineSetting::GetJsDirectionProperty(napi_env env)
138 {
139     napi_value direction = nullptr;
140     napi_value cursorUp = nullptr;
141     napi_value cursorDown = nullptr;
142     napi_value cursorLeft = nullptr;
143     napi_value cursorRight = nullptr;
144     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::UP), &cursorUp));
145     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::DOWN), &cursorDown));
146     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::LEFT), &cursorLeft));
147     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::RIGHT), &cursorRight));
148     NAPI_CALL(env, napi_create_object(env, &direction));
149     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_UP", cursorUp));
150     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_DOWN", cursorDown));
151     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_LEFT", cursorLeft));
152     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_RIGHT", cursorRight));
153     return direction;
154 }
155 
GetJsExtendActionProperty(napi_env env)156 napi_value JsInputMethodEngineSetting::GetJsExtendActionProperty(napi_env env)
157 {
158     napi_value action = nullptr;
159     napi_value actionSelectAll = nullptr;
160     napi_value actionCut = nullptr;
161     napi_value actionCopy = nullptr;
162     napi_value actionPaste = nullptr;
163     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::SELECT_ALL), &actionSelectAll));
164     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::CUT), &actionCut));
165     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::COPY), &actionCopy));
166     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::PASTE), &actionPaste));
167     NAPI_CALL(env, napi_create_object(env, &action));
168     NAPI_CALL(env, napi_set_named_property(env, action, "SELECT_ALL", actionSelectAll));
169     NAPI_CALL(env, napi_set_named_property(env, action, "CUT", actionCut));
170     NAPI_CALL(env, napi_set_named_property(env, action, "COPY", actionCopy));
171     NAPI_CALL(env, napi_set_named_property(env, action, "PASTE", actionPaste));
172     return action;
173 }
174 
GetInputMethodEngineSetting()175 std::shared_ptr<JsInputMethodEngineSetting> JsInputMethodEngineSetting::GetInputMethodEngineSetting()
176 {
177     if (inputMethodEngine_ == nullptr) {
178         std::lock_guard<std::mutex> lock(engineMutex_);
179         if (inputMethodEngine_ == nullptr) {
180             auto engine = std::make_shared<JsInputMethodEngineSetting>();
181             if (engine == nullptr) {
182                 IMSA_HILOGE("input method engine nullptr");
183                 return nullptr;
184             }
185             inputMethodEngine_ = engine;
186         }
187     }
188     return inputMethodEngine_;
189 }
190 
InitInputMethodSetting()191 bool JsInputMethodEngineSetting::InitInputMethodSetting()
192 {
193     if (InputMethodAbility::GetInstance()->SetCoreAndAgent() != ErrorCode::NO_ERROR) {
194         return false;
195     }
196     auto engine = GetInputMethodEngineSetting();
197     if (engine == nullptr) {
198         return false;
199     }
200     InputMethodAbility::GetInstance()->SetImeListener(engine);
201     return true;
202 }
203 
JsConstructor(napi_env env,napi_callback_info cbinfo)204 napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback_info cbinfo)
205 {
206     IMSA_HILOGI("run in JsConstructor");
207     napi_value thisVar = nullptr;
208     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
209     auto setting = GetInputMethodEngineSetting();
210     if (setting == nullptr || !InitInputMethodSetting()) {
211         IMSA_HILOGE("failed to get setting");
212         napi_value result = nullptr;
213         napi_get_null(env, &result);
214         return result;
215     }
216     napi_status status = napi_wrap(
217         env, thisVar, setting.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr);
218     if (status != napi_ok) {
219         IMSA_HILOGE("JsInputMethodEngineSetting napi_wrap failed: %{public}d", status);
220         return nullptr;
221     }
222     if (setting->loop_ == nullptr) {
223         napi_get_uv_event_loop(env, &setting->loop_);
224     }
225     return thisVar;
226 };
227 
GetInputMethodAbility(napi_env env,napi_callback_info info)228 napi_value JsInputMethodEngineSetting::GetInputMethodAbility(napi_env env, napi_callback_info info)
229 {
230     return GetIMEInstance(env, info);
231 }
232 
GetInputMethodEngine(napi_env env,napi_callback_info info)233 napi_value JsInputMethodEngineSetting::GetInputMethodEngine(napi_env env, napi_callback_info info)
234 {
235     return GetIMEInstance(env, info);
236 }
237 
GetIMEInstance(napi_env env,napi_callback_info info)238 napi_value JsInputMethodEngineSetting::GetIMEInstance(napi_env env, napi_callback_info info)
239 {
240     napi_value instance = nullptr;
241     napi_value cons = nullptr;
242     if (napi_get_reference_value(env, IMESRef_, &cons) != napi_ok) {
243         IMSA_HILOGE("failed to get reference value");
244         return nullptr;
245     }
246     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
247         IMSA_HILOGE("failed to new instance");
248         return nullptr;
249     }
250     return instance;
251 }
252 
RegisterListener(napi_value callback,std::string type,std::shared_ptr<JSCallbackObject> callbackObj)253 void JsInputMethodEngineSetting::RegisterListener(
254     napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj)
255 {
256     IMSA_HILOGI("RegisterListener %{public}s", type.c_str());
257     std::lock_guard<std::recursive_mutex> lock(mutex_);
258     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
259         IMSA_HILOGE("methodName: %{public}s not registered!", type.c_str());
260     }
261     auto callbacks = jsCbMap_[type];
262     bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr<JSCallbackObject> cb) {
263         return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_);
264     });
265     if (ret) {
266         IMSA_HILOGE("JsInputMethodEngineListener::RegisterListener callback already registered!");
267         return;
268     }
269 
270     IMSA_HILOGI("Add %{public}s callbackObj into jsCbMap_", type.c_str());
271     jsCbMap_[type].push_back(std::move(callbackObj));
272 }
273 
UnRegisterListener(napi_value callback,std::string type)274 void JsInputMethodEngineSetting::UnRegisterListener(napi_value callback, std::string type)
275 {
276     IMSA_HILOGI("UnRegisterListener %{public}s", type.c_str());
277     std::lock_guard<std::recursive_mutex> lock(mutex_);
278     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
279         IMSA_HILOGE("methodName: %{public}s already unRegistered!", type.c_str());
280         return;
281     }
282 
283     if (callback == nullptr) {
284         jsCbMap_.erase(type);
285         IMSA_HILOGE("callback is nullptr");
286         return;
287     }
288 
289     for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
290         if (JsUtils::Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_)) {
291             jsCbMap_[type].erase(item);
292             break;
293         }
294     }
295 
296     if (jsCbMap_[type].empty()) {
297         jsCbMap_.erase(type);
298     }
299 }
300 
Subscribe(napi_env env,napi_callback_info info)301 napi_value JsInputMethodEngineSetting::Subscribe(napi_env env, napi_callback_info info)
302 {
303     size_t argc = ARGC_MAX;
304     napi_value argv[ARGC_MAX] = { nullptr };
305     napi_value thisVar = nullptr;
306     void *data = nullptr;
307     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
308     std::string type;
309     // 2 means least param num.
310     if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
311         || !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type)
312         || JsUtil::GetType(env, argv[1]) != napi_function) {
313         IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
314         return nullptr;
315     }
316     IMSA_HILOGD("Subscribe type:%{public}s.", type.c_str());
317     auto engine = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
318     if (engine == nullptr) {
319         return nullptr;
320     }
321     std::shared_ptr<JSCallbackObject> callback =
322         std::make_shared<JSCallbackObject>(env, argv[ARGC_ONE], std::this_thread::get_id());
323     engine->RegisterListener(argv[ARGC_ONE], type, callback);
324 
325     napi_value result = nullptr;
326     napi_get_null(env, &result);
327     return result;
328 }
329 
GetContext(napi_env env,napi_value in,std::shared_ptr<OHOS::AbilityRuntime::Context> & context)330 napi_status JsInputMethodEngineSetting::GetContext(napi_env env, napi_value in,
331     std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
332 {
333     bool stageMode = false;
334     napi_status status = OHOS::AbilityRuntime::IsStageContext(env, in, stageMode);
335     if (status != napi_ok || (!stageMode)) {
336         IMSA_HILOGE("It's not in stage mode.");
337         return status;
338     }
339     context = OHOS::AbilityRuntime::GetStageModeContext(env, in);
340     if (context == nullptr) {
341         IMSA_HILOGE("Context is nullptr.");
342         return napi_generic_failure;
343     }
344     return napi_ok;
345 }
346 
CreatePanel(napi_env env,napi_callback_info info)347 napi_value JsInputMethodEngineSetting::CreatePanel(napi_env env, napi_callback_info info)
348 {
349     auto ctxt = std::make_shared<PanelContext>();
350     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
351         PARAM_CHECK_RETURN(env, argc >= 2, "should has 2 or 3 parameters!", TYPE_NONE, napi_invalid_arg);
352         napi_valuetype valueType = napi_undefined;
353         // 0 means parameter of ctx<BaseContext>
354         napi_typeof(env, argv[0], &valueType);
355         PARAM_CHECK_RETURN(env, valueType == napi_object, " ctx: ", TYPE_OBJECT, napi_invalid_arg);
356         napi_status status = GetContext(env, argv[0], ctxt->context);
357         if (status != napi_ok) {
358             return status;
359         }
360         // 1 means parameter of info<PanelInfo>
361         napi_typeof(env, argv[1], &valueType);
362         PARAM_CHECK_RETURN(env, valueType == napi_object, " panelInfo: ", TYPE_OBJECT, napi_invalid_arg);
363         status = JsUtils::GetValue(env, argv[1], ctxt->panelInfo);
364         PARAM_CHECK_RETURN(env, status == napi_ok, " panelInfo: ", TYPE_OBJECT, napi_invalid_arg);
365         return status;
366     };
367 
368     auto exec = [ctxt](AsyncCall::Context *ctx) {
369         auto ret = InputMethodAbility::GetInstance()->CreatePanel(ctxt->context, ctxt->panelInfo, ctxt->panel);
370         ctxt->SetErrorCode(ret);
371         CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "JsInputMethodEngineSetting CreatePanel failed!");
372         ctxt->SetState(napi_ok);
373     };
374 
375     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
376         JsPanel *jsPanel = nullptr;
377         napi_value constructor = JsPanel::Init(env);
378         CHECK_RETURN(constructor != nullptr, "get jsPanel constructor failed!", napi_generic_failure);
379 
380         napi_status status = napi_new_instance(env, constructor, 0, nullptr, result);
381         CHECK_RETURN(status == napi_ok, "get jsPanel instance failed!", napi_generic_failure);
382 
383         status = napi_unwrap(env, *result, (void **)(&jsPanel));
384         CHECK_RETURN((status == napi_ok) && (jsPanel != nullptr), "get jsPanel failed", napi_generic_failure);
385         jsPanel->SetNative(ctxt->panel);
386         return napi_ok;
387     };
388 
389     ctxt->SetAction(std::move(input), std::move(output));
390     // 3 means JsAPI:createPanel has 3 params at most.
391     AsyncCall asyncCall(env, info, ctxt, 3);
392     return asyncCall.Call(env, exec, "createPanel");
393 }
394 
DestroyPanel(napi_env env,napi_callback_info info)395 napi_value JsInputMethodEngineSetting::DestroyPanel(napi_env env, napi_callback_info info)
396 {
397     auto ctxt = std::make_shared<PanelContext>();
398     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
399         PARAM_CHECK_RETURN(env, argc >= 1, "should has 1 parameters!", TYPE_NONE, napi_invalid_arg);
400         napi_valuetype valueType = napi_undefined;
401         napi_typeof(env, argv[0], &valueType);
402         PARAM_CHECK_RETURN(env, valueType == napi_object, " target: ", TYPE_OBJECT, napi_invalid_arg);
403         bool isPanel = false;
404         napi_value constructor = JsPanel::Init(env);
405         CHECK_RETURN(constructor != nullptr, "Failed to get panel constructor.", napi_invalid_arg);
406         napi_status status = napi_instanceof(env, argv[0], constructor, &isPanel);
407         CHECK_RETURN((status == napi_ok) && isPanel, "It's not expected panel instance!", status);
408         JsPanel *jsPanel = nullptr;
409         status = napi_unwrap(env, argv[0], (void **)(&jsPanel));
410         CHECK_RETURN((status == napi_ok) && (jsPanel != nullptr), "Can not unwrap to JsPanel!", status);
411         ctxt->panel = jsPanel->GetNative();
412         CHECK_RETURN((ctxt->panel != nullptr), "not get valid inputMathodPanel!", napi_invalid_arg);
413         return status;
414     };
415 
416     auto exec = [ctxt](AsyncCall::Context *ctx) {
417         ctxt->SetState(napi_ok);
418     };
419 
420     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
421         CHECK_RETURN((ctxt->panel != nullptr), "inputMethodPanel is nullptr!", napi_generic_failure);
422         auto errCode = InputMethodAbility::GetInstance()->DestroyPanel(ctxt->panel);
423         if (errCode != ErrorCode::NO_ERROR) {
424             IMSA_HILOGE("DestroyPanel failed, errCode = %{public}d", errCode);
425             return napi_generic_failure;
426         }
427         ctxt->panel = nullptr;
428         return napi_ok;
429     };
430 
431     ctxt->SetAction(std::move(input), std::move(output));
432     // 2 means JsAPI:destroyPanel has 2 params at most.
433     AsyncCall asyncCall(env, info, ctxt, 2);
434     return asyncCall.Call(env, exec, "destroyPanel");
435 }
436 
UnSubscribe(napi_env env,napi_callback_info info)437 napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_info info)
438 {
439     size_t argc = ARGC_TWO;
440     napi_value argv[ARGC_TWO] = { nullptr };
441     napi_value thisVar = nullptr;
442     void *data = nullptr;
443     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
444     std::string type;
445     // 1 means least param num.
446     if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
447         || !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type)) {
448         IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
449         return nullptr;
450     }
451     // If the type of optional parameter is wrong, make it nullptr
452     if (JsUtil::GetType(env, argv[1]) != napi_function) {
453         argv[1] = nullptr;
454     }
455     IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str());
456     auto setting = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
457     if (setting == nullptr) {
458         return nullptr;
459     }
460     setting->UnRegisterListener(argv[ARGC_ONE], type);
461     napi_value result = nullptr;
462     napi_get_null(env, &result);
463     return result;
464 }
465 
GetResultOnSetSubtype(napi_env env,const SubProperty & property)466 napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property)
467 {
468     napi_value subType = nullptr;
469     napi_create_object(env, &subType);
470 
471     napi_value label = nullptr;
472     napi_create_string_utf8(env, property.label.c_str(), property.name.size(), &label);
473     napi_set_named_property(env, subType, "label", label);
474 
475     napi_value labelId = nullptr;
476     napi_create_int32(env, property.labelId, &labelId);
477     napi_set_named_property(env, subType, "labelId", labelId);
478 
479     napi_value name = nullptr;
480     napi_create_string_utf8(env, property.name.c_str(), property.name.size(), &name);
481     napi_set_named_property(env, subType, "name", name);
482 
483     napi_value id = nullptr;
484     napi_create_string_utf8(env, property.id.c_str(), property.id.size(), &id);
485     napi_set_named_property(env, subType, "id", id);
486 
487     napi_value mode = nullptr;
488     napi_create_string_utf8(env, property.mode.c_str(), property.mode.size(), &mode);
489     napi_set_named_property(env, subType, "mode", mode);
490 
491     napi_value locale = nullptr;
492     napi_create_string_utf8(env, property.locale.c_str(), property.locale.size(), &locale);
493     napi_set_named_property(env, subType, "locale", locale);
494 
495     napi_value language = nullptr;
496     napi_create_string_utf8(env, property.language.c_str(), property.language.size(), &language);
497     napi_set_named_property(env, subType, "language", language);
498 
499     napi_value icon = nullptr;
500     napi_create_string_utf8(env, property.icon.c_str(), property.icon.size(), &icon);
501     napi_set_named_property(env, subType, "icon", icon);
502 
503     napi_value iconId = nullptr;
504     napi_create_int32(env, property.iconId, &iconId);
505     napi_set_named_property(env, subType, "iconId", iconId);
506 
507     napi_value extra = nullptr;
508     napi_create_object(env, &extra);
509     napi_set_named_property(env, subType, "extra", extra);
510 
511     return subType;
512 }
513 
OnInputStart()514 void JsInputMethodEngineSetting::OnInputStart()
515 {
516     IMSA_HILOGD("run in");
517     std::string type = "inputStart";
518     uv_work_t *work = GetUVwork(type);
519     if (work == nullptr) {
520         IMSA_HILOGD("failed to get uv entry");
521         return;
522     }
523     uv_queue_work_with_qos(
524         loop_, work, [](uv_work_t *work) {},
525         [](uv_work_t *work, int status) {
526             std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
527                 delete data;
528                 delete work;
529             });
530             if (entry == nullptr) {
531                 IMSA_HILOGE("OnInputStart:: entryptr is null");
532                 return;
533             }
534             auto getInputStartProperty = [](napi_env env, napi_value *args, uint8_t argc) -> bool {
535                 if (argc < 2) {
536                     return false;
537                 }
538                 napi_value textInput = JsTextInputClientEngine::GetTextInputClientInstance(env);
539                 napi_value keyBoardController = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env);
540                 if (keyBoardController == nullptr || textInput == nullptr) {
541                     IMSA_HILOGE("get KBCins or TICins failed:");
542                     return false;
543                 }
544                 // 0 means the first param of callback.
545                 args[0] = keyBoardController;
546                 // 1 means the second param of callback.
547                 args[1] = textInput;
548                 return true;
549             };
550             // 2 means callback has 2 params.
551             JsCallbackHandler::Traverse(entry->vecCopy, { 2, getInputStartProperty });
552         },
553         uv_qos_user_initiated);
554 }
555 
OnKeyboardStatus(bool isShow)556 void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
557 {
558     std::string type = isShow ? "keyboardShow" : "keyboardHide";
559     IMSA_HILOGD("run in, %{public}s", type.c_str());
560     uv_work_t *work = GetUVwork(type);
561     if (work == nullptr) {
562         IMSA_HILOGD("failed to get uv entry");
563         return;
564     }
565     uv_queue_work_with_qos(
566         loop_, work, [](uv_work_t *work) {},
567         [](uv_work_t *work, int status) {
568             std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
569                 delete data;
570                 delete work;
571             });
572             JsCallbackHandler::Traverse(entry->vecCopy);
573         },
574         uv_qos_user_initiated);
575 }
576 
OnInputStop(const std::string & imeId)577 void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId)
578 {
579     IMSA_HILOGD("run in");
580     std::string type = "inputStop";
581     uv_work_t *work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; });
582     if (work == nullptr) {
583         IMSA_HILOGD("failed to get uv entry");
584         return;
585     }
586     uv_queue_work_with_qos(
587         loop_, work, [](uv_work_t *work) {},
588         [](uv_work_t *work, int status) {
589             std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
590                 delete data;
591                 delete work;
592             });
593             if (entry == nullptr) {
594                 IMSA_HILOGE("OnInputStop:: entryptr is null");
595                 return;
596             }
597             auto getInputStopProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
598                 if (argc == 0) {
599                     return false;
600                 }
601                 // 0 means the first param of callback.
602                 napi_create_string_utf8(env, entry->imeid.c_str(), NAPI_AUTO_LENGTH, &args[0]);
603                 return true;
604             };
605             // 1 means callback has one param.
606             JsCallbackHandler::Traverse(entry->vecCopy, { 1, getInputStopProperty });
607         },
608         uv_qos_user_initiated);
609 }
610 
OnSetCallingWindow(uint32_t windowId)611 void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
612 {
613     IMSA_HILOGD("run in");
614     std::string type = "setCallingWindow";
615     uv_work_t *work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; });
616     if (work == nullptr) {
617         IMSA_HILOGD("failed to get uv entry");
618         return;
619     }
620     uv_queue_work_with_qos(
621         loop_, work, [](uv_work_t *work) {},
622         [](uv_work_t *work, int status) {
623             std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
624                 delete data;
625                 delete work;
626             });
627             if (entry == nullptr) {
628                 IMSA_HILOGE("setCallingWindow:: entryptr is null");
629                 return;
630             }
631             auto getCallingWindowProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
632                 if (argc == 0) {
633                     return false;
634                 }
635                 // 0 means the first param of callback.
636                 napi_create_uint32(env, entry->windowid, &args[0]);
637                 return true;
638             };
639             // 1 means callback has one param.
640             JsCallbackHandler::Traverse(entry->vecCopy, { 1, getCallingWindowProperty });
641         },
642         uv_qos_user_initiated);
643 }
644 
OnSetSubtype(const SubProperty & property)645 void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
646 {
647     IMSA_HILOGD("run in");
648     std::string type = "setSubtype";
649     uv_work_t *work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; });
650     if (work == nullptr) {
651         IMSA_HILOGD("failed to get uv entry");
652         return;
653     }
654     uv_queue_work_with_qos(
655         loop_, work, [](uv_work_t *work) {},
656         [](uv_work_t *work, int status) {
657             std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
658                 delete data;
659                 delete work;
660             });
661             if (entry == nullptr) {
662                 IMSA_HILOGE("OnSetSubtype:: entryptr is null");
663                 return;
664             }
665             auto getSubtypeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
666                 if (argc == 0) {
667                     return false;
668                 }
669                 napi_value jsObject = GetResultOnSetSubtype(env, entry->subProperty);
670                 if (jsObject == nullptr) {
671                     IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr");
672                     return false;
673                 }
674                 // 0 means the first param of callback.
675                 args[0] = { jsObject };
676                 return true;
677             };
678             // 1 means callback has one param.
679             JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSubtypeProperty });
680         },
681         uv_qos_user_initiated);
682 }
683 
GetUVwork(const std::string & type,EntrySetter entrySetter)684 uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
685 {
686     IMSA_HILOGD("run in, type: %{public}s", type.c_str());
687     UvEntry *entry = nullptr;
688     {
689         std::lock_guard<std::recursive_mutex> lock(mutex_);
690 
691         if (jsCbMap_[type].empty()) {
692             IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
693             return nullptr;
694         }
695         entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
696         if (entry == nullptr) {
697             IMSA_HILOGE("entry ptr is nullptr!");
698             return nullptr;
699         }
700         if (entrySetter != nullptr) {
701             entrySetter(*entry);
702         }
703     }
704     uv_work_t *work = new (std::nothrow) uv_work_t;
705     if (work == nullptr) {
706         IMSA_HILOGE("entry ptr is nullptr!");
707         return nullptr;
708     }
709     work->data = entry;
710     return work;
711 }
712 } // namespace MiscServices
713 } // namespace OHOS
714