1 /*
2 * Copyright (c) 2023 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_panel.h"
17
18 #include "event_checker.h"
19 #include "input_method_ability.h"
20 #include "inputmethod_trace.h"
21 #include "js_text_input_client_engine.h"
22 #include "js_util.h"
23 #include "js_utils.h"
24 #include "napi/native_common.h"
25 #include "panel_listener_impl.h"
26
27 namespace OHOS {
28 namespace MiscServices {
29 using namespace std::chrono;
30 using WMError = OHOS::Rosen::WMError;
31 const std::string JsPanel::CLASS_NAME = "Panel";
32 thread_local napi_ref JsPanel::panelConstructorRef_ = nullptr;
33 std::mutex JsPanel::panelConstructorMutex_;
34 constexpr int32_t MAX_WAIT_TIME = 10;
35 constexpr int32_t MAX_INPUT_REGION_LEN = 4;
36 const constexpr char *LANDSCAPE_REGION_PARAM_NAME = "landscapeInputRegion";
37 const constexpr char *PORTRAIT_REGION_PARAM_NAME = "portraitInputRegion";
38 FFRTBlockQueue<JsEventInfo> JsPanel::jsQueue_{ MAX_WAIT_TIME };
39
Init(napi_env env)40 napi_value JsPanel::Init(napi_env env)
41 {
42 IMSA_HILOGI("JsPanel start.");
43 napi_value constructor = nullptr;
44 std::lock_guard<std::mutex> lock(panelConstructorMutex_);
45 if (panelConstructorRef_ != nullptr) {
46 napi_status status = napi_get_reference_value(env, panelConstructorRef_, &constructor);
47 CHECK_RETURN(status == napi_ok, "failed to get jsPanel constructor.", nullptr);
48 return constructor;
49 }
50 const napi_property_descriptor properties[] = {
51 DECLARE_NAPI_FUNCTION("setUiContent", SetUiContent),
52 DECLARE_NAPI_FUNCTION("resize", Resize),
53 DECLARE_NAPI_FUNCTION("moveTo", MoveTo),
54 DECLARE_NAPI_FUNCTION("show", Show),
55 DECLARE_NAPI_FUNCTION("hide", Hide),
56 DECLARE_NAPI_FUNCTION("changeFlag", ChangeFlag),
57 DECLARE_NAPI_FUNCTION("setPrivacyMode", SetPrivacyMode),
58 DECLARE_NAPI_FUNCTION("on", Subscribe),
59 DECLARE_NAPI_FUNCTION("off", UnSubscribe),
60 DECLARE_NAPI_FUNCTION("adjustPanelRect", AdjustPanelRect),
61 DECLARE_NAPI_FUNCTION("updateRegion", UpdateRegion),
62 DECLARE_NAPI_FUNCTION("startMoving", StartMoving),
63 DECLARE_NAPI_FUNCTION("getDisplayId", GetDisplayId),
64 DECLARE_NAPI_FUNCTION("setImmersiveMode", SetImmersiveMode),
65 DECLARE_NAPI_FUNCTION("getImmersiveMode", GetImmersiveMode),
66 };
67 NAPI_CALL(env, napi_define_class(env, CLASS_NAME.c_str(), CLASS_NAME.size(), JsNew, nullptr,
68 sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor));
69 CHECK_RETURN(constructor != nullptr, "failed to define class!", nullptr);
70 NAPI_CALL(env, napi_create_reference(env, constructor, 1, &panelConstructorRef_));
71 return constructor;
72 }
73
JsNew(napi_env env,napi_callback_info info)74 napi_value JsPanel::JsNew(napi_env env, napi_callback_info info)
75 {
76 IMSA_HILOGD("create panel instance start.");
77 std::shared_ptr<PanelListenerImpl> panelImpl = PanelListenerImpl::GetInstance();
78 if (panelImpl != nullptr) {
79 IMSA_HILOGD("set eventHandler.");
80 panelImpl->SetEventHandler(AppExecFwk::EventHandler::Current());
81 }
82 JsPanel *panel = new (std::nothrow) JsPanel();
83 CHECK_RETURN(panel != nullptr, "no memory for JsPanel!", nullptr);
84 auto finalize = [](napi_env env, void *data, void *hint) {
85 IMSA_HILOGD("jsPanel finalize.");
86 auto *jsPanel = reinterpret_cast<JsPanel *>(data);
87 CHECK_RETURN_VOID(jsPanel != nullptr, "finalize nullptr!");
88 jsPanel->GetNative() = nullptr;
89 delete jsPanel;
90 };
91 napi_value thisVar = nullptr;
92 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
93 if (status != napi_ok) {
94 IMSA_HILOGE("failed to get cb info: %{public}d!", status);
95 delete panel;
96 return nullptr;
97 }
98 status = napi_wrap(env, thisVar, panel, finalize, nullptr, nullptr);
99 if (status != napi_ok) {
100 IMSA_HILOGE("failed to wrap: %{public}d!", status);
101 delete panel;
102 return nullptr;
103 }
104 return thisVar;
105 }
106
~JsPanel()107 JsPanel::~JsPanel()
108 {
109 inputMethodPanel_ = nullptr;
110 }
111
SetNative(const std::shared_ptr<InputMethodPanel> & panel)112 void JsPanel::SetNative(const std::shared_ptr<InputMethodPanel> &panel)
113 {
114 inputMethodPanel_ = panel;
115 }
116
GetNative()117 std::shared_ptr<InputMethodPanel> JsPanel::GetNative()
118 {
119 return inputMethodPanel_;
120 }
121
SetUiContent(napi_env env,napi_callback_info info)122 napi_value JsPanel::SetUiContent(napi_env env, napi_callback_info info)
123 {
124 IMSA_HILOGI("JsPanel start.");
125 auto ctxt = std::make_shared<PanelContentContext>(env, info);
126 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
127 napi_status status = napi_generic_failure;
128 PARAM_CHECK_RETURN(env, argc >= 1, "at least one parameter is required!", TYPE_NONE, status);
129 // 0 means the first param path<std::string>
130 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], ctxt->path) == napi_ok,
131 "js param path covert failed, must be string!", TYPE_NONE, status);
132 // if type of argv[1] is object, we will get value of 'storage' from it.
133 if (argc >= 2) {
134 napi_valuetype valueType = napi_undefined;
135 status = napi_typeof(env, argv[1], &valueType);
136 CHECK_RETURN(status == napi_ok, "get valueType failed!", status);
137 if (valueType == napi_object) {
138 napi_ref storage = nullptr;
139 napi_create_reference(env, argv[1], 1, &storage);
140 auto contentStorage = (storage == nullptr) ? nullptr
141 : std::shared_ptr<NativeReference>(
142 reinterpret_cast<NativeReference *>(storage));
143 ctxt->contentStorage = contentStorage;
144 }
145 }
146 ctxt->info = { std::chrono::system_clock::now(), JsEvent::SET_UI_CONTENT };
147 jsQueue_.Push(ctxt->info);
148 return napi_ok;
149 };
150
151 auto exec = [ctxt](AsyncCall::Context *ctx) { ctxt->SetState(napi_ok); };
152 auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
153 jsQueue_.Wait(ctxt->info);
154 if (ctxt->inputMethodPanel == nullptr) {
155 IMSA_HILOGE("inputMethodPanel is nullptr!");
156 jsQueue_.Pop();
157 return napi_generic_failure;
158 }
159 auto code = ctxt->inputMethodPanel->SetUiContent(ctxt->path, env, ctxt->contentStorage);
160 jsQueue_.Pop();
161 if (code == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
162 ctxt->SetErrorCode(code);
163 ctxt->SetErrorMessage("path should be a path to specific page.");
164 return napi_generic_failure;
165 }
166 return napi_ok;
167 };
168 ctxt->SetAction(std::move(input), std::move(output));
169 // 3 means JsAPI:setUiContent has 3 params at most.
170 AsyncCall asyncCall(env, info, ctxt, 3);
171 return asyncCall.Call(env, exec, "setUiContent");
172 }
173
Resize(napi_env env,napi_callback_info info)174 napi_value JsPanel::Resize(napi_env env, napi_callback_info info)
175 {
176 auto ctxt = std::make_shared<PanelContentContext>(env, info);
177 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
178 napi_status status = napi_generic_failure;
179 PARAM_CHECK_RETURN(env, argc > 1, "at least two parameters is required", TYPE_NONE, status);
180 // 0 means the first param width<uint32_t>
181 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], ctxt->width) == napi_ok,
182 "width type must be number!", TYPE_NONE, status);
183 // 1 means the second param height<uint32_t>
184 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[1], ctxt->height) == napi_ok,
185 "height type must be number!", TYPE_NONE, status);
186 ctxt->info = { std::chrono::system_clock::now(), JsEvent::RESIZE };
187 jsQueue_.Push(ctxt->info);
188 return napi_ok;
189 };
190
191 auto exec = [ctxt](AsyncCall::Context *ctx) {
192 jsQueue_.Wait(ctxt->info);
193 if (ctxt->inputMethodPanel == nullptr) {
194 IMSA_HILOGE("inputMethodPanel_ is nullptr!");
195 jsQueue_.Pop();
196 return;
197 }
198 auto code = ctxt->inputMethodPanel->Resize(ctxt->width, ctxt->height);
199 jsQueue_.Pop();
200 if (code == ErrorCode::NO_ERROR) {
201 ctxt->SetState(napi_ok);
202 return;
203 }
204 ctxt->SetErrorCode(code);
205 };
206 ctxt->SetAction(std::move(input));
207 // 3 means JsAPI:resize has 3 params at most.
208 AsyncCall asyncCall(env, info, ctxt, 3);
209 return asyncCall.Call(env, exec, "resize");
210 }
211
MoveTo(napi_env env,napi_callback_info info)212 napi_value JsPanel::MoveTo(napi_env env, napi_callback_info info)
213 {
214 auto ctxt = std::make_shared<PanelContentContext>(env, info);
215 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
216 napi_status status = napi_generic_failure;
217 PARAM_CHECK_RETURN(env, argc > 1, "at least two parameters is required ", TYPE_NONE, status);
218 // 0 means the first param x<int32_t>
219 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], ctxt->x) == napi_ok, "x type must be number",
220 TYPE_NONE, status);
221 // 1 means the second param y<int32_t>
222 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[1], ctxt->y) == napi_ok, "y type must be number",
223 TYPE_NONE, status);
224 ctxt->info = { std::chrono::system_clock::now(), JsEvent::MOVE_TO };
225 jsQueue_.Push(ctxt->info);
226 return napi_ok;
227 };
228
229 auto exec = [ctxt](AsyncCall::Context *ctx) {
230 int64_t start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
231 jsQueue_.Wait(ctxt->info);
232 PrintEditorQueueInfoIfTimeout(start, ctxt->info);
233 if (ctxt->inputMethodPanel == nullptr) {
234 IMSA_HILOGE("inputMethodPanel_ is nullptr!");
235 jsQueue_.Pop();
236 return;
237 }
238 auto code = ctxt->inputMethodPanel->MoveTo(ctxt->x, ctxt->y);
239 jsQueue_.Pop();
240 if (code == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
241 ctxt->SetErrorCode(code);
242 return;
243 }
244 ctxt->SetState(napi_ok);
245 };
246 ctxt->SetAction(std::move(input));
247 // 3 means JsAPI:moveTo has 3 params at most.
248 AsyncCall asyncCall(env, info, ctxt, 3);
249 return asyncCall.Call(env, exec, "moveTo");
250 }
251
StartMoving(napi_env env,napi_callback_info info)252 napi_value JsPanel::StartMoving(napi_env env, napi_callback_info info)
253 {
254 napi_value self = nullptr;
255 NAPI_CALL(env, napi_get_cb_info(env, info, 0, nullptr, &self, nullptr));
256 RESULT_CHECK_RETURN(env, (self != nullptr), JsUtils::Convert(ErrorCode::ERROR_IME),
257 "", TYPE_NONE, JsUtil::Const::Null(env));
258 void *native = nullptr;
259 NAPI_CALL(env, napi_unwrap(env, self, &native));
260 RESULT_CHECK_RETURN(env, (native != nullptr), JsUtils::Convert(ErrorCode::ERROR_IME),
261 "", TYPE_NONE, JsUtil::Const::Null(env));
262 auto inputMethodPanel = reinterpret_cast<JsPanel *>(native)->GetNative();
263 if (inputMethodPanel == nullptr) {
264 IMSA_HILOGE("inputMethodPanel is nullptr!");
265 JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_IME),
266 "failed to start moving, inputMethodPanel is nullptr", TYPE_NONE);
267 return JsUtil::Const::Null(env);
268 }
269
270 auto ret = inputMethodPanel->StartMoving();
271 if (ret != ErrorCode::NO_ERROR) {
272 JsUtils::ThrowException(env, JsUtils::Convert(ret), "failed to start moving", TYPE_NONE);
273 }
274 return JsUtil::Const::Null(env);
275 }
276
GetDisplayId(napi_env env,napi_callback_info info)277 napi_value JsPanel::GetDisplayId(napi_env env, napi_callback_info info)
278 {
279 auto ctxt = std::make_shared<PanelContentContext>(env, info);
280 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
281 ctxt->info = { std::chrono::system_clock::now(), JsEvent::GET_DISPLAYID };
282 jsQueue_.Push(ctxt->info);
283 return napi_ok;
284 };
285 auto exec = [ctxt](AsyncCall::Context *ctx) {
286 jsQueue_.Wait(ctxt->info);
287 if (ctxt->inputMethodPanel == nullptr) {
288 IMSA_HILOGE("inputMethodPanel_ is nullptr!");
289 ctxt->SetErrorCode(ErrorCode::ERROR_IME);
290 jsQueue_.Pop();
291 return;
292 }
293 auto ret = ctxt->inputMethodPanel->GetDisplayId(ctxt->displayId);
294 jsQueue_.Pop();
295 if (ret != ErrorCode::NO_ERROR) {
296 IMSA_HILOGE("failed get displayId!");
297 ctxt->SetErrorCode(ret);
298 return;
299 }
300
301 if (ctxt->displayId > UINT32_MAX) {
302 IMSA_HILOGE("displayId is too large, displayId: %{public}" PRIu64 "", ctxt->displayId);
303 ctxt->SetErrorCode(ErrorCode::ERROR_WINDOW_MANAGER);
304 return;
305 }
306 ctxt->SetState(napi_ok);
307 };
308 auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
309 uint32_t displayId = static_cast<uint32_t>(ctxt->displayId);
310 return napi_create_uint32(env, displayId, result);
311 };
312 ctxt->SetAction(std::move(input), std::move(output));
313 // 1 means JsAPI:GetDisplayId has 1 params at most.
314 AsyncCall asyncCall(env, info, ctxt, 1);
315 return asyncCall.Call(env, exec, "getDisplayId");
316 }
317
PrintEditorQueueInfoIfTimeout(int64_t start,const JsEventInfo & currentInfo)318 void JsPanel::PrintEditorQueueInfoIfTimeout(int64_t start, const JsEventInfo ¤tInfo)
319 {
320 int64_t end = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
321 if (end - start >= MAX_WAIT_TIME) {
322 JsEventInfo frontInfo;
323 auto ret = jsQueue_.GetFront(frontInfo);
324 int64_t frontTime = duration_cast<microseconds>(frontInfo.timestamp.time_since_epoch()).count();
325 int64_t currentTime = duration_cast<microseconds>(currentInfo.timestamp.time_since_epoch()).count();
326 IMSA_HILOGI("ret:%{public}d,front[%{public}" PRId64 ",%{public}d],current[%{public}" PRId64 ",%{public}d]", ret,
327 frontTime, static_cast<int32_t>(frontInfo.event), currentTime, static_cast<int32_t>(currentInfo.event));
328 }
329 }
330
Show(napi_env env,napi_callback_info info)331 napi_value JsPanel::Show(napi_env env, napi_callback_info info)
332 {
333 InputMethodSyncTrace tracer("JsPanel_Show");
334 auto ctxt = std::make_shared<PanelContentContext>(env, info);
335 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
336 ctxt->info = { std::chrono::system_clock::now(), JsEvent::SHOW };
337 jsQueue_.Push(ctxt->info);
338 return napi_ok;
339 };
340 auto exec = [ctxt](AsyncCall::Context *ctx) {
341 jsQueue_.Wait(ctxt->info);
342 if (ctxt->inputMethodPanel == nullptr) {
343 IMSA_HILOGE("inputMethodPanel is nullptr!");
344 jsQueue_.Pop();
345 return;
346 }
347 auto code = InputMethodAbility::GetInstance()->ShowPanel(ctxt->inputMethodPanel);
348 if (code == ErrorCode::NO_ERROR) {
349 ctxt->SetState(napi_ok);
350 jsQueue_.Pop();
351 return;
352 }
353 jsQueue_.Pop();
354 ctxt->SetErrorCode(code);
355 };
356 ctxt->SetAction(std::move(input));
357 // 1 means JsAPI:show has 1 param at most.
358 AsyncCall asyncCall(env, info, ctxt, 1);
359 return asyncCall.Call(env, exec, "show");
360 }
361
Hide(napi_env env,napi_callback_info info)362 napi_value JsPanel::Hide(napi_env env, napi_callback_info info)
363 {
364 InputMethodSyncTrace tracer("JsPanel_Hide");
365 auto ctxt = std::make_shared<PanelContentContext>(env, info);
366
367 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
368 ctxt->info = { std::chrono::system_clock::now(), JsEvent::HIDE };
369 jsQueue_.Push(ctxt->info);
370 return napi_ok;
371 };
372 auto exec = [ctxt](AsyncCall::Context *ctx) {
373 jsQueue_.Wait(ctxt->info);
374 if (ctxt->inputMethodPanel == nullptr) {
375 IMSA_HILOGE("inputMethodPanel is nullptr!");
376 jsQueue_.Pop();
377 return;
378 }
379 auto code = InputMethodAbility::GetInstance()->HidePanel(ctxt->inputMethodPanel);
380 jsQueue_.Pop();
381 if (code == ErrorCode::NO_ERROR) {
382 ctxt->SetState(napi_ok);
383 return;
384 }
385 ctxt->SetErrorCode(code);
386 };
387 ctxt->SetAction(std::move(input));
388 // 1 means JsAPI:hide has 1 param at most.
389 AsyncCall asyncCall(env, info, ctxt, 1);
390 return asyncCall.Call(env, exec, "panel.hide");
391 }
392
ChangeFlag(napi_env env,napi_callback_info info)393 napi_value JsPanel::ChangeFlag(napi_env env, napi_callback_info info)
394 {
395 size_t argc = ARGC_MAX;
396 napi_value argv[ARGC_MAX] = { nullptr };
397 napi_value thisVar = nullptr;
398 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
399 PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required!", TYPE_NONE, nullptr);
400 int32_t panelFlag = 0;
401 // 0 means the first param flag<PanelFlag>
402 napi_status status = JsUtils::GetValue(env, argv[0], panelFlag);
403 PARAM_CHECK_RETURN(env, status == napi_ok, "flag type must be PanelFlag!", TYPE_NONE, nullptr);
404 auto inputMethodPanel = UnwrapPanel(env, thisVar);
405 if (inputMethodPanel == nullptr) {
406 IMSA_HILOGE("inputMethodPanel is nullptr!");
407 return nullptr;
408 }
409 PARAM_CHECK_RETURN(env,
410 (panelFlag == PanelFlag::FLG_FIXED || panelFlag == PanelFlag::FLG_FLOATING ||
411 panelFlag == PanelFlag::FLG_CANDIDATE_COLUMN),
412 "flag type must be one of PanelFlag!", TYPE_NONE, nullptr);
413 auto ret = inputMethodPanel->ChangePanelFlag(PanelFlag(panelFlag));
414 CHECK_RETURN(ret == ErrorCode::NO_ERROR, "failed to ChangePanelFlag!", nullptr);
415 return nullptr;
416 }
417
SetPrivacyMode(napi_env env,napi_callback_info info)418 napi_value JsPanel::SetPrivacyMode(napi_env env, napi_callback_info info)
419 {
420 size_t argc = ARGC_MAX;
421 napi_value argv[ARGC_MAX] = { nullptr };
422 napi_value thisVar = nullptr;
423 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
424 PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required!", TYPE_NONE, nullptr);
425 bool isPrivacyMode = false;
426 // 0 means the first param isPrivacyMode<boolean>
427 napi_status status = JsUtils::GetValue(env, argv[0], isPrivacyMode);
428 PARAM_CHECK_RETURN(env, status == napi_ok, "isPrivacyMode type must be boolean!", TYPE_NONE, nullptr);
429 CHECK_RETURN(status == napi_ok, "failed to get isPrivacyMode!", nullptr);
430 auto inputMethodPanel = UnwrapPanel(env, thisVar);
431 if (inputMethodPanel == nullptr) {
432 IMSA_HILOGE("inputMethodPanel is nullptr!");
433 return nullptr;
434 }
435 auto ret = inputMethodPanel->SetPrivacyMode(isPrivacyMode);
436 if (ret == static_cast<int32_t>(WMError::WM_ERROR_INVALID_PERMISSION)) {
437 JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_STATUS_PERMISSION_DENIED),
438 " ohos.permission.PRIVACY_WINDOW permission denied", TYPE_NONE);
439 }
440 CHECK_RETURN(ret == ErrorCode::NO_ERROR, "failed to SetPrivacyMode!", nullptr);
441 return nullptr;
442 }
443
Subscribe(napi_env env,napi_callback_info info)444 napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info)
445 {
446 IMSA_HILOGD("JsPanel start.");
447 size_t argc = ARGC_MAX;
448 napi_value argv[ARGC_MAX] = { nullptr };
449 napi_value thisVar = nullptr;
450 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
451 std::string type;
452 // 2 means least param num.
453 if (argc < 2 || !JsUtil::GetValue(env, argv[0], type) ||
454 !EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type) ||
455 JsUtil::GetType(env, argv[1]) != napi_function) {
456 IMSA_HILOGE("subscribe failed, type: %{public}s!", type.c_str());
457 return nullptr;
458 }
459 IMSA_HILOGD("subscribe type: %{public}s.", type.c_str());
460 if (type == "sizeUpdate") {
461 RESULT_CHECK_RETURN(env, InputMethodAbility::GetInstance()->IsSystemApp(), EXCEPTION_SYSTEM_PERMISSION, "",
462 TYPE_NONE, nullptr);
463 }
464 std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
465 auto inputMethodPanel = UnwrapPanel(env, thisVar);
466 if (inputMethodPanel == nullptr) {
467 IMSA_HILOGE("inputMethodPanel is nullptr!");
468 return nullptr;
469 }
470 // 1 means the second param callback.
471 std::shared_ptr<JSCallbackObject> cbObject =
472 std::make_shared<JSCallbackObject>(env, argv[1], std::this_thread::get_id(),
473 AppExecFwk::EventHandler::Current());
474 observer->Subscribe(inputMethodPanel->windowId_, type, cbObject);
475 bool ret = inputMethodPanel->SetPanelStatusListener(observer, type);
476 if (!ret) {
477 IMSA_HILOGE("failed to subscribe %{public}s!", type.c_str());
478 observer->RemoveInfo(type, inputMethodPanel->windowId_);
479 }
480 napi_value result = nullptr;
481 napi_get_undefined(env, &result);
482 return result;
483 }
484
UnSubscribe(napi_env env,napi_callback_info info)485 napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info)
486 {
487 size_t argc = ARGC_MAX;
488 napi_value argv[ARGC_MAX] = { nullptr };
489 napi_value thisVar = nullptr;
490 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
491 std::string type;
492 // 1 means least param num.
493 PARAM_CHECK_RETURN(env, argc >= 1, "at least one parameter is required!", TYPE_NONE, nullptr);
494 PARAM_CHECK_RETURN(env, JsUtil::GetValue(env, argv[0], type), "type must be string!", TYPE_NONE, nullptr);
495 PARAM_CHECK_RETURN(env, EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type),
496 "type should be show/hide/sizeChange!", TYPE_NONE, nullptr);
497 // if the second param is not napi_function/napi_null/napi_undefined, return
498 auto paramType = JsUtil::GetType(env, argv[1]);
499 PARAM_CHECK_RETURN(env, (paramType == napi_function || paramType == napi_null || paramType == napi_undefined),
500 "callback should be function or null or undefined!", TYPE_NONE, nullptr);
501 // if the second param is napi_function, delete it, else delete all
502 argv[1] = paramType == napi_function ? argv[1] : nullptr;
503
504 IMSA_HILOGD("unsubscribe type: %{public}s.", type.c_str());
505 std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
506 auto inputMethodPanel = UnwrapPanel(env, thisVar);
507 if (inputMethodPanel == nullptr) {
508 IMSA_HILOGE("inputMethodPanel is nullptr!");
509 return nullptr;
510 }
511 observer->RemoveInfo(type, inputMethodPanel->windowId_);
512 inputMethodPanel->ClearPanelListener(type);
513 napi_value result = nullptr;
514 napi_get_null(env, &result);
515 return result;
516 }
517
IsEnhancedAdjust(napi_env env,napi_value * argv)518 bool JsPanel::IsEnhancedAdjust(napi_env env, napi_value *argv)
519 {
520 PARAM_CHECK_RETURN(
521 env, JsUtil::GetType(env, argv[1]) == napi_object, "param rect type must be PanelRect", TYPE_NONE, false);
522 std::vector<const char *> properties = { "landscapeAvoidY", "portraitAvoidY", LANDSCAPE_REGION_PARAM_NAME,
523 PORTRAIT_REGION_PARAM_NAME, "fullScreenMode" };
524 return std::any_of(properties.begin(), properties.end(), [env, argv](const char *prop) {
525 bool hasProperty = false;
526 napi_has_named_property(env, argv[1], prop, &hasProperty);
527 return hasProperty;
528 });
529 }
530
CheckParam(napi_env env,size_t argc,napi_value * argv,std::shared_ptr<PanelContentContext> ctxt)531 napi_status JsPanel::CheckParam(napi_env env, size_t argc, napi_value *argv, std::shared_ptr<PanelContentContext> ctxt)
532 {
533 ctxt->isEnhancedCall = false;
534 CHECK_RETURN(ParsePanelFlag(env, argv, ctxt->panelFlag, false) == napi_ok, "parse flag", napi_generic_failure);
535 // 1 means the second param rect
536 PARAM_CHECK_RETURN(env, JsUtil::GetType(env, argv[1]) == napi_object, "param rect type must be PanelRect",
537 TYPE_NONE, napi_generic_failure);
538 PARAM_CHECK_RETURN(env, JsPanelRect::Read(env, argv[1], ctxt->layoutParams), "js param rect covert failed",
539 TYPE_NONE, napi_generic_failure);
540 return napi_ok;
541 }
542
CheckEnhancedParam(napi_env env,size_t argc,napi_value * argv,std::shared_ptr<PanelContentContext> ctxt)543 napi_status JsPanel::CheckEnhancedParam(
544 napi_env env, size_t argc, napi_value *argv, std::shared_ptr<PanelContentContext> ctxt)
545 {
546 ctxt->isEnhancedCall = true;
547 CHECK_RETURN(ParsePanelFlag(env, argv, ctxt->panelFlag, true) == napi_ok, "parse flag", napi_generic_failure);
548 // 1 means the second param rect
549 PARAM_CHECK_RETURN(env, JsUtil::GetType(env, argv[1]) == napi_object, "param rect type must be PanelRect",
550 TYPE_NONE, napi_generic_failure);
551 CHECK_RETURN(
552 JsEnhancedPanelRect::Read(env, argv[1], ctxt->enhancedLayoutParams), "read param", napi_generic_failure);
553 if (JsUtil::HasProperty(env, argv[1], LANDSCAPE_REGION_PARAM_NAME)) {
554 napi_value jsObject = nullptr;
555 napi_get_named_property(env, argv[1], LANDSCAPE_REGION_PARAM_NAME, &jsObject);
556 CHECK_RETURN(JsHotArea::Read(env, jsObject, ctxt->hotAreas.landscape.keyboardHotArea), "read landscape region",
557 napi_generic_failure);
558 } else {
559 ctxt->hotAreas.landscape.keyboardHotArea.clear();
560 }
561 if (JsUtil::HasProperty(env, argv[1], PORTRAIT_REGION_PARAM_NAME)) {
562 napi_value jsObject = nullptr;
563 napi_get_named_property(env, argv[1], PORTRAIT_REGION_PARAM_NAME, &jsObject);
564 CHECK_RETURN(JsHotArea::Read(env, jsObject, ctxt->hotAreas.portrait.keyboardHotArea), "read portrait region",
565 napi_generic_failure);
566 } else {
567 ctxt->hotAreas.portrait.keyboardHotArea.clear();
568 }
569 int32_t ret = ctxt->inputMethodPanel->IsEnhancedParamValid(ctxt->panelFlag, ctxt->enhancedLayoutParams);
570 if (ret == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
571 RESULT_CHECK_RETURN(env, false, JsUtils::Convert(ret),
572 "width limit:[0, displayWidth], avoidHeight limit:[0, 70 percent of displayHeight]", TYPE_NONE,
573 napi_generic_failure);
574 } else if (ret == ErrorCode::ERROR_INVALID_PANEL_TYPE) {
575 RESULT_CHECK_RETURN(
576 env, false, JsUtils::Convert(ret), "only used for SOFT_KEYBOARD panel", TYPE_NONE, napi_generic_failure);
577 } else if (ret != ErrorCode::NO_ERROR) {
578 RESULT_CHECK_RETURN(env, false, JsUtils::Convert(ret), "adjust failed", TYPE_NONE, napi_generic_failure);
579 }
580 return napi_ok;
581 }
582
IsPanelFlagValid(napi_env env,PanelFlag panelFlag,bool isEnhancedCalled)583 bool JsPanel::IsPanelFlagValid(napi_env env, PanelFlag panelFlag, bool isEnhancedCalled)
584 {
585 bool isValid = false;
586 if (InputMethodAbility::GetInstance()->IsDefaultIme()) {
587 isValid = panelFlag == FLG_FIXED || panelFlag == FLG_FLOATING || panelFlag == FLG_CANDIDATE_COLUMN;
588 } else {
589 isValid = panelFlag == FLG_FIXED || panelFlag == FLG_FLOATING;
590 }
591 IMSA_HILOGI("flag: %{public}d, isEnhanced: %{public}d, isValid: %{public}d", panelFlag, isEnhancedCalled, isValid);
592 if (!isEnhancedCalled) {
593 PARAM_CHECK_RETURN(env, isValid, "param flag type should be FLG_FIXED or FLG_FLOATING", TYPE_NONE, false);
594 } else {
595 RESULT_CHECK_RETURN(env, isValid, JsUtils::Convert(ErrorCode::ERROR_INVALID_PANEL_FLAG),
596 "param flag should be FIXED or FLOATING", TYPE_NONE, false);
597 }
598 return true;
599 }
600
ParsePanelFlag(napi_env env,napi_value * argv,PanelFlag & panelFlag,bool isEnhancedCalled)601 napi_status JsPanel::ParsePanelFlag(napi_env env, napi_value *argv, PanelFlag &panelFlag, bool isEnhancedCalled)
602 {
603 int32_t flag = 0;
604 // 0 means the first param flag
605 PARAM_CHECK_RETURN(env, JsUtil::GetType(env, argv[0]) == napi_number, "flag", TYPE_NUMBER, napi_generic_failure);
606 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], flag) == napi_ok, "js param flag covert failed", TYPE_NONE,
607 napi_generic_failure);
608 panelFlag = PanelFlag(flag);
609 CHECK_RETURN(IsPanelFlagValid(env, panelFlag, isEnhancedCalled), "invalid panelFlag", napi_generic_failure);
610 return napi_ok;
611 }
612
AdjustLayoutParam(std::shared_ptr<PanelContentContext> ctxt)613 void JsPanel::AdjustLayoutParam(std::shared_ptr<PanelContentContext> ctxt)
614 {
615 int32_t ret = ctxt->inputMethodPanel->AdjustPanelRect(ctxt->panelFlag, ctxt->layoutParams);
616 if (ret == ErrorCode::NO_ERROR) {
617 ctxt->SetState(napi_ok);
618 return;
619 } else if (ret == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
620 ctxt->SetErrorMessage("width limit:[0, displayWidth], height limit:[0, 70 percent of displayHeight]!");
621 }
622 ctxt->SetErrorCode(ret);
623 }
624
AdjustEnhancedLayoutParam(std::shared_ptr<PanelContentContext> ctxt)625 void JsPanel::AdjustEnhancedLayoutParam(std::shared_ptr<PanelContentContext> ctxt)
626 {
627 int32_t ret = ctxt->inputMethodPanel->AdjustPanelRect(ctxt->panelFlag, ctxt->enhancedLayoutParams, ctxt->hotAreas);
628 if (ret == ErrorCode::NO_ERROR) {
629 ctxt->SetState(napi_ok);
630 return;
631 } else if (ret == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
632 ctxt->SetErrorMessage("width limit:[0, displayWidth], avoidHeight limit:[0, 70 percent of displayHeight]");
633 } else if (ret == ErrorCode::ERROR_INVALID_PANEL_TYPE) {
634 ctxt->SetErrorMessage("only used for SOFT_KEYBOARD panel");
635 }
636 ctxt->SetErrorCode(ret);
637 }
638
AdjustPanelRect(napi_env env,napi_callback_info info)639 napi_value JsPanel::AdjustPanelRect(napi_env env, napi_callback_info info)
640 {
641 auto ctxt = std::make_shared<PanelContentContext>(env, info);
642 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
643 PARAM_CHECK_RETURN(env, argc > 1, "at least two parameters is required", TYPE_NONE, napi_generic_failure);
644 napi_status status = napi_generic_failure;
645 if (!IsEnhancedAdjust(env, argv)) {
646 CHECK_RETURN(CheckParam(env, argc, argv, ctxt) == napi_ok, "check param", napi_generic_failure);
647 } else {
648 CHECK_RETURN(CheckEnhancedParam(env, argc, argv, ctxt) == napi_ok, "check param", napi_generic_failure);
649 }
650 ctxt->info = { std::chrono::system_clock::now(), JsEvent::ADJUST_PANEL_RECT };
651 jsQueue_.Push(ctxt->info);
652 return napi_ok;
653 };
654
655 auto exec = [ctxt](AsyncCall::Context *ctx) {
656 jsQueue_.Wait(ctxt->info);
657 if (ctxt->inputMethodPanel == nullptr) {
658 IMSA_HILOGE("inputMethodPanel_ is nullptr!");
659 jsQueue_.Pop();
660 return;
661 }
662 if (!ctxt->isEnhancedCall) {
663 AdjustLayoutParam(ctxt);
664 } else {
665 AdjustEnhancedLayoutParam(ctxt);
666 }
667 jsQueue_.Pop();
668 };
669 ctxt->SetAction(std::move(input));
670 // 2 means JsAPI:adjustPanelRect has 2 params at most
671 AsyncCall asyncCall(env, info, ctxt, 2);
672 return asyncCall.Call(env, exec, "adjustPanelRect");
673 }
674
UpdateRegion(napi_env env,napi_callback_info info)675 napi_value JsPanel::UpdateRegion(napi_env env, napi_callback_info info)
676 {
677 auto ctxt = std::make_shared<PanelContentContext>(env, info);
678 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
679 PARAM_CHECK_RETURN(env, ctxt->inputMethodPanel != nullptr, "panel is null", TYPE_NONE, napi_generic_failure);
680 RESULT_CHECK_RETURN(env, ctxt->inputMethodPanel->GetPanelType() == PanelType::SOFT_KEYBOARD,
681 JsUtils::Convert(ErrorCode::ERROR_INVALID_PANEL_TYPE), "only used for SOFT_KEYBOARD panel", TYPE_NONE,
682 napi_generic_failure);
683 CHECK_RETURN(IsPanelFlagValid(env, ctxt->inputMethodPanel->GetPanelFlag(), true), "invalid panelFlag",
684 napi_generic_failure);
685 PARAM_CHECK_RETURN(env, argc > 0, "at least one parameters is required", TYPE_NONE, napi_generic_failure);
686 PARAM_CHECK_RETURN(env, JsHotArea::Read(env, argv[0], ctxt->hotArea), "failed to convert inputRegion",
687 TYPE_NONE, napi_generic_failure);
688 ctxt->info = { std::chrono::system_clock::now(), JsEvent::UPDATE_REGION };
689 jsQueue_.Push(ctxt->info);
690 return napi_ok;
691 };
692 auto exec = [ctxt](AsyncCall::Context *ctx) {
693 jsQueue_.Wait(ctxt->info);
694 if (ctxt->inputMethodPanel == nullptr) {
695 IMSA_HILOGE("inputMethodPanel_ is nullptr!");
696 jsQueue_.Pop();
697 return;
698 }
699 int32_t code = ctxt->inputMethodPanel->UpdateRegion(ctxt->hotArea);
700 if (code == ErrorCode::NO_ERROR) {
701 ctxt->SetState(napi_ok);
702 return;
703 } else if (code == ErrorCode::ERROR_INVALID_PANEL_TYPE) {
704 ctxt->SetErrorMessage("only used for SOFT_KEYBOARD panel");
705 } else if (code == ErrorCode::ERROR_INVALID_PANEL_FLAG) {
706 ctxt->SetErrorMessage("only used for fixed or floating panel");
707 }
708 ctxt->SetErrorCode(code);
709 jsQueue_.Pop();
710 };
711 ctxt->SetAction(std::move(input));
712 // 1 means JsAPI:updateRegion has 1 params at most
713 AsyncCall asyncCall(env, info, ctxt, 1);
714 return asyncCall.Call(env, exec, "updateRegion");
715 }
716
UnwrapPanel(napi_env env,napi_value thisVar)717 std::shared_ptr<InputMethodPanel> JsPanel::UnwrapPanel(napi_env env, napi_value thisVar)
718 {
719 void *native = nullptr;
720 napi_status status = napi_unwrap(env, thisVar, &native);
721 CHECK_RETURN((status == napi_ok && native != nullptr), "failed to unwrap!", nullptr);
722 auto jsPanel = reinterpret_cast<JsPanel *>(native);
723 if (jsPanel == nullptr) {
724 return nullptr;
725 }
726 auto inputMethodPanel = jsPanel->GetNative();
727 CHECK_RETURN(inputMethodPanel != nullptr, "inputMethodPanel is nullptr", nullptr);
728 return inputMethodPanel;
729 }
730
Write(napi_env env,const LayoutParams & layoutParams)731 napi_value JsPanelRect::Write(napi_env env, const LayoutParams &layoutParams)
732 {
733 napi_value jsObject = nullptr;
734 napi_create_object(env, &jsObject);
735 bool ret =
736 JsUtil::Object::WriteProperty(env, jsObject, "landscapeRect", JsRect::Write(env, layoutParams.landscapeRect));
737 ret = ret &&
738 JsUtil::Object::WriteProperty(env, jsObject, "portraitRect", JsRect::Write(env, layoutParams.portraitRect));
739 return ret ? jsObject : JsUtil::Const::Null(env);
740 }
Read(napi_env env,napi_value object,LayoutParams & layoutParams)741 bool JsPanelRect::Read(napi_env env, napi_value object, LayoutParams &layoutParams)
742 {
743 napi_value rectObject = nullptr;
744 napi_get_named_property(env, object, "landscapeRect", &rectObject);
745 bool ret = JsRect::Read(env, rectObject, layoutParams.landscapeRect);
746 napi_get_named_property(env, object, "portraitRect", &rectObject);
747 ret = ret && JsRect::Read(env, rectObject, layoutParams.portraitRect);
748 return ret;
749 }
750
Read(napi_env env,napi_value object,EnhancedLayoutParams & params)751 bool JsEnhancedPanelRect::Read(napi_env env, napi_value object, EnhancedLayoutParams ¶ms)
752 {
753 napi_status status = napi_generic_failure;
754 napi_value jsObject = nullptr;
755 bool ret = JsUtils::ReadOptionalProperty(
756 env, object, { napi_boolean, TYPE_BOOLEAN, "fullScreenMode" }, params.isFullScreen);
757 if (ret && params.isFullScreen) {
758 IMSA_HILOGD("full screen mode, no need to parse rect");
759 } else {
760 ret = JsUtils::ReadOptionalProperty(
761 env, object, { napi_object, TYPE_OBJECT, "landscapeRect" }, params.landscape.rect);
762 ret = ret && JsUtils::ReadOptionalProperty(
763 env, object, { napi_object, TYPE_OBJECT, "portraitRect" }, params.portrait.rect);
764 PARAM_CHECK_RETURN(env, ret, "landscapeRect and portraitRect should not be empty", TYPE_NONE, false);
765 }
766 JsUtils::ReadOptionalProperty(
767 env, object, { napi_number, TYPE_NUMBER, "landscapeAvoidY" }, params.landscape.avoidY);
768 JsUtils::ReadOptionalProperty(env, object, { napi_number, TYPE_NUMBER, "portraitAvoidY" }, params.portrait.avoidY);
769 return ret;
770 }
771
Read(napi_env env,napi_value object,std::vector<Rosen::Rect> & hotAreas)772 bool JsHotArea::Read(napi_env env, napi_value object, std::vector<Rosen::Rect> &hotAreas)
773 {
774 bool isArray = false;
775 napi_is_array(env, object, &isArray);
776 PARAM_CHECK_RETURN(env, isArray, "inputRegion", TYPE_ARRAY, false);
777 uint32_t length = 0;
778 napi_status status = napi_get_array_length(env, object, &length);
779 PARAM_CHECK_RETURN(env, status == napi_ok, "get array failed", TYPE_NONE, false);
780 PARAM_CHECK_RETURN(
781 env, (length > 0) && (length <= MAX_INPUT_REGION_LEN), "inputRegion size limit: [1, 4]", TYPE_NONE, false);
782 for (uint32_t i = 0; i < length; ++i) {
783 napi_value jsElement;
784 napi_get_element(env, object, i, &jsElement);
785 PARAM_CHECK_RETURN(
786 env, JsUtil::GetType(env, jsElement) == napi_object, "element of inputRegion", TYPE_OBJECT, false);
787 Rosen::Rect element;
788 PARAM_CHECK_RETURN(
789 env, JsRect::Read(env, jsElement, element), "failed to convert element in inputRegion", TYPE_NONE, false);
790 hotAreas.push_back(element);
791 }
792 return true;
793 }
SetImmersiveMode(napi_env env,napi_callback_info info)794 napi_value JsPanel::SetImmersiveMode(napi_env env, napi_callback_info info)
795 {
796 size_t argc = ARGC_MAX;
797 napi_value argv[ARGC_MAX] = { nullptr };
798 napi_value thisVar = nullptr;
799 napi_value retVal = JsUtil::Const::Null(env);
800 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
801 PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required", TYPE_NONE, retVal);
802 int32_t immersiveMode = 0;
803 // 0 means the first param immersiveMode<ImmersiveMode>
804 bool result = JsUtil::GetValue(env, argv[0], immersiveMode);
805 PARAM_CHECK_RETURN(env, result, "immersiveMode type must be ImmersiveMode", TYPE_NONE, retVal);
806 auto panel = UnwrapPanel(env, thisVar);
807 RESULT_CHECK_RETURN(env, panel != nullptr, JsUtils::Convert(ErrorCode::ERROR_IME), "", TYPE_NONE, retVal);
808 PARAM_CHECK_RETURN(env,
809 (immersiveMode == static_cast<int32_t>(ImmersiveMode::NONE_IMMERSIVE) ||
810 immersiveMode == static_cast<int32_t>(ImmersiveMode::LIGHT_IMMERSIVE) ||
811 immersiveMode == static_cast<int32_t>(ImmersiveMode::DARK_IMMERSIVE)),
812 "immersiveMode type must be ImmersiveMode and can not be IMMERSIVE", TYPE_NONE, retVal);
813 JsEventInfo eventInfo = { std::chrono::system_clock::now(), JsEvent::SET_IMMERSIVE_MODE };
814 jsQueue_.Push(eventInfo);
815 jsQueue_.Wait(eventInfo);
816 auto ret = panel->SetImmersiveMode(ImmersiveMode(immersiveMode));
817 jsQueue_.Pop();
818 RESULT_CHECK_RETURN(env, ret == ErrorCode::NO_ERROR, JsUtils::Convert(ret), "", TYPE_NONE, retVal);
819 return retVal;
820 }
821
GetImmersiveMode(napi_env env,napi_callback_info info)822 napi_value JsPanel::GetImmersiveMode(napi_env env, napi_callback_info info)
823 {
824 napi_value thisVar = nullptr;
825 napi_value retVal = JsUtil::Const::Null(env);
826 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
827 auto panel = UnwrapPanel(env, thisVar);
828 RESULT_CHECK_RETURN(env, panel != nullptr, JsUtils::Convert(ErrorCode::ERROR_IME), "", TYPE_NONE, retVal);
829 JsEventInfo eventInfo = { std::chrono::system_clock::now(), JsEvent::GET_IMMERSIVE_MODE };
830 jsQueue_.Push(eventInfo);
831 jsQueue_.Wait(eventInfo);
832 auto immersiveMode = panel->GetImmersiveMode();
833 jsQueue_.Pop();
834 napi_value jsImmersiveMode = nullptr;
835 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(immersiveMode), &jsImmersiveMode));
836 return jsImmersiveMode;
837 }
838 } // namespace MiscServices
839 } // namespace OHOS