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 16 #ifndef BASE_EVENTHANDLER_FRAMEWORKS_ANI_ASYNC_CALLBACK_MANAGER_H 17 #define BASE_EVENTHANDLER_FRAMEWORKS_ANI_ASYNC_CALLBACK_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include <memory> 22 #include <unordered_set> 23 24 #include "inner_event.h" 25 #include "ani.h" 26 #include "serialize.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 31 struct AniAsyncCallbackInfo { 32 ani_vm* vm = nullptr; 33 std::string dataType; 34 ani_ref callback = 0; 35 std::atomic<bool> once = false; 36 std::atomic<bool> isDeleted = false; 37 InnerEvent::EventId eventId; 38 39 ~AniAsyncCallbackInfo(); 40 void ProcessEvent([[maybe_unused]] const InnerEvent::Pointer& event); 41 static void ThreadFunction( 42 ani_vm* vm, ani_ref callback, std::string dataType, std::shared_ptr<SerializeData> serializeData); 43 static ani_status GetCallbackArgs( 44 ani_env *env, std::string& dataType, std::vector<ani_ref>& args, std::shared_ptr<SerializeData> serializeData); 45 }; 46 47 class AniAsyncCallbackManager { 48 public: 49 /** 50 * Delete all callback info of given event id. 51 * 52 * @param eventIdValue event id. 53 */ 54 void AniDeleteCallbackInfoByEventId(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 AniGetListenerCountByEventId(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 AniIsExistValidCallback(const InnerEvent::EventId &eventId); 71 72 /** 73 * Insert callback. 74 * 75 * @param env A pointer to the environment structure. 76 * @param eventId Event id. 77 * @param once Whether subscribe once. if true, subscribe once. 78 * @param callback Event's callback. 79 * @param dataType Data type of callback's parameter. 80 */ 81 void AniInsertCallbackInfo( 82 ani_env *env, InnerEvent::EventId eventId, bool once, ani_ref callback, ani_string dataType); 83 84 /** 85 * Delete callback of given event id and callback object. 86 * 87 * @param env A pointer to the environment structure. 88 * @param eventIdValue Event id. 89 * @param callback Event's callback. 90 */ 91 void AniDeleteCallbackInfo(ani_env *env, const InnerEvent::EventId &eventIdValue, ani_ref callback); 92 93 /** 94 * Execute callback. 95 * 96 * @param event Event Emitted by user. 97 */ 98 void AniDoCallback(const InnerEvent::Pointer& event); 99 100 private: 101 std::unordered_set<std::shared_ptr<AniAsyncCallbackInfo>> AniGetAsyncCallbackInfo( 102 const InnerEvent::EventId &eventId); 103 static void AniReleaseCallbackInfo(AniAsyncCallbackInfo* callbackInfo); 104 std::string AniGetStdString(ani_env *env, ani_string str); 105 106 private: 107 std::mutex aniAsyncCallbackContainerMutex_; 108 std::map<InnerEvent::EventId, std::unordered_set<std::shared_ptr<AniAsyncCallbackInfo>>> 109 aniAsyncCallbackContainer_; 110 }; 111 } // namespace AppExecFwk 112 } // namespace OHOS 113 #endif // BASE_EVENTHANDLER_FRAMEWORKS_ANI_ASYNC_CALLBACK_MANAGER_H