• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #ifndef SERVICES_EDM_INCLUDE_EDM_PLUGIN_SINGLETON_H
17 #define SERVICES_EDM_INCLUDE_EDM_PLUGIN_SINGLETON_H
18 
19 #include "iplugin_template.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 /*
24  * Policy processing plugin singleton mode template,which needs to inherit the template.
25  *
26  * @tparam CT Policy processing logic class, which is the code to be implemented.
27  * @tparam DT Policy data type, for example:string,vector<string>,map<string,string>...
28  */
29 template <typename CT, typename DT>
30 class PluginSingleton : public NoCopyable {
31 public:
32     /*
33      * Initialize the plugin. The subclass needs to implement the pure virtual
34      * function, define the code,name and permission of the plugin, and listen
35      * to policy events.
36      *
37      * @param ptr std::shared_ptr<IPluginTemplate<CT, DT>>
38      */
39     virtual void InitPlugin(std::shared_ptr<IPluginTemplate<CT, DT>> ptr) = 0;
40 
41     /*
42      * Obtains the singleton of the plugin interface.
43      *
44      * @return std::shared_ptr<IPlugin>
45      */
46     static std::shared_ptr<IPlugin> GetPlugin();
47 
48     virtual ErrCode OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId);
49 
50     static void DestroyPlugin();
51 
52 protected:
53     static std::shared_ptr<IPluginTemplate<CT, DT>> pluginInstance_;
54     static std::mutex mutexLock_;
55 };
56 
57 template <typename CT, typename DT>
58 std::shared_ptr<IPluginTemplate<CT, DT>> PluginSingleton<CT, DT>::pluginInstance_ = nullptr;
59 
60 template <typename CT, typename DT>
61 std::mutex PluginSingleton<CT, DT>::mutexLock_;
62 
63 template <typename CT, typename DT>
GetPlugin()64 std::shared_ptr<IPlugin> PluginSingleton<CT, DT>::GetPlugin()
65 {
66     if (pluginInstance_ == nullptr) {
67         std::lock_guard<std::mutex> lock(mutexLock_);
68         if (pluginInstance_ == nullptr) {
69             std::shared_ptr<CT> ptr = std::make_shared<CT>();
70             pluginInstance_ = std::make_shared<IPluginTemplate<CT, DT>>();
71             pluginInstance_->SetInstance(ptr);
72             ptr->InitPlugin(pluginInstance_);
73         }
74     }
75     return pluginInstance_;
76 }
77 
78 template <typename CT, typename DT>
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)79 ErrCode PluginSingleton<CT, DT>::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
80     int32_t userId)
81 {
82     EDMLOGI("PluginSingleton::OnGetPolicy");
83     return ERR_OK;
84 }
85 
86 template <typename CT, typename DT>
DestroyPlugin()87 void PluginSingleton<CT, DT>::DestroyPlugin()
88 {
89     std::lock_guard<std::mutex> lock(mutexLock_);
90     if (pluginInstance_ != nullptr) {
91         pluginInstance_.reset();
92     }
93 }
94 
95 } // namespace EDM
96 } // namespace OHOS
97 #endif // SERVICES_EDM_INCLUDE_EDM_PLUGIN_SINGLETON_H