• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "plugin.h"
16 #include <functional>
17 #include "audit.h"
18 #include "defines.h"
19 #include "file_util.h"
20 #include "thread_util.h"
21 namespace OHOS {
22 namespace HiviewDFX {
~Plugin()23 Plugin::~Plugin()
24 {
25     if (handle_ != DynamicModuleDefault) {
26         UnloadModule(handle_);
27         handle_ = DynamicModuleDefault;
28     }
29 }
30 
OnEvent(std::shared_ptr<Event> & event __UNUSED)31 bool Plugin::OnEvent(std::shared_ptr<Event>& event __UNUSED)
32 {
33     return true;
34 }
35 
CanProcessEvent(std::shared_ptr<Event> event __UNUSED)36 bool Plugin::CanProcessEvent(std::shared_ptr<Event> event __UNUSED)
37 {
38     return true;
39 }
40 
CanProcessMoreEvents()41 bool Plugin::CanProcessMoreEvents()
42 {
43     return true;
44 }
45 
OnEventProxy(std::shared_ptr<Event> event)46 bool Plugin::OnEventProxy(std::shared_ptr<Event> event)
47 {
48     if (event == nullptr) {
49         return false;
50     }
51 
52     std::shared_ptr<Event> dupEvent = event;
53     auto processorSize = dupEvent->GetPendingProcessorSize();
54     dupEvent->ResetPendingStatus();
55     bool ret = OnEvent(dupEvent);
56 
57     if (!dupEvent->IsPipelineEvent()) {
58         if (Audit::IsEnabled()) {
59             Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, dupEvent->createTime_,
60                 std::to_string(Thread::GetTid()));
61         }
62     } else {
63         if ((!dupEvent->HasFinish() && !dupEvent->HasPending()) &&
64             (processorSize == dupEvent->GetPendingProcessorSize())) {
65             dupEvent->OnContinue();
66         }
67     }
68     return ret;
69 }
70 
DelayProcessEvent(std::shared_ptr<Event> event,uint64_t delay)71 void Plugin::DelayProcessEvent(std::shared_ptr<Event> event, uint64_t delay)
72 {
73     if (workLoop_ == nullptr || event == nullptr) {
74         return;
75     }
76 
77     UpdateTimeByDelay(delay);
78     event->OnPending();
79     auto task = std::bind(&Plugin::OnEventProxy, this, event);
80     workLoop_->AddTimerEvent(nullptr, nullptr, task, delay, false);
81     return;
82 }
83 
AddEventListenerInfo(uint32_t type,const EventListener::EventIdRange & range)84 void Plugin::AddEventListenerInfo(uint32_t type, const EventListener::EventIdRange& range)
85 {
86     if (context_ == nullptr) {
87         return;
88     }
89     std::set<std::string> eventNames;
90     std::set<EventListener::EventIdRange> listenerInfo;
91     listenerInfo.insert(range);
92     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
93 }
94 
AddEventListenerInfo(uint32_t type,const std::set<EventListener::EventIdRange> & listenerInfo)95 void Plugin::AddEventListenerInfo(uint32_t type, const std::set<EventListener::EventIdRange> &listenerInfo)
96 {
97     if (context_ == nullptr) {
98         return;
99     }
100     std::set<std::string> eventNames;
101     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
102 }
103 
GetEventListenerInfo(uint32_t type,std::set<EventListener::EventIdRange> & listenerInfo)104 bool Plugin::GetEventListenerInfo(uint32_t type, std::set<EventListener::EventIdRange> &listenerInfo)
105 {
106     if (context_ == nullptr) {
107         return false;
108     }
109     return context_->GetListenerInfo(type, name_, listenerInfo);
110 }
111 
AddEventListenerInfo(uint32_t type,const std::string & eventName)112 void Plugin::AddEventListenerInfo(uint32_t type, const std::string& eventName)
113 {
114     if (context_ == nullptr) {
115         return;
116     }
117     std::set<std::string> eventNames;
118     eventNames.insert(eventName);
119     std::set<EventListener::EventIdRange> listenerInfo;
120     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
121 }
122 
AddEventListenerInfo(uint32_t type,const std::set<std::string> & eventNames)123 void Plugin::AddEventListenerInfo(uint32_t type, const std::set<std::string> &eventNames)
124 {
125     if (context_ == nullptr) {
126         return;
127     }
128     std::set<EventListener::EventIdRange> listenerInfo;
129     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
130 }
131 
GetEventListenerInfo(uint32_t type,std::set<std::string> & eventNames)132 bool Plugin::GetEventListenerInfo(uint32_t type, std::set<std::string> &eventNames)
133 {
134     if (context_ == nullptr) {
135         return false;
136     }
137     return context_->GetListenerInfo(type, name_, eventNames);
138 }
139 
GetPluginInfo()140 std::string Plugin::GetPluginInfo()
141 {
142     return GetName();
143 }
144 
GetHandlerInfo()145 std::string Plugin::GetHandlerInfo()
146 {
147     return GetName();
148 }
149 
GetName()150 const std::string& Plugin::GetName()
151 {
152     return name_;
153 }
154 
GetVersion()155 const std::string& Plugin::GetVersion()
156 {
157     return version_;
158 }
159 
SetName(const std::string & name)160 void Plugin::SetName(const std::string& name)
161 {
162     name_ = name;
163 }
164 
SetVersion(const std::string & version)165 void Plugin::SetVersion(const std::string& version)
166 {
167     version_ = version;
168 }
169 
BindWorkLoop(std::shared_ptr<EventLoop> loop)170 void Plugin::BindWorkLoop(std::shared_ptr<EventLoop> loop)
171 {
172     workLoop_ = loop;
173 }
174 
GetWorkLoop()175 std::shared_ptr<EventLoop> Plugin::GetWorkLoop()
176 {
177     return workLoop_;
178 }
179 
UpdateActiveTime()180 void Plugin::UpdateActiveTime()
181 {
182     lastActiveTime_ = time(nullptr);
183 }
184 
UpdateTimeByDelay(time_t delay)185 void Plugin::UpdateTimeByDelay(time_t delay)
186 {
187     lastActiveTime_ = time(nullptr) + delay;
188 }
189 } // namespace HiviewDFX
190 } // namespace OHOS
191