1 /*
2  * Copyright (c) 2021-2022 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_NAPI_INCLUDE_JS_EMITTER_H
17 #define BASE_EVENTHANDLER_FRAMEWORKS_NAPI_INCLUDE_JS_EMITTER_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "event_queue.h"
27 #include "inner_event.h"
28 #include <unordered_set>
29 #include "napi/native_api.h"
30 #include "napi/native_node_api.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 using Priority = EventQueue::Priority;
35 static const int32_t ARGC_NUM = 2;
36 static const int32_t NAPI_VALUE_STRING_LEN = 10240;
37 struct AsyncCallbackInfo;
38 class EventHandlerInstance : public EventHandler {
39 public:
40     EventHandlerInstance(const std::shared_ptr<EventRunner>& runner);
41     static std::shared_ptr<EventHandlerInstance> GetInstance();
42     ~EventHandlerInstance();
43     void ProcessEvent(const InnerEvent::Pointer& event) override;
44     std::unordered_set<std::shared_ptr<AsyncCallbackInfo>> GetAsyncCallbackInfo(const InnerEvent::EventId &eventId);
45     napi_env deleteEnv = nullptr;
46 };
47 
48 enum class DataType: uint32_t {
49     BOOL = 0,
50     INT,
51     STRING,
52 };
53 
54 struct Val {
55     DataType type;
56 
57     union {
58         bool bValue;
59         int32_t nValue;
60         char cValue[NAPI_VALUE_STRING_LEN] = {0};
61     } value;
62 };
63 
64 struct AsyncCallbackInfo {
65     std::atomic<napi_env> env;
66     std::atomic<bool> once = false;
67     std::atomic<bool> isDeleted = false;
68     napi_ref callback = 0;
69     napi_threadsafe_function tsfn = nullptr;
70     InnerEvent::EventId eventId;
71     ~AsyncCallbackInfo();
72 };
73 
74 using EventData = std::shared_ptr<napi_value>;
75 struct EventDataWorker {
76     EventData data;
77     std::shared_ptr<AsyncCallbackInfo> callbackInfo;
78 };
79 
80 napi_value EmitterInit(napi_env env, napi_value exports);
81 napi_value JS_On(napi_env env, napi_callback_info cbinfo);
82 napi_value JS_Off(napi_env env, napi_callback_info cbinfo);
83 napi_value JS_Once(napi_env env, napi_callback_info cbinfo);
84 napi_value JS_Emit(napi_env env, napi_callback_info cbinfo);
85 
GetNapiType(napi_env env,napi_value param)86 static inline napi_valuetype GetNapiType(napi_env env, napi_value param)
87 {
88     napi_valuetype type;
89     napi_typeof(env, param, &type);
90     return type;
91 }
92 } // namespace AppExecFwk
93 } // namespace OHOS
94 
95 #endif  // BASE_EVENTHANDLER_FRAMEWORKS_NAPI_INCLUDE_JS_EMITTER_H
96