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