blob: d4fba0cbb4701befff843f8edd67ab578fdcffcd [file] [log] [blame]
Alex Deymo8c499142014-01-02 19:33:34 -08001// 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 Deymoca0aaa62014-01-06 10:39:58 -080011#include <gtest/gtest_prod.h> // for FRIEND_TEST
Alex Deymo8c499142014-01-02 19:33:34 -080012
13namespace chromeos_policy_manager {
14
Alex Deymo0e433692014-02-20 07:23:03 -080015// The VariableMode specifies important behavior of the variable in terms of
16// whether, how and when the value of the variable changes.
17enum 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 Deymo391ad9f2014-01-29 14:36:20 -080036// 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.
38class BaseVariable {
Alex Deymo8c499142014-01-02 19:33:34 -080039 public:
Alex Deymo391ad9f2014-01-29 14:36:20 -080040 virtual ~BaseVariable() {}
41
42 // Returns the variable name as a string.
Alex Deymo0e433692014-02-20 07:23:03 -080043 const std::string& GetName() const {
Alex Deymo391ad9f2014-01-29 14:36:20 -080044 return name_;
45 }
46
Alex Deymo0e433692014-02-20 07:23:03 -080047 // 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 Deymo391ad9f2014-01-29 14:36:20 -080056 private:
57 // The variable's name as a string.
58 const std::string name_;
Alex Deymo0e433692014-02-20 07:23:03 -080059
60 // The variable's mode.
61 const VariableMode mode_;
Alex Deymo391ad9f2014-01-29 14:36:20 -080062};
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.
67template<typename T>
68class Variable : public BaseVariable {
69 public:
Alex Deymo8c499142014-01-02 19:33:34 -080070 virtual ~Variable() {}
71
72 protected:
Alex Deymo23949d42014-02-05 15:20:59 -080073 // Only allow to get values through the EvaluationContext class and not
74 // directly from the variable.
75 friend class EvaluationContext;
76
Alex Deymobb019fe2014-02-03 20:12:17 -080077 friend class PmRealRandomProviderTest;
78 FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues);
Gilad Arnold55f39b72014-01-28 12:51:45 -080079 friend class PmRealShillProviderTest;
80 FRIEND_TEST(PmRealShillProviderTest, DefaultValues);
Alex Deymoca0aaa62014-01-06 10:39:58 -080081
Alex Deymo0e433692014-02-20 07:23:03 -080082 Variable(const std::string& name, VariableMode mode)
83 : BaseVariable(name, mode) {}
84
Alex Deymo8c499142014-01-02 19:33:34 -080085 // 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