1 /*
2 * Copyright (c) 2021-2024 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 "server_msg_handler.h"
17
18 #include <cinttypes>
19 #include "ipc_skeleton.h"
20
21 #include "ability_manager_client.h"
22 #include "anr_manager.h"
23 #include "authorization_dialog.h"
24 #include "authorize_helper.h"
25 #include "bytrace_adapter.h"
26 #include "client_death_handler.h"
27 #include "display_event_monitor.h"
28 #include "event_dump.h"
29 #include "event_interceptor_handler.h"
30 #include "event_monitor_handler.h"
31 #include "event_log_helper.h"
32 #include "hos_key_event.h"
33 #include "input_device_manager.h"
34 #include "input_event.h"
35 #include "input_event_data_transformation.h"
36 #include "input_event_handler.h"
37 #include "i_input_windows_manager.h"
38 #include "i_pointer_drawing_manager.h"
39 #include "key_event_normalize.h"
40 #include "key_event_value_transformation.h"
41 #include "key_subscriber_handler.h"
42 #include "libinput_adapter.h"
43 #include "parameters.h"
44 #include "switch_subscriber_handler.h"
45 #include "time_cost_chk.h"
46 #include "touch_drawing_manager.h"
47
48 #undef MMI_LOG_DOMAIN
49 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
50 #undef MMI_LOG_TAG
51 #define MMI_LOG_TAG "ServerMsgHandler"
52
53 namespace OHOS {
54 namespace MMI {
55 namespace {
56 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
57 constexpr int32_t SECURITY_COMPONENT_SERVICE_ID = 3050;
58 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
59 constexpr int32_t SEND_NOTICE_OVERTIME { 5 };
60 constexpr int32_t DEFAULT_POINTER_ID { 10000 };
61 const int32_t ROTATE_POLICY = system::GetIntParameter("const.window.device.rotate_policy", 0);
62 const std::string PRODUCT_TYPE = system::GetParameter("const.product.devicetype", "unknown");
63 const std::string PRODUCT_TYPE_PC = "2in1";
64 constexpr int32_t WINDOW_ROTATE { 0 };
65 constexpr int32_t COMMON_PERMISSION_CHECK_ERROR { 201 };
66 } // namespace
67
Init(UDSServer & udsServer)68 void ServerMsgHandler::Init(UDSServer &udsServer)
69 {
70 udsServer_ = &udsServer;
71 MsgCallback funs[] = {
72 {MmiMessageId::DISPLAY_INFO, [this] (SessionPtr sess, NetPacket &pkt) {
73 return this->OnDisplayInfo(sess, pkt); }},
74 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
75 {MmiMessageId::WINDOW_AREA_INFO, [this] (SessionPtr sess, NetPacket &pkt) {
76 return this->OnWindowAreaInfo(sess, pkt); }},
77 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
78 {MmiMessageId::WINDOW_INFO, [this] (SessionPtr sess, NetPacket &pkt) {
79 return this->OnWindowGroupInfo(sess, pkt); }},
80 {MmiMessageId::WINDOW_STATE_ERROR_CALLBACK, [this] (SessionPtr sess, NetPacket &pkt) {
81 return this->RegisterWindowStateErrorCallback(sess, pkt); }},
82 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
83 {MmiMessageId::SCINFO_CONFIG, [this] (SessionPtr sess, NetPacket &pkt) {
84 return this->OnEnhanceConfig(sess, pkt); }},
85 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
86
87 };
88 for (auto &it : funs) {
89 if (!RegistrationEvent(it)) {
90 MMI_HILOGW("Failed to register event errCode:%{public}d", EVENT_REG_FAIL);
91 continue;
92 }
93 }
94 AUTHORIZE_HELPER->Init(clientDeathHandler_);
95 }
96
OnMsgHandler(SessionPtr sess,NetPacket & pkt)97 void ServerMsgHandler::OnMsgHandler(SessionPtr sess, NetPacket& pkt)
98 {
99 CHKPV(sess);
100 auto id = pkt.GetMsgId();
101 TimeCostChk chk("ServerMsgHandler::OnMsgHandler", "overtime 300(us)", MAX_OVER_TIME, id);
102 BytraceAdapter::StartSocketHandle(static_cast<int32_t>(id));
103 auto callback = GetMsgCallback(id);
104 if (callback == nullptr) {
105 MMI_HILOGE("Unknown msg id:%{public}d,errCode:%{public}d", id, UNKNOWN_MSG_ID);
106 return;
107 }
108 auto ret = (*callback)(sess, pkt);
109 BytraceAdapter::StopSocketHandle();
110 if (ret < 0) {
111 MMI_HILOGE("Msg handling failed. id:%{public}d,errCode:%{public}d", id, ret);
112 }
113 }
114
115 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent,int32_t pid,bool isNativeInject)116 int32_t ServerMsgHandler::OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject)
117 {
118 CALL_DEBUG_ENTER;
119 CHKPR(keyEvent, ERROR_NULL_POINTER);
120 LogTracer lt(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
121 if (isNativeInject) {
122 if (PRODUCT_TYPE != PRODUCT_TYPE_PC) {
123 MMI_HILOGW("Current device has no permission");
124 return COMMON_PERMISSION_CHECK_ERROR;
125 }
126 bool screenLocked = DISPLAY_MONITOR->GetScreenLocked();
127 if (screenLocked) {
128 MMI_HILOGW("Screen locked, no permission");
129 return COMMON_PERMISSION_CHECK_ERROR;
130 }
131 auto iter = authorizationCollection_.find(pid);
132 if ((iter == authorizationCollection_.end()) || (iter->second == AuthorizationStatus::UNAUTHORIZED)) {
133 auto state = AUTHORIZE_HELPER->GetAuthorizeState();
134 if (state != AuthorizeState::STATE_UNAUTHORIZE) {
135 MMI_HILOGI("The process is already being processed,s:%{public}d,pid:%{public}d,inputPid:%{public}d",
136 state, AUTHORIZE_HELPER->GetAuthorizePid(), pid);
137 return COMMON_PERMISSION_CHECK_ERROR;
138 }
139 CurrentPID_ = pid;
140 InjectionType_ = InjectionType::KEYEVENT;
141 keyEvent_ = keyEvent;
142 LaunchAbility();
143 AuthorizeExitCallback fnCallback = [&] (int32_t pid) {
144 MMI_HILOGI("User not authorized to inject pid:%{public}d", pid);
145 };
146 AUTHORIZE_HELPER->AddAuthorizeProcess(CurrentPID_, fnCallback);
147 return COMMON_PERMISSION_CHECK_ERROR;
148 }
149 CurrentPID_ = pid;
150 if (iter->second == AuthorizationStatus::UNAUTHORIZED) {
151 return COMMON_PERMISSION_CHECK_ERROR;
152 }
153 }
154 int32_t keyIntention = KeyItemsTransKeyIntention(keyEvent->GetKeyItems());
155 keyEvent->SetKeyIntention(keyIntention);
156 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
157 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
158 inputEventNormalizeHandler->HandleKeyEvent(keyEvent);
159 if (EventLogHelper::IsBetaVersion() && !keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
160 MMI_HILOGD("Inject keyCode:%{private}d, action:%{public}d", keyEvent->GetKeyCode(), keyEvent->GetKeyAction());
161 } else {
162 MMI_HILOGD("Inject keyCode:%{private}d, action:%{public}d", keyEvent->GetKeyCode(), keyEvent->GetKeyAction());
163 }
164 return RET_OK;
165 }
166
OnGetFunctionKeyState(int32_t funcKey,bool & state)167 int32_t ServerMsgHandler::OnGetFunctionKeyState(int32_t funcKey, bool &state)
168 {
169 CALL_INFO_TRACE;
170 const auto &keyEvent = KeyEventHdr->GetKeyEvent();
171 CHKPR(keyEvent, ERROR_NULL_POINTER);
172 state = keyEvent->GetFunctionKey(funcKey);
173 MMI_HILOGD("Get the function key:%{public}d status as %{public}s", funcKey, state ? "open" : "close");
174 return RET_OK;
175 }
176
OnSetFunctionKeyState(int32_t funcKey,bool enable)177 int32_t ServerMsgHandler::OnSetFunctionKeyState(int32_t funcKey, bool enable)
178 {
179 CALL_INFO_TRACE;
180 auto device = INPUT_DEV_MGR->GetKeyboardDevice();
181 CHKPR(device, ERROR_NULL_POINTER);
182 if (LibinputAdapter::DeviceLedUpdate(device, funcKey, enable) != RET_OK) {
183 MMI_HILOGE("Failed to set the keyboard led");
184 return RET_ERR;
185 }
186 int32_t state = libinput_get_funckey_state(device, funcKey);
187
188 auto keyEvent = KeyEventHdr->GetKeyEvent();
189 CHKPR(keyEvent, ERROR_NULL_POINTER);
190 int32_t ret = keyEvent->SetFunctionKey(funcKey, state);
191 if (ret != funcKey) {
192 MMI_HILOGE("Failed to enable the function key");
193 return RET_ERR;
194 }
195 MMI_HILOGD("Update function key:%{public}d succeed", funcKey);
196 return RET_OK;
197 }
198 #endif // OHOS_BUILD_ENABLE_KEYBOARD
199
200 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,int32_t pid,bool isNativeInject,bool isShell)201 int32_t ServerMsgHandler::OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
202 bool isNativeInject, bool isShell)
203 {
204 CALL_DEBUG_ENTER;
205 CHKPR(pointerEvent, ERROR_NULL_POINTER);
206 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
207 if (isNativeInject) {
208 if (PRODUCT_TYPE != PRODUCT_TYPE_PC) {
209 MMI_HILOGW("Current device has no permission");
210 return COMMON_PERMISSION_CHECK_ERROR;
211 }
212 bool screenLocked = DISPLAY_MONITOR->GetScreenLocked();
213 if (screenLocked) {
214 MMI_HILOGW("Screen locked, no permission");
215 return COMMON_PERMISSION_CHECK_ERROR;
216 }
217 auto iter = authorizationCollection_.find(pid);
218 if ((iter == authorizationCollection_.end()) || (iter->second == AuthorizationStatus::UNAUTHORIZED)) {
219 auto state = AUTHORIZE_HELPER->GetAuthorizeState();
220 if (state != AuthorizeState::STATE_UNAUTHORIZE) {
221 MMI_HILOGI("The process is already being processed,s:%{public}d,pid:%{public}d,inputPid:%{public}d",
222 state, AUTHORIZE_HELPER->GetAuthorizePid(), pid);
223 return COMMON_PERMISSION_CHECK_ERROR;
224 }
225 CurrentPID_ = pid;
226 InjectionType_ = InjectionType::POINTEREVENT;
227 pointerEvent_ = pointerEvent;
228 LaunchAbility();
229 AuthorizeExitCallback fnCallback = [&] (int32_t pid) {
230 MMI_HILOGI("User not authorized to inject pid:%{public}d", pid);
231 };
232 AUTHORIZE_HELPER->AddAuthorizeProcess(CurrentPID_, fnCallback);
233 return COMMON_PERMISSION_CHECK_ERROR;
234 }
235 CurrentPID_ = pid;
236 if (iter->second == AuthorizationStatus::UNAUTHORIZED) {
237 return COMMON_PERMISSION_CHECK_ERROR;
238 }
239 }
240 return OnInjectPointerEventExt(pointerEvent, isShell);
241 }
242
IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent)243 bool ServerMsgHandler::IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent)
244 {
245 return pointerEvent->GetZOrder() > 0;
246 }
247
OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent,bool isShell)248 int32_t ServerMsgHandler::OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, bool isShell)
249 {
250 CALL_DEBUG_ENTER;
251 CHKPR(pointerEvent, ERROR_NULL_POINTER);
252 EndLogTraceId(pointerEvent->GetId());
253 pointerEvent->UpdateId();
254 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
255 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
256 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
257 switch (pointerEvent->GetSourceType()) {
258 case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
259 #ifdef OHOS_BUILD_ENABLE_TOUCH
260 if (isShell) {
261 pointerEvent->AddFlag(InputEvent::EVENT_FLAG_SHELL);
262 }
263 if (!FixTargetWindowId(pointerEvent, pointerEvent->GetPointerAction(), isShell)) {
264 return RET_ERR;
265 }
266 inputEventNormalizeHandler->HandleTouchEvent(pointerEvent);
267 if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) &&
268 !IsNavigationWindowInjectEvent(pointerEvent)) {
269 TOUCH_DRAWING_MGR->TouchDrawHandler(pointerEvent);
270 }
271 #endif // OHOS_BUILD_ENABLE_TOUCH
272 break;
273 }
274 case PointerEvent::SOURCE_TYPE_MOUSE:
275 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
276 case PointerEvent::SOURCE_TYPE_JOYSTICK:
277 #endif // OHOS_BUILD_ENABLE_JOYSTICK
278 case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
279 #ifdef OHOS_BUILD_ENABLE_POINTER
280 int32_t ret = AccelerateMotion(pointerEvent);
281 if (ret != RET_OK) {
282 MMI_HILOGE("Failed to accelerate motion, error:%{public}d", ret);
283 return ret;
284 }
285 UpdatePointerEvent(pointerEvent);
286 inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
287 CHKPR(pointerEvent, ERROR_NULL_POINTER);
288 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
289 break;
290 } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER)) {
291 IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), false, 0, false);
292 } else if (((pointerEvent->GetPointerAction() < PointerEvent::POINTER_ACTION_PULL_DOWN) ||
293 (pointerEvent->GetPointerAction() > PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW)) &&
294 !IPointerDrawingManager::GetInstance()->IsPointerVisible()) {
295 IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), true, 0, false);
296 }
297 #endif // OHOS_BUILD_ENABLE_POINTER
298 break;
299 }
300 default: {
301 MMI_HILOGW("Source type is unknown, source:%{public}d", pointerEvent->GetSourceType());
302 break;
303 }
304 }
305 return SaveTargetWindowId(pointerEvent, isShell);
306 }
307 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
308
309 #ifdef OHOS_BUILD_ENABLE_POINTER
AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent)310 int32_t ServerMsgHandler::AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent)
311 {
312 if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_RAW_POINTER_MOVEMENT) ||
313 (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE) ||
314 ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE) &&
315 (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE))) {
316 return RET_OK;
317 }
318 PointerEvent::PointerItem pointerItem {};
319 if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
320 MMI_HILOGE("Pointer event is corrupted");
321 return RET_ERR;
322 }
323 CursorPosition cursorPos = WIN_MGR->GetCursorPos();
324 if (cursorPos.displayId < 0) {
325 MMI_HILOGE("No display");
326 return RET_ERR;
327 }
328 Offset offset {
329 .dx = pointerItem.GetRawDx(),
330 .dy = pointerItem.GetRawDy(),
331 };
332 auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos.displayId);
333 CHKPR(displayInfo, ERROR_NULL_POINTER);
334 #ifndef OHOS_BUILD_EMULATOR
335 if (TOUCH_DRAWING_MGR->IsWindowRotation()) {
336 CalculateOffset(displayInfo->direction, offset);
337 }
338 #endif // OHOS_BUILD_EMULATOR
339 int32_t ret = RET_OK;
340
341 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_TOUCHPAD_POINTER)) {
342 ret = HandleMotionAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(),
343 &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
344 MouseTransformProcessor::GetTouchpadSpeed(), static_cast<int32_t>(DeviceType::DEVICE_PC));
345 } else {
346 ret = HandleMotionAccelerateMouse(&offset, WIN_MGR->GetMouseIsCaptureMode(),
347 &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
348 MouseTransformProcessor::GetPointerSpeed(), static_cast<int32_t>(DeviceType::DEVICE_PC));
349 }
350 if (ret != RET_OK) {
351 MMI_HILOGE("Failed to accelerate pointer motion, error:%{public}d", ret);
352 return ret;
353 }
354 WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
355 if (EventLogHelper::IsBetaVersion() && !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
356 MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
357 cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
358 } else {
359 MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
360 cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
361 }
362 return RET_OK;
363 }
364
CalculateOffset(Direction direction,Offset & offset)365 void ServerMsgHandler::CalculateOffset(Direction direction, Offset &offset)
366 {
367 std::negate<double> neg;
368 if (direction == DIRECTION90) {
369 double tmp = offset.dx;
370 offset.dx = offset.dy;
371 offset.dy = neg(tmp);
372 } else if (direction == DIRECTION180) {
373 offset.dx = neg(offset.dx);
374 offset.dy = neg(offset.dy);
375 } else if (direction == DIRECTION270) {
376 double tmp = offset.dx;
377 offset.dx = neg(offset.dy);
378 offset.dy = tmp;
379 }
380 }
381 #endif // OHOS_BUILD_ENABLE_POINTER
382
383 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent)384 void ServerMsgHandler::UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
385 {
386 if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_RAW_POINTER_MOVEMENT) ||
387 (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE)) {
388 return;
389 }
390 PointerEvent::PointerItem pointerItem {};
391 if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
392 MMI_HILOGE("Pointer event is corrupted");
393 return;
394 }
395 auto mouseInfo = WIN_MGR->GetMouseInfo();
396 pointerItem.SetDisplayX(mouseInfo.physicalX);
397 pointerItem.SetDisplayY(mouseInfo.physicalY);
398 pointerEvent->UpdatePointerItem(pointerEvent->GetPointerId(), pointerItem);
399 pointerEvent->SetTargetDisplayId(mouseInfo.displayId);
400 }
401
SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,bool isShell)402 int32_t ServerMsgHandler::SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell)
403 {
404 CHKPR(pointerEvent, ERROR_NULL_POINTER);
405 if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
406 (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
407 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER)) {
408 int32_t pointerId = pointerEvent->GetPointerId();
409 PointerEvent::PointerItem pointerItem;
410 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
411 MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
412 return RET_ERR;
413 }
414 int32_t targetWindowId = pointerEvent->GetTargetWindowId();
415 if (isShell) {
416 shellTargetWindowIds_[pointerId] = targetWindowId;
417 } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
418 accessTargetWindowIds_[pointerId] = targetWindowId;
419 } else {
420 nativeTargetWindowIds_[pointerId] = targetWindowId;
421 }
422 }
423 if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
424 (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
425 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT)) {
426 int32_t pointerId = pointerEvent->GetPointerId();
427 if (isShell) {
428 shellTargetWindowIds_.erase(pointerId);
429 } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
430 accessTargetWindowIds_.erase(pointerId);
431 } else {
432 nativeTargetWindowIds_.erase(pointerId);
433 }
434 }
435 return RET_OK;
436 }
437 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
438
439 #ifdef OHOS_BUILD_ENABLE_TOUCH
FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,int32_t action,bool isShell)440 bool ServerMsgHandler::FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,
441 int32_t action, bool isShell)
442 {
443 int32_t targetWindowId = -1;
444 int32_t pointerId = pointerEvent->GetPointerId();
445 PointerEvent::PointerItem pointerItem;
446 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
447 MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
448 return false;
449 }
450 if (isShell) {
451 auto iter = shellTargetWindowIds_.find(pointerEvent->GetPointerId());
452 if (iter != shellTargetWindowIds_.end()) {
453 targetWindowId = iter->second;
454 }
455 } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
456 auto iter = accessTargetWindowIds_.find(pointerEvent->GetPointerId());
457 if (iter != accessTargetWindowIds_.end()) {
458 targetWindowId = iter->second;
459 }
460 } else {
461 pointerEvent->RemovePointerItem(pointerId);
462 pointerId += DEFAULT_POINTER_ID;
463 pointerItem.SetPointerId(pointerId);
464 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
465 pointerEvent->SetPointerId(pointerId);
466 auto iter = nativeTargetWindowIds_.find(pointerEvent->GetPointerId());
467 if (iter != nativeTargetWindowIds_.end()) {
468 targetWindowId = iter->second;
469 }
470 }
471 MMI_HILOGD("TargetWindowId:%{public}d %{public}d", pointerEvent->GetTargetWindowId(), targetWindowId);
472 if (action == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
473 action == PointerEvent::POINTER_ACTION_DOWN || targetWindowId < 0) {
474 MMI_HILOGD("Down event or targetWindowId less 0 is not need fix window id");
475 return true;
476 }
477 auto pointerIds = pointerEvent->GetPointerIds();
478 if (pointerIds.empty()) {
479 MMI_HILOGE("GetPointerIds is empty");
480 return false;
481 }
482
483 pointerEvent->SetTargetWindowId(targetWindowId);
484 pointerItem.SetTargetWindowId(targetWindowId);
485 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
486 return true;
487 }
488 #endif // OHOS_BUILD_ENABLE_TOUCH
489
OnUiExtentionWindowInfo(NetPacket & pkt,WindowInfo & info)490 int32_t ServerMsgHandler::OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info)
491 {
492 uint32_t num = 0;
493 pkt >> num;
494 if (pkt.ChkRWError()) {
495 MMI_HILOGE("Packet read display info failed");
496 return RET_ERR;
497 }
498 for (uint32_t i = 0; i < num; i++) {
499 WindowInfo extensionInfo;
500 pkt >> extensionInfo.id >> extensionInfo.pid >> extensionInfo.uid >> extensionInfo.area
501 >> extensionInfo.defaultHotAreas >> extensionInfo.pointerHotAreas >> extensionInfo.agentWindowId
502 >> extensionInfo.flags >> extensionInfo.action >> extensionInfo.displayId >> extensionInfo.zOrder
503 >> extensionInfo.pointerChangeAreas >> extensionInfo.transform >> extensionInfo.windowInputType
504 >> extensionInfo.privacyMode >> extensionInfo.windowType >> extensionInfo.privacyUIFlag
505 >> extensionInfo.rectChangeBySystem;
506 info.uiExtentionWindowInfo.push_back(extensionInfo);
507 if (pkt.ChkRWError()) {
508 MMI_HILOGE("Packet read extention window info failed");
509 return RET_ERR;
510 }
511 }
512 return RET_OK;
513 }
514
OnDisplayInfo(SessionPtr sess,NetPacket & pkt)515 int32_t ServerMsgHandler::OnDisplayInfo(SessionPtr sess, NetPacket &pkt)
516 {
517 CALL_DEBUG_ENTER;
518 CHKPR(sess, ERROR_NULL_POINTER);
519 int32_t tokenType = sess->GetTokenType();
520 if (tokenType != TokenType::TOKEN_NATIVE && tokenType != TokenType::TOKEN_SHELL &&
521 tokenType !=TokenType::TOKEN_SYSTEM_HAP) {
522 MMI_HILOGW("Not native or systemapp skip, pid:%{public}d tokenType:%{public}d", sess->GetPid(), tokenType);
523 return RET_ERR;
524 }
525 DisplayGroupInfo displayGroupInfo;
526 pkt >> displayGroupInfo.width >> displayGroupInfo.height >>
527 displayGroupInfo.focusWindowId >> displayGroupInfo.currentUserId;
528 uint32_t num = 0;
529 pkt >> num;
530 if (pkt.ChkRWError()) {
531 MMI_HILOGE("Packet read display info failed");
532 return RET_ERR;
533 }
534 for (uint32_t i = 0; i < num; i++) {
535 WindowInfo info;
536 int32_t byteCount = 0;
537 pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
538 >> info.pointerHotAreas >> info.agentWindowId >> info.flags >> info.action
539 >> info.displayId >> info.zOrder >> info.pointerChangeAreas >> info.transform
540 >> info.windowInputType >> info.privacyMode >> info.windowType >> byteCount;
541
542 OnUiExtentionWindowInfo(pkt, info);
543 pkt >> info.rectChangeBySystem;
544 displayGroupInfo.windowsInfo.push_back(info);
545 if (pkt.ChkRWError()) {
546 MMI_HILOGE("Packet read display info failed");
547 return RET_ERR;
548 }
549 }
550 pkt >> num;
551 for (uint32_t i = 0; i < num; i++) {
552 DisplayInfo info;
553 pkt >> info.id >> info.x >> info.y >> info.width >> info.height >> info.dpi >> info.name
554 >> info.uniq >> info.direction >> info.displayDirection >> info.displayMode >> info.transform;
555 displayGroupInfo.displaysInfo.push_back(info);
556 if (pkt.ChkRWError()) {
557 MMI_HILOGE("Packet read display info failed");
558 return RET_ERR;
559 }
560 }
561 if (pkt.ChkRWError()) {
562 MMI_HILOGE("Packet read display info failed");
563 return RET_ERR;
564 }
565 WIN_MGR->UpdateDisplayInfoExtIfNeed(displayGroupInfo, true);
566 return RET_OK;
567 }
568
569 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
OnWindowAreaInfo(SessionPtr sess,NetPacket & pkt)570 int32_t ServerMsgHandler::OnWindowAreaInfo(SessionPtr sess, NetPacket &pkt)
571 {
572 CALL_DEBUG_ENTER;
573 CHKPR(sess, ERROR_NULL_POINTER);
574 int32_t temp = 0;
575 int32_t pid = 0;
576 int32_t windowId = 0;
577 pkt >> temp >> pid >> windowId;
578 WindowArea area = static_cast<WindowArea>(temp);
579 if (pkt.ChkRWError()) {
580 MMI_HILOGE("Packet read display info failed");
581 return RET_ERR;
582 }
583 WIN_MGR->SetWindowPointerStyle(area, pid, windowId);
584 return RET_OK;
585 }
586 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
587
OnWindowGroupInfo(SessionPtr sess,NetPacket & pkt)588 int32_t ServerMsgHandler::OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt)
589 {
590 CALL_DEBUG_ENTER;
591 CHKPR(sess, ERROR_NULL_POINTER);
592 int32_t tokenType = sess->GetTokenType();
593 if (tokenType != TokenType::TOKEN_NATIVE && tokenType != TokenType::TOKEN_SHELL &&
594 tokenType !=TokenType::TOKEN_SYSTEM_HAP) {
595 MMI_HILOGW("Not native or systemapp skip, pid:%{public}d tokenType:%{public}d", sess->GetPid(), tokenType);
596 return RET_ERR;
597 }
598 WindowGroupInfo windowGroupInfo;
599 pkt >> windowGroupInfo.focusWindowId >> windowGroupInfo.displayId;
600 uint32_t num = 0;
601 pkt >> num;
602 if (pkt.ChkRWError()) {
603 MMI_HILOGE("Packet read window group info failed");
604 return RET_ERR;
605 }
606 for (uint32_t i = 0; i < num; i++) {
607 WindowInfo info;
608 pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
609 >> info.pointerHotAreas >> info.agentWindowId >> info.flags >> info.action
610 >> info.displayId >> info.zOrder >> info.pointerChangeAreas >> info.transform
611 >> info.windowInputType >> info.privacyMode >> info.windowType;
612 OnUiExtentionWindowInfo(pkt, info);
613 pkt >> info.rectChangeBySystem;
614 windowGroupInfo.windowsInfo.push_back(info);
615 if (pkt.ChkRWError()) {
616 MMI_HILOGE("Packet read display info failed");
617 return RET_ERR;
618 }
619 }
620 WIN_MGR->UpdateWindowInfo(windowGroupInfo);
621 return RET_OK;
622 }
623
RegisterWindowStateErrorCallback(SessionPtr sess,NetPacket & pkt)624 int32_t ServerMsgHandler::RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt)
625 {
626 CALL_DEBUG_ENTER;
627 int32_t pid = sess->GetPid();
628 WIN_MGR->SetWindowStateNotifyPid(pid);
629 MMI_HILOGI("pid:%{public}d", pid);
630 return RET_OK;
631 }
632
633 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
OnEnhanceConfig(SessionPtr sess,NetPacket & pkt)634 int32_t ServerMsgHandler::OnEnhanceConfig(SessionPtr sess, NetPacket &pkt)
635 {
636 CHKPR(sess, ERROR_NULL_POINTER);
637 int32_t userId = sess->GetUid();
638 if (userId != SECURITY_COMPONENT_SERVICE_ID) {
639 MMI_HILOGE("Session is not security component service");
640 return RET_ERR;
641 }
642 uint32_t num = 0;
643 pkt >> num;
644 uint8_t cfg[num];
645 for (uint32_t i = 0; i < num; i++) {
646 pkt >> cfg[i];
647 }
648
649 if (pkt.ChkRWError()) {
650 MMI_HILOGE("Packet read scinfo config failed");
651 return RET_ERR;
652 }
653 int32_t result = Security::SecurityComponent::SecCompEnhanceKit::SetEnhanceCfg(cfg, num);
654 if (result != 0) {
655 return RET_ERR;
656 }
657 return RET_OK;
658 }
659 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
660
661 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
OnAddInputHandler(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)662 int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType,
663 HandleEventType eventType, int32_t priority, uint32_t deviceTags)
664 {
665 CHKPR(sess, ERROR_NULL_POINTER);
666 MMI_HILOGD("handlerType:%{public}d", handlerType);
667 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
668 if (handlerType == InputHandlerType::INTERCEPTOR) {
669 auto interceptorHandler = InputHandler->GetInterceptorHandler();
670 CHKPR(interceptorHandler, ERROR_NULL_POINTER);
671 return interceptorHandler->AddInputHandler(handlerType, eventType, priority, deviceTags, sess);
672 }
673 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
674 #ifdef OHOS_BUILD_ENABLE_MONITOR
675 if (handlerType == InputHandlerType::MONITOR) {
676 auto monitorHandler = InputHandler->GetMonitorHandler();
677 CHKPR(monitorHandler, ERROR_NULL_POINTER);
678 return monitorHandler->AddInputHandler(handlerType, eventType, sess);
679 }
680 #endif // OHOS_BUILD_ENABLE_MONITOR
681 return RET_OK;
682 }
683
OnRemoveInputHandler(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)684 int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType,
685 HandleEventType eventType, int32_t priority, uint32_t deviceTags)
686 {
687 CHKPR(sess, ERROR_NULL_POINTER);
688 MMI_HILOGD("OnRemoveInputHandler handlerType:%{public}d eventType:%{public}u", handlerType, eventType);
689 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
690 if (handlerType == InputHandlerType::INTERCEPTOR) {
691 auto interceptorHandler = InputHandler->GetInterceptorHandler();
692 CHKPR(interceptorHandler, ERROR_NULL_POINTER);
693 interceptorHandler->RemoveInputHandler(handlerType, eventType, priority, deviceTags, sess);
694 }
695 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
696 #ifdef OHOS_BUILD_ENABLE_MONITOR
697 if (handlerType == InputHandlerType::MONITOR) {
698 auto monitorHandler = InputHandler->GetMonitorHandler();
699 CHKPR(monitorHandler, ERROR_NULL_POINTER);
700 monitorHandler->RemoveInputHandler(handlerType, eventType, sess);
701 ANRMgr->RemoveTimersByType(sess, ANR_MONITOR);
702 }
703 #endif // OHOS_BUILD_ENABLE_MONITOR
704 return RET_OK;
705 }
706 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
707
708 #ifdef OHOS_BUILD_ENABLE_MONITOR
OnMarkConsumed(SessionPtr sess,int32_t eventId)709 int32_t ServerMsgHandler::OnMarkConsumed(SessionPtr sess, int32_t eventId)
710 {
711 CHKPR(sess, ERROR_NULL_POINTER);
712 auto monitorHandler = InputHandler->GetMonitorHandler();
713 CHKPR(monitorHandler, ERROR_NULL_POINTER);
714 monitorHandler->MarkConsumed(eventId, sess);
715 return RET_OK;
716 }
717 #endif // OHOS_BUILD_ENABLE_MONITOR
718
719 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
OnMoveMouse(int32_t offsetX,int32_t offsetY)720 int32_t ServerMsgHandler::OnMoveMouse(int32_t offsetX, int32_t offsetY)
721 {
722 CALL_DEBUG_ENTER;
723 if (MouseEventHdr->NormalizeMoveMouse(offsetX, offsetY)) {
724 auto pointerEvent = MouseEventHdr->GetPointerEvent();
725 CHKPR(pointerEvent, ERROR_NULL_POINTER);
726 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
727 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
728 inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
729 MMI_HILOGD("Mouse movement message processed successfully");
730 }
731 return RET_OK;
732 }
733 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
734
735 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnSubscribeKeyEvent(IUdsServer * server,int32_t pid,int32_t subscribeId,const std::shared_ptr<KeyOption> option)736 int32_t ServerMsgHandler::OnSubscribeKeyEvent(IUdsServer *server, int32_t pid,
737 int32_t subscribeId, const std::shared_ptr<KeyOption> option)
738 {
739 CALL_DEBUG_ENTER;
740 CHKPR(server, ERROR_NULL_POINTER);
741 auto sess = server->GetSessionByPid(pid);
742 CHKPR(sess, ERROR_NULL_POINTER);
743 auto subscriberHandler = InputHandler->GetSubscriberHandler();
744 CHKPR(subscriberHandler, ERROR_NULL_POINTER);
745 return subscriberHandler->SubscribeKeyEvent(sess, subscribeId, option);
746 }
747
OnUnsubscribeKeyEvent(IUdsServer * server,int32_t pid,int32_t subscribeId)748 int32_t ServerMsgHandler::OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
749 {
750 CALL_DEBUG_ENTER;
751 CHKPR(server, ERROR_NULL_POINTER);
752 auto sess = server->GetSessionByPid(pid);
753 CHKPR(sess, ERROR_NULL_POINTER);
754 auto subscriberHandler = InputHandler->GetSubscriberHandler();
755 CHKPR(subscriberHandler, ERROR_NULL_POINTER);
756 return subscriberHandler->UnsubscribeKeyEvent(sess, subscribeId);
757 }
758 #endif // OHOS_BUILD_ENABLE_KEYBOARD
759
760 #ifdef OHOS_BUILD_ENABLE_SWITCH
OnSubscribeSwitchEvent(IUdsServer * server,int32_t pid,int32_t subscribeId,int32_t switchType)761 int32_t ServerMsgHandler::OnSubscribeSwitchEvent(
762 IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType)
763 {
764 CALL_DEBUG_ENTER;
765 CHKPR(server, ERROR_NULL_POINTER);
766 auto sess = server->GetSessionByPid(pid);
767 CHKPR(sess, ERROR_NULL_POINTER);
768 auto subscriberHandler = InputHandler->GetSwitchSubscriberHandler();
769 CHKPR(subscriberHandler, ERROR_NULL_POINTER);
770 return subscriberHandler->SubscribeSwitchEvent(sess, subscribeId, switchType);
771 }
772
OnUnsubscribeSwitchEvent(IUdsServer * server,int32_t pid,int32_t subscribeId)773 int32_t ServerMsgHandler::OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
774 {
775 CALL_DEBUG_ENTER;
776 CHKPR(server, ERROR_NULL_POINTER);
777 auto sess = server->GetSessionByPid(pid);
778 CHKPR(sess, ERROR_NULL_POINTER);
779 auto subscriberHandler = InputHandler->GetSwitchSubscriberHandler();
780 CHKPR(subscriberHandler, ERROR_NULL_POINTER);
781 return subscriberHandler->UnsubscribeSwitchEvent(sess, subscribeId);
782 }
783 #endif // OHOS_BUILD_ENABLE_SWITCH
784
785 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
AddInputEventFilter(sptr<IEventFilter> filter,int32_t filterId,int32_t priority,uint32_t deviceTags,int32_t clientPid)786 int32_t ServerMsgHandler::AddInputEventFilter(sptr<IEventFilter> filter,
787 int32_t filterId, int32_t priority, uint32_t deviceTags, int32_t clientPid)
788 {
789 auto filterHandler = InputHandler->GetFilterHandler();
790 CHKPR(filterHandler, ERROR_NULL_POINTER);
791 return filterHandler->AddInputEventFilter(filter, filterId, priority, deviceTags, clientPid);
792 }
793
RemoveInputEventFilter(int32_t clientPid,int32_t filterId)794 int32_t ServerMsgHandler::RemoveInputEventFilter(int32_t clientPid, int32_t filterId)
795 {
796 auto filterHandler = InputHandler->GetFilterHandler();
797 CHKPR(filterHandler, ERROR_NULL_POINTER);
798 return filterHandler->RemoveInputEventFilter(clientPid, filterId);
799 }
800 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
801
802 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
SetShieldStatus(int32_t shieldMode,bool isShield)803 int32_t ServerMsgHandler::SetShieldStatus(int32_t shieldMode, bool isShield)
804 {
805 return KeyEventHdr->SetShieldStatus(shieldMode, isShield);
806 }
807
GetShieldStatus(int32_t shieldMode,bool & isShield)808 int32_t ServerMsgHandler::GetShieldStatus(int32_t shieldMode, bool &isShield)
809 {
810 return KeyEventHdr->GetShieldStatus(shieldMode, isShield);
811 }
812 #endif // OHOS_BUILD_ENABLE_KEYBOARD
813
LaunchAbility()814 void ServerMsgHandler::LaunchAbility()
815 {
816 CALL_DEBUG_ENTER;
817 OHOS::MMI::AuthorizationDialog authorizationDialog;
818 authorizationDialog.ConnectSystemUi();
819 }
820
OnAuthorize(bool isAuthorize)821 int32_t ServerMsgHandler::OnAuthorize(bool isAuthorize)
822 {
823 CALL_DEBUG_ENTER;
824 if (isAuthorize) {
825 auto ret = authorizationCollection_.insert(std::make_pair(CurrentPID_, AuthorizationStatus::AUTHORIZED));
826 if (!ret.second) {
827 MMI_HILOGE("pid:%{public}d has already triggered authorization", CurrentPID_);
828 }
829 InjectNoticeInfo noticeInfo;
830 noticeInfo.pid = CurrentPID_;
831 AddInjectNotice(noticeInfo);
832 auto result = AUTHORIZE_HELPER->AddAuthorizeProcess(CurrentPID_,
833 [&] (int32_t pid) {
834 CloseInjectNotice(pid);
835 });
836 if (result != RET_OK) {
837 MMI_HILOGI("Authorize process failed, pid:%{public}d", CurrentPID_);
838 }
839 MMI_HILOGD("Agree to apply injection,pid:%{public}d", CurrentPID_);
840 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
841 if (InjectionType_ == InjectionType::KEYEVENT) {
842 OnInjectKeyEvent(keyEvent_, CurrentPID_, true);
843 }
844 #endif // OHOS_BUILD_ENABLE_KEYBOARD
845 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
846 if (InjectionType_ == InjectionType::POINTEREVENT) {
847 OnInjectPointerEvent(pointerEvent_, CurrentPID_, true, false);
848 }
849 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
850 return ERR_OK;
851 }
852 auto ret = authorizationCollection_.insert(std::make_pair(CurrentPID_, AuthorizationStatus::UNAUTHORIZED));
853 if (!ret.second) {
854 MMI_HILOGE("pid:%{public}d has already triggered authorization", CurrentPID_);
855 }
856 MMI_HILOGD("Reject application injection,pid:%{public}d", CurrentPID_);
857 return ERR_OK;
858 }
859
OnCancelInjection()860 int32_t ServerMsgHandler::OnCancelInjection()
861 {
862 CALL_DEBUG_ENTER;
863 auto iter = authorizationCollection_.find(CurrentPID_);
864 if (iter != authorizationCollection_.end()) {
865 authorizationCollection_.erase(iter);
866 AUTHORIZE_HELPER->CancelAuthorize(CurrentPID_);
867 auto state = AUTHORIZE_HELPER->GetAuthorizeState();
868 if (state != AuthorizeState::STATE_UNAUTHORIZE) {
869 CloseInjectNotice(CurrentPID_);
870 }
871 MMI_HILOGD("Cancel application authorization,pid:%{public}d", CurrentPID_);
872 CurrentPID_ = -1;
873 InjectionType_ = InjectionType::UNKNOWN;
874 keyEvent_ = nullptr;
875 pointerEvent_ = nullptr;
876 }
877 return ERR_OK;
878 }
879
SetWindowInfo(int32_t infoId,WindowInfo & info)880 void ServerMsgHandler::SetWindowInfo(int32_t infoId, WindowInfo &info)
881 {
882 CALL_DEBUG_ENTER;
883 if (transparentWins_.find(infoId) == transparentWins_.end()) {
884 MMI_HILOGE("The infoId is Invalid, infoId:%{public}d", infoId);
885 return;
886 }
887 info.pixelMap = transparentWins_[infoId].get();
888 }
889
SetPixelMapData(int32_t infoId,void * pixelMap)890 int32_t ServerMsgHandler::SetPixelMapData(int32_t infoId, void *pixelMap) __attribute__((no_sanitize("cfi")))
891 {
892 CALL_DEBUG_ENTER;
893 if (infoId < 0 || pixelMap == nullptr) {
894 MMI_HILOGE("The infoId is invalid or pixelMap is nullptr");
895 return ERR_INVALID_VALUE;
896 }
897
898 WIN_MGR->SetPixelMapData(infoId, pixelMap);
899 return RET_OK;
900 }
901
InitInjectNoticeSource()902 bool ServerMsgHandler::InitInjectNoticeSource()
903 {
904 CALL_DEBUG_ENTER;
905 MMI_HILOGD("Init InjectNoticeSource enter");
906 if (injectNotice_ == nullptr) {
907 injectNotice_ = std::make_shared<InjectNoticeManager>();
908 }
909 MMI_HILOGD("Injectnotice StartNoticeAbility end");
910 if (!injectNotice_->IsAbilityStart()) {
911 MMI_HILOGD("Injectnotice StartNoticeAbility begin");
912 bool isStart = injectNotice_->StartNoticeAbility();
913 if (!isStart) {
914 MMI_HILOGE("Injectnotice StartNoticeAbility isStart:%{public}d", isStart);
915 return false;
916 }
917 MMI_HILOGD("Injectnotice StartNoticeAbility end");
918 }
919 auto connection = injectNotice_->GetConnection();
920 CHKPF(connection);
921 if (!connection->IsConnected()) {
922 MMI_HILOGD("Injectnotice ConnectNoticeSrv begin");
923 bool isConnect = injectNotice_->ConnectNoticeSrv();
924 if (!isConnect) {
925 MMI_HILOGD("Injectnotice ConnectNoticeSrv isConnect:%{public}d", isConnect);
926 return false;
927 }
928 MMI_HILOGD("Injectnotice ConnectNoticeSrv end");
929 }
930 MMI_HILOGD("Injectnotice InitInjectNoticeSource end");
931 return true;
932 }
933
AddInjectNotice(const InjectNoticeInfo & noticeInfo)934 bool ServerMsgHandler::AddInjectNotice(const InjectNoticeInfo ¬iceInfo)
935 {
936 CALL_DEBUG_ENTER;
937 bool isInit = InitInjectNoticeSource();
938 if (!isInit) {
939 MMI_HILOGE("InitinjectNotice_ Source error");
940 return false;
941 }
942 MMI_HILOGD("SendNotice submit begin");
943 ffrt::submit([this, noticeInfo] {
944 MMI_HILOGD("SendNotice submit enter");
945 CHKPV(injectNotice_);
946 auto pConnect = injectNotice_->GetConnection();
947 CHKPV(pConnect);
948 int32_t timeSecond = 0;
949 while (timeSecond <= SEND_NOTICE_OVERTIME) {
950 bool isConnect = pConnect->IsConnected();
951 MMI_HILOGD("SendNotice %{public}d", isConnect);
952 if (isConnect) {
953 MMI_HILOGD("SendNotice begin");
954 pConnect->SendNotice(noticeInfo);
955 break;
956 }
957 timeSecond += 1;
958 sleep(1);
959 }
960 MMI_HILOGD("SendNotice submit leave");
961 });
962 return true;
963 }
964
CloseInjectNotice(int32_t pid)965 bool ServerMsgHandler::CloseInjectNotice(int32_t pid)
966 {
967 CALL_DEBUG_ENTER;
968 bool isInit = InitInjectNoticeSource();
969 if (!isInit) {
970 MMI_HILOGE("InitinjectNotice_ Source error");
971 return false;
972 }
973 MMI_HILOGD("CloseNotice submit begin");
974 InjectNoticeInfo noticeInfo;
975 noticeInfo.pid = pid;
976 ffrt::submit([this, noticeInfo] {
977 MMI_HILOGD("CloseNotice submit enter");
978 CHKPV(injectNotice_);
979 auto pConnect = injectNotice_->GetConnection();
980 CHKPV(pConnect);
981 int32_t timeSecond = 0;
982 while (timeSecond <= SEND_NOTICE_OVERTIME) {
983 bool isConnect = pConnect->IsConnected();
984 MMI_HILOGD("CloseNotice %{public}d", isConnect);
985 if (isConnect) {
986 MMI_HILOGD("CloseNotice begin");
987 pConnect->CancelNotice(noticeInfo);
988 break;
989 }
990 timeSecond += 1;
991 sleep(1);
992 }
993 MMI_HILOGD("CloseNotice submit leave");
994 });
995 return true;
996 }
997
OnTransferBinderClientSrv(const sptr<IRemoteObject> & binderClientObject,int32_t pid)998 int32_t ServerMsgHandler::OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid)
999 {
1000 CALL_DEBUG_ENTER;
1001 bool bRet = clientDeathHandler_.RegisterClientDeathRecipient(binderClientObject, pid);
1002 if (!bRet) {
1003 MMI_HILOGE("Failed to registerClientDeathRecipient");
1004 return RET_ERR;
1005 }
1006 return ERR_OK;
1007 }
1008 } // namespace MMI
1009 } // namespace OHOS
1010