• 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 SERVICES_EDM_INCLUDE_EDM_IPLUGIN_TEMPLATE_H
17 #define SERVICES_EDM_INCLUDE_EDM_IPLUGIN_TEMPLATE_H
18 
19 #include <functional>
20 #include "edm_log.h"
21 #include "iplugin.h"
22 #include "ipolicy_serializer.h"
23 #include "policy_manager.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 /*
28  * Policy processing template.Implements the IPlugin interface and
29  * provides the event registration method for policy processing.
30  *
31  * @tparam CT Policy processing logic class, which is the code to be implemented.
32  * @tparam DT Policy data type, for example:string,vector<string>,map<string,string>...
33  */
34 template<class CT, class DT>
35 class IPluginTemplate : public IPlugin {
36 public:
37     ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, std::string &policyData,
38         bool &isChanged) override;
39 
40     ErrCode MergePolicyData(const std::string &adminName, std::string &policyData) override;
41 
42     void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName,
43         bool isGlobalChanged) override;
44 
45     ErrCode OnAdminRemove(const std::string &adminName, const std::string &currentJsonData) override;
46 
47     void OnAdminRemoveDone(const std::string &adminName, const std::string &removedJsonData) override;
48 
49     ErrCode WritePolicyToParcel(const std::string &policyData, MessageParcel &reply) override;
50 
51     /*
52      * Sets the handle of the policy processing object.
53      *
54      * @param instance std::shared_ptr<CT>
55      */
56     void SetInstance(std::shared_ptr<CT> instance);
57 
58     /*
59      * Define CT as the friend class of IPluginTemplate.
60      */
61     friend CT;
62 
63     IPluginTemplate();
64 
65     ~IPluginTemplate();
66 
67 protected:
68     /*
69      * Represents a function that invoked during handling policy.
70      *
71      * @param one MessageParcel
72      * @param two Current policy data
73      * @param three Whether the policy data is changed
74      * @param four Handle type
75      * @see OnHandlePolicy
76      * @see HandlePolicyFunc
77      */
78     typedef std::function<ErrCode(MessageParcel &, std::string &, bool &, FuncOperateType)> HandlePolicy;
79 
80     /*
81      * Represents a function that invoked during handling policy.
82      *
83      * @param one Admin name
84      * @param two Whether the policy data is changed
85      * @param three Handle type
86      * @see OnHandlePolicyDone
87      * @see HandlePolicyDoneFunc
88      */
89     typedef std::function<ErrCode(const std::string &, bool, FuncOperateType)> HandlePolicyDone;
90 
91     /*
92      * Represents a function that invoked during the removal of admin.
93      *
94      * @see OnAdminRemove
95      * @see AdminRemoveFunc
96      */
97     typedef std::function<ErrCode(const std::string &, const std::string &)> AdminRemove;
98 
99     /*
100      * Represents a function that invoked after the admin is removed.
101      *
102      * @see OnAdminRemoveDone
103      * @see AdminRemoveDoneFunc
104      */
105     typedef std::function<ErrCode(const std::string &, const std::string &)> AdminRemoveDone;
106 
107     /*
108      * This is a member function pointer type of CT class.
109      * Represents a supplier of results. There is no requirement that a ErrCode result be returned each time the
110      * supplier is invoked.
111      * It is generally used in scenarios where only return values need to be processed and parameters are not concerned.
112      *
113      * @return Whether the policy is handled successfully.
114      * @see SetOnHandlePolicyListener
115      * @see SetOnAdminRemoveListener
116      */
117     typedef ErrCode (CT::*Supplier)();
118 
119     /*
120      * This is a member function pointer type of CT class.
121      * Represents a function that accepts one DT argument and produces a ErrCode result.
122      * It is generally used in the scenario where only one DT parameter and return value need to be processed.
123      *
124      * @param data In: Input policy data parameter,Out: Used to update the admin data.
125      * @return Whether the policy is handled successfully.
126      * @see SetOnHandlePolicyListener
127      */
128     typedef ErrCode (CT::*Function)(DT &data);
129 
130     /*
131      * This is a member function pointer type of CT class.
132      * Represents a function that accepts two DT arguments and produces a ErrCode result.
133      *
134      * @param data Input policy data parameter
135      * @param currentData In: data of the current admin,Out: Used to update the admin data.
136      * @return Whether the policy is handled successfully.
137      * @see SetOnHandlePolicyListener
138      */
139     typedef ErrCode (CT::*BiFunction)(DT &data, DT &currentData);
140 
141     /*
142      * This is a member function pointer type of CT class.
143      * Represents a function that accepts an string-valued and a DT-valued argument, and produces a ErrCode result.
144      *
145      * @param adminName Admin name
146      * @param data Admin policy data
147      * @see SetOnAdminRemoveListener
148      */
149     typedef ErrCode (CT::*BiAdminFunction)(const std::string &adminName, DT &data);
150 
151     /*
152      * This is a member function pointer type of CT class.
153      * Represents an operation that accepts a single bool input argument and returns no result.
154      *
155      * @param isGlobalChanged Whether the policy data is changed
156      * @see SetOnHandlePolicyDoneListener
157      */
158     typedef void (CT::*BoolConsumer)(bool isGlobalChanged);
159 
160     /*
161      * This is a member function pointer type of CT class.
162      * Represents an operation that accepts an DT-valued and a bool-valued argument, and returns no result.
163      *
164      * @param data Admin policy data
165      * @param isGlobalChanged Whether the policy data is changed
166      * @see SetOnHandlePolicyDoneListener
167      */
168     typedef void (CT::*BiBoolConsumer)(DT &data, bool isGlobalChanged);
169 
170     /*
171      * This is a member function pointer type of CT class.
172      * Represents an operation that accepts no arguments and returns no result.
173      *
174      * @param data Admin policy data
175      * @param isGlobalChanged Whether the policy data is changed
176      * @see SetOnAdminRemoveDoneListener
177      */
178     typedef void (CT::*Runner)();
179 
180     /*
181      * This is a member function pointer type of CT class.
182      * Represents an operation that accepts an string-valued and a DT-valued argument, and returns no result.
183      *
184      * @param adminName Admin name
185      * @param data Admin policy data
186      * @see SetOnAdminRemoveDoneListener
187      */
188     typedef void (CT::*BiAdminConsumer)(const std::string &adminName, DT &data);
189 
190     virtual bool GetMergePolicyData(DT &policyData);
191 
192     void SetSerializer(std::shared_ptr<IPolicySerializer<DT>> serializer);
193 
194     void InitAttribute(uint32_t policyCode, const std::string &policyName, const std::string &permission,
195         bool needSave = true, bool global = true);
196 
197     /*
198      * Registering Listening for HandlePolicy Events.
199      *
200      * @param listener Listening member function pointer of CT Class
201      * @param type Policy Data Processing Mode
202      * @see FuncOperateType
203      */
204     void SetOnHandlePolicyListener(Supplier &&listener, FuncOperateType type);
205 
206     /*
207      * Registering Listening for HandlePolicy Events.
208      *
209      * @param listener Listening member function pointer of CT Class
210      * @param type Policy Data Processing Mode,default FuncOperateType::REMOVE
211      * @see FuncOperateType
212      */
213     void SetOnHandlePolicyListener(Function &&listener, FuncOperateType type);
214 
215     /*
216      * Registering Listening for HandlePolicy Events.
217      *
218      * @param listener Listening member function pointer of CT Class
219      * @param type Policy Data Processing Mode,default FuncOperateType::SET
220      * @see FuncOperateType
221      */
222     void SetOnHandlePolicyListener(BiFunction &&listener, FuncOperateType type);
223 
224     /*
225      * Registering listening for HandlePolicyDone events.
226      *
227      * @param listener Listening member function pointer of CT Class
228      * @param type Policy Data Processing Mode
229      */
230     void SetOnHandlePolicyDoneListener(BoolConsumer &&listener, FuncOperateType type);
231 
232     /*
233      * Registering listening for HandlePolicyDone events.
234      *
235      * @param listener Listening member function pointer of CT Class
236      * @param type Policy Data Processing Mode
237      */
238     void SetOnHandlePolicyDoneListener(BiBoolConsumer &&listener, FuncOperateType type);
239 
240     /*
241      * Registering listening for AdminRemove events.
242      *
243      * @param listener Listening member function pointer of CT Class
244      */
245     void SetOnAdminRemoveListener(Supplier &&listener);
246 
247     /*
248      * Registering listening for AdminRemove events.
249      *
250      * @param listener Listening member function pointer of CT Class
251      */
252     void SetOnAdminRemoveListener(BiAdminFunction &&listener);
253 
254     /*
255      * Registering listening for AdminRemoveDone events.
256      *
257      * @param listener Listening member function pointer of CT Class
258      */
259     void SetOnAdminRemoveDoneListener(Runner &&listener);
260 
261     /*
262      * Registering listening for AdminRemoveDone events.
263      *
264      * @param listener Listening member function pointer of CT Class
265      */
266     void SetOnAdminRemoveDoneListener(BiAdminConsumer &&listener);
267 
268     /*
269      * Mapping between HandlePolicy and member function types that support overloading.
270      */
271     struct HandlePolicyFunc {
272         HandlePolicy handlePolicy_ = nullptr;
273         Supplier supplier_ = nullptr;
274         Function function_ = nullptr;
275         BiFunction biFunction_ = nullptr;
276 
HandlePolicyFuncHandlePolicyFunc277         HandlePolicyFunc() {}
278 
HandlePolicyFuncHandlePolicyFunc279         HandlePolicyFunc(HandlePolicy handlePolicy, Supplier supplier)
280             : handlePolicy_(std::move(handlePolicy)), supplier_(supplier) {}
281 
HandlePolicyFuncHandlePolicyFunc282         HandlePolicyFunc(HandlePolicy handlePolicy, Function function)
283             : handlePolicy_(std::move(handlePolicy)), function_(function) {}
284 
HandlePolicyFuncHandlePolicyFunc285         HandlePolicyFunc(HandlePolicy handlePolicy, BiFunction biFunction)
286             : handlePolicy_(std::move(handlePolicy)), biFunction_(biFunction) {}
287     };
288 
289     // Member function callback object of the HandlePolicy event.
290     std::map<FuncOperateType, HandlePolicyFunc> handlePolicyFuncMap_;
291 
292     /*
293      * Mapping between HandlePolicyDone and member function types that support overloading.
294      */
295     struct HandlePolicyDoneFunc {
296         HandlePolicyDone handlePolicyDone_ = nullptr;
297         BoolConsumer boolConsumer_ = nullptr;
298         BiBoolConsumer biBoolConsumer_ = nullptr;
299 
HandlePolicyDoneFuncHandlePolicyDoneFunc300         HandlePolicyDoneFunc() {}
301 
HandlePolicyDoneFuncHandlePolicyDoneFunc302         HandlePolicyDoneFunc(HandlePolicyDone handlePolicyDone, BoolConsumer boolConsumer)
303             : handlePolicyDone_(std::move(handlePolicyDone)), boolConsumer_(boolConsumer) {}
304 
HandlePolicyDoneFuncHandlePolicyDoneFunc305         HandlePolicyDoneFunc(HandlePolicyDone handlePolicyDone, BiBoolConsumer biBoolConsumer)
306             : handlePolicyDone_(std::move(handlePolicyDone)), biBoolConsumer_(biBoolConsumer) {}
307     };
308 
309     // Member function callback object of the HandlePolicyDone event.
310     std::map<FuncOperateType, HandlePolicyDoneFunc> handlePolicyDoneFuncMap_;
311 
312     /*
313      * Mapping between AdminRemove and member function types that support overloading.
314      */
315     struct AdminRemoveFunc {
316         AdminRemove adminRemove_ = nullptr;
317         Supplier supplier_ = nullptr;
318         BiAdminFunction biAdminFunction_ = nullptr;
319 
AdminRemoveFuncAdminRemoveFunc320         AdminRemoveFunc() {}
321 
AdminRemoveFuncAdminRemoveFunc322         AdminRemoveFunc(AdminRemove adminRemove, Supplier supplier)
323             : adminRemove_(std::move(adminRemove)), supplier_(supplier) {}
324 
AdminRemoveFuncAdminRemoveFunc325         AdminRemoveFunc(AdminRemove adminRemove, BiAdminFunction biAdminFunction)
326             : adminRemove_(std::move(adminRemove)), biAdminFunction_(biAdminFunction) {}
327     };
328 
329     // Member function callback object of the AdminRemove event.
330     AdminRemoveFunc adminRemoveFunc_;
331 
332     /*
333      * Mapping between AdminRemoveDone and member function types that support overloading.
334      */
335     struct AdminRemoveDoneFunc {
336         AdminRemoveDone adminRemoveDone_ = nullptr;
337         Runner runner_ = nullptr;
338         BiAdminConsumer biAdminConsumer_ = nullptr;
339 
AdminRemoveDoneFuncAdminRemoveDoneFunc340         AdminRemoveDoneFunc() {}
341 
AdminRemoveDoneFuncAdminRemoveDoneFunc342         AdminRemoveDoneFunc(AdminRemoveDone adminRemoveDone, Runner runner)
343             : adminRemoveDone_(std::move(adminRemoveDone)), runner_(runner) {}
344 
AdminRemoveDoneFuncAdminRemoveDoneFunc345         AdminRemoveDoneFunc(AdminRemoveDone adminRemoveDone, BiAdminConsumer biAdminConsumer)
346             : adminRemoveDone_(std::move(adminRemoveDone)), biAdminConsumer_(biAdminConsumer) {}
347     };
348 
349     // Member function callback object of the AdminRemoveDone event.
350     AdminRemoveDoneFunc adminRemoveDoneFunc_;
351     // Pointer to the callback member function.
352     std::shared_ptr<CT> instance_;
353     // Data serializer for policy data
354     std::shared_ptr<IPolicySerializer<DT>> serializer_;
355 };
356 
357 template<class CT, class DT>
IPluginTemplate()358 IPluginTemplate<CT, DT>::IPluginTemplate() {}
359 
360 template<class CT, class DT>
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,std::string & policyData,bool & isChanged)361 ErrCode IPluginTemplate<CT, DT>::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, std::string &policyData,
362     bool &isChanged)
363 {
364     uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
365     FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
366     auto entry = handlePolicyFuncMap_.find(type);
367     if (entry == handlePolicyFuncMap_.end() || entry->second.handlePolicy_ == nullptr) {
368         return ERR_OK;
369     }
370     ErrCode res = entry->second.handlePolicy_(data, policyData, isChanged, type);
371     EDMLOGI("IPluginTemplate::OnHandlePolicy operate: %{public}d, res: %{public}d", type, res);
372     return res;
373 }
374 
375 template<class CT, class DT>
SetOnHandlePolicyListener(Supplier && listener,FuncOperateType type)376 void IPluginTemplate<CT, DT>::SetOnHandlePolicyListener(Supplier &&listener, FuncOperateType type)
377 {
378     if (instance_ == nullptr) {
379         return;
380     }
381     auto handle = [this](MessageParcel &data, std::string &policyData, bool &isChanged,
382         FuncOperateType funcOperate) -> ErrCode {
383         auto entry = handlePolicyFuncMap_.find(funcOperate);
384         if (entry == handlePolicyFuncMap_.end() || entry->second.supplier_ == nullptr) {
385             return ERR_EDM_NOT_EXIST_FUNC;
386         }
387         return (instance_.get()->*(entry->second.supplier_))();
388     };
389     handlePolicyFuncMap_.insert(std::make_pair(type, HandlePolicyFunc(handle, listener)));
390 }
391 
392 template<class CT, class DT>
SetOnHandlePolicyListener(Function && listener,FuncOperateType type)393 void IPluginTemplate<CT, DT>::SetOnHandlePolicyListener(Function &&listener, FuncOperateType type)
394 {
395     if (instance_ == nullptr) {
396         return;
397     }
398     auto handle = [this](MessageParcel &data, std::string &policyData, bool &isChanged,
399         FuncOperateType funcOperate) -> ErrCode {
400         DT handleData;
401         if (!serializer_->GetPolicy(data, handleData)) {
402             return ERR_EDM_OPERATE_PARCEL;
403         }
404         auto entry = handlePolicyFuncMap_.find(funcOperate);
405         if (entry == handlePolicyFuncMap_.end() || entry->second.function_ == nullptr) {
406             return ERR_EDM_NOT_EXIST_FUNC;
407         }
408         ErrCode result = (instance_.get()->*(entry->second.function_))(handleData);
409         if (result != ERR_OK) {
410             return result;
411         }
412         std::string afterHandle;
413         if (!serializer_->Serialize(handleData, afterHandle)) {
414             return ERR_EDM_OPERATE_JSON;
415         }
416         isChanged = (policyData != afterHandle);
417         if (isChanged) {
418             policyData = afterHandle;
419         }
420         return ERR_OK;
421     };
422     handlePolicyFuncMap_.insert(std::make_pair(type, HandlePolicyFunc(handle, listener)));
423 }
424 
425 template<class CT, class DT>
SetOnHandlePolicyListener(BiFunction && listener,FuncOperateType type)426 void IPluginTemplate<CT, DT>::SetOnHandlePolicyListener(BiFunction &&listener, FuncOperateType type)
427 {
428     if (instance_ == nullptr || instance_.get() == nullptr) {
429         return;
430     }
431     auto handle = [this](MessageParcel &data, std::string &policyData, bool &isChanged,
432         FuncOperateType funcOperate) -> ErrCode {
433         DT handleData;
434         if (!serializer_->GetPolicy(data, handleData)) {
435             return ERR_EDM_OPERATE_PARCEL;
436         }
437         DT currentData;
438         if (!policyData.empty() && !serializer_->Deserialize(policyData, currentData)) {
439             return ERR_EDM_OPERATE_JSON;
440         }
441         auto entry = handlePolicyFuncMap_.find(funcOperate);
442         if (entry == handlePolicyFuncMap_.end() || entry->second.biFunction_ == nullptr) {
443             return ERR_EDM_NOT_EXIST_FUNC;
444         }
445         std::string beforeHandle;
446         if (!serializer_->Serialize(currentData, beforeHandle)) {
447             return ERR_EDM_OPERATE_JSON;
448         }
449         ErrCode result = (instance_.get()->*(entry->second.biFunction_))(handleData, currentData);
450         if (result != ERR_OK) {
451             return result;
452         }
453         std::string afterHandle;
454         if (!serializer_->Serialize(currentData, afterHandle)) {
455             return ERR_EDM_OPERATE_JSON;
456         }
457         policyData = afterHandle;
458         isChanged = (beforeHandle != afterHandle);
459         return ERR_OK;
460     };
461     handlePolicyFuncMap_.insert(std::make_pair(type, HandlePolicyFunc(handle, listener)));
462 }
463 
464 template<class CT, class DT>
MergePolicyData(const std::string & adminName,std::string & policyData)465 ErrCode IPluginTemplate<CT, DT>::MergePolicyData(const std::string &adminName, std::string &policyData)
466 {
467     AdminValueItemsMap adminValues;
468     PolicyManager::GetInstance()->GetAdminByPolicyName(GetPolicyName(), adminValues);
469     EDMLOGD("IPluginTemplate::MergePolicyData %{public}s value size %{public}d.",
470         GetPolicyName().c_str(), (uint32_t)adminValues.size());
471     if (adminValues.empty()) {
472         return ERR_OK;
473     }
474     // Update or remove policy value from the cache map.
475     auto entry = adminValues.find(adminName);
476     // Remove policy value from the cache map.
477     if (entry != adminValues.end()) {
478         adminValues.erase(entry);
479     }
480     if (adminValues.empty() && policyData.empty()) {
481         return ERR_OK;
482     }
483     std::vector<DT> data;
484     for (const auto &item : adminValues) {
485         DT dataItem;
486         if (!item.second.empty()) {
487             if (!serializer_->Deserialize(item.second, dataItem)) {
488                 return ERR_EDM_OPERATE_JSON;
489             }
490             data.push_back(dataItem);
491         }
492     }
493     // Add current policy to last, some policy must ensure the order, Deserialize can not parse empty String
494     DT last;
495     if (!policyData.empty()) {
496         if (!serializer_->Deserialize(policyData, last)) {
497             return ERR_EDM_OPERATE_JSON;
498         }
499         data.push_back(last);
500     }
501     DT result;
502     if (!serializer_->MergePolicy(data, result)) {
503         return ERR_EDM_OPERATE_JSON;
504     }
505     if (!serializer_->Serialize(result, policyData)) {
506         return ERR_EDM_OPERATE_JSON;
507     }
508     return ERR_OK;
509 }
510 
511 template<class CT, class DT>
OnHandlePolicyDone(std::uint32_t funcCode,const std::string & adminName,const bool isGlobalChanged)512 void IPluginTemplate<CT, DT>::OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName,
513     const bool isGlobalChanged)
514 {
515     uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
516     FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
517     auto entry = handlePolicyDoneFuncMap_.find(type);
518     if (entry == handlePolicyDoneFuncMap_.end() || entry->second.handlePolicyDone_ == nullptr) {
519         return;
520     }
521     ErrCode res = entry->second.handlePolicyDone_(adminName, isGlobalChanged, type);
522     EDMLOGI("IPluginTemplate::OnHandlePolicyDone operate: %{public}d, isGlobalChanged: %{public}d, res: %{public}d",
523         type, isGlobalChanged, res);
524 }
525 
526 template<class CT, class DT>
SetOnHandlePolicyDoneListener(BoolConsumer && listener,FuncOperateType type)527 void IPluginTemplate<CT, DT>::SetOnHandlePolicyDoneListener(BoolConsumer &&listener, FuncOperateType type)
528 {
529     if (instance_ == nullptr) {
530         return;
531     }
532     auto handle = [this](const std::string &adminName, bool isGlobalChanged, FuncOperateType funcOperate) -> ErrCode {
533         auto entry = handlePolicyDoneFuncMap_.find(funcOperate);
534         if (entry == handlePolicyDoneFuncMap_.end() || entry->second.boolConsumer_ == nullptr) {
535             return ERR_EDM_NOT_EXIST_FUNC;
536         }
537         (instance_.get()->*(entry->second.boolConsumer_))(isGlobalChanged);
538         return ERR_OK;
539     };
540     handlePolicyDoneFuncMap_.insert(std::make_pair(type, HandlePolicyDoneFunc(handle, listener)));
541 }
542 
543 template<class CT, class DT>
SetOnHandlePolicyDoneListener(BiBoolConsumer && listener,FuncOperateType type)544 void IPluginTemplate<CT, DT>::SetOnHandlePolicyDoneListener(BiBoolConsumer &&listener, FuncOperateType type)
545 {
546     if (instance_ == nullptr) {
547         return;
548     }
549     auto handle = [this](const std::string &adminName, bool isGlobalChanged, FuncOperateType funcOperate) -> ErrCode {
550         auto entry = handlePolicyDoneFuncMap_.find(funcOperate);
551         if (entry == handlePolicyDoneFuncMap_.end() || entry->second.biBoolConsumer_ == nullptr) {
552             return ERR_EDM_NOT_EXIST_FUNC;
553         }
554         DT currentData;
555         if (NeedSavePolicy() && !this->GetMergePolicyData(currentData)) {
556             return ERR_EDM_OPERATE_JSON;
557         }
558         (instance_.get()->*(entry->second.biBoolConsumer_))(currentData, isGlobalChanged);
559         return ERR_OK;
560     };
561     handlePolicyDoneFuncMap_.insert(std::make_pair(type, HandlePolicyDoneFunc(handle, listener)));
562 }
563 
564 template<class CT, class DT>
OnAdminRemove(const std::string & adminName,const std::string & currentJsonData)565 ErrCode IPluginTemplate<CT, DT>::OnAdminRemove(const std::string &adminName, const std::string &currentJsonData)
566 {
567     if (adminRemoveFunc_.adminRemove_ == nullptr) {
568         return ERR_OK;
569     }
570     ErrCode res = (adminRemoveFunc_.adminRemove_)(adminName, currentJsonData);
571     EDMLOGI("IPluginTemplate::OnAdminRemove admin:%{public}s, res: %{public}d", adminName.c_str(), res);
572     return res;
573 }
574 
575 template<class CT, class DT>
SetOnAdminRemoveListener(Supplier && listener)576 void IPluginTemplate<CT, DT>::SetOnAdminRemoveListener(Supplier &&listener)
577 {
578     if (instance_ == nullptr) {
579         return;
580     }
581     auto adminRemove = [this](const std::string &adminName, const std::string &currentJsonData) -> ErrCode {
582         if (adminRemoveFunc_.supplier_ == nullptr) {
583             return ERR_EDM_NOT_EXIST_FUNC;
584         }
585         return (instance_.get()->*(adminRemoveFunc_.supplier_))();
586     };
587     adminRemoveFunc_ = AdminRemoveFunc(adminRemove, listener);
588 }
589 
590 template<class CT, class DT>
SetOnAdminRemoveListener(BiAdminFunction && listener)591 void IPluginTemplate<CT, DT>::SetOnAdminRemoveListener(BiAdminFunction &&listener)
592 {
593     if (instance_ == nullptr) {
594         return;
595     }
596     auto adminRemove = [this](const std::string &adminName, const std::string &currentJsonData) -> ErrCode {
597         DT currentData;
598         if (!serializer_->Deserialize(currentJsonData, currentData)) {
599             return ERR_EDM_OPERATE_JSON;
600         }
601         if (adminRemoveFunc_.biAdminFunction_ == nullptr) {
602             return ERR_EDM_NOT_EXIST_FUNC;
603         }
604         return (instance_.get()->*(adminRemoveFunc_.biAdminFunction_))(adminName, currentData);
605     };
606     adminRemoveFunc_ = AdminRemoveFunc(adminRemove, listener);
607 }
608 
609 template<class CT, class DT>
OnAdminRemoveDone(const std::string & adminName,const std::string & currentJsonData)610 void IPluginTemplate<CT, DT>::OnAdminRemoveDone(const std::string &adminName, const std::string &currentJsonData)
611 {
612     if (instance_ == nullptr) {
613         return;
614     }
615     if (adminRemoveDoneFunc_.adminRemoveDone_ == nullptr) {
616         return;
617     }
618     (adminRemoveDoneFunc_.adminRemoveDone_)(adminName, currentJsonData);
619 }
620 
621 template<class CT, class DT>
SetOnAdminRemoveDoneListener(Runner && listener)622 void IPluginTemplate<CT, DT>::SetOnAdminRemoveDoneListener(Runner &&listener)
623 {
624     if (instance_ == nullptr) {
625         return;
626     }
627     auto adminRemoveDone = [this](const std::string &adminName, const std::string &currentJsonData) -> ErrCode {
628         if (adminRemoveDoneFunc_.runner_ == nullptr) {
629             return ERR_EDM_NOT_EXIST_FUNC;
630         }
631         (instance_.get()->*(adminRemoveDoneFunc_.runner_))();
632         return ERR_OK;
633     };
634     adminRemoveDoneFunc_ = AdminRemoveDoneFunc(adminRemoveDone, listener);
635 }
636 
637 template<class CT, class DT>
SetOnAdminRemoveDoneListener(BiAdminConsumer && listener)638 void IPluginTemplate<CT, DT>::SetOnAdminRemoveDoneListener(BiAdminConsumer &&listener)
639 {
640     if (instance_ == nullptr) {
641         return;
642     }
643     auto adminRemoveDone = [this](const std::string &adminName, const std::string &currentJsonData) -> ErrCode {
644         if (adminRemoveDoneFunc_.biAdminConsumer_ == nullptr) {
645             return ERR_EDM_NOT_EXIST_FUNC;
646         }
647         DT currentData;
648         if (!serializer_->Deserialize(currentJsonData, currentData)) {
649             return ERR_EDM_OPERATE_JSON;
650         }
651         (instance_.get()->*(adminRemoveDoneFunc_.biAdminConsumer_))(adminName, currentData);
652         return ERR_OK;
653     };
654     adminRemoveDoneFunc_ = AdminRemoveDoneFunc(adminRemoveDone, listener);
655 }
656 
657 template<class CT, class DT>
WritePolicyToParcel(const std::string & policyData,MessageParcel & reply)658 ErrCode IPluginTemplate<CT, DT>::WritePolicyToParcel(const std::string &policyData, MessageParcel &reply)
659 {
660     DT currentData;
661     if (!serializer_->Deserialize(policyData, currentData)) {
662         return ERR_EDM_OPERATE_JSON;
663     }
664     if (!serializer_->WritePolicy(reply, currentData)) {
665         return ERR_EDM_OPERATE_PARCEL;
666     }
667     return ERR_OK;
668 }
669 
670 template<class CT, class DT>
GetMergePolicyData(DT & policyData)671 bool IPluginTemplate<CT, DT>::GetMergePolicyData(DT &policyData)
672 {
673     AdminValueItemsMap adminValues;
674     PolicyManager::GetInstance()->GetAdminByPolicyName(GetPolicyName(), adminValues);
675     if (adminValues.empty()) {
676         return true;
677     }
678     if (adminValues.size() == 1) {
679         for (const auto &item : adminValues) {
680             if (!serializer_->Deserialize(item.second, policyData)) {
681                 return false;
682             } else {
683                 return true;
684             }
685         }
686     } else {
687         std::vector<DT> adminValueArray;
688         for (const auto &item : adminValues) {
689             DT dataItem;
690             if (!serializer_->Deserialize(item.second, dataItem)) {
691                 return false;
692             }
693             adminValueArray.push_back(dataItem);
694         }
695         if (!serializer_->MergePolicy(adminValueArray, policyData)) {
696             return false;
697         }
698     }
699     return true;
700 }
701 
702 template<class CT, class DT>
SetInstance(std::shared_ptr<CT> instance)703 void IPluginTemplate<CT, DT>::SetInstance(std::shared_ptr<CT> instance)
704 {
705     instance_ = instance;
706 }
707 
708 template<class CT, class DT>
SetSerializer(std::shared_ptr<IPolicySerializer<DT>> serializer)709 void IPluginTemplate<CT, DT>::SetSerializer(std::shared_ptr<IPolicySerializer<DT>> serializer)
710 {
711     serializer_ = serializer;
712 }
713 
714 template<class CT, class DT>
InitAttribute(uint32_t policyCode,const std::string & policyName,const std::string & permission,bool needSave,bool global)715 void IPluginTemplate<CT, DT>::InitAttribute(
716     uint32_t policyCode, const std::string &policyName, const std::string &permission, bool needSave, bool global)
717 {
718     policyCode_ = policyCode;
719     policyName_ = policyName;
720     permission_ = permission;
721     needSave_ = needSave;
722     isGlobal_ = global;
723 }
724 
725 template<class CT, class DT>
~IPluginTemplate()726 IPluginTemplate<CT, DT>::~IPluginTemplate()
727 {
728     if (instance_ != nullptr) {
729         instance_.reset();
730         instance_ = nullptr;
731     }
732     if (serializer_ != nullptr) {
733         serializer_.reset();
734         serializer_ = nullptr;
735     }
736 }
737 
738 /*
739  * Policy processing plugin singleton mode template,which needs to inherit the template.
740  *
741  * @tparam CT Policy processing logic class, which is the code to be implemented.
742  * @tparam DT Policy data type, for example:string,vector<string>,map<string,string>...
743  */
744 template<typename CT, typename DT>
745 class PluginSingleton : public NoCopyable {
746 public:
747     /*
748      * Initialize the plugin. The subclass needs to implement the pure virtual
749      * function, define the code,name and permission of the plugin, and listen
750      * to policy events.
751      *
752      * @param ptr std::shared_ptr<IPluginTemplate<CT, DT>>
753      */
754     virtual void InitPlugin(std::shared_ptr<IPluginTemplate<CT, DT>> ptr) = 0;
755 
756     /*
757      * Obtains the singleton of the plugin interface.
758      *
759      * @return std::shared_ptr<IPlugin>
760      */
761     static std::shared_ptr<IPlugin> GetPlugin();
762 
763     static void DestroyPlugin();
764 
765 private:
766     static std::shared_ptr<IPluginTemplate<CT, DT>> pluginInstance_;
767     static std::mutex mutexLock_;
768 };
769 
770 template<typename CT, typename DT>
771 std::shared_ptr<IPluginTemplate<CT, DT>> PluginSingleton<CT, DT>::pluginInstance_ = nullptr;
772 
773 template<typename CT, typename DT>
774 std::mutex PluginSingleton<CT, DT>::mutexLock_;
775 
776 template<typename CT, typename DT>
GetPlugin()777 std::shared_ptr<IPlugin> PluginSingleton<CT, DT>::GetPlugin()
778 {
779     if (pluginInstance_ == nullptr) {
780         std::lock_guard<std::mutex> lock(mutexLock_);
781         if (pluginInstance_ == nullptr) {
782             std::shared_ptr<CT> ptr = std::make_shared<CT>();
783             pluginInstance_ = std::make_shared<IPluginTemplate<CT, DT>>();
784             pluginInstance_->SetInstance(ptr);
785             ptr->InitPlugin(pluginInstance_);
786         }
787     }
788     return pluginInstance_;
789 }
790 
791 template<typename CT, typename DT>
DestroyPlugin()792 void PluginSingleton<CT, DT>::DestroyPlugin()
793 {
794     std::lock_guard<std::mutex> lock(mutexLock_);
795     if (pluginInstance_ != nullptr) {
796         pluginInstance_.reset();
797         pluginInstance_ = nullptr;
798     }
799 }
800 } // namespace EDM
801 } // namespace OHOS
802 #endif // SERVICES_EDM_INCLUDE_EDM_IPLUGIN_TEMPLATE_H
803