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