1 /* 2 * Copyright (C) 2024 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 NAPI_EVENT_SUBSCRIBE_MODULE_H 17 #define NAPI_EVENT_SUBSCRIBE_MODULE_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "napi_async_callback.h" 24 #include "napi_native_object.h" 25 #include "safe_map.h" 26 27 #ifndef BT_MODULE_NAME 28 #define BT_MODULE_NAME "default" 29 #endif 30 31 namespace OHOS { 32 namespace Bluetooth { 33 class NapiEventSubscribeModule { 34 public: 35 NapiEventSubscribeModule(const char *validEventName, const char *moduleName); 36 NapiEventSubscribeModule(std::vector<std::string> validEventNameVec, const char *moduleName); 37 ~NapiEventSubscribeModule() = default; 38 39 napi_status Register(napi_env env, napi_callback_info info); 40 napi_status Deregister(napi_env env, napi_callback_info info); 41 void PublishEvent(std::string eventName, const std::shared_ptr<NapiNativeObject> &nativeObject); 42 43 private: 44 bool IsValidEventName(const std::string &eventName) const; 45 bool IsNapiCallbackExist( 46 const std::vector<std::shared_ptr<NapiCallback>> &napiCallbackVec, napi_value &callback) const; 47 void CallFunction( 48 std::shared_ptr<NapiNativeObject> nativeObject, std::vector<std::shared_ptr<NapiCallback>> napiCallbackVec); 49 50 std::vector<std::string> validEventNameVec_ {}; 51 std::string moduleName_ = ""; 52 SafeMap<std::string, std::vector<std::shared_ptr<NapiCallback>>> eventSubscribeMap_ {}; 53 }; 54 55 } // namespace Bluetooth 56 } // namespace OHOS 57 #endif // NAPI_EVENT_SUBSCRIBE_MODULE_H 58