1 /*
2 * Copyright (c) 2024 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 "emitter_ffi.h"
17 #include "emitter_log.h"
18 #include "emitter.h"
19 #include "cj_lambda.h"
20
21 using namespace OHOS::FFI;
22 using namespace OHOS::EventsEmitter;
23
24 namespace {
CreateCallback(CEventCallback & callbackInfo)25 std::shared_ptr<CallbackImpl> CreateCallback(CEventCallback &callbackInfo)
26 {
27 auto onChange = [lambda = CJLambda::Create(callbackInfo.callbackRef)](const CEventData data) -> void {
28 lambda(data);
29 };
30 auto callbackImpl = std::make_shared<CallbackImpl>(std::string(callbackInfo.name), onChange);
31 if (callbackImpl == nullptr) {
32 LOGE("Fail to create CallbackImpl.")
33 return nullptr;
34 }
35 return callbackImpl;
36 }
37 }
38
39 extern "C" {
CJ_OnWithId(uint32_t eventId,CEventCallback callbackInfo)40 int32_t CJ_OnWithId(uint32_t eventId, CEventCallback callbackInfo)
41 {
42 auto callback = CreateCallback(callbackInfo);
43 if (callback == nullptr) {
44 return MEMORY_ERROR;
45 }
46 return Emitter::On(eventId, callback);
47 }
48
CJ_OnWithStringId(char * eventId,CEventCallback callbackInfo)49 int32_t CJ_OnWithStringId(char* eventId, CEventCallback callbackInfo)
50 {
51 auto callback = CreateCallback(callbackInfo);
52 if (callback == nullptr) {
53 return MEMORY_ERROR;
54 }
55 return Emitter::On(eventId, callback);
56 }
57
CJ_OnceWithId(uint32_t eventId,CEventCallback callbackInfo)58 int32_t CJ_OnceWithId(uint32_t eventId, CEventCallback callbackInfo)
59 {
60 auto callback = CreateCallback(callbackInfo);
61 if (callback == nullptr) {
62 return MEMORY_ERROR;
63 }
64 return Emitter::Once(eventId, callback);
65 }
66
CJ_OnceWithStringId(char * eventId,CEventCallback callbackInfo)67 int32_t CJ_OnceWithStringId(char* eventId, CEventCallback callbackInfo)
68 {
69 auto callback = CreateCallback(callbackInfo);
70 if (callback == nullptr) {
71 return MEMORY_ERROR;
72 }
73 return Emitter::Once(eventId, callback);
74 }
75
CJ_OffWithId(uint32_t eventId)76 void CJ_OffWithId(uint32_t eventId)
77 {
78 Emitter::Off(eventId);
79 }
80
CJ_OffWithString(char * eventId)81 void CJ_OffWithString(char* eventId)
82 {
83 Emitter::Off(eventId);
84 }
85
CJ_OffWithIdCallback(uint32_t eventId,CEventCallback callbackInfo)86 int32_t CJ_OffWithIdCallback(uint32_t eventId, CEventCallback callbackInfo)
87 {
88 auto callback = CreateCallback(callbackInfo);
89 if (callback == nullptr) {
90 return MEMORY_ERROR;
91 }
92 Emitter::Off(eventId, callback);
93 return SUCCESS_CODE;
94 }
95
CJ_OffWithStringCallback(char * eventId,CEventCallback callbackInfo)96 int32_t CJ_OffWithStringCallback(char* eventId, CEventCallback callbackInfo)
97 {
98 auto callback = CreateCallback(callbackInfo);
99 if (callback == nullptr) {
100 return MEMORY_ERROR;
101 }
102 Emitter::Off(eventId, callback);
103 return SUCCESS_CODE;
104 }
105
CJ_EmitWithId(uint32_t eventId,uint32_t priority,CEventData data)106 void CJ_EmitWithId(uint32_t eventId, uint32_t priority, CEventData data)
107 {
108 Emitter::Emit(eventId, priority, data);
109 }
110
CJ_EmitWithString(char * eventId,uint32_t priority,CEventData data)111 void CJ_EmitWithString(char* eventId, uint32_t priority, CEventData data)
112 {
113 Emitter::Emit(eventId, priority, data);
114 }
115
CJ_GetListenerCountById(uint32_t eventId)116 uint32_t CJ_GetListenerCountById(uint32_t eventId)
117 {
118 auto ret = Emitter::GetListenerCount(eventId);
119 return ret;
120 }
121
CJ_GetListenerCountByString(char * eventId)122 uint32_t CJ_GetListenerCountByString(char* eventId)
123 {
124 auto ret = Emitter::GetListenerCount(std::string(eventId));
125 return ret;
126 }
127 }
128