Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: base/message_loop.h

Issue 10479018: Add base::RunLoop and update ui_test_utils to use it to reduce flakiness (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win compile Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_H_ 5 #ifndef BASE_MESSAGE_LOOP_H_
6 #define BASE_MESSAGE_LOOP_H_ 6 #define BASE_MESSAGE_LOOP_H_
7 #pragma once 7 #pragma once
8 8
9 #include <queue> 9 #include <queue>
10 #include <string> 10 #include <string>
(...skipping 24 matching lines...) Expand all
35 #include "base/message_pump_aurax11.h" 35 #include "base/message_pump_aurax11.h"
36 #else 36 #else
37 #include "base/message_pump_gtk.h" 37 #include "base/message_pump_gtk.h"
38 #endif 38 #endif
39 39
40 #endif 40 #endif
41 #endif 41 #endif
42 42
43 namespace base { 43 namespace base {
44 class Histogram; 44 class Histogram;
45 class RunLoop;
45 class ThreadTaskRunnerHandle; 46 class ThreadTaskRunnerHandle;
47 #if defined(OS_ANDROID)
48 class MessagePumpForUI;
49 #endif
46 } // namespace base 50 } // namespace base
47 51
48 // A MessageLoop is used to process events for a particular thread. There is 52 // A MessageLoop is used to process events for a particular thread. There is
49 // at most one MessageLoop instance per thread. 53 // at most one MessageLoop instance per thread.
50 // 54 //
51 // Events include at a minimum Task instances submitted to PostTask or those 55 // Events include at a minimum Task instances submitted to PostTask or those
52 // managed by TimerManager. Depending on the type of message pump used by the 56 // managed by TimerManager. Depending on the type of message pump used by the
53 // MessageLoop other events such as UI messages may be processed. On Windows 57 // MessageLoop other events such as UI messages may be processed. On Windows
54 // APC calls (as time permits) and signals sent to a registered set of HANDLEs 58 // APC calls (as time permits) and signals sent to a registered set of HANDLEs
55 // may also be processed. 59 // may also be processed.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 // MessageLoop::Run(). If this is not the same as the thread that calls 206 // MessageLoop::Run(). If this is not the same as the thread that calls
203 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from 207 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
204 // RefCountedThreadSafe<T>! 208 // RefCountedThreadSafe<T>!
205 template <class T> 209 template <class T>
206 void ReleaseSoon(const tracked_objects::Location& from_here, 210 void ReleaseSoon(const tracked_objects::Location& from_here,
207 const T* object) { 211 const T* object) {
208 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner( 212 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
209 this, from_here, object); 213 this, from_here, object);
210 } 214 }
211 215
216 // Deprecated: use RunLoop instead.
212 // Run the message loop. 217 // Run the message loop.
213 void Run(); 218 void Run();
214 219
215 // Process all pending tasks, windows messages, etc., but don't wait/sleep. 220 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
216 // Return as soon as all items that can be run are taken care of. 221 // Return as soon as all items that can be run are taken care of.
217 void RunAllPending(); 222 void RunUntilIdle();
218 223
219 // Signals the Run method to return after it is done processing all pending 224 // TODO(jbates) remove this. crbug.com/131220. See RunUntilIdle().
220 // messages. This method may only be called on the same thread that called 225 void RunAllPending() { RunUntilIdle(); }
221 // Run, and Run must still be on the call stack. 226
227 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
228 void Quit() { QuitWhenIdle(); }
229
230 // Deprecated: use RunLoop or RunAllPending instead.
222 // 231 //
223 // Use QuitClosure if you need to Quit another thread's MessageLoop, but note 232 // Signals the Run method to return when it becomes idle. It will continue to
224 // that doing so is fairly dangerous if the target thread makes nested calls 233 // process pending messages and future messages as long as they are enqueued.
225 // to MessageLoop::Run. The problem being that you won't know which nested 234 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
226 // run loop you are quitting, so be careful! 235 // Quit method when looping procedures (such as web pages) have been shut
227 void Quit(); 236 // down.
237 //
238 // This method may only be called on the same thread that called Run, and Run
239 // must still be on the call stack.
240 //
241 // Use QuitClosure variants if you need to Quit another thread's MessageLoop,
242 // but note that doing so is fairly dangerous if the target thread makes
243 // nested calls to MessageLoop::Run. The problem being that you won't know
244 // which nested run loop you are quitting, so be careful!
245 void QuitWhenIdle();
228 246
247 // Deprecated: use RunLoop instead.
248 //
229 // This method is a variant of Quit, that does not wait for pending messages 249 // This method is a variant of Quit, that does not wait for pending messages
230 // to be processed before returning from Run. 250 // to be processed before returning from Run.
231 void QuitNow(); 251 void QuitNow();
232 252
233 // Invokes Quit on the current MessageLoop when run. Useful to schedule an 253 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
234 // arbitrary MessageLoop to Quit. 254 static base::Closure QuitClosure() { return QuitWhenIdleClosure(); }
235 static base::Closure QuitClosure(); 255
256 // Deprecated: use RunLoop instead.
257 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
258 // arbitrary MessageLoop to QuitWhenIdle.
259 static base::Closure QuitWhenIdleClosure();
sky 2012/06/27 14:02:10 MenuController, which runs a nested message loop v
jbates 2012/06/27 17:23:46 Can you elaborate on "not called off enough"? The
236 260
237 // Returns the type passed to the constructor. 261 // Returns the type passed to the constructor.
238 Type type() const { return type_; } 262 Type type() const { return type_; }
239 263
240 // Optional call to connect the thread name with this loop. 264 // Optional call to connect the thread name with this loop.
241 void set_thread_name(const std::string& thread_name) { 265 void set_thread_name(const std::string& thread_name) {
242 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; 266 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
243 thread_name_ = thread_name; 267 thread_name_ = thread_name;
244 } 268 }
245 const std::string& thread_name() const { return thread_name_; } 269 const std::string& thread_name() const { return thread_name_; }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 bool os_modal_loop() const { 371 bool os_modal_loop() const {
348 return os_modal_loop_; 372 return os_modal_loop_;
349 } 373 }
350 #endif // OS_WIN 374 #endif // OS_WIN
351 375
352 // Can only be called from the thread that owns the MessageLoop. 376 // Can only be called from the thread that owns the MessageLoop.
353 bool is_running() const; 377 bool is_running() const;
354 378
355 //---------------------------------------------------------------------------- 379 //----------------------------------------------------------------------------
356 protected: 380 protected:
357 struct RunState { 381 friend class base::RunLoop;
358 // Used to count how many Run() invocations are on the stack.
359 int run_depth;
360
361 // Used to record that Quit() was called, or that we should quit the pump
362 // once it becomes idle.
363 bool quit_received;
364
365 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
366 Dispatcher* dispatcher;
367 #endif
368 };
369
370 #if defined(OS_ANDROID)
371 // Android Java process manages the UI thread message loop. So its
372 // MessagePumpForUI needs to keep the RunState.
373 public:
374 #endif
375 class BASE_EXPORT AutoRunState : RunState {
376 public:
377 explicit AutoRunState(MessageLoop* loop);
378 ~AutoRunState();
379 private:
380 MessageLoop* loop_;
381 RunState* previous_state_;
382 };
383 #if defined(OS_ANDROID)
384 protected:
385 #endif
386 382
387 #if defined(OS_WIN) 383 #if defined(OS_WIN)
388 base::MessagePumpWin* pump_win() { 384 base::MessagePumpWin* pump_win() {
389 return static_cast<base::MessagePumpWin*>(pump_.get()); 385 return static_cast<base::MessagePumpWin*>(pump_.get());
390 } 386 }
391 #elif defined(OS_POSIX) 387 #elif defined(OS_POSIX)
392 base::MessagePumpLibevent* pump_libevent() { 388 base::MessagePumpLibevent* pump_libevent() {
393 return static_cast<base::MessagePumpLibevent*>(pump_.get()); 389 return static_cast<base::MessagePumpLibevent*>(pump_.get());
394 } 390 }
395 #endif 391 #endif
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 base::Histogram* message_histogram_; 485 base::Histogram* message_histogram_;
490 486
491 // A null terminated list which creates an incoming_queue of tasks that are 487 // A null terminated list which creates an incoming_queue of tasks that are
492 // acquired under a mutex for processing on this instance's thread. These 488 // acquired under a mutex for processing on this instance's thread. These
493 // tasks have not yet been sorted out into items for our work_queue_ vs items 489 // tasks have not yet been sorted out into items for our work_queue_ vs items
494 // that will be handled by the TimerManager. 490 // that will be handled by the TimerManager.
495 base::TaskQueue incoming_queue_; 491 base::TaskQueue incoming_queue_;
496 // Protect access to incoming_queue_. 492 // Protect access to incoming_queue_.
497 mutable base::Lock incoming_queue_lock_; 493 mutable base::Lock incoming_queue_lock_;
498 494
499 RunState* state_; 495 base::RunLoop* run_loop_;
500 496
501 #if defined(OS_WIN) 497 #if defined(OS_WIN)
502 base::TimeTicks high_resolution_timer_expiration_; 498 base::TimeTicks high_resolution_timer_expiration_;
503 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc 499 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
504 // which enter a modal message loop. 500 // which enter a modal message loop.
505 bool os_modal_loop_; 501 bool os_modal_loop_;
506 #endif 502 #endif
507 503
508 // The next sequence number to use for delayed tasks. 504 // The next sequence number to use for delayed tasks.
509 int next_sequence_num_; 505 int next_sequence_num_;
510 506
511 ObserverList<TaskObserver> task_observers_; 507 ObserverList<TaskObserver> task_observers_;
512 508
513 // The message loop proxy associated with this message loop, if one exists. 509 // The message loop proxy associated with this message loop, if one exists.
514 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 510 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
515 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; 511 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
516 512
517 private: 513 private:
518 template <class T, class R> friend class base::subtle::DeleteHelperInternal; 514 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
519 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; 515 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
520 516
521 void DeleteSoonInternal(const tracked_objects::Location& from_here, 517 void DeleteSoonInternal(const tracked_objects::Location& from_here,
522 void(*deleter)(const void*), 518 void(*deleter)(const void*),
523 const void* object); 519 const void* object);
524 void ReleaseSoonInternal(const tracked_objects::Location& from_here, 520 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
525 void(*releaser)(const void*), 521 void(*releaser)(const void*),
526 const void* object); 522 const void* object);
527 523
528
529 DISALLOW_COPY_AND_ASSIGN(MessageLoop); 524 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
530 }; 525 };
531 526
532 //----------------------------------------------------------------------------- 527 //-----------------------------------------------------------------------------
533 // MessageLoopForUI extends MessageLoop with methods that are particular to a 528 // MessageLoopForUI extends MessageLoop with methods that are particular to a
534 // MessageLoop instantiated with TYPE_UI. 529 // MessageLoop instantiated with TYPE_UI.
535 // 530 //
536 // This class is typically used like so: 531 // This class is typically used like so:
537 // MessageLoopForUI::current()->...call some method... 532 // MessageLoopForUI::current()->...call some method...
538 // 533 //
(...skipping 17 matching lines...) Expand all
556 #if defined(OS_ANDROID) 551 #if defined(OS_ANDROID)
557 // On Android, the UI message loop is handled by Java side. So Run() should 552 // On Android, the UI message loop is handled by Java side. So Run() should
558 // never be called. Instead use Start(), which will forward all the native UI 553 // never be called. Instead use Start(), which will forward all the native UI
559 // events to the Java message loop. 554 // events to the Java message loop.
560 void Start(); 555 void Start();
561 #elif !defined(OS_MACOSX) 556 #elif !defined(OS_MACOSX)
562 // Please see message_pump_win/message_pump_glib for definitions of these 557 // Please see message_pump_win/message_pump_glib for definitions of these
563 // methods. 558 // methods.
564 void AddObserver(Observer* observer); 559 void AddObserver(Observer* observer);
565 void RemoveObserver(Observer* observer); 560 void RemoveObserver(Observer* observer);
566 void RunWithDispatcher(Dispatcher* dispatcher);
567 void RunAllPendingWithDispatcher(Dispatcher* dispatcher);
568 561
569 protected: 562 protected:
570 // TODO(rvargas): Make this platform independent. 563 // TODO(rvargas): Make this platform independent.
571 base::MessagePumpForUI* pump_ui() { 564 base::MessagePumpForUI* pump_ui() {
572 return static_cast<base::MessagePumpForUI*>(pump_.get()); 565 return static_cast<base::MessagePumpForUI*>(pump_.get());
573 } 566 }
574 #endif // !defined(OS_MACOSX) 567 #endif // !defined(OS_MACOSX)
575 }; 568 };
576 569
577 // Do not add any member variables to MessageLoopForUI! This is important b/c 570 // Do not add any member variables to MessageLoopForUI! This is important b/c
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 #endif // defined(OS_POSIX) 644 #endif // defined(OS_POSIX)
652 }; 645 };
653 646
654 // Do not add any member variables to MessageLoopForIO! This is important b/c 647 // Do not add any member variables to MessageLoopForIO! This is important b/c
655 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra 648 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
656 // data that you need should be stored on the MessageLoop's pump_ instance. 649 // data that you need should be stored on the MessageLoop's pump_ instance.
657 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), 650 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
658 MessageLoopForIO_should_not_have_extra_member_variables); 651 MessageLoopForIO_should_not_have_extra_member_variables);
659 652
660 #endif // BASE_MESSAGE_LOOP_H_ 653 #endif // BASE_MESSAGE_LOOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698