• 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 
16 #ifndef BASE_EVENTHANDLER_FRAMEWORKS_ANI_EMITTER_H
17 #define BASE_EVENTHANDLER_FRAMEWORKS_ANI_EMITTER_H
18 
19 #include <memory>
20 #include "inner_event.h"
21 #include "event_queue.h"
22 #include "ani.h"
23 #include "ani_serialize.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 
28 using Priority = EventQueue::Priority;
29 using EventDataAni = std::shared_ptr<ani_object>;
30 
31 class EventsEmitter {
32 public:
33     /**
34      * Convert ani_string to std::string.
35      *
36      * @param env A pointer to the environment structure.
37      * @param str An ani_string to be converted.
38      * @return Returns a std::string of input str.
39      */
40     static std::string GetStdString(ani_env *env, ani_string str);
41 
42     /**
43      * Subscribe an event of given event id.
44      *
45      * @param env A pointer to the environment structure.
46      * @param eventId Event id.
47      * @param once Whether subscribe once. if true, subscribe once.
48      * @param callback Event's callback.
49      * @param dataType Data type of callback's parameter.
50      */
51     static void OnOrOnce(ani_env *env, InnerEvent::EventId eventId, bool once, ani_ref callback, ani_string dataType);
52 
53     /**
54      * Unsubscribe all of given event id.
55      *
56      * @param eventId Event id.
57      */
58     static void OffEmitterInstances(InnerEvent::EventId eventId);
59 
60     /**
61      * Get all listener counts of given event id.
62      *
63      * @param eventId Event id.
64      * @return Returns all listener counts of given event id.
65      */
66     static ani_double GetListenerCount(InnerEvent::EventId eventId);
67 
68     /**
69      * Emit an event of given event id.
70      *
71      * @param env A pointer to the environment structure.
72      * @param innerEvent An event structure including event id.
73      * @param eventData Data to be emitted.
74      */
75     static void EmitWithEventId(ani_env *env, ani_object innerEvent, ani_object eventData);
76 
77     /**
78      * Emit an event of given event id.
79      *
80      * @param env A pointer to the environment structure.
81      * @param eventId Event id.
82      * @param eventData Data to be emitted.
83      * @param enumItem Prority of event.
84      */
85     static void EmitWithEventIdString(ani_env *env, ani_string eventId, ani_object eventData, ani_enum_item enumItem);
86 
87 private:
88     static std::shared_ptr<SerializeData> GetSharedSerializeData(ani_env *env);
89 };
90 
91 } // namespace AppExecFwk
92 } // namespace OHOS
93 #endif // BASE_EVENTHANDLER_FRAMEWORKS_ANI_EMITTER_H