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