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 | |
| 52 | protected: |
| 53 | BaseVariable(const std::string& name, VariableMode mode) |
| 54 | : name_(name), mode_(mode) {} |
| 55 | |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 56 | private: |
| 57 | // The variable's name as a string. |
| 58 | const std::string name_; |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame^] | 59 | |
| 60 | // The variable's mode. |
| 61 | const VariableMode mode_; |
Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // Interface to a Policy Manager variable of a given type. Implementation |
| 65 | // internals are hidden as protected members, since policies should not be |
| 66 | // using them directly. |
| 67 | template<typename T> |
| 68 | class Variable : public BaseVariable { |
| 69 | public: |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 70 | virtual ~Variable() {} |
| 71 | |
| 72 | protected: |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 73 | // Only allow to get values through the EvaluationContext class and not |
| 74 | // directly from the variable. |
| 75 | friend class EvaluationContext; |
| 76 | |
Alex Deymo | bb019fe | 2014-02-03 20:12:17 -0800 | [diff] [blame] | 77 | friend class PmRealRandomProviderTest; |
| 78 | FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 79 | friend class PmRealShillProviderTest; |
| 80 | FRIEND_TEST(PmRealShillProviderTest, DefaultValues); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 81 | |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame^] | 82 | Variable(const std::string& name, VariableMode mode) |
| 83 | : BaseVariable(name, mode) {} |
| 84 | |
Alex Deymo | 8c49914 | 2014-01-02 19:33:34 -0800 | [diff] [blame] | 85 | // Gets the current value of the variable. The current value is copied to a |
| 86 | // new object and returned. The caller of this method owns the object and |
| 87 | // should delete it. |
| 88 | // |
| 89 | // In case of and error getting the current value or the |timeout| timeout is |
| 90 | // exceeded, a NULL value is returned and the |errmsg| is set. |
| 91 | // |
| 92 | // The caller can pass a NULL value for |errmsg|, in which case the error |
| 93 | // message won't be set. |
| 94 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg) = 0; |
| 95 | }; |
| 96 | |
| 97 | } // namespace chromeos_policy_manager |
| 98 | |
| 99 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_VARIABLE_H |