1 /*
2 * Copyright (c) 2021 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 #include "event_dispatcher.h"
16
17 #include <algorithm>
18
19 #include "logger.h"
20 namespace OHOS {
21 namespace HiviewDFX {
AddInterestType(uint32_t type)22 void EventDispatcher::AddInterestType(uint32_t type)
23 {
24 types_.insert(type);
25 }
26
ClearInvalidListeners()27 void EventDispatcher::ClearInvalidListeners()
28 {
29 std::lock_guard<std::mutex> lock(lock_);
30 auto channelMapperIter = channelMapper_.begin();
31 while (channelMapperIter != channelMapper_.end()) {
32 auto listeners = channelMapperIter->second;
33 auto listenerIter = listeners.begin();
34 while (listenerIter != listeners.end()) {
35 auto listener = listenerIter->lock();
36 if (listener == nullptr) {
37 listenerIter = listeners.erase(listenerIter);
38 continue;
39 }
40 listenerIter++;
41 }
42 channelMapperIter++;
43 }
44 }
45
DispatchEvent(Event event)46 void EventDispatcher::DispatchEvent(Event event)
47 {
48 if (types_.find(event.messageType_) == types_.end()) {
49 return;
50 }
51
52 auto listeners = channelMapper_[event.messageType_];
53 for (auto listener : listeners) {
54 auto sp = listener.lock();
55 if (sp == nullptr) {
56 continue;
57 }
58
59 std::set<EventListener::EventIdRange> listenerInfo;
60 if (!sp->GetListenerInfo(event.messageType_, listenerInfo)) {
61 continue;
62 }
63
64 if (std::any_of(listenerInfo.begin(), listenerInfo.end(),
65 [&](const EventListener::EventIdRange &range) {
66 return ((event.eventId_ >= range.begin) && (event.eventId_ <= range.end));
67 })) {
68 sp->OnUnorderedEvent(event);
69 }
70 }
71
72 auto plugins = channelPlugin_[event.messageType_];
73 for (auto listener : plugins) {
74 auto sp = listener.lock();
75 if (sp == nullptr) {
76 continue;
77 }
78
79 std::set<EventListener::EventIdRange> listenerInfo;
80 if (!sp->GetEventListenerInfo(event.messageType_, listenerInfo)) {
81 continue;
82 }
83
84 if (std::any_of(listenerInfo.begin(), listenerInfo.end(),
85 [&](const EventListener::EventIdRange &range) {
86 return ((event.eventId_ >= range.begin) && (event.eventId_ <= range.end));
87 })) {
88 sp->OnEventListeningCallback(event);
89 }
90 }
91 }
RegisterListener(std::weak_ptr<EventListener> listener)92 void EventDispatcher::RegisterListener(std::weak_ptr<EventListener> listener)
93 {
94 auto sp = listener.lock();
95 if (sp == nullptr) {
96 return;
97 }
98
99 for (auto type : types_) {
100 std::set<EventListener::EventIdRange> listenerInfo;
101 if (sp->GetListenerInfo(type, listenerInfo) && (!listenerInfo.empty())) {
102 std::lock_guard<std::mutex> lock(lock_);
103 channelMapper_[type].push_back(listener);
104 }
105 }
106 }
107
channelPluginFind(std::string name,int32_t type)108 bool EventDispatcher::channelPluginFind(std::string name, int32_t type)
109 {
110 for (auto& i : channelPlugin_[type]) {
111 auto ptr = i.lock();
112 if (ptr == nullptr) {
113 continue;
114 }
115 if (ptr->GetName() == name) {
116 return true;
117 }
118 }
119 return false;
120 }
121
RegisterListener(std::weak_ptr<Plugin> plugin)122 void EventDispatcher::RegisterListener(std::weak_ptr<Plugin> plugin)
123 {
124 auto sp = plugin.lock();
125 if (sp == nullptr) {
126 return;
127 }
128
129 for (auto type : types_) {
130 std::set<EventListener::EventIdRange> listenerInfo;
131 if (sp->GetEventListenerInfo(type, listenerInfo) && (!listenerInfo.empty())) {
132 std::lock_guard<std::mutex> lock(lock_);
133 if (channelPluginFind(sp->GetName(), type)) {
134 continue;
135 }
136 channelPlugin_[type].push_back(plugin);
137 }
138 }
139 }
140 } // namespace HiviewDFX
141 } // namespace OHOS