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 "ndk_app_event_watcher.h"
17 
18 #include "app_event_store.h"
19 #include "ffrt.h"
20 #include "hilog/log.h"
21 #include "hiappevent_base.h"
22 
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN 0xD002D07
25 
26 #undef LOG_TAG
27 #define LOG_TAG "NdkWatcher"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 
NdkAppEventWatcher(const std::string & name)32 NdkAppEventWatcher::NdkAppEventWatcher(const std::string &name) : AppEventWatcher(name) {}
33 
SetTriggerCondition(int row,int size,int timeOut)34 void NdkAppEventWatcher::SetTriggerCondition(int row, int size, int timeOut)
35 {
36     reportConfig_.triggerCond = {row, size, timeOut * HiAppEvent::TIMEOUT_STEP};
37 }
38 
AddAppEventFilter(const char * domain,uint8_t eventTypes,const char * const * names,int namesLen)39 int NdkAppEventWatcher::AddAppEventFilter(const char* domain, uint8_t eventTypes,
40                                           const char *const *names, int namesLen)
41 {
42     if (domain == nullptr) {
43         return ErrorCode::ERROR_INVALID_EVENT_DOMAIN;
44     }
45     uint32_t types = eventTypes << 1;
46     HiAppEvent::AppEventFilter filter{domain, types};
47     if (names == nullptr && namesLen > 0) {
48         return ErrorCode::ERROR_INVALID_EVENT_NAME;
49     }
50     for (int i = 0; i < namesLen; ++i) {
51         if (names[i] == nullptr) {
52             return ErrorCode::ERROR_INVALID_EVENT_NAME;
53         }
54         filter.names.insert(names[i]);
55     }
56     filters_.emplace_back(std::move(filter));
57     return 0;
58 }
59 
SetOnTrigger(OH_HiAppEvent_OnTrigger onTrigger)60 void NdkAppEventWatcher::SetOnTrigger(OH_HiAppEvent_OnTrigger onTrigger)
61 {
62     onTrigger_ = onTrigger;
63 }
64 
SetOnOnReceive(OH_HiAppEvent_OnReceive onReceive)65 void NdkAppEventWatcher::SetOnOnReceive(OH_HiAppEvent_OnReceive onReceive)
66 {
67     onReceive_ = onReceive;
68 }
69 
OnEvents(const std::vector<std::shared_ptr<AppEventPack>> & events)70 void NdkAppEventWatcher::OnEvents(const std::vector<std::shared_ptr<AppEventPack>> &events)
71 {
72     if (events.empty() || onReceive_ == nullptr) {
73         return;
74     }
75     std::unordered_map<std::string, std::vector<HiAppEvent_AppEventInfo>> eventMap;
76     constexpr size_t strNumPieceEvent = 3;
77     std::vector<std::string> strings(strNumPieceEvent * events.size());
78     size_t strIndex = 0;
79     std::vector<int64_t> eventSeqs;
80     for (const auto &event : events) {
81         auto& appEventInfo = eventMap[event->GetName()].emplace_back();
82         strings[strIndex] = event->GetDomain();
83         appEventInfo.domain = strings[strIndex++].c_str();
84         strings[strIndex] = event->GetName();
85         appEventInfo.name = strings[strIndex++].c_str();
86         strings[strIndex] = event->GetParamStr();
87         appEventInfo.params = strings[strIndex++].c_str();
88         appEventInfo.type = EventType(event->GetType());
89         eventSeqs.emplace_back(event->GetSeq());
90     }
91     std::vector<HiAppEvent_AppEventGroup> appEventGroup(eventMap.size());
92     uint32_t appEventIndex = 0;
93     for (const auto &[k, v] : eventMap) {
94         appEventGroup[appEventIndex].name = k.c_str();
95         appEventGroup[appEventIndex].appEventInfos = v.data();
96         appEventGroup[appEventIndex].infoLen = v.size();
97         appEventIndex++;
98     }
99     int64_t observerSeq = GetSeq();
100     ffrt::submit([observerSeq, eventSeqs]() {
101         if (!AppEventStore::GetInstance().DeleteData(observerSeq, eventSeqs)) {
102             HILOG_ERROR(LOG_CORE, "failed to delete mapping data, seq=%{public}" PRId64 ", event num=%{public}zu",
103                 observerSeq, eventSeqs.size());
104         }
105         }, {}, {}, ffrt::task_attr().name("appevent_del_map"));
106     std::string domain = events[0]->GetDomain();
107     onReceive_(domain.c_str(), appEventGroup.data(), static_cast<uint32_t>(eventMap.size()));
108 }
109 
OnTrigger(const HiAppEvent::TriggerCondition & triggerCond)110 void NdkAppEventWatcher::OnTrigger(const HiAppEvent::TriggerCondition &triggerCond)
111 {
112     HILOG_DEBUG(LOG_CORE, "onTrigger start");
113     if (onTrigger_ == nullptr) {
114         HILOG_WARN(LOG_CORE, "onTrigger_ is nullptr");
115         return;
116     }
117     onTrigger_(triggerCond.row, triggerCond.size);
118 }
119 
IsRealTimeEvent(std::shared_ptr<AppEventPack> event)120 bool NdkAppEventWatcher::IsRealTimeEvent(std::shared_ptr<AppEventPack> event)
121 {
122     return onReceive_ != nullptr;
123 }
124 }
125 }