[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_MESSAGE_PUMP_GLIB_H_ |
| 6 | #define BASE_MESSAGE_PUMP_GLIB_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 8 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 10 | #include "base/message_pump.h" |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 11 | #include "base/observer_list.h" |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 12 | #include "base/time.h" |
| 13 | |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 14 | typedef struct _GMainContext GMainContext; |
| 15 | typedef struct _GPollFD GPollFD; |
| 16 | typedef struct _GSource GSource; |
| 17 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 18 | namespace base { |
| 19 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 20 | // MessagePumpObserver is notified prior to an event being dispatched. As |
| 21 | // Observers are notified of every change, they have to be FAST! The platform |
| 22 | // specific implementation of the class is in message_pump_gtk/message_pump_x. |
| 23 | class MessagePumpObserver; |
| 24 | |
| 25 | // MessagePumpDispatcher is used during a nested invocation of Run to dispatch |
| 26 | // events. If Run is invoked with a non-NULL MessagePumpDispatcher, MessageLoop |
| 27 | // does not dispatch events (or invoke gtk_main_do_event), rather every event is |
| 28 | // passed to Dispatcher's Dispatch method for dispatch. It is up to the |
| 29 | // Dispatcher to dispatch, or not, the event. The platform specific |
| 30 | // implementation of the class is in message_pump_gtk/message_pump_x. |
| 31 | class MessagePumpDispatcher; |
| 32 | |
| 33 | // This class implements a base MessagePump needed for TYPE_UI MessageLoops on |
| 34 | // platforms using GLib. |
| 35 | class MessagePumpGlib : public MessagePump { |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 36 | public: |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 37 | MessagePumpGlib(); |
| 38 | virtual ~MessagePumpGlib(); |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 39 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 40 | // Like MessagePump::Run, but events are routed through dispatcher. |
| 41 | virtual void RunWithDispatcher(Delegate* delegate, |
| 42 | MessagePumpDispatcher* dispatcher); |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 43 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 44 | // Run a single iteration of the mainloop. A return value of true indicates |
| 45 | // that an event was handled. |block| indicates if it should wait if no event |
| 46 | // is ready for processing. |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 47 | virtual bool RunOnce(GMainContext* context, bool block) = 0; |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 48 | |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 49 | // Internal methods used for processing the pump callbacks. They are |
| 50 | // public for simplicity but should not be used directly. HandlePrepare |
| 51 | // is called during the prepare step of glib, and returns a timeout that |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 52 | // will be passed to the poll. HandleCheck is called after the poll |
| 53 | // has completed, and returns whether or not HandleDispatch should be called. |
| 54 | // HandleDispatch is called if HandleCheck returned true. |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 55 | int HandlePrepare(); |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 56 | bool HandleCheck(); |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 57 | void HandleDispatch(); |
| 58 | |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 59 | // Adds an Observer, which will start receiving notifications immediately. |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 60 | void AddObserver(MessagePumpObserver* observer); |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 61 | |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 62 | // Removes an Observer. It is safe to call this method while an Observer is |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 63 | // receiving a notification callback. |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 64 | void RemoveObserver(MessagePumpObserver* observer); |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 65 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 66 | // Overridden from MessagePump: |
| 67 | virtual void Run(Delegate* delegate); |
| 68 | virtual void Quit(); |
| 69 | virtual void ScheduleWork(); |
| 70 | virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); |
| 71 | |
[email protected] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 72 | protected: |
| 73 | // Returns the dispatcher for the current run state (|state_->dispatcher|). |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 74 | MessagePumpDispatcher* GetDispatcher(); |
[email protected] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 75 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 76 | ObserverList<MessagePumpObserver>& observers() { return observers_; } |
[email protected] | 5040dfab | 2011-05-11 20:50:00 | [diff] [blame] | 77 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 78 | private: |
| 79 | // We may make recursive calls to Run, so we save state that needs to be |
| 80 | // separate between them in this structure type. |
[email protected] | e7af596 | 2010-08-05 22:36:04 | [diff] [blame] | 81 | struct RunState; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 82 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 83 | RunState* state_; |
| 84 | |
| 85 | // This is a GLib structure that we can add event sources to. We use the |
| 86 | // default GLib context, which is the one to which all GTK events are |
| 87 | // dispatched. |
| 88 | GMainContext* context_; |
| 89 | |
| 90 | // This is the time when we need to do delayed work. |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 91 | TimeTicks delayed_work_time_; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 92 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 93 | // The work source. It is shared by all calls to Run and destroyed when |
| 94 | // the message pump is destroyed. |
| 95 | GSource* work_source_; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 96 | |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 97 | // We use a wakeup pipe to make sure we'll get out of the glib polling phase |
| 98 | // when another thread has scheduled us to do some work. There is a glib |
| 99 | // mechanism g_main_context_wakeup, but this won't guarantee that our event's |
| 100 | // Dispatch() will be called. |
| 101 | int wakeup_pipe_read_; |
| 102 | int wakeup_pipe_write_; |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 103 | // Use a scoped_ptr to avoid needing the definition of GPollFD in the header. |
| 104 | scoped_ptr<GPollFD> wakeup_gpollfd_; |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 105 | |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 106 | // List of observers. |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 107 | ObserverList<MessagePumpObserver> observers_; |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 108 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame^] | 109 | DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace base |
| 113 | |
| 114 | #endif // BASE_MESSAGE_PUMP_GLIB_H_ |