Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 1 | // 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 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame^] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H_ |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 7 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <list> |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include <base/time.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 14 | |
| 15 | namespace chromeos_policy_manager { |
| 16 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 17 | // The VariableMode specifies important behavior of the variable in terms of |
| 18 | // whether, how and when the value of the variable changes. |
| 19 | enum 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 Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 38 | // 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. |
| 40 | class BaseVariable { |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 41 | public: |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 42 | // 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 Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 51 | virtual ~BaseVariable() {} |
| 52 | |
| 53 | // Returns the variable name as a string. |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 54 | const std::string& GetName() const { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 55 | return name_; |
| 56 | } |
| 57 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 58 | // Returns the variable mode. |
| 59 | VariableMode GetMode() const { |
| 60 | return mode_; |
| 61 | } |
| 62 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 63 | // 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 Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 69 | // 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 Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 83 | protected: |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 84 | // Creates a BaseVariable using the default polling interval (5 minutes). |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 85 | BaseVariable(const std::string& name, VariableMode mode) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 86 | : 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 Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 93 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 94 | // Calls ValueChanged on all the observers. |
| 95 | void NotifyValueChanged() { |
| 96 | for (auto& observer : observer_list_) |
| 97 | observer->ValueChanged(this); |
| 98 | } |
| 99 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 100 | private: |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 101 | friend class PmBaseVariableTest; |
| 102 | FRIEND_TEST(PmBaseVariableTest, RepeatedObserverTest); |
| 103 | FRIEND_TEST(PmBaseVariableTest, NotifyValueChangedTest); |
| 104 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 105 | 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 Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 114 | // The variable's name as a string. |
| 115 | const std::string name_; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 116 | |
| 117 | // The variable's mode. |
| 118 | const VariableMode mode_; |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 119 | |
| 120 | // The variable's polling interval for VariableModePoll variable and 0 for |
| 121 | // other modes. |
| 122 | const base::TimeDelta poll_interval_; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 123 | |
| 124 | // The list of value changes observers. |
| 125 | std::list<BaseVariable::Observer*> observer_list_; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 126 | }; |
| 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. |
| 131 | template<typename T> |
| 132 | class Variable : public BaseVariable { |
| 133 | public: |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 134 | virtual ~Variable() {} |
| 135 | |
| 136 | protected: |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 137 | // Only allow to get values through the EvaluationContext class and not |
| 138 | // directly from the variable. |
| 139 | friend class EvaluationContext; |
| 140 | |
Alex Deymo | bb019fe | 2014-02-03 20:12:17 -0800 | [diff] [blame] | 141 | friend class PmRealRandomProviderTest; |
| 142 | FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 143 | friend class PmRealShillProviderTest; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 144 | FRIEND_TEST(PmRealShillProviderTest, ReadDefaultValues); |
| 145 | FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedViaEthernet); |
| 146 | FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedViaVpn); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 147 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 148 | Variable(const std::string& name, VariableMode mode) |
| 149 | : BaseVariable(name, mode) {} |
| 150 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 151 | Variable(const std::string& name, const base::TimeDelta& poll_interval) |
| 152 | : BaseVariable(name, poll_interval) {} |
| 153 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 154 | // Gets the current value of the variable. The current value is copied to a |
| 155 | // new object and returned. The caller of this method owns the object and |
| 156 | // should delete it. |
| 157 | // |
| 158 | // In case of and error getting the current value or the |timeout| timeout is |
| 159 | // exceeded, a NULL value is returned and the |errmsg| is set. |
| 160 | // |
| 161 | // The caller can pass a NULL value for |errmsg|, in which case the error |
| 162 | // message won't be set. |
| 163 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0; |
| 164 | }; |
| 165 | |
| 166 | } // namespace chromeos_policy_manager |
| 167 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame^] | 168 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H_ |