blob: 6c7d350618696622746bdd5e6a5fecb6272790d2 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo8c499142014-01-02 19:33:34 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_
Alex Deymo8c499142014-01-02 19:33:34 -080019
Alex Deymoa07a1232014-02-25 14:19:50 -080020#include <algorithm>
21#include <list>
Alex Deymo8c499142014-01-02 19:33:34 -080022#include <string>
23
Alex Deymo53556ec2014-03-17 10:05:57 -070024#include <base/bind.h>
Alex Deymo0bb23412015-06-19 00:04:46 -070025#include <base/location.h>
Alex Deymo53556ec2014-03-17 10:05:57 -070026#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070028#include <brillo/message_loops/message_loop.h>
Alex Deymoca0aaa62014-01-06 10:39:58 -080029#include <gtest/gtest_prod.h> // for FRIEND_TEST
Alex Deymo8c499142014-01-02 19:33:34 -080030
Alex Deymo63784a52014-05-28 10:46:14 -070031namespace chromeos_update_manager {
Alex Deymo8c499142014-01-02 19:33:34 -080032
Alex Deymo0e433692014-02-20 07:23:03 -080033// The VariableMode specifies important behavior of the variable in terms of
34// whether, how and when the value of the variable changes.
35enum 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 Deymo391ad9f2014-01-29 14:36:20 -080054// This class is a base class with the common functionality that doesn't
Alex Vakulenko072359c2014-07-18 11:41:07 -070055// depend on the variable's type, implemented by all the variables.
Alex Deymo391ad9f2014-01-29 14:36:20 -080056class BaseVariable {
Alex Deymo8c499142014-01-02 19:33:34 -080057 public:
Alex Deymoa07a1232014-02-25 14:19:50 -080058 // Interface for observing changes on variable value.
Alex Deymo53556ec2014-03-17 10:05:57 -070059 class ObserverInterface {
Alex Deymoa07a1232014-02-25 14:19:50 -080060 public:
Alex Deymo53556ec2014-03-17 10:05:57 -070061 virtual ~ObserverInterface() {}
Alex Deymoa07a1232014-02-25 14:19:50 -080062
63 // Called when the value on the variable changes.
64 virtual void ValueChanged(BaseVariable* variable) = 0;
65 };
66
Alex Deymo53556ec2014-03-17 10:05:57 -070067 virtual ~BaseVariable() {
68 if (!observer_list_.empty()) {
69 LOG(WARNING) << "Variable " << name_ << " deleted with "
70 << observer_list_.size() << " observers.";
71 }
Alex Deymoc83baf62014-04-02 17:43:35 -070072 DCHECK(observer_list_.empty()) << "Don't destroy the variable without "
73 "removing the observers.";
Alex Deymo53556ec2014-03-17 10:05:57 -070074 }
Alex Deymo391ad9f2014-01-29 14:36:20 -080075
76 // Returns the variable name as a string.
Amin Hassani4b717432019-01-14 16:24:20 -080077 const std::string& GetName() const { return name_; }
Alex Deymo391ad9f2014-01-29 14:36:20 -080078
Alex Deymo0e433692014-02-20 07:23:03 -080079 // Returns the variable mode.
Amin Hassani4b717432019-01-14 16:24:20 -080080 VariableMode GetMode() const { return mode_; }
Alex Deymo0e433692014-02-20 07:23:03 -080081
Alex Deymoa8033932014-02-25 10:33:13 -080082 // For VariableModePoll variables, it returns the polling interval of this
83 // variable. In other case, it returns 0.
Amin Hassani4b717432019-01-14 16:24:20 -080084 base::TimeDelta GetPollInterval() const { return poll_interval_; }
Alex Deymoa8033932014-02-25 10:33:13 -080085
Alex Deymoa07a1232014-02-25 14:19:50 -080086 // Adds and removes observers for value changes on the variable. This only
87 // works for kVariableAsync variables since the other modes don't track value
88 // changes. Adding the same observer twice has no effect.
Alex Deymo53556ec2014-03-17 10:05:57 -070089 virtual void AddObserver(BaseVariable::ObserverInterface* observer) {
Alex Deymoa07a1232014-02-25 14:19:50 -080090 if (std::find(observer_list_.begin(), observer_list_.end(), observer) ==
91 observer_list_.end()) {
92 observer_list_.push_back(observer);
93 }
94 }
95
Alex Deymo53556ec2014-03-17 10:05:57 -070096 virtual void RemoveObserver(BaseVariable::ObserverInterface* observer) {
Alex Deymoa07a1232014-02-25 14:19:50 -080097 observer_list_.remove(observer);
98 }
99
Alex Deymo0e433692014-02-20 07:23:03 -0800100 protected:
Alex Deymoa8033932014-02-25 10:33:13 -0800101 // Creates a BaseVariable using the default polling interval (5 minutes).
Alex Deymo0e433692014-02-20 07:23:03 -0800102 BaseVariable(const std::string& name, VariableMode mode)
Amin Hassani4b717432019-01-14 16:24:20 -0800103 : BaseVariable(
104 name, mode, base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {}
Alex Deymoa8033932014-02-25 10:33:13 -0800105
106 // Creates a BaseVariable with mode kVariableModePoll and the provided
107 // polling interval.
108 BaseVariable(const std::string& name, base::TimeDelta poll_interval)
109 : BaseVariable(name, kVariableModePoll, poll_interval) {}
Alex Deymo0e433692014-02-20 07:23:03 -0800110
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700111 // Reset the poll interval on a polling variable to the given one.
112 void SetPollInterval(base::TimeDelta poll_interval) {
Amin Hassani4b717432019-01-14 16:24:20 -0800113 DCHECK_EQ(kVariableModePoll, mode_)
114 << "Can't set the poll_interval on a " << mode_ << " variable";
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700115 poll_interval_ = poll_interval;
116 }
117
Alex Deymoa07a1232014-02-25 14:19:50 -0800118 // Calls ValueChanged on all the observers.
119 void NotifyValueChanged() {
Alex Deymo59d378d2014-04-17 12:53:43 -0700120 // Fire all the observer methods from the main loop as single call. In order
121 // to avoid scheduling these callbacks when it is not needed, we check
122 // first the list of observers.
123 if (!observer_list_.empty()) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700124 brillo::MessageLoop::current()->PostTask(
Alex Deymo0bb23412015-06-19 00:04:46 -0700125 FROM_HERE,
Alex Deymo509dd532015-06-10 14:11:05 -0700126 base::Bind(&BaseVariable::OnValueChangedNotification,
127 base::Unretained(this)));
Alex Deymo59d378d2014-04-17 12:53:43 -0700128 }
Alex Deymoa07a1232014-02-25 14:19:50 -0800129 }
130
Alex Deymo391ad9f2014-01-29 14:36:20 -0800131 private:
Alex Deymo63784a52014-05-28 10:46:14 -0700132 friend class UmEvaluationContextTest;
133 FRIEND_TEST(UmBaseVariableTest, RepeatedObserverTest);
134 FRIEND_TEST(UmBaseVariableTest, NotifyValueChangedTest);
135 FRIEND_TEST(UmBaseVariableTest, NotifyValueRemovesObserversTest);
Alex Deymoa07a1232014-02-25 14:19:50 -0800136
Amin Hassani4b717432019-01-14 16:24:20 -0800137 BaseVariable(const std::string& name,
138 VariableMode mode,
Alex Deymoa8033932014-02-25 10:33:13 -0800139 base::TimeDelta poll_interval)
Amin Hassani4b717432019-01-14 16:24:20 -0800140 : name_(name),
141 mode_(mode),
142 poll_interval_(mode == kVariableModePoll ? poll_interval
143 : base::TimeDelta()) {}
Alex Deymoa8033932014-02-25 10:33:13 -0800144
Alex Deymoa5856e42014-03-31 18:01:36 -0700145 void OnValueChangedNotification() {
146 // A ValueChanged() method can change the list of observers, for example
147 // removing itself and invalidating the iterator, so we create a snapshot
148 // of the observers first. Also, to support the case when *another* observer
149 // is removed, we check for them.
150 std::list<BaseVariable::ObserverInterface*> observer_list_copy(
151 observer_list_);
152
153 for (auto& observer : observer_list_copy) {
154 if (std::find(observer_list_.begin(), observer_list_.end(), observer) !=
155 observer_list_.end()) {
156 observer->ValueChanged(this);
157 }
158 }
159 }
160
Alex Deymoa8033932014-02-25 10:33:13 -0800161 // The default PollInterval in minutes.
162 static constexpr int kDefaultPollMinutes = 5;
163
Alex Deymo391ad9f2014-01-29 14:36:20 -0800164 // The variable's name as a string.
165 const std::string name_;
Alex Deymo0e433692014-02-20 07:23:03 -0800166
167 // The variable's mode.
168 const VariableMode mode_;
Alex Deymoa8033932014-02-25 10:33:13 -0800169
170 // The variable's polling interval for VariableModePoll variable and 0 for
171 // other modes.
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700172 base::TimeDelta poll_interval_;
Alex Deymoa07a1232014-02-25 14:19:50 -0800173
174 // The list of value changes observers.
Alex Deymo53556ec2014-03-17 10:05:57 -0700175 std::list<BaseVariable::ObserverInterface*> observer_list_;
Alex Deymo231a8512014-03-21 12:56:10 -0700176
177 DISALLOW_COPY_AND_ASSIGN(BaseVariable);
Alex Deymo391ad9f2014-01-29 14:36:20 -0800178};
179
Alex Deymo63784a52014-05-28 10:46:14 -0700180// Interface to an Update Manager variable of a given type. Implementation
Alex Deymo391ad9f2014-01-29 14:36:20 -0800181// internals are hidden as protected members, since policies should not be
182// using them directly.
Amin Hassani4b717432019-01-14 16:24:20 -0800183template <typename T>
Alex Deymo391ad9f2014-01-29 14:36:20 -0800184class Variable : public BaseVariable {
185 public:
Alex Deymo610277e2014-11-11 21:18:11 -0800186 ~Variable() override {}
Alex Deymo8c499142014-01-02 19:33:34 -0800187
188 protected:
Alex Deymo23949d42014-02-05 15:20:59 -0800189 // Only allow to get values through the EvaluationContext class and not
190 // directly from the variable.
191 friend class EvaluationContext;
192
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700193 // Needed to be able to verify variable contents during unit testing.
Alex Deymo63784a52014-05-28 10:46:14 -0700194 friend class UmTestUtils;
195 FRIEND_TEST(UmRealRandomProviderTest, GetRandomValues);
Alex Deymoca0aaa62014-01-06 10:39:58 -0800196
Alex Deymo0e433692014-02-20 07:23:03 -0800197 Variable(const std::string& name, VariableMode mode)
198 : BaseVariable(name, mode) {}
199
Gilad Arnoldb6a039f2014-03-26 12:12:39 -0700200 Variable(const std::string& name, const base::TimeDelta poll_interval)
Alex Deymoa8033932014-02-25 10:33:13 -0800201 : BaseVariable(name, poll_interval) {}
202
Alex Deymo8c499142014-01-02 19:33:34 -0800203 // Gets the current value of the variable. The current value is copied to a
204 // new object and returned. The caller of this method owns the object and
205 // should delete it.
206 //
207 // In case of and error getting the current value or the |timeout| timeout is
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700208 // exceeded, a null value is returned and the |errmsg| is set.
Alex Deymo8c499142014-01-02 19:33:34 -0800209 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700210 // The caller can pass a null value for |errmsg|, in which case the error
Alex Deymo8c499142014-01-02 19:33:34 -0800211 // message won't be set.
212 virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0;
Alex Deymo231a8512014-03-21 12:56:10 -0700213
214 private:
215 DISALLOW_COPY_AND_ASSIGN(Variable);
Alex Deymo8c499142014-01-02 19:33:34 -0800216};
217
Alex Deymo63784a52014-05-28 10:46:14 -0700218} // namespace chromeos_update_manager
Alex Deymo8c499142014-01-02 19:33:34 -0800219
Gilad Arnold48415f12014-06-27 07:10:58 -0700220#endif // UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_