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 [[ maybe_unused ]] 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 [[ maybe_unused ]] 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:%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 (!FixTargetWindowId(pointerEvent, pointerEvent->GetPointerAction(), isShell)) {
261                 return RET_ERR;
262             }
263             inputEventNormalizeHandler->HandleTouchEvent(pointerEvent);
264             if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) &&
265                 !IsNavigationWindowInjectEvent(pointerEvent)) {
266                 TOUCH_DRAWING_MGR->TouchDrawHandler(pointerEvent);
267             }
268 #endif // OHOS_BUILD_ENABLE_TOUCH
269             break;
270         }
271         case PointerEvent::SOURCE_TYPE_MOUSE:
272 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
273         case PointerEvent::SOURCE_TYPE_JOYSTICK:
274 #endif // OHOS_BUILD_ENABLE_JOYSTICK
275         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
276 #ifdef OHOS_BUILD_ENABLE_POINTER
277             int32_t ret = AccelerateMotion(pointerEvent);
278             if (ret != RET_OK) {
279                 MMI_HILOGE("Failed to accelerate motion, error:%{public}d", ret);
280                 return ret;
281             }
282             UpdatePointerEvent(pointerEvent);
283             inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
284             CHKPR(pointerEvent, ERROR_NULL_POINTER);
285             if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
286                 break;
287             } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER)) {
288                 IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), false, 0, false);
289             } else if (((pointerEvent->GetPointerAction() < PointerEvent::POINTER_ACTION_PULL_DOWN) ||
290                 (pointerEvent->GetPointerAction() > PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW)) &&
291                 !IPointerDrawingManager::GetInstance()->IsPointerVisible()) {
292                 IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), true, 0, false);
293             }
294 #endif // OHOS_BUILD_ENABLE_POINTER
295             break;
296         }
297         default: {
298             MMI_HILOGW("Source type is unknown, source:%{public}d", pointerEvent->GetSourceType());
299             break;
300         }
301     }
302     return SaveTargetWindowId(pointerEvent, isShell);
303 }
304 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
305 
306 #ifdef OHOS_BUILD_ENABLE_POINTER
AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent)307 int32_t ServerMsgHandler::AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent)
308 {
309     if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_RAW_POINTER_MOVEMENT) ||
310         (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE) ||
311         ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE) &&
312         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE) &&
313         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_BUTTON_DOWN))) {
314             return RET_OK;
315     }
316     PointerEvent::PointerItem pointerItem {};
317     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
318         MMI_HILOGE("Pointer event is corrupted");
319         return RET_ERR;
320     }
321     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
322     if (cursorPos.displayId < 0) {
323         MMI_HILOGE("No display");
324         return RET_ERR;
325     }
326     Offset offset {
327         .dx = pointerItem.GetRawDx(),
328         .dy = pointerItem.GetRawDy(),
329     };
330     auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos.displayId);
331     CHKPR(displayInfo, ERROR_NULL_POINTER);
332 #ifndef OHOS_BUILD_EMULATOR
333     if (TOUCH_DRAWING_MGR->IsWindowRotation()) {
334         CalculateOffset(displayInfo->direction, offset);
335     }
336 #endif // OHOS_BUILD_EMULATOR
337     int32_t ret = RET_OK;
338     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_TOUCHPAD_POINTER)) {
339         ret = HandleMotionAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(),
340             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
341             MouseTransformProcessor::GetTouchpadSpeed(), static_cast<int32_t>(DeviceType::DEVICE_KLV));
342     } else {
343         ret = HandleMotionAccelerateMouse(&offset, WIN_MGR->GetMouseIsCaptureMode(),
344             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
345             MouseTransformProcessor::GetPointerSpeed(), static_cast<int32_t>(DeviceType::DEVICE_KLV));
346     }
347     if (ret != RET_OK) {
348         MMI_HILOGE("Failed to accelerate pointer motion, error:%{public}d", ret);
349         return ret;
350     }
351     WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
352     if (EventLogHelper::IsBetaVersion() && !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
353         MMI_HILOGD("Cursor move to (x:%{public}.2f, y:%{public}.2f, DisplayId:%{public}d)",
354             cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
355     } else {
356         MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
357             cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
358     }
359     return RET_OK;
360 }
361 
CalculateOffset(Direction direction, Offset &offset)362 void ServerMsgHandler::CalculateOffset(Direction direction, Offset &offset)
363 {
364     std::negate<double> neg;
365     if (direction == DIRECTION90) {
366         double tmp = offset.dx;
367         offset.dx = offset.dy;
368         offset.dy = neg(tmp);
369     } else if (direction == DIRECTION180) {
370         offset.dx = neg(offset.dx);
371         offset.dy = neg(offset.dy);
372     } else if (direction == DIRECTION270) {
373         double tmp = offset.dx;
374         offset.dx = neg(offset.dy);
375         offset.dy = tmp;
376     }
377 }
378 #endif // OHOS_BUILD_ENABLE_POINTER
379 
380 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent)381 void ServerMsgHandler::UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
382 {
383     if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_RAW_POINTER_MOVEMENT) ||
384         (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE)) {
385         return;
386     }
387     PointerEvent::PointerItem pointerItem {};
388     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
389         MMI_HILOGE("Pointer event is corrupted");
390         return;
391     }
392     auto mouseInfo = WIN_MGR->GetMouseInfo();
393     pointerItem.SetDisplayX(mouseInfo.physicalX);
394     pointerItem.SetDisplayY(mouseInfo.physicalY);
395     pointerEvent->UpdatePointerItem(pointerEvent->GetPointerId(), pointerItem);
396     pointerEvent->SetTargetDisplayId(mouseInfo.displayId);
397 }
398 
SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell)399 int32_t ServerMsgHandler::SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell)
400 {
401     CHKPR(pointerEvent, ERROR_NULL_POINTER);
402     if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
403         (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
404         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER)) {
405         int32_t pointerId = pointerEvent->GetPointerId();
406         PointerEvent::PointerItem pointerItem;
407         if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
408             MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
409             return RET_ERR;
410         }
411         int32_t targetWindowId = pointerEvent->GetTargetWindowId();
412         if (isShell) {
413             shellTargetWindowIds_[pointerId] = targetWindowId;
414         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
415             accessTargetWindowIds_[pointerId] = targetWindowId;
416         } else {
417             nativeTargetWindowIds_[pointerId] = targetWindowId;
418         }
419     }
420     if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
421         (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
422         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT)) {
423         int32_t pointerId = pointerEvent->GetPointerId();
424         if (isShell) {
425             shellTargetWindowIds_.erase(pointerId);
426         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
427             accessTargetWindowIds_.erase(pointerId);
428         } else {
429             nativeTargetWindowIds_.erase(pointerId);
430         }
431     }
432     return RET_OK;
433 }
434 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
435 
436 #ifdef OHOS_BUILD_ENABLE_TOUCH
FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, int32_t action, bool isShell)437 bool ServerMsgHandler::FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,
438     int32_t action, bool isShell)
439 {
440     CHKPF(pointerEvent);
441     int32_t targetWindowId = -1;
442     int32_t pointerId = pointerEvent->GetPointerId();
443     PointerEvent::PointerItem pointerItem;
444     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
445         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
446         return false;
447     }
448     if (pointerEvent->GetTargetWindowId() > 0) {
449         return true;
450     }
451     if (isShell) {
452         auto iter = shellTargetWindowIds_.find(pointerEvent->GetPointerId());
453         if (iter != shellTargetWindowIds_.end()) {
454             targetWindowId = iter->second;
455         }
456     } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
457         auto iter = accessTargetWindowIds_.find(pointerEvent->GetPointerId());
458         if (iter != accessTargetWindowIds_.end()) {
459             targetWindowId = iter->second;
460         }
461     } else {
462         auto iter = nativeTargetWindowIds_.find(pointerEvent->GetPointerId());
463         if (iter != nativeTargetWindowIds_.end()) {
464             targetWindowId = iter->second;
465         }
466     }
467     MMI_HILOGD("TargetWindowId:%{public}d %{public}d", pointerEvent->GetTargetWindowId(), targetWindowId);
468     if (action == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
469         action == PointerEvent::POINTER_ACTION_DOWN || targetWindowId < 0) {
470         MMI_HILOGD("Down event or targetWindowId less 0 is not need fix window id");
471         return true;
472     }
473     auto pointerIds = pointerEvent->GetPointerIds();
474     if (pointerIds.empty()) {
475         MMI_HILOGE("GetPointerIds is empty");
476         return false;
477     }
478     pointerEvent->SetTargetWindowId(targetWindowId);
479     pointerItem.SetTargetWindowId(targetWindowId);
480     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
481     return true;
482 }
483 #endif // OHOS_BUILD_ENABLE_TOUCH
484 
OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info)485 int32_t ServerMsgHandler::OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info)
486 {
487     uint32_t num = 0;
488     pkt >> num;
489     if (pkt.ChkRWError()) {
490         MMI_HILOGE("Packet read display info failed");
491         return RET_ERR;
492     }
493     for (uint32_t i = 0; i < num; i++) {
494         WindowInfo extensionInfo;
495         pkt >> extensionInfo.id >> extensionInfo.pid >> extensionInfo.uid >> extensionInfo.area
496             >> extensionInfo.defaultHotAreas >> extensionInfo.pointerHotAreas >> extensionInfo.agentWindowId
497             >> extensionInfo.flags >> extensionInfo.action >> extensionInfo.displayId >> extensionInfo.zOrder
498             >> extensionInfo.pointerChangeAreas >> extensionInfo.transform >> extensionInfo.windowInputType
499             >> extensionInfo.privacyMode >> extensionInfo.windowType >> extensionInfo.privacyUIFlag
500             >> extensionInfo.rectChangeBySystem;
501         info.uiExtentionWindowInfo.push_back(extensionInfo);
502         if (pkt.ChkRWError()) {
503             MMI_HILOGE("Packet read extention window info failed");
504             return RET_ERR;
505         }
506     }
507     return RET_OK;
508 }
509 
OnDisplayInfo(SessionPtr sess, NetPacket &pkt)510 int32_t ServerMsgHandler::OnDisplayInfo(SessionPtr sess, NetPacket &pkt)
511 {
512     CALL_DEBUG_ENTER;
513     CHKPR(sess, ERROR_NULL_POINTER);
514     DisplayGroupInfo displayGroupInfo;
515     pkt >> displayGroupInfo.width >> displayGroupInfo.height >>
516         displayGroupInfo.focusWindowId >> displayGroupInfo.currentUserId;
517     uint32_t num = 0;
518     pkt >> num;
519     if (pkt.ChkRWError()) {
520         MMI_HILOGE("Packet read display info failed");
521         return RET_ERR;
522     }
523     for (uint32_t i = 0; i < num; i++) {
524         WindowInfo info;
525         int32_t byteCount = 0;
526         pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
527             >> info.pointerHotAreas >> info.agentWindowId >> info.flags >> info.action
528             >> info.displayId >> info.zOrder >> info.pointerChangeAreas >> info.transform
529             >> info.windowInputType >> info.privacyMode >> info.windowType >> byteCount;
530 
531         OnUiExtentionWindowInfo(pkt, info);
532         pkt >> info.rectChangeBySystem;
533         displayGroupInfo.windowsInfo.push_back(info);
534         if (pkt.ChkRWError()) {
535             MMI_HILOGE("Packet read display info failed");
536             return RET_ERR;
537         }
538     }
539     pkt >> num;
540     for (uint32_t i = 0; i < num; i++) {
541         DisplayInfo info;
542         pkt >> info.id >> info.x >> info.y >> info.width >> info.height >> info.dpi >> info.name
543             >> info.uniq >> info.direction >> info.displayDirection >> info.displayMode >> info.transform;
544         displayGroupInfo.displaysInfo.push_back(info);
545         if (pkt.ChkRWError()) {
546             MMI_HILOGE("Packet read display info failed");
547             return RET_ERR;
548         }
549     }
550     if (pkt.ChkRWError()) {
551         MMI_HILOGE("Packet read display info failed");
552         return RET_ERR;
553     }
554     WIN_MGR->UpdateDisplayInfoExtIfNeed(displayGroupInfo, true);
555     return RET_OK;
556 }
557 
558 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
OnWindowAreaInfo(SessionPtr sess, NetPacket &pkt)559 int32_t ServerMsgHandler::OnWindowAreaInfo(SessionPtr sess, NetPacket &pkt)
560 {
561     CALL_DEBUG_ENTER;
562     CHKPR(sess, ERROR_NULL_POINTER);
563     int32_t temp = 0;
564     int32_t pid = 0;
565     int32_t windowId = 0;
566     pkt >> temp >> pid >> windowId;
567     WindowArea area = static_cast<WindowArea>(temp);
568     if (pkt.ChkRWError()) {
569         MMI_HILOGE("Packet read display info failed");
570         return RET_ERR;
571     }
572     WIN_MGR->SetWindowPointerStyle(area, pid, windowId);
573     return RET_OK;
574 }
575 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
576 
OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt)577 int32_t ServerMsgHandler::OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt)
578 {
579     CALL_DEBUG_ENTER;
580     CHKPR(sess, ERROR_NULL_POINTER);
581     WindowGroupInfo windowGroupInfo;
582     pkt >> windowGroupInfo.focusWindowId >> windowGroupInfo.displayId;
583     uint32_t num = 0;
584     pkt >> num;
585     if (pkt.ChkRWError()) {
586         MMI_HILOGE("Packet read window group info failed");
587         return RET_ERR;
588     }
589     for (uint32_t i = 0; i < num; i++) {
590         WindowInfo info;
591         pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
592             >> info.pointerHotAreas >> info.agentWindowId >> info.flags >> info.action
593             >> info.displayId >> info.zOrder >> info.pointerChangeAreas >> info.transform
594             >> info.windowInputType >> info.privacyMode >> info.windowType;
595         OnUiExtentionWindowInfo(pkt, info);
596         pkt >> info.rectChangeBySystem;
597         windowGroupInfo.windowsInfo.push_back(info);
598         if (pkt.ChkRWError()) {
599             MMI_HILOGE("Packet read display info failed");
600             return RET_ERR;
601         }
602     }
603     WIN_MGR->UpdateWindowInfo(windowGroupInfo);
604     return RET_OK;
605 }
606 
RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt)607 int32_t ServerMsgHandler::RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt)
608 {
609     CALL_DEBUG_ENTER;
610     int32_t pid = sess->GetPid();
611     WIN_MGR->SetWindowStateNotifyPid(pid);
612     MMI_HILOGI("pid:%{public}d", pid);
613     return RET_OK;
614 }
615 
616 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
OnEnhanceConfig(SessionPtr sess, NetPacket &pkt)617 int32_t ServerMsgHandler::OnEnhanceConfig(SessionPtr sess, NetPacket &pkt)
618 {
619     CHKPR(sess, ERROR_NULL_POINTER);
620     int32_t userId = sess->GetUid();
621     if (userId != SECURITY_COMPONENT_SERVICE_ID) {
622         MMI_HILOGE("Session is not security component service");
623         return RET_ERR;
624     }
625     uint32_t num = 0;
626     pkt >> num;
627     uint8_t cfg[num];
628     for (uint32_t i = 0; i < num; i++) {
629         pkt >> cfg[i];
630     }
631 
632     if (pkt.ChkRWError()) {
633         MMI_HILOGE("Packet read scinfo config failed");
634         return RET_ERR;
635     }
636     int32_t result = Security::SecurityComponent::SecCompEnhanceKit::SetEnhanceCfg(cfg, num);
637     if (result != 0) {
638         return RET_ERR;
639     }
640     return RET_OK;
641 }
642 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
643 
644 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags)645 int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType,
646     HandleEventType eventType, int32_t priority, uint32_t deviceTags)
647 {
648     CHKPR(sess, ERROR_NULL_POINTER);
649     MMI_HILOGD("handlerType:%{public}d", handlerType);
650 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
651     if (handlerType == InputHandlerType::INTERCEPTOR) {
652         auto interceptorHandler = InputHandler->GetInterceptorHandler();
653         CHKPR(interceptorHandler, ERROR_NULL_POINTER);
654         return interceptorHandler->AddInputHandler(handlerType, eventType, priority, deviceTags, sess);
655     }
656 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
657 #ifdef OHOS_BUILD_ENABLE_MONITOR
658     if (handlerType == InputHandlerType::MONITOR) {
659         auto monitorHandler = InputHandler->GetMonitorHandler();
660         CHKPR(monitorHandler, ERROR_NULL_POINTER);
661         return monitorHandler->AddInputHandler(handlerType, eventType, sess);
662     }
663 #endif // OHOS_BUILD_ENABLE_MONITOR
664     return RET_OK;
665 }
666 
OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags)667 int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType,
668     HandleEventType eventType, int32_t priority, uint32_t deviceTags)
669 {
670     CHKPR(sess, ERROR_NULL_POINTER);
671     MMI_HILOGD("OnRemoveInputHandler handlerType:%{public}d eventType:%{public}u", handlerType, eventType);
672 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
673     if (handlerType == InputHandlerType::INTERCEPTOR) {
674         auto interceptorHandler = InputHandler->GetInterceptorHandler();
675         CHKPR(interceptorHandler, ERROR_NULL_POINTER);
676         interceptorHandler->RemoveInputHandler(handlerType, eventType, priority, deviceTags, sess);
677     }
678 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
679 #ifdef OHOS_BUILD_ENABLE_MONITOR
680     if (handlerType == InputHandlerType::MONITOR) {
681         auto monitorHandler = InputHandler->GetMonitorHandler();
682         CHKPR(monitorHandler, ERROR_NULL_POINTER);
683         monitorHandler->RemoveInputHandler(handlerType, eventType, sess);
684         ANRMgr->RemoveTimersByType(sess, ANR_MONITOR);
685     }
686 #endif // OHOS_BUILD_ENABLE_MONITOR
687     return RET_OK;
688 }
689 
OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, std::vector<int32_t> actionsType)690 int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType,
691     std::vector<int32_t> actionsType)
692 {
693     CHKPR(sess, ERROR_NULL_POINTER);
694     MMI_HILOGD("handlerType:%{public}d", handlerType);
695 #ifdef OHOS_BUILD_ENABLE_MONITOR
696     if (handlerType == InputHandlerType::MONITOR) {
697         auto monitorHandler = InputHandler->GetMonitorHandler();
698         CHKPR(monitorHandler, ERROR_NULL_POINTER);
699         return monitorHandler->AddInputHandler(handlerType, actionsType, sess);
700     }
701 #endif // OHOS_BUILD_ENABLE_MONITOR
702     return RET_OK;
703 }
704 
OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, std::vector<int32_t> actionsType)705 int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType,
706     std::vector<int32_t> actionsType)
707 {
708     CHKPR(sess, ERROR_NULL_POINTER);
709     MMI_HILOGD("OnRemoveInputHandler handlerType:%{public}d", handlerType);
710 #ifdef OHOS_BUILD_ENABLE_MONITOR
711     if (handlerType == InputHandlerType::MONITOR) {
712         auto monitorHandler = InputHandler->GetMonitorHandler();
713         CHKPR(monitorHandler, ERROR_NULL_POINTER);
714         monitorHandler->RemoveInputHandler(handlerType, actionsType, sess);
715         ANRMgr->RemoveTimersByType(sess, ANR_MONITOR);
716     }
717 #endif // OHOS_BUILD_ENABLE_MONITOR
718     return RET_OK;
719 }
720 
721 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
722 
OnAddGestureMonitor(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)723 int32_t ServerMsgHandler::OnAddGestureMonitor(SessionPtr sess, InputHandlerType handlerType,
724     HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)
725 {
726 #ifdef OHOS_BUILD_ENABLE_MONITOR
727     if (handlerType == InputHandlerType::MONITOR) {
728         auto monitorHandler = InputHandler->GetMonitorHandler();
729         CHKPR(monitorHandler, ERROR_NULL_POINTER);
730         return monitorHandler->AddInputHandler(handlerType, eventType, sess, gestureType, fingers);
731     }
732 #endif // OHOS_BUILD_ENABLE_MONITOR
733     return RET_OK;
734 }
735 
OnRemoveGestureMonitor(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)736 int32_t ServerMsgHandler::OnRemoveGestureMonitor(SessionPtr sess, InputHandlerType handlerType,
737     HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)
738 {
739 #ifdef OHOS_BUILD_ENABLE_MONITOR
740     if (handlerType == InputHandlerType::MONITOR) {
741         CHKPR(sess, ERROR_NULL_POINTER);
742         auto monitorHandler = InputHandler->GetMonitorHandler();
743         CHKPR(monitorHandler, ERROR_NULL_POINTER);
744         monitorHandler->RemoveInputHandler(handlerType, eventType, sess, gestureType, fingers);
745     }
746 #endif // OHOS_BUILD_ENABLE_MONITOR
747     return RET_OK;
748 }
749 
750 #ifdef OHOS_BUILD_ENABLE_MONITOR
OnMarkConsumed(SessionPtr sess, int32_t eventId)751 int32_t ServerMsgHandler::OnMarkConsumed(SessionPtr sess, int32_t eventId)
752 {
753     CHKPR(sess, ERROR_NULL_POINTER);
754     auto monitorHandler = InputHandler->GetMonitorHandler();
755     CHKPR(monitorHandler, ERROR_NULL_POINTER);
756     monitorHandler->MarkConsumed(eventId, sess);
757     return RET_OK;
758 }
759 #endif // OHOS_BUILD_ENABLE_MONITOR
760 
761 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
OnMoveMouse(int32_t offsetX, int32_t offsetY)762 int32_t ServerMsgHandler::OnMoveMouse(int32_t offsetX, int32_t offsetY)
763 {
764     CALL_DEBUG_ENTER;
765     if (MouseEventHdr->NormalizeMoveMouse(offsetX, offsetY)) {
766         auto pointerEvent = MouseEventHdr->GetPointerEvent();
767         CHKPR(pointerEvent, ERROR_NULL_POINTER);
768         auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
769         CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
770         inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
771         MMI_HILOGD("Mouse movement message processed successfully");
772     }
773     return RET_OK;
774 }
775 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
776 
777 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnSubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId, const std::shared_ptr<KeyOption> option)778 int32_t ServerMsgHandler::OnSubscribeKeyEvent(IUdsServer *server, int32_t pid,
779     int32_t subscribeId, const std::shared_ptr<KeyOption> option)
780 {
781     CALL_DEBUG_ENTER;
782     CHKPR(server, ERROR_NULL_POINTER);
783     auto sess = server->GetSessionByPid(pid);
784     CHKPR(sess, ERROR_NULL_POINTER);
785     auto subscriberHandler = InputHandler->GetSubscriberHandler();
786     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
787     return subscriberHandler->SubscribeKeyEvent(sess, subscribeId, option);
788 }
789 
OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)790 int32_t ServerMsgHandler::OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
791 {
792     CALL_DEBUG_ENTER;
793     CHKPR(server, ERROR_NULL_POINTER);
794     auto sess = server->GetSessionByPid(pid);
795     CHKPR(sess, ERROR_NULL_POINTER);
796     auto subscriberHandler = InputHandler->GetSubscriberHandler();
797     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
798     return subscriberHandler->UnsubscribeKeyEvent(sess, subscribeId);
799 }
800 
OnSubscribeHotkey(IUdsServer *server, int32_t pid, int32_t subscribeId, const std::shared_ptr<KeyOption> option)801 int32_t ServerMsgHandler::OnSubscribeHotkey(IUdsServer *server, int32_t pid,
802     int32_t subscribeId, const std::shared_ptr<KeyOption> option)
803 {
804     CALL_DEBUG_ENTER;
805     CHKPR(server, ERROR_NULL_POINTER);
806     auto sess = server->GetSessionByPid(pid);
807     CHKPR(sess, ERROR_NULL_POINTER);
808     auto subscriberHandler = InputHandler->GetSubscriberHandler();
809     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
810     return subscriberHandler->SubscribeHotkey(sess, subscribeId, option);
811 }
812 
OnUnsubscribeHotkey(IUdsServer *server, int32_t pid, int32_t subscribeId)813 int32_t ServerMsgHandler::OnUnsubscribeHotkey(IUdsServer *server, int32_t pid, int32_t subscribeId)
814 {
815     CALL_DEBUG_ENTER;
816     CHKPR(server, ERROR_NULL_POINTER);
817     auto sess = server->GetSessionByPid(pid);
818     CHKPR(sess, ERROR_NULL_POINTER);
819     auto subscriberHandler = InputHandler->GetSubscriberHandler();
820     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
821     return subscriberHandler->UnsubscribeHotkey(sess, subscribeId);
822 }
823 #endif // OHOS_BUILD_ENABLE_KEYBOARD
824 
825 #ifdef OHOS_BUILD_ENABLE_SWITCH
OnSubscribeSwitchEvent( IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType)826 int32_t ServerMsgHandler::OnSubscribeSwitchEvent(
827     IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType)
828 {
829     CALL_DEBUG_ENTER;
830     CHKPR(server, ERROR_NULL_POINTER);
831     auto sess = server->GetSessionByPid(pid);
832     CHKPR(sess, ERROR_NULL_POINTER);
833     auto subscriberHandler = InputHandler->GetSwitchSubscriberHandler();
834     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
835     return subscriberHandler->SubscribeSwitchEvent(sess, subscribeId, switchType);
836 }
837 
OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)838 int32_t ServerMsgHandler::OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
839 {
840     CALL_DEBUG_ENTER;
841     CHKPR(server, ERROR_NULL_POINTER);
842     auto sess = server->GetSessionByPid(pid);
843     CHKPR(sess, ERROR_NULL_POINTER);
844     auto subscriberHandler = InputHandler->GetSwitchSubscriberHandler();
845     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
846     return subscriberHandler->UnsubscribeSwitchEvent(sess, subscribeId);
847 }
848 #endif // OHOS_BUILD_ENABLE_SWITCH
849 
850 #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)851 int32_t ServerMsgHandler::AddInputEventFilter(sptr<IEventFilter> filter,
852     int32_t filterId, int32_t priority, uint32_t deviceTags, int32_t clientPid)
853 {
854     auto filterHandler = InputHandler->GetFilterHandler();
855     CHKPR(filterHandler, ERROR_NULL_POINTER);
856     return filterHandler->AddInputEventFilter(filter, filterId, priority, deviceTags, clientPid);
857 }
858 
RemoveInputEventFilter(int32_t clientPid, int32_t filterId)859 int32_t ServerMsgHandler::RemoveInputEventFilter(int32_t clientPid, int32_t filterId)
860 {
861     auto filterHandler = InputHandler->GetFilterHandler();
862     CHKPR(filterHandler, ERROR_NULL_POINTER);
863     return filterHandler->RemoveInputEventFilter(clientPid, filterId);
864 }
865 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
866 
867 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
SetShieldStatus(int32_t shieldMode, bool isShield)868 int32_t ServerMsgHandler::SetShieldStatus(int32_t shieldMode, bool isShield)
869 {
870     return KeyEventHdr->SetShieldStatus(shieldMode, isShield);
871 }
872 
GetShieldStatus(int32_t shieldMode, bool &isShield)873 int32_t ServerMsgHandler::GetShieldStatus(int32_t shieldMode, bool &isShield)
874 {
875     return KeyEventHdr->GetShieldStatus(shieldMode, isShield);
876 }
877 #endif // OHOS_BUILD_ENABLE_KEYBOARD
878 
LaunchAbility()879 void ServerMsgHandler::LaunchAbility()
880 {
881     CALL_DEBUG_ENTER;
882     OHOS::MMI::AuthorizationDialog authorizationDialog;
883     authorizationDialog.ConnectSystemUi();
884 }
885 
OnAuthorize(bool isAuthorize)886 int32_t ServerMsgHandler::OnAuthorize(bool isAuthorize)
887 {
888     CALL_DEBUG_ENTER;
889     if (isAuthorize) {
890         auto ret = authorizationCollection_.insert(std::make_pair(CurrentPID_, AuthorizationStatus::AUTHORIZED));
891         if (!ret.second) {
892             MMI_HILOGE("pid:%{public}d has already triggered authorization", CurrentPID_);
893         }
894         InjectNoticeInfo noticeInfo;
895         noticeInfo.pid = CurrentPID_;
896         AddInjectNotice(noticeInfo);
897         auto result = AUTHORIZE_HELPER->AddAuthorizeProcess(CurrentPID_,
898             [&] (int32_t pid) {
899                 CloseInjectNotice(pid);
900         });
901         if (result != RET_OK) {
902             MMI_HILOGI("Authorize process failed, pid:%{public}d", CurrentPID_);
903         }
904         MMI_HILOGD("Agree to apply injection,pid:%{public}d", CurrentPID_);
905 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
906         if (InjectionType_ == InjectionType::KEYEVENT) {
907             OnInjectKeyEvent(keyEvent_, CurrentPID_, true);
908         }
909 #endif // OHOS_BUILD_ENABLE_KEYBOARD
910 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
911         if (InjectionType_ == InjectionType::POINTEREVENT) {
912             OnInjectPointerEvent(pointerEvent_, CurrentPID_, true, false);
913         }
914 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
915         return ERR_OK;
916     }
917     AUTHORIZE_HELPER->CancelAuthorize(CurrentPID_);
918     auto ret = authorizationCollection_.insert(std::make_pair(CurrentPID_, AuthorizationStatus::UNAUTHORIZED));
919     if (!ret.second) {
920         MMI_HILOGE("pid:%{public}d has already triggered authorization", CurrentPID_);
921     }
922     MMI_HILOGD("Reject application injection,pid:%{public}d", CurrentPID_);
923     return ERR_OK;
924 }
925 
OnCancelInjection()926 int32_t ServerMsgHandler::OnCancelInjection()
927 {
928     CALL_DEBUG_ENTER;
929     auto iter = authorizationCollection_.find(CurrentPID_);
930     if (iter != authorizationCollection_.end()) {
931         authorizationCollection_.erase(iter);
932         AUTHORIZE_HELPER->CancelAuthorize(CurrentPID_);
933         auto state = AUTHORIZE_HELPER->GetAuthorizeState();
934         if (state != AuthorizeState::STATE_UNAUTHORIZE) {
935             CloseInjectNotice(CurrentPID_);
936         }
937         MMI_HILOGD("Cancel application authorization,pid:%{public}d", CurrentPID_);
938         CurrentPID_ = -1;
939         InjectionType_ = InjectionType::UNKNOWN;
940         keyEvent_ = nullptr;
941         pointerEvent_ = nullptr;
942     }
943     return ERR_OK;
944 }
945 
SetWindowInfo(int32_t infoId, WindowInfo &info)946 void ServerMsgHandler::SetWindowInfo(int32_t infoId, WindowInfo &info)
947 {
948     CALL_DEBUG_ENTER;
949     if (transparentWins_.find(infoId) == transparentWins_.end()) {
950         MMI_HILOGE("The infoId is Invalid, infoId:%{public}d", infoId);
951         return;
952     }
953     info.pixelMap = transparentWins_[infoId].get();
954 }
955 
SetPixelMapData(int32_t infoId, void *pixelMap)956 int32_t ServerMsgHandler::SetPixelMapData(int32_t infoId, void *pixelMap) __attribute__((no_sanitize("cfi")))
957 {
958     CALL_DEBUG_ENTER;
959     if (infoId < 0 || pixelMap == nullptr) {
960         MMI_HILOGE("The infoId is invalid or pixelMap is nullptr");
961         return ERR_INVALID_VALUE;
962     }
963 
964     WIN_MGR->SetPixelMapData(infoId, pixelMap);
965     return RET_OK;
966 }
967 
InitInjectNoticeSource()968 bool ServerMsgHandler::InitInjectNoticeSource()
969 {
970     CALL_DEBUG_ENTER;
971     MMI_HILOGD("Init InjectNoticeSource enter");
972     if (injectNotice_ == nullptr) {
973         injectNotice_ = std::make_shared<InjectNoticeManager>();
974     }
975     MMI_HILOGD("Injectnotice StartNoticeAbility ok");
976     if (!injectNotice_->IsAbilityStart()) {
977         MMI_HILOGD("Injectnotice StartNoticeAbility begin");
978         bool isStart = injectNotice_->StartNoticeAbility();
979         if (!isStart) {
980             MMI_HILOGE("Injectnotice StartNoticeAbility isStart:%{public}d", isStart);
981             return false;
982         }
983         MMI_HILOGD("Injectnotice StartNoticeAbility ok");
984     }
985     auto connection = injectNotice_->GetConnection();
986     CHKPF(connection);
987     if (!connection->IsConnected()) {
988         MMI_HILOGD("Injectnotice ConnectNoticeSrv begin");
989         bool isConnect = injectNotice_->ConnectNoticeSrv();
990         if (!isConnect) {
991             MMI_HILOGD("Injectnotice ConnectNoticeSrv isConnect:%{public}d", isConnect);
992             return false;
993         }
994         MMI_HILOGD("Injectnotice ConnectNoticeSrv ok");
995     }
996     MMI_HILOGD("Injectnotice InitInjectNoticeSource ok");
997     return true;
998 }
999 
AddInjectNotice(const InjectNoticeInfo &noticeInfo)1000 bool ServerMsgHandler::AddInjectNotice(const InjectNoticeInfo &noticeInfo)
1001 {
1002     CALL_DEBUG_ENTER;
1003     bool isInit = InitInjectNoticeSource();
1004     if (!isInit) {
1005         MMI_HILOGE("InitinjectNotice_ Source error");
1006         return false;
1007     }
1008     MMI_HILOGD("SendNotice submit begin");
1009     ffrt::submit([this, noticeInfo] {
1010         MMI_HILOGD("SendNotice submit enter");
1011         CHKPV(injectNotice_);
1012         auto pConnect = injectNotice_->GetConnection();
1013         CHKPV(pConnect);
1014         int32_t timeSecond = 0;
1015         while (timeSecond <= SEND_NOTICE_OVERTIME) {
1016             bool isConnect = pConnect->IsConnected();
1017             MMI_HILOGD("SendNotice %{public}d", isConnect);
1018             if (isConnect) {
1019                 MMI_HILOGD("SendNotice begin");
1020                 pConnect->SendNotice(noticeInfo);
1021                 break;
1022             }
1023             timeSecond += 1;
1024             sleep(1);
1025         }
1026         MMI_HILOGD("SendNotice submit leave");
1027     });
1028     return true;
1029 }
1030 
CloseInjectNotice(int32_t pid)1031 bool ServerMsgHandler::CloseInjectNotice(int32_t pid)
1032 {
1033     CALL_DEBUG_ENTER;
1034     bool isInit = InitInjectNoticeSource();
1035     if (!isInit) {
1036         MMI_HILOGE("InitinjectNotice_ Source error");
1037         return false;
1038     }
1039     MMI_HILOGD("CloseNotice submit begin");
1040     InjectNoticeInfo noticeInfo;
1041     noticeInfo.pid = pid;
1042     ffrt::submit([this, noticeInfo] {
1043         MMI_HILOGD("CloseNotice submit enter");
1044         CHKPV(injectNotice_);
1045         auto pConnect = injectNotice_->GetConnection();
1046         CHKPV(pConnect);
1047         int32_t timeSecond = 0;
1048         while (timeSecond <= SEND_NOTICE_OVERTIME) {
1049             bool isConnect = pConnect->IsConnected();
1050             MMI_HILOGD("CloseNotice %{public}d", isConnect);
1051             if (isConnect) {
1052                 MMI_HILOGD("CloseNotice begin");
1053                 pConnect->CancelNotice(noticeInfo);
1054                 break;
1055             }
1056             timeSecond += 1;
1057             sleep(1);
1058         }
1059         MMI_HILOGD("CloseNotice submit leave");
1060     });
1061     return true;
1062 }
1063 
OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid)1064 int32_t ServerMsgHandler::OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid)
1065 {
1066     CALL_DEBUG_ENTER;
1067     bool bRet = clientDeathHandler_.RegisterClientDeathRecipient(binderClientObject, pid);
1068     if (!bRet) {
1069         MMI_HILOGE("Failed to registerClientDeathRecipient");
1070         return RET_ERR;
1071     }
1072     return ERR_OK;
1073 }
1074 } // namespace MMI
1075 } // namespace OHOS
1076