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