PolicyManager: Initial support of async PolicyRequests.

This patch adds the new method PolicyManager::AsyncPolicyRequest
that takes a callback to be called once the policy request finishes.

If the given policy request returns kAskMeAgainLater, its evaluation
will be re-scheduled until it returns a given value. On this initial
patch, the evaluation is re-scheduled to happen after a fixed amount
of time instead of using the information collected on the
EvaluationContext.

BUG=chromium:340871
TEST=Unittest added.

Change-Id: If0e2888f26c379b806d811d52c4c3d4a8a6d8efb
Reviewed-on: https://chromium-review.googlesource.com/189636
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/policy_manager.cc b/policy_manager/policy_manager.cc
index e94df93..146c177 100644
--- a/policy_manager/policy_manager.cc
+++ b/policy_manager/policy_manager.cc
@@ -2,10 +2,13 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "update_engine/policy_manager/chromeos_policy.h"
 #include "update_engine/policy_manager/policy_manager.h"
+
+#include "update_engine/policy_manager/chromeos_policy.h"
 #include "update_engine/policy_manager/real_state.h"
 
+using base::Closure;
+
 namespace chromeos_policy_manager {
 
 template <typename T>
@@ -24,4 +27,28 @@
   return state_->Init();
 }
 
+void PolicyManager::RunFromMainLoop(const Closure& callback) {
+  Closure* callback_p = new base::Closure(callback);
+  g_idle_add_full(G_PRIORITY_DEFAULT,
+                  OnRanFromMainLoop,
+                  reinterpret_cast<gpointer>(callback_p),
+                  NULL);
+}
+
+void PolicyManager::RunFromMainLoopAfterTimeout(
+    const Closure& callback,
+    base::TimeDelta timeout) {
+  Closure* callback_p = new Closure(callback);
+  g_timeout_add_seconds(timeout.InSeconds(),
+                        OnRanFromMainLoop,
+                        reinterpret_cast<gpointer>(callback_p));
+}
+
+gboolean PolicyManager::OnRanFromMainLoop(gpointer user_data) {
+  Closure* callback_p = reinterpret_cast<Closure*>(user_data);
+  callback_p->Run();
+  delete callback_p;
+  return FALSE;  // Removes the source since a callback can only be called once.
+}
+
 }  // namespace chromeos_policy_manager