blob: 174bd051f5764dea5c8b26df6b12bce912dfb3c8 [file] [log] [blame]
[email protected]4e0f45f52012-05-18 18:00:221// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8fc3a482008-10-03 16:52:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]59e69e742013-06-18 20:27:525#ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_
6#define BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_
[email protected]8fc3a482008-10-03 16:52:597
[email protected]4168ad72012-08-30 16:57:218#include "base/base_export.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]59e69e742013-06-18 20:27:5210#include "base/message_loop/message_pump.h"
[email protected]05062e22009-05-15 22:40:0511#include "base/observer_list.h"
[email protected]99084f62013-06-28 00:49:0712#include "base/time/time.h"
[email protected]8fc3a482008-10-03 16:52:5913
[email protected]831a32d2009-12-04 20:45:5414typedef struct _GMainContext GMainContext;
15typedef struct _GPollFD GPollFD;
16typedef struct _GSource GSource;
17
[email protected]8fc3a482008-10-03 16:52:5918namespace base {
19
[email protected]2047ef42011-06-24 20:10:2520// 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.
23class 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.
31class MessagePumpDispatcher;
32
33// This class implements a base MessagePump needed for TYPE_UI MessageLoops on
34// platforms using GLib.
[email protected]4168ad72012-08-30 16:57:2135class BASE_EXPORT MessagePumpGlib : public MessagePump {
[email protected]8fc3a482008-10-03 16:52:5936 public:
[email protected]2047ef42011-06-24 20:10:2537 MessagePumpGlib();
[email protected]54aa4f12013-07-22 22:24:1338 virtual ~MessagePumpGlib();
[email protected]05062e22009-05-15 22:40:0539
[email protected]2047ef42011-06-24 20:10:2540 // Like MessagePump::Run, but events are routed through dispatcher.
41 virtual void RunWithDispatcher(Delegate* delegate,
42 MessagePumpDispatcher* dispatcher);
[email protected]148d1052009-07-31 22:53:3743
[email protected]95fac4232008-11-13 00:25:5144 // Internal methods used for processing the pump callbacks. They are
45 // public for simplicity but should not be used directly. HandlePrepare
46 // is called during the prepare step of glib, and returns a timeout that
[email protected]b105b9e2009-06-01 22:01:5347 // will be passed to the poll. HandleCheck is called after the poll
48 // has completed, and returns whether or not HandleDispatch should be called.
49 // HandleDispatch is called if HandleCheck returned true.
[email protected]95fac4232008-11-13 00:25:5150 int HandlePrepare();
[email protected]b105b9e2009-06-01 22:01:5351 bool HandleCheck();
[email protected]95fac4232008-11-13 00:25:5152 void HandleDispatch();
53
[email protected]a502bbe72011-01-07 18:06:4554 // Overridden from MessagePump:
[email protected]28a39502011-11-23 21:35:1555 virtual void Run(Delegate* delegate) OVERRIDE;
56 virtual void Quit() OVERRIDE;
57 virtual void ScheduleWork() OVERRIDE;
58 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:4559
[email protected]b2f7ac42010-10-26 18:43:1860 protected:
61 // Returns the dispatcher for the current run state (|state_->dispatcher|).
[email protected]2047ef42011-06-24 20:10:2562 MessagePumpDispatcher* GetDispatcher();
[email protected]b2f7ac42010-10-26 18:43:1863
[email protected]8fc3a482008-10-03 16:52:5964 private:
65 // We may make recursive calls to Run, so we save state that needs to be
66 // separate between them in this structure type.
[email protected]e7af5962010-08-05 22:36:0467 struct RunState;
[email protected]8fc3a482008-10-03 16:52:5968
[email protected]8fc3a482008-10-03 16:52:5969 RunState* state_;
70
71 // This is a GLib structure that we can add event sources to. We use the
72 // default GLib context, which is the one to which all GTK events are
73 // dispatched.
74 GMainContext* context_;
75
76 // This is the time when we need to do delayed work.
[email protected]7e7fab42010-11-06 22:23:2977 TimeTicks delayed_work_time_;
[email protected]8fc3a482008-10-03 16:52:5978
[email protected]8fc3a482008-10-03 16:52:5979 // The work source. It is shared by all calls to Run and destroyed when
80 // the message pump is destroyed.
81 GSource* work_source_;
[email protected]8fc3a482008-10-03 16:52:5982
[email protected]aa0f2662008-11-18 01:30:4283 // We use a wakeup pipe to make sure we'll get out of the glib polling phase
84 // when another thread has scheduled us to do some work. There is a glib
85 // mechanism g_main_context_wakeup, but this won't guarantee that our event's
86 // Dispatch() will be called.
87 int wakeup_pipe_read_;
88 int wakeup_pipe_write_;
[email protected]831a32d2009-12-04 20:45:5489 // Use a scoped_ptr to avoid needing the definition of GPollFD in the header.
90 scoped_ptr<GPollFD> wakeup_gpollfd_;
[email protected]aa0f2662008-11-18 01:30:4291
[email protected]2047ef42011-06-24 20:10:2592 DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib);
[email protected]8fc3a482008-10-03 16:52:5993};
94
95} // namespace base
96
[email protected]59e69e742013-06-18 20:27:5297#endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_