• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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_LOOP_MESSAGE_PUMP_GLIB_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_
7 
8 #include <memory>
9 
10 #include "base/base_export.h"
11 #include "base/macros.h"
12 #include "base/message_loop/message_pump.h"
13 #include "base/observer_list.h"
14 #include "base/time/time.h"
15 
16 typedef struct _GMainContext GMainContext;
17 typedef struct _GPollFD GPollFD;
18 typedef struct _GSource GSource;
19 
20 namespace base {
21 
22 // This class implements a base MessagePump needed for TYPE_UI MessageLoops on
23 // platforms using GLib.
24 class BASE_EXPORT MessagePumpGlib : public MessagePump {
25  public:
26   MessagePumpGlib();
27   ~MessagePumpGlib() override;
28 
29   // Internal methods used for processing the pump callbacks.  They are
30   // public for simplicity but should not be used directly.  HandlePrepare
31   // is called during the prepare step of glib, and returns a timeout that
32   // will be passed to the poll. HandleCheck is called after the poll
33   // has completed, and returns whether or not HandleDispatch should be called.
34   // HandleDispatch is called if HandleCheck returned true.
35   int HandlePrepare();
36   bool HandleCheck();
37   void HandleDispatch();
38 
39   // Overridden from MessagePump:
40   void Run(Delegate* delegate) override;
41   void Quit() override;
42   void ScheduleWork() override;
43   void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
44 
45  private:
46   bool ShouldQuit() const;
47 
48   // We may make recursive calls to Run, so we save state that needs to be
49   // separate between them in this structure type.
50   struct RunState;
51 
52   RunState* state_;
53 
54   // This is a GLib structure that we can add event sources to.  We use the
55   // default GLib context, which is the one to which all GTK events are
56   // dispatched.
57   GMainContext* context_;
58 
59   // This is the time when we need to do delayed work.
60   TimeTicks delayed_work_time_;
61 
62   // The work source.  It is shared by all calls to Run and destroyed when
63   // the message pump is destroyed.
64   GSource* work_source_;
65 
66   // We use a wakeup pipe to make sure we'll get out of the glib polling phase
67   // when another thread has scheduled us to do some work.  There is a glib
68   // mechanism g_main_context_wakeup, but this won't guarantee that our event's
69   // Dispatch() will be called.
70   int wakeup_pipe_read_;
71   int wakeup_pipe_write_;
72   // Use a scoped_ptr to avoid needing the definition of GPollFD in the header.
73   std::unique_ptr<GPollFD> wakeup_gpollfd_;
74 
75   DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib);
76 };
77 
78 }  // namespace base
79 
80 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_
81