Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 | // A simple event bridge to allow glib programs to operate on a libchrome |
| 6 | // message loop. |
| 7 | |
| 8 | #ifndef GLIB_BRIDGE_GLIB_BRIDGE_H_ |
| 9 | #define GLIB_BRIDGE_GLIB_BRIDGE_H_ |
| 10 | |
| 11 | #include <glib.h> |
| 12 | |
| 13 | #include <map> |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include <base/bind.h> |
| 18 | #include <base/cancelable_callback.h> |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 19 | #include <base/files/file_descriptor_watcher_posix.h> |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 20 | |
Eric Caruso | 353af30 | 2020-10-12 11:44:25 -0700 | [diff] [blame] | 21 | #include "glib-bridge/glib_bridge_export.h" |
| 22 | |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 23 | namespace glib_bridge { |
| 24 | |
Eric Caruso | 353af30 | 2020-10-12 11:44:25 -0700 | [diff] [blame] | 25 | struct GLIB_BRIDGE_EXPORT GlibBridge { |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 26 | public: |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 27 | GlibBridge(); |
| 28 | virtual ~GlibBridge(); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | enum class State { |
| 32 | kPreparingIteration, |
| 33 | kWaitingForEvents, |
| 34 | kReadyForDispatch, |
| 35 | }; |
| 36 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 37 | struct Watcher { |
| 38 | std::unique_ptr<base::FileDescriptorWatcher::Controller> reader; |
| 39 | std::unique_ptr<base::FileDescriptorWatcher::Controller> writer; |
| 40 | }; |
| 41 | |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 42 | void PrepareIteration(); |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 43 | void Timeout(); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 44 | void Dispatch(); |
| 45 | |
| 46 | void OnEvent(int fd, int flag); |
| 47 | |
| 48 | // If we ever need to support multiple GMainContexts instead of just the |
| 49 | // default one then we can wrap a different context here. This is a weak |
| 50 | // pointer. |
| 51 | GMainContext* glib_context_; |
| 52 | |
| 53 | // glib event and source bits. |
| 54 | int max_priority_ = -1; |
| 55 | std::vector<GPollFD> poll_fds_; |
| 56 | std::map<int, std::vector<GPollFD*>> fd_map_; |
| 57 | |
| 58 | // libchrome message loop bits. |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 59 | std::map<int, Watcher> watchers_; |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 60 | base::CancelableClosure timeout_closure_; |
| 61 | |
| 62 | State state_; |
| 63 | |
| 64 | base::WeakPtrFactory<GlibBridge> weak_ptr_factory_; |
| 65 | }; |
| 66 | |
| 67 | } // namespace glib_bridge |
| 68 | |
| 69 | #endif // GLIB_BRIDGE_GLIB_BRIDGE_H_ |