• 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_POLICY_MANAGER_H
17 #define SERVICES_EDM_INCLUDE_EDM_POLICY_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 #include "edm_errors.h"
24 #include "json/json.h"
25 
26 namespace OHOS {
27 namespace EDM {
28 using PolicyItemsMap = std::unordered_map<std::string, std::string>;     /* PolicyName and PolicyValue pair */
29 using AdminValueItemsMap = std::unordered_map<std::string, std::string>; /* AdminName and PolicyValue pair */
30 
31 /*
32  * This class is used to load and store /data/service/el1/public/edm/device_policies.json file.
33  * provide the Get and Set api to operate on json file, the read and write json
34  * file depend on jsoncpp library
35  */
36 class PolicyManager : public std::enable_shared_from_this<PolicyManager> {
37 public:
38     /*
39      * The static function used to get singleton instance of PolicyManager.
40      *
41      * @return return thr shared pointer of PolicyManager
42      */
43     static std::shared_ptr<PolicyManager> GetInstance();
44 
45     /*
46      * This function is used to get all policy items of an admin, an admin represent an EDM application
47      *
48      * @param adminName the application's bundle name
49      * @param allAdminPolicy the all policy item packaged in std::unordered_map
50      * @return return thr ErrCode of this function
51      */
52     ErrCode GetAllPolicyByAdmin(const std::string &adminName, PolicyItemsMap &allAdminPolicy);
53 
54     /*
55      * This function is used to get policy items by admin name policy name
56      * If the adminName is null, will get the combined policy, otherwise will
57      * get the admin policy
58      *
59      * @param adminName the application's bundle name
60      * @param policyName the policy item name
61      * @param policyValue the policy value which the caller wanted to get
62      * @return return thr ErrCode of this function
63      */
64     ErrCode GetPolicy(const std::string &adminName, const std::string &policyName, std::string &policyValue);
65 
66     /*
67      * This function is used to set policy items by admin name policy name. If the adminName is null,
68      * will set the combined policy. If the policyName is null, will set the admin policy, otherwise will
69      * set both the admin policy and merged policy, if the policy value is null, the policy item will be
70      * deleted, this function will write json file. write merged policy and admin policy simultaneously
71      * is very useful for atomic operation
72      *
73      * @param adminName the application's bundle name
74      * @param policyName the policy item name
75      * @param adminPolicyValue the admin policy value which the caller wanted to set
76      * @param mergedPolicyValue the merged policy value which the caller wanted to set
77      * @return return thr ErrCode of this function
78      */
79     ErrCode SetPolicy(const std::string &adminName, const std::string &policyName, const std::string &adminPolicyValue,
80         const std::string &mergedPolicyValue);
81 
82     /*
83      * This function is used to get admin name by policy name, then the caller will know
84      * which application set the policy
85      *
86      * @param policyName the policy item name
87      * @param adminValueItems the all admin name and policy value packaged in std::unordered_map
88      * @return return thr ErrCode of this function
89      */
90     ErrCode GetAdminByPolicyName(const std::string &policyName, AdminValueItemsMap &adminValueItems);
91 
92     /*
93      * This function is used to init the PolicyManager, must be called before any of other api
94      * init function will read and parse json file and construct some std::unordered_map to
95      * provide get and set operation
96      */
97     void Init();
98 
99     /*
100      * This function is debug api used to print all admin policy
101      */
102     void DumpAdminPolicy();
103 
104     /*
105      * This function is debug api used to print all admin list
106      */
107     void DumpAdminList();
108 
109     /*
110      * This function is debug api used to print all combined policy
111      */
112     void DumpCombinedPolicy();
113 
114     virtual ~PolicyManager();
115 
116 private:
117     PolicyManager();
118     bool DeleteAdminPolicyItemJsonValue(Json::Value &admin, const std::string &adminName,
119         const std::string &policyName, unsigned int &policyItemsNum);
120     bool GetAdminItemJsonObject(const Json::Value &admin, const std::string &adminName);
121     bool ParseAdminList(const std::string &adminName, const PolicyItemsMap &itemsMap);
122     bool ParseAdminPolicy(const Json::Value &admin);
123     bool ParseCombinedPolicy(const Json::Value &combined);
124     bool ParsePolicyItems(const Json::Value &items, PolicyItemsMap &itemsMap);
125     bool SetAdminPolicyItemJsonValue(Json::Value &admin, const std::string &adminName, const std::string &policyName,
126         const std::string &policyValue);
127 
128     ErrCode DeleteAdminJsonValue(const std::string &adminName, const std::string &policyName);
129     ErrCode DeleteAdminPolicy(const std::string &adminName, const std::string &policyName);
130     ErrCode DeleteCombinedJsonValue(const std::string &policyName);
131     ErrCode DeleteCombinedPolicy(const std::string &policyName);
132     ErrCode GetAdminPolicy(const std::string &adminName, const std::string &policyName, std::string &policyValue);
133     ErrCode GetCombinedPolicy(const std::string &policyName, std::string &policyValue);
134     ErrCode LoadPolicy();
135     ErrCode SetAdminJsonValue(const std::string &adminName, const std::string &policyName,
136         const std::string &policyValue);
137     ErrCode SetAdminPolicy(const std::string &adminName, const std::string &policyName, const std::string &policyValue);
138     ErrCode SetCombinedJsonValue(const std::string &policyName, const std::string &policyValue);
139     ErrCode SetCombinedPolicy(const std::string &policyName, const std::string &policyValue);
140     ErrCode ParseDevicePolicyJsonFile(const Json::Value &policyRoot);
141     ErrCode ParseJsonString(const std::string &policyValue, Json::Value &policyValueRoot);
142 
143     void CreateEmptyJsonFile();
144     void DeleteAdminList(const std::string &adminName, const std::string &policyName);
145     void SavePolicy();
146     void SetAdminList(const std::string &adminName, const std::string &policyName, const std::string &policyValue);
147 
148     /*
149      * This member is the combined policy and combined value pair
150      */
151     PolicyItemsMap combinedPolicies_;
152 
153     /*
154      * This member is the admin name and policyName, policyValue pairs
155      */
156     std::unordered_map<std::string, PolicyItemsMap> adminPolicies_;
157 
158     /*
159      * This member is the policy name and adminName, policyValue pairs
160      */
161     std::unordered_map<std::string, AdminValueItemsMap> policyAdmins_;
162 
163     /*
164      * This member is the json root, used to parse and write json file
165      */
166     Json::Value policyRoot_;
167 
168     /*
169      * This member is the singleton instance of PolicyManager
170      */
171     static std::shared_ptr<PolicyManager> instance_;
172 
173     /*
174      * This member is the mutex lock used to ensure the instance_ is unique
175      */
176     static std::mutex mutexLock_;
177 };
178 } // namespace EDM
179 } // namespace OHOS
180 
181 #endif // SERVICES_EDM_INCLUDE_EDM_POLICY_MANAGER_H
182