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 #include "aync_callback_manager.h"
17 #include "event_logger.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
21 namespace {
22 DEFINE_EH_HILOG_LABEL("EventsEmitter");
23 }
GetInstance()24 AsyncCallbackManager& AsyncCallbackManager::GetInstance()
25 {
26 static AsyncCallbackManager instance;
27 return instance;
28 }
29
DeleteCallbackInfoByEventId(const InnerEvent::EventId & eventIdValue)30 void AsyncCallbackManager::DeleteCallbackInfoByEventId(const InnerEvent::EventId &eventIdValue)
31 {
32 aniAsyncCallbackManager_.AniDeleteCallbackInfoByEventId(eventIdValue);
33 napiAsyncCallbackManager_.NapiDeleteCallbackInfoByEventId(eventIdValue);
34 }
35
GetListenerCountByEventId(const InnerEvent::EventId & eventId)36 uint32_t AsyncCallbackManager::GetListenerCountByEventId(const InnerEvent::EventId &eventId)
37 {
38 uint32_t cnt = 0u;
39 cnt += aniAsyncCallbackManager_.AniGetListenerCountByEventId(eventId);
40 cnt += napiAsyncCallbackManager_.NapiGetListenerCountByEventId(eventId);
41 return cnt;
42 }
43
IsExistValidCallback(const InnerEvent::EventId & eventId)44 bool AsyncCallbackManager::IsExistValidCallback(const InnerEvent::EventId &eventId)
45 {
46 auto ret = napiAsyncCallbackManager_.NapiIsExistValidCallback(eventId) ||
47 aniAsyncCallbackManager_.AniIsExistValidCallback(eventId);
48 if (!ret) {
49 if (eventId.index() == OHOS::AppExecFwk::TYPE_U32_INDEX) {
50 HILOGE("Event id: %{public}u has no callback", std::get<uint32_t>(eventId));
51 } else {
52 HILOGE("Event id: %{public}s has no callback", std::get<std::string>(eventId).c_str());
53 }
54 }
55 return ret;
56 }
57
InsertCallbackInfo(ani_env * env,InnerEvent::EventId eventId,bool once,ani_ref callback,ani_string dataType)58 void AsyncCallbackManager::InsertCallbackInfo(
59 ani_env *env, InnerEvent::EventId eventId, bool once, ani_ref callback, ani_string dataType)
60 {
61 aniAsyncCallbackManager_.AniInsertCallbackInfo(env, eventId, once, callback, dataType);
62 }
63
DeleteCallbackInfo(napi_env env,const InnerEvent::EventId & eventIdValue,napi_value argv)64 void AsyncCallbackManager::DeleteCallbackInfo(
65 napi_env env, const InnerEvent::EventId &eventIdValue, napi_value argv)
66 {
67 napiAsyncCallbackManager_.NapiDeleteCallbackInfo(env, eventIdValue, argv);
68 }
69
DeleteCallbackInfo(ani_env * env,const InnerEvent::EventId & eventIdValue,ani_ref callback)70 void AsyncCallbackManager::DeleteCallbackInfo(
71 ani_env *env, const InnerEvent::EventId &eventIdValue, ani_ref callback)
72 {
73 aniAsyncCallbackManager_.AniDeleteCallbackInfo(env, eventIdValue, callback);
74 }
75
DoCallback(const InnerEvent::Pointer & event)76 void AsyncCallbackManager::DoCallback(const InnerEvent::Pointer& event)
77 {
78 napiAsyncCallbackManager_.NapiDoCallback(event);
79 aniAsyncCallbackManager_.AniDoCallback(event);
80 }
81
IsCrossRuntime(const InnerEvent::EventId & eventId,EnvType envType)82 bool AsyncCallbackManager::IsCrossRuntime(const InnerEvent::EventId &eventId, EnvType envType)
83 {
84 if (envType == EnvType::NAPI) {
85 return aniAsyncCallbackManager_.AniIsExistValidCallback(eventId);
86 }
87
88 if (envType == EnvType::ANI) {
89 return napiAsyncCallbackManager_.NapiIsExistValidCallback(eventId);
90 }
91 return false;
92 }
93 }
94 }