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