• 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_INTEROPS_H
16 #define BASE_EVENTHANDLER_FRAMEWORKS_INTEROPS_H
17 
18 #include <map>
19 #include <memory>
20 #include <atomic>
21 #include <unordered_set>
22 
23 #include "inner_event.h"
24 #include "event_handler.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 
31 struct AsyncCallbackInfo {
32     std::atomic<napi_env> env;
33     std::atomic<bool> once = false;
34     std::atomic<bool> isDeleted = false;
35     napi_ref callback = 0;
36     napi_threadsafe_function tsfn = nullptr;
37     InnerEvent::EventId eventId;
38     ~AsyncCallbackInfo();
39 };
40 
41 class EventHandlerInstance : public EventHandler {
42 public:
43     EventHandlerInstance(const std::shared_ptr<EventRunner>& runner);
44     static std::shared_ptr<EventHandlerInstance> GetInstance();
45     ~EventHandlerInstance();
46     void ProcessEvent(const InnerEvent::Pointer& event) override;
47     std::unordered_set<std::shared_ptr<AsyncCallbackInfo>> GetAsyncCallbackInfo(const InnerEvent::EventId &eventId);
48     napi_env deleteEnv = nullptr;
49 };
50 
51 using AsyncCallbackInfoContainer =
52     std::map<InnerEvent::EventId, std::unordered_set<std::shared_ptr<AsyncCallbackInfo>>>;
53 AsyncCallbackInfoContainer& GetAsyncCallbackInfoContainer();
54 std::mutex& GetAsyncCallbackInfoContainerMutex();
55 
56 using EventData = std::shared_ptr<napi_value>;
57 struct AsyncCallbackInfo;
58 struct EventDataWorker {
59     EventData data{nullptr};
60     std::shared_ptr<AsyncCallbackInfo> callbackInfo{nullptr};
61     std::shared_ptr<void> serializePtr {nullptr};
62     bool isCrossRuntime{false};
63     void* enhancedData {nullptr};
64     bool IsEnhanced{false};
65 };
66 
67 struct EmitterEnhancedApi {
68     napi_value (*JS_Off)(napi_env env, napi_callback_info cbinfo) = nullptr;
69     napi_value (*JS_Emit)(napi_env env, napi_callback_info cbinfo) = nullptr;
70     napi_value (*JS_GetListenerCount)(napi_env env, napi_callback_info cbinfo) = nullptr;
71     void (*ProcessEvent)(const InnerEvent::Pointer& event) = nullptr;
72     void (*ProcessCallbackEnhanced)(const EventDataWorker* eventDataInner) = nullptr;
73 };
74 
75 class EmitterEnhancedApiRegister {
76 public:
77     EmitterEnhancedApiRegister();
78     void Register(const EmitterEnhancedApi& api);
79     bool IsInit() const;
80     std::shared_ptr<EmitterEnhancedApi> GetEnhancedApi() const;
81 
82 private:
83     std::shared_ptr<EmitterEnhancedApi> enhancedApi_;
84     std::atomic<bool> isInit_{false};
85 };
86 
87 EmitterEnhancedApiRegister& GetEmitterEnhancedApiRegister();
88 }
89 }
90 #endif  // BASE_EVENTHANDLER_FRAMEWORKS_INTEROPS_H