• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_LIBEVENTHANDLER_INCLUDE_EVENT_HANDLER_H
17 #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_LIBEVENTHANDLER_INCLUDE_EVENT_HANDLER_H
18 
19 #include "event_runner.h"
20 #include "dumper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 enum class EventType {
25     SYNC_EVENT = 0,
26     DELAY_EVENT = 1,
27     TIMING_EVENT = 2,
28 };
29 
30 template<typename T>
31 class ThreadLocalData;
32 
33 class EventHandler : public std::enable_shared_from_this<EventHandler> {
34 public:
35     using CallbackTimeout = std::function<void()>;
36     using Callback = InnerEvent::Callback;
37     using Priority = EventQueue::Priority;
38 
39     /**
40      * Constructor, set 'EventRunner' automatically.
41      *
42      * @param runner The 'EventRunner'.
43      */
44     explicit EventHandler(const std::shared_ptr<EventRunner> &runner = nullptr);
45     virtual ~EventHandler();
46     DISALLOW_COPY_AND_MOVE(EventHandler);
47 
48     /**
49      * Get event handler that running on current thread.
50      *
51      * @return Returns shared pointer of the current 'EventHandler'.
52      */
53     static std::shared_ptr<EventHandler> Current();
54 
55     /**
56      * Send an event.
57      *
58      * @param event Event which should be handled.
59      * @param delayTime Process the event after 'delayTime' milliseconds.
60      * @param priority Priority of the event queue for this event.
61      * @return Returns true if event has been sent successfully. If returns false, event should be released manually.
62      */
63     bool SendEvent(InnerEvent::Pointer &event, int64_t delayTime = 0, Priority priority = Priority::LOW);
64 
65     /**
66      * Send an event.
67      *
68      * @param event Event which should be handled.
69      * @param taskTime Process the event at taskTime.
70      * @param priority Priority of the event queue for this event.
71      * @return Returns true if event has been sent successfully. If returns false, event should be released manually.
72      */
73     bool SendTimingEvent(InnerEvent::Pointer &event, int64_t taskTime, Priority priority = Priority::LOW);
74 
75     /**
76      * Send an event.
77      *
78      * @param event Event which should be handled.
79      * @param priority Priority of the event queue for this event.
80      * @return Returns true if event has been sent successfully. If returns false, event should be released manually.
81      */
SendEvent(InnerEvent::Pointer & event,Priority priority)82     inline bool SendEvent(InnerEvent::Pointer &event, Priority priority)
83     {
84         return SendEvent(event, 0, priority);
85     }
86 
87     /**
88      * Send an event.
89      *
90      * @param event Event which should be handled.
91      * @param delayTime Process the event after 'delayTime' milliseconds.
92      * @param priority Priority of the event queue for this event.
93      * @return Returns true if event has been sent successfully.
94      */
95     inline bool SendEvent(InnerEvent::Pointer &&event, int64_t delayTime = 0, Priority priority = Priority::LOW)
96     {
97         return SendEvent(event, delayTime, priority);
98     }
99 
100     /**
101      * Send an event.
102      *
103      * @param innerEventId The id of the event.
104      * @param param Basic parameter of the event, default is 0.
105      * @param delayTime Process the event after 'delayTime' milliseconds.
106      * @return Returns true if event has been sent successfully.
107      */
SendEvent(uint32_t innerEventId,int64_t param,int64_t delayTime)108     inline bool SendEvent(uint32_t innerEventId, int64_t param, int64_t delayTime)
109     {
110         return SendEvent(InnerEvent::Get(innerEventId, param), delayTime);
111     }
112 
113     /**
114      * Send an event.
115      *
116      * @param innerEventId The id of the event.
117      * @param delayTime Process the event after 'delayTime' milliseconds.
118      * @param priority Priority of the event queue for this event.
119      * @return Returns true if event has been sent successfully.
120      */
121     inline bool SendEvent(uint32_t innerEventId, int64_t delayTime = 0, Priority priority = Priority::LOW)
122     {
123         return SendEvent(InnerEvent::Get(innerEventId, 0), delayTime, priority);
124     }
125 
126     /**
127      * Send an event.
128      *
129      * @param innerEventId The id of the event.
130      * @param priority Priority of the event queue for this event.
131      * @return Returns true if event has been sent successfully.
132      */
SendEvent(uint32_t innerEventId,Priority priority)133     inline bool SendEvent(uint32_t innerEventId, Priority priority)
134     {
135         return SendEvent(InnerEvent::Get(innerEventId, 0), 0, priority);
136     }
137 
138     /**
139      * Send an event.
140      *
141      * @param innerEventId The id of the event.
142      * @param object Shared pointer of object.
143      * @param delayTime Process the event after 'delayTime' milliseconds.
144      * @return Returns true if event has been sent successfully.
145      */
146     template<typename T>
147     inline bool SendEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, int64_t delayTime = 0)
148     {
149         return SendEvent(InnerEvent::Get(innerEventId, object), delayTime);
150     }
151 
152     /**
153      * Send an event.
154      *
155      * @param innerEventId The id of the event.
156      * @param object Weak pointer of object.
157      * @param delayTime Process the event after 'delayTime' milliseconds.
158      * @return Returns true if event has been sent successfully.
159      */
160     template<typename T>
161     inline bool SendEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, int64_t delayTime = 0)
162     {
163         return SendEvent(InnerEvent::Get(innerEventId, object), delayTime);
164     }
165 
166     /**
167      * Send an event.
168      *
169      * @param innerEventId The id of the event.
170      * @param object Unique pointer of object.
171      * @param delayTime Process the event after 'delayTime' milliseconds.
172      * @return Returns true if event has been sent successfully.
173      */
174     template<typename T, typename D>
175     inline bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t delayTime = 0)
176     {
177         return SendEvent(InnerEvent::Get(innerEventId, object), delayTime);
178     }
179 
180     /**
181      * Send an event.
182      *
183      * @param innerEventId The id of the event.
184      * @param object Unique pointer of object.
185      * @param delayTime Process the event after 'delayTime' milliseconds.
186      * @return Returns true if event has been sent successfully.
187      */
188     template<typename T, typename D>
189     inline bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, int64_t delayTime = 0)
190     {
191         return SendEvent(InnerEvent::Get(innerEventId, object), delayTime);
192     }
193 
194     /**
195      * Send an immediate event.
196      *
197      * @param event Event which should be handled.
198      * @return Returns true if event has been sent successfully.
199      */
SendImmediateEvent(InnerEvent::Pointer & event)200     inline bool SendImmediateEvent(InnerEvent::Pointer &event)
201     {
202         return SendEvent(event, 0, Priority::IMMEDIATE);
203     }
204 
205     /**
206      * Send an immediate event.
207      *
208      * @param event Event which should be handled.
209      * @return Returns true if event has been sent successfully.
210      */
SendImmediateEvent(InnerEvent::Pointer && event)211     inline bool SendImmediateEvent(InnerEvent::Pointer &&event)
212     {
213         return SendImmediateEvent(event);
214     }
215 
216     /**
217      * Send an immediate event.
218      *
219      * @param innerEventId The id of the event.
220      * @param param Basic parameter of the event, default is 0.
221      * @return Returns true if event has been sent successfully.
222      */
223     inline bool SendImmediateEvent(uint32_t innerEventId, int64_t param = 0)
224     {
225         return SendImmediateEvent(InnerEvent::Get(innerEventId, param));
226     }
227 
228     /**
229      * Send an immediate event.
230      *
231      * @param innerEventId The id of the event.
232      * @param object Shared pointer of object.
233      * @return Returns true if event has been sent successfully.
234      */
235     template<typename T>
SendImmediateEvent(uint32_t innerEventId,const std::shared_ptr<T> & object)236     inline bool SendImmediateEvent(uint32_t innerEventId, const std::shared_ptr<T> &object)
237     {
238         return SendImmediateEvent(InnerEvent::Get(innerEventId, object));
239     }
240 
241     /**
242      * Send an immediate event.
243      *
244      * @param innerEventId The id of the event.
245      * @param object Weak pointer of object.
246      * @return Returns true if event has been sent successfully.
247      */
248     template<typename T>
SendImmediateEvent(uint32_t innerEventId,const std::weak_ptr<T> & object)249     inline bool SendImmediateEvent(uint32_t innerEventId, const std::weak_ptr<T> &object)
250     {
251         return SendImmediateEvent(InnerEvent::Get(innerEventId, object));
252     }
253 
254     /**
255      * Send an immediate event.
256      *
257      * @param innerEventId The id of the event.
258      * @param object Unique pointer of object.
259      * @return Returns true if event has been sent successfully.
260      */
261     template<typename T, typename D>
SendImmediateEvent(uint32_t innerEventId,std::unique_ptr<T,D> & object)262     inline bool SendImmediateEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object)
263     {
264         return SendImmediateEvent(InnerEvent::Get(innerEventId, object));
265     }
266 
267     /**
268      * Send an immediate event.
269      *
270      * @param innerEventId The id of the event.
271      * @param object Unique pointer of object.
272      * @return Returns true if event has been sent successfully.
273      */
274     template<typename T, typename D>
SendImmediateEvent(uint32_t innerEventId,std::unique_ptr<T,D> && object)275     inline bool SendImmediateEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object)
276     {
277         return SendImmediateEvent(InnerEvent::Get(innerEventId, object));
278     }
279 
280     /**
281      * Send an high priority event.
282      *
283      * @param event Event which should be handled.
284      * @param delayTime Process the event after 'delayTime' milliseconds.
285      * @return Returns true if event has been sent successfully.
286      */
287     inline bool SendHighPriorityEvent(InnerEvent::Pointer &event, int64_t delayTime = 0)
288     {
289         return SendEvent(event, delayTime, Priority::HIGH);
290     }
291 
292     /**
293      * Send an high priority event.
294      *
295      * @param event Event which should be handled.
296      * @param delayTime Process the event after 'delayTime' milliseconds.
297      * @return Returns true if event has been sent successfully.
298      */
299     inline bool SendHighPriorityEvent(InnerEvent::Pointer &&event, int64_t delayTime = 0)
300     {
301         return SendHighPriorityEvent(event, delayTime);
302     }
303 
304     /**
305      * Send an high priority event.
306      *
307      * @param innerEventId The id of the event.
308      * @param param Basic parameter of the event, default is 0.
309      * @param delayTime Process the event after 'delayTime' milliseconds.
310      * @return Returns true if event has been sent successfully.
311      */
312     inline bool SendHighPriorityEvent(uint32_t innerEventId, int64_t param = 0, int64_t delayTime = 0)
313     {
314         return SendHighPriorityEvent(InnerEvent::Get(innerEventId, param), delayTime);
315     }
316 
317     /**
318      * Send an high priority event.
319      *
320      * @param innerEventId The id of the event.
321      * @param object Shared pointer of object.
322      * @param delayTime Process the event after 'delayTime' milliseconds.
323      * @return Returns true if event has been sent successfully.
324      */
325     template<typename T>
326     inline bool SendHighPriorityEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, int64_t delayTime = 0)
327     {
328         return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object), delayTime);
329     }
330 
331     /**
332      * Send an high priority event.
333      *
334      * @param innerEventId The id of the event.
335      * @param object Weak pointer of object.
336      * @param delayTime Process the event after 'delayTime' milliseconds.
337      * @return Returns true if event has been sent successfully.
338      */
339     template<typename T>
340     inline bool SendHighPriorityEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, int64_t delayTime = 0)
341     {
342         return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object), delayTime);
343     }
344 
345     /**
346      * Send an high priority event.
347      *
348      * @param innerEventId The id of the event.
349      * @param object Unique pointer of object.
350      * @param delayTime Process the event after 'delayTime' milliseconds.
351      * @return Returns true if event has been sent successfully.
352      */
353     template<typename T, typename D>
354     inline bool SendHighPriorityEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t delayTime = 0)
355     {
356         return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object), delayTime);
357     }
358 
359     /**
360      * Send an high priority event.
361      *
362      * @param innerEventId The id of the event.
363      * @param object Unique pointer of object.
364      * @param delayTime Process the event after 'delayTime' milliseconds.
365      * @return Returns true if event has been sent successfully.
366      */
367     template<typename T, typename D>
368     inline bool SendHighPriorityEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, int64_t delayTime = 0)
369     {
370         return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object), delayTime);
371     }
372 
373     /**
374      * Post a task.
375      *
376      * @param callback Task callback.
377      * @param name Name of the task.
378      * @param delayTime Process the event after 'delayTime' milliseconds.
379      * @param priority Priority of the event queue for this event.
380      * @return Returns true if task has been sent successfully.
381      */
382     inline bool PostTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0,
383         Priority priority = Priority::LOW)
384     {
385         return SendEvent(InnerEvent::Get(callback, name), delayTime, priority);
386     }
387 
388     /**
389      * Set delivery time out callback.
390      *
391      * @param callback Delivery Time out callback.
392      */
SetDeliveryTimeoutCallback(const Callback & callback)393     void SetDeliveryTimeoutCallback(const Callback &callback)
394     {
395         deliveryTimeoutCallback_ = callback;
396     }
397 
398     /**
399      * Set distribute time out callback.
400      *
401      * @param callback Distribute Time out callback.
402      */
SetDistributeTimeoutCallback(const Callback & callback)403     void SetDistributeTimeoutCallback(const Callback &callback)
404     {
405         distributeTimeoutCallback_ = callback;
406     }
407 
408     /**
409      * Post a task.
410      *
411      * @param callback Task callback.
412      * @param priority Priority of the event queue for this event.
413      * @return Returns true if task has been sent successfully.
414      */
PostTask(const Callback & callback,Priority priority)415     inline bool PostTask(const Callback &callback, Priority priority)
416     {
417         return PostTask(callback, std::string(), 0, priority);
418     }
419 
420     /**
421      * Post a task.
422      *
423      * @param callback Task callback.
424      * @param delayTime Process the event after 'delayTime' milliseconds.
425      * @param priority Priority of the event queue for this event.
426      * @return Returns true if task has been sent successfully.
427      */
428     inline bool PostTask(const Callback &callback, int64_t delayTime, Priority priority = Priority::LOW)
429     {
430         return PostTask(callback, std::string(), delayTime, priority);
431     }
432 
433     /**
434      * Post an immediate task.
435      *
436      * @param callback Task callback.
437      * @param name Remove events by name of the task.
438      * @return Returns true if task has been sent successfully.
439      */
440     inline bool PostImmediateTask(const Callback &callback, const std::string &name = std::string())
441     {
442         return SendEvent(InnerEvent::Get(callback, name), 0, Priority::IMMEDIATE);
443     }
444 
445     /**
446      * Post a high priority task.
447      *
448      * @param callback Task callback.
449      * @param name Name of the task.
450      * @param delayTime Process the event after 'delayTime' milliseconds.
451      * @return Returns true if task has been sent successfully.
452      */
453     inline bool PostHighPriorityTask(
454         const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0)
455     {
456         return PostTask(callback, name, delayTime, Priority::HIGH);
457     }
458 
459     /**
460      * Post a high priority task.
461      *
462      * @param callback Task callback.
463      * @param delayTime Process the event after 'delayTime' milliseconds.
464      * @return Returns true if task has been sent successfully.
465      */
PostHighPriorityTask(const Callback & callback,int64_t delayTime)466     inline bool PostHighPriorityTask(const Callback &callback, int64_t delayTime)
467     {
468         return PostHighPriorityTask(callback, std::string(), delayTime);
469     }
470 
471     /**
472      * Post a idle task.
473      *
474      * @param callback task callback.
475      * @param name Name of the task.
476      * @param delayTime Process the event after 'delayTime' milliseconds.
477      * @return Returns true if task has been sent successfully.
478      */
479     inline bool PostIdleTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0)
480     {
481         return PostTask(callback, name, delayTime, Priority::IDLE);
482     }
483 
484     /**
485      * Post a idle task.
486      *
487      * @param callback Task callback.
488      * @param delayTime Process the event after 'delayTime' milliseconds.
489      * @return Returns true if task has been sent successfully.
490      */
PostIdleTask(const Callback & callback,int64_t delayTime)491     inline bool PostIdleTask(const Callback &callback, int64_t delayTime)
492     {
493         return PostIdleTask(callback, std::string(), delayTime);
494     }
495 
496     /**
497      * Send an event, and wait until this event has been handled.
498      *
499      * @param event Event which should be handled.
500      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
501      * @return Returns true if event has been sent successfully. If returns false, event should be released manually.
502      */
503     bool SendSyncEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW);
504 
505     /**
506      * Send an event.
507      *
508      * @param event Event which should be handled.
509      * @param priority Priority of the event queue for this event.
510      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
511      * @return Returns true if event has been sent successfully.
512      */
513     inline bool SendSyncEvent(InnerEvent::Pointer &&event, Priority priority = Priority::LOW)
514     {
515         return SendSyncEvent(event, priority);
516     }
517 
518     /**
519      * Send an event, and wait until this event has been handled.
520      *
521      * @param innerEventId The id of the event.
522      * @param param Basic parameter of the event, default is 0.
523      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
524      * @return Returns true if event has been sent successfully.
525      */
526     inline bool SendSyncEvent(uint32_t innerEventId, int64_t param = 0, Priority priority = Priority::LOW)
527     {
528         return SendSyncEvent(InnerEvent::Get(innerEventId, param), priority);
529     }
530 
531     /**
532      * Send an event, and wait until this event has been handled.
533      *
534      * @param innerEventId The id of the event.
535      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
536      * @return Returns true if event has been sent successfully.
537      */
SendSyncEvent(uint32_t innerEventId,Priority priority)538     inline bool SendSyncEvent(uint32_t innerEventId, Priority priority)
539     {
540         return SendSyncEvent(InnerEvent::Get(innerEventId, 0), priority);
541     }
542 
543     /**
544      * Send an event, and wait until this event has been handled.
545      *
546      * @param innerEventId The id of the event.
547      * @param object Shared pointer of object.
548      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
549      * @return Returns true if event has been sent successfully.
550      */
551     template<typename T>
552     inline bool SendSyncEvent(
553         uint32_t innerEventId, const std::shared_ptr<T> &object, Priority priority = Priority::LOW)
554     {
555         return SendSyncEvent(InnerEvent::Get(innerEventId, object), priority);
556     }
557 
558     /**
559      * Send an event, and wait until this event has been handled.
560      *
561      * @param innerEventId The id of the event.
562      * @param object Weak pointer of object.
563      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
564      * @return Returns true if event has been sent successfully.
565      */
566     template<typename T>
567     inline bool SendSyncEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, Priority priority = Priority::LOW)
568     {
569         return SendSyncEvent(InnerEvent::Get(innerEventId, object), priority);
570     }
571 
572     /**
573      * Send an event, and wait until this event has been handled.
574      *
575      * @param innerEventId The id of the event.
576      * @param object Unique pointer of object.
577      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
578      * @return Returns true if event has been sent successfully.
579      */
580     template<typename T, typename D>
581     inline bool SendSyncEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, Priority priority = Priority::LOW)
582     {
583         return SendSyncEvent(InnerEvent::Get(innerEventId, object), priority);
584     }
585 
586     /**
587      * Send an event, and wait until this event has been handled.
588      *
589      * @param innerEventId The id of the event.
590      * @param object Unique pointer of object.
591      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
592      * @return Returns true if event has been sent successfully.
593      */
594     template<typename T, typename D>
595     inline bool SendSyncEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, Priority priority = Priority::LOW)
596     {
597         return SendSyncEvent(InnerEvent::Get(innerEventId, object), priority);
598     }
599 
600     /**
601      * Post a task, and wait until this task has been handled.
602      *
603      * @param callback Task callback.
604      * @param name Name of the task.
605      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
606      * @return Returns true if task has been sent successfully.
607      */
608     inline bool PostSyncTask(const Callback &callback, const std::string &name, Priority priority = Priority::LOW)
609     {
610         return SendSyncEvent(InnerEvent::Get(callback, name), priority);
611     }
612 
613     /**
614      * Post a task, and wait until this task has been handled.
615      *
616      * @param callback Task callback.
617      * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event.
618      * @return Returns true if task has been sent successfully.
619      */
620     inline bool PostSyncTask(const Callback &callback, Priority priority = Priority::LOW)
621     {
622         return PostSyncTask(callback, std::string(), priority);
623     }
624 
625     /**
626      * Send a timing event.
627      *
628      * @param event Event which should be handled.
629      * @param taskTime Process the event at taskTime.
630      * @param priority Priority of the event queue for this event.
631      * @return Returns true if event has been sent successfully.
632      */
SendTimingEvent(InnerEvent::Pointer && event,int64_t taskTime,Priority priority)633     inline bool SendTimingEvent(InnerEvent::Pointer &&event, int64_t taskTime, Priority priority)
634     {
635         return SendTimingEvent(event, taskTime, priority);
636     }
637 
638     /**
639      * Send a timing event.
640      *
641      * @param event Event which should be handled.
642      * @param taskTime Process the event at taskTime.
643      * @return Returns true if event has been sent successfully.
644      */
SendTimingEvent(InnerEvent::Pointer && event,int64_t taskTime)645     inline bool SendTimingEvent(InnerEvent::Pointer &&event, int64_t taskTime)
646     {
647         return SendTimingEvent(event, taskTime, Priority::LOW);
648     }
649 
650     /**
651      * Send a timing event.
652      *
653      * @param innerEventId The id of the event.
654      * @param taskTime Process the event at taskTime.
655      * @param param Basic parameter of the event.
656      * @return Returns true if event has been sent successfully.
657      */
SendTimingEvent(uint32_t innerEventId,int64_t taskTime,int64_t param)658     inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, int64_t param)
659     {
660         return SendTimingEvent(InnerEvent::Get(innerEventId, param), taskTime);
661     }
662 
663     /**
664      * Send a timing event.
665      *
666      * @param innerEventId The id of the event.
667      * @param taskTime Process the event at taskTime.
668      * @param priority Priority of the event queue for this event.
669      * @return Returns true if event has been sent successfully.
670      */
SendTimingEvent(uint32_t innerEventId,int64_t taskTime,Priority priority)671     inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, Priority priority)
672     {
673         return SendTimingEvent(InnerEvent::Get(innerEventId, 0), taskTime, priority);
674     }
675 
676     /**
677      * Send a timing event.
678      *
679      * @param innerEventId The id of the event.
680      * @param taskTime Process the event at taskTime.
681      * @param priority Priority of the event queue for this event.
682      * @return Returns true if event has been sent successfully.
683      */
SendTimingEvent(uint32_t innerEventId,int64_t taskTime)684     inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime)
685     {
686         return SendTimingEvent(InnerEvent::Get(innerEventId, 0), taskTime, Priority::LOW);
687     }
688 
689     /**
690      * Send a timing event.
691      *
692      * @param innerEventId The id of the event.
693      * @param object Shared pointer of object.
694      * @param taskTime Process the event at taskTime.
695      * @param priority Priority of the event queue for this event
696      * @return Returns true if event has been sent successfully.
697      */
698     template<typename T>
699     inline bool SendTimingEvent(
700         uint32_t innerEventId, const std::shared_ptr<T> &object, int64_t taskTime, Priority priority = Priority::LOW)
701     {
702         return SendTimingEvent(InnerEvent::Get(innerEventId, object), taskTime, priority);
703     }
704 
705     /**
706      * Send a timing event.
707      *
708      * @param innerEventId The id of the event.
709      * @param object Weak pointer of object.
710      * @param taskTime Process the event at taskTime.
711      * @param priority Priority of the event queue for this event
712      * @return Returns true if event has been sent successfully.
713      */
714     template<typename T>
715     inline bool SendTimingEvent(
716         uint32_t innerEventId, const std::weak_ptr<T> &object, int64_t taskTime, Priority priority = Priority::LOW)
717     {
718         return SendTimingEvent(InnerEvent::Get(innerEventId, object), taskTime, priority);
719     }
720 
721     /**
722      * Send a timing event.
723      *
724      * @param innerEventId The id of the event.
725      * @param object Unique pointer of object.
726      * @param taskTime Process the event at taskTime.
727      * @param priority Priority of the event queue for this event
728      * @return Returns true if event has been sent successfully.
729      */
730     template<typename T, typename D>
731     inline bool SendTimingEvent(
732         uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t taskTime, Priority priority = Priority::LOW)
733     {
734         return SendTimingEvent(InnerEvent::Get(innerEventId, object), taskTime, priority);
735     }
736 
737     /**
738      * Send a timing event.
739      *
740      * @param innerEventId The id of the event.
741      * @param object Unique pointer of object.
742      * @param taskTime Process the event at taskTime.
743      * @param priority Priority of the event queue for this event
744      * @return Returns true if event has been sent successfully.
745      */
746     template<typename T, typename D>
747     inline bool SendTimingEvent(
748         uint32_t innerEventId, std::unique_ptr<T, D> &&object, int64_t taskTime, Priority priority = Priority::LOW)
749     {
750         return SendTimingEvent(InnerEvent::Get(innerEventId, object), taskTime, priority);
751     }
752 
753     /**
754      * Post a timing task.
755      *
756      * @param callback Task callback.
757      * @param taskTime Process the event at taskTime.
758      * @param name Name of the task.
759      * @param priority Priority of the event queue for this event.
760      * @return Returns true if task has been sent successfully.
761      */
762     inline bool PostTimingTask(const Callback &callback, int64_t taskTime, const std::string &name = std::string(),
763         Priority priority = Priority::LOW)
764     {
765         return SendTimingEvent(InnerEvent::Get(callback, name), taskTime, priority);
766     }
767 
768     /**
769      * Post a timing task.
770      *
771      * @param callback Task callback.
772      * @param taskTime Process the event at taskTime.
773      * @param priority Priority of the event queue for this event.
774      * @return Returns true if task has been sent successfully.
775      */
776     inline bool PostTimingTask(const Callback &callback, int64_t taskTime, Priority priority = Priority::LOW)
777     {
778         return PostTimingTask(callback, taskTime, std::string(), priority);
779     }
780 
781     /**
782      * Remove all sent events.
783      */
784     void RemoveAllEvents();
785 
786     /**
787      * Remove sent events.
788      *
789      * @param innerEventId The id of the event.
790      */
791     void RemoveEvent(uint32_t innerEventId);
792 
793     /**
794      * Remove sent events.
795      *
796      * @param innerEventId The id of the event.
797      * @param param Basic parameter of the event.
798      */
799     void RemoveEvent(uint32_t innerEventId, int64_t param);
800 
801     /**
802      * Remove a task.
803      *
804      * @param name Name of the task.
805      */
806     void RemoveTask(const std::string &name);
807 
808     /**
809      * Add file descriptor listener for a file descriptor.
810      *
811      * @param fileDescriptor File descriptor.
812      * @param events Events from file descriptor, such as input, output, error
813      * @param listener Listener callback.
814      * @return Return 'ERR_OK' on success.
815      */
816     ErrCode AddFileDescriptorListener(
817         int32_t fileDescriptor, uint32_t events, const std::shared_ptr<FileDescriptorListener> &listener);
818 
819     /**
820      * Remove all file descriptor listeners.
821      */
822     void RemoveAllFileDescriptorListeners();
823 
824     /**
825      * Remove file descriptor listener for a file descriptor.
826      *
827      * @param fileDescriptor File descriptor.
828      */
829     void RemoveFileDescriptorListener(int32_t fileDescriptor);
830 
831     /**
832      * Set the 'EventRunner' to the 'EventHandler'.
833      *
834      * @param runner The 'EventRunner'.
835      */
836     void SetEventRunner(const std::shared_ptr<EventRunner> &runner);
837 
838     /**
839      * Get the 'EventRunner' of the 'EventHandler'.
840      *
841      * @return Return the 'EventRunner'.
842      */
GetEventRunner()843     inline const std::shared_ptr<EventRunner> &GetEventRunner() const
844     {
845         return eventRunner_;
846     }
847 
848     /**
849      * Distribute the event.
850      *
851      * @param event The event should be distributed.
852      */
853     void DistributeEvent(const InnerEvent::Pointer &event);
854 
855     /**
856      * Distribute time out action.
857      *
858      * @param event The event should be distribute.
859      * @param nowStart Dotting before distribution.
860      */
861     void DistributeTimeAction(const InnerEvent::Pointer &event, InnerEvent::TimePoint nowStart);
862 
863     /**
864      * Delivery time out action.
865      *
866      * @param event The event should be distribute.
867      * @param nowStart Dotting before distribution.
868      */
869     void DeliveryTimeAction(const InnerEvent::Pointer &event, InnerEvent::TimePoint nowStart);
870 
871     /**
872      * Print out the internal information about an object in the specified format,
873      * helping you diagnose internal errors of the object.
874      *
875      * @param dumpr The Dumper object you have implemented to process the output internal information.
876      */
877     void Dump(Dumper &dumper);
878 
879     /**
880      * Check whether an event with the given ID can be found among the events that have been sent but not processed.
881      *
882      * @param innerEventId The id of the event.
883      */
884     bool HasInnerEvent(uint32_t innerEventId);
885 
886     /**
887      * Check whether an event carrying the given param can be found among the events that have been sent but not
888      * processed.
889      *
890      * @param param Basic parameter of the event.
891      */
892     bool HasInnerEvent(int64_t param);
893 
894     /**
895      * Check whether an event carrying the given param can be found among the events that have been sent but not
896      * processed.
897      *
898      * @param event InnerEvent whose name is to be obtained.
899      * @return Returns the task name if the given event contains a specific task; returns the event ID otherwise.
900      */
901     std::string GetEventName(const InnerEvent::Pointer &event);
902 
903     /**
904      * Checks whether the current event handler is idle
905      * @return Returns true if current event handler is idle otherwise return false.
906      */
907     bool IsIdle();
908 
909 protected:
910     /**
911      * Process the event. Developers should override this method.
912      *
913      * @param event The event should be processed.
914      */
915     virtual void ProcessEvent(const InnerEvent::Pointer &event);
916 
917 private:
918     std::shared_ptr<EventRunner> eventRunner_;
919     CallbackTimeout deliveryTimeoutCallback_;
920     CallbackTimeout distributeTimeoutCallback_;
921 
922     static ThreadLocalData<std::weak_ptr<EventHandler>> currentEventHandler;
923 };
924 }  // namespace AppExecFwk
925 }  // namespace OHOS
926 
927 #endif  // #ifndef FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_LIBEVENTHANDLER_INCLUDE_EVENT_HANDLER_H
928