OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // tasks. | 392 // tasks. |
393 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } | 393 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } |
394 | 394 |
395 // Runs the specified PendingTask. | 395 // Runs the specified PendingTask. |
396 void RunTask(const PendingTask& pending_task); | 396 void RunTask(const PendingTask& pending_task); |
397 | 397 |
398 //---------------------------------------------------------------------------- | 398 //---------------------------------------------------------------------------- |
399 protected: | 399 protected: |
400 scoped_ptr<MessagePump> pump_; | 400 scoped_ptr<MessagePump> pump_; |
401 | 401 |
| 402 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; |
| 403 |
| 404 // Common protected constructor. Other constructors delegate the |
| 405 // initialization to this constructor. |
| 406 // A subclass can invoke this constructor to create a message_loop of a |
| 407 // specific type with a custom loop. The implementation does not call |
| 408 // BindToCurrentThread. If this constructor is invoked directly by a subclass, |
| 409 // then the subclass must subsequently bind the message loop. |
| 410 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
| 411 |
| 412 // Configure various members and bind this message loop to the current thread. |
| 413 void BindToCurrentThread(); |
| 414 |
402 private: | 415 private: |
403 friend class RunLoop; | 416 friend class RunLoop; |
404 friend class internal::IncomingTaskQueue; | 417 friend class internal::IncomingTaskQueue; |
405 friend class ScheduleWorkTest; | 418 friend class ScheduleWorkTest; |
406 friend class Thread; | 419 friend class Thread; |
407 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); | 420 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); |
408 | 421 |
409 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; | |
410 | |
411 // Creates a MessageLoop without binding to a thread. | 422 // Creates a MessageLoop without binding to a thread. |
412 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given | 423 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given |
413 // to create a message pump for this message loop. Otherwise a default | 424 // to create a message pump for this message loop. Otherwise a default |
414 // message pump for the |type| is created. | 425 // message pump for the |type| is created. |
415 // | 426 // |
416 // It is valid to call this to create a new message loop on one thread, | 427 // It is valid to call this to create a new message loop on one thread, |
417 // and then pass it to the thread where the message loop actually runs. | 428 // and then pass it to the thread where the message loop actually runs. |
418 // The message loop's BindToCurrentThread() method must be called on the | 429 // The message loop's BindToCurrentThread() method must be called on the |
419 // thread the message loop runs on, before calling Run(). | 430 // thread the message loop runs on, before calling Run(). |
420 // Before BindToCurrentThread() is called, only Post*Task() functions can | 431 // Before BindToCurrentThread() is called, only Post*Task() functions can |
421 // be called on the message loop. | 432 // be called on the message loop. |
422 static scoped_ptr<MessageLoop> CreateUnbound( | 433 static scoped_ptr<MessageLoop> CreateUnbound( |
423 Type type, | 434 Type type, |
424 MessagePumpFactoryCallback pump_factory); | 435 MessagePumpFactoryCallback pump_factory); |
425 | 436 |
426 // Common private constructor. Other constructors delegate the initialization | |
427 // to this constructor. | |
428 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); | |
429 | |
430 // Configure various members and bind this message loop to the current thread. | |
431 void BindToCurrentThread(); | |
432 | |
433 // Sets the ThreadTaskRunnerHandle for the current thread to point to the | 437 // Sets the ThreadTaskRunnerHandle for the current thread to point to the |
434 // task runner for this message loop. | 438 // task runner for this message loop. |
435 void SetThreadTaskRunnerHandle(); | 439 void SetThreadTaskRunnerHandle(); |
436 | 440 |
437 // Invokes the actual run loop using the message pump. | 441 // Invokes the actual run loop using the message pump. |
438 void RunHandler(); | 442 void RunHandler(); |
439 | 443 |
440 // Called to process any delayed non-nestable tasks. | 444 // Called to process any delayed non-nestable tasks. |
441 bool ProcessNextDelayedNonNestableTask(); | 445 bool ProcessNextDelayedNonNestableTask(); |
442 | 446 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 // MessageLoop instantiated with TYPE_UI. | 560 // MessageLoop instantiated with TYPE_UI. |
557 // | 561 // |
558 // This class is typically used like so: | 562 // This class is typically used like so: |
559 // MessageLoopForUI::current()->...call some method... | 563 // MessageLoopForUI::current()->...call some method... |
560 // | 564 // |
561 class BASE_EXPORT MessageLoopForUI : public MessageLoop { | 565 class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
562 public: | 566 public: |
563 MessageLoopForUI() : MessageLoop(TYPE_UI) { | 567 MessageLoopForUI() : MessageLoop(TYPE_UI) { |
564 } | 568 } |
565 | 569 |
| 570 explicit MessageLoopForUI(scoped_ptr<MessagePump> pump); |
| 571 |
566 // Returns the MessageLoopForUI of the current thread. | 572 // Returns the MessageLoopForUI of the current thread. |
567 static MessageLoopForUI* current() { | 573 static MessageLoopForUI* current() { |
568 MessageLoop* loop = MessageLoop::current(); | 574 MessageLoop* loop = MessageLoop::current(); |
569 DCHECK(loop); | 575 DCHECK(loop); |
570 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); | 576 DCHECK(loop->IsType(MessageLoop::TYPE_UI)); |
571 return static_cast<MessageLoopForUI*>(loop); | 577 return static_cast<MessageLoopForUI*>(loop); |
572 } | 578 } |
573 | 579 |
574 static bool IsCurrent() { | 580 static bool IsCurrent() { |
575 MessageLoop* loop = MessageLoop::current(); | 581 MessageLoop* loop = MessageLoop::current(); |
576 return loop && loop->type() == MessageLoop::TYPE_UI; | 582 return loop && loop->IsType(MessageLoop::TYPE_UI); |
577 } | 583 } |
578 | 584 |
579 #if defined(OS_IOS) | 585 #if defined(OS_IOS) |
580 // On iOS, the main message loop cannot be Run(). Instead call Attach(), | 586 // On iOS, the main message loop cannot be Run(). Instead call Attach(), |
581 // which connects this MessageLoop to the UI thread's CFRunLoop and allows | 587 // which connects this MessageLoop to the UI thread's CFRunLoop and allows |
582 // PostTask() to work. | 588 // PostTask() to work. |
583 void Attach(); | 589 void Attach(); |
584 #endif | 590 #endif |
585 | 591 |
586 #if defined(OS_ANDROID) | 592 #if defined(OS_ANDROID) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 | 690 |
685 // Do not add any member variables to MessageLoopForIO! This is important b/c | 691 // Do not add any member variables to MessageLoopForIO! This is important b/c |
686 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 692 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
687 // data that you need should be stored on the MessageLoop's pump_ instance. | 693 // data that you need should be stored on the MessageLoop's pump_ instance. |
688 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 694 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
689 "MessageLoopForIO should not have extra member variables"); | 695 "MessageLoopForIO should not have extra member variables"); |
690 | 696 |
691 } // namespace base | 697 } // namespace base |
692 | 698 |
693 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 699 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |