• 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 "event_checker.h"
21 #include "input_method_ability.h"
22 #include "input_method_property.h"
23 #include "input_method_utils.h"
24 #include "js_callback_handler.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 "js_utils.h"
30 #include "napi/native_api.h"
31 #include "napi/native_node_api.h"
32 #include "napi_base_context.h"
33 
34 namespace OHOS {
35 namespace MiscServices {
36 constexpr size_t ARGC_ONE = 1;
37 constexpr size_t ARGC_TWO = 2;
38 constexpr size_t ARGC_MAX = 6;
39 const std::string JsInputMethodEngineSetting::IMES_CLASS_NAME = "InputMethodEngine";
40 thread_local napi_ref JsInputMethodEngineSetting::IMESRef_ = nullptr;
41 
42 std::mutex JsInputMethodEngineSetting::engineMutex_;
43 std::shared_ptr<JsInputMethodEngineSetting> JsInputMethodEngineSetting::inputMethodEngine_{ nullptr };
44 std::mutex JsInputMethodEngineSetting::eventHandlerMutex_;
45 std::shared_ptr<AppExecFwk::EventHandler> JsInputMethodEngineSetting::handler_{ nullptr };
46 
Init(napi_env env,napi_value exports)47 napi_value JsInputMethodEngineSetting::Init(napi_env env, napi_value exports)
48 {
49     napi_property_descriptor descriptor[] = {
50         DECLARE_NAPI_PROPERTY(
51             "ENTER_KEY_TYPE_UNSPECIFIED", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::UNSPECIFIED))),
52         DECLARE_NAPI_PROPERTY("ENTER_KEY_TYPE_GO", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::GO))),
53         DECLARE_NAPI_PROPERTY(
54             "ENTER_KEY_TYPE_SEARCH", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::SEARCH))),
55         DECLARE_NAPI_PROPERTY(
56             "ENTER_KEY_TYPE_SEND", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::SEND))),
57         DECLARE_NAPI_PROPERTY(
58             "ENTER_KEY_TYPE_NEXT", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::NEXT))),
59         DECLARE_NAPI_PROPERTY(
60             "ENTER_KEY_TYPE_DONE", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::DONE))),
61         DECLARE_NAPI_PROPERTY(
62             "ENTER_KEY_TYPE_PREVIOUS", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::PREVIOUS))),
63         DECLARE_NAPI_PROPERTY(
64             "ENTER_KEY_TYPE_NEWLINE", GetJsConstProperty(env, static_cast<uint32_t>(EnterKeyType::NEW_LINE))),
65         DECLARE_NAPI_PROPERTY("PATTERN_NULL", GetIntJsConstProperty(env, static_cast<int32_t>(TextInputType::NONE))),
66         DECLARE_NAPI_PROPERTY("PATTERN_TEXT", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::TEXT))),
67         DECLARE_NAPI_PROPERTY("PATTERN_NUMBER", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::NUMBER))),
68         DECLARE_NAPI_PROPERTY("PATTERN_PHONE", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::PHONE))),
69         DECLARE_NAPI_PROPERTY(
70             "PATTERN_DATETIME", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::DATETIME))),
71         DECLARE_NAPI_PROPERTY(
72             "PATTERN_EMAIL", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::EMAIL_ADDRESS))),
73         DECLARE_NAPI_PROPERTY("PATTERN_URI", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::URL))),
74         DECLARE_NAPI_PROPERTY(
75             "PATTERN_PASSWORD", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::VISIBLE_PASSWORD))),
76         DECLARE_NAPI_PROPERTY(
77             "PATTERN_PASSWORD_NUMBER", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::NUMBER_PASSWORD))),
78         DECLARE_NAPI_PROPERTY("PATTERN_PASSWORD_SCREEN_LOCK",
79             GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::SCREEN_LOCK_PASSWORD))),
80         DECLARE_NAPI_PROPERTY(
81             "PATTERN_USER_NAME", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::USER_NAME))),
82         DECLARE_NAPI_PROPERTY(
83             "PATTERN_NEW_PASSWORD", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::NEW_PASSWORD))),
84         DECLARE_NAPI_PROPERTY(
85             "PATTERN_NUMBER_DECIMAL", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::NUMBER_DECIMAL))),
86         DECLARE_NAPI_PROPERTY(
87             "PATTERN_ONE_TIME_CODE", GetJsConstProperty(env, static_cast<uint32_t>(TextInputType::ONE_TIME_CODE))),
88         DECLARE_NAPI_FUNCTION("getInputMethodEngine", GetInputMethodEngine),
89         DECLARE_NAPI_FUNCTION("getInputMethodAbility", GetInputMethodAbility),
90         DECLARE_NAPI_STATIC_PROPERTY("PanelType", GetJsPanelTypeProperty(env)),
91         DECLARE_NAPI_STATIC_PROPERTY("PanelFlag", GetJsPanelFlagProperty(env)),
92         DECLARE_NAPI_STATIC_PROPERTY("Direction", GetJsDirectionProperty(env)),
93         DECLARE_NAPI_STATIC_PROPERTY("ExtendAction", GetJsExtendActionProperty(env)),
94         DECLARE_NAPI_STATIC_PROPERTY("SecurityMode", GetJsSecurityModeProperty(env)),
95         DECLARE_NAPI_STATIC_PROPERTY("ImmersiveMode", GetJsImmersiveModeProperty(env)),
96         DECLARE_NAPI_STATIC_PROPERTY("RequestKeyboardReason", GetJsRequestKeyboardReasonProperty(env)),
97         DECLARE_NAPI_STATIC_PROPERTY("CapitalizeMode", GetJsCapitalizeModeProperty(env)),
98         DECLARE_NAPI_STATIC_PROPERTY("GradientMode", GetJsGradientModeProperty(env)),
99         DECLARE_NAPI_STATIC_PROPERTY("FluidLightMode", GetJsFluidLightModeProperty(env))
100     };
101     NAPI_CALL(
102         env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
103     return InitProperty(env, exports);
104 }
105 
InitProperty(napi_env env,napi_value exports)106 napi_value JsInputMethodEngineSetting::InitProperty(napi_env env, napi_value exports)
107 {
108     napi_property_descriptor properties[] = {
109         DECLARE_NAPI_FUNCTION("on", Subscribe),
110         DECLARE_NAPI_FUNCTION("off", UnSubscribe),
111         DECLARE_NAPI_FUNCTION("createPanel", CreatePanel),
112         DECLARE_NAPI_FUNCTION("destroyPanel", DestroyPanel),
113         DECLARE_NAPI_FUNCTION("getSecurityMode", GetSecurityMode),
114     };
115     napi_value cons = nullptr;
116     NAPI_CALL(env, napi_define_class(env, IMES_CLASS_NAME.c_str(), IMES_CLASS_NAME.size(), JsConstructor, nullptr,
117                        sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
118     NAPI_CALL(env, napi_create_reference(env, cons, 1, &IMESRef_));
119     NAPI_CALL(env, napi_set_named_property(env, exports, IMES_CLASS_NAME.c_str(), cons));
120     return exports;
121 }
122 
GetJsConstProperty(napi_env env,uint32_t num)123 napi_value JsInputMethodEngineSetting::GetJsConstProperty(napi_env env, uint32_t num)
124 {
125     napi_value jsNumber = nullptr;
126     napi_create_uint32(env, num, &jsNumber);
127     return jsNumber;
128 }
129 
GetIntJsConstProperty(napi_env env,int32_t num)130 napi_value JsInputMethodEngineSetting::GetIntJsConstProperty(napi_env env, int32_t num)
131 {
132     napi_value jsNumber = nullptr;
133     napi_create_int32(env, num, &jsNumber);
134     return jsNumber;
135 }
136 
GetJsPanelTypeProperty(napi_env env)137 napi_value JsInputMethodEngineSetting::GetJsPanelTypeProperty(napi_env env)
138 {
139     napi_value panelType = nullptr;
140     napi_value typeSoftKeyboard = nullptr;
141     napi_value typeStatusBar = nullptr;
142     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelType::SOFT_KEYBOARD), &typeSoftKeyboard));
143     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelType::STATUS_BAR), &typeStatusBar));
144     NAPI_CALL(env, napi_create_object(env, &panelType));
145     NAPI_CALL(env, napi_set_named_property(env, panelType, "SOFT_KEYBOARD", typeSoftKeyboard));
146     NAPI_CALL(env, napi_set_named_property(env, panelType, "STATUS_BAR", typeStatusBar));
147     return panelType;
148 }
149 
GetJsPanelFlagProperty(napi_env env)150 napi_value JsInputMethodEngineSetting::GetJsPanelFlagProperty(napi_env env)
151 {
152     napi_value panelFlag = nullptr;
153     napi_value flagFixed = nullptr;
154     napi_value flagFloating = nullptr;
155     napi_value flagCandidate = nullptr;
156     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelFlag::FLG_FIXED), &flagFixed));
157     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelFlag::FLG_FLOATING), &flagFloating));
158     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PanelFlag::FLG_CANDIDATE_COLUMN), &flagCandidate));
159     NAPI_CALL(env, napi_create_object(env, &panelFlag));
160     NAPI_CALL(env, napi_set_named_property(env, panelFlag, "FLG_FIXED", flagFixed));
161     NAPI_CALL(env, napi_set_named_property(env, panelFlag, "FLG_FLOATING", flagFloating));
162     NAPI_CALL(env, napi_set_named_property(env, panelFlag, "FLAG_CANDIDATE", flagCandidate));
163     return panelFlag;
164 }
165 
GetJsDirectionProperty(napi_env env)166 napi_value JsInputMethodEngineSetting::GetJsDirectionProperty(napi_env env)
167 {
168     napi_value direction = nullptr;
169     napi_value cursorUp = nullptr;
170     napi_value cursorDown = nullptr;
171     napi_value cursorLeft = nullptr;
172     napi_value cursorRight = nullptr;
173     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::UP), &cursorUp));
174     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::DOWN), &cursorDown));
175     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::LEFT), &cursorLeft));
176     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(Direction::RIGHT), &cursorRight));
177     NAPI_CALL(env, napi_create_object(env, &direction));
178     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_UP", cursorUp));
179     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_DOWN", cursorDown));
180     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_LEFT", cursorLeft));
181     NAPI_CALL(env, napi_set_named_property(env, direction, "CURSOR_RIGHT", cursorRight));
182     return direction;
183 }
184 
GetJsExtendActionProperty(napi_env env)185 napi_value JsInputMethodEngineSetting::GetJsExtendActionProperty(napi_env env)
186 {
187     napi_value action = nullptr;
188     napi_value actionSelectAll = nullptr;
189     napi_value actionCut = nullptr;
190     napi_value actionCopy = nullptr;
191     napi_value actionPaste = nullptr;
192     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::SELECT_ALL), &actionSelectAll));
193     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::CUT), &actionCut));
194     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::COPY), &actionCopy));
195     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ExtendAction::PASTE), &actionPaste));
196     NAPI_CALL(env, napi_create_object(env, &action));
197     NAPI_CALL(env, napi_set_named_property(env, action, "SELECT_ALL", actionSelectAll));
198     NAPI_CALL(env, napi_set_named_property(env, action, "CUT", actionCut));
199     NAPI_CALL(env, napi_set_named_property(env, action, "COPY", actionCopy));
200     NAPI_CALL(env, napi_set_named_property(env, action, "PASTE", actionPaste));
201     return action;
202 }
203 
GetJsCapitalizeModeProperty(napi_env env)204 napi_value JsInputMethodEngineSetting::GetJsCapitalizeModeProperty(napi_env env)
205 {
206     napi_value jsObject = nullptr;
207     napi_create_object(env, &jsObject);
208     bool ret = JsUtil::Object::WriteProperty(env, jsObject, "NONE", static_cast<int32_t>(CapitalizeMode::NONE));
209     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "SENTENCES",
210         static_cast<int32_t>(CapitalizeMode::SENTENCES));
211     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "WORDS", static_cast<int32_t>(CapitalizeMode::WORDS));
212     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "CHARACTERS",
213         static_cast<int32_t>(CapitalizeMode::CHARACTERS));
214     return ret ? jsObject : JsUtil::Const::Null(env);
215 }
216 
GetJsSecurityModeProperty(napi_env env)217 napi_value JsInputMethodEngineSetting::GetJsSecurityModeProperty(napi_env env)
218 {
219     napi_value securityMode = nullptr;
220     napi_value basic = nullptr;
221     napi_value full = nullptr;
222     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(SecurityMode::BASIC), &basic));
223     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(SecurityMode::FULL), &full));
224     NAPI_CALL(env, napi_create_object(env, &securityMode));
225     NAPI_CALL(env, napi_set_named_property(env, securityMode, "BASIC", basic));
226     NAPI_CALL(env, napi_set_named_property(env, securityMode, "FULL", full));
227     return securityMode;
228 }
229 
GetJsImmersiveModeProperty(napi_env env)230 napi_value JsInputMethodEngineSetting::GetJsImmersiveModeProperty(napi_env env)
231 {
232     napi_value immersive = nullptr;
233     NAPI_CALL(env, napi_create_object(env, &immersive));
234     bool ret = JsUtil::Object::WriteProperty(
235         env, immersive, "NONE_IMMERSIVE", static_cast<int32_t>(ImmersiveMode::NONE_IMMERSIVE));
236     ret = ret &&
237         JsUtil::Object::WriteProperty(env, immersive, "IMMERSIVE", static_cast<int32_t>(ImmersiveMode::IMMERSIVE));
238     ret = ret &&
239         JsUtil::Object::WriteProperty(
240             env, immersive, "LIGHT_IMMERSIVE", static_cast<int32_t>(ImmersiveMode::LIGHT_IMMERSIVE));
241     ret = ret &&
242         JsUtil::Object::WriteProperty(
243             env, immersive, "DARK_IMMERSIVE", static_cast<int32_t>(ImmersiveMode::DARK_IMMERSIVE));
244     return ret ? immersive : JsUtil::Const::Null(env);
245 }
246 
GetJsGradientModeProperty(napi_env env)247 napi_value JsInputMethodEngineSetting::GetJsGradientModeProperty(napi_env env)
248 {
249     napi_value gradientMode = nullptr;
250     NAPI_CALL(env, napi_create_object(env, &gradientMode));
251     bool ret = JsUtil::Object::WriteProperty(env, gradientMode, "NONE", static_cast<int32_t>(GradientMode::NONE));
252     ret = ret &&
253         JsUtil::Object::WriteProperty(
254             env, gradientMode, "LINEAR_GRADIENT", static_cast<int32_t>(GradientMode::LINEAR_GRADIENT));
255     return ret ? gradientMode : JsUtil::Const::Null(env);
256 }
257 
GetJsFluidLightModeProperty(napi_env env)258 napi_value JsInputMethodEngineSetting::GetJsFluidLightModeProperty(napi_env env)
259 {
260     napi_value fluidLightMode = nullptr;
261     NAPI_CALL(env, napi_create_object(env, &fluidLightMode));
262     bool ret = JsUtil::Object::WriteProperty(
263         env, fluidLightMode, "NONE", static_cast<int32_t>(FluidLightMode::NONE));
264     ret = ret && JsUtil::Object::WriteProperty(
265         env, fluidLightMode, "BACKGROUND_FLUID_LIGHT", static_cast<int32_t>(FluidLightMode::BACKGROUND_FLUID_LIGHT));
266     return ret ? fluidLightMode : JsUtil::Const::Null(env);
267 }
268 
GetJsRequestKeyboardReasonProperty(napi_env env)269 napi_value JsInputMethodEngineSetting::GetJsRequestKeyboardReasonProperty(napi_env env)
270 {
271     napi_value requestKeyboardReason = nullptr;
272     NAPI_CALL(env, napi_create_object(env, &requestKeyboardReason));
273     bool ret = JsUtil::Object::WriteProperty(
274         env, requestKeyboardReason, "NONE", static_cast<int32_t>(RequestKeyboardReason::NONE));
275     ret = ret &&
276         JsUtil::Object::WriteProperty(
277             env, requestKeyboardReason, "MOUSE", static_cast<int32_t>(RequestKeyboardReason::MOUSE));
278     ret = ret &&
279         JsUtil::Object::WriteProperty(
280             env, requestKeyboardReason, "TOUCH", static_cast<int32_t>(RequestKeyboardReason::TOUCH));
281     ret = ret &&
282         JsUtil::Object::WriteProperty(
283             env, requestKeyboardReason, "OTHER", static_cast<int32_t>(RequestKeyboardReason::OTHER));
284     return ret ? requestKeyboardReason : JsUtil::Const::Null(env);
285 }
286 
GetInputMethodEngineSetting()287 std::shared_ptr<JsInputMethodEngineSetting> JsInputMethodEngineSetting::GetInputMethodEngineSetting()
288 {
289     if (inputMethodEngine_ == nullptr) {
290         std::lock_guard<std::mutex> lock(engineMutex_);
291         if (inputMethodEngine_ == nullptr) {
292             auto engine = std::make_shared<JsInputMethodEngineSetting>();
293             if (engine == nullptr) {
294                 IMSA_HILOGE("create engine failed!");
295                 return nullptr;
296             }
297             inputMethodEngine_ = engine;
298         }
299     }
300     return inputMethodEngine_;
301 }
302 
InitInputMethodSetting()303 bool JsInputMethodEngineSetting::InitInputMethodSetting()
304 {
305     if (!InputMethodAbility::GetInstance().IsCurrentIme()) {
306         return false;
307     }
308     auto engine = GetInputMethodEngineSetting();
309     if (engine == nullptr) {
310         return false;
311     }
312     InputMethodAbility::GetInstance().SetImeListener(engine);
313     {
314         std::lock_guard<std::mutex> lock(eventHandlerMutex_);
315         handler_ = AppExecFwk::EventHandler::Current();
316     }
317     return true;
318 }
319 
JsConstructor(napi_env env,napi_callback_info cbinfo)320 napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback_info cbinfo)
321 {
322     napi_value thisVar = nullptr;
323     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
324     auto setting = GetInputMethodEngineSetting();
325     if (setting == nullptr || !InitInputMethodSetting()) {
326         IMSA_HILOGE("failed to get setting.");
327         napi_value result = nullptr;
328         napi_get_null(env, &result);
329         return result;
330     }
331     napi_status status = napi_wrap(
332         env, thisVar, setting.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr);
333     if (status != napi_ok) {
334         IMSA_HILOGE("JsInputMethodEngineSetting napi_wrap failed: %{public}d", status);
335         return nullptr;
336     }
337     return thisVar;
338 };
339 
GetInputMethodAbility(napi_env env,napi_callback_info info)340 napi_value JsInputMethodEngineSetting::GetInputMethodAbility(napi_env env, napi_callback_info info)
341 {
342     return GetIMEInstance(env, info);
343 }
344 
GetInputMethodEngine(napi_env env,napi_callback_info info)345 napi_value JsInputMethodEngineSetting::GetInputMethodEngine(napi_env env, napi_callback_info info)
346 {
347     return GetIMEInstance(env, info);
348 }
349 
GetIMEInstance(napi_env env,napi_callback_info info)350 napi_value JsInputMethodEngineSetting::GetIMEInstance(napi_env env, napi_callback_info info)
351 {
352     napi_value instance = nullptr;
353     napi_value cons = nullptr;
354     if (napi_get_reference_value(env, IMESRef_, &cons) != napi_ok) {
355         IMSA_HILOGE("failed to get reference value.");
356         return nullptr;
357     }
358     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
359         IMSA_HILOGE("failed to new instance.");
360         return nullptr;
361     }
362     return instance;
363 }
364 
RegisterListener(napi_value callback,std::string type,std::shared_ptr<JSCallbackObject> callbackObj)365 void JsInputMethodEngineSetting::RegisterListener(napi_value callback, std::string type,
366     std::shared_ptr<JSCallbackObject> callbackObj)
367 {
368     IMSA_HILOGD("register listener: %{public}s.", type.c_str());
369     std::lock_guard<std::recursive_mutex> lock(mutex_);
370     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
371         IMSA_HILOGD("methodName: %{public}s not registered!", type.c_str());
372     }
373     auto callbacks = jsCbMap_[type];
374     bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr<JSCallbackObject> cb) {
375         if (cb == nullptr) {
376             return false;
377         }
378         return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_);
379     });
380     if (ret) {
381         IMSA_HILOGD("JsInputMethodEngineListener callback already registered!");
382         return;
383     }
384 
385     IMSA_HILOGI("add %{public}s callbackObj into jsCbMap_.", type.c_str());
386     jsCbMap_[type].push_back(std::move(callbackObj));
387 }
388 
UnRegisterListener(napi_value callback,std::string type)389 void JsInputMethodEngineSetting::UnRegisterListener(napi_value callback, std::string type)
390 {
391     IMSA_HILOGI("unregister listener: %{public}s.", type.c_str());
392     std::lock_guard<std::recursive_mutex> lock(mutex_);
393     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
394         IMSA_HILOGE("methodName: %{public}s already unregistered!", type.c_str());
395         return;
396     }
397 
398     if (callback == nullptr) {
399         jsCbMap_.erase(type);
400         IMSA_HILOGE("callback is nullptr.");
401         return;
402     }
403 
404     for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
405         if (JsUtils::Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_)) {
406             jsCbMap_[type].erase(item);
407             break;
408         }
409     }
410 
411     if (jsCbMap_[type].empty()) {
412         jsCbMap_.erase(type);
413     }
414 }
415 
Subscribe(napi_env env,napi_callback_info info)416 napi_value JsInputMethodEngineSetting::Subscribe(napi_env env, napi_callback_info info)
417 {
418     size_t argc = ARGC_MAX;
419     napi_value argv[ARGC_MAX] = { nullptr };
420     napi_value thisVar = nullptr;
421     void *data = nullptr;
422     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
423     std::string type;
424     // 2 means least param num.
425     if (argc < 2 || !JsUtil::GetValue(env, argv[0], type) ||
426         !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type) ||
427         JsUtil::GetType(env, argv[1]) != napi_function) {
428         IMSA_HILOGE("subscribe failed, type: %{public}s.", type.c_str());
429         return nullptr;
430     }
431     if (type == "privateCommand" && !InputMethodAbility::GetInstance().IsDefaultIme()) {
432         JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_NOT_DEFAULT_IME), "default ime check failed",
433             TYPE_NONE);
434     }
435 #ifndef SCENE_BOARD_ENABLE
436     if (type == "callingDisplayDidChange") {
437         JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_DEVICE_UNSUPPORTED),
438             "capability not supported.", TYPE_NONE);
439     }
440 #endif
441     IMSA_HILOGD("subscribe type:%{public}s.", type.c_str());
442     auto engine = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
443     if (engine == nullptr) {
444         return nullptr;
445     }
446     std::shared_ptr<JSCallbackObject> callback =
447         std::make_shared<JSCallbackObject>(env, argv[ARGC_ONE], std::this_thread::get_id(),
448             AppExecFwk::EventHandler::Current());
449     engine->RegisterListener(argv[ARGC_ONE], type, callback);
450 
451     napi_value result = nullptr;
452     napi_get_null(env, &result);
453     return result;
454 }
455 
GetContext(napi_env env,napi_value in,std::shared_ptr<OHOS::AbilityRuntime::Context> & context)456 napi_status JsInputMethodEngineSetting::GetContext(napi_env env, napi_value in,
457     std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
458 {
459     bool stageMode = false;
460     napi_status status = OHOS::AbilityRuntime::IsStageContext(env, in, stageMode);
461     if (status != napi_ok || (!stageMode)) {
462         IMSA_HILOGE("it's not in stage mode.");
463         return status;
464     }
465     context = OHOS::AbilityRuntime::GetStageModeContext(env, in);
466     if (context == nullptr) {
467         IMSA_HILOGE("context is nullptr.");
468         return napi_generic_failure;
469     }
470     return napi_ok;
471 }
472 
CreatePanel(napi_env env,napi_callback_info info)473 napi_value JsInputMethodEngineSetting::CreatePanel(napi_env env, napi_callback_info info)
474 {
475     auto ctxt = std::make_shared<PanelContext>();
476     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
477         PARAM_CHECK_RETURN(env, argc >= 2, "at least two parameters is required.", TYPE_NONE, napi_invalid_arg);
478         napi_valuetype valueType = napi_undefined;
479         // 0 means parameter of ctx<BaseContext>
480         napi_typeof(env, argv[0], &valueType);
481         PARAM_CHECK_RETURN(env, valueType == napi_object, "ctx type must be BaseContext.", TYPE_NONE, napi_invalid_arg);
482         napi_status status = GetContext(env, argv[0], ctxt->context);
483         if (status != napi_ok) {
484             return status;
485         }
486         // 1 means parameter of info<PanelInfo>
487         napi_typeof(env, argv[1], &valueType);
488         PARAM_CHECK_RETURN(env, valueType == napi_object, "param info type must be PanelInfo.", TYPE_NONE,
489             napi_invalid_arg);
490         status = JsUtils::GetValue(env, argv[1], ctxt->panelInfo);
491         PARAM_CHECK_RETURN(env, status == napi_ok, "js param info covert failed!", TYPE_NONE, napi_invalid_arg);
492         return status;
493     };
494 
495     auto exec = [ctxt](AsyncCall::Context *ctx) {
496         auto ret = InputMethodAbility::GetInstance().CreatePanel(ctxt->context, ctxt->panelInfo, ctxt->panel);
497         ctxt->SetErrorCode(ret);
498         CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "JsInputMethodEngineSetting CreatePanel failed!");
499         ctxt->SetState(napi_ok);
500     };
501 
502     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
503         JsPanel *jsPanel = nullptr;
504         napi_value constructor = JsPanel::Init(env);
505         CHECK_RETURN(constructor != nullptr, "failed to get panel constructor!", napi_generic_failure);
506 
507         napi_status status = napi_new_instance(env, constructor, 0, nullptr, result);
508         CHECK_RETURN(status == napi_ok, "jsPanel new instance failed!", napi_generic_failure);
509 
510         status = napi_unwrap(env, *result, (void **)(&jsPanel));
511         CHECK_RETURN((status == napi_ok) && (jsPanel != nullptr), "get jsPanel unwrap failed!", napi_generic_failure);
512         jsPanel->SetNative(ctxt->panel);
513         return napi_ok;
514     };
515 
516     ctxt->SetAction(std::move(input), std::move(output));
517     // 3 means JsAPI:createPanel has 3 params at most.
518     AsyncCall asyncCall(env, info, ctxt, 3);
519     return asyncCall.Call(env, exec, "createPanel");
520 }
521 
DestroyPanel(napi_env env,napi_callback_info info)522 napi_value JsInputMethodEngineSetting::DestroyPanel(napi_env env, napi_callback_info info)
523 {
524     auto ctxt = std::make_shared<PanelContext>();
525     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
526         PARAM_CHECK_RETURN(env, argc >= 1, "at least one parameter is required!", TYPE_NONE, napi_invalid_arg);
527         napi_valuetype valueType = napi_undefined;
528         napi_typeof(env, argv[0], &valueType);
529         PARAM_CHECK_RETURN(env, valueType == napi_object, "param panel type must be InputMethodPanel.", TYPE_NONE,
530             napi_invalid_arg);
531         bool isPanel = false;
532         napi_value constructor = JsPanel::Init(env);
533         CHECK_RETURN(constructor != nullptr, "failed to get panel constructor.", napi_invalid_arg);
534         napi_status status = napi_instanceof(env, argv[0], constructor, &isPanel);
535         CHECK_RETURN((status == napi_ok) && isPanel, "param verification failed, it's not expected panel instance!",
536             status);
537         JsPanel *jsPanel = nullptr;
538         status = napi_unwrap(env, argv[0], (void **)(&jsPanel));
539         CHECK_RETURN((status == napi_ok) && (jsPanel != nullptr), "failed to unwrap JsPanel!", status);
540         ctxt->panel = jsPanel->GetNative();
541         CHECK_RETURN((ctxt->panel != nullptr), "panel is nullptr!", napi_invalid_arg);
542         return status;
543     };
544 
545     auto exec = [ctxt](AsyncCall::Context *ctx) { ctxt->SetState(napi_ok); };
546 
547     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
548         CHECK_RETURN((ctxt->panel != nullptr), "panel is nullptr!", napi_generic_failure);
549         auto errCode = InputMethodAbility::GetInstance().DestroyPanel(ctxt->panel);
550         if (errCode != ErrorCode::NO_ERROR) {
551             IMSA_HILOGE("DestroyPanel failed, errCode: %{public}d!", errCode);
552             return napi_generic_failure;
553         }
554         ctxt->panel = nullptr;
555         return napi_ok;
556     };
557 
558     ctxt->SetAction(std::move(input), std::move(output));
559     // 2 means JsAPI:destroyPanel has 2 params at most.
560     AsyncCall asyncCall(env, info, ctxt, 2);
561     return asyncCall.Call(env, exec, "destroyPanel");
562 }
563 
GetSecurityMode(napi_env env,napi_callback_info info)564 napi_value JsInputMethodEngineSetting::GetSecurityMode(napi_env env, napi_callback_info info)
565 {
566     IMSA_HILOGD("start get security mode.");
567     size_t argc = 1;
568     napi_value argv[1] = { nullptr };
569     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
570     int32_t security;
571     int32_t ret = InputMethodAbility::GetInstance().GetSecurityMode(security);
572     if (ret != ErrorCode::NO_ERROR) {
573         JsUtils::ThrowException(env, JsUtils::Convert(ret), "failed to get security mode", TYPE_NONE);
574     }
575     napi_value result = nullptr;
576     napi_create_int32(env, security, &result);
577     return result;
578 }
579 
UnSubscribe(napi_env env,napi_callback_info info)580 napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_info info)
581 {
582     size_t argc = ARGC_TWO;
583     napi_value argv[ARGC_TWO] = { nullptr };
584     napi_value thisVar = nullptr;
585     void *data = nullptr;
586     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
587     std::string type;
588     // 1 means least param num.
589     if (argc < 1 || !JsUtil::GetValue(env, argv[0], type) ||
590         !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type)) {
591         IMSA_HILOGE("unsubscribe failed, type: %{public}s!", type.c_str());
592         return nullptr;
593     }
594     if (type == "privateCommand" && !InputMethodAbility::GetInstance().IsDefaultIme()) {
595         JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_NOT_DEFAULT_IME), "default ime check failed",
596             TYPE_NONE);
597     }
598     // if the second param is not napi_function/napi_null/napi_undefined, return
599     auto paramType = JsUtil::GetType(env, argv[1]);
600     if (paramType != napi_function && paramType != napi_null && paramType != napi_undefined) {
601         return nullptr;
602     }
603     // if the second param is napi_function, delete it, else delete all
604     argv[1] = paramType == napi_function ? argv[1] : nullptr;
605 
606     IMSA_HILOGD("unsubscribe type: %{public}s.", type.c_str());
607     auto setting = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
608     if (setting == nullptr) {
609         return nullptr;
610     }
611     setting->UnRegisterListener(argv[ARGC_ONE], type);
612     napi_value result = nullptr;
613     napi_get_null(env, &result);
614     return result;
615 }
616 
GetResultOnSetSubtype(napi_env env,const SubProperty & property)617 napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property)
618 {
619     napi_value subType = nullptr;
620     napi_create_object(env, &subType);
621 
622     napi_value label = nullptr;
623     napi_create_string_utf8(env, property.label.c_str(), property.name.size(), &label);
624     napi_set_named_property(env, subType, "label", label);
625 
626     napi_value labelId = nullptr;
627     napi_create_uint32(env, property.labelId, &labelId);
628     napi_set_named_property(env, subType, "labelId", labelId);
629 
630     napi_value name = nullptr;
631     napi_create_string_utf8(env, property.name.c_str(), property.name.size(), &name);
632     napi_set_named_property(env, subType, "name", name);
633 
634     napi_value id = nullptr;
635     napi_create_string_utf8(env, property.id.c_str(), property.id.size(), &id);
636     napi_set_named_property(env, subType, "id", id);
637 
638     napi_value mode = nullptr;
639     napi_create_string_utf8(env, property.mode.c_str(), property.mode.size(), &mode);
640     napi_set_named_property(env, subType, "mode", mode);
641 
642     napi_value locale = nullptr;
643     napi_create_string_utf8(env, property.locale.c_str(), property.locale.size(), &locale);
644     napi_set_named_property(env, subType, "locale", locale);
645 
646     napi_value language = nullptr;
647     napi_create_string_utf8(env, property.language.c_str(), property.language.size(), &language);
648     napi_set_named_property(env, subType, "language", language);
649 
650     napi_value icon = nullptr;
651     napi_create_string_utf8(env, property.icon.c_str(), property.icon.size(), &icon);
652     napi_set_named_property(env, subType, "icon", icon);
653 
654     napi_value iconId = nullptr;
655     napi_create_uint32(env, property.iconId, &iconId);
656     napi_set_named_property(env, subType, "iconId", iconId);
657 
658     napi_value extra = nullptr;
659     napi_create_object(env, &extra);
660     napi_set_named_property(env, subType, "extra", extra);
661 
662     return subType;
663 }
664 
OnInputStart()665 void JsInputMethodEngineSetting::OnInputStart()
666 {
667     std::string type = "inputStart";
668     auto entry = GetEntry(type);
669     if (entry == nullptr) {
670         IMSA_HILOGE("entry is nullptr!");
671         return;
672     }
673     auto eventHandler = GetEventHandler();
674     if (eventHandler == nullptr) {
675         IMSA_HILOGE("eventHandler is nullptr!");
676         return;
677     }
678     IMSA_HILOGI("OnInputStart start.");
679     auto task = [entry]() {
680         IMSA_HILOGI("OnInputStart task start!");
681         auto paramGetter = [](napi_env env, napi_value *args, uint8_t argc) -> bool {
682             if (argc < 2) {
683                 IMSA_HILOGE("the num:%{public}u of params in OnInputStart is abnormal!", argc);
684                 return false;
685             }
686             napi_value textInput = JsTextInputClientEngine::GetTextInputClientInstance(env);
687             napi_value keyBoardController = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env);
688             if (keyBoardController == nullptr || textInput == nullptr) {
689                 IMSA_HILOGE("get KBCins or TICins failed!");
690                 return false;
691             }
692             // 0 means the first param of callback.
693             args[0] = keyBoardController;
694             // 1 means the second param of callback.
695             args[1] = textInput;
696             return true;
697         };
698         // 2 means callback has 2 params.
699         JsCallbackHandler::Traverse(entry->vecCopy, { 2, paramGetter });
700         IMSA_HILOGI("OnInputStart task end!");
701     };
702     auto ret = eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
703     if (!ret) {
704         IMSA_HILOGE("OnInputStart PostTask failed!");
705     }
706 }
707 
OnDiscardTypingText()708 int32_t JsInputMethodEngineSetting::OnDiscardTypingText()
709 {
710     IMSA_HILOGI("DiscardTypingText start.");
711     std::string type = "discardTypingText";
712     auto entry = GetEntry(type);
713     if (entry == nullptr) {
714         IMSA_HILOGE("entry is nullptr!");
715         return ErrorCode::ERROR_NULL_POINTER;
716     }
717     auto eventHandler = GetEventHandler();
718     if (eventHandler == nullptr) {
719         IMSA_HILOGE("eventHandler is nullptr!");
720         return ErrorCode::ERROR_NULL_POINTER;
721     }
722     auto task = [entry]() { JsCallbackHandler::Traverse(entry->vecCopy); };
723     return eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP)
724         ? ErrorCode::NO_ERROR : ErrorCode::ERROR_IME;
725 }
726 
OnKeyboardStatus(bool isShow)727 void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
728 {
729     std::string type = isShow ? "keyboardShow" : "keyboardHide";
730     auto entry = GetEntry(type);
731     if (entry == nullptr) {
732         return;
733     }
734     auto eventHandler = GetEventHandler();
735     if (eventHandler == nullptr) {
736         IMSA_HILOGE("eventHandler is nullptr!");
737         return;
738     }
739 
740     auto task = [entry]() { JsCallbackHandler::Traverse(entry->vecCopy); };
741     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
742 }
743 
OnInputStop()744 int32_t JsInputMethodEngineSetting::OnInputStop()
745 {
746     std::string type = "inputStop";
747     auto entry = GetEntry(type);
748     if (entry == nullptr) {
749         return ErrorCode::ERROR_NULL_POINTER;
750     }
751     auto eventHandler = GetEventHandler();
752     if (eventHandler == nullptr) {
753         IMSA_HILOGE("eventHandler is nullptr!");
754         return ErrorCode::ERROR_NULL_POINTER;
755     }
756     auto task = [entry]() { JsCallbackHandler::Traverse(entry->vecCopy); };
757     return eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP)
758         ? ErrorCode::NO_ERROR : ErrorCode::ERROR_IME;
759 }
760 
OnSetCallingWindow(uint32_t windowId)761 void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
762 {
763     std::string type = "setCallingWindow";
764     auto entry = GetEntry(type, [&windowId](UvEntry &entry) { entry.windowid = windowId; });
765     if (entry == nullptr) {
766         return;
767     }
768     auto eventHandler = GetEventHandler();
769     if (eventHandler == nullptr) {
770         IMSA_HILOGE("eventHandler is nullptr!");
771         return;
772     }
773     IMSA_HILOGD("windowId: %{public}d.", windowId);
774     auto task = [entry]() {
775         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
776             if (argc == 0) {
777                 return false;
778             }
779             // 0 means the first param of callback.
780             napi_create_uint32(env, entry->windowid, &args[0]);
781             return true;
782         };
783         // 1 means callback has one param.
784         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
785     };
786     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
787 }
788 
OnSetSubtype(const SubProperty & property)789 void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
790 {
791     std::string type = "setSubtype";
792     auto entry = GetEntry(type, [&property](UvEntry &entry) { entry.subProperty = property; });
793     if (entry == nullptr) {
794         IMSA_HILOGD("failed to get uv entry.");
795         return;
796     }
797     auto eventHandler = GetEventHandler();
798     if (eventHandler == nullptr) {
799         IMSA_HILOGE("eventHandler is nullptr!");
800         return;
801     }
802     IMSA_HILOGI("subtypeId: %{public}s.", property.id.c_str());
803     auto task = [entry]() {
804         auto getSubtypeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
805             if (argc == 0) {
806                 return false;
807             }
808             napi_value jsObject = GetResultOnSetSubtype(env, entry->subProperty);
809             if (jsObject == nullptr) {
810                 IMSA_HILOGE("jsObject is nullptr!");
811                 return false;
812             }
813             // 0 means the first param of callback.
814             args[0] = { jsObject };
815             return true;
816         };
817         // 1 means callback has one param.
818         JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSubtypeProperty });
819     };
820     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
821 }
822 
OnSecurityChange(int32_t security)823 void JsInputMethodEngineSetting::OnSecurityChange(int32_t security)
824 {
825     std::string type = "securityModeChange";
826     auto entry = GetEntry(type, [&security](UvEntry &entry) { entry.security = security; });
827     if (entry == nullptr) {
828         IMSA_HILOGD("failed to get uv entry.");
829         return;
830     }
831     auto eventHandler = GetEventHandler();
832     if (eventHandler == nullptr) {
833         IMSA_HILOGE("eventHandler is nullptr!");
834         return;
835     }
836     IMSA_HILOGI("run in: %{public}s", type.c_str());
837     auto task = [entry]() {
838         auto getSecurityProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
839             if (argc == 0) {
840                 return false;
841             }
842             // 0 means the first param of callback.
843             napi_create_int32(env, entry->security, &args[0]);
844             return true;
845         };
846             // 1 means callback has one param.
847             JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSecurityProperty });
848     };
849     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
850 }
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)851 void JsInputMethodEngineSetting::ReceivePrivateCommand(
852     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
853 {
854     IMSA_HILOGD("start.");
855     std::string type = "privateCommand";
856     auto entry = GetEntry(type, [&privateCommand](UvEntry &entry) { entry.privateCommand = privateCommand; });
857     if (entry == nullptr) {
858         return;
859     }
860     auto eventHandler = GetEventHandler();
861     if (eventHandler == nullptr) {
862         IMSA_HILOGE("eventHandler is nullptr!");
863         return;
864     }
865     auto task = [entry]() {
866         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
867             if (argc < 1) {
868                 return false;
869             }
870             napi_value jsObject = JsUtils::GetJsPrivateCommand(env, entry->privateCommand);
871             if (jsObject == nullptr) {
872                 IMSA_HILOGE("jsObject is nullptr!");
873                 return false;
874             }
875             // 0 means the first param of callback.
876             args[0] = { jsObject };
877             return true;
878         };
879         // 1 means callback has 1 params.
880         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
881     };
882     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
883 }
884 
GetEventHandler()885 std::shared_ptr<AppExecFwk::EventHandler> JsInputMethodEngineSetting::GetEventHandler()
886 {
887     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
888     return handler_;
889 }
890 
GetEntry(const std::string & type,EntrySetter entrySetter)891 std::shared_ptr<JsInputMethodEngineSetting::UvEntry> JsInputMethodEngineSetting::GetEntry(const std::string &type,
892     EntrySetter entrySetter)
893 {
894     IMSA_HILOGD("type: %{public}s.", type.c_str());
895     std::shared_ptr<UvEntry> entry = nullptr;
896     {
897         std::lock_guard<std::recursive_mutex> lock(mutex_);
898         if (jsCbMap_[type].empty()) {
899             IMSA_HILOGD("%{public}s cb-vector is empty", type.c_str());
900             return nullptr;
901         }
902         entry = std::make_shared<UvEntry>(jsCbMap_[type], type);
903     }
904     if (entrySetter != nullptr) {
905         entrySetter(*entry);
906     }
907     return entry;
908 }
909 
IsCallbackRegistered(const std::string & type)910 bool JsInputMethodEngineSetting::IsCallbackRegistered(const std::string &type)
911 {
912     IMSA_HILOGD("type: %{public}s.", type.c_str());
913     std::lock_guard<std::recursive_mutex> lock(mutex_);
914     if (jsCbMap_[type].empty()) {
915         IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str());
916         return false;
917     }
918     return true;
919 }
920 
PostTaskToEventHandler(std::function<void ()> task,const std::string & taskName)921 bool JsInputMethodEngineSetting::PostTaskToEventHandler(std::function<void()> task, const std::string &taskName)
922 {
923     auto eventHandler = GetEventHandler();
924     if (eventHandler == nullptr) {
925         IMSA_HILOGE("eventHandler is nullptr!");
926         return false;
927     }
928     if (eventHandler == AppExecFwk::EventHandler::Current()) {
929         IMSA_HILOGE("in current thread!");
930         return false;
931     }
932     eventHandler->PostTask(task, taskName, 0, AppExecFwk::EventQueue::Priority::VIP);
933     return true;
934 }
935 
OnCallingDisplayIdChanged(uint64_t callingDisplayId)936 void JsInputMethodEngineSetting::OnCallingDisplayIdChanged(uint64_t callingDisplayId)
937 {
938     std::string type = "callingDisplayDidChange";
939     if (callingDisplayId > UINT32_MAX) {
940         IMSA_HILOGE("callingDisplayId over range!");
941         return;
942     }
943     auto entry = GetEntry(type, [&callingDisplayId](UvEntry &entry) { entry.callingDisplayId = callingDisplayId; });
944     if (entry == nullptr) {
945         return;
946     }
947     auto eventHandler = GetEventHandler();
948     if (eventHandler == nullptr) {
949         IMSA_HILOGE("eventHandler is nullptr!");
950         return;
951     }
952     IMSA_HILOGD("callingDisplayId: %{public}d", static_cast<uint32_t>(callingDisplayId));
953     auto task = [entry]() {
954         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
955             if (argc == 0) {
956                 return false;
957             }
958             if (entry == nullptr) {
959                 return false;
960             }
961             // 0 means the first param of callback.
962             uint32_t displayId = static_cast<uint32_t>(entry->callingDisplayId);
963             args[0] = JsUtil::GetValue(env, displayId);
964             return true;
965         };
966         // 1 means callback has one param.
967         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
968     };
969     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
970 }
971 } // namespace MiscServices
972 } // namespace OHOS
973