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