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 <memory> |
8 #include <queue> | 9 #include <queue> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/base_export.h" | 12 #include "base/base_export.h" |
12 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
13 #include "base/debug/task_annotator.h" | 14 #include "base/debug/task_annotator.h" |
14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
15 #include "base/location.h" | 16 #include "base/location.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/message_loop/incoming_task_queue.h" | 19 #include "base/message_loop/incoming_task_queue.h" |
20 #include "base/message_loop/message_loop_task_runner.h" | 20 #include "base/message_loop/message_loop_task_runner.h" |
21 #include "base/message_loop/message_pump.h" | 21 #include "base/message_loop/message_pump.h" |
22 #include "base/message_loop/timer_slack.h" | 22 #include "base/message_loop/timer_slack.h" |
23 #include "base/observer_list.h" | 23 #include "base/observer_list.h" |
24 #include "base/pending_task.h" | 24 #include "base/pending_task.h" |
25 #include "base/sequenced_task_runner_helpers.h" | 25 #include "base/sequenced_task_runner_helpers.h" |
26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
28 #include "base/tracking_info.h" | 28 #include "base/tracking_info.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 #if defined(OS_ANDROID) | 108 #if defined(OS_ANDROID) |
109 TYPE_JAVA, | 109 TYPE_JAVA, |
110 #endif // defined(OS_ANDROID) | 110 #endif // defined(OS_ANDROID) |
111 }; | 111 }; |
112 | 112 |
113 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it | 113 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
114 // is typical to make use of the current thread's MessageLoop instance. | 114 // is typical to make use of the current thread's MessageLoop instance. |
115 explicit MessageLoop(Type type = TYPE_DEFAULT); | 115 explicit MessageLoop(Type type = TYPE_DEFAULT); |
116 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must | 116 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must |
117 // be non-NULL. | 117 // be non-NULL. |
118 explicit MessageLoop(scoped_ptr<MessagePump> pump); | 118 explicit MessageLoop(std::unique_ptr<MessagePump> pump); |
119 | 119 |
120 ~MessageLoop() override; | 120 ~MessageLoop() override; |
121 | 121 |
122 // Returns the MessageLoop object for the current thread, or null if none. | 122 // Returns the MessageLoop object for the current thread, or null if none. |
123 static MessageLoop* current(); | 123 static MessageLoop* current(); |
124 | 124 |
125 static void EnableHistogrammer(bool enable_histogrammer); | 125 static void EnableHistogrammer(bool enable_histogrammer); |
126 | 126 |
127 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); | 127 typedef std::unique_ptr<MessagePump>(MessagePumpFactory)(); |
128 // Uses the given base::MessagePumpForUIFactory to override the default | 128 // Uses the given base::MessagePumpForUIFactory to override the default |
129 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory | 129 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory |
130 // was successfully registered. | 130 // was successfully registered. |
131 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); | 131 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); |
132 | 132 |
133 // Creates the default MessagePump based on |type|. Caller owns return | 133 // Creates the default MessagePump based on |type|. Caller owns return |
134 // value. | 134 // value. |
135 static scoped_ptr<MessagePump> CreateMessagePumpForType(Type type); | 135 static std::unique_ptr<MessagePump> CreateMessagePumpForType(Type type); |
136 // A DestructionObserver is notified when the current MessageLoop is being | 136 // A DestructionObserver is notified when the current MessageLoop is being |
137 // destroyed. These observers are notified prior to MessageLoop::current() | 137 // destroyed. These observers are notified prior to MessageLoop::current() |
138 // being changed to return NULL. This gives interested parties the chance to | 138 // being changed to return NULL. This gives interested parties the chance to |
139 // do final cleanup that depends on the MessageLoop. | 139 // do final cleanup that depends on the MessageLoop. |
140 // | 140 // |
141 // NOTE: Any tasks posted to the MessageLoop during this notification will | 141 // NOTE: Any tasks posted to the MessageLoop during this notification will |
142 // not be run. Instead, they will be deleted. | 142 // not be run. Instead, they will be deleted. |
143 // | 143 // |
144 class BASE_EXPORT DestructionObserver { | 144 class BASE_EXPORT DestructionObserver { |
145 public: | 145 public: |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 390 |
391 // Returns the TaskAnnotator which is used to add debug information to posted | 391 // Returns the TaskAnnotator which is used to add debug information to posted |
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 std::unique_ptr<MessagePump> pump_; |
401 | 401 |
402 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; | 402 using MessagePumpFactoryCallback = Callback<std::unique_ptr<MessagePump>()>; |
403 | 403 |
404 // Common protected constructor. Other constructors delegate the | 404 // Common protected constructor. Other constructors delegate the |
405 // initialization to this constructor. | 405 // initialization to this constructor. |
406 // A subclass can invoke this constructor to create a message_loop of a | 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 | 407 // specific type with a custom loop. The implementation does not call |
408 // BindToCurrentThread. If this constructor is invoked directly by a subclass, | 408 // BindToCurrentThread. If this constructor is invoked directly by a subclass, |
409 // then the subclass must subsequently bind the message loop. | 409 // then the subclass must subsequently bind the message loop. |
410 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); | 410 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
411 | 411 |
412 // Configure various members and bind this message loop to the current thread. | 412 // Configure various members and bind this message loop to the current thread. |
(...skipping 10 matching lines...) Expand all Loading... |
423 // 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 |
424 // 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 |
425 // message pump for the |type| is created. | 425 // message pump for the |type| is created. |
426 // | 426 // |
427 // 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, |
428 // 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. |
429 // The message loop's BindToCurrentThread() method must be called on the | 429 // The message loop's BindToCurrentThread() method must be called on the |
430 // thread the message loop runs on, before calling Run(). | 430 // thread the message loop runs on, before calling Run(). |
431 // Before BindToCurrentThread() is called, only Post*Task() functions can | 431 // Before BindToCurrentThread() is called, only Post*Task() functions can |
432 // be called on the message loop. | 432 // be called on the message loop. |
433 static scoped_ptr<MessageLoop> CreateUnbound( | 433 static std::unique_ptr<MessageLoop> CreateUnbound( |
434 Type type, | 434 Type type, |
435 MessagePumpFactoryCallback pump_factory); | 435 MessagePumpFactoryCallback pump_factory); |
436 | 436 |
437 // Sets the ThreadTaskRunnerHandle for the current thread to point to the | 437 // Sets the ThreadTaskRunnerHandle for the current thread to point to the |
438 // task runner for this message loop. | 438 // task runner for this message loop. |
439 void SetThreadTaskRunnerHandle(); | 439 void SetThreadTaskRunnerHandle(); |
440 | 440 |
441 // Invokes the actual run loop using the message pump. | 441 // Invokes the actual run loop using the message pump. |
442 void RunHandler(); | 442 void RunHandler(); |
443 | 443 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 | 531 |
532 debug::TaskAnnotator task_annotator_; | 532 debug::TaskAnnotator task_annotator_; |
533 | 533 |
534 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; | 534 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
535 | 535 |
536 // A task runner which we haven't bound to a thread yet. | 536 // A task runner which we haven't bound to a thread yet. |
537 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; | 537 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
538 | 538 |
539 // The task runner associated with this message loop. | 539 // The task runner associated with this message loop. |
540 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 540 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
541 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 541 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
542 | 542 |
543 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | 543 template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
544 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | 544 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
545 | 545 |
546 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 546 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
547 void(*deleter)(const void*), | 547 void(*deleter)(const void*), |
548 const void* object); | 548 const void* object); |
549 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | 549 void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
550 void(*releaser)(const void*), | 550 void(*releaser)(const void*), |
551 const void* object); | 551 const void* object); |
552 | 552 |
553 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 553 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
554 }; | 554 }; |
555 | 555 |
556 #if !defined(OS_NACL) | 556 #if !defined(OS_NACL) |
557 | 557 |
558 //----------------------------------------------------------------------------- | 558 //----------------------------------------------------------------------------- |
559 // MessageLoopForUI extends MessageLoop with methods that are particular to a | 559 // MessageLoopForUI extends MessageLoop with methods that are particular to a |
560 // MessageLoop instantiated with TYPE_UI. | 560 // MessageLoop instantiated with TYPE_UI. |
561 // | 561 // |
562 // This class is typically used like so: | 562 // This class is typically used like so: |
563 // MessageLoopForUI::current()->...call some method... | 563 // MessageLoopForUI::current()->...call some method... |
564 // | 564 // |
565 class BASE_EXPORT MessageLoopForUI : public MessageLoop { | 565 class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
566 public: | 566 public: |
567 MessageLoopForUI() : MessageLoop(TYPE_UI) { | 567 MessageLoopForUI() : MessageLoop(TYPE_UI) { |
568 } | 568 } |
569 | 569 |
570 explicit MessageLoopForUI(scoped_ptr<MessagePump> pump); | 570 explicit MessageLoopForUI(std::unique_ptr<MessagePump> pump); |
571 | 571 |
572 // Returns the MessageLoopForUI of the current thread. | 572 // Returns the MessageLoopForUI of the current thread. |
573 static MessageLoopForUI* current() { | 573 static MessageLoopForUI* current() { |
574 MessageLoop* loop = MessageLoop::current(); | 574 MessageLoop* loop = MessageLoop::current(); |
575 DCHECK(loop); | 575 DCHECK(loop); |
576 DCHECK(loop->IsType(MessageLoop::TYPE_UI)); | 576 DCHECK(loop->IsType(MessageLoop::TYPE_UI)); |
577 return static_cast<MessageLoopForUI*>(loop); | 577 return static_cast<MessageLoopForUI*>(loop); |
578 } | 578 } |
579 | 579 |
580 static bool IsCurrent() { | 580 static bool IsCurrent() { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 | 691 |
692 // Do not add any member variables to MessageLoopForIO! This is important b/c | 692 // Do not add any member variables to MessageLoopForIO! This is important b/c |
693 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 693 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
694 // data that you need should be stored on the MessageLoop's pump_ instance. | 694 // data that you need should be stored on the MessageLoop's pump_ instance. |
695 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 695 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
696 "MessageLoopForIO should not have extra member variables"); | 696 "MessageLoopForIO should not have extra member variables"); |
697 | 697 |
698 } // namespace base | 698 } // namespace base |
699 | 699 |
700 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 700 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |