• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #ifndef FOUNDATION_RESOURCESCHEDULE_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H
17 #define FOUNDATION_RESOURCESCHEDULE_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H
18 
19 #include <functional>
20 #include <list>
21 #include <string>
22 #include <memory>
23 #include <map>
24 #include "event_handler.h"
25 #include "config_reader.h"
26 #include "plugin_switch.h"
27 #include "plugin.h"
28 #include "nocopyable.h"
29 #include "res_data.h"
30 #include "single_instance.h"
31 #include "config_info.h"
32 
33 namespace OHOS {
34 namespace ResourceSchedule {
35 using OnDispatchResourceFunc = void (*)(const std::shared_ptr<ResData>&);
36 using OnPluginDisableFunc = void (*)();
37 
38 struct PluginLib {
39     std::shared_ptr<void> handle = nullptr;
40     OnDispatchResourceFunc onDispatchResourceFunc_;
41     OnPluginDisableFunc onPluginDisableFunc_;
42 };
43 
44 class PluginMgr {
45     DECLARE_SINGLE_INSTANCE_BASE(PluginMgr);
46 
47 public:
48     ~PluginMgr();
49 
50     /**
51      * Init pluginmanager, load xml config file, construct plugin instances.
52      */
53     void Init();
54 
55     /**
56      * Disable all plugins, maybe service exception happens or stopped.
57      */
58     void Stop();
59 
60     /**
61      * receive all reported resource data, then dispatch all plugins.
62      *
63      * @param resData Reported resource data.
64      */
65     void DispatchResource(const std::shared_ptr<ResData>& resData);
66 
67     /**
68      * Subscribe resource type from plugin.
69      *
70      * @param pluginLib The lib name of plugin.
71      * @param resType interested in resource type.
72      */
73     void SubscribeResource(const std::string& pluginLib, uint32_t resType);
74 
75     /**
76      * Unsubscribe resource type from plugin.
77      *
78      * @param pluginLib The lib name of plugin.
79      * @param resType interested in resource type.
80      */
81     void UnSubscribeResource(const std::string& pluginLib, uint32_t resType);
82 
83     PluginConfig GetConfig(const std::string& pluginName, const std::string& configName);
84 
85 private:
86     PluginMgr() = default;
87     void OnDestroy();
88     void LoadPlugin();
89     void UnLoadPlugin();
90     void ClearResource();
91     void deliverResourceToPlugin(const std::string& pluginLib, const std::shared_ptr<ResData>& resData);
92 
93     using DlHandle = void*;
94 
95     static void CloseHandle(const DlHandle& handle);
96 
97     std::unique_ptr<ConfigReader> configReader_ = nullptr;
98     std::unique_ptr<PluginSwitch> pluginSwitch_ = nullptr;
99 
100     std::mutex pluginMutex_;
101     std::mutex dispatcherHandlerMutex_;
102     std::map<std::string, PluginLib> pluginLibMap_;
103 
104     // mutex for resTypeMap_
105     std::mutex resTypeMutex_;
106     std::map<uint32_t, std::list<std::string>> resTypeLibMap_;
107 
108     // handler use for dispatch resource data
109     std::shared_ptr<OHOS::AppExecFwk::EventHandler> dispatcherHandler_ = nullptr;
110 };
111 } // namespace ResourceSchedule
112 } // namespace OHOS
113 
114 #endif // FOUNDATION_RESOURCESCHEDULE_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H