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_PLUGIN_H 16 #define HIVIEW_BASE_PLUGIN_H 17 18 #include <atomic> 19 #include <ctime> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "defines.h" 25 #include "dynamic_module.h" 26 #include "event.h" 27 #include "event_loop.h" 28 #include "plugin_extra_info.h" 29 30 class HiEvent; 31 namespace OHOS { 32 namespace HiviewDFX { 33 class HiviewContext; 34 class DllExport Plugin : public EventHandler, public std::enable_shared_from_this<Plugin> { 35 public: 36 enum class PluginType { 37 STATIC, 38 DYNAMIC, 39 PROXY, 40 }; 41 public: Plugin()42 Plugin() : handle_(DynamicModuleDefault), context_(nullptr), useCount_(0) {}; 43 virtual ~Plugin(); 44 // do not store the event in the callback 45 // create a new one through copy constructor is preferred 46 virtual bool OnEvent(std::shared_ptr<Event>& event) override; 47 48 /* If the plugin is at the top of a pipeline, this function will be used to determine 49 * whether the current event can be processed by the pipeline. 50 */ 51 virtual bool CanProcessEvent(std::shared_ptr<Event> event) override; 52 53 /* If the plugin is in a pipeline, this function will be used to determine 54 * whether the current pipeline event can be processed by the plugin. 55 */ 56 virtual bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override; 57 virtual bool CanProcessMoreEvents() override; 58 bool OnEventProxy(std::shared_ptr<Event> event) override; 59 virtual std::string GetHandlerInfo() override; 60 61 // check whether the plugin should be loaded in current environment 62 // the plugin will not be load if return false 63 // called by plugin framework in main thread ReadyToLoad()64 virtual bool ReadyToLoad() 65 { 66 return true; 67 }; 68 69 // the plugin instance will be stored in platform after calling this function 70 // and other loaded plugin can pass event to this plugin 71 // called by plugin framework in main thread OnLoad()72 virtual void OnLoad(){}; 73 74 // release resource and clear pending workloads 75 // after calling this function ,the reference of this plugin will be deleted 76 // called by plugin framework in main thread OnUnload()77 virtual void OnUnload(){}; 78 79 // dump plugin info from dump command line Dump(int fd __UNUSED,const std::vector<std::string> & cmds __UNUSED)80 virtual void Dump(int fd __UNUSED, const std::vector<std::string>& cmds __UNUSED) {}; 81 82 // create an event with specific type GetEvent(Event::MessageType type)83 virtual std::shared_ptr<Event> GetEvent(Event::MessageType type) 84 { 85 auto event = std::make_shared<Event>(name_); 86 event->messageType_ = type; 87 return event; 88 }; 89 90 // called by audit module 91 virtual std::string GetPluginInfo(); 92 93 // Listener callback OnEventListeningCallback(const Event & msg)94 virtual void OnEventListeningCallback(const Event &msg) 95 { 96 return; 97 } 98 99 // Make sure that you insert non-overlayed range 100 void AddEventListenerInfo(uint32_t type, const EventListener::EventIdRange &range = EventListener::EventIdRange(0)); 101 void AddEventListenerInfo(uint32_t type, const std::set<EventListener::EventIdRange> &listenerInfo); 102 bool GetEventListenerInfo(uint32_t type, std::set<EventListener::EventIdRange> &listenerInfo); 103 104 void AddEventListenerInfo(uint32_t type, const std::string& eventName); 105 void AddEventListenerInfo(uint32_t type, const std::set<std::string> &eventNames); 106 bool GetEventListenerInfo(uint32_t type, std::set<std::string> &eventNames); 107 108 // reinsert the event into the workloop 109 // delay in seconds 110 void DelayProcessEvent(std::shared_ptr<Event> event, uint64_t delay); 111 112 const std::string& GetName(); 113 const std::string& GetVersion(); 114 void SetName(const std::string& name); 115 void SetVersion(const std::string& version); 116 void BindWorkLoop(std::shared_ptr<EventLoop> loop); 117 void UpdateActiveTime(); 118 void UpdateTimeByDelay(time_t delay); 119 std::shared_ptr<EventLoop> GetWorkLoop(); 120 GetHiviewContext()121 HiviewContext* GetHiviewContext() 122 { 123 return context_; 124 }; 125 SetHiviewContext(HiviewContext * context)126 void SetHiviewContext(HiviewContext* context) 127 { 128 std::call_once(contextSetFlag_, [&]() { context_ = context; }); 129 }; 130 SetHandle(DynamicModule handle)131 void SetHandle(DynamicModule handle) 132 { 133 handle_ = handle; 134 if (handle_ != nullptr) { 135 type_ = PluginType::DYNAMIC; 136 } 137 }; 138 SetBundleName(const std::string & bundle)139 void SetBundleName(const std::string& bundle) 140 { 141 bundle_ = bundle; 142 } 143 IsBundlePlugin()144 bool IsBundlePlugin() 145 { 146 return bundle_.empty(); 147 } 148 HasLoaded()149 bool HasLoaded() 150 { 151 return loaded_; 152 } 153 SetType(PluginType type)154 void SetType(PluginType type) 155 { 156 type_ = type; 157 } 158 GetType()159 PluginType GetType() const 160 { 161 return type_; 162 } 163 GetLastActiveTime()164 time_t GetLastActiveTime() const 165 { 166 return lastActiveTime_; 167 } 168 GetUseCount()169 int64_t GetUseCount() const 170 { 171 return useCount_; 172 } 173 AddUseCount()174 void AddUseCount() 175 { 176 ++useCount_; 177 } 178 SubUseCount()179 void SubUseCount() 180 { 181 --useCount_; 182 } 183 protected: 184 std::string name_; 185 std::string bundle_; 186 std::string version_; 187 std::shared_ptr<EventLoop> workLoop_; 188 PluginType type_ = PluginType::STATIC; 189 std::atomic<time_t> lastActiveTime_; 190 191 private: 192 DynamicModule handle_; 193 // the reference of the plugin platform 194 // always available in framework callbacks 195 HiviewContext* context_; 196 std::once_flag contextSetFlag_; 197 std::atomic<bool> loaded_; 198 std::atomic<int64_t> useCount_; 199 }; 200 class HiviewContext { 201 public: 202 struct InstanceInfo { 203 std::weak_ptr<Plugin> plugin; 204 std::weak_ptr<EventListener> listener; 205 bool isPlugin; 206 std::string name; 207 }; 208 ~HiviewContext()209 virtual ~HiviewContext(){}; 210 // post event to broadcast queue, the event will be delivered to all plugin that concern this event PostUnorderedEvent(std::shared_ptr<Plugin> plugin __UNUSED,std::shared_ptr<Event> event __UNUSED)211 virtual void PostUnorderedEvent(std::shared_ptr<Plugin> plugin __UNUSED, std::shared_ptr<Event> event __UNUSED) {}; 212 213 // register listener to unordered broadcast queue RegisterUnorderedEventListener(std::weak_ptr<EventListener> listener __UNUSED)214 virtual void RegisterUnorderedEventListener(std::weak_ptr<EventListener> listener __UNUSED) {}; 215 216 // register dynamic listener to queue RegisterDynamicListenerInfo(std::weak_ptr<Plugin> listener __UNUSED)217 virtual void RegisterDynamicListenerInfo(std::weak_ptr<Plugin> listener __UNUSED) {}; 218 219 // send a event to a specific plugin and wait the return of the OnEvent. PostSyncEventToTarget(std::shared_ptr<Plugin> caller __UNUSED,const std::string & callee __UNUSED,std::shared_ptr<Event> event __UNUSED)220 virtual bool PostSyncEventToTarget(std::shared_ptr<Plugin> caller __UNUSED, const std::string& callee __UNUSED, 221 std::shared_ptr<Event> event __UNUSED) 222 { 223 return true; 224 } 225 226 // send a event to a specific plugin and return at once PostAsyncEventToTarget(std::shared_ptr<Plugin> caller __UNUSED,const std::string & callee __UNUSED,std::shared_ptr<Event> event __UNUSED)227 virtual void PostAsyncEventToTarget(std::shared_ptr<Plugin> caller __UNUSED, const std::string& callee __UNUSED, 228 std::shared_ptr<Event> event __UNUSED) {}; 229 230 // post event to distributed communicator plugin, to send to remote device PostEventToRemote(std::shared_ptr<Plugin> caller __UNUSED,const std::string & deviceId __UNUSED,const std::string & targetPlugin __UNUSED,std::shared_ptr<Event> event __UNUSED)231 virtual int32_t PostEventToRemote(std::shared_ptr<Plugin> caller __UNUSED, const std::string& deviceId __UNUSED, 232 const std::string& targetPlugin __UNUSED, std::shared_ptr<Event> event __UNUSED) 233 { 234 return 0; 235 } 236 // request plugin platform to release plugin instance 237 // do not recommend unload an plugin in pipeline scheme 238 // platform will check the reference count if other module still holding the reference of this module RequestUnloadPlugin(std::shared_ptr<Plugin> caller __UNUSED)239 virtual void RequestUnloadPlugin(std::shared_ptr<Plugin> caller __UNUSED) {}; 240 241 // publish plugin capacity and get remote capacity PublishPluginCapacity(PluginCapacityInfo & pluginCapacityInfo __UNUSED)242 virtual void PublishPluginCapacity(PluginCapacityInfo &pluginCapacityInfo __UNUSED) {}; GetRemoteByCapacity(const std::string & plugin __UNUSED,const std::string & capacity __UNUSED,std::list<std::string> & deviceIdList __UNUSED)243 virtual void GetRemoteByCapacity(const std::string& plugin __UNUSED, const std::string& capacity __UNUSED, 244 std::list<std::string> &deviceIdList __UNUSED) {}; 245 246 // get the shared event loop reference GetSharedWorkLoop()247 virtual std::shared_ptr<EventLoop> GetSharedWorkLoop() 248 { 249 return nullptr; 250 } 251 252 // get predefined pipeline list GetPipelineSequenceByName(const std::string & name __UNUSED)253 virtual std::list<std::weak_ptr<Plugin>> GetPipelineSequenceByName(const std::string& name __UNUSED) 254 { 255 return std::list<std::weak_ptr<Plugin>>(0); 256 } 257 258 // check if all non-pending loaded plugins are loaded IsReady()259 virtual bool IsReady() 260 { 261 return false; 262 } 263 264 enum class DirectoryType { 265 CONFIG_DIRECTORY, 266 WORK_DIRECTORY, 267 PERSIST_DIR, 268 }; 269 // get hiview available directory GetHiViewDirectory(DirectoryType type __UNUSED)270 virtual std::string GetHiViewDirectory(DirectoryType type __UNUSED) 271 { 272 return ""; 273 } GetHiviewProperty(const std::string & key __UNUSED,const std::string & defaultValue)274 virtual std::string GetHiviewProperty(const std::string& key __UNUSED, const std::string& defaultValue) 275 { 276 return defaultValue; 277 } 278 SetHiviewProperty(const std::string & key __UNUSED,const std::string & value __UNUSED,bool forceUpdate __UNUSED)279 virtual bool SetHiviewProperty(const std::string& key __UNUSED, const std::string& value __UNUSED, 280 bool forceUpdate __UNUSED) 281 { 282 return true; 283 } 284 AppendPluginToPipeline(const std::string & pluginName __UNUSED,const std::string & pipelineName __UNUSED)285 virtual void AppendPluginToPipeline(const std::string& pluginName __UNUSED, 286 const std::string& pipelineName __UNUSED) {}; RequestLoadBundle(const std::string & bundleName __UNUSED)287 virtual void RequestLoadBundle(const std::string& bundleName __UNUSED) {}; InstancePluginByProxy(std::shared_ptr<Plugin> proxy __UNUSED)288 virtual std::shared_ptr<Plugin> InstancePluginByProxy(std::shared_ptr<Plugin> proxy __UNUSED) 289 { 290 return nullptr; 291 } 292 GetPluginByName(const std::string & name)293 virtual std::shared_ptr<Plugin> GetPluginByName(const std::string& name) 294 { 295 return nullptr; 296 } 297 AddListenerInfo(uint32_t type,const std::string & name,const std::set<std::string> & eventNames,const std::set<EventListener::EventIdRange> & listenerInfo)298 virtual void AddListenerInfo(uint32_t type, const std::string& name, 299 const std::set<std::string>& eventNames, const std::set<EventListener::EventIdRange>& listenerInfo) {}; 300 AddListenerInfo(uint32_t type,std::weak_ptr<Plugin> plugin,const std::set<std::string> & eventNames,const std::set<EventListener::EventIdRange> & listenerInfo)301 virtual void AddListenerInfo(uint32_t type, std::weak_ptr<Plugin> plugin, 302 const std::set<std::string>& eventNames, const std::set<EventListener::EventIdRange>& listenerInfo) {}; 303 GetListenerInfo(uint32_t type,const std::string & eventNames,uint32_t eventId)304 virtual std::vector<std::weak_ptr<InstanceInfo>> GetListenerInfo(uint32_t type, 305 const std::string& eventNames, uint32_t eventId) 306 { 307 return std::vector<std::weak_ptr<InstanceInfo>>(); 308 } 309 GetListenerInfo(uint32_t type,const std::string & name,std::set<EventListener::EventIdRange> & listenerInfo)310 virtual bool GetListenerInfo(uint32_t type, const std::string& name, 311 std::set<EventListener::EventIdRange> &listenerInfo) 312 { 313 return false; 314 } 315 GetListenerInfo(uint32_t type,const std::string & name,std::set<std::string> & eventNames)316 virtual bool GetListenerInfo(uint32_t type, const std::string& name, std::set<std::string> &eventNames) 317 { 318 return false; 319 } 320 }; 321 } // namespace HiviewDFX 322 } // namespace OHOS 323 #endif // HIVIEW_BASE_PLUGIN_H 324