• 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 #include "policy_manager.h"
17 #include <algorithm>
18 #include <ctime>
19 #include <fstream>
20 #include <unistd.h>
21 #include "directory_ex.h"
22 #include "edm_log.h"
23 
24 namespace OHOS {
25 namespace EDM {
26 const std::string EDM_POLICY_JSON_FILE = "/data/service/el1/public/edm/device_policies.json";
27 const std::string EDM_POLICY_JSON_FILE_BAK = "/data/service/el1/public/edm/device_policies.json.bak";
28 constexpr unsigned int INVALID_INDEX = -1;
29 
30 std::shared_ptr<PolicyManager> PolicyManager::instance_;
31 std::mutex PolicyManager::mutexLock_;
32 
PolicyManager()33 PolicyManager::PolicyManager()
34 {
35     EDMLOGD("PolicyManager::PolicyManager\n");
36 }
37 
~PolicyManager()38 PolicyManager::~PolicyManager()
39 {
40     EDMLOGD("PolicyManager::~PolicyManager\n");
41 }
42 
ParsePolicyItems(const Json::Value & items,PolicyItemsMap & itemsMap)43 bool PolicyManager::ParsePolicyItems(const Json::Value &items, PolicyItemsMap &itemsMap)
44 {
45     if (!items.isObject()) {
46         EDMLOGW("ParsePolicyItems items is not object");
47         return false;
48     }
49 
50     Json::Value::Members mem = items.getMemberNames();
51     for (const auto &i : mem) {
52         Json::StreamWriterBuilder builder;
53         builder["indentation"] = "    ";
54         std::string arrayToString = Json::writeString(builder, items[i]);
55         itemsMap.insert(std::pair<std::string, std::string>(i, arrayToString));
56     }
57     return true;
58 }
59 
ParseAdminList(const std::string & adminName,const PolicyItemsMap & itemsMap)60 bool PolicyManager::ParseAdminList(const std::string &adminName, const PolicyItemsMap &itemsMap)
61 {
62     for (const auto &iter : itemsMap) {
63         std::string policyName = iter.first;
64         std::string policyValue = iter.second;
65         auto it = policyAdmins_.find(policyName);
66         if (it == policyAdmins_.end()) {
67             /* policy first added into map */
68             AdminValueItemsMap adminValueItem;
69             adminValueItem.insert(std::pair<std::string, std::string>(adminName, policyValue));
70             policyAdmins_.insert(std::pair<std::string, AdminValueItemsMap>(policyName, adminValueItem));
71         } else {
72             AdminValueItemsMap &adminValueRef = it->second;
73             auto iterValue = adminValueRef.find(adminName);
74             if (iterValue != adminValueRef.end()) {
75                 EDMLOGW("AdminName:%{public}s PolicyName:%{public}s should not repetitive\n",
76                         adminName.c_str(), policyName.c_str());
77                 iterValue->second = policyValue;
78             } else {
79                 adminValueRef.insert(std::pair<std::string, std::string>(adminName, policyValue));
80             }
81         }
82     }
83     return true;
84 }
85 
ParseAdminPolicy(const Json::Value & admin)86 bool PolicyManager::ParseAdminPolicy(const Json::Value &admin)
87 {
88     std::string adminName;
89     PolicyItemsMap itemsMap;
90     if (!admin.isObject()) {
91         EDMLOGI("admin root policy is not object\n");
92         return false;
93     }
94 
95     if (admin.isMember("AdminName") && admin["AdminName"].isString()) {
96         adminName = admin["AdminName"].asString();
97     }
98 
99     bool isParsePolicySuccess = false;
100     bool isParseAdminListSuccess = false;
101     if (admin.isMember("PolicyItems") && admin["PolicyItems"].isObject() && !adminName.empty()) {
102         isParsePolicySuccess = ParsePolicyItems(admin["PolicyItems"], itemsMap);
103         adminPolicies_.insert(std::pair<std::string, PolicyItemsMap>(adminName, itemsMap));
104         isParseAdminListSuccess = ParseAdminList(adminName, itemsMap);
105     }
106     return isParsePolicySuccess && isParseAdminListSuccess;
107 }
108 
ParseCombinedPolicy(const Json::Value & combined)109 bool PolicyManager::ParseCombinedPolicy(const Json::Value &combined)
110 {
111     if (!combined.isObject()) {
112         EDMLOGI("combined root is not object\n");
113         return false;
114     }
115     return ParsePolicyItems(combined, combinedPolicies_);
116 }
117 
ParseDevicePolicyJsonFile(const Json::Value & policyRoot)118 ErrCode PolicyManager::ParseDevicePolicyJsonFile(const Json::Value &policyRoot)
119 {
120     if (!policyRoot.isObject()) {
121         EDMLOGW("json root is not object\n");
122         return ERR_EDM_POLICY_PARSE_JSON_FAILED;
123     }
124 
125     bool isParseAdminSuccess = false;
126     bool isParseCombinedSuccess = false;
127     if (policyRoot.isMember("AdminPolicies") && policyRoot["AdminPolicies"].isArray()) {
128         for (const auto &item : policyRoot["AdminPolicies"]) {
129             if (!item.isObject()) {
130                 EDMLOGI("is not object");
131                 return ERR_EDM_POLICY_PARSE_JSON_FAILED;
132             }
133             isParseAdminSuccess = ParseAdminPolicy(item);
134             if (!isParseAdminSuccess) {
135                 EDMLOGI("AdminPolicies parse failed object");
136                 return ERR_EDM_POLICY_PARSE_JSON_FAILED;
137             }
138         }
139     }
140 
141     if (isParseAdminSuccess) {
142         if (policyRoot.isMember("CombinedPolicies") && policyRoot["CombinedPolicies"].isObject()) {
143             isParseCombinedSuccess = ParseCombinedPolicy(policyRoot["CombinedPolicies"]);
144         }
145     } else {
146         EDMLOGW("ParseAdminPolicy failed\n");
147     }
148 
149     if (isParseAdminSuccess && isParseCombinedSuccess) {
150         return ERR_OK;
151     }
152     return ERR_EDM_POLICY_PARSE_JSON_FAILED;
153 }
154 
LoadPolicy()155 ErrCode PolicyManager::LoadPolicy()
156 {
157     if (access(EDM_POLICY_JSON_FILE.c_str(), F_OK) != 0) {
158         EDMLOGI("LoadPolicy: create an empty json file\n");
159         CreateEmptyJsonFile();
160     }
161 
162     std::ifstream ifs(EDM_POLICY_JSON_FILE);
163     if (!ifs.is_open()) {
164         EDMLOGE("LoadPolicy: open edm policy json file failed\n");
165         return ERR_EDM_POLICY_OPEN_JSON_FAILED;
166     }
167 
168     Json::String errs;
169     Json::CharReaderBuilder builder;
170     if (!parseFromStream(builder, ifs, &policyRoot_, &errs)) {
171         EDMLOGW("parse from stream failed: %{public}s\n", errs.c_str());
172         ifs.close();
173         return ERR_EDM_POLICY_LOAD_JSON_FAILED;
174     }
175     ifs.close();
176     return ParseDevicePolicyJsonFile(policyRoot_);
177 }
178 
SavePolicy()179 void PolicyManager::SavePolicy()
180 {
181     /* the default file permission is 600, no need to change  */
182     std::ofstream ofs(EDM_POLICY_JSON_FILE_BAK, std::ofstream::binary);
183     if (!ofs.is_open()) {
184         EDMLOGW("SavePolicy open edm policy json file failed\n");
185         return;
186     }
187 
188     double time1 = clock();
189     Json::StreamWriterBuilder builder;
190     /* use 4 spaces instead of tab for indentation */
191     builder["indentation"] = "    ";
192     const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
193     writer->write(policyRoot_, &ofs);
194 
195     ofs.flush();
196     ofs.close();
197     double time2 = clock();
198     std::rename(EDM_POLICY_JSON_FILE_BAK.c_str(), EDM_POLICY_JSON_FILE.c_str());
199     if (!ChangeModeFile(EDM_POLICY_JSON_FILE, S_IRUSR | S_IWUSR)) {
200         EDMLOGW("PolicyManager::ChangeModeFile failed");
201     }
202     double time3 = clock();
203     EDMLOGI("SavePolicy spend time %{public}f, %{public}f", (time2 - time1) / CLOCKS_PER_SEC,
204         (time3 - time2) / CLOCKS_PER_SEC);
205 }
206 
GetAdminByPolicyName(const std::string & policyName,AdminValueItemsMap & adminValueItems)207 ErrCode PolicyManager::GetAdminByPolicyName(const std::string &policyName, AdminValueItemsMap &adminValueItems)
208 {
209     auto iter = policyAdmins_.find(policyName);
210     if (iter != policyAdmins_.end()) {
211         adminValueItems = iter->second;
212         return ERR_OK;
213     }
214     return ERR_EDM_POLICY_NOT_FOUND;
215 }
216 
GetAllPolicyByAdmin(const std::string & adminName,PolicyItemsMap & allAdminPolicy)217 ErrCode PolicyManager::GetAllPolicyByAdmin(const std::string &adminName, PolicyItemsMap &allAdminPolicy)
218 {
219     auto iter = adminPolicies_.find(adminName);
220     if (iter != adminPolicies_.end()) {
221         allAdminPolicy = iter->second;
222         return ERR_OK;
223     }
224     return ERR_EDM_POLICY_NOT_FIND;
225 }
226 
GetAdminPolicy(const std::string & adminName,const std::string & policyName,std::string & policyValue)227 ErrCode PolicyManager::GetAdminPolicy(const std::string &adminName, const std::string &policyName,
228     std::string &policyValue)
229 {
230     auto iter = adminPolicies_.find(adminName);
231     if (iter != adminPolicies_.end()) {
232         PolicyItemsMap &policyItem = iter->second;
233         auto it = policyItem.find(policyName);
234         if (it != policyItem.end()) {
235             policyValue = it->second;
236             return ERR_OK;
237         }
238     }
239     return ERR_EDM_POLICY_NOT_FIND;
240 }
241 
GetCombinedPolicy(const std::string & policyName,std::string & policyValue)242 ErrCode PolicyManager::GetCombinedPolicy(const std::string &policyName, std::string &policyValue)
243 {
244     auto it = combinedPolicies_.find(policyName);
245     if (it != combinedPolicies_.end()) {
246         policyValue = it->second;
247         return ERR_OK;
248     }
249     return ERR_EDM_POLICY_NOT_FIND;
250 }
251 
GetAdminItemJsonObject(const Json::Value & admin,const std::string & adminName)252 bool PolicyManager::GetAdminItemJsonObject(const Json::Value &admin, const std::string &adminName)
253 {
254     if (admin.isMember("AdminName") && admin["AdminName"].isString()) {
255         std::string adminItemName = admin["AdminName"].asString();
256         if ((adminItemName == adminName) && (admin.isMember("PolicyItems") && admin["PolicyItems"].isObject())) {
257             return true;
258         }
259     }
260     return false;
261 }
262 
GetPolicy(const std::string & adminName,const std::string & policyName,std::string & policyValue)263 ErrCode PolicyManager::GetPolicy(const std::string &adminName, const std::string &policyName,
264     std::string &policyValue)
265 {
266     if (adminName.empty()) {
267         return GetCombinedPolicy(policyName, policyValue);
268     } else {
269         return GetAdminPolicy(adminName, policyName, policyValue);
270     }
271 }
272 
SetAdminList(const std::string & adminName,const std::string & policyName,const std::string & policyValue)273 void PolicyManager::SetAdminList(const std::string &adminName, const std::string &policyName,
274     const std::string &policyValue)
275 {
276     auto iter = policyAdmins_.find(policyName);
277     if (iter == policyAdmins_.end()) {
278         /* policy first added into map */
279         AdminValueItemsMap adminValueItem;
280         adminValueItem.insert(std::pair<std::string, std::string>(adminName, policyValue));
281         policyAdmins_.insert(std::pair<std::string, AdminValueItemsMap>(policyName, adminValueItem));
282         return;
283     }
284 
285     AdminValueItemsMap &adminValueRef = iter->second;
286     auto it = adminValueRef.find(adminName);
287     if (it != adminValueRef.end()) {
288         it->second = policyValue;
289     } else {
290         adminValueRef.insert(std::pair<std::string, std::string>(adminName, policyValue));
291     }
292 }
293 
SetAdminPolicyItemJsonValue(Json::Value & admin,const std::string & adminName,const std::string & policyName,const std::string & policyValue)294 bool PolicyManager::SetAdminPolicyItemJsonValue(Json::Value &admin, const std::string &adminName,
295     const std::string &policyName, const std::string &policyValue)
296 {
297     if (GetAdminItemJsonObject(admin, adminName)) {
298         Json::Value policyValueRoot;
299         if (ParseJsonString(policyValue, policyValueRoot) != ERR_OK) {
300             return false;
301         }
302         Json::Value &adminObject = admin["PolicyItems"];
303         adminObject[policyName] = policyValueRoot;
304         return true;
305     }
306     return false;
307 }
308 
ParseJsonString(const std::string & policyValue,Json::Value & policyValueRoot)309 ErrCode PolicyManager::ParseJsonString(const std::string &policyValue, Json::Value &policyValueRoot)
310 {
311     const auto policyValueLength = static_cast<int>(policyValue.length());
312     JSONCPP_STRING err;
313     Json::CharReaderBuilder builder;
314     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
315     if (!reader->parse(policyValue.c_str(), policyValue.c_str() + policyValueLength, &policyValueRoot, &err)) {
316         policyValueRoot = Json::Value(policyValue);
317     }
318     return ERR_OK;
319 }
320 
SetAdminJsonValue(const std::string & adminName,const std::string & policyName,const std::string & policyValue)321 ErrCode PolicyManager::SetAdminJsonValue(const std::string &adminName, const std::string &policyName,
322     const std::string &policyValue)
323 {
324     if (policyRoot_.isMember("AdminPolicies") && policyRoot_["AdminPolicies"].isArray()) {
325         for (auto &admin : policyRoot_["AdminPolicies"]) {
326             if (admin.isObject() && SetAdminPolicyItemJsonValue(admin, adminName, policyName, policyValue)) {
327                 EDMLOGW("SetAdminJsonValue exist:%{public}s %{public}s ", adminName.c_str(), policyName.c_str());
328                 return ERR_OK;
329             }
330         }
331         EDMLOGI("SetAdminJsonValue new object:%{public}s %{public}s ", adminName.c_str(), policyName.c_str());
332         Json::Value policyValueRoot;
333         if (ParseJsonString(policyValue, policyValueRoot) != ERR_OK) {
334             return ERR_EDM_POLICY_SET_FAILED;
335         }
336 
337         Json::Value adminObject;
338         Json::Value policyItemsObject;
339         adminObject["AdminName"] = adminName;
340         policyItemsObject[policyName] = policyValueRoot;
341         adminObject["PolicyItems"] = policyItemsObject;
342         policyRoot_["AdminPolicies"].append(adminObject);
343         return ERR_OK;
344     }
345     return ERR_EDM_POLICY_SET_FAILED;
346 }
347 
SetAdminPolicy(const std::string & adminName,const std::string & policyName,const std::string & policyValue)348 ErrCode PolicyManager::SetAdminPolicy(const std::string &adminName, const std::string &policyName,
349     const std::string &policyValue)
350 {
351     SetAdminList(adminName, policyName, policyValue);
352     auto iter = adminPolicies_.find(adminName);
353     if (iter == adminPolicies_.end()) {
354         PolicyItemsMap itemMap;
355         itemMap.insert(std::pair<std::string, std::string>(policyName, policyValue));
356         adminPolicies_.insert(std::pair<std::string, PolicyItemsMap>(adminName, itemMap));
357         return SetAdminJsonValue(adminName, policyName, policyValue);
358     }
359 
360     PolicyItemsMap &policyItem = iter->second;
361     auto it = policyItem.find(policyName);
362     if (it != policyItem.end()) {
363         it->second = policyValue;
364     } else {
365         policyItem.insert(std::pair<std::string, std::string>(policyName, policyValue));
366     }
367     return SetAdminJsonValue(adminName, policyName, policyValue);
368 }
369 
SetCombinedJsonValue(const std::string & policyName,const std::string & policyValue)370 ErrCode PolicyManager::SetCombinedJsonValue(const std::string &policyName, const std::string &policyValue)
371 {
372     if (policyRoot_.isMember("CombinedPolicies") && policyRoot_["CombinedPolicies"].isObject()) {
373         Json::Value policyValueRoot;
374         if (ParseJsonString(policyValue, policyValueRoot) != ERR_OK) {
375             return ERR_EDM_POLICY_SET_FAILED;
376         }
377         policyRoot_["CombinedPolicies"][policyName] = policyValueRoot;
378         return ERR_OK;
379     }
380     return ERR_EDM_POLICY_SET_FAILED;
381 }
382 
SetCombinedPolicy(const std::string & policyName,const std::string & policyValue)383 ErrCode PolicyManager::SetCombinedPolicy(const std::string &policyName, const std::string &policyValue)
384 {
385     auto it = combinedPolicies_.find(policyName);
386     if (it != combinedPolicies_.end()) {
387         it->second = policyValue;
388     } else {
389         combinedPolicies_.insert(std::pair<std::string, std::string>(policyName, policyValue));
390     }
391     return SetCombinedJsonValue(policyName, policyValue);
392 }
393 
DeleteAdminList(const std::string & adminName,const std::string & policyName)394 void PolicyManager::DeleteAdminList(const std::string &adminName, const std::string &policyName)
395 {
396     auto iter = policyAdmins_.find(policyName);
397     if (iter == policyAdmins_.end()) {
398         return;
399     }
400 
401     AdminValueItemsMap &adminValueRef = iter->second;
402     auto it = adminValueRef.find(adminName);
403     if (it == adminValueRef.end()) {
404         return;
405     }
406 
407     adminValueRef.erase(it);
408     if (adminValueRef.empty()) {
409         policyAdmins_.erase(iter);
410     }
411 }
412 
DeleteAdminPolicyItemJsonValue(Json::Value & admin,const std::string & adminName,const std::string & policyName,unsigned int & policyItemsNum)413 bool PolicyManager::DeleteAdminPolicyItemJsonValue(Json::Value &admin, const std::string &adminName,
414     const std::string &policyName, unsigned int &policyItemsNum)
415 {
416     if (GetAdminItemJsonObject(admin, adminName)) {
417         Json::Value &adminObject = admin["PolicyItems"];
418         if (adminObject.isMember(policyName)) {
419             adminObject.removeMember(policyName);
420             policyItemsNum = adminObject.size();
421             return true;
422         }
423     }
424     return false;
425 }
426 
DeleteAdminJsonValue(const std::string & adminName,const std::string & policyName)427 ErrCode PolicyManager::DeleteAdminJsonValue(const std::string &adminName, const std::string &policyName)
428 {
429     if (!policyRoot_.isMember("AdminPolicies") || !policyRoot_["AdminPolicies"].isArray()) {
430         return ERR_EDM_POLICY_DEL_FAILED;
431     }
432     unsigned int policyItemsNum = 0;
433     unsigned int deleteIndex = INVALID_INDEX;
434     unsigned int policyRootSize = policyRoot_["AdminPolicies"].size();
435     for (Json::ArrayIndex i = 0; i < policyRootSize; i++) {
436         Json::Value &admin = policyRoot_["AdminPolicies"][i];
437         if (admin.isObject() && DeleteAdminPolicyItemJsonValue(admin, adminName, policyName, policyItemsNum)) {
438             if (policyItemsNum == 0) {
439                 deleteIndex = i;
440                 break;
441             }
442         }
443     }
444     if (deleteIndex != INVALID_INDEX) {
445         policyRoot_["AdminPolicies"].removeIndex(deleteIndex, nullptr);
446     }
447     return ERR_OK;
448 }
449 
DeleteAdminPolicy(const std::string & adminName,const std::string & policyName)450 ErrCode PolicyManager::DeleteAdminPolicy(const std::string &adminName, const std::string &policyName)
451 {
452     auto iter = adminPolicies_.find(adminName);
453     if (iter != adminPolicies_.end()) {
454         PolicyItemsMap &policyItem = iter->second;
455         auto it = policyItem.find(policyName);
456         if (it != policyItem.end()) {
457             policyItem.erase(it);
458         }
459 
460         if (iter->second.empty()) {
461             adminPolicies_.erase(iter);
462         }
463 
464         DeleteAdminList(adminName, policyName);
465         return DeleteAdminJsonValue(adminName, policyName);
466     }
467     return ERR_OK;
468 }
469 
DeleteCombinedJsonValue(const std::string & policyName)470 ErrCode PolicyManager::DeleteCombinedJsonValue(const std::string &policyName)
471 {
472     if (policyRoot_.isMember("CombinedPolicies") && policyRoot_["CombinedPolicies"].isObject()) {
473         Json::Value &rootObject = policyRoot_["CombinedPolicies"];
474         if (rootObject.isMember(policyName)) {
475             rootObject.removeMember(policyName);
476             return true;
477         }
478     }
479     return ERR_EDM_POLICY_DEL_FAILED;
480 }
481 
DeleteCombinedPolicy(const std::string & policyName)482 ErrCode PolicyManager::DeleteCombinedPolicy(const std::string &policyName)
483 {
484     auto it = combinedPolicies_.find(policyName);
485     if (it != combinedPolicies_.end()) {
486         combinedPolicies_.erase(it);
487     }
488     return DeleteCombinedJsonValue(policyName);
489 }
490 
DumpAdminPolicy()491 void PolicyManager::DumpAdminPolicy()
492 {
493     std::for_each(adminPolicies_.begin(), adminPolicies_.end(), [](auto iter) {
494         EDMLOGD("AdminName: %{public}s\n", iter.first.c_str());
495         std::unordered_map<std::string, std::string> map = iter.second;
496         std::for_each(map.begin(), map.end(),
497             [](auto subIter) { EDMLOGD("%{public}s : %{public}s\n", subIter.first.c_str(), subIter.second.c_str()); });
498     });
499 }
500 
DumpAdminList()501 void PolicyManager::DumpAdminList()
502 {
503     std::for_each(policyAdmins_.begin(), policyAdmins_.end(), [](auto iter) {
504         EDMLOGD("PolicyName: %{public}s\n", iter.first.c_str());
505         std::unordered_map<std::string, std::string> map = iter.second;
506         std::for_each(map.begin(), map.end(),
507             [](auto subIter) { EDMLOGD("%{public}s : %{public}s\n", subIter.first.c_str(), subIter.second.c_str()); });
508     });
509 }
510 
DumpCombinedPolicy()511 void PolicyManager::DumpCombinedPolicy()
512 {
513     std::for_each(combinedPolicies_.begin(), combinedPolicies_.end(),
514         [](auto iter) { EDMLOGD("%{public}s : %{public}s\n", iter.first.c_str(), iter.second.c_str()); });
515 }
516 
SetPolicy(const std::string & adminName,const std::string & policyName,const std::string & adminPolicy,const std::string & mergedPolicy)517 ErrCode PolicyManager::SetPolicy(const std::string &adminName, const std::string &policyName,
518     const std::string &adminPolicy, const std::string &mergedPolicy)
519 {
520     if (policyName.empty()) {
521         return ERR_EDM_POLICY_SET_FAILED;
522     }
523 
524     ErrCode err;
525     if (mergedPolicy.empty()) {
526         err = DeleteCombinedPolicy(policyName);
527     } else {
528         err = SetCombinedPolicy(policyName, mergedPolicy);
529     }
530     if (FAILED(err)) {
531         EDMLOGW("Set or delete combined policy failed:%{public}d, merged policy:%{public}s\n",
532             err, mergedPolicy.c_str());
533     }
534 
535     if (!adminName.empty()) {
536         if (adminPolicy.empty()) {
537             err = DeleteAdminPolicy(adminName, policyName);
538         } else {
539             err = SetAdminPolicy(adminName, policyName, adminPolicy);
540         }
541     }
542     if (FAILED(err)) {
543         EDMLOGW("Set or delete admin policy failed:%{public}d, admin policy:%{public}s\n",
544             err, adminPolicy.c_str());
545     }
546 
547     SavePolicy();
548     return err;
549 }
550 
CreateEmptyJsonFile()551 void PolicyManager::CreateEmptyJsonFile()
552 {
553     Json::Value adminObject(Json::arrayValue);
554     Json::Value combinedObject(Json::objectValue);
555     policyRoot_["AdminPolicies"] = adminObject;
556     policyRoot_["CombinedPolicies"] = combinedObject;
557     SavePolicy();
558 }
559 
Init()560 void PolicyManager::Init()
561 {
562     LoadPolicy();
563 }
564 
GetInstance()565 std::shared_ptr<PolicyManager> PolicyManager::GetInstance()
566 {
567     if (instance_ == nullptr) {
568         std::lock_guard<std::mutex> lock(mutexLock_);
569         if (instance_ == nullptr) {
570             instance_.reset(new (std::nothrow) PolicyManager());
571         }
572     }
573     return instance_;
574 }
575 } // namespace EDM
576 } // namespace OHOS
577