buffet: Implement exponential backoff for cloud requests

Simplified logic of DoCloudRequest() method in buffet to de-couple
the multiple levels of callbacks and retries. Also added support for
exponential backoff on request failures.

In the process made RefreshAccessToken and GetDeviceInfo methods
fully asynchronous.

BUG=brillo:955
TEST=`FEATURES=test emerge-link buffet`
     `test_that -b link 100.96.49.59 e:buffet_.*`

Change-Id: Ieeb2fa42ea25f15841bad5c6c09c6c9990f96943
Reviewed-on: https://chromium-review.googlesource.com/280833
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/manager.h b/buffet/manager.h
index 2f8fd66..4cee9cc 100644
--- a/buffet/manager.h
+++ b/buffet/manager.h
@@ -39,9 +39,13 @@
 class StateManager;
 
 template<typename... Types>
-using DBusMethodResponse =
+using DBusMethodResponsePtr =
     std::unique_ptr<chromeos::dbus_utils::DBusMethodResponse<Types...>>;
 
+template<typename... Types>
+using DBusMethodResponse =
+    chromeos::dbus_utils::DBusMethodResponse<Types...>;
+
 // The Manager is responsible for global state of Buffet.  It exposes
 // interfaces which affect the entire device such as device registration and
 // device state.
@@ -67,9 +71,10 @@
 
  private:
   // DBus methods:
-  void CheckDeviceRegistered(DBusMethodResponse<std::string> response) override;
-  void GetDeviceInfo(DBusMethodResponse<std::string> response) override;
-  void RegisterDevice(DBusMethodResponse<std::string> response,
+  void CheckDeviceRegistered(
+      DBusMethodResponsePtr<std::string> response) override;
+  void GetDeviceInfo(DBusMethodResponsePtr<std::string> response) override;
+  void RegisterDevice(DBusMethodResponsePtr<std::string> response,
                       const std::string& ticket_id) override;
   bool UpdateDeviceInfo(chromeos::ErrorPtr* error,
                         const std::string& in_name,
@@ -81,18 +86,17 @@
                            const std::string& api_key,
                            const std::string& oauth_url,
                            const std::string& service_url) override;
-  void UpdateState(DBusMethodResponse<> response,
+  void UpdateState(DBusMethodResponsePtr<> response,
                    const chromeos::VariantDictionary& property_set) override;
   bool GetState(chromeos::ErrorPtr* error, std::string* state) override;
-  void AddCommand(DBusMethodResponse<std::string> response,
+  void AddCommand(DBusMethodResponsePtr<std::string> response,
                   const std::string& json_command,
                   const std::string& in_user_role) override;
-  void GetCommand(DBusMethodResponse<std::string> response,
+  void GetCommand(DBusMethodResponsePtr<std::string> response,
                   const std::string& id) override;
-  void SetCommandVisibility(
-      std::unique_ptr<chromeos::dbus_utils::DBusMethodResponse<>> response,
-      const std::vector<std::string>& in_names,
-      const std::string& in_visibility) override;
+  void SetCommandVisibility(DBusMethodResponsePtr<> response,
+                            const std::vector<std::string>& in_names,
+                            const std::string& in_visibility) override;
   std::string TestMethod(const std::string& message) override;
   bool EnableWiFiBootstrapping(
       chromeos::ErrorPtr* error,
@@ -105,6 +109,13 @@
       const chromeos::VariantDictionary& in_options) override;
   bool DisableGCDBootstrapping(chromeos::ErrorPtr* error) override;
 
+  void OnGetDeviceInfoSuccess(
+      const std::shared_ptr<DBusMethodResponse<std::string>>& response,
+      const base::DictionaryValue& device_info);
+  void OnGetDeviceInfoError(
+      const std::shared_ptr<DBusMethodResponse<std::string>>& response,
+      const chromeos::Error* error);
+
   void StartPrivet(const privetd::Manager::Options& options,
                    chromeos::dbus_utils::AsyncEventSequencer* sequencer);