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 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
64 #ifdef OHOS_BUILD_ENABLE_MONITOR
65     int32_t OnMarkConsumed(SessionPtr sess, int32_t eventId);
66 #endif // OHOS_BUILD_ENABLE_MONITOR
67 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
68     int32_t OnSubscribeKeyEvent(IUdsServer *server, int32_t pid,
69         int32_t subscribeId, const std::shared_ptr<KeyOption> option);
70     int32_t OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId);
71 #endif // OHOS_BUILD_ENABLE_KEYBOARD
72 
73 #ifdef OHOS_BUILD_ENABLE_SWITCH
74     int32_t OnSubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType);
75     int32_t OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId);
76 #endif // OHOS_BUILD_ENABLE_SWITCH
77 
78 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
79     int32_t OnMoveMouse(int32_t offsetX, int32_t offsetY);
80 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
81 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
82     int32_t OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject);
83     int32_t OnGetFunctionKeyState(int32_t funcKey, bool &state);
84     int32_t OnSetFunctionKeyState(int32_t funcKey, bool enable);
85 #endif // OHOS_BUILD_ENABLE_KEYBOARD
86 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
87     int32_t OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
88         bool isNativeInject, bool isShell);
89     int32_t OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, bool isShell);
90     int32_t SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell);
91 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
92 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
93     int32_t AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority, uint32_t deviceTags,
94         int32_t clientPid);
95     int32_t RemoveInputEventFilter(int32_t clientPid, int32_t filterId);
96 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
97 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
98     int32_t SetShieldStatus(int32_t shieldMode, bool isShield);
99     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield);
100 #endif // OHOS_BUILD_ENABLE_KEYBOARD
101     int32_t OnAuthorize(bool isAuthorize);
102     int32_t OnCancelInjection(int32_t callPidId = 0);
103     int32_t SetPixelMapData(int32_t infoId, void* pixelMap);
104     bool InitInjectNoticeSource();
105     bool AddInjectNotice(const InjectNoticeInfo& noticeInfo);
106     int32_t OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid);
107     int32_t RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt);
108 
109 protected:
110     int32_t OnRegisterMsgHandler(SessionPtr sess, NetPacket& pkt);
111     int32_t OnDisplayInfo(SessionPtr sess, NetPacket& pkt);
112 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
113     int32_t OnWindowAreaInfo(SessionPtr sess, NetPacket& pkt);
114 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
115     int32_t OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt);
116 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
117     int32_t OnEnhanceConfig(SessionPtr sess, NetPacket& pkt);
118 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
119     void SetWindowInfo(int32_t infoId, WindowInfo &info);
120 
121 private:
122 #ifdef OHOS_BUILD_ENABLE_TOUCH
123     bool FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, int32_t action, bool isShell);
124     bool UpdateTouchEvent(std::shared_ptr<PointerEvent> pointerEvent, int32_t action, int32_t targetWindowId);
125 #endif // OHOS_BUILD_ENABLE_TOUCH
126     void LaunchAbility();
127 #ifdef OHOS_BUILD_ENABLE_POINTER
128     int32_t AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent);
129 #endif // OHOS_BUILD_ENABLE_POINTER
130 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
131     void UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent);
132 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
133 #ifdef OHOS_BUILD_ENABLE_POINTER
134     void CalculateOffset(Direction direction, Offset &offset);
135 #endif // OHOS_BUILD_ENABLE_POINTER
136     int32_t OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info);
137     bool CloseInjectNotice(int32_t pid);
138     bool IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent);
139     int32_t NativeInjectCheck(int32_t pid);
140 private:
141     UDSServer *udsServer_ { nullptr };
142     std::map<int32_t, int32_t> nativeTargetWindowIds_;
143     std::map<int32_t, int32_t> shellTargetWindowIds_;
144     std::map<int32_t, int32_t> accessTargetWindowIds_;
145     std::map<int32_t, int32_t> castTargetWindowIds_;
146     std::map<int32_t, AuthorizationStatus> authorizationCollection_;
147     int32_t CurrentPID_ { -1 };
148     InjectionType InjectionType_ { InjectionType::UNKNOWN };
149     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
150     std::shared_ptr<PointerEvent> pointerEvent_ { nullptr };
151     std::map<int32_t, std::unique_ptr<Media::PixelMap>> transparentWins_;
152     std::shared_ptr<InjectNoticeManager> injectNotice_ { nullptr };
153     ClientDeathHandler clientDeathHandler_;
154 };
155 } // namespace MMI
156 } // namespace OHOS
157 #endif // SERVER_MSG_HANDLER_H
158