blob: a2b54d8542cf5cc73ae064200a1ca817e4929ee8 [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
dcheng093de9b2016-04-04 21:25:518#include <memory>
9
[email protected]4168ad72012-08-30 16:57:2110#include "base/base_export.h"
avi9b6f42932015-12-26 22:15:1411#include "base/macros.h"
[email protected]59e69e742013-06-18 20:27:5212#include "base/message_loop/message_pump.h"
[email protected]05062e22009-05-15 22:40:0513#include "base/observer_list.h"
[email protected]99084f62013-06-28 00:49:0714#include "base/time/time.h"
[email protected]8fc3a482008-10-03 16:52:5915
[email protected]831a32d2009-12-04 20:45:5416typedef struct _GMainContext GMainContext;
17typedef struct _GPollFD GPollFD;
18typedef struct _GSource GSource;
19
[email protected]8fc3a482008-10-03 16:52:5920namespace base {
21
[email protected]2047ef42011-06-24 20:10:2522// This class implements a base MessagePump needed for TYPE_UI MessageLoops on
23// platforms using GLib.
[email protected]4168ad72012-08-30 16:57:2124class BASE_EXPORT MessagePumpGlib : public MessagePump {
[email protected]8fc3a482008-10-03 16:52:5925 public:
[email protected]2047ef42011-06-24 20:10:2526 MessagePumpGlib();
viettrungluub9f1b812014-10-22 01:48:3027 ~MessagePumpGlib() override;
[email protected]05062e22009-05-15 22:40:0528
[email protected]95fac4232008-11-13 00:25:5129 // 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
[email protected]b105b9e2009-06-01 22:01:5332 // 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.
[email protected]95fac4232008-11-13 00:25:5135 int HandlePrepare();
[email protected]b105b9e2009-06-01 22:01:5336 bool HandleCheck();
[email protected]95fac4232008-11-13 00:25:5137 void HandleDispatch();
38
[email protected]a502bbe72011-01-07 18:06:4539 // Overridden from MessagePump:
viettrungluub9f1b812014-10-22 01:48:3040 void Run(Delegate* delegate) override;
41 void Quit() override;
42 void ScheduleWork() override;
43 void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
[email protected]a502bbe72011-01-07 18:06:4544
[email protected]b9f12d8f2014-04-16 05:29:4945 private:
[email protected]93805f32014-02-08 00:10:0346 bool ShouldQuit() const;
47
[email protected]8fc3a482008-10-03 16:52:5948 // We may make recursive calls to Run, so we save state that needs to be
49 // separate between them in this structure type.
[email protected]e7af5962010-08-05 22:36:0450 struct RunState;
[email protected]8fc3a482008-10-03 16:52:5951
[email protected]8fc3a482008-10-03 16:52:5952 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.
[email protected]7e7fab42010-11-06 22:23:2960 TimeTicks delayed_work_time_;
[email protected]8fc3a482008-10-03 16:52:5961
[email protected]8fc3a482008-10-03 16:52:5962 // The work source. It is shared by all calls to Run and destroyed when
63 // the message pump is destroyed.
64 GSource* work_source_;
[email protected]8fc3a482008-10-03 16:52:5965
[email protected]aa0f2662008-11-18 01:30:4266 // 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_;
[email protected]831a32d2009-12-04 20:45:5472 // Use a scoped_ptr to avoid needing the definition of GPollFD in the header.
dcheng093de9b2016-04-04 21:25:5173 std::unique_ptr<GPollFD> wakeup_gpollfd_;
[email protected]aa0f2662008-11-18 01:30:4274
[email protected]2047ef42011-06-24 20:10:2575 DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib);
[email protected]8fc3a482008-10-03 16:52:5976};
77
78} // namespace base
79
[email protected]59e69e742013-06-18 20:27:5280#endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_