PolicyManager: Include the Polling interval for kVariableModePoll variables.
A kVariableModePoll variable requires to be polled to know the if the
value changed. This patch adds a new GetPollInterval method that
tells how often a kVariableModePoll variable should be polled. This
value will be used by the policy manager to trigger re-evaluations
of the same policy request.
BUG=chromium:341209
TEST=unit tests added and passes.
Change-Id: I9c982ec8106ce39a0bc0889df7686add131b3bea
Reviewed-on: https://chromium-review.googlesource.com/187703
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/generic_variables.h b/policy_manager/generic_variables.h
index 7f76981..48764f1 100644
--- a/policy_manager/generic_variables.h
+++ b/policy_manager/generic_variables.h
@@ -39,6 +39,9 @@
// GetValue() method is called.
CopyVariable(const std::string& name, VariableMode mode, const T& ref)
: Variable<T>(name, mode), ref_(ref) {}
+ CopyVariable(const std::string& name, const base::TimeDelta& poll_interval,
+ const T& ref)
+ : Variable<T>(name, poll_interval), ref_(ref) {}
protected:
friend class PmCopyVariableTest;
diff --git a/policy_manager/variable.h b/policy_manager/variable.h
index d4fba0c..373af7f 100644
--- a/policy_manager/variable.h
+++ b/policy_manager/variable.h
@@ -49,16 +49,42 @@
return mode_;
}
+ // For VariableModePoll variables, it returns the polling interval of this
+ // variable. In other case, it returns 0.
+ base::TimeDelta GetPollInterval() const {
+ return poll_interval_;
+ }
+
protected:
+ // Creates a BaseVariable using the default polling interval (5 minutes).
BaseVariable(const std::string& name, VariableMode mode)
- : name_(name), mode_(mode) {}
+ : BaseVariable(name, mode,
+ base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {}
+
+ // Creates a BaseVariable with mode kVariableModePoll and the provided
+ // polling interval.
+ BaseVariable(const std::string& name, base::TimeDelta poll_interval)
+ : BaseVariable(name, kVariableModePoll, poll_interval) {}
private:
+ BaseVariable(const std::string& name, VariableMode mode,
+ base::TimeDelta poll_interval)
+ : name_(name), mode_(mode),
+ poll_interval_(mode == kVariableModePoll ?
+ poll_interval : base::TimeDelta()) {}
+
+ // The default PollInterval in minutes.
+ static constexpr int kDefaultPollMinutes = 5;
+
// The variable's name as a string.
const std::string name_;
// The variable's mode.
const VariableMode mode_;
+
+ // The variable's polling interval for VariableModePoll variable and 0 for
+ // other modes.
+ const base::TimeDelta poll_interval_;
};
// Interface to a Policy Manager variable of a given type. Implementation
@@ -82,6 +108,9 @@
Variable(const std::string& name, VariableMode mode)
: BaseVariable(name, mode) {}
+ Variable(const std::string& name, const base::TimeDelta& poll_interval)
+ : BaseVariable(name, poll_interval) {}
+
// Gets the current value of the variable. The current value is copied to a
// new object and returned. The caller of this method owns the object and
// should delete it.
diff --git a/policy_manager/variable_unittest.cc b/policy_manager/variable_unittest.cc
index e346c0d..5d3b8d9 100644
--- a/policy_manager/variable_unittest.cc
+++ b/policy_manager/variable_unittest.cc
@@ -6,6 +6,7 @@
#include "update_engine/policy_manager/variable.h"
+using base::TimeDelta;
using std::string;
namespace chromeos_policy_manager {
@@ -16,6 +17,8 @@
public:
DefaultVariable(const string& name, VariableMode mode)
: Variable<T>(name, mode) {}
+ DefaultVariable(const string& name, const TimeDelta& poll_interval)
+ : Variable<T>(name, poll_interval) {}
virtual ~DefaultVariable() {}
protected:
@@ -40,4 +43,17 @@
EXPECT_EQ(other_var.GetMode(), kVariableModePoll);
}
+TEST(PmBaseVariableTest, DefaultPollIntervalTest) {
+ DefaultVariable<int> const_var("const_var", kVariableModeConst);
+ EXPECT_EQ(const_var.GetPollInterval(), TimeDelta());
+ DefaultVariable<int> poll_var("poll_var", kVariableModePoll);
+ EXPECT_EQ(poll_var.GetPollInterval(), TimeDelta::FromMinutes(5));
+}
+
+TEST(PmBaseVariableTest, GetPollIntervalTest) {
+ DefaultVariable<int> var("var", TimeDelta::FromMinutes(3));
+ EXPECT_EQ(var.GetMode(), kVariableModePoll);
+ EXPECT_EQ(var.GetPollInterval(), TimeDelta::FromMinutes(3));
+}
+
} // namespace chromeos_policy_manager