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 #include "wifi_logger.h" 28 29 DEFINE_WIFILOG_LABEL("WifiNapiEvent"); 30 31 namespace OHOS { 32 namespace Wifi { 33 class RegObj { 34 public: RegObj()35 RegObj() : m_regEnv(0), m_regHanderRef(nullptr) { 36 } RegObj(const napi_env & env,const napi_ref & ref)37 explicit RegObj(const napi_env& env, const napi_ref& ref) { 38 m_regEnv = env; 39 m_regHanderRef = ref; 40 } 41 ~RegObj()42 ~RegObj() { 43 } 44 45 bool operator == (const RegObj& other) const { 46 return m_regEnv == other.m_regEnv && m_regHanderRef == other.m_regHanderRef; 47 } 48 49 bool operator != (const RegObj& other) const { 50 return !(*this == other); 51 } 52 53 bool operator < (const RegObj& other) const { 54 return m_regEnv < other.m_regEnv || (m_regEnv == other.m_regEnv && m_regHanderRef < other.m_regHanderRef); 55 } 56 57 napi_env m_regEnv; 58 napi_ref m_regHanderRef; 59 }; 60 61 class AsyncEventData { 62 public: 63 napi_env env; 64 napi_ref callbackRef; 65 std::function<napi_value ()> packResult; 66 AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> p)67 explicit AsyncEventData(napi_env e, napi_ref r, std::function<napi_value ()> p) { 68 env = e; 69 callbackRef = r; 70 packResult = p; 71 } 72 73 AsyncEventData() = delete; 74 ~AsyncEventData()75 virtual ~AsyncEventData() { 76 } 77 }; 78 79 static std::shared_mutex g_regInfoMutex; 80 static std::map<std::string, std::vector<RegObj>> g_eventRegisterInfo; 81 82 class NapiEvent { 83 public: 84 napi_value CreateResult(const napi_env& env, int value); 85 napi_value CreateResult(const napi_env& env, const StationInfo& info); 86 napi_value CreateResult(const napi_env& env, napi_value placehoders); 87 napi_value CreateResult(const napi_env& env, const WifiP2pDevice& device); 88 napi_value CreateResult(const napi_env& env, const std::vector<WifiP2pDevice>& devices); 89 napi_value CreateResult(const napi_env& env, const WifiP2pLinkedInfo& info); 90 void EventNotify(AsyncEventData *asyncEvent); 91 92 template<typename T> CheckAndNotify(const std::string & type,const T & obj)93 void CheckAndNotify(const std::string& type, const T& obj) { 94 std::shared_lock<std::shared_mutex> guard(g_regInfoMutex); 95 auto it = g_eventRegisterInfo.find(type); 96 if (it == g_eventRegisterInfo.end()) { 97 WIFI_LOGW("not find register info."); 98 return; 99 } 100 for (auto& each : it->second) { 101 auto func = [this, env = each.m_regEnv, obj] () -> napi_value { return CreateResult(env, obj); }; 102 AsyncEventData *asyncEvent = new (std::nothrow)AsyncEventData(each.m_regEnv, each.m_regHanderRef, func); 103 if (asyncEvent == nullptr) { 104 return; 105 } 106 EventNotify(asyncEvent); 107 } 108 } 109 }; 110 111 class EventRegister { 112 public: EventRegister()113 EventRegister() { 114 } ~EventRegister()115 ~EventRegister() { 116 } 117 118 static EventRegister& GetInstance(); 119 120 void Register(const napi_env& env, const std::string& type, napi_value handler); 121 void Unregister(const napi_env& env, const std::string& type, napi_value handler); 122 123 private: 124 ErrCode RegisterWifiEvents(int32_t sysCap); 125 bool IsEventSupport(const std::string& type); 126 int CheckPermission(const std::string& eventType); 127 void DeleteRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs, napi_value& handler); 128 void DeleteAllRegisterObj(const napi_env& env, std::vector<RegObj>& vecRegObjs); 129 }; 130 131 napi_value On(napi_env env, napi_callback_info cbinfo); 132 napi_value Off(napi_env env, napi_callback_info cbinfo); 133 } // namespace Wifi 134 } // namespace OHOS 135 136 #endif 137