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