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 #ifndef SERVER_MSG_HANDLER_H
17 #define SERVER_MSG_HANDLER_H
18 
19 #include "nocopyable.h"
20 
21 #include "event_dispatch_handler.h"
22 #include "i_event_filter.h"
23 #include "input_handler_type.h"
24 #include "key_option.h"
25 #include "mouse_event_normalize.h"
26 #include "msg_handler.h"
27 #include "pixel_map.h"
28 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
29 #include "sec_comp_enhance_kit.h"
30 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
31 #include "window_info.h"
32 #include "inject_notice_manager.h"
33 #include "client_death_handler.h"
34 
35 namespace OHOS {
36 namespace MMI {
37 enum class AuthorizationStatus : int32_t {
38     UNKNOWN,
39     AUTHORIZED,
40     UNAUTHORIZED
41 };
42 
43 enum class InjectionType : int32_t {
44     UNKNOWN,
45     KEYEVENT,
46     POINTEREVENT
47 };
48 
49 typedef std::function<int32_t(SessionPtr sess, NetPacket& pkt)> ServerMsgFun;
50 class ServerMsgHandler final : public MsgHandler<MmiMessageId, ServerMsgFun> {
51 public:
52     ServerMsgHandler() = default;
53     DISALLOW_COPY_AND_MOVE(ServerMsgHandler);
54     ~ServerMsgHandler() override = default;
55 
56     void Init(UDSServer& udsServer);
57     void OnMsgHandler(SessionPtr sess, NetPacket& pkt);
58 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
59     int32_t OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType,
60         int32_t priority, uint32_t deviceTags);
61     int32_t OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType,
62         int32_t priority, uint32_t deviceTags);
63     int32_t OnAddGestureMonitor(SessionPtr sess, InputHandlerType handlerType,
64         HandleEventType eventType, TouchGestureType gestureType, int32_t fingers);
65     int32_t OnRemoveGestureMonitor(SessionPtr sess, InputHandlerType handlerType,
66         HandleEventType eventType, TouchGestureType gestureType, int32_t fingers);
67     int32_t OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, std::vector<int32_t> actionsType);
68     int32_t OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, std::vector<int32_t> actionsType);
69 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
70 #ifdef OHOS_BUILD_ENABLE_MONITOR
71     int32_t OnMarkConsumed(SessionPtr sess, int32_t eventId);
72 #endif // OHOS_BUILD_ENABLE_MONITOR
73 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
74     int32_t OnSubscribeKeyEvent(IUdsServer *server, int32_t pid,
75         int32_t subscribeId, const std::shared_ptr<KeyOption> option);
76     int32_t OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId);
77     int32_t OnSubscribeHotkey(IUdsServer *server, int32_t pid,
78         int32_t subscribeId, const std::shared_ptr<KeyOption> option);
79     int32_t OnUnsubscribeHotkey(IUdsServer *server, int32_t pid, int32_t subscribeId);
80 #endif // OHOS_BUILD_ENABLE_KEYBOARD
81 
82 #ifdef OHOS_BUILD_ENABLE_SWITCH
83     int32_t OnSubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType);
84     int32_t OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId);
85 #endif // OHOS_BUILD_ENABLE_SWITCH
86 
87 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
88     int32_t OnMoveMouse(int32_t offsetX, int32_t offsetY);
89 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
90 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
91     int32_t OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject);
92     int32_t OnGetFunctionKeyState(int32_t funcKey, bool &state);
93     int32_t OnSetFunctionKeyState(int32_t funcKey, bool enable);
94 #endif // OHOS_BUILD_ENABLE_KEYBOARD
95 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
96     int32_t OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
97         bool isNativeInject, bool isShell);
98     int32_t OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, bool isShell);
99     int32_t SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell);
100 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
101 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
102     int32_t AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority, uint32_t deviceTags,
103         int32_t clientPid);
104     int32_t RemoveInputEventFilter(int32_t clientPid, int32_t filterId);
105 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
106 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
107     int32_t SetShieldStatus(int32_t shieldMode, bool isShield);
108     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield);
109 #endif // OHOS_BUILD_ENABLE_KEYBOARD
110     int32_t OnAuthorize(bool isAuthorize);
111     int32_t OnCancelInjection();
112     int32_t SetPixelMapData(int32_t infoId, void* pixelMap);
113     bool InitInjectNoticeSource();
114     bool AddInjectNotice(const InjectNoticeInfo& noticeInfo);
115     int32_t OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid);
116     int32_t RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt);
117 
118 protected:
119     int32_t OnRegisterMsgHandler(SessionPtr sess, NetPacket& pkt);
120     int32_t OnDisplayInfo(SessionPtr sess, NetPacket& pkt);
121 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
122     int32_t OnWindowAreaInfo(SessionPtr sess, NetPacket& pkt);
123 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
124     int32_t OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt);
125 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
126     int32_t OnEnhanceConfig(SessionPtr sess, NetPacket& pkt);
127 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
128     void SetWindowInfo(int32_t infoId, WindowInfo &info);
129 
130 private:
131 #ifdef OHOS_BUILD_ENABLE_TOUCH
132     bool FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, int32_t action, bool isShell);
133 #endif // OHOS_BUILD_ENABLE_TOUCH
134     void LaunchAbility();
135 #ifdef OHOS_BUILD_ENABLE_POINTER
136     int32_t AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent);
137 #endif // OHOS_BUILD_ENABLE_POINTER
138 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
139     void UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent);
140 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
141 #ifdef OHOS_BUILD_ENABLE_POINTER
142     void CalculateOffset(Direction direction, Offset &offset);
143 #endif // OHOS_BUILD_ENABLE_POINTER
144     bool CloseInjectNotice(int32_t pid);
145     int32_t OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info);
146     bool IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent);
147 private:
148     UDSServer *udsServer_ { nullptr };
149     std::map<int32_t, int32_t> nativeTargetWindowIds_;
150     std::map<int32_t, int32_t> shellTargetWindowIds_;
151     std::map<int32_t, int32_t> accessTargetWindowIds_;
152     std::map<int32_t, AuthorizationStatus> authorizationCollection_;
153     int32_t CurrentPID_ { -1 };
154     InjectionType InjectionType_ { InjectionType::UNKNOWN };
155     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
156     std::shared_ptr<PointerEvent> pointerEvent_ { nullptr };
157     std::map<int32_t, std::unique_ptr<Media::PixelMap>> transparentWins_;
158     std::shared_ptr<InjectNoticeManager> injectNotice_ { nullptr };
159     ClientDeathHandler clientDeathHandler_;
160 };
161 } // namespace MMI
162 } // namespace OHOS
163 #endif // SERVER_MSG_HANDLER_H
164