• 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     napi_env env = nullptr;
27     napi_ref onTrigger = nullptr;
28     napi_ref holder = nullptr;
29 };
30 
31 struct OnReceiveContext {
32     ~OnReceiveContext();
33     napi_env env = nullptr;
34     napi_ref onReceive = nullptr;
35 };
36 
37 class NapiAppEventWatcher : public AppEventWatcher {
38 public:
39     NapiAppEventWatcher(
40         const std::string& name,
41         const std::vector<AppEventFilter>& filters,
42         TriggerCondition cond);
43     ~NapiAppEventWatcher();
44     void DeleteWatcherContext();
45     void InitTrigger(const napi_env env, const napi_value trigger);
46     void InitHolder(const napi_env env, const napi_value holder);
47     void InitReceiver(const napi_env env, const napi_value receiver);
48     void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) override;
49     bool IsRealTimeEvent(std::shared_ptr<AppEventPack> event) override;
50 
51 protected:
52     void OnTrigger(const TriggerCondition& triggerCond) override;
53 
54 private:
55     std::shared_ptr<OnTriggerContext> triggerContext_ = nullptr;
56     std::shared_ptr<OnReceiveContext> receiveContext_ = nullptr;
57     std::mutex mutex_;
58 };
59 } // namespace HiviewDFX
60 } // namespace OHOS
61 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_APP_EVENT_WATCHER_H
62