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