power: wait briefly before executing base.reboot

If the device reboots before buffet has a change to indicate to the
cloud that the command has been completed, the device will get stuck in
a reboot loop until the command expires.

This is a work-around since a more correct solution isn't feasible in the short
term.

BUG=chrome-os-partner:43342
TEST=manually executed base.reboot command

Change-Id: Iafc162729b4742e1ead7365a3f1fe4e693c01b05
Reviewed-on: https://chromium-review.googlesource.com/290046
Reviewed-by: Dan Erat <derat@chromium.org>
Commit-Queue: Aaron Kemp <kemp@google.com>
Tested-by: Aaron Kemp <kemp@google.com>
(cherry picked from commit fadb6a8c02737e5d8a60391a08ddb2a81aac123a)
Reviewed-on: https://chromium-review.googlesource.com/290376
Reviewed-by: Aaron Kemp <kemp@google.com>
diff --git a/power_manager/powerd/buffet/command_handlers.cc b/power_manager/powerd/buffet/command_handlers.cc
index 63a392d..38e8187 100644
--- a/power_manager/powerd/buffet/command_handlers.cc
+++ b/power_manager/powerd/buffet/command_handlers.cc
@@ -9,6 +9,8 @@
 #include <base/callback.h>
 #include <base/macros.h>
 #include <base/memory/weak_ptr.h>
+#include <base/message_loop/message_loop.h>
+#include <base/time/time.h>
 
 #include "buffet/dbus-proxies.h"
 
@@ -20,6 +22,10 @@
 
 namespace {
 
+// This is the number of seconds to wait before rebooting to allow the command
+// status to make it to the cloud.
+static const int kRebootDelayInSeconds = 3;
+
 // The GCD command name powerd handles.
 const char kBaseRebootCommand[] = "base.reboot";
 
@@ -50,8 +56,15 @@
         command->name() == kBaseRebootCommand) {
       // Right now powerd handles only 'base.reboot' command and ignores
       // everything else.
-      if (command->Done(nullptr))
-        reboot_callback_.Run();
+      if (command->Done(nullptr)) {
+        // Delay the reboot by a small amount to help mitigate a race where the
+        // cloud status doesn't get updated and we get into a reboot loop.
+        //
+        // TODO(kemp): This should be removed once brbug.com/1265 is fixed.
+        base::MessageLoop::current()->PostDelayedTask(
+            FROM_HERE, reboot_callback_, base::TimeDelta::FromSeconds(
+                kRebootDelayInSeconds));
+      }
     }
 }