• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ~OnTriggerContext();
27     napi_env env;
28     napi_ref onTrigger;
29     napi_ref holder;
30     int row;
31     int size;
32 };
33 
34 struct OnReceiveContext {
35     OnReceiveContext();
36     ~OnReceiveContext();
37     napi_env env;
38     napi_ref onReceive;
39     std::string domain;
40     std::vector<std::shared_ptr<AppEventPack>> events;
41 };
42 
43 struct WatcherContext {
44     WatcherContext();
45     ~WatcherContext();
46     OnTriggerContext* triggerContext;
47     OnReceiveContext* receiveContext;
48 };
49 
50 class NapiAppEventWatcher : public AppEventWatcher {
51 public:
52     NapiAppEventWatcher(
53         const std::string& name,
54         const std::vector<AppEventFilter>& filters,
55         TriggerCondition cond);
56     ~NapiAppEventWatcher();
57     void InitTrigger(const napi_env env, const napi_value trigger);
58     void InitHolder(const napi_env env, const napi_value holder);
59     void InitReceiver(const napi_env env, const napi_value receiver);
60     void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) override;
61     bool IsRealTimeEvent(std::shared_ptr<AppEventPack> event) override;
62 
63 protected:
64     void OnTrigger(const TriggerCondition& triggerCond) override;
65 
66 private:
67     WatcherContext* context_;
68 };
69 } // namespace HiviewDFX
70 } // namespace OHOS
71 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_APP_EVENT_WATCHER_H
72