1 /*
2 * Copyright (c) 2021-2025 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
17 #include "defines.h"
18 #include "file_util.h"
19 #include "hiview_event_report.h"
20 #include "thread_util.h"
21 #include "time_util.h"
22
23 #include <functional>
24
25 namespace OHOS {
26 namespace HiviewDFX {
~Plugin()27 Plugin::~Plugin()
28 {
29 if (handle_ != DynamicModuleDefault) {
30 UnloadModule(handle_);
31 handle_ = DynamicModuleDefault;
32 }
33 }
34
OnEvent(std::shared_ptr<Event> & event __UNUSED)35 bool Plugin::OnEvent(std::shared_ptr<Event>& event __UNUSED)
36 {
37 return true;
38 }
39
CanProcessEvent(std::shared_ptr<Event> event __UNUSED)40 bool Plugin::CanProcessEvent(std::shared_ptr<Event> event __UNUSED)
41 {
42 return true;
43 }
44
IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)45 bool Plugin::IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)
46 {
47 return true;
48 }
49
CanProcessMoreEvents()50 bool Plugin::CanProcessMoreEvents()
51 {
52 return true;
53 }
54
OnEventProxy(std::shared_ptr<Event> event)55 bool Plugin::OnEventProxy(std::shared_ptr<Event> event)
56 {
57 if (event == nullptr) {
58 return false;
59 }
60
61 std::shared_ptr<Event> dupEvent = event;
62 auto processorSize = dupEvent->GetPendingProcessorSize();
63 dupEvent->ResetPendingStatus();
64 bool ret = false;
65 auto timePtr = std::make_shared<uint64_t>(0);
66 {
67 TimeUtil::TimeCalculator tc(timePtr);
68 ret = OnEvent(dupEvent);
69 }
70 HiviewEventReport::UpdatePluginStats(this->name_, event->eventName_, *timePtr);
71 event->realtime_ += *timePtr;
72
73 if (!dupEvent->IsPipelineEvent()) {
74 return ret;
75 }
76
77 if ((!dupEvent->HasFinish() && !dupEvent->HasPending()) &&
78 (processorSize == dupEvent->GetPendingProcessorSize())) {
79 dupEvent->OnContinue();
80 }
81
82 return ret;
83 }
84
DelayProcessEvent(std::shared_ptr<Event> event,uint64_t delay)85 void Plugin::DelayProcessEvent(std::shared_ptr<Event> event, uint64_t delay)
86 {
87 if (workLoop_ == nullptr || event == nullptr) {
88 return;
89 }
90
91 UpdateTimeByDelay(delay);
92 event->OnPending();
93 auto task = std::bind(&Plugin::OnEventProxy, this, event);
94 workLoop_->AddTimerEvent(nullptr, nullptr, task, delay, false);
95 return;
96 }
97
AddDispatchInfo(const std::unordered_set<uint8_t> & types,const std::unordered_set<std::string> & eventNames,const std::unordered_set<std::string> & tags,const std::unordered_map<std::string,DomainRule> & domainRulesMap)98 void Plugin::AddDispatchInfo(const std::unordered_set<uint8_t>& types,
99 const std::unordered_set<std::string> &eventNames, const std::unordered_set<std::string> &tags,
100 const std::unordered_map<std::string, DomainRule>& domainRulesMap)
101 {
102 if (context_ == nullptr) {
103 return;
104 }
105 context_->AddDispatchInfo(shared_from_this(), types, eventNames, tags, domainRulesMap);
106 }
107
GetPluginInfo()108 std::string Plugin::GetPluginInfo()
109 {
110 return GetName();
111 }
112
GetHandlerInfo()113 std::string Plugin::GetHandlerInfo()
114 {
115 return GetName();
116 }
117
GetName()118 const std::string& Plugin::GetName()
119 {
120 return name_;
121 }
122
GetVersion()123 const std::string& Plugin::GetVersion()
124 {
125 return version_;
126 }
127
SetName(const std::string & name)128 void Plugin::SetName(const std::string& name)
129 {
130 name_ = name;
131 }
132
SetVersion(const std::string & version)133 void Plugin::SetVersion(const std::string& version)
134 {
135 version_ = version;
136 }
137
BindWorkLoop(std::shared_ptr<EventLoop> loop)138 void Plugin::BindWorkLoop(std::shared_ptr<EventLoop> loop)
139 {
140 workLoop_ = loop;
141 }
142
GetWorkLoop()143 std::shared_ptr<EventLoop> Plugin::GetWorkLoop()
144 {
145 return workLoop_;
146 }
147
UpdateActiveTime()148 void Plugin::UpdateActiveTime()
149 {
150 lastActiveTime_ = time(nullptr);
151 }
152
UpdateTimeByDelay(time_t delay)153 void Plugin::UpdateTimeByDelay(time_t delay)
154 {
155 lastActiveTime_ = time(nullptr) + delay;
156 }
157 } // namespace HiviewDFX
158 } // namespace OHOS
159