• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "event_filter_handler.h"
17 
18 #include "error_multimodal.h"
19 #include "input_device_manager.h"
20 #include "mmi_log.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "EventFilterHandler" };
26 } // namespace
27 
28 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)29 void EventFilterHandler::HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
30 {
31     CALL_DEBUG_ENTER;
32     CHKPV(keyEvent);
33     if (HandleKeyEventFilter(keyEvent)) {
34         MMI_HILOGD("Key event is filtered");
35         return;
36     }
37     CHKPV(nextHandler_);
38     nextHandler_->HandleKeyEvent(keyEvent);
39 }
40 #endif // OHOS_BUILD_ENABLE_KEYBOARD
41 
42 #ifdef OHOS_BUILD_ENABLE_POINTER
HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)43 void EventFilterHandler::HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
44 {
45     CHKPV(pointerEvent);
46     if (HandlePointerEventFilter(pointerEvent)) {
47         MMI_HILOGD("Pointer event is filtered");
48         return;
49     }
50     CHKPV(nextHandler_);
51     nextHandler_->HandlePointerEvent(pointerEvent);
52 }
53 #endif // OHOS_BUILD_ENABLE_POINTER
54 
55 #ifdef OHOS_BUILD_ENABLE_TOUCH
HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)56 void EventFilterHandler::HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)
57 {
58     CHKPV(pointerEvent);
59     if (HandlePointerEventFilter(pointerEvent)) {
60         MMI_HILOGD("Touch event is filtered");
61         return;
62     }
63     CHKPV(nextHandler_);
64     nextHandler_->HandleTouchEvent(pointerEvent);
65 }
66 #endif // OHOS_BUILD_ENABLE_TOUCH
67 
AddInputEventFilter(sptr<IEventFilter> filter,int32_t filterId,int32_t priority,uint32_t deviceTags,int32_t clientPid)68 int32_t EventFilterHandler::AddInputEventFilter(sptr<IEventFilter> filter,
69     int32_t filterId, int32_t priority, uint32_t deviceTags, int32_t clientPid)
70 {
71     CALL_INFO_TRACE;
72     std::lock_guard<std::mutex> guard(lockFilter_);
73     CHKPR(filter, ERROR_NULL_POINTER);
74     MMI_HILOGI("Add filter,filterId:%{public}d,priority:%{public}d,clientPid:%{public}d,filters_ size:%{public}zu",
75         filterId, priority, clientPid, filters_.size());
76 
77     std::weak_ptr<EventFilterHandler> weakPtr = shared_from_this();
78     auto deathCallback = [weakPtr, filterId, clientPid](const wptr<IRemoteObject> &object) {
79         auto sharedPtr = weakPtr.lock();
80         if (sharedPtr != nullptr) {
81             auto ret = sharedPtr->RemoveInputEventFilter(filterId, clientPid);
82             if (ret != RET_OK) {
83                 MMI_HILOGW("Remove filter on dead return:%{public}d, filterId:%{public}d,clientPid:%{public}d",
84                     ret, filterId, clientPid);
85             } else {
86                 MMI_HILOGW("Remove filter on dead success, filterId:%{public}d,clientPid:%{public}d",
87                     filterId, clientPid);
88             }
89         }
90     };
91     sptr<IRemoteObject::DeathRecipient> deathRecipient = new (std::nothrow) EventFilterDeathRecipient(deathCallback);
92     CHKPR(deathRecipient, RET_ERR);
93     filter->AsObject()->AddDeathRecipient(deathRecipient);
94 
95     FilterInfo info { .filter = filter, .deathRecipient = deathRecipient, .filterId = filterId,
96         .priority = priority, .deviceTags = deviceTags, .clientPid = clientPid };
97     auto it = filters_.cbegin();
98     for (; it != filters_.cend(); ++it) {
99         if (info.priority < it->priority) {
100             break;
101         }
102     }
103     auto it2 = filters_.emplace(it, std::move(info));
104     if (it2 == filters_.end()) {
105         MMI_HILOGE("Fail to add filter");
106         return ERROR_FILTER_ADD_FAIL;
107     }
108     return RET_OK;
109 }
110 
RemoveInputEventFilter(int32_t filterId,int32_t clientPid)111 int32_t EventFilterHandler::RemoveInputEventFilter(int32_t filterId, int32_t clientPid)
112 {
113     CALL_INFO_TRACE;
114     std::lock_guard<std::mutex> guard(lockFilter_);
115     if (filters_.empty()) {
116         MMI_HILOGD("Filter is empty");
117         return RET_OK;
118     }
119     for (auto it = filters_.begin(); it != filters_.end();) {
120         if (filterId == -1) {
121             if (it->clientPid == clientPid) {
122                 auto id = it->filterId;
123                 filters_.erase(it++);
124                 MMI_HILOGI("Filter remove success, filterId:%{public}d,clientPid:%{public}d", id, clientPid);
125                 continue;
126             }
127             ++it;
128             continue;
129         }
130         if (it->IsSameClient(filterId, clientPid)) {
131             filters_.erase(it++);
132             MMI_HILOGI("Filter remove success, filterId:%{public}d,clientPid:%{public}d", filterId, clientPid);
133             return RET_OK;
134         }
135         ++it;
136     }
137     if (filterId == -1) {
138         return RET_OK;
139     }
140     MMI_HILOGI("Filter not found, filterId:%{public}d,clientPid:%{public}d", filterId, clientPid);
141     return RET_OK;
142 }
143 
Dump(int32_t fd,const std::vector<std::string> & args)144 void EventFilterHandler::Dump(int32_t fd, const std::vector<std::string> &args)
145 {
146     CALL_DEBUG_ENTER;
147     std::lock_guard<std::mutex> guard(lockFilter_);
148     dprintf(fd, "Filter information:\n");
149     dprintf(fd, "Filters: count=%d\n", filters_.size());
150     for (const auto &item : filters_) {
151         dprintf(fd, "priority:%d | filterId:%d | Pid:%d\n", item.priority, item.filterId, item.clientPid);
152     }
153 }
154 
HandleKeyEventFilter(std::shared_ptr<KeyEvent> event)155 bool EventFilterHandler::HandleKeyEventFilter(std::shared_ptr<KeyEvent> event)
156 {
157     CALL_DEBUG_ENTER;
158     CHKPF(event);
159     std::lock_guard<std::mutex> guard(lockFilter_);
160     if (filters_.empty()) {
161         return false;
162     }
163     std::vector<KeyEvent::KeyItem> keyItems = event->GetKeyItems();
164     if (keyItems.empty()) {
165         MMI_HILOGE("keyItems is empty");
166         return false;
167     }
168     std::shared_ptr<InputDevice> inputDevice = InputDevMgr->GetInputDevice(keyItems.front().GetDeviceId());
169     CHKPF(inputDevice);
170     for (auto &i: filters_) {
171         if (!inputDevice->HasCapability(i.deviceTags)) {
172             continue;
173         }
174         if (i.filter->HandleKeyEvent(event)) {
175             MMI_HILOGD("Call HandleKeyEventFilter return true");
176             return true;
177         }
178     }
179     return false;
180 }
181 
HandlePointerEventFilter(std::shared_ptr<PointerEvent> event)182 bool EventFilterHandler::HandlePointerEventFilter(std::shared_ptr<PointerEvent> event)
183 {
184     CALL_DEBUG_ENTER;
185     CHKPF(event);
186     std::lock_guard<std::mutex> guard(lockFilter_);
187     if (filters_.empty()) {
188         return false;
189     }
190     PointerEvent::PointerItem pointerItem;
191     int32_t pointerId = event->GetPointerId();
192     if (!event->GetPointerItem(pointerId, pointerItem)) {
193         MMI_HILOGE("GetPointerItem:%{public}d fail", pointerId);
194         return false;
195     }
196     std::shared_ptr<InputDevice> inputDevice = InputDevMgr->GetInputDevice(pointerItem.GetDeviceId());
197     CHKPF(inputDevice);
198     for (auto &i: filters_) {
199         if (!inputDevice->HasCapability(i.deviceTags)) {
200             continue;
201         }
202         if (i.filter->HandlePointerEvent(event)) {
203             MMI_HILOGD("Call HandlePointerEvent return true");
204             return true;
205         }
206     }
207     return false;
208 }
209 } // namespace MMI
210 } // namespace OHOS
211