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 | |
Alex Deymo | 6e97bb2 | 2014-02-05 16:46:16 -0800 | [diff] [blame] | 12 | #include "update_engine/policy_manager/variable.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 13 | |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame^] | 14 | namespace { |
| 15 | |
| 16 | const char* kCopyVariableDefaultErrMsg = "Requested value is not set"; |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 21 | namespace chromeos_policy_manager { |
| 22 | |
| 23 | // Variable class returning a copy of a given object using the copy constructor. |
| 24 | // This template class can be used to define variables that expose as a variable |
| 25 | // any fixed object, such as the a provider's private member. The variable will |
| 26 | // create copies of the provided object using the copy constructor of that |
| 27 | // class. |
| 28 | // |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 29 | // For example, a state provider exposing a private member as a variable can |
| 30 | // implement this as follows: |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 31 | // |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 32 | // class SomethingProvider { |
| 33 | // public: |
| 34 | // SomethingProvider(...) { |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 35 | // var_something_foo = new CopyVariable<MyType>(foo_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 36 | // } |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 37 | // ... |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 38 | // private: |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 39 | // MyType foo_; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 40 | // }; |
| 41 | template<typename T> |
| 42 | class CopyVariable : public Variable<T> { |
| 43 | public: |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame^] | 44 | // Creates the variable returning copies of the passed |ref|. The reference to |
| 45 | // this object is kept and it should be available whenever the GetValue() |
| 46 | // method is called. If |is_set_p| is not null, then this flag will be |
| 47 | // consulted prior to returning the value, and an |errmsg| will be returned if |
| 48 | // it is not set. |
| 49 | CopyVariable(const std::string& name, VariableMode mode, const T& ref, |
| 50 | const bool* is_set_p, const std::string& errmsg) |
| 51 | : Variable<T>(name, mode), ref_(ref), is_set_p_(is_set_p), |
| 52 | errmsg_(errmsg) {} |
| 53 | CopyVariable(const std::string& name, VariableMode mode, const T& ref, |
| 54 | const bool* is_set_p) |
| 55 | : CopyVariable(name, mode, ref, is_set_p, kCopyVariableDefaultErrMsg) {} |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 56 | CopyVariable(const std::string& name, VariableMode mode, const T& ref) |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame^] | 57 | : CopyVariable(name, mode, ref, nullptr) {} |
| 58 | |
| 59 | CopyVariable(const std::string& name, const base::TimeDelta poll_interval, |
| 60 | const T& ref, const bool* is_set_p, const std::string& errmsg) |
| 61 | : Variable<T>(name, poll_interval), ref_(ref), is_set_p_(is_set_p), |
| 62 | errmsg_(errmsg) {} |
| 63 | CopyVariable(const std::string& name, const base::TimeDelta poll_interval, |
| 64 | const T& ref, const bool* is_set_p) |
| 65 | : CopyVariable(name, poll_interval, ref, is_set_p, |
| 66 | kCopyVariableDefaultErrMsg) {} |
Gilad Arnold | b6a039f | 2014-03-26 12:12:39 -0700 | [diff] [blame] | 67 | CopyVariable(const std::string& name, const base::TimeDelta poll_interval, |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 68 | const T& ref) |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame^] | 69 | : CopyVariable(name, poll_interval, ref, nullptr) {} |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 70 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 71 | protected: |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 72 | friend class PmCopyVariableTest; |
| 73 | FRIEND_TEST(PmCopyVariableTest, SimpleTest); |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame^] | 74 | FRIEND_TEST(PmCopyVariableTest, SetFlagTest); |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 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: |
| 112 | friend class PmConstCopyVariableTest; |
| 113 | FRIEND_TEST(PmConstCopyVariableTest, SimpleTest); |
| 114 | |
| 115 | // Variable override. |
| 116 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 117 | std::string* /* errmsg */) { |
| 118 | return new T(obj_); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | // Value to be copied by GetValue(). |
| 123 | const T obj_; |
| 124 | }; |
| 125 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 126 | } // namespace chromeos_policy_manager |
| 127 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 128 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |