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(napi_value callback, std::string type,
304 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(env, JsUtils::Convert(ErrorCode::ERROR_NOT_DEFAULT_IME), "default ime check failed",
368 TYPE_NONE);
369 }
370 #ifndef SCENE_BOARD_ENABLE
371 if (type == "callingDisplayDidChange") {
372 JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_DEVICE_UNSUPPORTED),
373 "capability not supported.", TYPE_NONE);
374 }
375 #endif
376 IMSA_HILOGD("subscribe type:%{public}s.", type.c_str());
377 auto engine = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
378 if (engine == nullptr) {
379 return nullptr;
380 }
381 std::shared_ptr<JSCallbackObject> callback =
382 std::make_shared<JSCallbackObject>(env, argv[ARGC_ONE], std::this_thread::get_id(),
383 AppExecFwk::EventHandler::Current());
384 engine->RegisterListener(argv[ARGC_ONE], type, callback);
385
386 napi_value result = nullptr;
387 napi_get_null(env, &result);
388 return result;
389 }
390
GetContext(napi_env env,napi_value in,std::shared_ptr<OHOS::AbilityRuntime::Context> & context)391 napi_status JsInputMethodEngineSetting::GetContext(napi_env env, napi_value in,
392 std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
393 {
394 bool stageMode = false;
395 napi_status status = OHOS::AbilityRuntime::IsStageContext(env, in, stageMode);
396 if (status != napi_ok || (!stageMode)) {
397 IMSA_HILOGE("it's not in stage mode.");
398 return status;
399 }
400 context = OHOS::AbilityRuntime::GetStageModeContext(env, in);
401 if (context == nullptr) {
402 IMSA_HILOGE("context is nullptr.");
403 return napi_generic_failure;
404 }
405 return napi_ok;
406 }
407
CreatePanel(napi_env env,napi_callback_info info)408 napi_value JsInputMethodEngineSetting::CreatePanel(napi_env env, napi_callback_info info)
409 {
410 auto ctxt = std::make_shared<PanelContext>();
411 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
412 PARAM_CHECK_RETURN(env, argc >= 2, "at least two parameters is required.", TYPE_NONE, napi_invalid_arg);
413 napi_valuetype valueType = napi_undefined;
414 // 0 means parameter of ctx<BaseContext>
415 napi_typeof(env, argv[0], &valueType);
416 PARAM_CHECK_RETURN(env, valueType == napi_object, "ctx type must be BaseContext.", TYPE_NONE, napi_invalid_arg);
417 napi_status status = GetContext(env, argv[0], ctxt->context);
418 if (status != napi_ok) {
419 return status;
420 }
421 // 1 means parameter of info<PanelInfo>
422 napi_typeof(env, argv[1], &valueType);
423 PARAM_CHECK_RETURN(env, valueType == napi_object, "param info type must be PanelInfo.", TYPE_NONE,
424 napi_invalid_arg);
425 status = JsUtils::GetValue(env, argv[1], ctxt->panelInfo);
426 PARAM_CHECK_RETURN(env, status == napi_ok, "js param info covert failed!", TYPE_NONE, napi_invalid_arg);
427 return status;
428 };
429
430 auto exec = [ctxt](AsyncCall::Context *ctx) {
431 auto ret = InputMethodAbility::GetInstance()->CreatePanel(ctxt->context, ctxt->panelInfo, ctxt->panel);
432 ctxt->SetErrorCode(ret);
433 CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "JsInputMethodEngineSetting CreatePanel failed!");
434 ctxt->SetState(napi_ok);
435 };
436
437 auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
438 JsPanel *jsPanel = nullptr;
439 napi_value constructor = JsPanel::Init(env);
440 CHECK_RETURN(constructor != nullptr, "failed to get panel constructor!", napi_generic_failure);
441
442 napi_status status = napi_new_instance(env, constructor, 0, nullptr, result);
443 CHECK_RETURN(status == napi_ok, "jsPanel new instance failed!", napi_generic_failure);
444
445 status = napi_unwrap(env, *result, (void **)(&jsPanel));
446 CHECK_RETURN((status == napi_ok) && (jsPanel != nullptr), "get jsPanel unwrap failed!", napi_generic_failure);
447 jsPanel->SetNative(ctxt->panel);
448 return napi_ok;
449 };
450
451 ctxt->SetAction(std::move(input), std::move(output));
452 // 3 means JsAPI:createPanel has 3 params at most.
453 AsyncCall asyncCall(env, info, ctxt, 3);
454 return asyncCall.Call(env, exec, "createPanel");
455 }
456
DestroyPanel(napi_env env,napi_callback_info info)457 napi_value JsInputMethodEngineSetting::DestroyPanel(napi_env env, napi_callback_info info)
458 {
459 auto ctxt = std::make_shared<PanelContext>();
460 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
461 PARAM_CHECK_RETURN(env, argc >= 1, "at least one parameter is required!", TYPE_NONE, napi_invalid_arg);
462 napi_valuetype valueType = napi_undefined;
463 napi_typeof(env, argv[0], &valueType);
464 PARAM_CHECK_RETURN(env, valueType == napi_object, "param panel type must be InputMethodPanel.", TYPE_NONE,
465 napi_invalid_arg);
466 bool isPanel = false;
467 napi_value constructor = JsPanel::Init(env);
468 CHECK_RETURN(constructor != nullptr, "failed to get panel constructor.", napi_invalid_arg);
469 napi_status status = napi_instanceof(env, argv[0], constructor, &isPanel);
470 CHECK_RETURN((status == napi_ok) && isPanel, "param verification failed, it's not expected panel instance!",
471 status);
472 JsPanel *jsPanel = nullptr;
473 status = napi_unwrap(env, argv[0], (void **)(&jsPanel));
474 CHECK_RETURN((status == napi_ok) && (jsPanel != nullptr), "failed to unwrap JsPanel!", status);
475 ctxt->panel = jsPanel->GetNative();
476 CHECK_RETURN((ctxt->panel != nullptr), "panel is nullptr!", napi_invalid_arg);
477 return status;
478 };
479
480 auto exec = [ctxt](AsyncCall::Context *ctx) { ctxt->SetState(napi_ok); };
481
482 auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
483 CHECK_RETURN((ctxt->panel != nullptr), "panel is nullptr!", napi_generic_failure);
484 auto errCode = InputMethodAbility::GetInstance()->DestroyPanel(ctxt->panel);
485 if (errCode != ErrorCode::NO_ERROR) {
486 IMSA_HILOGE("DestroyPanel failed, errCode: %{public}d!", errCode);
487 return napi_generic_failure;
488 }
489 ctxt->panel = nullptr;
490 return napi_ok;
491 };
492
493 ctxt->SetAction(std::move(input), std::move(output));
494 // 2 means JsAPI:destroyPanel has 2 params at most.
495 AsyncCall asyncCall(env, info, ctxt, 2);
496 return asyncCall.Call(env, exec, "destroyPanel");
497 }
498
GetSecurityMode(napi_env env,napi_callback_info info)499 napi_value JsInputMethodEngineSetting::GetSecurityMode(napi_env env, napi_callback_info info)
500 {
501 IMSA_HILOGD("start get security mode.");
502 size_t argc = 1;
503 napi_value argv[1] = { nullptr };
504 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
505 int32_t security;
506 int32_t ret = InputMethodAbility::GetInstance()->GetSecurityMode(security);
507 if (ret != ErrorCode::NO_ERROR) {
508 JsUtils::ThrowException(env, JsUtils::Convert(ret), "failed to get security mode", TYPE_NONE);
509 }
510 napi_value result = nullptr;
511 napi_create_int32(env, security, &result);
512 return result;
513 }
514
UnSubscribe(napi_env env,napi_callback_info info)515 napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_info info)
516 {
517 size_t argc = ARGC_TWO;
518 napi_value argv[ARGC_TWO] = { nullptr };
519 napi_value thisVar = nullptr;
520 void *data = nullptr;
521 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
522 std::string type;
523 // 1 means least param num.
524 if (argc < 1 || !JsUtil::GetValue(env, argv[0], type) ||
525 !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type)) {
526 IMSA_HILOGE("unsubscribe failed, type: %{public}s!", type.c_str());
527 return nullptr;
528 }
529 if (type == "privateCommand" && !InputMethodAbility::GetInstance()->IsDefaultIme()) {
530 JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_NOT_DEFAULT_IME), "default ime check failed",
531 TYPE_NONE);
532 }
533 // if the second param is not napi_function/napi_null/napi_undefined, return
534 auto paramType = JsUtil::GetType(env, argv[1]);
535 if (paramType != napi_function && paramType != napi_null && paramType != napi_undefined) {
536 return nullptr;
537 }
538 // if the second param is napi_function, delete it, else delete all
539 argv[1] = paramType == napi_function ? argv[1] : nullptr;
540
541 IMSA_HILOGD("unsubscribe type: %{public}s.", type.c_str());
542 auto setting = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
543 if (setting == nullptr) {
544 return nullptr;
545 }
546 setting->UnRegisterListener(argv[ARGC_ONE], type);
547 napi_value result = nullptr;
548 napi_get_null(env, &result);
549 return result;
550 }
551
GetResultOnSetSubtype(napi_env env,const SubProperty & property)552 napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property)
553 {
554 napi_value subType = nullptr;
555 napi_create_object(env, &subType);
556
557 napi_value label = nullptr;
558 napi_create_string_utf8(env, property.label.c_str(), property.name.size(), &label);
559 napi_set_named_property(env, subType, "label", label);
560
561 napi_value labelId = nullptr;
562 napi_create_uint32(env, property.labelId, &labelId);
563 napi_set_named_property(env, subType, "labelId", labelId);
564
565 napi_value name = nullptr;
566 napi_create_string_utf8(env, property.name.c_str(), property.name.size(), &name);
567 napi_set_named_property(env, subType, "name", name);
568
569 napi_value id = nullptr;
570 napi_create_string_utf8(env, property.id.c_str(), property.id.size(), &id);
571 napi_set_named_property(env, subType, "id", id);
572
573 napi_value mode = nullptr;
574 napi_create_string_utf8(env, property.mode.c_str(), property.mode.size(), &mode);
575 napi_set_named_property(env, subType, "mode", mode);
576
577 napi_value locale = nullptr;
578 napi_create_string_utf8(env, property.locale.c_str(), property.locale.size(), &locale);
579 napi_set_named_property(env, subType, "locale", locale);
580
581 napi_value language = nullptr;
582 napi_create_string_utf8(env, property.language.c_str(), property.language.size(), &language);
583 napi_set_named_property(env, subType, "language", language);
584
585 napi_value icon = nullptr;
586 napi_create_string_utf8(env, property.icon.c_str(), property.icon.size(), &icon);
587 napi_set_named_property(env, subType, "icon", icon);
588
589 napi_value iconId = nullptr;
590 napi_create_uint32(env, property.iconId, &iconId);
591 napi_set_named_property(env, subType, "iconId", iconId);
592
593 napi_value extra = nullptr;
594 napi_create_object(env, &extra);
595 napi_set_named_property(env, subType, "extra", extra);
596
597 return subType;
598 }
599
OnInputStart()600 void JsInputMethodEngineSetting::OnInputStart()
601 {
602 IMSA_HILOGI("OnInputStart start.");
603 std::string type = "inputStart";
604 auto entry = GetEntry(type);
605 if (entry == nullptr) {
606 IMSA_HILOGE("entry is nullptr!");
607 return;
608 }
609 auto eventHandler = GetEventHandler();
610 if (eventHandler == nullptr) {
611 IMSA_HILOGE("eventHandler is nullptr!");
612 return;
613 }
614 auto task = [entry]() {
615 IMSA_HILOGI("OnInputStart task start!");
616 auto paramGetter = [](napi_env env, napi_value *args, uint8_t argc) -> bool {
617 if (argc < 2) {
618 IMSA_HILOGE("the num:%{public}d of params in OnInputStart is abnormal!", argc);
619 return false;
620 }
621 napi_value textInput = JsTextInputClientEngine::GetTextInputClientInstance(env);
622 napi_value keyBoardController = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env);
623 if (keyBoardController == nullptr || textInput == nullptr) {
624 IMSA_HILOGE("get KBCins or TICins failed!");
625 return false;
626 }
627 // 0 means the first param of callback.
628 args[0] = keyBoardController;
629 // 1 means the second param of callback.
630 args[1] = textInput;
631 return true;
632 };
633 // 2 means callback has 2 params.
634 JsCallbackHandler::Traverse(entry->vecCopy, { 2, paramGetter });
635 IMSA_HILOGI("OnInputStart task end!");
636 };
637 auto ret = eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
638 if (!ret) {
639 IMSA_HILOGE("OnInputStart PostTask failed!");
640 }
641 }
642
OnKeyboardStatus(bool isShow)643 void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow)
644 {
645 std::string type = isShow ? "keyboardShow" : "keyboardHide";
646 auto entry = GetEntry(type);
647 if (entry == nullptr) {
648 return;
649 }
650 auto eventHandler = GetEventHandler();
651 if (eventHandler == nullptr) {
652 IMSA_HILOGE("eventHandler is nullptr!");
653 return;
654 }
655
656 auto task = [entry]() { JsCallbackHandler::Traverse(entry->vecCopy); };
657 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
658 }
659
OnInputStop()660 int32_t JsInputMethodEngineSetting::OnInputStop()
661 {
662 std::string type = "inputStop";
663 auto entry = GetEntry(type);
664 if (entry == nullptr) {
665 return ErrorCode::ERROR_NULL_POINTER;
666 }
667 auto eventHandler = GetEventHandler();
668 if (eventHandler == nullptr) {
669 IMSA_HILOGE("eventHandler is nullptr!");
670 return ErrorCode::ERROR_NULL_POINTER;
671 }
672 auto task = [entry]() { JsCallbackHandler::Traverse(entry->vecCopy); };
673 return eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP)
674 ? ErrorCode::NO_ERROR : ErrorCode::ERROR_IME;
675 }
676
OnSetCallingWindow(uint32_t windowId)677 void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId)
678 {
679 std::string type = "setCallingWindow";
680 auto entry = GetEntry(type, [&windowId](UvEntry &entry) { entry.windowid = windowId; });
681 if (entry == nullptr) {
682 return;
683 }
684 auto eventHandler = GetEventHandler();
685 if (eventHandler == nullptr) {
686 IMSA_HILOGE("eventHandler is nullptr!");
687 return;
688 }
689 IMSA_HILOGD("windowId: %{public}d.", windowId);
690 auto task = [entry]() {
691 auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
692 if (argc == 0) {
693 return false;
694 }
695 // 0 means the first param of callback.
696 napi_create_uint32(env, entry->windowid, &args[0]);
697 return true;
698 };
699 // 1 means callback has one param.
700 JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
701 };
702 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
703 }
704
OnSetSubtype(const SubProperty & property)705 void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property)
706 {
707 std::string type = "setSubtype";
708 auto entry = GetEntry(type, [&property](UvEntry &entry) { entry.subProperty = property; });
709 if (entry == nullptr) {
710 IMSA_HILOGD("failed to get uv entry.");
711 return;
712 }
713 auto eventHandler = GetEventHandler();
714 if (eventHandler == nullptr) {
715 IMSA_HILOGE("eventHandler is nullptr!");
716 return;
717 }
718 IMSA_HILOGI("subtypeId: %{public}s.", property.id.c_str());
719 auto task = [entry]() {
720 auto getSubtypeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
721 if (argc == 0) {
722 return false;
723 }
724 napi_value jsObject = GetResultOnSetSubtype(env, entry->subProperty);
725 if (jsObject == nullptr) {
726 IMSA_HILOGE("jsObject is nullptr!");
727 return false;
728 }
729 // 0 means the first param of callback.
730 args[0] = { jsObject };
731 return true;
732 };
733 // 1 means callback has one param.
734 JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSubtypeProperty });
735 };
736 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
737 }
738
OnSecurityChange(int32_t security)739 void JsInputMethodEngineSetting::OnSecurityChange(int32_t security)
740 {
741 std::string type = "securityModeChange";
742 auto entry = GetEntry(type, [&security](UvEntry &entry) { entry.security = security; });
743 if (entry == nullptr) {
744 IMSA_HILOGD("failed to get uv entry.");
745 return;
746 }
747 auto eventHandler = GetEventHandler();
748 if (eventHandler == nullptr) {
749 IMSA_HILOGE("eventHandler is nullptr!");
750 return;
751 }
752 IMSA_HILOGI("run in: %{public}s", type.c_str());
753 auto task = [entry]() {
754 auto getSecurityProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
755 if (argc == 0) {
756 return false;
757 }
758 // 0 means the first param of callback.
759 napi_create_int32(env, entry->security, &args[0]);
760 return true;
761 };
762 // 1 means callback has one param.
763 JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSecurityProperty });
764 };
765 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
766 }
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)767 void JsInputMethodEngineSetting::ReceivePrivateCommand(
768 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
769 {
770 IMSA_HILOGD("start.");
771 std::string type = "privateCommand";
772 auto entry = GetEntry(type, [&privateCommand](UvEntry &entry) { entry.privateCommand = privateCommand; });
773 if (entry == nullptr) {
774 return;
775 }
776 auto eventHandler = GetEventHandler();
777 if (eventHandler == nullptr) {
778 IMSA_HILOGE("eventHandler is nullptr!");
779 return;
780 }
781 auto task = [entry]() {
782 auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
783 if (argc < 1) {
784 return false;
785 }
786 napi_value jsObject = JsUtils::GetJsPrivateCommand(env, entry->privateCommand);
787 if (jsObject == nullptr) {
788 IMSA_HILOGE("jsObject is nullptr!");
789 return false;
790 }
791 // 0 means the first param of callback.
792 args[0] = { jsObject };
793 return true;
794 };
795 // 1 means callback has 1 params.
796 JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
797 };
798 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
799 }
800
GetUVwork(const std::string & type,EntrySetter entrySetter)801 uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter)
802 {
803 IMSA_HILOGD("run in, type: %{public}s.", type.c_str());
804 UvEntry *entry = nullptr;
805 {
806 std::lock_guard<std::recursive_mutex> lock(mutex_);
807
808 if (jsCbMap_[type].empty()) {
809 IMSA_HILOGD("%{public}s cb-vector is empty.", type.c_str());
810 return nullptr;
811 }
812 entry = new (std::nothrow) UvEntry(jsCbMap_[type], type);
813 if (entry == nullptr) {
814 IMSA_HILOGE("entry is nullptr!");
815 return nullptr;
816 }
817 if (entrySetter != nullptr) {
818 entrySetter(*entry);
819 }
820 }
821 uv_work_t *work = new (std::nothrow) uv_work_t;
822 if (work == nullptr) {
823 IMSA_HILOGE("work is nullptr!");
824 delete entry;
825 return nullptr;
826 }
827 work->data = entry;
828 return work;
829 }
830
GetEventHandler()831 std::shared_ptr<AppExecFwk::EventHandler> JsInputMethodEngineSetting::GetEventHandler()
832 {
833 std::lock_guard<std::mutex> lock(eventHandlerMutex_);
834 return handler_;
835 }
836
GetEntry(const std::string & type,EntrySetter entrySetter)837 std::shared_ptr<JsInputMethodEngineSetting::UvEntry> JsInputMethodEngineSetting::GetEntry(const std::string &type,
838 EntrySetter entrySetter)
839 {
840 IMSA_HILOGD("type: %{public}s.", type.c_str());
841 std::shared_ptr<UvEntry> entry = nullptr;
842 {
843 std::lock_guard<std::recursive_mutex> lock(mutex_);
844 if (jsCbMap_[type].empty()) {
845 IMSA_HILOGD("%{public}s cb-vector is empty", type.c_str());
846 return nullptr;
847 }
848 entry = std::make_shared<UvEntry>(jsCbMap_[type], type);
849 }
850 if (entrySetter != nullptr) {
851 entrySetter(*entry);
852 }
853 return entry;
854 }
855
FreeWorkIfFail(int ret,uv_work_t * work)856 void JsInputMethodEngineSetting::FreeWorkIfFail(int ret, uv_work_t *work)
857 {
858 if (ret == 0 || work == nullptr) {
859 return;
860 }
861
862 UvEntry *data = static_cast<UvEntry *>(work->data);
863 delete data;
864 delete work;
865 IMSA_HILOGE("uv_queue_work failed retCode: %{public}d!", ret);
866 }
867
PostTaskToEventHandler(std::function<void ()> task,const std::string & taskName)868 bool JsInputMethodEngineSetting::PostTaskToEventHandler(std::function<void()> task, const std::string &taskName)
869 {
870 auto eventHandler = GetEventHandler();
871 if (eventHandler == nullptr) {
872 IMSA_HILOGE("eventHandler is nullptr!");
873 return false;
874 }
875 if (eventHandler == AppExecFwk::EventHandler::Current()) {
876 IMSA_HILOGE("in current thread!");
877 return false;
878 }
879 eventHandler->PostTask(task, taskName, 0, AppExecFwk::EventQueue::Priority::VIP);
880 return true;
881 }
882
OnCallingDisplayIdChanged(uint64_t callingDisplayId)883 void JsInputMethodEngineSetting::OnCallingDisplayIdChanged(uint64_t callingDisplayId)
884 {
885 std::string type = "callingDisplayDidChange";
886 if (callingDisplayId > UINT32_MAX) {
887 IMSA_HILOGE("callingDisplayId over range!");
888 return;
889 }
890 auto entry = GetEntry(type, [&callingDisplayId](UvEntry &entry) { entry.callingDisplayId = callingDisplayId; });
891 if (entry == nullptr) {
892 return;
893 }
894 auto eventHandler = GetEventHandler();
895 if (eventHandler == nullptr) {
896 IMSA_HILOGE("eventHandler is nullptr!");
897 return;
898 }
899 IMSA_HILOGD("callingDisplayId: %{public}d", static_cast<uint32_t>(callingDisplayId));
900 auto task = [entry]() {
901 auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
902 if (argc == 0) {
903 return false;
904 }
905 if (entry == nullptr) {
906 return false;
907 }
908 // 0 means the first param of callback.
909 uint32_t displayId = static_cast<uint32_t>(entry->callingDisplayId);
910 args[0] = JsUtil::GetValue(env, displayId);
911 return true;
912 };
913 // 1 means callback has one param.
914 JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
915 };
916 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
917 }
918 } // namespace MiscServices
919 } // namespace OHOS
920