blob: dc6eb308782529b923ac3f1cd8ec6b4c34526e8d [file] [log] [blame]
Alex Deymo53556ec2014-03-17 10:05:57 -07001// Copyright (c) 2014 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#include "update_engine/policy_manager/event_loop.h"
6
7#include <cmath>
8
9using base::Closure;
10
11namespace {
12
13// Called by the GLib's main loop when is time to call the callback scheduled
14// with RunFromMainLopp() and similar functions. The pointer to the callback
15// passed when scheduling it is passed to this functions as a gpointer on
16// |user_data|.
17gboolean OnRanFromMainLoop(gpointer user_data) {
18 Closure* callback_p = reinterpret_cast<Closure*>(user_data);
19 callback_p->Run();
20 delete callback_p;
21 return FALSE; // Removes the source since a callback can only be called once.
22}
23
24} // namespace
25
26namespace chromeos_policy_manager {
27
28EventId RunFromMainLoop(const Closure& callback) {
29 Closure* callback_p = new Closure(callback);
30 return g_idle_add_full(G_PRIORITY_DEFAULT,
31 OnRanFromMainLoop,
32 reinterpret_cast<gpointer>(callback_p),
33 NULL);
34}
35
36EventId RunFromMainLoopAfterTimeout(
37 const Closure& callback,
38 base::TimeDelta timeout) {
39 Closure* callback_p = new Closure(callback);
40 return g_timeout_add_seconds(static_cast<guint>(ceil(timeout.InSecondsF())),
41 OnRanFromMainLoop,
42 reinterpret_cast<gpointer>(callback_p));
43}
44
45bool CancelMainLoopEvent(EventId event) {
46 if (event != kEventIdNull)
47 return g_source_remove(event);
48 return false;
49}
50
51} // namespace chromeos_policy_manager