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