1 /*
2  * Copyright (c) 2022-2023 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 #ifndef HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_APP_EVENT_WATCHER_H
16 #define HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_APP_EVENT_WATCHER_H
17 
18 #include "app_event_watcher.h"
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 struct OnTriggerContext {
25     ~OnTriggerContext();
26     napi_env env = nullptr;
27     napi_ref onTrigger = nullptr;
28     napi_ref holder = nullptr;
29     int row = 0;
30     int size = 0;
31 };
32 
33 struct OnReceiveContext {
34     ~OnReceiveContext();
35     napi_env env = nullptr;
36     napi_ref onReceive = nullptr;
37     std::string domain;
38     std::vector<std::shared_ptr<AppEventPack>> events;
39     int64_t observerSeq = 0;
40 };
41 
42 struct WatcherContext {
43     ~WatcherContext();
44     OnTriggerContext* triggerContext = nullptr;
45     OnReceiveContext* receiveContext = nullptr;
46 };
47 
48 class NapiAppEventWatcher : public AppEventWatcher {
49 public:
50     NapiAppEventWatcher(
51         const std::string& name,
52         const std::vector<AppEventFilter>& filters,
53         TriggerCondition cond);
54     ~NapiAppEventWatcher();
55     void InitTrigger(const napi_env env, const napi_value trigger);
56     void InitHolder(const napi_env env, const napi_value holder);
57     void InitReceiver(const napi_env env, const napi_value receiver);
58     void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) override;
59     bool IsRealTimeEvent(std::shared_ptr<AppEventPack> event) override;
60 
61 protected:
62     void OnTrigger(const TriggerCondition& triggerCond) override;
63 
64 private:
65     WatcherContext* context_;
66 };
67 } // namespace HiviewDFX
68 } // namespace OHOS
69 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_APP_EVENT_WATCHER_H
70