| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_PUMP_GLIB_H_ | 5 #ifndef BASE_MESSAGE_PUMP_GLIB_H_ |
| 6 #define BASE_MESSAGE_PUMP_GLIB_H_ | 6 #define BASE_MESSAGE_PUMP_GLIB_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_pump.h" | 10 #include "base/message_pump.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 | 13 |
| 14 typedef union _GdkEvent GdkEvent; | |
| 15 typedef struct _GMainContext GMainContext; | 14 typedef struct _GMainContext GMainContext; |
| 16 typedef struct _GPollFD GPollFD; | 15 typedef struct _GPollFD GPollFD; |
| 17 typedef struct _GSource GSource; | 16 typedef struct _GSource GSource; |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 | 19 |
| 21 // This class implements a MessagePump needed for TYPE_UI MessageLoops on | 20 // MessagePumpObserver is notified prior to an event being dispatched. As |
| 22 // OS_LINUX platforms using GLib. | 21 // Observers are notified of every change, they have to be FAST! The platform |
| 23 class MessagePumpForUI : public MessagePump { | 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 { |
| 24 public: | 36 public: |
| 25 // Observer is notified prior to a GdkEvent event being dispatched. As | 37 MessagePumpGlib(); |
| 26 // Observers are notified of every change, they have to be FAST! | 38 virtual ~MessagePumpGlib(); |
| 27 class Observer { | |
| 28 public: | |
| 29 virtual ~Observer() {} | |
| 30 | 39 |
| 31 // This method is called before processing a message. | 40 // Like MessagePump::Run, but events are routed through dispatcher. |
| 32 virtual void WillProcessEvent(GdkEvent* event) = 0; | 41 virtual void RunWithDispatcher(Delegate* delegate, |
| 33 | 42 MessagePumpDispatcher* dispatcher); |
| 34 // This method is called after processing a message. | |
| 35 virtual void DidProcessEvent(GdkEvent* event) = 0; | |
| 36 }; | |
| 37 | |
| 38 // Dispatcher is used during a nested invocation of Run to dispatch events. | |
| 39 // If Run is invoked with a non-NULL Dispatcher, MessageLoop does not | |
| 40 // dispatch events (or invoke gtk_main_do_event), rather every event is | |
| 41 // passed to Dispatcher's Dispatch method for dispatch. It is up to the | |
| 42 // Dispatcher to dispatch, or not, the event. | |
| 43 // | |
| 44 // The nested loop is exited by either posting a quit, or returning false | |
| 45 // from Dispatch. | |
| 46 class Dispatcher { | |
| 47 public: | |
| 48 virtual ~Dispatcher() {} | |
| 49 // Dispatches the event. If true is returned processing continues as | |
| 50 // normal. If false is returned, the nested loop exits immediately. | |
| 51 virtual bool Dispatch(GdkEvent* event) = 0; | |
| 52 }; | |
| 53 | |
| 54 MessagePumpForUI(); | |
| 55 virtual ~MessagePumpForUI(); | |
| 56 | |
| 57 // Like MessagePump::Run, but GdkEvent objects are routed through dispatcher. | |
| 58 virtual void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher); | |
| 59 | 43 |
| 60 // Run a single iteration of the mainloop. A return value of true indicates | 44 // Run a single iteration of the mainloop. A return value of true indicates |
| 61 // that an event was handled. |block| indicates if it should wait if no event | 45 // that an event was handled. |block| indicates if it should wait if no event |
| 62 // is ready for processing. | 46 // is ready for processing. |
| 63 virtual bool RunOnce(GMainContext* context, bool block); | 47 virtual bool RunOnce(GMainContext* context, bool block) = 0; |
| 64 | 48 |
| 65 // Internal methods used for processing the pump callbacks. They are | 49 // Internal methods used for processing the pump callbacks. They are |
| 66 // public for simplicity but should not be used directly. HandlePrepare | 50 // public for simplicity but should not be used directly. HandlePrepare |
| 67 // is called during the prepare step of glib, and returns a timeout that | 51 // is called during the prepare step of glib, and returns a timeout that |
| 68 // will be passed to the poll. HandleCheck is called after the poll | 52 // will be passed to the poll. HandleCheck is called after the poll |
| 69 // has completed, and returns whether or not HandleDispatch should be called. | 53 // has completed, and returns whether or not HandleDispatch should be called. |
| 70 // HandleDispatch is called if HandleCheck returned true. | 54 // HandleDispatch is called if HandleCheck returned true. |
| 71 int HandlePrepare(); | 55 int HandlePrepare(); |
| 72 bool HandleCheck(); | 56 bool HandleCheck(); |
| 73 void HandleDispatch(); | 57 void HandleDispatch(); |
| 74 | 58 |
| 75 // Adds an Observer, which will start receiving notifications immediately. | 59 // Adds an Observer, which will start receiving notifications immediately. |
| 76 void AddObserver(Observer* observer); | 60 void AddObserver(MessagePumpObserver* observer); |
| 77 | 61 |
| 78 // Removes an Observer. It is safe to call this method while an Observer is | 62 // Removes an Observer. It is safe to call this method while an Observer is |
| 79 // receiving a notification callback. | 63 // receiving a notification callback. |
| 80 void RemoveObserver(Observer* observer); | 64 void RemoveObserver(MessagePumpObserver* observer); |
| 81 | |
| 82 // Dispatch an available GdkEvent. Essentially this allows a subclass to do | |
| 83 // some task before/after calling the default handler (EventDispatcher). | |
| 84 virtual void DispatchEvents(GdkEvent* event); | |
| 85 | 65 |
| 86 // Overridden from MessagePump: | 66 // Overridden from MessagePump: |
| 87 virtual void Run(Delegate* delegate); | 67 virtual void Run(Delegate* delegate); |
| 88 virtual void Quit(); | 68 virtual void Quit(); |
| 89 virtual void ScheduleWork(); | 69 virtual void ScheduleWork(); |
| 90 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); | 70 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); |
| 91 | 71 |
| 92 protected: | 72 protected: |
| 93 // Returns the dispatcher for the current run state (|state_->dispatcher|). | 73 // Returns the dispatcher for the current run state (|state_->dispatcher|). |
| 94 Dispatcher* GetDispatcher(); | 74 MessagePumpDispatcher* GetDispatcher(); |
| 95 | 75 |
| 96 ObserverList<Observer>& observers() { return observers_; } | 76 ObserverList<MessagePumpObserver>& observers() { return observers_; } |
| 97 | 77 |
| 98 private: | 78 private: |
| 99 // We may make recursive calls to Run, so we save state that needs to be | 79 // We may make recursive calls to Run, so we save state that needs to be |
| 100 // separate between them in this structure type. | 80 // separate between them in this structure type. |
| 101 struct RunState; | 81 struct RunState; |
| 102 | 82 |
| 103 // Invoked from EventDispatcher. Notifies all observers we're about to | |
| 104 // process an event. | |
| 105 void WillProcessEvent(GdkEvent* event); | |
| 106 | |
| 107 // Invoked from EventDispatcher. Notifies all observers we processed an | |
| 108 // event. | |
| 109 void DidProcessEvent(GdkEvent* event); | |
| 110 | |
| 111 // Callback prior to gdk dispatching an event. | |
| 112 static void EventDispatcher(GdkEvent* event, void* data); | |
| 113 | |
| 114 RunState* state_; | 83 RunState* state_; |
| 115 | 84 |
| 116 // This is a GLib structure that we can add event sources to. We use the | 85 // This is a GLib structure that we can add event sources to. We use the |
| 117 // default GLib context, which is the one to which all GTK events are | 86 // default GLib context, which is the one to which all GTK events are |
| 118 // dispatched. | 87 // dispatched. |
| 119 GMainContext* context_; | 88 GMainContext* context_; |
| 120 | 89 |
| 121 // This is the time when we need to do delayed work. | 90 // This is the time when we need to do delayed work. |
| 122 TimeTicks delayed_work_time_; | 91 TimeTicks delayed_work_time_; |
| 123 | 92 |
| 124 // The work source. It is shared by all calls to Run and destroyed when | 93 // The work source. It is shared by all calls to Run and destroyed when |
| 125 // the message pump is destroyed. | 94 // the message pump is destroyed. |
| 126 GSource* work_source_; | 95 GSource* work_source_; |
| 127 | 96 |
| 128 // We use a wakeup pipe to make sure we'll get out of the glib polling phase | 97 // We use a wakeup pipe to make sure we'll get out of the glib polling phase |
| 129 // when another thread has scheduled us to do some work. There is a glib | 98 // when another thread has scheduled us to do some work. There is a glib |
| 130 // mechanism g_main_context_wakeup, but this won't guarantee that our event's | 99 // mechanism g_main_context_wakeup, but this won't guarantee that our event's |
| 131 // Dispatch() will be called. | 100 // Dispatch() will be called. |
| 132 int wakeup_pipe_read_; | 101 int wakeup_pipe_read_; |
| 133 int wakeup_pipe_write_; | 102 int wakeup_pipe_write_; |
| 134 // Use a scoped_ptr to avoid needing the definition of GPollFD in the header. | 103 // Use a scoped_ptr to avoid needing the definition of GPollFD in the header. |
| 135 scoped_ptr<GPollFD> wakeup_gpollfd_; | 104 scoped_ptr<GPollFD> wakeup_gpollfd_; |
| 136 | 105 |
| 137 // List of observers. | 106 // List of observers. |
| 138 ObserverList<Observer> observers_; | 107 ObserverList<MessagePumpObserver> observers_; |
| 139 | 108 |
| 140 DISALLOW_COPY_AND_ASSIGN(MessagePumpForUI); | 109 DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib); |
| 141 }; | 110 }; |
| 142 | 111 |
| 143 } // namespace base | 112 } // namespace base |
| 144 | 113 |
| 145 #endif // BASE_MESSAGE_PUMP_GLIB_H_ | 114 #endif // BASE_MESSAGE_PUMP_GLIB_H_ |
| OLD | NEW |