PolicyManager: Schedule re-evaluations based on variable usage.

This patch makes the EvaluationContext re-schedule a policy request
based on the variables used by that method, waiting for the Async
variables and polling the Poll variables on the suggested interval.

In order to use the main loop functions from the EvaluationContext
they were moved to its own file called event_loop.h.

BUG=chromium:340871
TEST=Unit tests added.

Change-Id: Ibfc52e4dfd12c5e1ef87b5ad9cc318f9821dcfdd
Reviewed-on: https://chromium-review.googlesource.com/190424
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/test_utils.cc b/test_utils.cc
index 31555d9..8391c8e 100644
--- a/test_utils.cc
+++ b/test_utils.cc
@@ -310,6 +310,27 @@
   unmounter_.reset(new ScopedFilesystemUnmounter(*mnt_path));
 }
 
+static gboolean RunGMainLoopOnTimeout(gpointer user_data) {
+  bool* timeout = static_cast<bool*>(user_data);
+  *timeout = true;
+  return FALSE;  // Remove timeout source
+}
+
+void RunGMainLoopUntil(int timeout_msec, base::Callback<bool()> terminate) {
+  GMainLoop* loop = g_main_loop_new(NULL, FALSE);
+  GMainContext* context = g_main_context_default();
+
+  bool timeout = false;
+  guint source_id = g_timeout_add(
+      timeout_msec, RunGMainLoopOnTimeout, &timeout);
+
+  while (!timeout && (terminate.is_null() || !terminate.Run()))
+    g_main_context_iteration(context, TRUE);
+
+  g_source_remove(source_id);
+  g_main_loop_unref(loop);
+}
+
 int RunGMainLoopMaxIterations(int iterations) {
   int result;
   GMainContext* context = g_main_context_default();