1 /* 2 * Copyright (c) 2025 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 #ifndef BASE_EVENTHANDLER_FRAMEWORKS_NAPI_ASYNC_CALLBACK_MANAGER_H 16 #define BASE_EVENTHANDLER_FRAMEWORKS_NAPI_ASYNC_CALLBACK_MANAGER_H 17 18 #include <map> 19 #include <mutex> 20 #include <memory> 21 #include <unordered_set> 22 23 #include "nocopyable.h" 24 #include "inner_event.h" 25 #include "napi/native_api.h" 26 #include "js_native_api_types.h" 27 #include "napi/native_node_api.h" 28 #include "serialize.h" 29 #include "interops.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 34 struct NapiEventDataWorker { 35 std::shared_ptr<SerializeData> serializeData; 36 std::shared_ptr<AsyncCallbackInfo> callbackInfo; 37 int type; 38 }; 39 40 class NapiAsyncCallbackManager { 41 using AsyncCallbackInfoContainer = 42 std::map<InnerEvent::EventId, std::unordered_set<std::shared_ptr<AsyncCallbackInfo>>>; 43 public: 44 NapiAsyncCallbackManager( 45 std::mutex& containerMutex = GetAsyncCallbackInfoContainerMutex(), 46 AsyncCallbackInfoContainer& callbackInfoContainer = GetAsyncCallbackInfoContainer() napiAsyncCallbackContainerMutex_(containerMutex)47 ) : napiAsyncCallbackContainerMutex_(containerMutex), napiAsyncCallbackContainer_(callbackInfoContainer) {} 48 49 /** 50 * Delete all callback info of given event id. 51 * 52 * @param eventIdValue event id. 53 */ 54 void NapiDeleteCallbackInfoByEventId(const InnerEvent::EventId &eventIdValue); 55 56 /** 57 * Get all callback info counts of given event id. 58 * 59 * @param eventId event id. 60 * @return Counts of callback info. 61 */ 62 uint32_t NapiGetListenerCountByEventId(const InnerEvent::EventId &eventId); 63 64 /** 65 * Find whether exists valid callback. 66 * 67 * @param eventId event id. 68 * @return Returns true if exists valid callback. 69 */ 70 bool NapiIsExistValidCallback(const InnerEvent::EventId &eventId); 71 72 /** 73 * Delete callback of given event id and callback object. 74 * 75 * @param env A pointer to the environment structure. 76 * @param eventIdValue Event id. 77 * @param argv Event's callback. 78 */ 79 void NapiDeleteCallbackInfo(napi_env env, const InnerEvent::EventId &eventIdValue, napi_value argv); 80 81 /** 82 * Execute callback. 83 * 84 * @param event Event Emitted by user. 85 */ 86 void NapiDoCallback(const InnerEvent::Pointer& event); 87 88 private: 89 std::unordered_set<std::shared_ptr<AsyncCallbackInfo>> NapiGetAsyncCallbackInfo( 90 const InnerEvent::EventId &eventId); 91 private: 92 std::mutex& napiAsyncCallbackContainerMutex_; 93 AsyncCallbackInfoContainer& napiAsyncCallbackContainer_; 94 }; 95 96 } // namespace AppExecFwk 97 } // namespace OHOS 98 99 #endif // BASE_EVENTHANDLER_FRAMEWORKS_NAPI_ASYNC_CALLBACK_MANAGER_H