1 /*
2 * Copyright (c) 2022 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 #include "freeze_common.h"
17
18 #include "hiview_logger.h"
19 namespace OHOS {
20 namespace HiviewDFX {
21 namespace {
22 static const int SYSTEM_WARNING_RESULT_ID = 2;
23 }
24 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
FreezeCommon()25 FreezeCommon::FreezeCommon()
26 {
27 freezeRuleCluster_ = nullptr;
28 }
29
~FreezeCommon()30 FreezeCommon::~FreezeCommon()
31 {
32 freezeRuleCluster_ = nullptr;
33 }
34
Init()35 bool FreezeCommon::Init()
36 {
37 freezeRuleCluster_ = std::make_shared<FreezeRuleCluster>();
38 return freezeRuleCluster_->Init();
39 }
40
IsFreezeEvent(const std::string & domain,const std::string & stringId) const41 bool FreezeCommon::IsFreezeEvent(const std::string& domain, const std::string& stringId) const
42 {
43 return IsApplicationEvent(domain, stringId) || IsSystemEvent(domain, stringId) ||
44 IsSysWarningEvent(domain, stringId);
45 }
46
IsApplicationEvent(const std::string & domain,const std::string & stringId) const47 bool FreezeCommon::IsApplicationEvent(const std::string& domain, const std::string& stringId) const
48 {
49 return IsAssignedEvent(domain, stringId, APPLICATION_RESULT_ID);
50 }
51
IsSystemEvent(const std::string & domain,const std::string & stringId) const52 bool FreezeCommon::IsSystemEvent(const std::string& domain, const std::string& stringId) const
53 {
54 return IsAssignedEvent(domain, stringId, SYSTEM_RESULT_ID);
55 }
56
IsSysWarningEvent(const std::string & domain,const std::string & stringId) const57 bool FreezeCommon::IsSysWarningEvent(const std::string& domain, const std::string& stringId) const
58 {
59 return IsAssignedEvent(domain, stringId, SYSTEM_WARNING_RESULT_ID);
60 }
61
IsAssignedEvent(const std::string & domain,const std::string & stringId,int freezeId) const62 bool FreezeCommon::IsAssignedEvent(const std::string& domain, const std::string& stringId, int freezeId) const
63 {
64 if (freezeRuleCluster_ == nullptr) {
65 HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
66 return false;
67 }
68
69 std::map<std::string, std::pair<std::string, bool>> pairs;
70 switch (freezeId) {
71 case APPLICATION_RESULT_ID:
72 pairs = freezeRuleCluster_->GetApplicationPairs();
73 break;
74 case SYSTEM_RESULT_ID:
75 pairs = freezeRuleCluster_->GetSystemPairs();
76 break;
77 case SYSTEM_WARNING_RESULT_ID:
78 pairs = freezeRuleCluster_->GetSysWarningPairs();
79 break;
80 default:
81 return false;
82 }
83 for (auto const &pair : pairs) {
84 if (stringId == pair.first && domain == pair.second.first) {
85 return true;
86 }
87 }
88 return false;
89 }
90
IsSystemResult(const FreezeResult & result) const91 bool FreezeCommon::IsSystemResult(const FreezeResult& result) const
92 {
93 return result.GetId() == SYSTEM_RESULT_ID;
94 }
95
IsApplicationResult(const FreezeResult & result) const96 bool FreezeCommon::IsApplicationResult(const FreezeResult& result) const
97 {
98 return result.GetId() == APPLICATION_RESULT_ID;
99 }
100
IsSysWarningResult(const FreezeResult & result) const101 bool FreezeCommon::IsSysWarningResult(const FreezeResult& result) const
102 {
103 return result.GetId() == SYSTEM_WARNING_RESULT_ID;
104 }
105
IsBetaVersion() const106 bool FreezeCommon::IsBetaVersion() const
107 {
108 return true;
109 }
110
GetPrincipalStringIds() const111 std::set<std::string> FreezeCommon::GetPrincipalStringIds() const
112 {
113 std::set<std::string> set;
114 if (freezeRuleCluster_ == nullptr) {
115 HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
116 return set;
117 }
118 auto applicationPairs = freezeRuleCluster_->GetApplicationPairs();
119 auto systemPairs = freezeRuleCluster_->GetSystemPairs();
120 auto sysWarningPairs = freezeRuleCluster_->GetSysWarningPairs();
121 for (auto const &pair : applicationPairs) {
122 if (pair.second.second) {
123 set.insert(pair.first);
124 }
125 }
126 for (auto const &pair : systemPairs) {
127 if (pair.second.second) {
128 set.insert(pair.first);
129 }
130 }
131 for (auto const &pair : sysWarningPairs) {
132 if (pair.second.second) {
133 set.insert(pair.first);
134 }
135 }
136
137 return set;
138 }
139 } // namespace HiviewDFX
140 } // namespace OHOS