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 | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_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> |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame^] | 13 | #include <base/location.h> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 14 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 15 | #include <base/time/time.h> |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 16 | #include <chromeos/message_loops/message_loop.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 17 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 19 | namespace chromeos_update_manager { |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 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 |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 43 | // depend on the variable's type, implemented by all the variables. |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 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 | } |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 60 | DCHECK(observer_list_.empty()) << "Don't destroy the variable without " |
| 61 | "removing the observers."; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 62 | } |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 63 | |
| 64 | // Returns the variable name as a string. |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 65 | const std::string& GetName() const { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 66 | return name_; |
| 67 | } |
| 68 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 69 | // Returns the variable mode. |
| 70 | VariableMode GetMode() const { |
| 71 | return mode_; |
| 72 | } |
| 73 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 74 | // For VariableModePoll variables, it returns the polling interval of this |
| 75 | // variable. In other case, it returns 0. |
| 76 | base::TimeDelta GetPollInterval() const { |
| 77 | return poll_interval_; |
| 78 | } |
| 79 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 80 | // Adds and removes observers for value changes on the variable. This only |
| 81 | // works for kVariableAsync variables since the other modes don't track value |
| 82 | // changes. Adding the same observer twice has no effect. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 83 | virtual void AddObserver(BaseVariable::ObserverInterface* observer) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 84 | if (std::find(observer_list_.begin(), observer_list_.end(), observer) == |
| 85 | observer_list_.end()) { |
| 86 | observer_list_.push_back(observer); |
| 87 | } |
| 88 | } |
| 89 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 90 | virtual void RemoveObserver(BaseVariable::ObserverInterface* observer) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 91 | observer_list_.remove(observer); |
| 92 | } |
| 93 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 94 | protected: |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 95 | // Creates a BaseVariable using the default polling interval (5 minutes). |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 96 | BaseVariable(const std::string& name, VariableMode mode) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 97 | : BaseVariable(name, mode, |
| 98 | base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {} |
| 99 | |
| 100 | // Creates a BaseVariable with mode kVariableModePoll and the provided |
| 101 | // polling interval. |
| 102 | BaseVariable(const std::string& name, base::TimeDelta poll_interval) |
| 103 | : BaseVariable(name, kVariableModePoll, poll_interval) {} |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 104 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 105 | // Calls ValueChanged on all the observers. |
| 106 | void NotifyValueChanged() { |
Alex Deymo | 59d378d | 2014-04-17 12:53:43 -0700 | [diff] [blame] | 107 | // Fire all the observer methods from the main loop as single call. In order |
| 108 | // to avoid scheduling these callbacks when it is not needed, we check |
| 109 | // first the list of observers. |
| 110 | if (!observer_list_.empty()) { |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 111 | chromeos::MessageLoop::current()->PostTask( |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame^] | 112 | FROM_HERE, |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 113 | base::Bind(&BaseVariable::OnValueChangedNotification, |
| 114 | base::Unretained(this))); |
Alex Deymo | 59d378d | 2014-04-17 12:53:43 -0700 | [diff] [blame] | 115 | } |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 118 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 119 | friend class UmEvaluationContextTest; |
| 120 | FRIEND_TEST(UmBaseVariableTest, RepeatedObserverTest); |
| 121 | FRIEND_TEST(UmBaseVariableTest, NotifyValueChangedTest); |
| 122 | FRIEND_TEST(UmBaseVariableTest, NotifyValueRemovesObserversTest); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 123 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 124 | BaseVariable(const std::string& name, VariableMode mode, |
| 125 | base::TimeDelta poll_interval) |
| 126 | : name_(name), mode_(mode), |
| 127 | poll_interval_(mode == kVariableModePoll ? |
| 128 | poll_interval : base::TimeDelta()) {} |
| 129 | |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 130 | void OnValueChangedNotification() { |
| 131 | // A ValueChanged() method can change the list of observers, for example |
| 132 | // removing itself and invalidating the iterator, so we create a snapshot |
| 133 | // of the observers first. Also, to support the case when *another* observer |
| 134 | // is removed, we check for them. |
| 135 | std::list<BaseVariable::ObserverInterface*> observer_list_copy( |
| 136 | observer_list_); |
| 137 | |
| 138 | for (auto& observer : observer_list_copy) { |
| 139 | if (std::find(observer_list_.begin(), observer_list_.end(), observer) != |
| 140 | observer_list_.end()) { |
| 141 | observer->ValueChanged(this); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 146 | // The default PollInterval in minutes. |
| 147 | static constexpr int kDefaultPollMinutes = 5; |
| 148 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 149 | // The variable's name as a string. |
| 150 | const std::string name_; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 151 | |
| 152 | // The variable's mode. |
| 153 | const VariableMode mode_; |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 154 | |
| 155 | // The variable's polling interval for VariableModePoll variable and 0 for |
| 156 | // other modes. |
| 157 | const base::TimeDelta poll_interval_; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 158 | |
| 159 | // The list of value changes observers. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 160 | std::list<BaseVariable::ObserverInterface*> observer_list_; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 161 | |
| 162 | DISALLOW_COPY_AND_ASSIGN(BaseVariable); |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 165 | // Interface to an Update Manager variable of a given type. Implementation |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 166 | // internals are hidden as protected members, since policies should not be |
| 167 | // using them directly. |
| 168 | template<typename T> |
| 169 | class Variable : public BaseVariable { |
| 170 | public: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 171 | ~Variable() override {} |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 172 | |
| 173 | protected: |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 174 | // Only allow to get values through the EvaluationContext class and not |
| 175 | // directly from the variable. |
| 176 | friend class EvaluationContext; |
| 177 | |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 178 | // Needed to be able to verify variable contents during unit testing. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 179 | friend class UmTestUtils; |
| 180 | FRIEND_TEST(UmRealRandomProviderTest, GetRandomValues); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 181 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 182 | Variable(const std::string& name, VariableMode mode) |
| 183 | : BaseVariable(name, mode) {} |
| 184 | |
Gilad Arnold | b6a039f | 2014-03-26 12:12:39 -0700 | [diff] [blame] | 185 | Variable(const std::string& name, const base::TimeDelta poll_interval) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 186 | : BaseVariable(name, poll_interval) {} |
| 187 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 188 | // Gets the current value of the variable. The current value is copied to a |
| 189 | // new object and returned. The caller of this method owns the object and |
| 190 | // should delete it. |
| 191 | // |
| 192 | // In case of and error getting the current value or the |timeout| timeout is |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 193 | // exceeded, a null value is returned and the |errmsg| is set. |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 194 | // |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 195 | // The caller can pass a null value for |errmsg|, in which case the error |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 196 | // message won't be set. |
| 197 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 198 | |
| 199 | private: |
| 200 | DISALLOW_COPY_AND_ASSIGN(Variable); |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 201 | }; |
| 202 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 203 | } // namespace chromeos_update_manager |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 204 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 205 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_ |