blob: 28a9fb22a2cdbc804a5d80dfff8d64f94e52fc53 [file] [log] [blame]
Alex Deymo8c499142014-01-02 19:33:34 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H
7
Alex Deymoa07a1232014-02-25 14:19:50 -08008#include <algorithm>
9#include <list>
Alex Deymo8c499142014-01-02 19:33:34 -080010#include <string>
11
12#include <base/time.h>
Alex Deymoca0aaa62014-01-06 10:39:58 -080013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Alex Deymo8c499142014-01-02 19:33:34 -080014
15namespace chromeos_policy_manager {
16
Alex Deymo0e433692014-02-20 07:23:03 -080017// The VariableMode specifies important behavior of the variable in terms of
18// whether, how and when the value of the variable changes.
19enum VariableMode {
20 // Const variables never changes during the life of a policy request, so the
21 // EvaluationContext caches the value even between different evaluations of
22 // the same policy request.
23 kVariableModeConst,
24
25 // Poll variables, or synchronous variables, represent a variable with a value
26 // that can be queried at any time, but it is not known when the value
27 // changes on the source of information. In order to detect if the value of
28 // the variable changes, it has to be queried again.
29 kVariableModePoll,
30
31 // Async variables are able to produce a signal or callback whenever the
32 // value changes. This means that it's not required to poll the value to
33 // detect when it changes, instead, you should register an observer to get
34 // a notification when that happens.
35 kVariableModeAsync,
36};
37
Alex Deymo391ad9f2014-01-29 14:36:20 -080038// This class is a base class with the common functionality that doesn't
39// deppend on the variable's type, implemented by all the variables.
40class BaseVariable {
Alex Deymo8c499142014-01-02 19:33:34 -080041 public:
Alex Deymoa07a1232014-02-25 14:19:50 -080042 // Interface for observing changes on variable value.
43 class Observer {
44 public:
45 virtual ~Observer() {}
46
47 // Called when the value on the variable changes.
48 virtual void ValueChanged(BaseVariable* variable) = 0;
49 };
50
Alex Deymo391ad9f2014-01-29 14:36:20 -080051 virtual ~BaseVariable() {}
52
53 // Returns the variable name as a string.
Alex Deymo0e433692014-02-20 07:23:03 -080054 const std::string& GetName() const {
Alex Deymo391ad9f2014-01-29 14:36:20 -080055 return name_;
56 }
57
Alex Deymo0e433692014-02-20 07:23:03 -080058 // Returns the variable mode.
59 VariableMode GetMode() const {
60 return mode_;
61 }
62
Alex Deymoa8033932014-02-25 10:33:13 -080063 // For VariableModePoll variables, it returns the polling interval of this
64 // variable. In other case, it returns 0.
65 base::TimeDelta GetPollInterval() const {
66 return poll_interval_;
67 }
68
Alex Deymoa07a1232014-02-25 14:19:50 -080069 // Adds and removes observers for value changes on the variable. This only
70 // works for kVariableAsync variables since the other modes don't track value
71 // changes. Adding the same observer twice has no effect.
72 virtual void AddObserver(BaseVariable::Observer* observer) {
73 if (std::find(observer_list_.begin(), observer_list_.end(), observer) ==
74 observer_list_.end()) {
75 observer_list_.push_back(observer);
76 }
77 }
78
79 virtual void RemoveObserver(BaseVariable::Observer* observer) {
80 observer_list_.remove(observer);
81 }
82
Alex Deymo0e433692014-02-20 07:23:03 -080083 protected:
Alex Deymoa8033932014-02-25 10:33:13 -080084 // Creates a BaseVariable using the default polling interval (5 minutes).
Alex Deymo0e433692014-02-20 07:23:03 -080085 BaseVariable(const std::string& name, VariableMode mode)
Alex Deymoa8033932014-02-25 10:33:13 -080086 : BaseVariable(name, mode,
87 base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {}
88
89 // Creates a BaseVariable with mode kVariableModePoll and the provided
90 // polling interval.
91 BaseVariable(const std::string& name, base::TimeDelta poll_interval)
92 : BaseVariable(name, kVariableModePoll, poll_interval) {}
Alex Deymo0e433692014-02-20 07:23:03 -080093
Alex Deymoa07a1232014-02-25 14:19:50 -080094 // Calls ValueChanged on all the observers.
95 void NotifyValueChanged() {
96 for (auto& observer : observer_list_)
97 observer->ValueChanged(this);
98 }
99
Alex Deymo391ad9f2014-01-29 14:36:20 -0800100 private:
Alex Deymoa07a1232014-02-25 14:19:50 -0800101 friend class PmBaseVariableTest;
102 FRIEND_TEST(PmBaseVariableTest, RepeatedObserverTest);
103 FRIEND_TEST(PmBaseVariableTest, NotifyValueChangedTest);
104
Alex Deymoa8033932014-02-25 10:33:13 -0800105 BaseVariable(const std::string& name, VariableMode mode,
106 base::TimeDelta poll_interval)
107 : name_(name), mode_(mode),
108 poll_interval_(mode == kVariableModePoll ?
109 poll_interval : base::TimeDelta()) {}
110
111 // The default PollInterval in minutes.
112 static constexpr int kDefaultPollMinutes = 5;
113
Alex Deymo391ad9f2014-01-29 14:36:20 -0800114 // The variable's name as a string.
115 const std::string name_;
Alex Deymo0e433692014-02-20 07:23:03 -0800116
117 // The variable's mode.
118 const VariableMode mode_;
Alex Deymoa8033932014-02-25 10:33:13 -0800119
120 // The variable's polling interval for VariableModePoll variable and 0 for
121 // other modes.
122 const base::TimeDelta poll_interval_;
Alex Deymoa07a1232014-02-25 14:19:50 -0800123
124 // The list of value changes observers.
125 std::list<BaseVariable::Observer*> observer_list_;
Alex Deymo391ad9f2014-01-29 14:36:20 -0800126};
127
128// Interface to a Policy Manager variable of a given type. Implementation
129// internals are hidden as protected members, since policies should not be
130// using them directly.
131template<typename T>
132class Variable : public BaseVariable {
133 public:
Alex Deymo8c499142014-01-02 19:33:34 -0800134 virtual ~Variable() {}
135
136 protected:
Alex Deymo23949d42014-02-05 15:20:59 -0800137 // Only allow to get values through the EvaluationContext class and not
138 // directly from the variable.
139 friend class EvaluationContext;
140
Alex Deymobb019fe2014-02-03 20:12:17 -0800141 friend class PmRealRandomProviderTest;
142 FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues);
Gilad Arnold55f39b72014-01-28 12:51:45 -0800143 friend class PmRealShillProviderTest;
144 FRIEND_TEST(PmRealShillProviderTest, DefaultValues);
Alex Deymoca0aaa62014-01-06 10:39:58 -0800145
Alex Deymo0e433692014-02-20 07:23:03 -0800146 Variable(const std::string& name, VariableMode mode)
147 : BaseVariable(name, mode) {}
148
Alex Deymoa8033932014-02-25 10:33:13 -0800149 Variable(const std::string& name, const base::TimeDelta& poll_interval)
150 : BaseVariable(name, poll_interval) {}
151
Alex Deymo8c499142014-01-02 19:33:34 -0800152 // Gets the current value of the variable. The current value is copied to a
153 // new object and returned. The caller of this method owns the object and
154 // should delete it.
155 //
156 // In case of and error getting the current value or the |timeout| timeout is
157 // exceeded, a NULL value is returned and the |errmsg| is set.
158 //
159 // The caller can pass a NULL value for |errmsg|, in which case the error
160 // message won't be set.
161 virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0;
162};
163
164} // namespace chromeos_policy_manager
165
166#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H