1 /*
2 * Copyright (c) 2025 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 #include "event_validator.h"
16
17 #include "daily_controller.h"
18 #include "event_json_parser.h"
19 #include "hiview_config_util.h"
20 #include "hiview_logger.h"
21 #include "plugin_factory.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 REGISTER(EventValidator);
26 DEFINE_LOG_TAG("EventValidator");
27 namespace {
28 constexpr char THRESHOLD_CONFIG_FILE_NAME[] = "event_threshold.json";
29 }
30
OnLoad()31 void EventValidator::OnLoad()
32 {
33 HIVIEW_LOGI("start to load EventValidator");
34 Init();
35 }
36
Init()37 void EventValidator::Init()
38 {
39 auto context = GetHiviewContext();
40 if (context == nullptr) {
41 HIVIEW_LOGW("context is null");
42 return;
43 }
44
45 InitWorkLoop(context);
46 InitEventVerifyUtil(context);
47 InitController(context);
48 AddListenerInfo(Event::MessageType::ENGINE_UPDATE_PRIVACY_CFG_MSG);
49 context->RegisterUnorderedEventListener(std::static_pointer_cast<EventValidator>(shared_from_this()));
50 }
51
InitWorkLoop(HiviewContext * context)52 void EventValidator::InitWorkLoop(HiviewContext* context)
53 {
54 workLoop_ = context->GetMainWorkLoop();
55 }
56
InitEventVerifyUtil(HiviewContext * context)57 void EventValidator::InitEventVerifyUtil(HiviewContext* context)
58 {
59 eventVerifyUtil_.Init(context);
60 }
61
InitController(HiviewContext * context)62 void EventValidator::InitController(HiviewContext* context)
63 {
64 std::string workPath = context->GetHiViewDirectory(HiviewContext::DirectoryType::WORK_DIRECTORY);
65 controller_ = std::make_unique<DailyController>(
66 workPath, HiViewConfigUtil::GetConfigFilePath(THRESHOLD_CONFIG_FILE_NAME));
67 }
68
OnUnload()69 void EventValidator::OnUnload()
70 {
71 HIVIEW_LOGI("start to unload EventValidator");
72 }
73
OnConfigUpdate(const std::string & localCfgPath,const std::string & cloudCfgPath)74 void EventValidator::OnConfigUpdate(const std::string& localCfgPath, const std::string& cloudCfgPath)
75 {
76 isConfigUpdated_.store(true);
77 }
78
OnEvent(std::shared_ptr<Event> & event)79 bool EventValidator::OnEvent(std::shared_ptr<Event> &event)
80 {
81 if (event == nullptr) {
82 HIVIEW_LOGI("event is null");
83 return false;
84 }
85 if (event->messageType_ != Event::MessageType::SYS_EVENT) {
86 HIVIEW_LOGI("event is not sys event");
87 return false;
88 }
89 if (isConfigUpdated_) {
90 UpdateConfig();
91 }
92 if (!CheckEvent(event)) {
93 event->OnFinish();
94 return false;
95 }
96 return true;
97 }
98
UpdateConfig()99 void EventValidator::UpdateConfig()
100 {
101 // update controller
102 if (controller_ != nullptr) {
103 controller_->OnConfigUpdate(HiViewConfigUtil::GetConfigFilePath(THRESHOLD_CONFIG_FILE_NAME));
104 }
105
106 // update parser
107 EventJsonParser::GetInstance()->OnConfigUpdate();
108
109 // update flag
110 isConfigUpdated_.store(false);
111 }
112
OnUnorderedEvent(const Event & msg)113 void EventValidator::OnUnorderedEvent(const Event& msg)
114 {
115 if (msg.messageType_ == Event::MessageType::ENGINE_UPDATE_PRIVACY_CFG_MSG) {
116 isConfigUpdated_.store(true);
117 }
118 }
119
GetListenerName()120 std::string EventValidator::GetListenerName()
121 {
122 return "EventValidator";
123 }
124
CheckEvent(std::shared_ptr<Event> event)125 bool EventValidator::CheckEvent(std::shared_ptr<Event> event)
126 {
127 std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(event);
128 if (sysEvent == nullptr) {
129 HIVIEW_LOGE("sysevent is null");
130 return false;
131 }
132 eventDelayedUtil_.CheckIfEventDelayed(sysEvent);
133
134 if (controller_ != nullptr && !controller_->CheckThreshold(sysEvent)) {
135 HIVIEW_LOGD("event[%{public}s|%{public}s}] exceeds the threshold",
136 sysEvent->domain_.c_str(), sysEvent->eventName_.c_str());
137 return false;
138 }
139
140 return eventVerifyUtil_.IsValidEvent(sysEvent);
141 }
142 } // namespace HiviewDFX
143 } // namespace OHOS
144