summaryrefslogtreecommitdiff
path: root/brillo/message_loops/glib_message_loop.h
blob: 50fe2ce5760dd053d2aba54721ef85d4f0c31c6b (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2015 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.

#ifndef LIBBRILLO_BRILLO_MESSAGE_LOOPS_GLIB_MESSAGE_LOOP_H_
#define LIBBRILLO_BRILLO_MESSAGE_LOOPS_GLIB_MESSAGE_LOOP_H_

#include <map>
#include <memory>

#include <base/location.h>
#include <base/time/time.h>
#include <glib.h>

#include <brillo/brillo_export.h>
#include <brillo/message_loops/message_loop.h>

namespace brillo {

class BRILLO_EXPORT GlibMessageLoop : public MessageLoop {
 public:
  GlibMessageLoop();
  ~GlibMessageLoop() override;

  // MessageLoop overrides.
  TaskId PostDelayedTask(const tracked_objects::Location& from_here,
                         const base::Closure& task,
                         base::TimeDelta delay) override;
  using MessageLoop::PostDelayedTask;
  TaskId WatchFileDescriptor(const tracked_objects::Location& from_here,
                             int fd,
                             WatchMode mode,
                             bool persistent,
                             const base::Closure& task) override;
  using MessageLoop::WatchFileDescriptor;
  bool CancelTask(TaskId task_id) override;
  bool RunOnce(bool may_block) override;
  void Run() override;
  void BreakLoop() override;

 private:
  // Called by the GLib's main loop when is time to call the callback scheduled
  // with Post*Task(). The pointer to the callback passed when scheduling it is
  // passed to this function as a gpointer on |user_data|.
  static gboolean OnRanPostedTask(gpointer user_data);

  // Called by the GLib's main loop when the watched source |source| is
  // ready to perform the operation given in |condition| without blocking.
  static gboolean OnWatchedFdReady(GIOChannel *source,
                                   GIOCondition condition,
                                   gpointer user_data);

  // Called by the GLib's main loop when the scheduled callback is removed due
  // to it being executed or canceled.
  static void DestroyPostedTask(gpointer user_data);

  // Return a new unused task_id.
  TaskId NextTaskId();

  GMainLoop* loop_;

  struct ScheduledTask {
    // A pointer to this GlibMessageLoop so we can remove the Task from the
    // glib callback.
    GlibMessageLoop* loop;
    tracked_objects::Location location;

    MessageLoop::TaskId task_id;
    guint source_id;
    bool persistent;
    base::Closure closure;
  };

  std::map<MessageLoop::TaskId, ScheduledTask*> tasks_;

  MessageLoop::TaskId last_id_ = kTaskIdNull;

  DISALLOW_COPY_AND_ASSIGN(GlibMessageLoop);
};

}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_MESSAGE_LOOPS_GLIB_MESSAGE_LOOP_H_