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