update_manager: Make Prefs Variable async.

The update_engine prefs, while stored in disk, are private daemon
values changed by the daemon only. There was a 5 minutes delay between
changing this value and the update policy checking it again, and there
was a log spam every 5 minutes due to policy re-evaluations.

This patch makes these Prefs-based variables async by implementing an
observer pattern in the PrefsInterface and makes these variables async.

Bug: chromium:367333
Test: Added uniittest. No more log spam every 5 minutes.

Change-Id: I8b3f7072cc87515972c9f5b1ddcc54b865ffe238
diff --git a/prefs.h b/prefs.h
index 5ed56c6..1bcfef8 100644
--- a/prefs.h
+++ b/prefs.h
@@ -17,9 +17,12 @@
 #ifndef UPDATE_ENGINE_PREFS_H_
 #define UPDATE_ENGINE_PREFS_H_
 
+#include <map>
 #include <string>
+#include <vector>
 
 #include <base/files/file_path.h>
+
 #include "gtest/gtest_prod.h"  // for FRIEND_TEST
 #include "update_engine/prefs_interface.h"
 
@@ -31,7 +34,7 @@
 
 class Prefs : public PrefsInterface {
  public:
-  Prefs() {}
+  Prefs() = default;
 
   // Initializes the store by associating this object with |prefs_dir|
   // as the preference store directory. Returns true on success, false
@@ -39,16 +42,21 @@
   bool Init(const base::FilePath& prefs_dir);
 
   // PrefsInterface methods.
-  bool GetString(const std::string& key, std::string* value) override;
+  bool GetString(const std::string& key, std::string* value) const override;
   bool SetString(const std::string& key, const std::string& value) override;
-  bool GetInt64(const std::string& key, int64_t* value) override;
+  bool GetInt64(const std::string& key, int64_t* value) const override;
   bool SetInt64(const std::string& key, const int64_t value) override;
-  bool GetBoolean(const std::string& key, bool* value) override;
+  bool GetBoolean(const std::string& key, bool* value) const override;
   bool SetBoolean(const std::string& key, const bool value) override;
 
-  bool Exists(const std::string& key) override;
+  bool Exists(const std::string& key) const override;
   bool Delete(const std::string& key) override;
 
+  void AddObserver(const std::string& key,
+                   ObserverInterface* observer) override;
+  void RemoveObserver(const std::string& key,
+                      ObserverInterface* observer) override;
+
  private:
   FRIEND_TEST(PrefsTest, GetFileNameForKey);
   FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
@@ -56,11 +64,15 @@
 
   // Sets |filename| to the full path to the file containing the data
   // associated with |key|. Returns true on success, false otherwise.
-  bool GetFileNameForKey(const std::string& key, base::FilePath* filename);
+  bool GetFileNameForKey(const std::string& key,
+                         base::FilePath* filename) const;
 
   // Preference store directory.
   base::FilePath prefs_dir_;
 
+  // The registered observers watching for changes.
+  std::map<std::string, std::vector<ObserverInterface*>> observers_;
+
   DISALLOW_COPY_AND_ASSIGN(Prefs);
 };