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