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), delay_(0), code_(0), scope_(""), samePackage_(""), domain_(""), stringId_(""), 32 action_("and") {}; 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), delay_(0), code_(0), scope_(""), samePackage_(""), domain_(domain), stringId_(stringId), 35 action_("and") {}; FreezeResult(unsigned long code,const std::string & scope)36 FreezeResult(unsigned long code, const std::string& scope) 37 : window_(0), delay_(0), code_(code), scope_(scope), samePackage_(""), domain_(""), stringId_(""), 38 action_("and") {}; ~FreezeResult()39 ~FreezeResult() {}; 40 std::string GetDomain() const; 41 std::string GetStringId() const; 42 unsigned long GetId() const; 43 void SetId(unsigned long code); 44 std::string GetScope() const; 45 void SetScope(const std::string& scope); 46 long GetWindow() const; 47 long GetDelay() const; 48 void SetDelay(long delay); 49 std::string GetSamePackage() const; 50 void SetSamePackage(const std::string& samePackage); 51 std::string GetAction() const; 52 void SetAction(const std::string& action); 53 54 private: 55 long window_; 56 long delay_; 57 unsigned long code_; 58 std::string scope_; 59 std::string samePackage_; 60 std::string domain_; 61 std::string stringId_; 62 std::string action_; 63 }; 64 65 class FreezeRule { 66 public: FreezeRule()67 FreezeRule() : domain_(""), stringId_("") {}; FreezeRule(const std::string & domain,const std::string & stringId)68 FreezeRule(const std::string& domain, const std::string& stringId) 69 : domain_(domain), stringId_(stringId) {}; ~FreezeRule()70 ~FreezeRule() 71 { 72 results_.clear(); 73 } 74 75 std::string GetDomain() const; 76 void SetDomain(const std::string& domain); 77 std::string GetStringId() const; 78 void SetStringId(const std::string& stringId); 79 std::map<std::string, FreezeResult> GetMap() const; 80 81 void AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result); 82 bool GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result); 83 84 private: 85 std::string domain_; 86 std::string stringId_; 87 std::map<std::string, FreezeResult> results_; 88 }; 89 90 class FreezeRuleCluster { 91 public: 92 FreezeRuleCluster(); 93 ~FreezeRuleCluster(); 94 FreezeRuleCluster& operator=(const FreezeRuleCluster&) = delete; 95 FreezeRuleCluster(const FreezeRuleCluster&) = delete; 96 97 bool Init(); 98 bool CheckFileSize(const std::string& path); 99 bool ParseRuleFile(const std::string& file); 100 void ParseTagFreeze(xmlNode* tag); 101 void ParseTagRules(xmlNode* tag); 102 void ParseTagRule(xmlNode* tag); 103 void ParseTagLinks(xmlNode* tag, FreezeRule& rule); 104 void ParseTagEvent(xmlNode* tag, FreezeResult& result); 105 void ParseTagResult(xmlNode* tag, FreezeResult& result); 106 void ParseTagRelevance(xmlNode* tag, FreezeResult& result); 107 template<typename T> 108 T GetAttributeValue(xmlNode* node, const std::string& name); 109 bool GetResult(const WatchPoint& watchPoint, std::vector<FreezeResult>& list); 110 std::map<std::string, std::pair<std::string, bool>> GetApplicationPairs() const; 111 std::map<std::string, std::pair<std::string, bool>> GetSystemPairs() const; 112 std::map<std::string, std::pair<std::string, bool>> GetSysWarningPairs() const; 113 114 private: 115 std::map<std::string, FreezeRule> rules_; 116 std::map<std::string, std::pair<std::string, bool>> applicationPairs_; 117 std::map<std::string, std::pair<std::string, bool>> systemPairs_; 118 std::map<std::string, std::pair<std::string, bool>> sysWarningPairs_; 119 }; 120 } // namespace HiviewDFX 121 } // namespace OHOS 122 #endif 123