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