• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 
16 #ifndef WIFI_NAPI_EVENT_H_
17 #define WIFI_NAPI_EVENT_H_
18 
19 #include <string>
20 #include <set>
21 #include <map>
22 #include "napi/native_api.h"
23 #include "wifi_errcode.h"
24 #include <shared_mutex>
25 #include "wifi_p2p.h"
26 #include "wifi_hotspot.h"
27 
28 namespace OHOS {
29 namespace Wifi {
30 class AsyncEventData {
31 public:
32     napi_env env;
33     napi_ref callbackRef;
34     std::function<napi_value ()> packResult;
35 
AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> p)36     explicit AsyncEventData(napi_env e, napi_ref r, std::function<napi_value ()> p) {
37         env = e;
38         callbackRef = r;
39         packResult = p;
40     }
41 
42     AsyncEventData() = delete;
43 
~AsyncEventData()44     virtual ~AsyncEventData() {
45     }
46 };
47 
48 class RegObj {
49 public:
RegObj()50     RegObj() : m_regEnv(0), m_regHanderRef(nullptr) {
51     }
RegObj(const napi_env & env,const napi_ref & ref)52     explicit RegObj(const napi_env& env, const napi_ref& ref) {
53         m_regEnv = env;
54         m_regHanderRef = ref;
55     }
56 
~RegObj()57     ~RegObj() {
58     }
59 
60     napi_env m_regEnv;
61     napi_ref m_regHanderRef;
62 };
63 
64 static std::shared_mutex g_regInfoMutex;
65 static std::map<std::string, std::vector<RegObj>> g_eventRegisterInfo;
66 
67 class NapiEvent {
68 public:
69     bool CheckIsRegister(const std::string& type);
70     napi_value CreateResult(const napi_env& env, int value);
71     napi_value CreateResult(const napi_env& env, const StationInfo& info);
72     napi_value CreateResult(const napi_env& env, napi_value placehoders);
73     napi_value CreateResult(const napi_env& env, const WifiP2pDevice& device);
74     napi_value CreateResult(const napi_env& env, const std::vector<WifiP2pDevice>& devices);
75     napi_value CreateResult(const napi_env& env, const WifiP2pLinkedInfo& info);
76     void EventNotify(AsyncEventData *asyncEvent);
77 
78     template<typename T>
CheckAndNotify(const std::string & type,const T & obj)79     void CheckAndNotify(const std::string& type, const T& obj) {
80         std::shared_lock<std::shared_mutex> guard(g_regInfoMutex);
81         if (!CheckIsRegister(type)) {
82             return;
83         }
84 
85         std::vector<RegObj>& vecObj = g_eventRegisterInfo[type];
86         for (auto& each : vecObj) {
87             auto func = [this, env = each.m_regEnv, obj] () -> napi_value { return CreateResult(env, obj); };
88             AsyncEventData *asyncEvent = new AsyncEventData(each.m_regEnv, each.m_regHanderRef, func);
89             if (asyncEvent == nullptr) {
90                 return;
91             }
92             EventNotify(asyncEvent);
93         }
94     }
95 };
96 
97 class EventRegister {
98 public:
EventRegister()99     EventRegister() {
100     }
~EventRegister()101     ~EventRegister() {
102     }
103 
104     static EventRegister& GetInstance();
105 
106     void Register(const napi_env& env, const std::string& type, napi_value handler);
107     void Unregister(const napi_env& env, const std::string& type, napi_value handler);
108 
109 private:
110     ErrCode RegisterWifiEvents();
111     bool IsEventSupport(const std::string& type);
112     void DeleteRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs, napi_value& handler);
113     void DeleteAllRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs);
114 
115     static bool isEventRegistered;
116 };
117 
118 napi_value On(napi_env env, napi_callback_info cbinfo);
119 napi_value Off(napi_env env, napi_callback_info cbinfo);
120 }  // namespace Wifi
121 }  // namespace OHOS
122 
123 #endif
124