• 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 "anr_manager.h"
19 #include "app_mgr_client.h"
20 #include "authorization_dialog.h"
21 #include "authorize_helper.h"
22 #include "bytrace_adapter.h"
23 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
24 #include "dfx_hisysevent.h"
25 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
26 #include "display_event_monitor.h"
27 #include "event_log_helper.h"
28 #include "input_device_manager.h"
29 #include "input_event_handler.h"
30 #include "i_pointer_drawing_manager.h"
31 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
32 #include "key_monitor_manager.h"
33 #endif // OHOS_BUILD_ENABLE_KEYBOARD
34 #include "long_press_subscriber_handler.h"
35 #include "libinput_adapter.h"
36 #include "time_cost_chk.h"
37 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
38 #include "touch_drawing_manager.h"
39 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
40 
41 #undef MMI_LOG_DOMAIN
42 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
43 #undef MMI_LOG_TAG
44 #define MMI_LOG_TAG "ServerMsgHandler"
45 
46 namespace OHOS {
47 namespace MMI {
48 namespace {
49 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
50 constexpr int32_t SECURITY_COMPONENT_SERVICE_ID = 3050;
51 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
52 constexpr int32_t SEND_NOTICE_OVERTIME { 5 };
53 [[ maybe_unused ]] constexpr int32_t DEFAULT_POINTER_ID { 10000 };
54 [[ maybe_unused ]] constexpr int32_t CAST_POINTER_ID { 5000 };
55 const std::string PRODUCT_TYPE = system::GetParameter("const.product.devicetype", "unknown");
56 const std::string PRODUCT_TYPE_PC = "2in1";
57 [[ maybe_unused ]] constexpr int32_t WINDOW_ROTATE { 0 };
58 constexpr int32_t COMMON_PERMISSION_CHECK_ERROR { 201 };
59 constexpr int32_t CAST_INPUT_DEVICEID { 0xAAAAAAFF };
60 constexpr int32_t CAST_SCREEN_DEVICEID { 0xAAAAAAFE };
61 constexpr int32_t ANGLE_90 { 90 };
62 constexpr int32_t ANGLE_360 { 360 };
63 constexpr int32_t ERR_DEVICE_NOT_EXIST { 3900002 };
64 constexpr int32_t ERR_NON_INPUT_APPLICATION { 3900003 };
65 constexpr int32_t SIMULATE_EVENT_START_ID { 10000 };
66 constexpr float MM_TO_INCH { 25.4f };
67 constexpr int32_t SCREEN_DIAGONAL_0 { 0 };
68 constexpr int32_t SCREEN_DIAGONAL_8 { 8 };
69 constexpr int32_t SCREEN_DIAGONAL_18 { 18 };
70 constexpr int32_t SCREEN_DIAGONAL_27 { 27 };
71 constexpr int32_t SCREEN_DIAGONAL_55 { 55 };
72 constexpr float FACTOR_0 { 1.0f };
73 constexpr float FACTOR_8 { 0.7f };
74 constexpr float FACTOR_18 { 1.0f };
75 constexpr float FACTOR_27 { 1.2f };
76 constexpr float FACTOR_55 { 1.6f };
77 constexpr float FACTOR_MAX { 2.4f };
78 } // namespace
79 
Init(UDSServer & udsServer)80 void ServerMsgHandler::Init(UDSServer &udsServer)
81 {
82     udsServer_ = &udsServer;
83     MsgCallback funs[] = {
84         {MmiMessageId::DISPLAY_INFO, [this] (SessionPtr sess, NetPacket &pkt) {
85             return this->OnDisplayInfo(sess, pkt); }},
86         {MmiMessageId::WINDOW_INFO, [this] (SessionPtr sess, NetPacket &pkt) {
87             return this->OnWindowGroupInfo(sess, pkt); }},
88         {MmiMessageId::WINDOW_STATE_ERROR_CALLBACK, [this] (SessionPtr sess, NetPacket &pkt) {
89             return this->RegisterWindowStateErrorCallback(sess, pkt); }},
90 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
91         {MmiMessageId::SCINFO_CONFIG, [this] (SessionPtr sess, NetPacket &pkt) {
92             return this->OnEnhanceConfig(sess, pkt); }},
93 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
94 
95     };
96     for (auto &it : funs) {
97         if (!RegistrationEvent(it)) {
98             MMI_HILOGW("Failed to register event errCode:%{public}d", EVENT_REG_FAIL);
99             continue;
100         }
101     }
102     AUTHORIZE_HELPER->Init(clientDeathHandler_);
103 }
104 
OnMsgHandler(SessionPtr sess,NetPacket & pkt)105 void ServerMsgHandler::OnMsgHandler(SessionPtr sess, NetPacket& pkt)
106 {
107     CHKPV(sess);
108     auto id = pkt.GetMsgId();
109     TimeCostChk chk("ServerMsgHandler::OnMsgHandler", "overtime 300(us)", MAX_OVER_TIME, id);
110     BytraceAdapter::StartSocketHandle(static_cast<int32_t>(id));
111     auto callback = GetMsgCallback(id);
112     if (callback == nullptr) {
113         MMI_HILOGE("Unknown msg id:%{public}d,errCode:%{public}d", id, UNKNOWN_MSG_ID);
114         return;
115     }
116     auto ret = (*callback)(sess, pkt);
117     BytraceAdapter::StopSocketHandle();
118     if (ret < 0) {
119         MMI_HILOGE("Msg handling failed. id:%{public}d,errCode:%{public}d", id, ret);
120     }
121 }
122 
123 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent,int32_t pid,bool isNativeInject)124 int32_t ServerMsgHandler::OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject)
125 {
126     CALL_DEBUG_ENTER;
127     CHKPR(keyEvent, ERROR_NULL_POINTER);
128     LogTracer lt(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
129     if (isNativeInject) {
130         int32_t checkReturn = NativeInjectCheck(pid);
131         if (checkReturn != RET_OK) {
132             return checkReturn;
133         }
134     }
135     keyEvent->SetKeyIntention(KeyItemsTransKeyIntention(keyEvent->GetKeyItems()));
136     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
137     CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
138     inputEventNormalizeHandler->HandleKeyEvent(keyEvent);
139     if (EventLogHelper::IsBetaVersion() && !keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
140         MMI_HILOGD("Inject keyCode:%{private}d, action:%{public}d", keyEvent->GetKeyCode(), keyEvent->GetKeyAction());
141     } else {
142         MMI_HILOGD("Inject keyCode:%{private}d, action:%{public}d", keyEvent->GetKeyCode(), keyEvent->GetKeyAction());
143     }
144     return RET_OK;
145 }
146 
OnGetFunctionKeyState(int32_t funcKey,bool & state)147 int32_t ServerMsgHandler::OnGetFunctionKeyState(int32_t funcKey, bool &state)
148 {
149     CALL_INFO_TRACE;
150     std::vector<struct libinput_device*> input_device;
151     INPUT_DEV_MGR->GetMultiKeyboardDevice(input_device);
152     if (input_device.size() == 0) {
153         MMI_HILOGW("No keyboard device is currently available");
154         return ERR_DEVICE_NOT_EXIST;
155     }
156     const auto &keyEvent = KeyEventHdr->GetKeyEvent();
157     CHKPR(keyEvent, ERROR_NULL_POINTER);
158     state = keyEvent->GetFunctionKey(funcKey);
159     MMI_HILOGD("Get the function key:%{public}d status as %{public}s", funcKey, state ? "open" : "close");
160     return RET_OK;
161 }
162 
OnSetFunctionKeyState(int32_t pid,int32_t funcKey,bool enable)163 int32_t ServerMsgHandler::OnSetFunctionKeyState(int32_t pid, int32_t funcKey, bool enable)
164 {
165     CALL_INFO_TRACE;
166     AppExecFwk::RunningProcessInfo processInfo;
167     auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
168     CHKPR(appMgrClient, ERROR_NULL_POINTER);
169     auto begin = std::chrono::high_resolution_clock::now();
170     appMgrClient->GetRunningProcessInfoByPid(pid, processInfo);
171     auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
172         std::chrono::high_resolution_clock::now() - begin).count();
173 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
174     DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::GET_RUNNING_PROCESS_INFO_BY_PID, durationMS);
175 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
176     if (processInfo.extensionType_ != AppExecFwk::ExtensionAbilityType::INPUTMETHOD) {
177         MMI_HILOGW("It is prohibited for non-input applications");
178         return ERR_NON_INPUT_APPLICATION;
179     }
180     std::vector<struct libinput_device*> input_device;
181     int32_t DeviceId = -1;
182     INPUT_DEV_MGR->GetMultiKeyboardDevice(input_device);
183     if (input_device.size() == 0) {
184         MMI_HILOGW("No keyboard device is currently available");
185         return ERR_DEVICE_NOT_EXIST;
186     }
187     auto keyEvent = KeyEventHdr->GetKeyEvent();
188     CHKPR(keyEvent, ERROR_NULL_POINTER);
189     bool checkState = keyEvent->GetFunctionKey(funcKey);
190     if (checkState == enable) {
191         MMI_HILOGE("Current device no need to set up");
192         return RET_OK;
193     }
194     for (auto it = input_device.begin(); it != input_device.end(); ++it) {
195         auto device = (*it);
196         DeviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
197         if (LibinputAdapter::DeviceLedUpdate(device, funcKey, enable) != RET_OK) {
198             MMI_HILOGE("Failed to set the keyboard led, device id %{public}d", DeviceId);
199         }
200         int32_t state = libinput_get_funckey_state(device, funcKey);
201         if (state != enable) {
202             MMI_HILOGE("Failed to enable the function key, device id %{public}d", DeviceId);
203         }
204     }
205     int32_t ret = keyEvent->SetFunctionKey(funcKey, enable);
206     if (ret != funcKey) {
207         MMI_HILOGE("Failed to enable the function key");
208         return RET_ERR;
209     }
210     MMI_HILOGD("Update function key:%{public}d succeed", funcKey);
211     return RET_OK;
212 }
213 #endif // OHOS_BUILD_ENABLE_KEYBOARD
214 
215 #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)216 int32_t ServerMsgHandler::OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
217     bool isNativeInject, bool isShell)
218 {
219     CALL_DEBUG_ENTER;
220     CHKPR(pointerEvent, ERROR_NULL_POINTER);
221     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
222     if (isNativeInject) {
223         int32_t checkReturn = NativeInjectCheck(pid);
224         if (checkReturn != RET_OK) {
225             return checkReturn;
226         }
227     }
228     return OnInjectPointerEventExt(pointerEvent, isShell);
229 }
230 
OnInjectTouchPadEvent(const std::shared_ptr<PointerEvent> pointerEvent,int32_t pid,const TouchpadCDG & touchpadCDG,bool isNativeInject,bool isShell)231 int32_t ServerMsgHandler::OnInjectTouchPadEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
232     const TouchpadCDG &touchpadCDG, bool isNativeInject, bool isShell)
233 {
234     CALL_DEBUG_ENTER;
235     CHKPR(pointerEvent, ERROR_NULL_POINTER);
236     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
237     if (isNativeInject) {
238         int32_t checkReturn = NativeInjectCheck(pid);
239         if (checkReturn != RET_OK) {
240             return checkReturn;
241         }
242     }
243     return OnInjectTouchPadEventExt(pointerEvent, touchpadCDG, isShell);
244 }
245 
IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent)246 bool ServerMsgHandler::IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent)
247 {
248     return pointerEvent->GetZOrder() > 0;
249 }
250 
OnInjectTouchPadEventExt(const std::shared_ptr<PointerEvent> pointerEvent,const TouchpadCDG & touchpadCDG,bool isShell)251 int32_t ServerMsgHandler::OnInjectTouchPadEventExt(const std::shared_ptr<PointerEvent> pointerEvent,
252     const TouchpadCDG &touchpadCDG, bool isShell)
253 {
254     CALL_DEBUG_ENTER;
255     CHKPR(pointerEvent, ERROR_NULL_POINTER);
256     EndLogTraceId(pointerEvent->GetId());
257     pointerEvent->UpdateId();
258     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
259     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
260     CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
261     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
262 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
263         int32_t ret = AccelerateMotionTouchpad(pointerEvent, touchpadCDG);
264         if (ret != RET_OK) {
265             MMI_HILOGE("Failed to accelerate motion, error:%{public}d", ret);
266             return ret;
267         }
268         UpdatePointerEvent(pointerEvent);
269         inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
270         CHKPR(pointerEvent, ERROR_NULL_POINTER);
271         pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY);
272         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 || OHOS_BUILD_ENABLE_TOUCH
280     } else {
281             MMI_HILOGW("Source types are not Touchpad, source:%{public}d", pointerEvent->GetSourceType());
282     }
283     return SaveTargetWindowId(pointerEvent, isShell);
284 }
285 
DealGesturePointers(std::shared_ptr<PointerEvent> pointerEvent)286 void ServerMsgHandler::DealGesturePointers(std::shared_ptr<PointerEvent> pointerEvent)
287 {
288     MMI_HILOGI("Check : current PointerEvent's info :Id=>%{public}d, pointerId=>%{public}d",
289         pointerEvent->GetId(), pointerEvent->GetPointerId());
290     std::shared_ptr<PointerEvent> touchEvent = WIN_MGR->GetLastPointerEventForGesture();
291     if (touchEvent != nullptr) {
292         std::list<PointerEvent::PointerItem> listPtItems = touchEvent->GetAllPointerItems();
293         for (auto &item : listPtItems) {
294             MMI_HILOGI("Check : current Item : pointerId=>%{public}d, OriginPointerId=>%{public}d",
295                 item.GetPointerId(), item.GetOriginPointerId());
296             if ((item.GetPointerId() % SIMULATE_EVENT_START_ID) !=
297                 (pointerEvent->GetPointerId() % SIMULATE_EVENT_START_ID) && item.IsPressed()) {
298                 pointerEvent->AddPointerItem(item);
299                 MMI_HILOGI("Check : add Item : pointerId=>%{public}d, OriginPointerId=>%{public}d",
300                     item.GetPointerId(), item.GetOriginPointerId());
301             }
302         }
303     }
304 }
305 
OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent,bool isShell)306 int32_t ServerMsgHandler::OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, bool isShell)
307 {
308     CALL_DEBUG_ENTER;
309     CHKPR(pointerEvent, ERROR_NULL_POINTER);
310     EndLogTraceId(pointerEvent->GetId());
311     pointerEvent->UpdateId();
312     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
313     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
314     CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
315     switch (pointerEvent->GetSourceType()) {
316         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
317 #ifdef OHOS_BUILD_ENABLE_TOUCH
318             if (!FixTargetWindowId(pointerEvent, pointerEvent->GetPointerAction(), isShell)) {
319                 return RET_ERR;
320             }
321             DealGesturePointers(pointerEvent);
322             MMI_HILOGI("Check : prepare to send inject pointer event");
323             inputEventNormalizeHandler->HandleTouchEvent(pointerEvent);
324             if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) &&
325                 !(IsCastInject(pointerEvent->GetDeviceId())) &&
326                 !IsNavigationWindowInjectEvent(pointerEvent)) {
327 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
328                 TOUCH_DRAWING_MGR->TouchDrawHandler(pointerEvent);
329 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
330             }
331 #endif // OHOS_BUILD_ENABLE_TOUCH
332             break;
333         }
334         case PointerEvent::SOURCE_TYPE_MOUSE:
335 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
336         case PointerEvent::SOURCE_TYPE_JOYSTICK:
337 #endif // OHOS_BUILD_ENABLE_JOYSTICK
338         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
339 #ifdef OHOS_BUILD_ENABLE_POINTER
340             int32_t ret = AccelerateMotion(pointerEvent);
341             if (ret != RET_OK) {
342                 MMI_HILOGE("Failed to accelerate motion, error:%{public}d", ret);
343                 return ret;
344             }
345             UpdatePointerEvent(pointerEvent);
346             inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
347             CHKPR(pointerEvent, ERROR_NULL_POINTER);
348             if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
349                 break;
350             } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER)) {
351                 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
352             } else if (((pointerEvent->GetPointerAction() < PointerEvent::POINTER_ACTION_PULL_DOWN) ||
353                 (pointerEvent->GetPointerAction() > PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW)) &&
354                 !IPointerDrawingManager::GetInstance()->IsPointerVisible()) {
355                 IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), true, 0, false);
356             }
357 #endif // OHOS_BUILD_ENABLE_POINTER
358             break;
359         }
360         default: {
361             MMI_HILOGW("Source type is unknown, source:%{public}d", pointerEvent->GetSourceType());
362             break;
363         }
364     }
365     return SaveTargetWindowId(pointerEvent, isShell);
366 }
367 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
368 
369 #ifdef OHOS_BUILD_ENABLE_POINTER
ScreenFactor(const int32_t diagonalInch)370 float ServerMsgHandler::ScreenFactor(const int32_t diagonalInch)
371 {
372     if (diagonalInch <= SCREEN_DIAGONAL_0) {
373         return FACTOR_0;
374     } else if (diagonalInch < SCREEN_DIAGONAL_8) {
375         return FACTOR_8;
376     } else if (diagonalInch < SCREEN_DIAGONAL_18) {
377         return FACTOR_18;
378     } else if (diagonalInch < SCREEN_DIAGONAL_27) {
379         return FACTOR_27;
380     } else if (diagonalInch < SCREEN_DIAGONAL_55) {
381         return FACTOR_55;
382     } else {
383         return FACTOR_MAX;
384     }
385 }
386 
AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent)387 int32_t ServerMsgHandler::AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent)
388 {
389     if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_RAW_POINTER_MOVEMENT) ||
390         (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE) ||
391         ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE) &&
392         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE) &&
393         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_BUTTON_DOWN))) {
394             return RET_OK;
395     }
396     PointerEvent::PointerItem pointerItem {};
397     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
398         MMI_HILOGE("Pointer event is corrupted");
399         return RET_ERR;
400     }
401     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
402     if (cursorPos.displayId < 0) {
403         MMI_HILOGE("No display");
404         return RET_ERR;
405     }
406     Offset offset {
407         .dx = pointerItem.GetRawDx(),
408         .dy = pointerItem.GetRawDy(),
409     };
410     auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos.displayId);
411     CHKPR(displayInfo, ERROR_NULL_POINTER);
412 #ifndef OHOS_BUILD_EMULATOR
413     Direction displayDirection = static_cast<Direction>((
414         ((displayInfo->direction - displayInfo->displayDirection) * ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
415 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
416         if (WIN_MGR->IsSupported()) {
417             direction = displayInfo->direction;
418         }
419 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
420     CalculateOffset(displayDirection, offset);
421 #endif // OHOS_BUILD_EMULATOR
422     int32_t ret = RET_OK;
423     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_TOUCHPAD_POINTER) &&
424         pointerEvent->HasFlag(InputEvent::EVENT_FLAG_VIRTUAL_TOUCHPAD_POINTER)) {
425         ret = HandleMotionAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(),
426             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
427             MouseTransformProcessor::GetTouchpadSpeed(), static_cast<int32_t>(DeviceType::DEVICE_FOLD_PC_VIRT));
428     } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_TOUCHPAD_POINTER)) {
429         ret = HandleMotionAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(),
430             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
431             MouseTransformProcessor::GetTouchpadSpeed(), static_cast<int32_t>(DeviceType::DEVICE_PC));
432     } else {
433         uint64_t deltaTime = 0;
434 #ifdef OHOS_BUILD_MOUSE_REPORTING_RATE
435         static uint64_t preTime = -1;
436         uint64_t currentTime = static_cast<uint64_t>(pointerEvent->GetActionTime());
437         preTime = fmin(preTime, currentTime);
438         deltaTime = (currentTime - preTime);
439         preTime = currentTime;
440 #endif // OHOS_BUILD_MOUSE_REPORTING_RATE
441         if (displayInfo->ppi != 0) {
442             int32_t diagonalMm = static_cast<int32_t>(sqrt((displayInfo->physicalWidth * displayInfo->physicalWidth) +
443             (displayInfo->physicalHeight * displayInfo->physicalHeight)));
444             int32_t diagonalInch = static_cast<int32_t>(diagonalMm / MM_TO_INCH);
445             float factor = ScreenFactor(diagonalInch);
446             ret = HandleMotionDynamicAccelerateMouse(&offset, WIN_MGR->GetMouseIsCaptureMode(),
447             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y, MouseTransformProcessor::GetPointerSpeed(),
448             deltaTime, static_cast<double>(displayInfo->ppi), static_cast<double>(factor));
449         } else {
450             ret = HandleMotionAccelerateMouse(&offset, WIN_MGR->GetMouseIsCaptureMode(),
451             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
452             MouseTransformProcessor::GetPointerSpeed(), static_cast<int32_t>(DeviceType::DEVICE_PC));
453         }
454     }
455     if (ret != RET_OK) {
456         MMI_HILOGE("Failed to accelerate pointer motion, error:%{public}d", ret);
457         return ret;
458     }
459     WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
460     if (EventLogHelper::IsBetaVersion() && !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
461         MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
462             cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
463     } else {
464         MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
465             cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
466     }
467     return RET_OK;
468 }
469 
AccelerateMotionTouchpad(std::shared_ptr<PointerEvent> pointerEvent,const TouchpadCDG & touchpadCDG)470 int32_t ServerMsgHandler::AccelerateMotionTouchpad(std::shared_ptr<PointerEvent> pointerEvent,
471     const TouchpadCDG &touchpadCDG)
472 {
473     PointerEvent::PointerItem pointerItem {};
474     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
475         MMI_HILOGE("Pointer event is corrupted");
476         return RET_ERR;
477     }
478     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
479     if (cursorPos.displayId < 0) {
480         MMI_HILOGE("No display");
481         return RET_ERR;
482     }
483     Offset offset {
484         .dx = pointerItem.GetRawDx(),
485         .dy = pointerItem.GetRawDy(),
486     };
487     auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos.displayId);
488     CHKPR(displayInfo, ERROR_NULL_POINTER);
489 #ifndef OHOS_BUILD_EMULATOR
490     Direction displayDirection = static_cast<Direction>((
491         ((displayInfo->direction - displayInfo->displayDirection) * ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
492     CalculateOffset(displayDirection, offset);
493 #endif // OHOS_BUILD_EMULATOR
494     int32_t ret = RET_OK;
495 
496 #ifdef OHOS_BUILD_MOUSE_REPORTING_RATE
497     MMI_HILOGE("Hidumper before HandleMotionDynamicAccelerateTouchpad");
498     static uint64_t preTime = -1;
499     uint64_t currentTime = static_cast<uint64_t>(pointerEvent->GetActionTime());
500     preTime = fmin(preTime, currentTime);
501     uint64_t deltaTime = (currentTime - preTime);
502     MMI_HILOGE("DeltaTime before HandleMotionDynamicAccelerateTouchpad: %{public}PRId64 ms", deltaTime);
503 
504     double displaySize = sqrt(pow(displayInfo->width, 2) + pow(displayInfo->height, 2));
505     double touchpadSize = touchpadCDG.size;
506     double touchpadPPi = touchpadCDG.ppi;
507     int32_t touchpadSpeed = touchpadCDG.speed;
508     if (touchpadSize <= 0 || touchpadPPi <= 0 || touchpadSpeed <= 0) {
509         MMI_HILOGE("touchpadSize, touchpadPPi or touchpadSpeed are invalid,
510             touchpadSize:%{public}lf, touchpadPPi:%{public}lf, touchpadSpeed:%{public}d",
511             touchpadSize, touchpadPPi, touchpadSpeed);
512         return RET_ERR;
513     }
514     ret = HandleMotionDynamicAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(), &cursorPos.cursorPos.x,
515         &cursorPos.cursorPos.y, touchpadSpeed, displaySize, touchpadSize, touchpadPPi);
516 
517     MMI_HILOGE("DeltaTime after HandleMotionDynamicAccelerateTouchpad: %{public}PRId64 ms", deltaTime);
518     MMI_HILOGE("Hidumper after HandleMotionDynamicAccelerateTouchpad");
519     preTime = currentTime;
520 #else
521     ret = HandleMotionAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(),
522         &cursorPos.cursorPos.x, &cursorPos.cursorPos.y,
523         MouseTransformProcessor::GetTouchpadSpeed(), static_cast<int32_t>(DeviceType::DEVICE_PC));
524 #endif // OHOS_BUILD_MOUSE_REPORTING_RATE
525     if (ret != RET_OK) {
526         MMI_HILOGE("Failed to accelerate pointer motion, error:%{public}d", ret);
527         return ret;
528     }
529     WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
530     if (EventLogHelper::IsBetaVersion() && !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
531         MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
532             cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
533     } else {
534         MMI_HILOGD("Cursor move to (x:%.2f, y:%.2f, DisplayId:%d)",
535             cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
536     }
537     return RET_OK;
538 }
539 
CalculateOffset(Direction direction,Offset & offset)540 void ServerMsgHandler::CalculateOffset(Direction direction, Offset &offset)
541 {
542     std::negate<double> neg;
543     if (direction == DIRECTION90) {
544         double tmp = offset.dx;
545         offset.dx = offset.dy;
546         offset.dy = neg(tmp);
547     } else if (direction == DIRECTION180) {
548         offset.dx = neg(offset.dx);
549         offset.dy = neg(offset.dy);
550     } else if (direction == DIRECTION270) {
551         double tmp = offset.dx;
552         offset.dx = neg(offset.dy);
553         offset.dy = tmp;
554     }
555 }
556 #endif // OHOS_BUILD_ENABLE_POINTER
557 
558 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent)559 void ServerMsgHandler::UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
560 {
561     if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_RAW_POINTER_MOVEMENT) ||
562         (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE)) {
563         return;
564     }
565     PointerEvent::PointerItem pointerItem {};
566     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
567         MMI_HILOGE("Pointer event is corrupted");
568         return;
569     }
570     auto mouseInfo = WIN_MGR->GetMouseInfo();
571     pointerItem.SetDisplayX(mouseInfo.physicalX);
572     pointerItem.SetDisplayY(mouseInfo.physicalY);
573     pointerEvent->UpdatePointerItem(pointerEvent->GetPointerId(), pointerItem);
574     pointerEvent->SetTargetDisplayId(mouseInfo.displayId);
575 }
576 
SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,bool isShell)577 int32_t ServerMsgHandler::SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell)
578 {
579     CHKPR(pointerEvent, ERROR_NULL_POINTER);
580     if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
581         (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
582         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER)) {
583         int32_t pointerId = pointerEvent->GetPointerId();
584         PointerEvent::PointerItem pointerItem;
585         if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
586             MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
587             return RET_ERR;
588         }
589         int32_t targetWindowId = pointerEvent->GetTargetWindowId();
590         if (isShell) {
591             shellTargetWindowIds_[pointerId] = targetWindowId;
592         } else if (IsCastInject(pointerEvent->GetDeviceId()) && (pointerEvent->GetZOrder() > 0)) {
593             castTargetWindowIds_[pointerId] = targetWindowId;
594         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
595             accessTargetWindowIds_[pointerId] = targetWindowId;
596         } else {
597             nativeTargetWindowIds_[pointerId] = targetWindowId;
598         }
599     }
600     if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
601         (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
602         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT)) {
603         int32_t pointerId = pointerEvent->GetPointerId();
604         if (isShell) {
605             shellTargetWindowIds_.erase(pointerId);
606         } else if (IsCastInject(pointerEvent->GetDeviceId()) && (pointerEvent->GetZOrder() > 0)) {
607             castTargetWindowIds_.erase(pointerId);
608         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
609             accessTargetWindowIds_.erase(pointerId);
610         } else {
611             nativeTargetWindowIds_.erase(pointerId);
612         }
613     }
614     return RET_OK;
615 }
616 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
617 
618 #ifdef OHOS_BUILD_ENABLE_TOUCH
FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,int32_t action,bool isShell)619 bool ServerMsgHandler::FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,
620     int32_t action, bool isShell)
621 {
622     int32_t targetWindowId = -1;
623     int32_t pointerId = pointerEvent->GetPointerId();
624     PointerEvent::PointerItem pointerItem;
625     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
626         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
627         return false;
628     }
629     if (isShell) {
630         auto iter = shellTargetWindowIds_.find(pointerEvent->GetPointerId());
631         if (iter != shellTargetWindowIds_.end()) {
632             targetWindowId = iter->second;
633         }
634     } else if ((IsCastInject(pointerEvent->GetDeviceId())) && (pointerEvent->GetZOrder() > 0)) {
635         pointerEvent->RemovePointerItem(pointerId);
636         pointerId += CAST_POINTER_ID;
637         pointerItem.SetPointerId(pointerId);
638         pointerEvent->UpdatePointerItem(pointerId, pointerItem);
639         pointerEvent->SetPointerId(pointerId);
640         auto iter = castTargetWindowIds_.find(pointerEvent->GetPointerId());
641         if (iter != castTargetWindowIds_.end()) {
642             targetWindowId = iter->second;
643         }
644     } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
645         auto iter = accessTargetWindowIds_.find(pointerEvent->GetPointerId());
646         if (iter != accessTargetWindowIds_.end()) {
647             targetWindowId = iter->second;
648         }
649     } else {
650         pointerEvent->RemovePointerItem(pointerId);
651         pointerId += DEFAULT_POINTER_ID;
652         pointerItem.SetPointerId(pointerId);
653         pointerEvent->UpdatePointerItem(pointerId, pointerItem);
654         pointerEvent->SetPointerId(pointerId);
655         auto iter = nativeTargetWindowIds_.find(pointerEvent->GetPointerId());
656         if (iter != nativeTargetWindowIds_.end()) {
657             targetWindowId = iter->second;
658         }
659     }
660     MMI_HILOGD("TargetWindowId:%{public}d %{public}d", pointerEvent->GetTargetWindowId(), targetWindowId);
661     return UpdateTouchEvent(pointerEvent, action, targetWindowId);
662 }
663 
UpdateTouchEvent(std::shared_ptr<PointerEvent> pointerEvent,int32_t action,int32_t targetWindowId)664 bool ServerMsgHandler::UpdateTouchEvent(std::shared_ptr<PointerEvent> pointerEvent,
665     int32_t action, int32_t targetWindowId)
666 {
667     int32_t pointerId = pointerEvent->GetPointerId();
668     PointerEvent::PointerItem pointerItem;
669     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
670         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
671         return false;
672     }
673     if (action == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
674         action == PointerEvent::POINTER_ACTION_DOWN || targetWindowId < 0) {
675         MMI_HILOGD("Down event or targetWindowId less 0 is not need fix window id");
676         return true;
677     }
678     auto pointerIds = pointerEvent->GetPointerIds();
679     if (pointerIds.empty()) {
680         MMI_HILOGE("GetPointerIds is empty");
681         return false;
682     }
683 
684     pointerEvent->SetTargetWindowId(targetWindowId);
685     pointerItem.SetTargetWindowId(targetWindowId);
686     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
687     return true;
688 }
689 #endif // OHOS_BUILD_ENABLE_TOUCH
690 
OnUiExtentionWindowInfo(NetPacket & pkt,WindowInfo & info)691 int32_t ServerMsgHandler::OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info)
692 {
693     uint32_t num = 0;
694     pkt >> num;
695     if (pkt.ChkRWError()) {
696         MMI_HILOGE("Packet read display info failed");
697         return RET_ERR;
698     }
699     for (uint32_t i = 0; i < num; i++) {
700         WindowInfo extensionInfo;
701         pkt >> extensionInfo.id >> extensionInfo.pid >> extensionInfo.uid >> extensionInfo.area
702             >> extensionInfo.defaultHotAreas >> extensionInfo.pointerHotAreas >> extensionInfo.agentWindowId
703             >> extensionInfo.flags >> extensionInfo.action >> extensionInfo.displayId >> extensionInfo.zOrder
704             >> extensionInfo.pointerChangeAreas >> extensionInfo.transform >> extensionInfo.windowInputType
705             >> extensionInfo.privacyMode >> extensionInfo.windowType >> extensionInfo.privacyUIFlag
706             >> extensionInfo.rectChangeBySystem;
707         info.uiExtentionWindowInfo.push_back(extensionInfo);
708         if (pkt.ChkRWError()) {
709             MMI_HILOGE("Packet read extention window info failed");
710             return RET_ERR;
711         }
712     }
713     return RET_OK;
714 }
715 
ReadDisplayInfo(NetPacket & pkt,DisplayGroupInfo & displayGroupInfo)716 int32_t ServerMsgHandler::ReadDisplayInfo(NetPacket &pkt, DisplayGroupInfo &displayGroupInfo)
717 {
718     uint32_t num = 0;
719     pkt >> num;
720     for (uint32_t i = 0; i < num; i++) {
721         DisplayInfo info;
722         pkt >> info.id >> info.x >> info.y >> info.width >> info.height >> info.dpi >> info.name
723             >> info.uniq >> info.direction >> info.displayDirection >> info.displayMode >> info.transform >> info.ppi
724             >> info.offsetX >> info.offsetY >> info.isCurrentOffScreenRendering >> info.screenRealWidth
725             >> info.screenRealHeight >> info.screenRealPPI >> info.screenRealDPI >> info.screenCombination
726             >> info.validWidth >> info.validHeight >> info.fixedDirection
727             >> info.physicalWidth >> info.physicalHeight >> info.scalePercent >> info.expandHeight
728             >> info.oneHandX >> info.oneHandY;
729 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
730         pkt >> info.pointerActiveWidth >> info.pointerActiveHeight;
731 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
732         displayGroupInfo.displaysInfo.push_back(info);
733         if (pkt.ChkRWError()) {
734             MMI_HILOGE("Packet read display info failed");
735             return RET_ERR;
736         }
737     }
738     if (pkt.ChkRWError()) {
739         MMI_HILOGE("Packet read display info failed");
740         return RET_ERR;
741     }
742     return RET_OK;
743 }
744 
IsCastInject(int32_t deviceid)745 bool ServerMsgHandler::IsCastInject(int32_t deviceid)
746 {
747     return (deviceid == CAST_INPUT_DEVICEID || deviceid == CAST_SCREEN_DEVICEID);
748 }
749 
OnDisplayInfo(SessionPtr sess,NetPacket & pkt)750 int32_t ServerMsgHandler::OnDisplayInfo(SessionPtr sess, NetPacket &pkt)
751 {
752     CALL_DEBUG_ENTER;
753     CHKPR(sess, ERROR_NULL_POINTER);
754     int32_t tokenType = sess->GetTokenType();
755     if (tokenType != TokenType::TOKEN_NATIVE && tokenType != TokenType::TOKEN_SHELL &&
756         tokenType !=TokenType::TOKEN_SYSTEM_HAP) {
757         MMI_HILOGW("Not native or systemapp skip, pid:%{public}d tokenType:%{public}d", sess->GetPid(), tokenType);
758         return RET_ERR;
759     }
760     DisplayGroupInfo displayGroupInfo;
761     pkt >> displayGroupInfo.width >> displayGroupInfo.height >>
762         displayGroupInfo.focusWindowId >> displayGroupInfo.currentUserId;
763     uint32_t num = 0;
764     pkt >> num;
765     if (pkt.ChkRWError()) {
766         MMI_HILOGE("Packet read display info failed");
767         return RET_ERR;
768     }
769     for (uint32_t i = 0; i < num; i++) {
770         WindowInfo info;
771         int32_t byteCount = 0;
772         pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
773             >> info.pointerHotAreas >> info.agentWindowId >> info.flags >> info.action
774             >> info.displayId >> info.zOrder >> info.pointerChangeAreas >> info.transform
775             >> info.windowInputType >> info.privacyMode >> info.windowType >> byteCount;
776 
777         OnUiExtentionWindowInfo(pkt, info);
778         pkt >> info.rectChangeBySystem;
779         displayGroupInfo.windowsInfo.push_back(info);
780         if (pkt.ChkRWError()) {
781             MMI_HILOGE("Packet read display info failed");
782             return RET_ERR;
783         }
784     }
785     if (ReadDisplayInfo(pkt, displayGroupInfo) != RET_OK) {
786         return RET_ERR;
787     }
788     WIN_MGR->UpdateDisplayInfoExtIfNeed(displayGroupInfo, true);
789     return RET_OK;
790 }
791 
OnWindowGroupInfo(SessionPtr sess,NetPacket & pkt)792 int32_t ServerMsgHandler::OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt)
793 {
794     CALL_DEBUG_ENTER;
795     CHKPR(sess, ERROR_NULL_POINTER);
796     int32_t tokenType = sess->GetTokenType();
797     if (tokenType != TokenType::TOKEN_NATIVE && tokenType != TokenType::TOKEN_SHELL &&
798         tokenType !=TokenType::TOKEN_SYSTEM_HAP) {
799         MMI_HILOGW("Not native or systemapp skip, pid:%{public}d tokenType:%{public}d", sess->GetPid(), tokenType);
800         return RET_ERR;
801     }
802     WindowGroupInfo windowGroupInfo;
803     pkt >> windowGroupInfo.focusWindowId >> windowGroupInfo.displayId;
804     uint32_t num = 0;
805     pkt >> num;
806     if (pkt.ChkRWError()) {
807         MMI_HILOGE("Packet read window group info failed");
808         return RET_ERR;
809     }
810     for (uint32_t i = 0; i < num; i++) {
811         WindowInfo info;
812         pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
813             >> info.pointerHotAreas >> info.agentWindowId >> info.flags >> info.action
814             >> info.displayId >> info.zOrder >> info.pointerChangeAreas >> info.transform
815             >> info.windowInputType >> info.privacyMode >> info.windowType;
816         OnUiExtentionWindowInfo(pkt, info);
817         pkt >> info.rectChangeBySystem;
818         windowGroupInfo.windowsInfo.push_back(info);
819         if (pkt.ChkRWError()) {
820             MMI_HILOGE("Packet read display info failed");
821             return RET_ERR;
822         }
823     }
824     WIN_MGR->UpdateWindowInfo(windowGroupInfo);
825     return RET_OK;
826 }
827 
RegisterWindowStateErrorCallback(SessionPtr sess,NetPacket & pkt)828 int32_t ServerMsgHandler::RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt)
829 {
830     CALL_DEBUG_ENTER;
831     CHKPR(sess, ERROR_NULL_POINTER);
832     int32_t tokenType = sess->GetTokenType();
833     if (tokenType != TokenType::TOKEN_NATIVE && tokenType != TokenType::TOKEN_SHELL &&
834         tokenType !=TokenType::TOKEN_SYSTEM_HAP) {
835         MMI_HILOGW("Not native or systemapp skip, pid:%{public}d tokenType:%{public}d", sess->GetPid(), tokenType);
836         return RET_ERR;
837     }
838     int32_t pid = sess->GetPid();
839     WIN_MGR->SetWindowStateNotifyPid(pid);
840     MMI_HILOGI("The pid:%{public}d", pid);
841     return RET_OK;
842 }
843 
844 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
OnEnhanceConfig(SessionPtr sess,NetPacket & pkt)845 int32_t ServerMsgHandler::OnEnhanceConfig(SessionPtr sess, NetPacket &pkt)
846 {
847     CHKPR(sess, ERROR_NULL_POINTER);
848     int32_t userId = sess->GetUid();
849     if (userId != SECURITY_COMPONENT_SERVICE_ID) {
850         MMI_HILOGE("Session is not security component service");
851         return RET_ERR;
852     }
853     uint32_t num = 0;
854     pkt >> num;
855     uint8_t cfg[num];
856     for (uint32_t i = 0; i < num; i++) {
857         pkt >> cfg[i];
858     }
859 
860     if (pkt.ChkRWError()) {
861         MMI_HILOGE("Packet read scinfo config failed");
862         return RET_ERR;
863     }
864     int32_t result = Security::SecurityComponent::SecCompEnhanceKit::SetEnhanceCfg(cfg, num);
865     if (result != 0) {
866         return RET_ERR;
867     }
868     return RET_OK;
869 }
870 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
871 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
OnAddInputHandler(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)872 int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType,
873     HandleEventType eventType, int32_t priority, uint32_t deviceTags)
874 {
875     CHKPR(sess, ERROR_NULL_POINTER);
876     MMI_HILOGD("The handlerType:%{public}d", handlerType);
877 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
878     if (handlerType == InputHandlerType::INTERCEPTOR) {
879         auto interceptorHandler = InputHandler->GetInterceptorHandler();
880         CHKPR(interceptorHandler, ERROR_NULL_POINTER);
881         return interceptorHandler->AddInputHandler(handlerType, eventType, priority, deviceTags, sess);
882     }
883 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
884 #ifdef OHOS_BUILD_ENABLE_MONITOR
885     if (handlerType == InputHandlerType::MONITOR) {
886         auto monitorHandler = InputHandler->GetMonitorHandler();
887         CHKPR(monitorHandler, ERROR_NULL_POINTER);
888         return monitorHandler->AddInputHandler(handlerType, eventType, sess);
889     }
890 #endif // OHOS_BUILD_ENABLE_MONITOR
891     return RET_OK;
892 }
893 
OnRemoveInputHandler(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)894 int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType,
895     HandleEventType eventType, int32_t priority, uint32_t deviceTags)
896 {
897     CHKPR(sess, ERROR_NULL_POINTER);
898     MMI_HILOGD("OnRemoveInputHandler handlerType:%{public}d eventType:%{public}u", handlerType, eventType);
899 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
900     if (handlerType == InputHandlerType::INTERCEPTOR) {
901         auto interceptorHandler = InputHandler->GetInterceptorHandler();
902         CHKPR(interceptorHandler, ERROR_NULL_POINTER);
903         interceptorHandler->RemoveInputHandler(handlerType, eventType, priority, deviceTags, sess);
904     }
905 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
906 #ifdef OHOS_BUILD_ENABLE_MONITOR
907     if (handlerType == InputHandlerType::MONITOR) {
908         auto monitorHandler = InputHandler->GetMonitorHandler();
909         CHKPR(monitorHandler, ERROR_NULL_POINTER);
910         monitorHandler->RemoveInputHandler(handlerType, eventType, sess);
911         ANRMgr->RemoveTimersByType(sess, ANR_MONITOR);
912     }
913 #endif // OHOS_BUILD_ENABLE_MONITOR
914     return RET_OK;
915 }
916 
917 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
918 
OnAddGestureMonitor(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType,TouchGestureType gestureType,int32_t fingers)919 int32_t ServerMsgHandler::OnAddGestureMonitor(SessionPtr sess, InputHandlerType handlerType,
920     HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)
921 {
922 #ifdef OHOS_BUILD_ENABLE_MONITOR
923     if (handlerType == InputHandlerType::MONITOR) {
924         auto monitorHandler = InputHandler->GetMonitorHandler();
925         CHKPR(monitorHandler, ERROR_NULL_POINTER);
926         return monitorHandler->AddInputHandler(handlerType, eventType, sess, gestureType, fingers);
927     }
928 #endif // OHOS_BUILD_ENABLE_MONITOR
929     return RET_OK;
930 }
931 
OnRemoveGestureMonitor(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType,TouchGestureType gestureType,int32_t fingers)932 int32_t ServerMsgHandler::OnRemoveGestureMonitor(SessionPtr sess, InputHandlerType handlerType,
933     HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)
934 {
935 #ifdef OHOS_BUILD_ENABLE_MONITOR
936     if (handlerType == InputHandlerType::MONITOR) {
937         CHKPR(sess, ERROR_NULL_POINTER);
938         auto monitorHandler = InputHandler->GetMonitorHandler();
939         CHKPR(monitorHandler, ERROR_NULL_POINTER);
940         monitorHandler->RemoveInputHandler(handlerType, eventType, sess, gestureType, fingers);
941     }
942 #endif // OHOS_BUILD_ENABLE_MONITOR
943     return RET_OK;
944 }
945 
946 #ifdef OHOS_BUILD_ENABLE_MONITOR
OnMarkConsumed(SessionPtr sess,int32_t eventId)947 int32_t ServerMsgHandler::OnMarkConsumed(SessionPtr sess, int32_t eventId)
948 {
949     CHKPR(sess, ERROR_NULL_POINTER);
950     auto monitorHandler = InputHandler->GetMonitorHandler();
951     CHKPR(monitorHandler, ERROR_NULL_POINTER);
952     monitorHandler->MarkConsumed(eventId, sess);
953     return RET_OK;
954 }
955 #endif // OHOS_BUILD_ENABLE_MONITOR
956 
957 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
OnMoveMouse(int32_t offsetX,int32_t offsetY)958 int32_t ServerMsgHandler::OnMoveMouse(int32_t offsetX, int32_t offsetY)
959 {
960     CALL_DEBUG_ENTER;
961     if (MouseEventHdr->NormalizeMoveMouse(offsetX, offsetY)) {
962         auto pointerEvent = MouseEventHdr->GetPointerEvent();
963         CHKPR(pointerEvent, ERROR_NULL_POINTER);
964         auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
965         CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
966         inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
967         MMI_HILOGD("Mouse movement message processed successfully");
968     }
969     return RET_OK;
970 }
971 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
972 
973 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnSubscribeKeyEvent(IUdsServer * server,int32_t pid,int32_t subscribeId,const std::shared_ptr<KeyOption> option)974 int32_t ServerMsgHandler::OnSubscribeKeyEvent(IUdsServer *server, int32_t pid,
975     int32_t subscribeId, const std::shared_ptr<KeyOption> option)
976 {
977     CALL_DEBUG_ENTER;
978     CHKPR(server, ERROR_NULL_POINTER);
979     auto sess = server->GetSessionByPid(pid);
980     CHKPR(sess, ERROR_NULL_POINTER);
981     auto subscriberHandler = InputHandler->GetSubscriberHandler();
982     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
983     return subscriberHandler->SubscribeKeyEvent(sess, subscribeId, option);
984 }
985 
OnUnsubscribeKeyEvent(IUdsServer * server,int32_t pid,int32_t subscribeId)986 int32_t ServerMsgHandler::OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
987 {
988     CALL_DEBUG_ENTER;
989     CHKPR(server, ERROR_NULL_POINTER);
990     auto sess = server->GetSessionByPid(pid);
991     CHKPR(sess, ERROR_NULL_POINTER);
992     auto subscriberHandler = InputHandler->GetSubscriberHandler();
993     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
994     return subscriberHandler->UnsubscribeKeyEvent(sess, subscribeId);
995 }
996 
OnSubscribeHotkey(IUdsServer * server,int32_t pid,int32_t subscribeId,const std::shared_ptr<KeyOption> option)997 int32_t ServerMsgHandler::OnSubscribeHotkey(IUdsServer *server, int32_t pid,
998     int32_t subscribeId, const std::shared_ptr<KeyOption> option)
999 {
1000 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
1001     CALL_DEBUG_ENTER;
1002     CHKPR(server, ERROR_NULL_POINTER);
1003     auto sess = server->GetSessionByPid(pid);
1004     CHKPR(sess, ERROR_NULL_POINTER);
1005     auto subscriberHandler = InputHandler->GetSubscriberHandler();
1006     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
1007     return subscriberHandler->SubscribeHotkey(sess, subscribeId, option);
1008 #else
1009     MMI_HILOGI("OnSubscribeHotkey function does not support");
1010     return ERROR_UNSUPPORT;
1011 #endif // SHORTCUT_KEY_MANAGER_ENABLED
1012 }
1013 
OnUnsubscribeHotkey(IUdsServer * server,int32_t pid,int32_t subscribeId)1014 int32_t ServerMsgHandler::OnUnsubscribeHotkey(IUdsServer *server, int32_t pid, int32_t subscribeId)
1015 {
1016 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
1017     CALL_DEBUG_ENTER;
1018     CHKPR(server, ERROR_NULL_POINTER);
1019     auto sess = server->GetSessionByPid(pid);
1020     CHKPR(sess, ERROR_NULL_POINTER);
1021     auto subscriberHandler = InputHandler->GetSubscriberHandler();
1022     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
1023     return subscriberHandler->UnsubscribeHotkey(sess, subscribeId);
1024 #else
1025     MMI_HILOGI("OnUnsubscribeHotkey function does not support");
1026     return ERROR_UNSUPPORT;
1027 #endif // SHORTCUT_KEY_MANAGER_ENABLED
1028 }
1029 
SubscribeKeyMonitor(int32_t session,const KeyMonitorOption & keyOption)1030 int32_t ServerMsgHandler::SubscribeKeyMonitor(int32_t session, const KeyMonitorOption &keyOption)
1031 {
1032     KeyMonitorManager::Monitor monitor {
1033         .session_ = session,
1034         .key_ = keyOption.GetKey(),
1035         .action_ = keyOption.GetAction(),
1036         .isRepeat_ = keyOption.IsRepeat(),
1037     };
1038     return KEY_MONITOR_MGR->AddMonitor(monitor);
1039 }
1040 
UnsubscribeKeyMonitor(int32_t session,const KeyMonitorOption & keyOption)1041 int32_t ServerMsgHandler::UnsubscribeKeyMonitor(int32_t session, const KeyMonitorOption &keyOption)
1042 {
1043     KeyMonitorManager::Monitor monitor {
1044         .session_ = session,
1045         .key_ = keyOption.GetKey(),
1046         .action_ = keyOption.GetAction(),
1047         .isRepeat_ = keyOption.IsRepeat(),
1048     };
1049     KEY_MONITOR_MGR->RemoveMonitor(monitor);
1050     return RET_OK;
1051 }
1052 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1053 
1054 #ifdef OHOS_BUILD_ENABLE_SWITCH
OnSubscribeSwitchEvent(IUdsServer * server,int32_t pid,int32_t subscribeId,int32_t switchType)1055 int32_t ServerMsgHandler::OnSubscribeSwitchEvent(
1056     IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType)
1057 {
1058     CALL_DEBUG_ENTER;
1059     CHKPR(server, ERROR_NULL_POINTER);
1060     auto sess = server->GetSessionByPid(pid);
1061     CHKPR(sess, ERROR_NULL_POINTER);
1062     auto subscriberHandler = InputHandler->GetSwitchSubscriberHandler();
1063     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
1064     return subscriberHandler->SubscribeSwitchEvent(sess, subscribeId, switchType);
1065 }
1066 
OnUnsubscribeSwitchEvent(IUdsServer * server,int32_t pid,int32_t subscribeId)1067 int32_t ServerMsgHandler::OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
1068 {
1069     CALL_DEBUG_ENTER;
1070     CHKPR(server, ERROR_NULL_POINTER);
1071     auto sess = server->GetSessionByPid(pid);
1072     CHKPR(sess, ERROR_NULL_POINTER);
1073     auto subscriberHandler = InputHandler->GetSwitchSubscriberHandler();
1074     CHKPR(subscriberHandler, ERROR_NULL_POINTER);
1075     return subscriberHandler->UnsubscribeSwitchEvent(sess, subscribeId);
1076 }
1077 #endif // OHOS_BUILD_ENABLE_SWITCH
1078 
OnSubscribeLongPressEvent(IUdsServer * server,int32_t pid,int32_t subscribeId,const LongPressRequest & longPressRequest)1079 int32_t ServerMsgHandler::OnSubscribeLongPressEvent(IUdsServer *server, int32_t pid, int32_t subscribeId,
1080     const LongPressRequest &longPressRequest)
1081 {
1082     CALL_DEBUG_ENTER;
1083     CHKPR(server, ERROR_NULL_POINTER);
1084     auto sess = server->GetSessionByPid(pid);
1085     CHKPR(sess, ERROR_NULL_POINTER);
1086     return LONG_PRESS_EVENT_HANDLER->SubscribeLongPressEvent(sess, subscribeId, longPressRequest);
1087 }
1088 
OnUnsubscribeLongPressEvent(IUdsServer * server,int32_t pid,int32_t subscribeId)1089 int32_t ServerMsgHandler::OnUnsubscribeLongPressEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
1090 {
1091     CALL_DEBUG_ENTER;
1092     CHKPR(server, ERROR_NULL_POINTER);
1093     auto sess = server->GetSessionByPid(pid);
1094     CHKPR(sess, ERROR_NULL_POINTER);
1095     return LONG_PRESS_EVENT_HANDLER->UnsubscribeLongPressEvent(sess, subscribeId);
1096 }
1097 
1098 #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)1099 int32_t ServerMsgHandler::AddInputEventFilter(sptr<IEventFilter> filter,
1100     int32_t filterId, int32_t priority, uint32_t deviceTags, int32_t clientPid)
1101 {
1102     auto filterHandler = InputHandler->GetFilterHandler();
1103     CHKPR(filterHandler, ERROR_NULL_POINTER);
1104     return filterHandler->AddInputEventFilter(filter, filterId, priority, deviceTags, clientPid);
1105 }
1106 
RemoveInputEventFilter(int32_t clientPid,int32_t filterId)1107 int32_t ServerMsgHandler::RemoveInputEventFilter(int32_t clientPid, int32_t filterId)
1108 {
1109     auto filterHandler = InputHandler->GetFilterHandler();
1110     CHKPR(filterHandler, ERROR_NULL_POINTER);
1111     return filterHandler->RemoveInputEventFilter(clientPid, filterId);
1112 }
1113 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
1114 
1115 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
SetShieldStatus(int32_t shieldMode,bool isShield)1116 int32_t ServerMsgHandler::SetShieldStatus(int32_t shieldMode, bool isShield)
1117 {
1118     return KeyEventHdr->SetShieldStatus(shieldMode, isShield);
1119 }
1120 
GetShieldStatus(int32_t shieldMode,bool & isShield)1121 int32_t ServerMsgHandler::GetShieldStatus(int32_t shieldMode, bool &isShield)
1122 {
1123     return KeyEventHdr->GetShieldStatus(shieldMode, isShield);
1124 }
1125 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1126 
LaunchAbility()1127 void ServerMsgHandler::LaunchAbility()
1128 {
1129     CALL_DEBUG_ENTER;
1130 #ifndef OHOS_BUILD_ENABLE_WATCH
1131     AUTH_DIALOG.ConnectSystemUi();
1132 #endif // OHOS_BUILD_ENABLE_WATCH
1133 }
1134 
OnAuthorize(bool isAuthorize)1135 int32_t ServerMsgHandler::OnAuthorize(bool isAuthorize)
1136 {
1137     CALL_DEBUG_ENTER;
1138     if (isAuthorize) {
1139         auto state = AUTHORIZE_HELPER->GetAuthorizeState();
1140         int32_t authorPid = AUTHORIZE_HELPER->GetAuthorizePid();
1141         MMI_HILOGE("OnAuthorize not has authorizing s:%{public}d, authPid:%{public}d",
1142             state, authorPid);
1143         if (state == AuthorizeState::STATE_UNAUTHORIZE) {
1144             MMI_HILOGE("Current not has authorizing");
1145             return ERR_OK;
1146         }
1147         if (state == AuthorizeState::STATE_UNAUTHORIZE) {
1148             MMI_HILOGE("The injection permission has been granted. authPid:%{public}d ", authorPid);
1149             return ERR_OK;
1150         }
1151         InjectNoticeInfo noticeInfo;
1152         noticeInfo.pid = authorPid;
1153         AddInjectNotice(noticeInfo);
1154         auto result = AUTHORIZE_HELPER->AddAuthorizeProcess(authorPid, [&] (int32_t pid) {
1155                 CloseInjectNotice(pid);
1156         });
1157         if (result != RET_OK) {
1158             MMI_HILOGI("Authorize process failed, pid:%{public}d", authorPid);
1159         }
1160         MMI_HILOGD("Agree to apply injection,pid:%{public}d", authorPid);
1161         return ERR_OK;
1162     }
1163 
1164     auto state = AUTHORIZE_HELPER->GetAuthorizeState();
1165     int32_t curAuthPid = AUTHORIZE_HELPER->GetAuthorizePid();
1166     MMI_HILOGD("Reject application injection,s:%{public}d, authPid:%{public}d",
1167         state, curAuthPid);
1168     if (state != AuthorizeState::STATE_UNAUTHORIZE) {
1169         MMI_HILOGI("Cancel injection right,pid:%{public}d", curAuthPid);
1170         AUTHORIZE_HELPER->CancelAuthorize(curAuthPid);
1171         if (state == AuthorizeState::STATE_SELECTION_AUTHORIZE) {
1172             AUTH_DIALOG.CloseDialog();
1173         } else {
1174             CloseInjectNotice(AUTHORIZE_HELPER->GetAuthorizePid());
1175         }
1176     }
1177     return ERR_OK;
1178 }
1179 
OnCancelInjection(int32_t callPid)1180 int32_t ServerMsgHandler::OnCancelInjection(int32_t callPid)
1181 {
1182     CALL_DEBUG_ENTER;
1183     auto state = AUTHORIZE_HELPER->GetAuthorizeState();
1184     int32_t curAuthPid = AUTHORIZE_HELPER->GetAuthorizePid();
1185     MMI_HILOGD("Cancel application injection,s:%{public}d, authPid:%{public}d",
1186         state, curAuthPid);
1187     if (state != AuthorizeState::STATE_UNAUTHORIZE) {
1188         if (callPid != curAuthPid) {
1189             MMI_HILOGW("Authorized pid not callPid");
1190             return COMMON_PERMISSION_CHECK_ERROR;
1191         }
1192         AUTHORIZE_HELPER->CancelAuthorize(curAuthPid);
1193         if (state == AuthorizeState::STATE_SELECTION_AUTHORIZE) {
1194             AUTH_DIALOG.CloseDialog();
1195         } else {
1196             CloseInjectNotice(AUTHORIZE_HELPER->GetAuthorizePid());
1197         }
1198     }
1199     return ERR_OK;
1200 }
1201 
SetWindowInfo(int32_t infoId,WindowInfo & info)1202 void ServerMsgHandler::SetWindowInfo(int32_t infoId, WindowInfo &info)
1203 {
1204     CALL_DEBUG_ENTER;
1205     if (transparentWins_.find(infoId) == transparentWins_.end()) {
1206         MMI_HILOGE("The infoId is Invalid, infoId:%{public}d", infoId);
1207         return;
1208     }
1209     info.pixelMap = transparentWins_[infoId].get();
1210 }
1211 
SetPixelMapData(int32_t infoId,void * pixelMap)1212 int32_t ServerMsgHandler::SetPixelMapData(int32_t infoId, void *pixelMap) __attribute__((no_sanitize("cfi")))
1213 {
1214     CALL_DEBUG_ENTER;
1215     if (infoId < 0 || pixelMap == nullptr) {
1216         MMI_HILOGE("The infoId is invalid or pixelMap is nullptr");
1217         return ERR_INVALID_VALUE;
1218     }
1219 
1220     WIN_MGR->SetPixelMapData(infoId, pixelMap);
1221     return RET_OK;
1222 }
1223 
InitInjectNoticeSource()1224 bool ServerMsgHandler::InitInjectNoticeSource()
1225 {
1226     CALL_DEBUG_ENTER;
1227     MMI_HILOGD("Init InjectNoticeSource enter");
1228     if (injectNotice_ == nullptr) {
1229         injectNotice_ = std::make_shared<InjectNoticeManager>();
1230     }
1231     MMI_HILOGD("Injectnotice StartNoticeAbility end");
1232     if (!injectNotice_->IsAbilityStart()) {
1233         MMI_HILOGD("Injectnotice StartNoticeAbility begin");
1234         bool isStart = injectNotice_->StartNoticeAbility();
1235         if (!isStart) {
1236             MMI_HILOGE("Injectnotice StartNoticeAbility isStart:%{public}d", isStart);
1237             return false;
1238         }
1239         MMI_HILOGD("Injectnotice StartNoticeAbility end");
1240     }
1241     auto connection = injectNotice_->GetConnection();
1242     CHKPF(connection);
1243     if (!connection->IsConnected()) {
1244         MMI_HILOGD("Injectnotice ConnectNoticeSrv begin");
1245         bool isConnect = injectNotice_->ConnectNoticeSrv();
1246         if (!isConnect) {
1247             MMI_HILOGD("Injectnotice ConnectNoticeSrv isConnect:%{public}d", isConnect);
1248             return false;
1249         }
1250         MMI_HILOGD("Injectnotice ConnectNoticeSrv end");
1251     }
1252     MMI_HILOGD("Injectnotice InitInjectNoticeSource end");
1253     return true;
1254 }
1255 
AddInjectNotice(const InjectNoticeInfo & noticeInfo)1256 bool ServerMsgHandler::AddInjectNotice(const InjectNoticeInfo &noticeInfo)
1257 {
1258     CALL_DEBUG_ENTER;
1259     bool isInit = InitInjectNoticeSource();
1260     if (!isInit) {
1261         MMI_HILOGE("InitinjectNotice_ Source error");
1262         return false;
1263     }
1264     MMI_HILOGD("SendNotice submit begin");
1265     ffrt::submit([this, noticeInfo] {
1266         MMI_HILOGD("SendNotice submit enter");
1267         CHKPV(injectNotice_);
1268         auto pConnect = injectNotice_->GetConnection();
1269         CHKPV(pConnect);
1270         int32_t timeSecond = 0;
1271         while (timeSecond <= SEND_NOTICE_OVERTIME) {
1272             bool isConnect = pConnect->IsConnected();
1273             MMI_HILOGD("SendNotice %{public}d", isConnect);
1274             if (isConnect) {
1275                 MMI_HILOGD("SendNotice begin");
1276                 pConnect->SendNotice(noticeInfo);
1277                 break;
1278             }
1279             timeSecond += 1;
1280             sleep(1);
1281         }
1282         MMI_HILOGD("SendNotice submit leave");
1283     });
1284     return true;
1285 }
1286 
CloseInjectNotice(int32_t pid)1287 bool ServerMsgHandler::CloseInjectNotice(int32_t pid)
1288 {
1289     CALL_DEBUG_ENTER;
1290     bool isInit = InitInjectNoticeSource();
1291     if (!isInit) {
1292         MMI_HILOGE("InitinjectNotice_ Source error");
1293         return false;
1294     }
1295     MMI_HILOGD("CloseNotice submit begin");
1296     InjectNoticeInfo noticeInfo;
1297     noticeInfo.pid = pid;
1298     ffrt::submit([this, noticeInfo] {
1299         MMI_HILOGD("CloseNotice submit enter");
1300         CHKPV(injectNotice_);
1301         auto pConnect = injectNotice_->GetConnection();
1302         CHKPV(pConnect);
1303         int32_t timeSecond = 0;
1304         while (timeSecond <= SEND_NOTICE_OVERTIME) {
1305             bool isConnect = pConnect->IsConnected();
1306             MMI_HILOGD("CloseNotice %{public}d", isConnect);
1307             if (isConnect) {
1308                 MMI_HILOGD("CloseNotice begin");
1309                 pConnect->CancelNotice(noticeInfo);
1310                 break;
1311             }
1312             timeSecond += 1;
1313             sleep(1);
1314         }
1315         MMI_HILOGD("CloseNotice submit leave");
1316     });
1317     return true;
1318 }
1319 
OnTransferBinderClientSrv(const sptr<IRemoteObject> & binderClientObject,int32_t pid)1320 int32_t ServerMsgHandler::OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid)
1321 {
1322     CALL_DEBUG_ENTER;
1323     bool bRet = clientDeathHandler_.RegisterClientDeathRecipient(binderClientObject, pid);
1324     if (!bRet) {
1325         MMI_HILOGE("Failed to registerClientDeathRecipient");
1326         return RET_ERR;
1327     }
1328     return ERR_OK;
1329 }
1330 
NativeInjectCheck(int32_t pid)1331 int32_t ServerMsgHandler::NativeInjectCheck(int32_t pid)
1332 {
1333     CALL_DEBUG_ENTER;
1334     if (PRODUCT_TYPE != PRODUCT_TYPE_PC) {
1335         MMI_HILOGW("Current device has no permission");
1336         return COMMON_PERMISSION_CHECK_ERROR;
1337     }
1338     bool screenLocked = DISPLAY_MONITOR->GetScreenLocked();
1339     if (screenLocked) {
1340         MMI_HILOGW("Screen locked, no permission");
1341         return COMMON_PERMISSION_CHECK_ERROR;
1342     }
1343     if (pid <= 0) {
1344         MMI_HILOGW("Invalid process id pid:%{public}d", pid);
1345         return COMMON_PERMISSION_CHECK_ERROR;
1346     }
1347     auto state = AUTHORIZE_HELPER->GetAuthorizeState();
1348     MMI_HILOGI("The process is already being processed,s:%{public}d,pid:%{public}d,inputPid:%{public}d",
1349         state, AUTHORIZE_HELPER->GetAuthorizePid(), pid);
1350     if (state == AuthorizeState::STATE_UNAUTHORIZE) {
1351         LaunchAbility();
1352         AuthorizeExitCallback fnCallback = [&] (int32_t pid) {
1353             MMI_HILOGI("User not authorized to inject pid:%{public}d", pid);
1354             AUTH_DIALOG.CloseDialog();
1355         };
1356         AUTHORIZE_HELPER->AddAuthorizeProcess(pid, fnCallback);
1357         return COMMON_PERMISSION_CHECK_ERROR;
1358     }
1359 
1360     if (state == AuthorizeState::STATE_SELECTION_AUTHORIZE) {
1361         if (pid == AUTHORIZE_HELPER->GetAuthorizePid()) {
1362             MMI_HILOGI("The current PID is waiting for user authorization");
1363         } else {
1364             MMI_HILOGI("Another PID is waiting for user authorization");
1365         }
1366         return COMMON_PERMISSION_CHECK_ERROR;
1367     }
1368 
1369     // Currently, a process is authorized.state is AuthorizeState::STATE_AUTHORIZE
1370     if (pid != AUTHORIZE_HELPER->GetAuthorizePid()) {
1371         MMI_HILOGI("Other processes have been authorized");
1372         return COMMON_PERMISSION_CHECK_ERROR;
1373     }
1374     return RET_OK;
1375 }
1376 } // namespace MMI
1377 } // namespace OHOS
1378