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 "infc_controller_callback.h"
29 #include "nfc_sdk_common.h"
30 #include "nfc_sa_client.h"
31 #include "system_ability_status_change_stub.h"
32 
33 namespace OHOS {
34 namespace NFC {
35 namespace KITS {
36 class AsyncEventData {
37 public:
38     napi_env env;
39     napi_ref callbackRef;
40     std::function<napi_value ()> packResult;
41 
AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> v)42     explicit AsyncEventData(napi_env e, napi_ref r, std::function<napi_value ()> v)
43     {
44         env = e;
45         callbackRef = r;
46         packResult = v;
47     }
48 
49     AsyncEventData() = delete;
50 
~AsyncEventData()51     virtual ~AsyncEventData() {
52     }
53 };
54 
55 class RegObj {
56 public:
RegObj()57     RegObj() : m_regEnv(0), m_regHanderRef(nullptr) {
58     }
59 
RegObj(const napi_env & env,const napi_ref & ref)60     explicit RegObj(const napi_env& env, const napi_ref& ref)
61     {
62         m_regEnv = env;
63         m_regHanderRef = ref;
64     }
65 
~RegObj()66     ~RegObj() {
67     }
68 
69     napi_env m_regEnv;
70     napi_ref m_regHanderRef;
71 };
72 
73 class NfcNapiAbilityStatusChange : public SystemAbilityStatusChangeStub {
74 public:
75     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
76     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
77     void Init(int32_t systemAbilityId);
78 };
79 
80 class EventRegister {
81 public:
EventRegister()82     EventRegister()
83     {
84         mSaStatusListener_ = std::make_shared<NfcNapiAbilityStatusChange>();
85         mSaStatusListener_->Init(NFC_MANAGER_SYS_ABILITY_ID);
86     }
~EventRegister()87     ~EventRegister()
88     {
89         mSaStatusListener_ = nullptr;
90     }
91 
92     static EventRegister& GetInstance();
93 
94     void Register(const napi_env& env, const std::string& type, napi_value handler);
95     void Unregister(const napi_env& env, const std::string& type, napi_value handler);
96     ErrorCode RegisterNfcStateChangedEvents(const std::string& type);
97 
98 private:
99     ErrorCode UnRegisterNfcEvents(const std::string& type);
100     bool IsEventSupport(const std::string& type);
101     void DeleteRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs, napi_value& handler);
102     void DeleteAllRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs);
103 
104     static bool isEventRegistered;
105     std::shared_ptr<NfcNapiAbilityStatusChange> mSaStatusListener_;
106 };
107 
108 napi_value On(napi_env env, napi_callback_info cbinfo);
109 napi_value Off(napi_env env, napi_callback_info cbinfo);
110 }  // namespace KITS
111 }  // namespace NFC
112 }  // namespace OHOS
113 
114 #endif
115