• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ASYNC_CALLBACK_MANAGER_H
16 #define BASE_EVENTHANDLER_FRAMEWORKS_ASYNC_CALLBACK_MANAGER_H
17 
18 #include "napi_async_callback_manager.h"
19 #include "ani_async_callback_manager.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 
24 class AsyncCallbackManager {
25 public:
26     DISALLOW_COPY_AND_MOVE(AsyncCallbackManager);
27     static AsyncCallbackManager& GetInstance();
28 
29     /**
30      * Delete all callback info of given event id.
31      *
32      * @param eventIdValue event id.
33      */
34     void DeleteCallbackInfoByEventId(const InnerEvent::EventId &eventIdValue);
35 
36     /**
37      * Get all callback info counts of given event id.
38      *
39      * @param eventId event id.
40      * @return Counts of callback info.
41      */
42     uint32_t GetListenerCountByEventId(const InnerEvent::EventId &eventId);
43 
44     /**
45      * Find whether exists valid callback.
46      *
47      * @param eventId event id.
48      * @return Returns true if exists valid callback.
49      */
50     bool IsExistValidCallback(const InnerEvent::EventId &eventId);
51 
52     /**
53      * Insert callback.
54      *
55      * @param env A pointer to the environment structure.
56      * @param eventId Event id.
57      * @param once Whether subscribe once. if true, subscribe once.
58      * @param callback Event's callback.
59      * @param dataType Data type of callback's parameter.
60      */
61     void InsertCallbackInfo(
62         ani_env *env, InnerEvent::EventId eventId, bool once, ani_ref callback, ani_string dataType);
63 
64     /**
65      * Delete callback of given event id and callback object.
66      *
67      * @param env A pointer to the environment structure.
68      * @param eventIdValue Event id.
69      * @param argv Event's callback.
70      */
71     void DeleteCallbackInfo(napi_env env, const InnerEvent::EventId &eventIdValue, napi_value argv);
72 
73     /**
74      * Delete callback of given event id and callback object.
75      *
76      * @param env A pointer to the environment structure.
77      * @param eventIdValue Event id.
78      * @param callback Event's callback.
79      */
80     void DeleteCallbackInfo(ani_env *env, const InnerEvent::EventId &eventIdValue, ani_ref callback);
81 
82     /**
83      * Execute callback.
84      *
85      * @param event Event Emitted by user.
86      */
87     void DoCallback(const InnerEvent::Pointer& event);
88 
89     /**
90      * Whether needs to cross runtime.
91      *
92      * @param eventId event id.
93      * @param envType The type of runtime environment.
94      * @return Returns true if needs to cross runtime.
95      */
96     bool IsCrossRuntime(const InnerEvent::EventId &eventId, EnvType envType);
97 private:
98     AsyncCallbackManager() = default;
99 
100 private:
101     AniAsyncCallbackManager aniAsyncCallbackManager_;
102     NapiAsyncCallbackManager napiAsyncCallbackManager_;
103 };
104 
105 } // namespace AppExecFwk
106 } // namespace OHOS
107 
108 #endif // BASE_EVENTHANDLER_FRAMEWORKS_ASYNC_CALLBACK_MANAGER_H