• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 
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 
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     SetTriggerCond({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     AddFilter(std::move(filter));
57     return 0;
58 }
59 
SetOnTrigger(OH_HiAppEvent_OnTrigger onTrigger)60 void NdkAppEventWatcher::SetOnTrigger(OH_HiAppEvent_OnTrigger onTrigger)
61 {
62     std::lock_guard<std::mutex> lockGuard(mutex_);
63     onTrigger_ = onTrigger;
64 }
65 
SetOnOnReceive(OH_HiAppEvent_OnReceive onReceive)66 void NdkAppEventWatcher::SetOnOnReceive(OH_HiAppEvent_OnReceive onReceive)
67 {
68     std::lock_guard<std::mutex> lockGuard(mutex_);
69     onReceive_ = onReceive;
70 }
71 
OnEvents(const std::vector<std::shared_ptr<AppEventPack>> & events)72 void NdkAppEventWatcher::OnEvents(const std::vector<std::shared_ptr<AppEventPack>> &events)
73 {
74     std::lock_guard<std::mutex> lockGuard(mutex_);
75     if (events.empty() || onReceive_ == nullptr) {
76         return;
77     }
78     std::unordered_map<std::string, std::vector<HiAppEvent_AppEventInfo>> eventMap;
79     constexpr size_t strNumPieceEvent = 3;
80     std::vector<std::string> strings(strNumPieceEvent * events.size());
81     size_t strIndex = 0;
82     std::vector<int64_t> eventSeqs;
83     for (const auto &event : events) {
84         auto& appEventInfo = eventMap[event->GetName()].emplace_back();
85         strings[strIndex] = event->GetDomain();
86         appEventInfo.domain = strings[strIndex++].c_str();
87         strings[strIndex] = event->GetName();
88         appEventInfo.name = strings[strIndex++].c_str();
89         strings[strIndex] = event->GetParamStr();
90         appEventInfo.params = strings[strIndex++].c_str();
91         appEventInfo.type = EventType(event->GetType());
92         eventSeqs.emplace_back(event->GetSeq());
93     }
94     std::vector<HiAppEvent_AppEventGroup> appEventGroup(eventMap.size());
95     uint32_t appEventIndex = 0;
96     for (const auto &[k, v] : eventMap) {
97         appEventGroup[appEventIndex].name = k.c_str();
98         appEventGroup[appEventIndex].appEventInfos = v.data();
99         appEventGroup[appEventIndex].infoLen = v.size();
100         appEventIndex++;
101     }
102     int64_t observerSeq = GetSeq();
103     if (!AppEventStore::GetInstance().DeleteData(observerSeq, eventSeqs)) {
104         HILOG_ERROR(LOG_CORE, "failed to delete mapping data, seq=%{public}" PRId64 ", event num=%{public}zu",
105             observerSeq, eventSeqs.size());
106     }
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     std::lock_guard<std::mutex> lockGuard(mutex_);
116     if (onTrigger_ == nullptr) {
117         HILOG_WARN(LOG_CORE, "onTrigger_ is nullptr");
118         return;
119     }
120     onTrigger_(triggerCond.row, triggerCond.size);
121 }
122 
IsRealTimeEvent(std::shared_ptr<AppEventPack> event)123 bool NdkAppEventWatcher::IsRealTimeEvent(std::shared_ptr<AppEventPack> event)
124 {
125     std::lock_guard<std::mutex> lockGuard(mutex_);
126     return onReceive_ != nullptr;
127 }
128 }
129 }
130