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 #ifndef HIVIEW_BASE_EVENT_H 16 #define HIVIEW_BASE_EVENT_H 17 #include <iostream> 18 #include <list> 19 #include <map> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <string> 24 #include <sys/types.h> 25 #include "defines.h" 26 #include "public_defines.h" 27 28 #include "base/raw_data.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 class DllExport Event : public std::enable_shared_from_this<Event> { 33 public: Event(const std::string & sender)34 explicit Event(const std::string &sender) 35 : messageType_(MessageType::NONE), 36 processType_(ManageType::UNORDERED), 37 what_(0), 38 eventId_(0), 39 happenTime_(0), 40 targetDispatchTime_(0), 41 createTime_(0), 42 realtime_(0), 43 processTime_(0), 44 sender_(sender), 45 domain_(""), 46 eventName_(""), 47 isPipeline_(false), 48 hasFinish_(false), 49 hasPending_(false), 50 traceId_(""), 51 spanId_(""), 52 parentSpanId_(""), 53 traceFlag_(""), 54 normalExtraInfo_(""), 55 rawData_(nullptr) 56 { 57 ResetTimestamp(); 58 }; 59 Event(const std::string & sender,const std::string & name)60 explicit Event(const std::string &sender, const std::string &name) 61 : messageType_(MessageType::NONE), 62 processType_(ManageType::UNORDERED), 63 what_(0), 64 eventId_(0), 65 happenTime_(0), 66 targetDispatchTime_(0), 67 createTime_(0), 68 realtime_(0), 69 processTime_(0), 70 sender_(sender), 71 domain_(""), 72 eventName_(name), 73 isPipeline_(false), 74 hasFinish_(false), 75 hasPending_(false), 76 traceId_(""), 77 spanId_(""), 78 parentSpanId_(""), 79 traceFlag_(""), 80 normalExtraInfo_(""), 81 rawData_(nullptr) 82 { 83 ResetTimestamp(); 84 }; 85 ~Event()86 virtual ~Event() {}; 87 OnContinue()88 virtual bool OnContinue() 89 { 90 return true; 91 }; 92 OnFinish()93 virtual bool OnFinish() 94 { 95 hasFinish_ = true; 96 return true; 97 }; 98 OnRepack()99 virtual void OnRepack() {}; 100 OnPending()101 virtual void OnPending() {}; 102 GetPendingProcessorSize()103 virtual uint32_t GetPendingProcessorSize() 104 { 105 return 0; 106 } 107 108 // call from audit module 109 // if you want to override this function 110 // you should call parent function first and append to your result GetEventInfo()111 virtual std::string GetEventInfo() 112 { 113 return std::to_string(eventId_); 114 }; 115 116 // use for broadcasting events 117 enum MessageType { 118 NONE = 0, 119 PLUGIN_MAINTENANCE, // Reserved 120 FAULT_EVENT, 121 STATISTICS_EVENT, 122 RAW_EVENT, 123 SYS_EVENT, 124 UE_EVENT, 125 EXTERNAL_EVENT, 126 EXTERNAL_REMOTE_EVENT, 127 CROSS_PLATFORM, 128 PRIVATE_MESSAGE_TYPE // Expand macro from public_defines.h 129 }; 130 131 enum ManageType { 132 ORDERED, 133 UNORDERED, 134 PROCESS_TYPE_NUM, 135 }; 136 137 enum EventId { 138 PLUGIN_LOADED, 139 }; 140 141 MessageType messageType_; 142 ManageType processType_; 143 uint16_t what_; 144 uint32_t eventId_; 145 uint64_t happenTime_; 146 uint64_t targetDispatchTime_; 147 uint64_t createTime_; 148 uint64_t realtime_; 149 uint64_t processTime_; 150 std::string sender_; 151 std::string domain_; 152 std::string eventName_; 153 bool isPipeline_; 154 bool hasFinish_; 155 bool hasPending_; 156 // dft fault listener params 157 std::string traceId_; 158 std::string spanId_; 159 std::string parentSpanId_; 160 std::string traceFlag_; 161 std::string normalExtraInfo_; 162 std::shared_ptr<EventRaw::RawData> rawData_ = nullptr; 163 void SetValue(const std::string &name, const std::string &value); 164 void SetValue(const std::string &name, int32_t value); 165 void SetKeyValuePairs(std::map<std::string, std::string> keyValuePairs); 166 const std::string GetValue(const std::string &name) const; 167 int32_t GetIntValue(const std::string &name) const; 168 std::map<std::string, std::string> GetKeyValuePairs() const; 169 void ResetTimestamp(); ResetPendingStatus()170 void ResetPendingStatus() 171 { 172 hasPending_ = false; 173 }; 174 IsPipelineEvent()175 bool IsPipelineEvent() const 176 { 177 return isPipeline_; 178 }; 179 HasFinish()180 bool HasFinish() const 181 { 182 return hasFinish_; 183 }; 184 HasPending()185 bool HasPending() const 186 { 187 return hasPending_; 188 }; 189 GetEventName()190 std::string GetEventName() const 191 { 192 return eventName_; 193 } 194 SetEventName(const std::string & name)195 void SetEventName(const std::string &name) 196 { 197 eventName_ = name; 198 } 199 200 template <typename Derived> DownCastTo(std::shared_ptr<Event> event)201 static std::shared_ptr<Derived> DownCastTo(std::shared_ptr<Event> event) 202 { 203 return std::static_pointer_cast<Derived>(event); 204 }; 205 206 // Ensure the copy constructor of Base and Derived classes are carefully arranged 207 template <typename Base, typename Derived> 208 static std::shared_ptr<Derived> Repack(std::shared_ptr<Event>& event, bool replace = true) 209 { 210 auto base = std::static_pointer_cast<Base>(event); 211 if (replace) { 212 auto derived = new Derived(*(base.get())); 213 event.reset(derived); 214 return std::static_pointer_cast<Derived>(event); 215 } 216 217 auto newEvent = std::make_shared<Derived>(*(base.get())); 218 newEvent->OnRepack(); 219 return newEvent; 220 }; 221 222 protected: 223 std::map<std::string, std::string> bundle_; 224 }; 225 class DllExport EventHandler { 226 public: ~EventHandler()227 virtual ~EventHandler(){}; 228 virtual bool OnEvent(std::shared_ptr<Event>& event) = 0; OnEventProxy(std::shared_ptr<Event> event)229 virtual bool OnEventProxy(std::shared_ptr<Event> event) 230 { 231 return OnEvent(event); 232 }; 233 CanProcessEvent(std::shared_ptr<Event> event __UNUSED)234 virtual bool CanProcessEvent(std::shared_ptr<Event> event __UNUSED) 235 { 236 return true; 237 }; 238 IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)239 virtual bool IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED) 240 { 241 return true; 242 }; 243 CanProcessMoreEvents()244 virtual bool CanProcessMoreEvents() 245 { 246 return true; 247 }; 248 GetHandlerInfo()249 virtual std::string GetHandlerInfo() 250 { 251 return ""; 252 }; 253 }; 254 class EventListener { 255 public: EventListener()256 EventListener() {}; ~EventListener()257 virtual ~EventListener(){}; 258 OnOrderedEvent(const Event & msg)259 virtual bool OnOrderedEvent(const Event &msg) 260 { 261 return false; 262 } 263 virtual void OnUnorderedEvent(const Event &msg) = 0; 264 virtual std::string GetListenerName() = 0; 265 void AddListenerInfo(uint32_t type); 266 void AddListenerInfo(uint32_t type, const std::map<std::string, DomainRule>& domainRulesMap); 267 void AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames, 268 const std::map<std::string, DomainRule>& domainRulesMap = {}); 269 }; 270 } // namespace HiviewDFX 271 } // namespace OHOS 272 #endif // HIVIEW_BASE_EVENT_H 273