1 /* 2 * Copyright (c) 2021 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 FREEZE_RULE_CLUSTER_H 17 #define FREEZE_RULE_CLUSTER_H 18 19 #include <libxml/parser.h> 20 #include <libxml/tree.h> 21 #include <list> 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 #include "watch_point.h" 27 namespace OHOS { 28 namespace HiviewDFX { 29 class FreezeResult { 30 public: FreezeResult()31 FreezeResult() : window_(0), code_(0), scope_(""), samePackage_(""), domain_(""), stringId_(""), action_("and"), 32 ffrt_("") {}; FreezeResult(long window,const std::string & domain,const std::string & stringId)33 FreezeResult(long window, const std::string& domain, const std::string& stringId) 34 : window_(window), code_(0), scope_(""), samePackage_(""), domain_(domain), stringId_(stringId), 35 action_("and"), ffrt_("") {}; FreezeResult(unsigned long code,const std::string & scope)36 FreezeResult(unsigned long code, const std::string& scope) 37 : window_(0), code_(code), scope_(scope), samePackage_(""), domain_(""), stringId_(""), action_("and"), 38 ffrt_("") {}; ~FreezeResult()39 ~FreezeResult() {}; GetDomain()40 std::string GetDomain() const 41 { 42 return domain_; 43 } 44 GetStringId()45 std::string GetStringId() const 46 { 47 return stringId_; 48 } 49 GetId()50 unsigned long GetId() const 51 { 52 return code_; 53 } 54 SetId(unsigned long code)55 void SetId(unsigned long code) 56 { 57 code_ = code; 58 } 59 GetScope()60 std::string GetScope() const 61 { 62 return scope_; 63 } 64 SetScope(const std::string & scope)65 void SetScope(const std::string& scope) 66 { 67 scope_ = scope; 68 } 69 GetWindow()70 long GetWindow() const 71 { 72 return window_; 73 } 74 GetSamePackage()75 std::string GetSamePackage() const 76 { 77 return samePackage_; 78 } 79 SetSamePackage(const std::string & samePackage)80 void SetSamePackage(const std::string& samePackage) 81 { 82 samePackage_ = samePackage; 83 } 84 GetAction()85 std::string GetAction() const 86 { 87 return action_; 88 } 89 SetAction(const std::string & action)90 void SetAction(const std::string& action) 91 { 92 action_ = action; 93 } 94 GetFfrt()95 std::string GetFfrt() const 96 { 97 return ffrt_; 98 } 99 SetFfrt(const std::string & ffrt)100 void SetFfrt(const std::string& ffrt) 101 { 102 ffrt_ = ffrt; 103 } 104 105 private: 106 long window_; 107 unsigned long code_; 108 std::string scope_; 109 std::string samePackage_; 110 std::string domain_; 111 std::string stringId_; 112 std::string action_; 113 std::string ffrt_; 114 }; 115 116 class FreezeRule { 117 public: FreezeRule()118 FreezeRule() : domain_(""), stringId_("") {}; FreezeRule(const std::string & domain,const std::string & stringId)119 FreezeRule(const std::string& domain, const std::string& stringId) 120 : domain_(domain), stringId_(stringId) {}; ~FreezeRule()121 ~FreezeRule() 122 { 123 results_.clear(); 124 } 125 GetDomain()126 std::string GetDomain() const 127 { 128 return domain_; 129 } 130 SetDomain(const std::string & domain)131 void SetDomain(const std::string& domain) 132 { 133 domain_ = domain; 134 } 135 GetStringId()136 std::string GetStringId() const 137 { 138 return stringId_; 139 } 140 SetStringId(const std::string & stringId)141 void SetStringId(const std::string& stringId) 142 { 143 stringId_ = stringId; 144 } 145 GetMap()146 std::map<std::string, FreezeResult> GetMap() const 147 { 148 return results_; 149 } 150 151 void AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result); 152 bool GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result); 153 154 private: 155 std::string domain_; 156 std::string stringId_; 157 std::map<std::string, FreezeResult> results_; 158 }; 159 160 class FreezeRuleCluster { 161 public: 162 FreezeRuleCluster(); 163 ~FreezeRuleCluster(); 164 FreezeRuleCluster& operator=(const FreezeRuleCluster&) = delete; 165 FreezeRuleCluster(const FreezeRuleCluster&) = delete; 166 167 bool Init(); 168 bool CheckFileSize(const std::string& path); 169 bool ParseRuleFile(const std::string& file); 170 void ParseTagFreeze(xmlNode* tag); 171 void ParseTagRules(xmlNode* tag); 172 void ParseTagRule(xmlNode* tag); 173 void ParseTagLinks(xmlNode* tag, FreezeRule& rule); 174 void ParseTagEvent(xmlNode* tag, FreezeResult& result); 175 void ParseTagResult(xmlNode* tag, FreezeResult& result); 176 void ParseTagRelevance(xmlNode* tag, FreezeResult& result); 177 template<typename T> 178 T GetAttributeValue(xmlNode* node, const std::string& name); 179 bool GetResult(const WatchPoint& watchPoint, std::vector<FreezeResult>& list); GetApplicationPairs()180 std::map<std::string, std::pair<std::string, bool>> GetApplicationPairs() const 181 { 182 return applicationPairs_; 183 } 184 GetSystemPairs()185 std::map<std::string, std::pair<std::string, bool>> GetSystemPairs() const 186 { 187 return systemPairs_; 188 } 189 std::map<std::string, std::pair<std::string, bool>> GetSysWarningPairs() const; 190 191 private: 192 static const inline std::string DEFAULT_RULE_FILE = "/system/etc/hiview/freeze_rules.xml"; 193 static const inline std::string TAG_FREEZE = "freeze"; 194 static const inline std::string TAG_RULES = "rules"; 195 static const inline std::string TAG_RULE = "rule"; 196 static const inline std::string TAG_LINKS = "links"; 197 static const inline std::string TAG_EVENT = "event"; 198 static const inline std::string TAG_RESULT = "result"; 199 static const inline std::string TAG_RELEVANCE = "relevance"; 200 static const inline std::string ATTRIBUTE_ID = "id"; 201 static const inline std::string ATTRIBUTE_WINDOW = "window"; 202 static const inline std::string ATTRIBUTE_DOMAIN = "domain"; 203 static const inline std::string ATTRIBUTE_STRINGID = "stringid"; 204 static const inline std::string ATTRIBUTE_TYPE = "type"; 205 static const inline std::string ATTRIBUTE_USER = "user"; 206 static const inline std::string ATTRIBUTE_WATCHPOINT = "watchpoint"; 207 static const inline std::string ATTRIBUTE_CODE = "code"; 208 static const inline std::string ATTRIBUTE_SCOPE = "scope"; 209 static const inline std::string ATTRIBUTE_SAME_PACKAGE = "samePackage"; 210 static const inline std::string attributeAction = "action"; 211 static const inline std::string attributeFfrt = "ffrt"; 212 static const inline std::string ATTRIBUTE_APPLICATION = "application"; 213 static const inline std::string ATTRIBUTE_SYSTEM = "system"; 214 static const int MAX_FILE_SIZE = 512 * 1024; 215 216 std::map<std::string, FreezeRule> rules_; 217 std::map<std::string, std::pair<std::string, bool>> applicationPairs_; 218 std::map<std::string, std::pair<std::string, bool>> systemPairs_; 219 std::map<std::string, std::pair<std::string, bool>> sysWarningPairs_; 220 }; 221 } // namespace HiviewDFX 222 } // namespace OHOS 223 #endif 224