[email protected] | 4e0f45f5 | 2012-05-18 18:00:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 5 | #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_ |
6 | #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_ | ||||
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 7 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 8 | #include <memory> |
9 | |||||
[email protected] | 4168ad7 | 2012-08-30 16:57:21 | [diff] [blame] | 10 | #include "base/base_export.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 12 | #include "base/message_loop/message_pump.h" |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 13 | #include "base/observer_list.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 15 | |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 16 | typedef struct _GMainContext GMainContext; |
17 | typedef struct _GPollFD GPollFD; | ||||
18 | typedef struct _GSource GSource; | ||||
19 | |||||
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 20 | namespace base { |
21 | |||||
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 22 | // This class implements a base MessagePump needed for TYPE_UI MessageLoops on |
23 | // platforms using GLib. | ||||
[email protected] | 4168ad7 | 2012-08-30 16:57:21 | [diff] [blame] | 24 | class BASE_EXPORT MessagePumpGlib : public MessagePump { |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 25 | public: |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 26 | MessagePumpGlib(); |
viettrungluu | b9f1b81 | 2014-10-22 01:48:30 | [diff] [blame] | 27 | ~MessagePumpGlib() override; |
[email protected] | 05062e2 | 2009-05-15 22:40:05 | [diff] [blame] | 28 | |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 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 | ||||
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 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. | ||||
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 35 | int HandlePrepare(); |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 36 | bool HandleCheck(); |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 37 | void HandleDispatch(); |
38 | |||||
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 39 | // Overridden from MessagePump: |
viettrungluu | b9f1b81 | 2014-10-22 01:48:30 | [diff] [blame] | 40 | void Run(Delegate* delegate) override; |
41 | void Quit() override; | ||||
42 | void ScheduleWork() override; | ||||
43 | void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; | ||||
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 44 | |
[email protected] | b9f12d8f | 2014-04-16 05:29:49 | [diff] [blame] | 45 | private: |
[email protected] | 93805f3 | 2014-02-08 00:10:03 | [diff] [blame] | 46 | bool ShouldQuit() const; |
47 | |||||
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 48 | // 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] | e7af596 | 2010-08-05 22:36:04 | [diff] [blame] | 50 | struct RunState; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 51 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 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. | ||||
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 60 | TimeTicks delayed_work_time_; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 61 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 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_; | ||||
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 65 | |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 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_; | ||||
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 72 | // Use a scoped_ptr to avoid needing the definition of GPollFD in the header. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 73 | std::unique_ptr<GPollFD> wakeup_gpollfd_; |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 74 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 75 | DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 76 | }; |
77 | |||||
78 | } // namespace base | ||||
79 | |||||
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 80 | #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_GLIB_H_ |