• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_proxy.h"
17 
18 #include "app_event_observer_mgr.h"
19 #include "app_event_util.h"
20 #include "hilog/log.h"
21 #include "hiappevent_base.h"
22 #include "app_event_store.h"
23 
24 #undef LOG_DOMAIN
25 #define LOG_DOMAIN 0xD002D07
26 
27 #undef LOG_TAG
28 #define LOG_TAG "NdkWatcherProxy"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 
NdkAppEventWatcherProxy(const std::string & name)33 NdkAppEventWatcherProxy::NdkAppEventWatcherProxy(const std::string &name)
34     : watcher_(std::make_shared<NdkAppEventWatcher>(name)) {}
35 
SetTriggerCondition(int row,int size,int timeOut)36 int NdkAppEventWatcherProxy::SetTriggerCondition(int row, int size, int timeOut)
37 {
38     watcher_->SetTriggerCondition(row, size, timeOut);
39     return 0;
40 }
41 
SetAppEventFilter(const char * domain,uint8_t eventTypes,const char * const * names,int namesLen)42 int NdkAppEventWatcherProxy::SetAppEventFilter(const char *domain, uint8_t eventTypes,
43                                                const char *const *names, int namesLen)
44 {
45     return watcher_->AddAppEventFilter(domain, eventTypes, names, namesLen);
46 }
47 
SetWatcherOnTrigger(OH_HiAppEvent_OnTrigger onTrigger)48 int NdkAppEventWatcherProxy::SetWatcherOnTrigger(OH_HiAppEvent_OnTrigger onTrigger)
49 {
50     watcher_->SetOnTrigger(onTrigger);
51     return 0;
52 }
53 
SetWatcherOnReceiver(OH_HiAppEvent_OnReceive onReceiver)54 int NdkAppEventWatcherProxy::SetWatcherOnReceiver(OH_HiAppEvent_OnReceive onReceiver)
55 {
56     watcher_->SetOnOnReceive(onReceiver);
57     return 0;
58 }
59 
AddWatcher()60 int NdkAppEventWatcherProxy::AddWatcher()
61 {
62     AppEventObserverMgr::GetInstance().AddWatcher(watcher_);
63     return 0;
64 }
65 
TakeWatcherData(uint32_t size,OH_HiAppEvent_OnTake onTake)66 int NdkAppEventWatcherProxy::TakeWatcherData(uint32_t size, OH_HiAppEvent_OnTake onTake)
67 {
68     if (onTake == nullptr) {
69         HILOG_WARN(LOG_CORE, "onTake is nullptr");
70         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
71     }
72     if (watcher_->GetSeq() == 0) {
73         HILOG_WARN(LOG_CORE, "failed to query events, the observer has not been added");
74         return ErrorCode::ERROR_WATCHER_NOT_ADDED;
75     }
76     std::vector<std::shared_ptr<AppEventPack>> events;
77     if (AppEventStore::GetInstance().TakeEvents(events, watcher_->GetSeq(), size) != 0) {
78         HILOG_WARN(LOG_CORE, "failed to query events, seq=%{public}" PRId64, watcher_->GetSeq());
79         return ErrorCode::ERROR_UNKNOWN;
80     }
81     std::vector<std::string> eventStrs(events.size());
82     std::vector<const char*> retEvents(events.size());
83     for (size_t t = 0; t < events.size(); ++t) {
84         eventStrs[t] = events[t]->GetEventStr();
85         retEvents[t] = eventStrs[t].c_str();
86     }
87     AppEventUtil::ReportAppEventReceive(events, watcher_->GetName(), "takeNext");
88     onTake(retEvents.data(), static_cast<int32_t>(eventStrs.size()));
89     return 0;
90 }
91 
RemoveWatcher()92 int NdkAppEventWatcherProxy::RemoveWatcher()
93 {
94     int64_t watcherSeq = watcher_->GetSeq();
95     if (watcherSeq > 0) {
96         AppEventObserverMgr::GetInstance().RemoveObserver(watcherSeq);
97         watcher_->SetSeq(0);
98         return 0;
99     }
100     return ErrorCode::ERROR_WATCHER_NOT_ADDED;
101 }
102 }
103 }
104