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