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