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 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
| 13 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 14 | #include <base/time/time.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 17 | #include "update_engine/policy_manager/event_loop.h" |
| 18 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 19 | namespace chromeos_policy_manager { |
| 20 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 21 | // The VariableMode specifies important behavior of the variable in terms of |
| 22 | // whether, how and when the value of the variable changes. |
| 23 | enum VariableMode { |
| 24 | // Const variables never changes during the life of a policy request, so the |
| 25 | // EvaluationContext caches the value even between different evaluations of |
| 26 | // the same policy request. |
| 27 | kVariableModeConst, |
| 28 | |
| 29 | // Poll variables, or synchronous variables, represent a variable with a value |
| 30 | // that can be queried at any time, but it is not known when the value |
| 31 | // changes on the source of information. In order to detect if the value of |
| 32 | // the variable changes, it has to be queried again. |
| 33 | kVariableModePoll, |
| 34 | |
| 35 | // Async variables are able to produce a signal or callback whenever the |
| 36 | // value changes. This means that it's not required to poll the value to |
| 37 | // detect when it changes, instead, you should register an observer to get |
| 38 | // a notification when that happens. |
| 39 | kVariableModeAsync, |
| 40 | }; |
| 41 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 42 | // This class is a base class with the common functionality that doesn't |
| 43 | // deppend on the variable's type, implemented by all the variables. |
| 44 | class BaseVariable { |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 45 | public: |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 46 | // Interface for observing changes on variable value. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 47 | class ObserverInterface { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 48 | public: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 49 | virtual ~ObserverInterface() {} |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 50 | |
| 51 | // Called when the value on the variable changes. |
| 52 | virtual void ValueChanged(BaseVariable* variable) = 0; |
| 53 | }; |
| 54 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 55 | virtual ~BaseVariable() { |
| 56 | if (!observer_list_.empty()) { |
| 57 | LOG(WARNING) << "Variable " << name_ << " deleted with " |
| 58 | << observer_list_.size() << " observers."; |
| 59 | } |
| 60 | } |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 61 | |
| 62 | // Returns the variable name as a string. |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 63 | const std::string& GetName() const { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 64 | return name_; |
| 65 | } |
| 66 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 67 | // Returns the variable mode. |
| 68 | VariableMode GetMode() const { |
| 69 | return mode_; |
| 70 | } |
| 71 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 72 | // For VariableModePoll variables, it returns the polling interval of this |
| 73 | // variable. In other case, it returns 0. |
| 74 | base::TimeDelta GetPollInterval() const { |
| 75 | return poll_interval_; |
| 76 | } |
| 77 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 78 | // Adds and removes observers for value changes on the variable. This only |
| 79 | // works for kVariableAsync variables since the other modes don't track value |
| 80 | // changes. Adding the same observer twice has no effect. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 81 | virtual void AddObserver(BaseVariable::ObserverInterface* observer) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 82 | if (std::find(observer_list_.begin(), observer_list_.end(), observer) == |
| 83 | observer_list_.end()) { |
| 84 | observer_list_.push_back(observer); |
| 85 | } |
| 86 | } |
| 87 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 88 | virtual void RemoveObserver(BaseVariable::ObserverInterface* observer) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 89 | observer_list_.remove(observer); |
| 90 | } |
| 91 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 92 | protected: |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 93 | // Creates a BaseVariable using the default polling interval (5 minutes). |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 94 | BaseVariable(const std::string& name, VariableMode mode) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 95 | : BaseVariable(name, mode, |
| 96 | base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {} |
| 97 | |
| 98 | // Creates a BaseVariable with mode kVariableModePoll and the provided |
| 99 | // polling interval. |
| 100 | BaseVariable(const std::string& name, base::TimeDelta poll_interval) |
| 101 | : BaseVariable(name, kVariableModePoll, poll_interval) {} |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 102 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 103 | // Calls ValueChanged on all the observers. |
| 104 | void NotifyValueChanged() { |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame^] | 105 | RunFromMainLoop(base::Bind(&BaseVariable::OnValueChangedNotification, |
| 106 | base::Unretained(this))); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 109 | private: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 110 | friend class PmEvaluationContextTest; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 111 | friend class PmBaseVariableTest; |
| 112 | FRIEND_TEST(PmBaseVariableTest, RepeatedObserverTest); |
| 113 | FRIEND_TEST(PmBaseVariableTest, NotifyValueChangedTest); |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame^] | 114 | FRIEND_TEST(PmBaseVariableTest, NotifyValueRemovesObserversTest); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 115 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 116 | BaseVariable(const std::string& name, VariableMode mode, |
| 117 | base::TimeDelta poll_interval) |
| 118 | : name_(name), mode_(mode), |
| 119 | poll_interval_(mode == kVariableModePoll ? |
| 120 | poll_interval : base::TimeDelta()) {} |
| 121 | |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame^] | 122 | void OnValueChangedNotification() { |
| 123 | // A ValueChanged() method can change the list of observers, for example |
| 124 | // removing itself and invalidating the iterator, so we create a snapshot |
| 125 | // of the observers first. Also, to support the case when *another* observer |
| 126 | // is removed, we check for them. |
| 127 | std::list<BaseVariable::ObserverInterface*> observer_list_copy( |
| 128 | observer_list_); |
| 129 | |
| 130 | for (auto& observer : observer_list_copy) { |
| 131 | if (std::find(observer_list_.begin(), observer_list_.end(), observer) != |
| 132 | observer_list_.end()) { |
| 133 | observer->ValueChanged(this); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 138 | // The default PollInterval in minutes. |
| 139 | static constexpr int kDefaultPollMinutes = 5; |
| 140 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 141 | // The variable's name as a string. |
| 142 | const std::string name_; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 143 | |
| 144 | // The variable's mode. |
| 145 | const VariableMode mode_; |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 146 | |
| 147 | // The variable's polling interval for VariableModePoll variable and 0 for |
| 148 | // other modes. |
| 149 | const base::TimeDelta poll_interval_; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 150 | |
| 151 | // The list of value changes observers. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 152 | std::list<BaseVariable::ObserverInterface*> observer_list_; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 153 | |
| 154 | DISALLOW_COPY_AND_ASSIGN(BaseVariable); |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | // Interface to a Policy Manager variable of a given type. Implementation |
| 158 | // internals are hidden as protected members, since policies should not be |
| 159 | // using them directly. |
| 160 | template<typename T> |
| 161 | class Variable : public BaseVariable { |
| 162 | public: |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 163 | virtual ~Variable() {} |
| 164 | |
| 165 | protected: |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 166 | // Only allow to get values through the EvaluationContext class and not |
| 167 | // directly from the variable. |
| 168 | friend class EvaluationContext; |
| 169 | |
Alex Deymo | bb019fe | 2014-02-03 20:12:17 -0800 | [diff] [blame] | 170 | friend class PmRealRandomProviderTest; |
| 171 | FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 172 | friend class PmRealShillProviderTest; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 173 | FRIEND_TEST(PmRealShillProviderTest, ReadDefaultValues); |
| 174 | FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedViaEthernet); |
| 175 | FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedViaVpn); |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 176 | FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedTwoSignals); |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 177 | friend class PmRealTimeProviderTest; |
| 178 | FRIEND_TEST(PmRealTimeProviderTest, CurrDateValid); |
| 179 | FRIEND_TEST(PmRealTimeProviderTest, CurrHourValid); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 180 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 181 | Variable(const std::string& name, VariableMode mode) |
| 182 | : BaseVariable(name, mode) {} |
| 183 | |
Gilad Arnold | b6a039f | 2014-03-26 12:12:39 -0700 | [diff] [blame] | 184 | Variable(const std::string& name, const base::TimeDelta poll_interval) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 185 | : BaseVariable(name, poll_interval) {} |
| 186 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 187 | // Gets the current value of the variable. The current value is copied to a |
| 188 | // new object and returned. The caller of this method owns the object and |
| 189 | // should delete it. |
| 190 | // |
| 191 | // In case of and error getting the current value or the |timeout| timeout is |
| 192 | // exceeded, a NULL value is returned and the |errmsg| is set. |
| 193 | // |
| 194 | // The caller can pass a NULL value for |errmsg|, in which case the error |
| 195 | // message won't be set. |
| 196 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 197 | |
| 198 | private: |
| 199 | DISALLOW_COPY_AND_ASSIGN(Variable); |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace chromeos_policy_manager |
| 203 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 204 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H_ |