libchromeos: New chromeos::MessageLoop virtual interface.

This patch adds a MessageLoop interface to libchromeos, together with
a fake implementation used for testing and a glib main loop
implementation.

This new interface serves two purposes:
 * It provides a virtual interface that can be mocked or faked for testing.
 * It provides a new functionality to cancel a pending task before it runs,
   something that is not possible in the base message loop.
 * It provides a glib implementation of this interface that can be used by
   daemons calling directly glib main loop in many places, allowing us to
   progresivelly migrate those calls to this interface and then change the
   backend to a different implementation.

This patch includes the basic PostTask and PostDelayedTask methods. I/O
handling methods will be added later.

BUG=brillo:91
TEST=Added unittests.

Change-Id: Ibe62a1f460b2e044a7b515290c441bcac7bd602c
Reviewed-on: https://chromium-review.googlesource.com/276890
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/chromeos/message_loops/fake_message_loop.h b/chromeos/message_loops/fake_message_loop.h
new file mode 100644
index 0000000..143fe2a
--- /dev/null
+++ b/chromeos/message_loops/fake_message_loop.h
@@ -0,0 +1,68 @@
+// 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 LIBCHROMEOS_CHROMEOS_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_
+#define LIBCHROMEOS_CHROMEOS_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_
+
+#include <functional>
+#include <map>
+#include <queue>
+#include <utility>
+#include <vector>
+
+#include <base/test/simple_test_clock.h>
+#include <base/time/time.h>
+
+#include <chromeos/chromeos_export.h>
+#include <chromeos/message_loops/message_loop.h>
+
+namespace chromeos {
+
+// The FakeMessageLoop implements a message loop that doesn't block or wait for
+// time based tasks to be ready. The tasks are executed in the order they should
+// be executed in a real message loop implementation, but the time is advanced
+// to the time when the first task should be executed instead of blocking.
+// To keep a consistent notion of time for other classes, FakeMessageLoop
+// optionally updates a SimpleTestClock instance when it needs to advance the
+// clock.
+// This message loop implementation is useful for unittests.
+class CHROMEOS_EXPORT FakeMessageLoop : public MessageLoop {
+ public:
+  // Create a FakeMessageLoop optionally using a SimpleTestClock to update the
+  // time when Run() or RunOnce(true) are called and should block.
+  explicit FakeMessageLoop(base::SimpleTestClock* clock);
+  ~FakeMessageLoop() override = default;
+
+  MessageLoop::TaskId PostDelayedTask(const base::Closure &task,
+                                      base::TimeDelta delay) override;
+  bool CancelTask(TaskId task_id) override;
+  bool RunOnce(bool may_block) override;
+
+  // FakeMessageLoop methods:
+
+  // Return whether there are peding tasks. Useful to check that no
+  // callbacks were leaked.
+  bool PendingTasks();
+
+ private:
+  // Using std::greater<> for the priority_queue means that the top() of the
+  // queue is the lowest (earliest) time, and for the same time, the smallest
+  // TaskId. This determines the order in which the tasks will be fired.
+  std::priority_queue<
+      std::pair<base::Time, MessageLoop::TaskId>,
+      std::vector<std::pair<base::Time, MessageLoop::TaskId>>,
+      std::greater<std::pair<base::Time, MessageLoop::TaskId>>> fire_order_;
+  std::map<MessageLoop::TaskId, base::Closure> tasks_;
+
+  base::SimpleTestClock* test_clock_ = nullptr;
+  base::Time current_time_ = base::Time::FromDoubleT(1246996800.);
+
+  MessageLoop::TaskId last_id_ = kTaskIdNull;
+
+  DISALLOW_COPY_AND_ASSIGN(FakeMessageLoop);
+};
+
+}  // namespace chromeos
+
+#endif  // LIBCHROMEOS_CHROMEOS_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_