PolicyManager: New DevicePolicy provider.

This provider gives access to the members of the DevicePolicy
protobuf using the libpolicy to access it. The libpolicy doesn't
provide a way to get a notification when the policy was updated, but
that could be fixed on the future monitoring the file on disk. This
patch attempts to refresh the device policy every hour and updates
all the variables accordingly.

The variables exposed by this provider are only those used by the
update_engine code currently. If new variables need to be exposed
this can easily extended.

To achieve this, a new generic Variable class is introduce, named
AsyncCopyVariable, that allows you to Set or Unset the current value
of the variable and notify the subscribed observers while doing that.

BUG=chromium:358326
TEST=Unit tests added and pass.

Change-Id: Ieee6c9b33160f7dfe40c033db685a79d8fd57fe7
Reviewed-on: https://chromium-review.googlesource.com/194179
Reviewed-by: Alex Deymo <[email protected]>
Tested-by: Alex Deymo <[email protected]>
Commit-Queue: Alex Deymo <[email protected]>
diff --git a/policy_manager/generic_variables.h b/policy_manager/generic_variables.h
index 261b96c..9110c9a 100644
--- a/policy_manager/generic_variables.h
+++ b/policy_manager/generic_variables.h
@@ -123,6 +123,63 @@
   const T obj_;
 };
 
+// A Variable class to implement simple Async variables. It provides two methods
+// SetValue and UnsetValue to modify the current value of the variable and
+// notify the registered observers whenever the value changed.
+//
+// The type T needs to be copy-constructable, default-constructable and have an
+// operator== (to determine if the value changed), which makes this class
+// suitable for basic types.
+template<typename T>
+class AsyncCopyVariable : public Variable<T> {
+ public:
+  explicit AsyncCopyVariable(const std::string& name)
+      : Variable<T>(name, kVariableModeAsync), has_value_(false) {}
+
+  AsyncCopyVariable(const std::string& name, const T value)
+      : Variable<T>(name, kVariableModeAsync),
+        has_value_(true), value_(value) {}
+
+  void SetValue(const T& new_value) {
+    bool should_notify = !(has_value_ && new_value == value_);
+    value_ = new_value;
+    has_value_ = true;
+    if (should_notify)
+      this->NotifyValueChanged();
+  }
+
+  void UnsetValue() {
+    if (has_value_) {
+      has_value_ = false;
+      this->NotifyValueChanged();
+    }
+  }
+
+ protected:
+  friend class PmAsyncCopyVariableTest;
+  FRIEND_TEST(PmAsyncCopyVariableTest, ConstructorTest);
+  FRIEND_TEST(PmAsyncCopyVariableTest, SetValueTest);
+  FRIEND_TEST(PmAsyncCopyVariableTest, UnsetValueTest);
+
+  // Variable override.
+  virtual const T* GetValue(base::TimeDelta /* timeout */,
+                            std::string* errmsg) {
+    if (!has_value_) {
+      if (errmsg)
+        *errmsg = "No value set for " + this->GetName();
+      return nullptr;
+    }
+    return new T(value_);
+  }
+
+ private:
+  // Whether the variable has a value set.
+  bool has_value_;
+
+  // Copy of the object to be returned by GetValue().
+  T value_;
+};
+
 }  // namespace chromeos_policy_manager
 
 #endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_