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