• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 #ifndef NFC_NAPI_CONTROLLER_EVENT_H_
17 #define NFC_NAPI_CONTROLLER_EVENT_H_
18 
19 #include <map>
20 #include <set>
21 #include <shared_mutex>
22 #include <string>
23 #include <uv.h>
24 #include <vector>
25 
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28 #include "nfc_sdk_common.h"
29 #include "infc_controller_callback.h"
30 
31 namespace OHOS {
32 namespace NFC {
33 namespace KITS {
34 class AsyncEventData {
35 public:
36     napi_env env;
37     napi_ref callbackRef;
38     std::function<napi_value ()> packResult;
39 
AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> v)40     explicit AsyncEventData(napi_env e, napi_ref r, std::function<napi_value ()> v)
41     {
42         env = e;
43         callbackRef = r;
44         packResult = v;
45     }
46 
47     AsyncEventData() = delete;
48 
~AsyncEventData()49     virtual ~AsyncEventData() {
50     }
51 };
52 
53 class RegObj {
54 public:
RegObj()55     RegObj() : m_regEnv(0), m_regHanderRef(nullptr) {
56     }
57 
RegObj(const napi_env & env,const napi_ref & ref)58     explicit RegObj(const napi_env& env, const napi_ref& ref)
59     {
60         m_regEnv = env;
61         m_regHanderRef = ref;
62     }
63 
~RegObj()64     ~RegObj() {
65     }
66 
67     napi_env m_regEnv;
68     napi_ref m_regHanderRef;
69 };
70 
71 
72 class EventRegister {
73 public:
EventRegister()74     EventRegister() {
75     }
~EventRegister()76     ~EventRegister() {
77     }
78 
79     static EventRegister& GetInstance();
80 
81     void Register(const napi_env& env, const std::string& type, napi_value handler);
82     void Unregister(const napi_env& env, const std::string& type, napi_value handler);
83 
84 private:
85     ErrorCode RegisterNfcStateChangedEvents(const std::string& type);
86     ErrorCode UnRegisterNfcEvents(const std::string& type);
87     bool IsEventSupport(const std::string& type);
88     void DeleteRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs, napi_value& handler);
89     void DeleteAllRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs);
90 
91     static bool isEventRegistered;
92 };
93 
94 napi_value On(napi_env env, napi_callback_info cbinfo);
95 napi_value Off(napi_env env, napi_callback_info cbinfo);
96 }  // namespace KITS
97 }  // namespace NFC
98 }  // namespace OHOS
99 
100 #endif
101