• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 <memory>
21 #include <unordered_map>
22 #include "edm_log.h"
23 #include "func_code_utils.h"
24 #include "iplugin.h"
25 #include "ipolicy_manager.h"
26 #include "ipolicy_serializer.h"
27 #include "plugin_singleton.h"
28 
29 namespace OHOS {
30 namespace EDM {
31 /*
32  * Policy processing template.Implements the IPlugin interface and
33  * provides the event registration method for policy processing.
34  *
35  * @tparam CT Policy processing logic class, which is the code to be implemented.
36  * @tparam DT Policy data type, for example:string,vector<string>,map<string,string>...
37  */
38 template<class CT, class DT>
39 class IPluginTemplate : public IPlugin {
40 public:
41     ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
42         HandlePolicyData &policyData, int32_t userId) override;
43 
44     ErrCode GetOthersMergePolicyData(const std::string &adminName, std::string &othersMergePolicyData) override;
45 
46     void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName,
47         bool isGlobalChanged, int32_t userId) override;
48 
49     ErrCode OnAdminRemove(const std::string &adminName, const std::string &currentJsonData,
50         const std::string &mergeJsonData, int32_t userId) override;
51 
52     void OnAdminRemoveDone(const std::string &adminName, const std::string &removedJsonData, int32_t userId) override;
53 
54     ErrCode WritePolicyToParcel(const std::string &policyData, MessageParcel &reply) override;
55 
56     ErrCode OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId) override;
57 
58     /*
59      * Sets the handle of the policy processing object.
60      *
61      * @param instance std::shared_ptr<CT>
62      */
63     void SetInstance(std::shared_ptr<CT> instance);
64 
65     /*
66      * Define CT as the friend class of IPluginTemplate.
67      */
68     friend CT;
69 
70     IPluginTemplate();
71 
72     ~IPluginTemplate() override;
73 
74 protected:
75     /*
76      * Represents a function that invoked during handling policy.
77      *
78      * @param one MessageParcel
79      * @param two MessageParcel
80      * @param three Current policy data
81      * @param four Whether the policy data is changed
82      * @param five Handle type
83      * @see OnHandlePolicy
84      * @see HandlePolicyFunc
85      */
86     typedef std::function<ErrCode(MessageParcel &, MessageParcel &, HandlePolicyData &, FuncOperateType,
87         int32_t userId)> HandlePolicy;
88 
89     /*
90      * Represents a function that invoked during handling policy.
91      *
92      * @param one Admin name
93      * @param two Whether the policy data is changed
94      * @param three Handle type
95      * @see OnHandlePolicyDone
96      * @see HandlePolicyDoneFunc
97      */
98     typedef std::function<ErrCode(const std::string &, bool, FuncOperateType, int32_t userId)> HandlePolicyDone;
99 
100     /*
101      * Represents a function that invoked during the removal of admin.
102      *
103      * @see OnAdminRemove
104      * @see AdminRemoveFunc
105      */
106     typedef std::function<ErrCode(const std::string &, const std::string &, const std::string &,
107         int32_t userId)> AdminRemove;
108 
109     /*
110      * Represents a function that invoked after the admin is removed.
111      *
112      * @see OnAdminRemoveDone
113      * @see AdminRemoveDoneFunc
114      */
115     typedef std::function<ErrCode(const std::string &, const std::string &, int32_t userId)> AdminRemoveDone;
116 
117     /*
118      * This is a member function pointer type of CT class.
119      * Represents a supplier of results. There is no requirement that a ErrCode result be returned each time the
120      * supplier is invoked.
121      * It is generally used in scenarios where only return values need to be processed and parameters are not concerned.
122      *
123      * @return Whether the policy is handled successfully.
124      * @see SetOnHandlePolicyListener
125      * @see SetOnAdminRemoveListener
126      */
127     typedef ErrCode (CT::*Supplier)();
128 
129     /*
130      * This is a member function pointer type of CT class.
131      * Represents a function that accepts one DT argument and produces a ErrCode result.
132      * It is generally used in the scenario where only one DT parameter and return value need to be processed.
133      *
134      * @param data In: Input policy data parameter,Out: Used to update the admin data.
135      * @return Whether the policy is handled successfully.
136      * @see SetOnHandlePolicyListener
137      */
138     typedef ErrCode (CT::*Function)(DT &data);
139 
140     /*
141      * This is a member function pointer type of CT class.
142      * Represents a function that accepts one DT argument and produces a ErrCode result.
143      * It is generally used in the scenario where only one DT parameter and return value need to be processed.
144      *
145      * @param data In: Input policy data parameter,Out: Used to update the admin data.
146      * @param reply In: return message parcel for ipc,Out: parcel with return value or message.
147      * @return Whether the policy is handled successfully.
148      * @see SetOnHandlePolicyListener
149      */
150     typedef ErrCode (CT::*ReplyFunction)(DT &data, MessageParcel &reply);
151 
152     /*
153      * This is a member function pointer type of CT class.
154      * Represents a function that accepts two DT arguments and produces a ErrCode result.
155      *
156      * @param data Input policy data parameter
157      * @param currentData In: data of the current admin,Out: Used to update the admin data.
158      * @return Whether the policy is handled successfully.
159      * @see SetOnHandlePolicyListener
160      */
161     typedef ErrCode (CT::*BiFunction)(DT &data, DT &currentData, DT &mergeData, int32_t userId);
162 
163     /*
164      * This is a member function pointer type of CT class.
165      * Represents a function that accepts an string-valued and a DT-valued argument, and produces a ErrCode result.
166      *
167      * @param adminName Admin name
168      * @param data Admin policy data
169      * @see SetOnAdminRemoveListener
170      */
171     typedef ErrCode (CT::*BiAdminFunction)(const std::string &adminName, DT &data, DT &mergeData, int32_t userId);
172 
173     /*
174      * This is a member function pointer type of CT class.
175      * Represents an operation that accepts a single bool input argument and returns no result.
176      *
177      * @param isGlobalChanged Whether the policy data is changed
178      * @see SetOnHandlePolicyDoneListener
179      */
180     typedef void (CT::*BoolConsumer)(bool isGlobalChanged);
181 
182     /*
183      * This is a member function pointer type of CT class.
184      * Represents an operation that accepts an DT-valued and a bool-valued argument, and returns no result.
185      *
186      * @param data Admin policy data
187      * @param isGlobalChanged Whether the policy data is changed
188      * @see SetOnHandlePolicyDoneListener
189      */
190     typedef void (CT::*BiBoolConsumer)(DT &data, bool isGlobalChanged, int32_t userId);
191 
192     /*
193      * This is a member function pointer type of CT class.
194      * Represents an operation that accepts no arguments and returns no result.
195      *
196      * @param data Admin policy data
197      * @param isGlobalChanged Whether the policy data is changed
198      * @see SetOnAdminRemoveDoneListener
199      */
200     typedef void (CT::*Runner)();
201 
202     /*
203      * This is a member function pointer type of CT class.
204      * Represents an operation that accepts an string-valued and a DT-valued argument, and returns no result.
205      *
206      * @param adminName Admin name
207      * @param data Admin policy data
208      * @see SetOnAdminRemoveDoneListener
209      */
210     typedef void (CT::*BiAdminConsumer)(const std::string &adminName, DT &data, int32_t userId);
211 
212     virtual bool GetMergePolicyData(DT &policyData);
213 
214     void SetSerializer(std::shared_ptr<IPolicySerializer<DT>> serializer);
215 
216     void InitAttribute(uint32_t policyCode, const std::string &policyName, PolicyPermissionConfig config,
217         bool needSave = true, bool global = true);
218 
219     void InitAttribute(uint32_t policyCode, const std::string &policyName, const std::string &permission,
220         IPlugin::PermissionType permissionType, bool needSave = true, bool global = true);
221 
222     void InitAttribute(uint32_t policyCode, const std::string &policyName, bool needSave = true, bool global = true);
223 
224     void InitPermission(FuncOperateType operateType, PolicyPermissionConfig config);
225 
226     void InitPermission(FuncOperateType operateType, const std::string &permission,
227         IPlugin::PermissionType permissionType);
228 
229     /*
230      * Registering Listening for HandlePolicy Events.
231      *
232      * @param listener Listening member function pointer of CT Class
233      * @param type Policy Data Processing Mode
234      * @see FuncOperateType
235      */
236     void SetOnHandlePolicyListener(Supplier &&listener, FuncOperateType type);
237 
238     /*
239      * Registering Listening for HandlePolicy Events.
240      *
241      * @param listener Listening member function pointer of CT Class
242      * @param type Policy Data Processing Mode,default FuncOperateType::REMOVE
243      * @see FuncOperateType
244      */
245     void SetOnHandlePolicyListener(Function &&listener, FuncOperateType type);
246 
247     /*
248      * Registering Listening for HandlePolicy Events.
249      *
250      * @param listener Listening member function pointer of CT Class
251      * @param type Policy Data Processing Mode,default FuncOperateType::SET
252      * @see FuncOperateType
253      */
254     void SetOnHandlePolicyListener(BiFunction &&listener, FuncOperateType type);
255 
256     /*
257      * Registering Listening for HandlePolicy Events.
258      *
259      * @param listener Listening member function pointer of CT Class
260      * @param type Policy Data Processing Mode,default FuncOperateType::SET
261      * @see FuncOperateType
262      */
263     void SetOnHandlePolicyListener(ReplyFunction &&listener, FuncOperateType type);
264 
265     /*
266      * Registering listening for HandlePolicyDone events.
267      *
268      * @param listener Listening member function pointer of CT Class
269      * @param type Policy Data Processing Mode
270      */
271     void SetOnHandlePolicyDoneListener(BoolConsumer &&listener, FuncOperateType type);
272 
273     /*
274      * Registering listening for HandlePolicyDone events.
275      *
276      * @param listener Listening member function pointer of CT Class
277      * @param type Policy Data Processing Mode
278      */
279     void SetOnHandlePolicyDoneListener(BiBoolConsumer &&listener, FuncOperateType type);
280 
281     /*
282      * Registering listening for AdminRemove events.
283      *
284      * @param listener Listening member function pointer of CT Class
285      */
286     void SetOnAdminRemoveListener(Supplier &&listener);
287 
288     /*
289      * Registering listening for AdminRemove events.
290      *
291      * @param listener Listening member function pointer of CT Class
292      */
293     void SetOnAdminRemoveListener(BiAdminFunction &&listener);
294 
295     /*
296      * Registering listening for AdminRemoveDone events.
297      *
298      * @param listener Listening member function pointer of CT Class
299      */
300     void SetOnAdminRemoveDoneListener(Runner &&listener);
301 
302     /*
303      * Registering listening for AdminRemoveDone events.
304      *
305      * @param listener Listening member function pointer of CT Class
306      */
307     void SetOnAdminRemoveDoneListener(BiAdminConsumer &&listener);
308 
309     /*
310      * Mapping between HandlePolicy and member function types that support overloading.
311      */
312     struct HandlePolicyFunc {
313         HandlePolicy handlePolicy_ = nullptr;
314         Supplier supplier_ = nullptr;
315         Function function_ = nullptr;
316         ReplyFunction replyfunction_ = nullptr;
317         BiFunction biFunction_ = nullptr;
318 
HandlePolicyFuncHandlePolicyFunc319         HandlePolicyFunc() {}
320 
HandlePolicyFuncHandlePolicyFunc321         HandlePolicyFunc(HandlePolicy handlePolicy, Supplier supplier)
322             : handlePolicy_(std::move(handlePolicy)), supplier_(supplier) {}
323 
HandlePolicyFuncHandlePolicyFunc324         HandlePolicyFunc(HandlePolicy handlePolicy, Function function)
325             : handlePolicy_(std::move(handlePolicy)), function_(function) {}
326 
HandlePolicyFuncHandlePolicyFunc327         HandlePolicyFunc(HandlePolicy handlePolicy, ReplyFunction replyfunction)
328             : handlePolicy_(std::move(handlePolicy)), replyfunction_(replyfunction) {}
329 
HandlePolicyFuncHandlePolicyFunc330         HandlePolicyFunc(HandlePolicy handlePolicy, BiFunction biFunction)
331             : handlePolicy_(std::move(handlePolicy)), biFunction_(biFunction) {}
332     };
333 
334     // Member function callback object of the HandlePolicy event.
335     std::map<FuncOperateType, HandlePolicyFunc> handlePolicyFuncMap_;
336 
337     /*
338      * Mapping between HandlePolicyDone and member function types that support overloading.
339      */
340     struct HandlePolicyDoneFunc {
341         HandlePolicyDone handlePolicyDone_ = nullptr;
342         BoolConsumer boolConsumer_ = nullptr;
343         BiBoolConsumer biBoolConsumer_ = nullptr;
344 
HandlePolicyDoneFuncHandlePolicyDoneFunc345         HandlePolicyDoneFunc() {}
346 
HandlePolicyDoneFuncHandlePolicyDoneFunc347         HandlePolicyDoneFunc(HandlePolicyDone handlePolicyDone, BoolConsumer boolConsumer)
348             : handlePolicyDone_(std::move(handlePolicyDone)), boolConsumer_(boolConsumer) {}
349 
HandlePolicyDoneFuncHandlePolicyDoneFunc350         HandlePolicyDoneFunc(HandlePolicyDone handlePolicyDone, BiBoolConsumer biBoolConsumer)
351             : handlePolicyDone_(std::move(handlePolicyDone)), biBoolConsumer_(biBoolConsumer) {}
352     };
353 
354     // Member function callback object of the HandlePolicyDone event.
355     std::map<FuncOperateType, HandlePolicyDoneFunc> handlePolicyDoneFuncMap_;
356 
357     /*
358      * Mapping between AdminRemove and member function types that support overloading.
359      */
360     struct AdminRemoveFunc {
361         AdminRemove adminRemove_ = nullptr;
362         Supplier supplier_ = nullptr;
363         BiAdminFunction biAdminFunction_ = nullptr;
364 
AdminRemoveFuncAdminRemoveFunc365         AdminRemoveFunc() {}
366 
AdminRemoveFuncAdminRemoveFunc367         AdminRemoveFunc(AdminRemove adminRemove, Supplier supplier)
368             : adminRemove_(std::move(adminRemove)), supplier_(supplier) {}
369 
AdminRemoveFuncAdminRemoveFunc370         AdminRemoveFunc(AdminRemove adminRemove, BiAdminFunction biAdminFunction)
371             : adminRemove_(std::move(adminRemove)), biAdminFunction_(biAdminFunction) {}
372     };
373 
374     // Member function callback object of the AdminRemove event.
375     AdminRemoveFunc adminRemoveFunc_;
376 
377     /*
378      * Mapping between AdminRemoveDone and member function types that support overloading.
379      */
380     struct AdminRemoveDoneFunc {
381         AdminRemoveDone adminRemoveDone_ = nullptr;
382         Runner runner_ = nullptr;
383         BiAdminConsumer biAdminConsumer_ = nullptr;
384 
AdminRemoveDoneFuncAdminRemoveDoneFunc385         AdminRemoveDoneFunc() {}
386 
AdminRemoveDoneFuncAdminRemoveDoneFunc387         AdminRemoveDoneFunc(AdminRemoveDone adminRemoveDone, Runner runner)
388             : adminRemoveDone_(std::move(adminRemoveDone)), runner_(runner) {}
389 
AdminRemoveDoneFuncAdminRemoveDoneFunc390         AdminRemoveDoneFunc(AdminRemoveDone adminRemoveDone, BiAdminConsumer biAdminConsumer)
391             : adminRemoveDone_(std::move(adminRemoveDone)), biAdminConsumer_(biAdminConsumer) {}
392     };
393 
394     // Member function callback object of the AdminRemoveDone event.
395     AdminRemoveDoneFunc adminRemoveDoneFunc_;
396     // Pointer to the callback member function.
397     std::shared_ptr<CT> instance_;
398     // Data serializer for policy data
399     std::shared_ptr<IPolicySerializer<DT>> serializer_;
400 };
401 
402 #include "iplugin_template.tpp"
403 
404 } // namespace EDM
405 } // namespace OHOS
406 #endif // SERVICES_EDM_INCLUDE_EDM_IPLUGIN_TEMPLATE_H
407