PolicyManager: Add an base Variable class without type.
The Variable<T> is a parametric type that requires to know the type T
of the exported variable. This new BaseVariable class isn't
parametric and allows to keep a list of variable pointers on the
EvaluationContext to cache their provided results and dump the names
of those variables.
BUG=chromium:338590
TEST=unittest passes.
Change-Id: I1677e3975d44575ed12f35c36381101d4379c5fc
Reviewed-on: https://chromium-review.googlesource.com/184428
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/SConstruct b/SConstruct
index df6e8d8..0d121d5 100644
--- a/SConstruct
+++ b/SConstruct
@@ -334,6 +334,7 @@
payload_state_unittest.cc
policy_manager/generic_variables_unittest.cc
policy_manager/random_provider_unittest.cc
+ policy_manager/variable_unittest.cc
postinstall_runner_action_unittest.cc
prefs_unittest.cc
simple_key_value_store_unittest.cc
diff --git a/policy_manager/generic_variables.h b/policy_manager/generic_variables.h
index 32ea1b9..87ba066 100644
--- a/policy_manager/generic_variables.h
+++ b/policy_manager/generic_variables.h
@@ -37,7 +37,8 @@
// Creates the variable returning copies of the passed |obj| reference. The
// reference to this object is kept and it should be available whenever the
// GetValue() method is called.
- CopyVariable(const T& ref) : ref_(ref) {};
+ CopyVariable(const std::string& name, const T& ref)
+ : Variable<T>(name), ref_(ref) {}
protected:
friend class PmCopyVariableTest;
diff --git a/policy_manager/generic_variables_unittest.cc b/policy_manager/generic_variables_unittest.cc
index 14b40d3..15ea3d7 100644
--- a/policy_manager/generic_variables_unittest.cc
+++ b/policy_manager/generic_variables_unittest.cc
@@ -13,7 +13,7 @@
TEST(PmCopyVariableTest, SimpleTest) {
int obj_int = 5;
- CopyVariable<int> var(obj_int);
+ CopyVariable<int> var("var", obj_int);
string errmsg = "Nope";
@@ -50,7 +50,7 @@
ASSERT_FALSE(obj.copied_);
string errmsg;
- CopyVariable<ConstructorTestClass> var(obj);
+ CopyVariable<ConstructorTestClass> var("var", obj);
const ConstructorTestClass* value =
var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
EXPECT_NE(value, static_cast<void*>(NULL));
diff --git a/policy_manager/random_provider.cc b/policy_manager/random_provider.cc
index 73caa04..8359c44 100644
--- a/policy_manager/random_provider.cc
+++ b/policy_manager/random_provider.cc
@@ -25,7 +25,8 @@
// The random variable class implementation.
class RandomVariable : public Variable<uint64_t> {
public:
- RandomVariable(FILE* fp) : fp_(fp) {}
+ RandomVariable(const string& name, FILE* fp)
+ : Variable<uint64_t>(name), fp_(fp) {}
virtual ~RandomVariable() {}
protected:
@@ -65,7 +66,7 @@
FILE* fp = fopen(kRandomDevice, "r");
if (!fp)
return false;
- var_random_seed = new RandomVariable(fp);
+ var_random_seed = new RandomVariable("random_seed", fp);
return true;
}
diff --git a/policy_manager/variable.h b/policy_manager/variable.h
index 22039c0..9b933d7 100644
--- a/policy_manager/variable.h
+++ b/policy_manager/variable.h
@@ -12,11 +12,30 @@
namespace chromeos_policy_manager {
-// Interface to a Policy Manager variable. Implementation internals are hidden
-// as protected members, since policies should not be using them directly.
-template<typename T>
-class Variable {
+// This class is a base class with the common functionality that doesn't
+// deppend on the variable's type, implemented by all the variables.
+class BaseVariable {
public:
+ BaseVariable(const std::string& name) : name_(name) {}
+ virtual ~BaseVariable() {}
+
+ // Returns the variable name as a string.
+ virtual const std::string& GetName() {
+ return name_;
+ }
+
+ private:
+ // The variable's name as a string.
+ const std::string name_;
+};
+
+// Interface to a Policy Manager variable of a given type. Implementation
+// internals are hidden as protected members, since policies should not be
+// using them directly.
+template<typename T>
+class Variable : public BaseVariable {
+ public:
+ Variable(const std::string& name) : BaseVariable(name) {}
virtual ~Variable() {}
protected:
diff --git a/policy_manager/variable_unittest.cc b/policy_manager/variable_unittest.cc
new file mode 100644
index 0000000..f4aa084
--- /dev/null
+++ b/policy_manager/variable_unittest.cc
@@ -0,0 +1,35 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <gtest/gtest.h>
+
+#include "policy_manager/variable.h"
+
+using std::string;
+
+namespace chromeos_policy_manager {
+
+// Variable class that returns a value constructed with the default value.
+template <typename T>
+class DefaultVariable : public Variable<T> {
+ public:
+ DefaultVariable(const string& name) : Variable<T>(name) {}
+ virtual ~DefaultVariable() {}
+
+ protected:
+ virtual const T* GetValue(base::TimeDelta /* timeout */,
+ string* /* errmsg */) {
+ return new T();
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DefaultVariable);
+};
+
+TEST(PmBaseVariableTest, GetNameTest) {
+ DefaultVariable<int> var("var");
+ EXPECT_EQ(var.GetName(), string("var"));
+}
+
+} // namespace chromeos_policy_manager