Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -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 | |
| 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/time.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 11 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 12 | |
| 13 | namespace chromeos_policy_manager { |
| 14 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 15 | // The VariableMode specifies important behavior of the variable in terms of |
| 16 | // whether, how and when the value of the variable changes. |
| 17 | enum VariableMode { |
| 18 | // Const variables never changes during the life of a policy request, so the |
| 19 | // EvaluationContext caches the value even between different evaluations of |
| 20 | // the same policy request. |
| 21 | kVariableModeConst, |
| 22 | |
| 23 | // Poll variables, or synchronous variables, represent a variable with a value |
| 24 | // that can be queried at any time, but it is not known when the value |
| 25 | // changes on the source of information. In order to detect if the value of |
| 26 | // the variable changes, it has to be queried again. |
| 27 | kVariableModePoll, |
| 28 | |
| 29 | // Async variables are able to produce a signal or callback whenever the |
| 30 | // value changes. This means that it's not required to poll the value to |
| 31 | // detect when it changes, instead, you should register an observer to get |
| 32 | // a notification when that happens. |
| 33 | kVariableModeAsync, |
| 34 | }; |
| 35 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 36 | // This class is a base class with the common functionality that doesn't |
| 37 | // deppend on the variable's type, implemented by all the variables. |
| 38 | class BaseVariable { |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 39 | public: |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 40 | virtual ~BaseVariable() {} |
| 41 | |
| 42 | // Returns the variable name as a string. |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 43 | const std::string& GetName() const { |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 44 | return name_; |
| 45 | } |
| 46 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 47 | // Returns the variable mode. |
| 48 | VariableMode GetMode() const { |
| 49 | return mode_; |
| 50 | } |
| 51 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame^] | 52 | // For VariableModePoll variables, it returns the polling interval of this |
| 53 | // variable. In other case, it returns 0. |
| 54 | base::TimeDelta GetPollInterval() const { |
| 55 | return poll_interval_; |
| 56 | } |
| 57 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 58 | protected: |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame^] | 59 | // Creates a BaseVariable using the default polling interval (5 minutes). |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 60 | BaseVariable(const std::string& name, VariableMode mode) |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame^] | 61 | : BaseVariable(name, mode, |
| 62 | base::TimeDelta::FromMinutes(kDefaultPollMinutes)) {} |
| 63 | |
| 64 | // Creates a BaseVariable with mode kVariableModePoll and the provided |
| 65 | // polling interval. |
| 66 | BaseVariable(const std::string& name, base::TimeDelta poll_interval) |
| 67 | : BaseVariable(name, kVariableModePoll, poll_interval) {} |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 68 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 69 | private: |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame^] | 70 | BaseVariable(const std::string& name, VariableMode mode, |
| 71 | base::TimeDelta poll_interval) |
| 72 | : name_(name), mode_(mode), |
| 73 | poll_interval_(mode == kVariableModePoll ? |
| 74 | poll_interval : base::TimeDelta()) {} |
| 75 | |
| 76 | // The default PollInterval in minutes. |
| 77 | static constexpr int kDefaultPollMinutes = 5; |
| 78 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 79 | // The variable's name as a string. |
| 80 | const std::string name_; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 81 | |
| 82 | // The variable's mode. |
| 83 | const VariableMode mode_; |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame^] | 84 | |
| 85 | // The variable's polling interval for VariableModePoll variable and 0 for |
| 86 | // other modes. |
| 87 | const base::TimeDelta poll_interval_; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // Interface to a Policy Manager variable of a given type. Implementation |
| 91 | // internals are hidden as protected members, since policies should not be |
| 92 | // using them directly. |
| 93 | template<typename T> |
| 94 | class Variable : public BaseVariable { |
| 95 | public: |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 96 | virtual ~Variable() {} |
| 97 | |
| 98 | protected: |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 99 | // Only allow to get values through the EvaluationContext class and not |
| 100 | // directly from the variable. |
| 101 | friend class EvaluationContext; |
| 102 | |
Alex Deymo | bb019fe | 2014-02-03 20:12:17 -0800 | [diff] [blame] | 103 | friend class PmRealRandomProviderTest; |
| 104 | FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 105 | friend class PmRealShillProviderTest; |
| 106 | FRIEND_TEST(PmRealShillProviderTest, DefaultValues); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 107 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 108 | Variable(const std::string& name, VariableMode mode) |
| 109 | : BaseVariable(name, mode) {} |
| 110 | |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame^] | 111 | Variable(const std::string& name, const base::TimeDelta& poll_interval) |
| 112 | : BaseVariable(name, poll_interval) {} |
| 113 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 114 | // Gets the current value of the variable. The current value is copied to a |
| 115 | // new object and returned. The caller of this method owns the object and |
| 116 | // should delete it. |
| 117 | // |
| 118 | // In case of and error getting the current value or the |timeout| timeout is |
| 119 | // exceeded, a NULL value is returned and the |errmsg| is set. |
| 120 | // |
| 121 | // The caller can pass a NULL value for |errmsg|, in which case the error |
| 122 | // message won't be set. |
| 123 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0; |
| 124 | }; |
| 125 | |
| 126 | } // namespace chromeos_policy_manager |
| 127 | |
| 128 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H |