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