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 #include "app_event_watcher.h"
16
17 #include "app_event_cache.h"
18 #include "hilog/log.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace {
23 const HiLogLabel LABEL = { LOG_CORE, 0xD002D07, "HiAppEvent_Watcher" };
24 constexpr unsigned int BIT_MASK = 1;
25 }
AppEventWatcher(const std::string & name,const std::map<std::string,unsigned int> & filters,TriggerCondition cond)26 AppEventWatcher::AppEventWatcher(const std::string& name, const std::map<std::string, unsigned int>& filters,
27 TriggerCondition cond) : name_(name), filters_(filters), cond_(cond), status_({ 0 })
28 {}
29
OnTrigger(int row,int size)30 void AppEventWatcher::OnTrigger(int row, int size)
31 {
32 HiLog::Info(LABEL, "default OnTrigger implementation, row=%{public}d, size=%{public}d", row, size);
33 }
34
GetName() const35 std::string AppEventWatcher::GetName() const
36 {
37 return name_;
38 }
39
GetCond() const40 TriggerCondition AppEventWatcher::GetCond() const
41 {
42 return cond_;
43 }
44
ProcessEvent(const std::string & domain,int type,const std::string & event)45 void AppEventWatcher::ProcessEvent(const std::string& domain, int type, const std::string& event)
46 {
47 HiLog::Debug(LABEL, "watcher=%{public}s start to process event", name_.c_str());
48 if (!IsInterestedEvent(domain, type)) {
49 return;
50 }
51 auto block = AppEventCache::GetInstance()->GetBlock(name_);
52 if (block == nullptr) {
53 HiLog::Error(LABEL, "failed to get block=%{public}s from cache", name_.c_str());
54 return;
55 }
56 if (block->Add(event) != 0) {
57 HiLog::Error(LABEL, "failed to add event to the block=%{public}s", name_.c_str());
58 return;
59 }
60
61 std::lock_guard<std::mutex> lock(mutex_);
62 ++status_.row;
63 status_.size += static_cast<int>(event.size());
64 if ((cond_.row > 0 && status_.row >= cond_.row) || (cond_.size > 0 && status_.size >= cond_.size)) {
65 OnTrigger(status_.row, status_.size);
66 ResetStatus();
67 }
68 }
69
ProcessTimeOut()70 void AppEventWatcher::ProcessTimeOut()
71 {
72 HiLog::Debug(LABEL, "watcher=%{public}s start to process timeOut", name_.c_str());
73 std::lock_guard<std::mutex> lock(mutex_);
74 ++status_.timeOut;
75 if ((cond_.timeOut > 0 && status_.timeOut >= cond_.timeOut) && status_.row > 0) {
76 OnTrigger(status_.row, status_.size);
77 ResetStatus();
78 }
79 }
80
IsInterestedEvent(const std::string & domain,int type)81 bool AppEventWatcher::IsInterestedEvent(const std::string& domain, int type)
82 {
83 if (filters_.empty()) {
84 return true;
85 }
86 if (auto it = filters_.find(domain); it != filters_.end() && (it->second & (BIT_MASK << type))) {
87 return true;
88 }
89 return false;
90 }
91
ResetStatus()92 void AppEventWatcher::ResetStatus()
93 {
94 status_.row = 0;
95 status_.size = 0;
96 status_.timeOut = 0;
97 }
98 } // namespace HiviewDFX
99 } // namespace OHOS
100