-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathglib_bridge.h
68 lines (50 loc) · 1.59 KB
/
glib_bridge.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A simple event bridge to allow glib programs to operate on a libchrome
// message loop.
#ifndef GLIB_BRIDGE_GLIB_BRIDGE_H_
#define GLIB_BRIDGE_GLIB_BRIDGE_H_
#include <glib.h>
#include <map>
#include <memory>
#include <vector>
#include <base/bind.h>
#include <base/cancelable_callback.h>
#include <base/files/file_descriptor_watcher_posix.h>
#include <base/message_loop/message_loop.h>
namespace glib_bridge {
struct GlibBridge {
public:
GlibBridge();
virtual ~GlibBridge();
private:
enum class State {
kPreparingIteration,
kWaitingForEvents,
kReadyForDispatch,
};
struct Watcher {
std::unique_ptr<base::FileDescriptorWatcher::Controller> reader;
std::unique_ptr<base::FileDescriptorWatcher::Controller> writer;
};
void PrepareIteration();
void Timeout();
void Dispatch();
void OnEvent(int fd, int flag);
// If we ever need to support multiple GMainContexts instead of just the
// default one then we can wrap a different context here. This is a weak
// pointer.
GMainContext* glib_context_;
// glib event and source bits.
int max_priority_ = -1;
std::vector<GPollFD> poll_fds_;
std::map<int, std::vector<GPollFD*>> fd_map_;
// libchrome message loop bits.
std::map<int, Watcher> watchers_;
base::CancelableClosure timeout_closure_;
State state_;
base::WeakPtrFactory<GlibBridge> weak_ptr_factory_;
};
} // namespace glib_bridge
#endif // GLIB_BRIDGE_GLIB_BRIDGE_H_