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