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 private: 46 void CloseSo(); 47 uint32_t GetBootPriorityPara(const std::string& bootPhase); 48 void OpenSo(SaProfile& saProfile); 49 bool IsUpdateSA(const std::string& saId); 50 void CloseHandle(SaProfile& saProfile); 51 bool ParseJsonFile(const std::string& realPath); 52 bool ParseJsonObj(nlohmann::json& jsonObj, const std::string& jsonPath); 53 bool ParseSystemAbility(SaProfile& saProfile, nlohmann::json& systemAbilityJson); 54 bool ParseJsonTag(const nlohmann::json& systemAbilityJson, const std::string& jsonTag, 55 nlohmann::json& onDemandJson); 56 void ParseOndemandTag(const nlohmann::json& onDemandJson, std::vector<OnDemandEvent>& onDemandEvents); 57 void ParseStartOndemandTag(const nlohmann::json& systemAbilityJson, 58 const std::string& jsonTag, StartOnDemand& startOnDemand); 59 void ParseStopOndemandTag(const nlohmann::json& systemAbilityJson, 60 const std::string& jsonTag, StopOnDemand& stopOnDemand); 61 void GetOnDemandArrayFromJson(int32_t eventId, const nlohmann::json& obj, 62 const std::string& key, std::vector<OnDemandEvent>& out); 63 void GetOnDemandConditionsFromJson(const nlohmann::json& obj, 64 const std::string& key, std::vector<OnDemandCondition>& out); 65 GetBoolFromJson(const nlohmann::json & obj,const std::string & key,bool & out)66 static inline void GetBoolFromJson(const nlohmann::json& obj, const std::string& key, bool& out) 67 { 68 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_boolean()) { 69 obj[key.c_str()].get_to(out); 70 } 71 } 72 GetStringFromJson(const nlohmann::json & obj,const std::string & key,std::string & out)73 static inline void GetStringFromJson(const nlohmann::json& obj, const std::string& key, std::string& out) 74 { 75 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_string()) { 76 obj[key.c_str()].get_to(out); 77 } 78 } 79 GetInt32FromJson(const nlohmann::json & obj,const std::string & key,int32_t & out)80 static inline void GetInt32FromJson(const nlohmann::json& obj, const std::string& key, int32_t& out) 81 { 82 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_number_integer()) { 83 obj[key.c_str()].get_to(out); 84 } 85 } 86 GetStringArrayFromJson(const nlohmann::json & obj,const std::string & key,std::vector<std::string> & out)87 static inline void GetStringArrayFromJson(const nlohmann::json& obj, const std::string& key, 88 std::vector<std::string>& out) 89 { 90 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) { 91 for (auto& item : obj[key.c_str()]) { 92 if (item.is_string()) { 93 out.emplace_back(item.get<std::string>()); 94 } 95 } 96 } 97 } 98 GetIntArrayFromJson(const nlohmann::json & obj,const std::string & key,std::set<int32_t> & out)99 static inline void GetIntArrayFromJson(const nlohmann::json& obj, const std::string& key, 100 std::set<int32_t>& out) 101 { 102 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) { 103 for (auto& item : obj[key.c_str()]) { 104 if (item.is_number_integer()) { 105 out.insert(item.get<int32_t>()); 106 } 107 } 108 } 109 } 110 GetIntArrayFromJson(const nlohmann::json & obj,const std::string & key,std::vector<int32_t> & out)111 static inline void GetIntArrayFromJson(const nlohmann::json& obj, const std::string& key, 112 std::vector<int32_t>& out) 113 { 114 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) { 115 for (auto& item : obj[key.c_str()]) { 116 if (item.is_number_integer()) { 117 out.emplace_back(item.get<int32_t>()); 118 } 119 } 120 } 121 } 122 static bool Endswith(const std::string& src, const std::string& sub); 123 std::string GetRealPath(const std::string& profilePath) const; 124 std::list<SaProfile> saProfiles_; 125 std::u16string procName_; 126 std::vector<std::string> updateVec_; 127 }; 128 } // namespace OHOS 129 130 #endif // SAMGR_INTERFACE_INNERKITS_COMMOM_INCLUDE_PARSE_UTIL_H 131