Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -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 | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 5 | // Generic and provider-independent Variable subclasses. These variables can be |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 6 | // used by any state provider to implement simple variables to avoid repeat the |
| 7 | // same common code on different state providers. |
| 8 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 9 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |
| 10 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 11 | |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame^] | 12 | #include <base/callback.h> |
| 13 | |
Alex Deymo | 6e97bb2 | 2014-02-05 16:46:16 -0800 | [diff] [blame] | 14 | #include "update_engine/policy_manager/variable.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 15 | |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | const char* kCopyVariableDefaultErrMsg = "Requested value is not set"; |
| 19 | |
| 20 | } // namespace |
| 21 | |
| 22 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 23 | namespace chromeos_policy_manager { |
| 24 | |
| 25 | // Variable class returning a copy of a given object using the copy constructor. |
| 26 | // This template class can be used to define variables that expose as a variable |
| 27 | // any fixed object, such as the a provider's private member. The variable will |
| 28 | // create copies of the provided object using the copy constructor of that |
| 29 | // class. |
| 30 | // |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 31 | // For example, a state provider exposing a private member as a variable can |
| 32 | // implement this as follows: |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 33 | // |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 34 | // class SomethingProvider { |
| 35 | // public: |
| 36 | // SomethingProvider(...) { |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 37 | // var_something_foo = new CopyVariable<MyType>(foo_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 38 | // } |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 39 | // ... |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 40 | // private: |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 41 | // MyType foo_; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 42 | // }; |
| 43 | template<typename T> |
| 44 | class CopyVariable : public Variable<T> { |
| 45 | public: |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 46 | // Creates the variable returning copies of the passed |ref|. The reference to |
| 47 | // this object is kept and it should be available whenever the GetValue() |
| 48 | // method is called. If |is_set_p| is not null, then this flag will be |
| 49 | // consulted prior to returning the value, and an |errmsg| will be returned if |
| 50 | // it is not set. |
| 51 | CopyVariable(const std::string& name, VariableMode mode, const T& ref, |
| 52 | const bool* is_set_p, const std::string& errmsg) |
| 53 | : Variable<T>(name, mode), ref_(ref), is_set_p_(is_set_p), |
| 54 | errmsg_(errmsg) {} |
| 55 | CopyVariable(const std::string& name, VariableMode mode, const T& ref, |
| 56 | const bool* is_set_p) |
| 57 | : CopyVariable(name, mode, ref, is_set_p, kCopyVariableDefaultErrMsg) {} |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 58 | CopyVariable(const std::string& name, VariableMode mode, const T& ref) |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 59 | : CopyVariable(name, mode, ref, nullptr) {} |
| 60 | |
| 61 | CopyVariable(const std::string& name, const base::TimeDelta poll_interval, |
| 62 | const T& ref, const bool* is_set_p, const std::string& errmsg) |
| 63 | : Variable<T>(name, poll_interval), ref_(ref), is_set_p_(is_set_p), |
| 64 | errmsg_(errmsg) {} |
| 65 | CopyVariable(const std::string& name, const base::TimeDelta poll_interval, |
| 66 | const T& ref, const bool* is_set_p) |
| 67 | : CopyVariable(name, poll_interval, ref, is_set_p, |
| 68 | kCopyVariableDefaultErrMsg) {} |
Gilad Arnold | b6a039f | 2014-03-26 12:12:39 -0700 | [diff] [blame] | 69 | CopyVariable(const std::string& name, const base::TimeDelta poll_interval, |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 70 | const T& ref) |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 71 | : CopyVariable(name, poll_interval, ref, nullptr) {} |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 72 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 73 | protected: |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 74 | FRIEND_TEST(PmCopyVariableTest, SimpleTest); |
| 75 | FRIEND_TEST(PmCopyVariableTest, UseCopyConstructorTest); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 76 | |
| 77 | // Variable override. |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 78 | virtual inline const T* GetValue(base::TimeDelta /* timeout */, |
| 79 | std::string* errmsg) { |
| 80 | if (is_set_p_ && !(*is_set_p_)) { |
| 81 | if (errmsg) |
| 82 | *errmsg = errmsg_; |
| 83 | return nullptr; |
| 84 | } |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 85 | return new T(ref_); |
| 86 | } |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | // Reference to the object to be copied by GetValue(). |
| 90 | const T& ref_; |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 91 | |
| 92 | // A pointer to a flag indicating whether the value is set. If null, then the |
| 93 | // value is assumed to be set. |
| 94 | const bool* const is_set_p_; |
| 95 | |
| 96 | // An error message to be returned when attempting to get an unset value. |
| 97 | const std::string errmsg_; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 100 | // Variable class returning a constant value that is cached on the variable when |
| 101 | // it is created. |
| 102 | template<typename T> |
| 103 | class ConstCopyVariable : public Variable<T> { |
| 104 | public: |
| 105 | // Creates the variable returning copies of the passed |obj|. The value passed |
| 106 | // is copied in this variable, and new copies of it will be returned by |
| 107 | // GetValue(). |
| 108 | ConstCopyVariable(const std::string& name, const T& obj) |
| 109 | : Variable<T>(name, kVariableModeConst), obj_(obj) {} |
| 110 | |
| 111 | protected: |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 112 | // Variable override. |
| 113 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 114 | std::string* /* errmsg */) { |
| 115 | return new T(obj_); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | // Value to be copied by GetValue(). |
| 120 | const T obj_; |
| 121 | }; |
| 122 | |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame^] | 123 | // Variable class returning a copy of a value returned by a given function. The |
| 124 | // function is called every time the variable is being polled. |
| 125 | template<typename T> |
| 126 | class CallCopyVariable : public Variable<T> { |
| 127 | public: |
| 128 | CallCopyVariable(const std::string& name, base::Callback<T(void)> func) |
| 129 | : Variable<T>(name, kVariableModePoll), func_(func) {} |
| 130 | CallCopyVariable(const std::string& name, |
| 131 | const base::TimeDelta poll_interval, |
| 132 | base::Callback<T(void)> func) |
| 133 | : Variable<T>(name, poll_interval), func_(func) {} |
| 134 | |
| 135 | protected: |
| 136 | // Variable override. |
| 137 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 138 | std::string* /* errmsg */) { |
| 139 | if (func_.is_null()) |
| 140 | return nullptr; |
| 141 | return new T(func_.Run()); |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | FRIEND_TEST(PmCallCopyVariableTest, SimpleTest); |
| 146 | |
| 147 | // The function to be called, stored as a base::Callback. |
| 148 | base::Callback<T(void)> func_; |
| 149 | |
| 150 | DISALLOW_COPY_AND_ASSIGN(CallCopyVariable); |
| 151 | }; |
| 152 | |
| 153 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 154 | // A Variable class to implement simple Async variables. It provides two methods |
| 155 | // SetValue and UnsetValue to modify the current value of the variable and |
| 156 | // notify the registered observers whenever the value changed. |
| 157 | // |
| 158 | // The type T needs to be copy-constructable, default-constructable and have an |
| 159 | // operator== (to determine if the value changed), which makes this class |
| 160 | // suitable for basic types. |
| 161 | template<typename T> |
| 162 | class AsyncCopyVariable : public Variable<T> { |
| 163 | public: |
| 164 | explicit AsyncCopyVariable(const std::string& name) |
| 165 | : Variable<T>(name, kVariableModeAsync), has_value_(false) {} |
| 166 | |
| 167 | AsyncCopyVariable(const std::string& name, const T value) |
| 168 | : Variable<T>(name, kVariableModeAsync), |
| 169 | has_value_(true), value_(value) {} |
| 170 | |
| 171 | void SetValue(const T& new_value) { |
| 172 | bool should_notify = !(has_value_ && new_value == value_); |
| 173 | value_ = new_value; |
| 174 | has_value_ = true; |
| 175 | if (should_notify) |
| 176 | this->NotifyValueChanged(); |
| 177 | } |
| 178 | |
| 179 | void UnsetValue() { |
| 180 | if (has_value_) { |
| 181 | has_value_ = false; |
| 182 | this->NotifyValueChanged(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | protected: |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 187 | // Variable override. |
| 188 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 189 | std::string* errmsg) { |
| 190 | if (!has_value_) { |
| 191 | if (errmsg) |
| 192 | *errmsg = "No value set for " + this->GetName(); |
| 193 | return nullptr; |
| 194 | } |
| 195 | return new T(value_); |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | // Whether the variable has a value set. |
| 200 | bool has_value_; |
| 201 | |
| 202 | // Copy of the object to be returned by GetValue(). |
| 203 | T value_; |
| 204 | }; |
| 205 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 206 | } // namespace chromeos_policy_manager |
| 207 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 208 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |