1 /*
2  * Copyright (c) 2024 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 "compliant_event_checker.h"
17 
18 #include <list>
19 #include <unordered_map>
20 
21 #include "hiview_logger.h"
22 #include "parameter_ex.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 DEFINE_LOG_TAG("CompliantEventChecker");
28 
29 constexpr int64_t SECURE_ENABALED_VAL = 1;
30 
31 std::unordered_map<std::string, std::list<std::string>> COMPLIANT_EVENT_CONFIGS {
32     {"AAFWK", {"ABILITY_ONACTIVE", "ABILITY_ONBACKGROUND", "ABILITY_ONFOREGROUND",
33         "ABILITY_ONINACTIVE", "JS_ERROR", "START_ABILITY", "TERMINATE_ABILITY"}},
34     {"ACE", {"INTERACTION_COMPLETED_LATENCY", "JS_ERROR"}},
35     {"AV_CODEC", {"CODEC_START_INFO"}},
36     {"GRAPHIC", {"INTERACTION_COMPLETED_LATENCY", "INTERACTION_RESPONSE_LATENCY"}},
37     {"LOCATION", {"GNSS_STATE"}},
38     {"PERFORMANCE", {}},
39     {"RELIABILITY", {}},
40     {"SANITIZER", {"ADDR_SANITIZER"}},
41     {"WORKSCHEDULER", {"WORK_ADD", "WORK_REMOVE", "WORK_START", "WORK_STOP"}},
42 };
43 }
44 
CompliantEventChecker()45 CompliantEventChecker::CompliantEventChecker()
46 {
47     isSecureEnabeled_ = (Parameter::GetInteger("const.secure", SECURE_ENABALED_VAL) == SECURE_ENABALED_VAL);
48     HIVIEW_LOGD("value of const.secure is %{public}d", isSecureEnabeled_);
49 }
50 
IsCompliantEvent(const std::string& domain, const std::string& eventName)51 bool CompliantEventChecker::IsCompliantEvent(const std::string& domain, const std::string& eventName)
52 {
53     if (!isSecureEnabeled_) {
54         return true;
55     }
56     auto iter = COMPLIANT_EVENT_CONFIGS.find(domain);
57     if (iter == COMPLIANT_EVENT_CONFIGS.end()) {
58         return false;
59     }
60     if (iter->second.empty()) {
61         HIVIEW_LOGD("event with domain [%{public}s] is compliant", domain.c_str());
62         return true;
63     }
64     auto findRet = std::find(iter->second.begin(), iter->second.end(), eventName);
65     if (findRet != iter->second.end()) {
66         HIVIEW_LOGD("event [%{public}s|%{public}s] is compliant", domain.c_str(), eventName.c_str());
67         return true;
68     }
69     return false;
70 }
71 } // namespace HiviewDFX
72 } // namespace OHOS