Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 16 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_ |
| 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_ |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 19 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <list> |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 24 | #include <base/bind.h> |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 25 | #include <base/location.h> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 26 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 27 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 28 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 29 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 30 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 31 | namespace chromeos_update_manager { |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 32 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 33 | // The VariableMode specifies important behavior of the variable in terms of |
| 34 | // whether, how and when the value of the variable changes. |
| 35 | enum VariableMode { |
| 36 | // Const variables never changes during the life of a policy request, so the |
| 37 | // EvaluationContext caches the value even between different evaluations of |
| 38 | // the same policy request. |
| 39 | kVariableModeConst, |
| 40 | |
| 41 | // Poll variables, or synchronous variables, represent a variable with a value |
| 42 | // that can be queried at any time, but it is not known when the value |
| 43 | // changes on the source of information. In order to detect if the value of |
| 44 | // the variable changes, it has to be queried again. |
| 45 | kVariableModePoll, |
| 46 | |
| 47 | // Async variables are able to produce a signal or callback whenever the |
| 48 | // value changes. This means that it's not required to poll the value to |
| 49 | // detect when it changes, instead, you should register an observer to get |
| 50 | // a notification when that happens. |
| 51 | kVariableModeAsync, |
| 52 | }; |
| 53 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 54 | // 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] | 55 | // depend on the variable's type, implemented by all the variables. |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 56 | class BaseVariable { |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 57 | public: |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 58 | // Interface for observing changes on variable value. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 59 | class ObserverInterface { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 60 | public: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 61 | virtual ~ObserverInterface() {} |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 62 | |
| 63 | // Called when the value on the variable changes. |
| 64 | virtual void ValueChanged(BaseVariable* variable) = 0; |
| 65 | }; |
| 66 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 67 | virtual ~BaseVariable() { |
| 68 | if (!observer_list_.empty()) { |
| 69 | LOG(WARNING) << "Variable " << name_ << " deleted with " |
| 70 | << observer_list_.size() << " observers."; |
| 71 | } |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 72 | DCHECK(observer_list_.empty()) << "Don't destroy the variable without " |
| 73 | "removing the observers."; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 74 | } |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 75 | |
| 76 | // Returns the variable name as a string. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 77 | const std::string& GetName() const { return name_; } |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 78 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 79 | // Returns the variable mode. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 80 | VariableMode GetMode() const { return mode_; } |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 81 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 82 | // For VariableModePoll variables, it returns the polling interval of this |
| 83 | // variable. In other case, it returns 0. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 84 | base::TimeDelta GetPollInterval() const { return poll_interval_; } |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 85 | |
Amin Hassani | 03277de | 2020-07-28 12:32:49 -0700 | [diff] [blame^] | 86 | // Returns true, if the value for this variable is expected to be missing |
| 87 | // sometimes so we can avoid printing confusing error logs. |
| 88 | bool IsMissingOk() const { return missing_ok_; } |
| 89 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 90 | // Adds and removes observers for value changes on the variable. This only |
| 91 | // works for kVariableAsync variables since the other modes don't track value |
| 92 | // changes. Adding the same observer twice has no effect. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 93 | virtual void AddObserver(BaseVariable::ObserverInterface* observer) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 94 | if (std::find(observer_list_.begin(), observer_list_.end(), observer) == |
| 95 | observer_list_.end()) { |
| 96 | observer_list_.push_back(observer); |
| 97 | } |
| 98 | } |
| 99 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 100 | virtual void RemoveObserver(BaseVariable::ObserverInterface* observer) { |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 101 | observer_list_.remove(observer); |
| 102 | } |
| 103 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 104 | protected: |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 105 | // Creates a BaseVariable using the default polling interval (5 minutes). |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 106 | BaseVariable(const std::string& name, VariableMode mode) |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 107 | : BaseVariable( |
| 108 | name, mode, base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {} |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 109 | |
| 110 | // Creates a BaseVariable with mode kVariableModePoll and the provided |
| 111 | // polling interval. |
| 112 | BaseVariable(const std::string& name, base::TimeDelta poll_interval) |
| 113 | : BaseVariable(name, kVariableModePoll, poll_interval) {} |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 114 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 115 | // Reset the poll interval on a polling variable to the given one. |
| 116 | void SetPollInterval(base::TimeDelta poll_interval) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 117 | DCHECK_EQ(kVariableModePoll, mode_) |
| 118 | << "Can't set the poll_interval on a " << mode_ << " variable"; |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 119 | poll_interval_ = poll_interval; |
| 120 | } |
| 121 | |
Amin Hassani | 03277de | 2020-07-28 12:32:49 -0700 | [diff] [blame^] | 122 | void SetMissingOk() { missing_ok_ = true; } |
| 123 | |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 124 | // Calls ValueChanged on all the observers. |
| 125 | void NotifyValueChanged() { |
Alex Deymo | 59d378d | 2014-04-17 12:53:43 -0700 | [diff] [blame] | 126 | // Fire all the observer methods from the main loop as single call. In order |
| 127 | // to avoid scheduling these callbacks when it is not needed, we check |
| 128 | // first the list of observers. |
| 129 | if (!observer_list_.empty()) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 130 | brillo::MessageLoop::current()->PostTask( |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 131 | FROM_HERE, |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 132 | base::Bind(&BaseVariable::OnValueChangedNotification, |
| 133 | base::Unretained(this))); |
Alex Deymo | 59d378d | 2014-04-17 12:53:43 -0700 | [diff] [blame] | 134 | } |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 137 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 138 | friend class UmEvaluationContextTest; |
| 139 | FRIEND_TEST(UmBaseVariableTest, RepeatedObserverTest); |
| 140 | FRIEND_TEST(UmBaseVariableTest, NotifyValueChangedTest); |
| 141 | FRIEND_TEST(UmBaseVariableTest, NotifyValueRemovesObserversTest); |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 142 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 143 | BaseVariable(const std::string& name, |
| 144 | VariableMode mode, |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 145 | base::TimeDelta poll_interval) |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 146 | : name_(name), |
| 147 | mode_(mode), |
| 148 | poll_interval_(mode == kVariableModePoll ? poll_interval |
Amin Hassani | 03277de | 2020-07-28 12:32:49 -0700 | [diff] [blame^] | 149 | : base::TimeDelta()), |
| 150 | missing_ok_(false) {} |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 151 | |
Alex Deymo | a5856e4 | 2014-03-31 18:01:36 -0700 | [diff] [blame] | 152 | void OnValueChangedNotification() { |
| 153 | // A ValueChanged() method can change the list of observers, for example |
| 154 | // removing itself and invalidating the iterator, so we create a snapshot |
| 155 | // of the observers first. Also, to support the case when *another* observer |
| 156 | // is removed, we check for them. |
| 157 | std::list<BaseVariable::ObserverInterface*> observer_list_copy( |
| 158 | observer_list_); |
| 159 | |
| 160 | for (auto& observer : observer_list_copy) { |
| 161 | if (std::find(observer_list_.begin(), observer_list_.end(), observer) != |
| 162 | observer_list_.end()) { |
| 163 | observer->ValueChanged(this); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 168 | // The default PollInterval in minutes. |
| 169 | static constexpr int kDefaultPollMinutes = 5; |
| 170 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 171 | // The variable's name as a string. |
| 172 | const std::string name_; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 173 | |
| 174 | // The variable's mode. |
| 175 | const VariableMode mode_; |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 176 | |
| 177 | // The variable's polling interval for VariableModePoll variable and 0 for |
| 178 | // other modes. |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 179 | base::TimeDelta poll_interval_; |
Alex Deymo | a07a123 | 2014-02-25 14:19:50 -0800 | [diff] [blame] | 180 | |
| 181 | // The list of value changes observers. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 182 | std::list<BaseVariable::ObserverInterface*> observer_list_; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 183 | |
Amin Hassani | 03277de | 2020-07-28 12:32:49 -0700 | [diff] [blame^] | 184 | // Defines whether this variable is expected to have no value. |
| 185 | bool missing_ok_; |
| 186 | |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 187 | DISALLOW_COPY_AND_ASSIGN(BaseVariable); |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 190 | // Interface to an Update Manager variable of a given type. Implementation |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 191 | // internals are hidden as protected members, since policies should not be |
| 192 | // using them directly. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 193 | template <typename T> |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 194 | class Variable : public BaseVariable { |
| 195 | public: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 196 | ~Variable() override {} |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 197 | |
| 198 | protected: |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 199 | // Only allow to get values through the EvaluationContext class and not |
| 200 | // directly from the variable. |
| 201 | friend class EvaluationContext; |
| 202 | |
Gilad Arnold | 67ed78d | 2014-04-23 13:17:46 -0700 | [diff] [blame] | 203 | // Needed to be able to verify variable contents during unit testing. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 204 | friend class UmTestUtils; |
| 205 | FRIEND_TEST(UmRealRandomProviderTest, GetRandomValues); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 206 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 207 | Variable(const std::string& name, VariableMode mode) |
| 208 | : BaseVariable(name, mode) {} |
| 209 | |
Gilad Arnold | b6a039f | 2014-03-26 12:12:39 -0700 | [diff] [blame] | 210 | Variable(const std::string& name, const base::TimeDelta poll_interval) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 211 | : BaseVariable(name, poll_interval) {} |
| 212 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 213 | // Gets the current value of the variable. The current value is copied to a |
| 214 | // new object and returned. The caller of this method owns the object and |
| 215 | // should delete it. |
| 216 | // |
| 217 | // 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] | 218 | // exceeded, a null value is returned and the |errmsg| is set. |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 219 | // |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 220 | // 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] | 221 | // message won't be set. |
| 222 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 223 | |
| 224 | private: |
| 225 | DISALLOW_COPY_AND_ASSIGN(Variable); |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 228 | } // namespace chromeos_update_manager |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 229 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 230 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_ |