buffet: Push state changed to the server immediately

Current delay for polling_period_ms is not acceptable. We'd like to
see state change on server immediately.
Simple throttling to 1 update per seconds.

BUG=brillo:1124
TEST=`FEATURES=test emerge-gizmo buffet`

Change-Id: Id042e1a6445850d024849e689b8d305c5c3af30f
Reviewed-on: https://chromium-review.googlesource.com/273367
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
Trybot-Ready: Vitaly Buka <vitalybuka@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index f31495c..15d3dc4 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -123,6 +123,9 @@
   command_manager_->AddOnCommandDefChanged(
       base::Bind(&DeviceRegistrationInfo::OnCommandDefsChanged,
                  weak_factory_.GetWeakPtr()));
+  state_manager_->AddOnChangedCallback(
+      base::Bind(&DeviceRegistrationInfo::OnStateChanged,
+                 weak_factory_.GetWeakPtr()));
 }
 
 DeviceRegistrationInfo::~DeviceRegistrationInfo() = default;
@@ -836,14 +839,6 @@
                  base::Bind(&DeviceRegistrationInfo::PublishCommands,
                             base::Unretained(this)),
                             base::Bind(&IgnoreCloudError)));
-  // TODO(antonm): Use better trigger: when StateManager registers new updates,
-  // it should call closure which will post a task, probably with some
-  // throttling, to publish state updates.
-  state_push_timer_.Start(
-      FROM_HERE,
-      base::TimeDelta::FromMilliseconds(config_->polling_period_ms()),
-      base::Bind(&DeviceRegistrationInfo::PublishStateUpdates,
-                 base::Unretained(this)));
 }
 
 void DeviceRegistrationInfo::PublishCommands(const base::ListValue& commands) {
@@ -942,6 +937,15 @@
                        base::Bind(&IgnoreCloudError));
 }
 
+void DeviceRegistrationInfo::OnStateChanged() {
+  VLOG(1) << "StateChanged notification received";
+  if (!HaveRegistrationCredentials(nullptr))
+    return;
+
+  // TODO(vitalybuka): Integrate BackoffEntry.
+  PublishStateUpdates();
+}
+
 void DeviceRegistrationInfo::OnConnected(const std::string& channel_name) {
   LOG(INFO) << "Notification channel successfully established over "
             << channel_name;
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h
index 1314d5f..674bbd6 100644
--- a/buffet/device_registration_info.h
+++ b/buffet/device_registration_info.h
@@ -224,6 +224,7 @@
 
   // Callback called when command definitions are changed to re-publish new CDD.
   void OnCommandDefsChanged();
+  void OnStateChanged();
 
   // Overrides from NotificationDelegate
   void OnConnected(const std::string& channel_name) override;
@@ -251,7 +252,6 @@
   RegistrationStatus registration_status_{RegistrationStatus::kUnconfigured};
 
   base::RepeatingTimer<DeviceRegistrationInfo> command_poll_timer_;
-  base::RepeatingTimer<DeviceRegistrationInfo> state_push_timer_;
 
   std::vector<OnRegistrationChangedCallback> on_registration_changed_;