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