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 <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 typedef std::unique_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 std::unique_ptr<MessagePump> CreateMessagePumpForType(Type type); | 135 static std::unique_ptr<MessagePump> CreateMessagePumpForType(Type type); |
| 136 |
136 // A DestructionObserver is notified when the current MessageLoop is being | 137 // A DestructionObserver is notified when the current MessageLoop is being |
137 // destroyed. These observers are notified prior to MessageLoop::current() | 138 // destroyed. These observers are notified prior to MessageLoop::current() |
138 // being changed to return NULL. This gives interested parties the chance to | 139 // being changed to return NULL. This gives interested parties the chance to |
139 // do final cleanup that depends on the MessageLoop. | 140 // do final cleanup that depends on the MessageLoop. |
140 // | 141 // |
141 // NOTE: Any tasks posted to the MessageLoop during this notification will | 142 // NOTE: Any tasks posted to the MessageLoop during this notification will |
142 // not be run. Instead, they will be deleted. | 143 // not be run. Instead, they will be deleted. |
143 // | 144 // |
144 class BASE_EXPORT DestructionObserver { | 145 class BASE_EXPORT DestructionObserver { |
145 public: | 146 public: |
146 virtual void WillDestroyCurrentMessageLoop() = 0; | 147 virtual void WillDestroyCurrentMessageLoop() = 0; |
147 | 148 |
148 protected: | 149 protected: |
149 virtual ~DestructionObserver(); | 150 virtual ~DestructionObserver(); |
150 }; | 151 }; |
151 | 152 |
152 // Add a DestructionObserver, which will start receiving notifications | 153 // Add a DestructionObserver, which will start receiving notifications |
153 // immediately. | 154 // immediately. |
154 void AddDestructionObserver(DestructionObserver* destruction_observer); | 155 void AddDestructionObserver(DestructionObserver* destruction_observer); |
155 | 156 |
156 // Remove a DestructionObserver. It is safe to call this method while a | 157 // Remove a DestructionObserver. It is safe to call this method while a |
157 // DestructionObserver is receiving a notification callback. | 158 // DestructionObserver is receiving a notification callback. |
158 void RemoveDestructionObserver(DestructionObserver* destruction_observer); | 159 void RemoveDestructionObserver(DestructionObserver* destruction_observer); |
159 | 160 |
| 161 // A NestingObserver is notified when a nested message loop begins. The |
| 162 // observers are notified before the first task is processed. |
| 163 class BASE_EXPORT NestingObserver { |
| 164 public: |
| 165 virtual void OnBeginNestedMessageLoop() = 0; |
| 166 |
| 167 protected: |
| 168 virtual ~NestingObserver(); |
| 169 }; |
| 170 |
| 171 void AddNestingObserver(NestingObserver* observer); |
| 172 void RemoveNestingObserver(NestingObserver* observer); |
| 173 |
160 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces. | 174 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces. |
161 // TODO(skyostil): Remove these functions (crbug.com/465354). | 175 // TODO(skyostil): Remove these functions (crbug.com/465354). |
162 // | 176 // |
163 // The "PostTask" family of methods call the task's Run method asynchronously | 177 // The "PostTask" family of methods call the task's Run method asynchronously |
164 // from within a message loop at some point in the future. | 178 // from within a message loop at some point in the future. |
165 // | 179 // |
166 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed | 180 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed |
167 // with normal UI or IO event processing. With the PostDelayedTask variant, | 181 // with normal UI or IO event processing. With the PostDelayedTask variant, |
168 // tasks are called after at least approximately 'delay_ms' have elapsed. | 182 // tasks are called after at least approximately 'delay_ms' have elapsed. |
169 // | 183 // |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 | 480 |
467 // Start recording histogram info about events and action IF it was enabled | 481 // Start recording histogram info about events and action IF it was enabled |
468 // and IF the statistics recorder can accept a registration of our histogram. | 482 // and IF the statistics recorder can accept a registration of our histogram. |
469 void StartHistogrammer(); | 483 void StartHistogrammer(); |
470 | 484 |
471 // Add occurrence of event to our histogram, so that we can see what is being | 485 // Add occurrence of event to our histogram, so that we can see what is being |
472 // done in a specific MessageLoop instance (i.e., specific thread). | 486 // done in a specific MessageLoop instance (i.e., specific thread). |
473 // If message_histogram_ is NULL, this is a no-op. | 487 // If message_histogram_ is NULL, this is a no-op. |
474 void HistogramEvent(int event); | 488 void HistogramEvent(int event); |
475 | 489 |
| 490 // Notify observers that a nested message loop is starting. |
| 491 void NotifyBeginNestedLoop(); |
| 492 |
476 // MessagePump::Delegate methods: | 493 // MessagePump::Delegate methods: |
477 bool DoWork() override; | 494 bool DoWork() override; |
478 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; | 495 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; |
479 bool DoIdleWork() override; | 496 bool DoIdleWork() override; |
480 | 497 |
481 const Type type_; | 498 const Type type_; |
482 | 499 |
483 // A list of tasks that need to be processed by this instance. Note that | 500 // A list of tasks that need to be processed by this instance. Note that |
484 // this queue is only accessed (push/pop) by our current thread. | 501 // this queue is only accessed (push/pop) by our current thread. |
485 TaskQueue work_queue_; | 502 TaskQueue work_queue_; |
(...skipping 14 matching lines...) Expand all Loading... |
500 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. | 517 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
501 TimeTicks recent_time_; | 518 TimeTicks recent_time_; |
502 | 519 |
503 // A queue of non-nestable tasks that we had to defer because when it came | 520 // A queue of non-nestable tasks that we had to defer because when it came |
504 // time to execute them we were in a nested message loop. They will execute | 521 // time to execute them we were in a nested message loop. They will execute |
505 // once we're out of nested message loops. | 522 // once we're out of nested message loops. |
506 TaskQueue deferred_non_nestable_work_queue_; | 523 TaskQueue deferred_non_nestable_work_queue_; |
507 | 524 |
508 ObserverList<DestructionObserver> destruction_observers_; | 525 ObserverList<DestructionObserver> destruction_observers_; |
509 | 526 |
| 527 ObserverList<NestingObserver> nesting_observers_; |
| 528 |
510 // A recursion block that prevents accidentally running additional tasks when | 529 // A recursion block that prevents accidentally running additional tasks when |
511 // insider a (accidentally induced?) nested message pump. | 530 // insider a (accidentally induced?) nested message pump. |
512 bool nestable_tasks_allowed_; | 531 bool nestable_tasks_allowed_; |
513 | 532 |
514 #if defined(OS_WIN) | 533 #if defined(OS_WIN) |
515 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc. | 534 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc. |
516 // which enter a modal message loop. | 535 // which enter a modal message loop. |
517 bool os_modal_loop_; | 536 bool os_modal_loop_; |
518 #endif | 537 #endif |
519 | 538 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 | 710 |
692 // Do not add any member variables to MessageLoopForIO! This is important b/c | 711 // Do not add any member variables to MessageLoopForIO! This is important b/c |
693 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 712 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
694 // data that you need should be stored on the MessageLoop's pump_ instance. | 713 // data that you need should be stored on the MessageLoop's pump_ instance. |
695 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 714 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
696 "MessageLoopForIO should not have extra member variables"); | 715 "MessageLoopForIO should not have extra member variables"); |
697 | 716 |
698 } // namespace base | 717 } // namespace base |
699 | 718 |
700 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 719 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |