blob: 373af7fecf0ba8281e8c6854acef25e1d6badf93 [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
Alex Deymoa8033932014-02-25 10:33:13 -080052 // 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 Deymo0e433692014-02-20 07:23:03 -080058 protected:
Alex Deymoa8033932014-02-25 10:33:13 -080059 // Creates a BaseVariable using the default polling interval (5 minutes).
Alex Deymo0e433692014-02-20 07:23:03 -080060 BaseVariable(const std::string& name, VariableMode mode)
Alex Deymoa8033932014-02-25 10:33:13 -080061 : 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 Deymo0e433692014-02-20 07:23:03 -080068
Alex Deymo391ad9f2014-01-29 14:36:20 -080069 private:
Alex Deymoa8033932014-02-25 10:33:13 -080070 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 Deymo391ad9f2014-01-29 14:36:20 -080079 // The variable's name as a string.
80 const std::string name_;
Alex Deymo0e433692014-02-20 07:23:03 -080081
82 // The variable's mode.
83 const VariableMode mode_;
Alex Deymoa8033932014-02-25 10:33:13 -080084
85 // The variable's polling interval for VariableModePoll variable and 0 for
86 // other modes.
87 const base::TimeDelta poll_interval_;
Alex Deymo391ad9f2014-01-29 14:36:20 -080088};
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.
93template<typename T>
94class Variable : public BaseVariable {
95 public:
Alex Deymo8c499142014-01-02 19:33:34 -080096 virtual ~Variable() {}
97
98 protected:
Alex Deymo23949d42014-02-05 15:20:59 -080099 // Only allow to get values through the EvaluationContext class and not
100 // directly from the variable.
101 friend class EvaluationContext;
102
Alex Deymobb019fe2014-02-03 20:12:17 -0800103 friend class PmRealRandomProviderTest;
104 FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues);
Gilad Arnold55f39b72014-01-28 12:51:45 -0800105 friend class PmRealShillProviderTest;
106 FRIEND_TEST(PmRealShillProviderTest, DefaultValues);
Alex Deymoca0aaa62014-01-06 10:39:58 -0800107
Alex Deymo0e433692014-02-20 07:23:03 -0800108 Variable(const std::string& name, VariableMode mode)
109 : BaseVariable(name, mode) {}
110
Alex Deymoa8033932014-02-25 10:33:13 -0800111 Variable(const std::string& name, const base::TimeDelta& poll_interval)
112 : BaseVariable(name, poll_interval) {}
113
Alex Deymo8c499142014-01-02 19:33:34 -0800114 // 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