blob: bf610c15ec194b78abf91df44d29ec2893e57d9b [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 Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_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 Deymoca0aaa62014-01-06 10:39:58 -080015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Alex Deymo8c499142014-01-02 19:33:34 -080016
Alex Deymo53556ec2014-03-17 10:05:57 -070017#include "update_engine/policy_manager/event_loop.h"
18
Alex Deymo8c499142014-01-02 19:33:34 -080019namespace chromeos_policy_manager {
20
Alex Deymo0e433692014-02-20 07:23:03 -080021// The VariableMode specifies important behavior of the variable in terms of
22// whether, how and when the value of the variable changes.
23enum 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 Deymo391ad9f2014-01-29 14:36:20 -080042// 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.
44class BaseVariable {
Alex Deymo8c499142014-01-02 19:33:34 -080045 public:
Alex Deymoa07a1232014-02-25 14:19:50 -080046 // Interface for observing changes on variable value.
Alex Deymo53556ec2014-03-17 10:05:57 -070047 class ObserverInterface {
Alex Deymoa07a1232014-02-25 14:19:50 -080048 public:
Alex Deymo53556ec2014-03-17 10:05:57 -070049 virtual ~ObserverInterface() {}
Alex Deymoa07a1232014-02-25 14:19:50 -080050
51 // Called when the value on the variable changes.
52 virtual void ValueChanged(BaseVariable* variable) = 0;
53 };
54
Alex Deymo53556ec2014-03-17 10:05:57 -070055 virtual ~BaseVariable() {
56 if (!observer_list_.empty()) {
57 LOG(WARNING) << "Variable " << name_ << " deleted with "
58 << observer_list_.size() << " observers.";
59 }
60 }
Alex Deymo391ad9f2014-01-29 14:36:20 -080061
62 // Returns the variable name as a string.
Alex Deymo0e433692014-02-20 07:23:03 -080063 const std::string& GetName() const {
Alex Deymo391ad9f2014-01-29 14:36:20 -080064 return name_;
65 }
66
Alex Deymo0e433692014-02-20 07:23:03 -080067 // Returns the variable mode.
68 VariableMode GetMode() const {
69 return mode_;
70 }
71
Alex Deymoa8033932014-02-25 10:33:13 -080072 // 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 Deymoa07a1232014-02-25 14:19:50 -080078 // 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 Deymo53556ec2014-03-17 10:05:57 -070081 virtual void AddObserver(BaseVariable::ObserverInterface* observer) {
Alex Deymoa07a1232014-02-25 14:19:50 -080082 if (std::find(observer_list_.begin(), observer_list_.end(), observer) ==
83 observer_list_.end()) {
84 observer_list_.push_back(observer);
85 }
86 }
87
Alex Deymo53556ec2014-03-17 10:05:57 -070088 virtual void RemoveObserver(BaseVariable::ObserverInterface* observer) {
Alex Deymoa07a1232014-02-25 14:19:50 -080089 observer_list_.remove(observer);
90 }
91
Alex Deymo0e433692014-02-20 07:23:03 -080092 protected:
Alex Deymoa8033932014-02-25 10:33:13 -080093 // Creates a BaseVariable using the default polling interval (5 minutes).
Alex Deymo0e433692014-02-20 07:23:03 -080094 BaseVariable(const std::string& name, VariableMode mode)
Alex Deymoa8033932014-02-25 10:33:13 -080095 : 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 Deymo0e433692014-02-20 07:23:03 -0800102
Alex Deymoa07a1232014-02-25 14:19:50 -0800103 // Calls ValueChanged on all the observers.
104 void NotifyValueChanged() {
Alex Deymo59d378d2014-04-17 12:53:43 -0700105 // Fire all the observer methods from the main loop as single call. In order
106 // to avoid scheduling these callbacks when it is not needed, we check
107 // first the list of observers.
108 if (!observer_list_.empty()) {
109 RunFromMainLoop(base::Bind(&BaseVariable::OnValueChangedNotification,
110 base::Unretained(this)));
111 }
Alex Deymoa07a1232014-02-25 14:19:50 -0800112 }
113
Alex Deymo391ad9f2014-01-29 14:36:20 -0800114 private:
Alex Deymo53556ec2014-03-17 10:05:57 -0700115 friend class PmEvaluationContextTest;
Alex Deymoa07a1232014-02-25 14:19:50 -0800116 friend class PmBaseVariableTest;
117 FRIEND_TEST(PmBaseVariableTest, RepeatedObserverTest);
118 FRIEND_TEST(PmBaseVariableTest, NotifyValueChangedTest);
Alex Deymoa5856e42014-03-31 18:01:36 -0700119 FRIEND_TEST(PmBaseVariableTest, NotifyValueRemovesObserversTest);
Alex Deymoa07a1232014-02-25 14:19:50 -0800120
Alex Deymoa8033932014-02-25 10:33:13 -0800121 BaseVariable(const std::string& name, VariableMode mode,
122 base::TimeDelta poll_interval)
123 : name_(name), mode_(mode),
124 poll_interval_(mode == kVariableModePoll ?
125 poll_interval : base::TimeDelta()) {}
126
Alex Deymoa5856e42014-03-31 18:01:36 -0700127 void OnValueChangedNotification() {
128 // A ValueChanged() method can change the list of observers, for example
129 // removing itself and invalidating the iterator, so we create a snapshot
130 // of the observers first. Also, to support the case when *another* observer
131 // is removed, we check for them.
132 std::list<BaseVariable::ObserverInterface*> observer_list_copy(
133 observer_list_);
134
135 for (auto& observer : observer_list_copy) {
136 if (std::find(observer_list_.begin(), observer_list_.end(), observer) !=
137 observer_list_.end()) {
138 observer->ValueChanged(this);
139 }
140 }
141 }
142
Alex Deymoa8033932014-02-25 10:33:13 -0800143 // The default PollInterval in minutes.
144 static constexpr int kDefaultPollMinutes = 5;
145
Alex Deymo391ad9f2014-01-29 14:36:20 -0800146 // The variable's name as a string.
147 const std::string name_;
Alex Deymo0e433692014-02-20 07:23:03 -0800148
149 // The variable's mode.
150 const VariableMode mode_;
Alex Deymoa8033932014-02-25 10:33:13 -0800151
152 // The variable's polling interval for VariableModePoll variable and 0 for
153 // other modes.
154 const base::TimeDelta poll_interval_;
Alex Deymoa07a1232014-02-25 14:19:50 -0800155
156 // The list of value changes observers.
Alex Deymo53556ec2014-03-17 10:05:57 -0700157 std::list<BaseVariable::ObserverInterface*> observer_list_;
Alex Deymo231a8512014-03-21 12:56:10 -0700158
159 DISALLOW_COPY_AND_ASSIGN(BaseVariable);
Alex Deymo391ad9f2014-01-29 14:36:20 -0800160};
161
162// Interface to a Policy Manager variable of a given type. Implementation
163// internals are hidden as protected members, since policies should not be
164// using them directly.
165template<typename T>
166class Variable : public BaseVariable {
167 public:
Alex Deymo8c499142014-01-02 19:33:34 -0800168 virtual ~Variable() {}
169
170 protected:
Alex Deymo23949d42014-02-05 15:20:59 -0800171 // Only allow to get values through the EvaluationContext class and not
172 // directly from the variable.
173 friend class EvaluationContext;
174
Alex Deymobb019fe2014-02-03 20:12:17 -0800175 friend class PmRealRandomProviderTest;
176 FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues);
Gilad Arnold55f39b72014-01-28 12:51:45 -0800177 friend class PmRealShillProviderTest;
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700178 FRIEND_TEST(PmRealShillProviderTest, ReadBaseValues);
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800179 FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedViaVpn);
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700180 FRIEND_TEST(PmRealShillProviderTest, ReadChangedValuesConnectedTwoSignals);
Gilad Arnoldecc7f792014-04-09 12:56:13 -0700181 FRIEND_TEST(PmRealShillProviderTest, ConnectionTypeCached);
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700182 FRIEND_TEST(PmRealShillProviderTest, NoInitConnStatusReadBaseValues);
Gilad Arnold78a78112014-03-13 14:58:06 -0700183 friend class PmRealTimeProviderTest;
184 FRIEND_TEST(PmRealTimeProviderTest, CurrDateValid);
185 FRIEND_TEST(PmRealTimeProviderTest, CurrHourValid);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700186 friend class PmRealUpdaterProviderTest;
Alex Deymoca0aaa62014-01-06 10:39:58 -0800187
Alex Deymo0e433692014-02-20 07:23:03 -0800188 Variable(const std::string& name, VariableMode mode)
189 : BaseVariable(name, mode) {}
190
Gilad Arnoldb6a039f2014-03-26 12:12:39 -0700191 Variable(const std::string& name, const base::TimeDelta poll_interval)
Alex Deymoa8033932014-02-25 10:33:13 -0800192 : BaseVariable(name, poll_interval) {}
193
Alex Deymo8c499142014-01-02 19:33:34 -0800194 // Gets the current value of the variable. The current value is copied to a
195 // new object and returned. The caller of this method owns the object and
196 // should delete it.
197 //
198 // In case of and error getting the current value or the |timeout| timeout is
199 // exceeded, a NULL value is returned and the |errmsg| is set.
200 //
201 // The caller can pass a NULL value for |errmsg|, in which case the error
202 // message won't be set.
203 virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0;
Alex Deymo231a8512014-03-21 12:56:10 -0700204
205 private:
206 DISALLOW_COPY_AND_ASSIGN(Variable);
Alex Deymo8c499142014-01-02 19:33:34 -0800207};
208
209} // namespace chromeos_policy_manager
210
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800211#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H_