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_H 17 #define SERVICES_EDM_INCLUDE_EDM_IPLUGIN_H 18 19 #include <iostream> 20 #include <map> 21 #include <string> 22 #include "edm_errors.h" 23 #include "func_code.h" 24 #include "handle_policy_data.h" 25 #include "iplugin_execute_strategy.h" 26 #include "message_parcel.h" 27 28 namespace OHOS { 29 namespace EDM { 30 constexpr int32_t DEFAULT_USER_ID = 100; 31 constexpr const char *NONE_PERMISSION_MATCH = "NA"; 32 33 class IPlugin { 34 public: 35 enum class PluginType { 36 BASIC = 0, 37 EXTENSION, 38 }; 39 40 enum class PermissionType { 41 NORMAL_DEVICE_ADMIN = 0, 42 SUPER_DEVICE_ADMIN, 43 UNKNOWN, 44 }; 45 46 enum class ApiType { 47 PUBLIC = 0, 48 SYSTEM, 49 TEST, 50 UNKNOWN, 51 }; 52 53 struct PolicyPermissionConfig { 54 std::string permission; 55 std::map<std::string, std::string> tagPermissions; 56 PermissionType permissionType; 57 ApiType apiType; 58 PolicyPermissionConfigPolicyPermissionConfig59 PolicyPermissionConfig() 60 { 61 permissionType = PermissionType::UNKNOWN; 62 apiType = ApiType::UNKNOWN; 63 } 64 PolicyPermissionConfigPolicyPermissionConfig65 PolicyPermissionConfig(std::string _permission, PermissionType _permissionType, ApiType _apiType) 66 : permission(std::move(_permission)), permissionType(std::move(_permissionType)), 67 apiType(std::move(_apiType)) {} 68 PolicyPermissionConfigPolicyPermissionConfig69 PolicyPermissionConfig(std::map<std::string, std::string> _tagPermissions, 70 PermissionType _permissionType, ApiType _apiType) : tagPermissions(std::move(_tagPermissions)), 71 permissionType(std::move(_permissionType)), apiType(std::move(_apiType)) {} 72 }; 73 74 /* 75 * handle policy 76 * 77 * @param funcCode func code 78 * @param data Data sent from the IPC 79 * @param reply Reply return to the IPC 80 * @param policyData Policy data after processing 81 * @return If the operation is successful, ERR_OK is returned. 82 */ 83 virtual ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply, 84 HandlePolicyData &policyData, int32_t userId) = 0; 85 86 /* 87 * Merge policy data 88 * 89 * @param adminName current admin name 90 * @param policyData in:Current cached policy data,out:comprehensive data of all admins currently cached. 91 * @return If ERR_OK is returned,policyData incoming and outgoing data will be saved to a file. 92 */ 93 virtual ErrCode MergePolicyData(const std::string &adminName, std::string &policyData); 94 virtual void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName, bool isGlobalChanged, 95 int32_t userId) = 0; 96 virtual ErrCode OnAdminRemove(const std::string &adminName, const std::string &policyData, int32_t userId) = 0; 97 virtual void OnAdminRemoveDone(const std::string &adminName, const std::string ¤tJsonData, 98 int32_t userId) = 0; 99 virtual ErrCode WritePolicyToParcel(const std::string &policyData, MessageParcel &reply); 100 virtual ErrCode OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, 101 int32_t userId) = 0; 102 103 std::uint32_t GetCode(); 104 std::string GetPolicyName(); 105 bool NeedSavePolicy(); 106 bool IsGlobalPolicy(); 107 PolicyPermissionConfig GetAllPermission(FuncOperateType operaType); 108 std::string GetPermission(FuncOperateType operaType, std::string permissionTag = ""); 109 IPlugin::PermissionType GetPermissionType(FuncOperateType operaType); 110 IPlugin::ApiType GetApiType(FuncOperateType operaType); 111 void SetExtensionPlugin(std::shared_ptr<IPlugin> extensionPlugin); 112 std::shared_ptr<IPlugin> GetExtensionPlugin(); 113 void SetExecuteStrategy(std::shared_ptr<IPluginExecuteStrategy> strategy); 114 std::shared_ptr<IPluginExecuteStrategy> GetExecuteStrategy(); 115 void SetPluginType(IPlugin::PluginType type); 116 IPlugin::PluginType GetPluginType(); 117 virtual ~IPlugin(); 118 119 protected: 120 std::uint32_t policyCode_ = 0; 121 std::string policyName_; 122 PolicyPermissionConfig permissionConfig_; 123 std::map<FuncOperateType, PolicyPermissionConfig> permissionMap_; 124 std::shared_ptr<IPlugin> extensionPlugin_ = nullptr; 125 std::shared_ptr<IPluginExecuteStrategy> strategy_ = std::make_shared<IPluginExecuteStrategy>(); 126 bool needSave_ = true; 127 bool isGlobal_ = true; 128 IPlugin::PluginType type_ = PluginType::BASIC; 129 130 private: 131 std::string CheckAndGetPermissionFromConfig(const std::string &permissionTag, 132 std::map<std::string, std::string> tagPermissions, const std::string &commonPermission); 133 }; 134 } // namespace EDM 135 } // namespace OHOS 136 137 #endif // SERVICES_EDM_INCLUDE_EDM_IPLUGIN_H 138