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