• 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 SAMGR_INTERFACE_INNERKITS_COMMOM_INCLUDE_PARSE_UTIL_H
17 #define SAMGR_INTERFACE_INNERKITS_COMMOM_INCLUDE_PARSE_UTIL_H
18 
19 #include <list>
20 #include <map>
21 #include <set>
22 #include <string>
23 #include "sa_profiles.h"
24 #include "nlohmann/json.hpp"
25 
26 namespace OHOS {
27 class ParseUtil {
28 public:
29     ~ParseUtil();
30     bool ParseSaProfiles(const std::string& profilePath);
31     const std::list<SaProfile>& GetAllSaProfiles() const;
32     bool GetProfile(int32_t saId, SaProfile& saProfile);
33     void ClearResource();
34     void OpenSo(uint32_t bootPhase);
35     void CloseSo(int32_t systemAbilityId);
36     bool LoadSaLib(int32_t systemAbilityId);
37     bool ParseTrustConfig(const std::string& profilePath, std::map<std::u16string, std::set<int32_t>>& values);
38     void RemoveSaProfile(int32_t saId);
39     bool CheckPathExist(const std::string& profilePath);
40     std::u16string GetProcessName() const;
41     void SetUpdateList(const std::vector<std::string>& updateVec);
42     static std::unordered_map<std::string, std::string> StringToMap(const std::string& eventStr);
43     static nlohmann::json StringToJsonObj(const std::string& eventStr);
44     static std::unordered_map<std::string, std::string> JsonObjToMap(const nlohmann::json& eventJson);
45     static bool CheckLogicRelationship(const std::string& state, const std::string& profile);
46 private:
47     void CloseSo();
48     uint32_t GetBootPriorityPara(const std::string& bootPhase);
49     uint32_t GetOndemandPriorityPara(const std::string& loadpriority);
50     void OpenSo(SaProfile& saProfile);
51     bool IsUpdateSA(const std::string& saId);
52     void CloseHandle(SaProfile& saProfile);
53     bool ParseJsonFile(const std::string& realPath);
54     bool ParseJsonObj(nlohmann::json& jsonObj, const std::string& jsonPath);
55     bool ParseSystemAbility(SaProfile& saProfile, nlohmann::json& systemAbilityJson);
56     bool ParseJsonTag(const nlohmann::json& systemAbilityJson, const std::string& jsonTag,
57         nlohmann::json& onDemandJson);
58     void ParseOndemandTag(const nlohmann::json& onDemandJson, std::vector<OnDemandEvent>& onDemandEvents);
59     void ParseStartOndemandTag(const nlohmann::json& systemAbilityJson,
60         const std::string& jsonTag, StartOnDemand& startOnDemand);
61     void ParseStopOndemandTag(const nlohmann::json& systemAbilityJson,
62         const std::string& jsonTag, StopOnDemand& stopOnDemand);
63     void GetOnDemandArrayFromJson(int32_t eventId, const nlohmann::json& obj,
64         const std::string& key, std::vector<OnDemandEvent>& out);
65     void GetOnDemandConditionsFromJson(const nlohmann::json& obj,
66         const std::string& key, std::vector<OnDemandCondition>& out);
67     void GetOnDemandExtraMessagesFromJson(const nlohmann::json& obj,
68         const std::string& key, std::map<std::string, std::string>& out);
69     bool CheckRecycleStrategy(const std::string& recycleStrategyStr, int32_t& recycleStrategy);
70 
GetBoolFromJson(const nlohmann::json & obj,const std::string & key,bool & out)71     static inline void GetBoolFromJson(const nlohmann::json& obj, const std::string& key, bool& out)
72     {
73         if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_boolean()) {
74             obj[key.c_str()].get_to(out);
75         }
76     }
77 
GetStringFromJson(const nlohmann::json & obj,const std::string & key,std::string & out)78     static inline void GetStringFromJson(const nlohmann::json& obj, const std::string& key, std::string& out)
79     {
80         if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_string()) {
81             obj[key.c_str()].get_to(out);
82         }
83     }
84 
GetInt32FromJson(const nlohmann::json & obj,const std::string & key,int32_t & out)85     static inline void GetInt32FromJson(const nlohmann::json& obj, const std::string& key, int32_t& out)
86     {
87         if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_number_integer()) {
88             obj[key.c_str()].get_to(out);
89         }
90     }
91 
GetStringArrayFromJson(const nlohmann::json & obj,const std::string & key,std::vector<std::string> & out)92     static inline void GetStringArrayFromJson(const nlohmann::json& obj, const std::string& key,
93         std::vector<std::string>& out)
94     {
95         if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) {
96             for (auto& item : obj[key.c_str()]) {
97                 if (item.is_string()) {
98                     out.emplace_back(item.get<std::string>());
99                 }
100             }
101         }
102     }
103 
GetIntArrayFromJson(const nlohmann::json & obj,const std::string & key,std::set<int32_t> & out)104     static inline void GetIntArrayFromJson(const nlohmann::json& obj, const std::string& key,
105         std::set<int32_t>& out)
106     {
107         if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) {
108             for (auto& item : obj[key.c_str()]) {
109                 if (item.is_number_integer()) {
110                     out.insert(item.get<int32_t>());
111                 }
112             }
113         }
114     }
115 
GetIntArrayFromJson(const nlohmann::json & obj,const std::string & key,std::vector<int32_t> & out)116     static inline void GetIntArrayFromJson(const nlohmann::json& obj, const std::string& key,
117         std::vector<int32_t>& out)
118     {
119         if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) {
120             for (auto& item : obj[key.c_str()]) {
121                 if (item.is_number_integer()) {
122                     out.emplace_back(item.get<int32_t>());
123                 }
124             }
125         }
126     }
127     static bool Endswith(const std::string& src, const std::string& sub);
128     std::string GetRealPath(const std::string& profilePath) const;
129     std::list<SaProfile> saProfiles_;
130     std::u16string procName_;
131     std::vector<std::string> updateVec_;
132 };
133 } // namespace OHOS
134 
135 #endif // SAMGR_INTERFACE_INNERKITS_COMMOM_INCLUDE_PARSE_UTIL_H
136