buffet: Add state change queue

When device state properties are updated, compile the change
notifications in StateChangeQueue object so they can be pulled
by Cloud Server adaptor and batch-update the device state on
the server.

When StateManager receives property updates, it notifies the
StateChangeQueue object of the changes via StateChangeQueueInterface.
The changes are kept in the queue along with the their time stamps
until they are pulled by Cloud Server adaper (using StateChangeQueue
method). At this point, the adapter would notify the server of
recorded device state changes and the StateChangeQueue is cleared,
ready to record new state updates.

BUG=chromium:415364
TEST=FEATURES=test emerge-link buffet

Change-Id: Ie99e2ada39aaf0164e08699d65153abfc5235a2f
Reviewed-on: https://chromium-review.googlesource.com/226014
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/states/state_manager.cc b/buffet/states/state_manager.cc
index 1171ea0..f31801f 100644
--- a/buffet/states/state_manager.cc
+++ b/buffet/states/state_manager.cc
@@ -12,10 +12,16 @@
 #include <chromeos/strings/string_utils.h>
 
 #include "buffet/states/error_codes.h"
+#include "buffet/states/state_change_queue_interface.h"
 #include "buffet/utils.h"
 
 namespace buffet {
 
+StateManager::StateManager(StateChangeQueueInterface* state_change_queue)
+    : state_change_queue_(state_change_queue) {
+  CHECK(state_change_queue_) << "State change queue not specified";
+}
+
 void StateManager::Startup() {
   LOG(INFO) << "Initializing StateManager.";
 
@@ -73,9 +79,9 @@
   return dict;
 }
 
-bool StateManager::SetPropertyValue(const std::string& full_property_name,
-                                    const chromeos::Any& value,
-                                    chromeos::ErrorPtr* error) {
+bool StateManager::UpdatePropertyValue(const std::string& full_property_name,
+                                       const chromeos::Any& value,
+                                       chromeos::ErrorPtr* error) {
   std::string package_name;
   std::string property_name;
   bool split = chromeos::string_utils::SplitAtFirst(
@@ -103,16 +109,38 @@
   return package->SetPropertyValue(property_name, value, error);
 }
 
+bool StateManager::SetPropertyValue(const std::string& full_property_name,
+                                    const chromeos::Any& value,
+                                    chromeos::ErrorPtr* error) {
+  if (!UpdatePropertyValue(full_property_name, value, error))
+    return false;
+
+  StateChange change;
+  change.timestamp = base::Time::Now();
+  change.property_set.emplace(full_property_name, value);
+  state_change_queue_->NotifyPropertiesUpdated(change);
+  return true;
+}
+
 bool StateManager::UpdateProperties(
     const chromeos::VariantDictionary& property_set,
     chromeos::ErrorPtr* error) {
   for (const auto& pair : property_set) {
-    if (!SetPropertyValue(pair.first, pair.second, error))
+    if (!UpdatePropertyValue(pair.first, pair.second, error))
       return false;
   }
+
+  StateChange change;
+  change.timestamp = base::Time::Now();
+  change.property_set = property_set;
+  state_change_queue_->NotifyPropertiesUpdated(change);
   return true;
 }
 
+std::vector<StateChange> StateManager::GetAndClearRecordedStateChanges() {
+  return state_change_queue_->GetAndClearRecordedStateChanges();
+}
+
 bool StateManager::LoadStateDefinition(const base::DictionaryValue& json,
                                        const std::string& category,
                                        chromeos::ErrorPtr* error) {