• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "logger.h"
19 namespace OHOS {
20 namespace HiviewDFX {
21 DEFINE_LOG_TAG("FreezeDetector");
FreezeCommon()22 FreezeCommon::FreezeCommon()
23 {
24     freezeRuleCluster_ = nullptr;
25 }
26 
~FreezeCommon()27 FreezeCommon::~FreezeCommon()
28 {
29     freezeRuleCluster_ = nullptr;
30 }
31 
Init()32 bool FreezeCommon::Init()
33 {
34     freezeRuleCluster_ = std::make_shared<FreezeRuleCluster>();
35     return freezeRuleCluster_->Init();
36 }
37 
IsFreezeEvent(const std::string & domain,const std::string & stringId) const38 bool FreezeCommon::IsFreezeEvent(const std::string& domain, const std::string& stringId) const
39 {
40     return IsApplicationEvent(domain, stringId) || IsSystemEvent(domain, stringId);
41 }
42 
IsApplicationEvent(const std::string & domain,const std::string & stringId) const43 bool FreezeCommon::IsApplicationEvent(const std::string& domain, const std::string& stringId) const
44 {
45     if (freezeRuleCluster_ == nullptr) {
46         HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
47         return false;
48     }
49     auto applicationPairs = freezeRuleCluster_->GetApplicationPairs();
50     for (auto const &pair : applicationPairs) {
51         if (stringId == pair.first && domain == pair.second.first) {
52             return true;
53         }
54     }
55     return false;
56 }
57 
IsSystemEvent(const std::string & domain,const std::string & stringId) const58 bool FreezeCommon::IsSystemEvent(const std::string& domain, const std::string& stringId) const
59 {
60     if (freezeRuleCluster_ == nullptr) {
61         HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
62         return false;
63     }
64     auto systemPairs = freezeRuleCluster_->GetSystemPairs();
65     for (auto const &pair : systemPairs) {
66         if (stringId == pair.first && domain == pair.second.first) {
67             return true;
68         }
69     }
70     return false;
71 }
72 
IsSystemResult(const FreezeResult & result) const73 bool FreezeCommon::IsSystemResult(const FreezeResult& result) const
74 {
75     return result.GetId() == SYSTEM_RESULT_ID;
76 }
77 
IsApplicationResult(const FreezeResult & result) const78 bool FreezeCommon::IsApplicationResult(const FreezeResult& result) const
79 {
80     return result.GetId() == APPLICATION_RESULT_ID;
81 }
82 
IsBetaVersion() const83 bool FreezeCommon::IsBetaVersion() const
84 {
85     return true;
86 }
87 
GetPrincipalStringIds() const88 std::set<std::string> FreezeCommon::GetPrincipalStringIds() const
89 {
90     std::set<std::string> set;
91     if (freezeRuleCluster_ == nullptr) {
92         HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
93         return set;
94     }
95     auto applicationPairs = freezeRuleCluster_->GetApplicationPairs();
96     auto systemPairs = freezeRuleCluster_->GetSystemPairs();
97     for (auto const &pair : applicationPairs) {
98         if (pair.second.second) {
99             set.insert(pair.first);
100         }
101     }
102     for (auto const &pair : systemPairs) {
103         if (pair.second.second) {
104             set.insert(pair.first);
105         }
106     }
107 
108     return set;
109 }
110 }  // namespace HiviewDFX
111 }  // namespace OHOS