• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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     void AddDispatchInfo(const std::unordered_set<uint8_t>& types,
100         const std::unordered_set<std::string> &eventNames = {}, const std::unordered_set<std::string> &tags = {},
101             const std::unordered_map<std::string, DomainRule>& domainRulesMap = {});
102 
103     // reinsert the event into the workloop
104     // delay in seconds
105     void DelayProcessEvent(std::shared_ptr<Event> event, uint64_t delay);
106 
107     const std::string& GetName();
108     const std::string& GetVersion();
109     void SetName(const std::string& name);
110     void SetVersion(const std::string& version);
111     void BindWorkLoop(std::shared_ptr<EventLoop> loop);
112     void UpdateActiveTime();
113     void UpdateTimeByDelay(time_t delay);
114     std::shared_ptr<EventLoop> GetWorkLoop();
115 
GetHiviewContext()116     HiviewContext* GetHiviewContext()
117     {
118         return context_;
119     };
120 
SetHiviewContext(HiviewContext * context)121     void SetHiviewContext(HiviewContext* context)
122     {
123         std::call_once(contextSetFlag_, [&]() { context_ = context; });
124     };
125 
SetHandle(DynamicModule handle)126     void SetHandle(DynamicModule handle)
127     {
128         handle_ = handle;
129         if (handle_ != nullptr) {
130             type_ = PluginType::DYNAMIC;
131         }
132     };
133 
SetBundleName(const std::string & bundle)134     void SetBundleName(const std::string& bundle)
135     {
136         bundle_ = bundle;
137     }
138 
IsBundlePlugin()139     bool IsBundlePlugin()
140     {
141         return !bundle_.empty();
142     }
143 
HasLoaded()144     bool HasLoaded()
145     {
146         return loaded_;
147     }
148 
SetType(PluginType type)149     void SetType(PluginType type)
150     {
151         type_ = type;
152     }
153 
GetType()154     PluginType GetType() const
155     {
156         return type_;
157     }
158 
GetLastActiveTime()159     time_t GetLastActiveTime() const
160     {
161         return lastActiveTime_;
162     }
163 
GetUseCount()164     int64_t GetUseCount() const
165     {
166         return useCount_;
167     }
168 
AddUseCount()169     void AddUseCount()
170     {
171         ++useCount_;
172     }
173 
SubUseCount()174     void SubUseCount()
175     {
176         --useCount_;
177     }
178 protected:
179     std::string name_;
180     std::string bundle_;
181     std::string version_;
182     std::shared_ptr<EventLoop> workLoop_;
183     PluginType type_ = PluginType::STATIC;
184     std::atomic<time_t> lastActiveTime_;
185 
186 private:
187     DynamicModule handle_;
188     // the reference of the plugin platform
189     // always available in framework callbacks
190     HiviewContext* context_;
191     std::once_flag contextSetFlag_;
192     std::atomic<bool> loaded_;
193     std::atomic<int64_t> useCount_;
194 };
195 class HiviewContext {
196 public:
~HiviewContext()197     virtual ~HiviewContext(){};
198     // 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)199     virtual void PostUnorderedEvent(std::shared_ptr<Plugin> plugin __UNUSED, std::shared_ptr<Event> event __UNUSED) {};
200 
201     // register listener to unordered broadcast queue
RegisterUnorderedEventListener(std::weak_ptr<EventListener> listener __UNUSED)202     virtual void RegisterUnorderedEventListener(std::weak_ptr<EventListener> listener __UNUSED) {};
203 
204     // 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)205     virtual bool PostSyncEventToTarget(std::shared_ptr<Plugin> caller __UNUSED, const std::string& callee __UNUSED,
206                                        std::shared_ptr<Event> event __UNUSED)
207     {
208         return true;
209     }
210 
211     // 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)212     virtual void PostAsyncEventToTarget(std::shared_ptr<Plugin> caller __UNUSED, const std::string& callee __UNUSED,
213                                         std::shared_ptr<Event> event __UNUSED) {};
214 
215     // 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)216     virtual int32_t PostEventToRemote(std::shared_ptr<Plugin> caller __UNUSED, const std::string& deviceId __UNUSED,
217         const std::string& targetPlugin __UNUSED, std::shared_ptr<Event> event __UNUSED)
218     {
219         return 0;
220     }
221     // request plugin platform to release plugin instance
222     // do not recommend unload an plugin in pipeline scheme
223     // platform will check the reference count if other module still holding the reference of this module
RequestUnloadPlugin(std::shared_ptr<Plugin> caller __UNUSED)224     virtual void RequestUnloadPlugin(std::shared_ptr<Plugin> caller __UNUSED) {};
225 
226     // publish plugin capacity and get remote capacity
PublishPluginCapacity(PluginCapacityInfo & pluginCapacityInfo __UNUSED)227     virtual void PublishPluginCapacity(PluginCapacityInfo &pluginCapacityInfo __UNUSED) {};
GetRemoteByCapacity(const std::string & plugin __UNUSED,const std::string & capacity __UNUSED,std::list<std::string> & deviceIdList __UNUSED)228     virtual void GetRemoteByCapacity(const std::string& plugin __UNUSED, const std::string& capacity __UNUSED,
229         std::list<std::string> &deviceIdList __UNUSED) {};
230 
231     // get the shared event loop reference
GetSharedWorkLoop()232     virtual std::shared_ptr<EventLoop> GetSharedWorkLoop()
233     {
234         return nullptr;
235     }
236 
237     // get predefined pipeline list
GetPipelineSequenceByName(const std::string & name __UNUSED)238     virtual std::list<std::weak_ptr<Plugin>> GetPipelineSequenceByName(const std::string& name __UNUSED)
239     {
240         return std::list<std::weak_ptr<Plugin>>(0);
241     }
242 
243     // check if all non-pending loaded plugins are loaded
IsReady()244     virtual bool IsReady()
245     {
246         return false;
247     }
248 
249     enum class DirectoryType {
250         CONFIG_DIRECTORY,
251         WORK_DIRECTORY,
252         PERSIST_DIR,
253     };
254     // get hiview available directory
GetHiViewDirectory(DirectoryType type __UNUSED)255     virtual std::string GetHiViewDirectory(DirectoryType type __UNUSED)
256     {
257         return "";
258     }
GetHiviewProperty(const std::string & key __UNUSED,const std::string & defaultValue)259     virtual std::string GetHiviewProperty(const std::string& key __UNUSED, const std::string& defaultValue)
260     {
261         return defaultValue;
262     }
263 
SetHiviewProperty(const std::string & key __UNUSED,const std::string & value __UNUSED,bool forceUpdate __UNUSED)264     virtual bool SetHiviewProperty(const std::string& key __UNUSED, const std::string& value __UNUSED,
265         bool forceUpdate __UNUSED)
266     {
267         return true;
268     }
269 
AppendPluginToPipeline(const std::string & pluginName __UNUSED,const std::string & pipelineName __UNUSED)270     virtual void AppendPluginToPipeline(const std::string& pluginName __UNUSED,
271                                         const std::string& pipelineName __UNUSED) {};
RequestLoadBundle(const std::string & bundleName __UNUSED)272     virtual void RequestLoadBundle(const std::string& bundleName __UNUSED) {};
273 
274     // request plugin platform to release plugin bundle
275     virtual void RequestUnloadBundle(const std::string& bundleName, uint64_t delay = 0) {};
276 
InstancePluginByProxy(std::shared_ptr<Plugin> proxy __UNUSED)277     virtual std::shared_ptr<Plugin> InstancePluginByProxy(std::shared_ptr<Plugin> proxy __UNUSED)
278     {
279         return nullptr;
280     }
281 
GetPluginByName(const std::string & name)282     virtual std::shared_ptr<Plugin> GetPluginByName(const std::string& name)
283     {
284         return nullptr;
285     }
286 
AddListenerInfo(uint32_t type,const std::string & name,const std::set<std::string> & eventNames,const std::map<std::string,DomainRule> & domainRulesMap)287     virtual void AddListenerInfo(uint32_t type, const std::string& name, const std::set<std::string>& eventNames,
288         const std::map<std::string, DomainRule>& domainRulesMap) {};
289 
AddListenerInfo(uint32_t type,const std::string & name)290     virtual void AddListenerInfo(uint32_t type, const std::string& name) {};
291 
GetListenerInfo(uint32_t type,const std::string & eventName,const std::string & domain)292     virtual std::vector<std::weak_ptr<EventListener>> GetListenerInfo(uint32_t type,
293         const std::string& eventName, const std::string& domain)
294     {
295         return {};
296     }
297 
AddDispatchInfo(std::weak_ptr<Plugin> plugin,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)298     virtual void AddDispatchInfo(std::weak_ptr<Plugin> plugin, const std::unordered_set<uint8_t>& types,
299         const std::unordered_set<std::string>& eventNames, const std::unordered_set<std::string>& tags,
300             const std::unordered_map<std::string, DomainRule>& domainRulesMap) {};
301 
GetDisPatcherInfo(uint32_t type,const std::string & eventName,const std::string & tag,const std::string & domain)302     virtual std::vector<std::weak_ptr<Plugin>> GetDisPatcherInfo(uint32_t type,
303         const std::string& eventName, const std::string& tag, const std::string& domain)
304     {
305         return {};
306     }
307 };
308 } // namespace HiviewDFX
309 } // namespace OHOS
310 #endif // HIVIEW_BASE_PLUGIN_H
311