blob: 9f80461a5860735d43a5e978ba4d8a33f9daede9 [file] [log] [blame]
Alex Deymo8c499142014-01-02 19:33:34 -08001// 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 Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_
Alex Deymo8c499142014-01-02 19:33:34 -08007
Alex Deymoa07a1232014-02-25 14:19:50 -08008#include <algorithm>
9#include <list>
Alex Deymo8c499142014-01-02 19:33:34 -080010#include <string>
11
Alex Deymo53556ec2014-03-17 10:05:57 -070012#include <base/bind.h>
13#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/time/time.h>
Alex Deymo509dd532015-06-10 14:11:05 -070015#include <chromeos/message_loops/message_loop.h>
Alex Deymoca0aaa62014-01-06 10:39:58 -080016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Alex Deymo8c499142014-01-02 19:33:34 -080017
Alex Deymo63784a52014-05-28 10:46:14 -070018namespace chromeos_update_manager {
Alex Deymo8c499142014-01-02 19:33:34 -080019
Alex Deymo0e433692014-02-20 07:23:03 -080020// The VariableMode specifies important behavior of the variable in terms of
21// whether, how and when the value of the variable changes.
22enum VariableMode {
23 // Const variables never changes during the life of a policy request, so the
24 // EvaluationContext caches the value even between different evaluations of
25 // the same policy request.
26 kVariableModeConst,
27
28 // Poll variables, or synchronous variables, represent a variable with a value
29 // that can be queried at any time, but it is not known when the value
30 // changes on the source of information. In order to detect if the value of
31 // the variable changes, it has to be queried again.
32 kVariableModePoll,
33
34 // Async variables are able to produce a signal or callback whenever the
35 // value changes. This means that it's not required to poll the value to
36 // detect when it changes, instead, you should register an observer to get
37 // a notification when that happens.
38 kVariableModeAsync,
39};
40
Alex Deymo391ad9f2014-01-29 14:36:20 -080041// This class is a base class with the common functionality that doesn't
Alex Vakulenko072359c2014-07-18 11:41:07 -070042// depend on the variable's type, implemented by all the variables.
Alex Deymo391ad9f2014-01-29 14:36:20 -080043class BaseVariable {
Alex Deymo8c499142014-01-02 19:33:34 -080044 public:
Alex Deymoa07a1232014-02-25 14:19:50 -080045 // Interface for observing changes on variable value.
Alex Deymo53556ec2014-03-17 10:05:57 -070046 class ObserverInterface {
Alex Deymoa07a1232014-02-25 14:19:50 -080047 public:
Alex Deymo53556ec2014-03-17 10:05:57 -070048 virtual ~ObserverInterface() {}
Alex Deymoa07a1232014-02-25 14:19:50 -080049
50 // Called when the value on the variable changes.
51 virtual void ValueChanged(BaseVariable* variable) = 0;
52 };
53
Alex Deymo53556ec2014-03-17 10:05:57 -070054 virtual ~BaseVariable() {
55 if (!observer_list_.empty()) {
56 LOG(WARNING) << "Variable " << name_ << " deleted with "
57 << observer_list_.size() << " observers.";
58 }
Alex Deymoc83baf62014-04-02 17:43:35 -070059 DCHECK(observer_list_.empty()) << "Don't destroy the variable without "
60 "removing the observers.";
Alex Deymo53556ec2014-03-17 10:05:57 -070061 }
Alex Deymo391ad9f2014-01-29 14:36:20 -080062
63 // Returns the variable name as a string.
Alex Deymo0e433692014-02-20 07:23:03 -080064 const std::string& GetName() const {
Alex Deymo391ad9f2014-01-29 14:36:20 -080065 return name_;
66 }
67
Alex Deymo0e433692014-02-20 07:23:03 -080068 // Returns the variable mode.
69 VariableMode GetMode() const {
70 return mode_;
71 }
72
Alex Deymoa8033932014-02-25 10:33:13 -080073 // For VariableModePoll variables, it returns the polling interval of this
74 // variable. In other case, it returns 0.
75 base::TimeDelta GetPollInterval() const {
76 return poll_interval_;
77 }
78
Alex Deymoa07a1232014-02-25 14:19:50 -080079 // Adds and removes observers for value changes on the variable. This only
80 // works for kVariableAsync variables since the other modes don't track value
81 // changes. Adding the same observer twice has no effect.
Alex Deymo53556ec2014-03-17 10:05:57 -070082 virtual void AddObserver(BaseVariable::ObserverInterface* observer) {
Alex Deymoa07a1232014-02-25 14:19:50 -080083 if (std::find(observer_list_.begin(), observer_list_.end(), observer) ==
84 observer_list_.end()) {
85 observer_list_.push_back(observer);
86 }
87 }
88
Alex Deymo53556ec2014-03-17 10:05:57 -070089 virtual void RemoveObserver(BaseVariable::ObserverInterface* observer) {
Alex Deymoa07a1232014-02-25 14:19:50 -080090 observer_list_.remove(observer);
91 }
92
Alex Deymo0e433692014-02-20 07:23:03 -080093 protected:
Alex Deymoa8033932014-02-25 10:33:13 -080094 // Creates a BaseVariable using the default polling interval (5 minutes).
Alex Deymo0e433692014-02-20 07:23:03 -080095 BaseVariable(const std::string& name, VariableMode mode)
Alex Deymoa8033932014-02-25 10:33:13 -080096 : BaseVariable(name, mode,
97 base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {}
98
99 // Creates a BaseVariable with mode kVariableModePoll and the provided
100 // polling interval.
101 BaseVariable(const std::string& name, base::TimeDelta poll_interval)
102 : BaseVariable(name, kVariableModePoll, poll_interval) {}
Alex Deymo0e433692014-02-20 07:23:03 -0800103
Alex Deymoa07a1232014-02-25 14:19:50 -0800104 // Calls ValueChanged on all the observers.
105 void NotifyValueChanged() {
Alex Deymo59d378d2014-04-17 12:53:43 -0700106 // Fire all the observer methods from the main loop as single call. In order
107 // to avoid scheduling these callbacks when it is not needed, we check
108 // first the list of observers.
109 if (!observer_list_.empty()) {
Alex Deymo509dd532015-06-10 14:11:05 -0700110 chromeos::MessageLoop::current()->PostTask(
111 base::Bind(&BaseVariable::OnValueChangedNotification,
112 base::Unretained(this)));
Alex Deymo59d378d2014-04-17 12:53:43 -0700113 }
Alex Deymoa07a1232014-02-25 14:19:50 -0800114 }
115
Alex Deymo391ad9f2014-01-29 14:36:20 -0800116 private:
Alex Deymo63784a52014-05-28 10:46:14 -0700117 friend class UmEvaluationContextTest;
118 FRIEND_TEST(UmBaseVariableTest, RepeatedObserverTest);
119 FRIEND_TEST(UmBaseVariableTest, NotifyValueChangedTest);
120 FRIEND_TEST(UmBaseVariableTest, NotifyValueRemovesObserversTest);
Alex Deymoa07a1232014-02-25 14:19:50 -0800121
Alex Deymoa8033932014-02-25 10:33:13 -0800122 BaseVariable(const std::string& name, VariableMode mode,
123 base::TimeDelta poll_interval)
124 : name_(name), mode_(mode),
125 poll_interval_(mode == kVariableModePoll ?
126 poll_interval : base::TimeDelta()) {}
127
Alex Deymoa5856e42014-03-31 18:01:36 -0700128 void OnValueChangedNotification() {
129 // A ValueChanged() method can change the list of observers, for example
130 // removing itself and invalidating the iterator, so we create a snapshot
131 // of the observers first. Also, to support the case when *another* observer
132 // is removed, we check for them.
133 std::list<BaseVariable::ObserverInterface*> observer_list_copy(
134 observer_list_);
135
136 for (auto& observer : observer_list_copy) {
137 if (std::find(observer_list_.begin(), observer_list_.end(), observer) !=
138 observer_list_.end()) {
139 observer->ValueChanged(this);
140 }
141 }
142 }
143
Alex Deymoa8033932014-02-25 10:33:13 -0800144 // The default PollInterval in minutes.
145 static constexpr int kDefaultPollMinutes = 5;
146
Alex Deymo391ad9f2014-01-29 14:36:20 -0800147 // The variable's name as a string.
148 const std::string name_;
Alex Deymo0e433692014-02-20 07:23:03 -0800149
150 // The variable's mode.
151 const VariableMode mode_;
Alex Deymoa8033932014-02-25 10:33:13 -0800152
153 // The variable's polling interval for VariableModePoll variable and 0 for
154 // other modes.
155 const base::TimeDelta poll_interval_;
Alex Deymoa07a1232014-02-25 14:19:50 -0800156
157 // The list of value changes observers.
Alex Deymo53556ec2014-03-17 10:05:57 -0700158 std::list<BaseVariable::ObserverInterface*> observer_list_;
Alex Deymo231a8512014-03-21 12:56:10 -0700159
160 DISALLOW_COPY_AND_ASSIGN(BaseVariable);
Alex Deymo391ad9f2014-01-29 14:36:20 -0800161};
162
Alex Deymo63784a52014-05-28 10:46:14 -0700163// Interface to an Update Manager variable of a given type. Implementation
Alex Deymo391ad9f2014-01-29 14:36:20 -0800164// internals are hidden as protected members, since policies should not be
165// using them directly.
166template<typename T>
167class Variable : public BaseVariable {
168 public:
Alex Deymo610277e2014-11-11 21:18:11 -0800169 ~Variable() override {}
Alex Deymo8c499142014-01-02 19:33:34 -0800170
171 protected:
Alex Deymo23949d42014-02-05 15:20:59 -0800172 // Only allow to get values through the EvaluationContext class and not
173 // directly from the variable.
174 friend class EvaluationContext;
175
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700176 // Needed to be able to verify variable contents during unit testing.
Alex Deymo63784a52014-05-28 10:46:14 -0700177 friend class UmTestUtils;
178 FRIEND_TEST(UmRealRandomProviderTest, GetRandomValues);
Alex Deymoca0aaa62014-01-06 10:39:58 -0800179
Alex Deymo0e433692014-02-20 07:23:03 -0800180 Variable(const std::string& name, VariableMode mode)
181 : BaseVariable(name, mode) {}
182
Gilad Arnoldb6a039f2014-03-26 12:12:39 -0700183 Variable(const std::string& name, const base::TimeDelta poll_interval)
Alex Deymoa8033932014-02-25 10:33:13 -0800184 : BaseVariable(name, poll_interval) {}
185
Alex Deymo8c499142014-01-02 19:33:34 -0800186 // Gets the current value of the variable. The current value is copied to a
187 // new object and returned. The caller of this method owns the object and
188 // should delete it.
189 //
190 // In case of and error getting the current value or the |timeout| timeout is
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700191 // exceeded, a null value is returned and the |errmsg| is set.
Alex Deymo8c499142014-01-02 19:33:34 -0800192 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700193 // The caller can pass a null value for |errmsg|, in which case the error
Alex Deymo8c499142014-01-02 19:33:34 -0800194 // message won't be set.
195 virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0;
Alex Deymo231a8512014-03-21 12:56:10 -0700196
197 private:
198 DISALLOW_COPY_AND_ASSIGN(Variable);
Alex Deymo8c499142014-01-02 19:33:34 -0800199};
200
Alex Deymo63784a52014-05-28 10:46:14 -0700201} // namespace chromeos_update_manager
Alex Deymo8c499142014-01-02 19:33:34 -0800202
Gilad Arnold48415f12014-06-27 07:10:58 -0700203#endif // UPDATE_ENGINE_UPDATE_MANAGER_VARIABLE_H_