buffet: StartDevice automatically after a short delay

BUG=brillo:15
TEST=buffet_Registration confirms that we StartDevice
automatically shortly after registering.

Change-Id: I6c903e67f0556c7db817e126c72711f7d781592d
Reviewed-on: https://chromium-review.googlesource.com/249441
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index 31fb7b6..abe130b 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -8,6 +8,7 @@
 #include <utility>
 #include <vector>
 
+#include <base/bind.h>
 #include <base/json/json_writer.h>
 #include <base/message_loop/message_loop.h>
 #include <base/values.h>
@@ -238,6 +239,17 @@
   return storage_->Save(&dict);
 }
 
+void DeviceRegistrationInfo::ScheduleStartDevice(const base::TimeDelta& later) {
+  base::MessageLoop* current = base::MessageLoop::current();
+  if (!current)
+    return;  // Assume we're in unittests
+  current->PostDelayedTask(
+      FROM_HERE,
+      base::Bind(&DeviceRegistrationInfo::StartDevice,
+                 weak_factory_.GetWeakPtr(), nullptr),
+      later);
+}
+
 bool DeviceRegistrationInfo::CheckRegistration(chromeos::ErrorPtr* error) {
   LOG(INFO) << "Checking device registration record.";
   if (refresh_token_.empty() ||
@@ -447,6 +459,10 @@
                              base::TimeDelta::FromSeconds(expires_in);
 
   Save();
+
+  // We're going to respond with our success immediately and we'll StartDevice
+  // shortly after.
+  ScheduleStartDevice(base::TimeDelta::FromSeconds(0));
   return device_id_;
 }