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 static std::unordered_map<std::string, std::string> StringToMap(const std::string& eventStr); 42 static nlohmann::json StringToJsonObj(const std::string& eventStr); 43 static std::unordered_map<std::string, std::string> JsonObjToMap(const nlohmann::json& eventJson); 44 static bool CheckLogicRelationship(const std::string& state, const std::string& profile); 45 private: 46 void CloseSo(); 47 uint32_t GetBootPriorityPara(const std::string& bootPhase); 48 uint32_t GetOndemandPriorityPara(const std::string& loadpriority); 49 void OpenSo(SaProfile& saProfile); 50 void CloseHandle(SaProfile& saProfile); 51 bool ParseJsonFile(const std::string& realPath); 52 bool ParseJsonObj(nlohmann::json& jsonObj, const std::string& jsonPath); 53 bool ParseSystemAbilityGetSaBaseInfo(SaProfile& saProfile, nlohmann::json& systemAbilityJson); 54 bool ParseSystemAbilityGetSaExtInfo(SaProfile& saProfile, nlohmann::json& systemAbilityJson); 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 bool ParseSystemAbilityGetExtension(SaProfile& saProfile, nlohmann::json& systemAbilityJson); 64 void GetOnDemandArrayFromJson(int32_t eventId, const nlohmann::json& obj, 65 const std::string& key, std::vector<OnDemandEvent>& out); 66 void GetOnDemandConditionsFromJson(const nlohmann::json& obj, 67 const std::string& key, std::vector<OnDemandCondition>& out); 68 void GetOnDemandExtraMessagesFromJson(const nlohmann::json& obj, 69 const std::string& key, std::map<std::string, std::string>& out); 70 bool CheckRecycleStrategy(const std::string& recycleStrategyStr, int32_t& recycleStrategy); 71 GetBoolFromJson(const nlohmann::json & obj,const std::string & key,bool & out)72 static inline void GetBoolFromJson(const nlohmann::json& obj, const std::string& key, bool& out) 73 { 74 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_boolean()) { 75 obj[key.c_str()].get_to(out); 76 } 77 } 78 GetStringFromJson(const nlohmann::json & obj,const std::string & key,std::string & out)79 static inline void GetStringFromJson(const nlohmann::json& obj, const std::string& key, std::string& out) 80 { 81 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_string()) { 82 obj[key.c_str()].get_to(out); 83 } 84 } 85 GetInt32FromJson(const nlohmann::json & obj,const std::string & key,int32_t & out)86 static inline void GetInt32FromJson(const nlohmann::json& obj, const std::string& key, int32_t& out) 87 { 88 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_number_integer()) { 89 obj[key.c_str()].get_to(out); 90 } 91 } 92 GetStringArrayFromJson(const nlohmann::json & obj,const std::string & key,std::vector<std::string> & out)93 static inline void GetStringArrayFromJson(const nlohmann::json& obj, const std::string& key, 94 std::vector<std::string>& out) 95 { 96 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) { 97 for (auto& item : obj[key.c_str()]) { 98 if (item.is_string()) { 99 out.emplace_back(item.get<std::string>()); 100 } 101 } 102 } 103 } 104 GetIntArrayFromJson(const nlohmann::json & obj,const std::string & key,std::set<int32_t> & out)105 static inline void GetIntArrayFromJson(const nlohmann::json& obj, const std::string& key, 106 std::set<int32_t>& out) 107 { 108 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) { 109 for (auto& item : obj[key.c_str()]) { 110 if (item.is_number_integer()) { 111 out.insert(item.get<int32_t>()); 112 } 113 } 114 } 115 } 116 GetIntArrayFromJson(const nlohmann::json & obj,const std::string & key,std::vector<int32_t> & out)117 static inline void GetIntArrayFromJson(const nlohmann::json& obj, const std::string& key, 118 std::vector<int32_t>& out) 119 { 120 if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) { 121 for (auto& item : obj[key.c_str()]) { 122 if (item.is_number_integer()) { 123 out.emplace_back(item.get<int32_t>()); 124 } 125 } 126 } 127 } 128 static bool Endswith(const std::string& src, const std::string& sub); 129 std::string GetRealPath(const std::string& profilePath) const; 130 std::list<SaProfile> saProfiles_; 131 std::u16string procName_; 132 std::vector<std::string> updateVec_; 133 }; 134 } // namespace OHOS 135 136 #endif // SAMGR_INTERFACE_INNERKITS_COMMOM_INCLUDE_PARSE_UTIL_H 137