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