1 // Copyright (c) 2012 The Chromium 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.
5 #ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_
6 #define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
22 // Maps a policy type to a preference path, and to the expected value type.
23 struct PolicyToPreferenceMapEntry {
24 const char* const policy_name;
25 const char* const preference_path;
26 const base::Value::Type value_type;
29 // An abstract super class that subclasses should implement to map policies to
30 // their corresponding preferences, and to check whether the policies are valid.
31 class ConfigurationPolicyHandler {
33 static std::string ValueTypeToString(Value::Type type);
35 ConfigurationPolicyHandler();
36 virtual ~ConfigurationPolicyHandler();
38 // Returns whether the policy settings handled by this
39 // ConfigurationPolicyHandler can be applied. Fills |errors| with error
40 // messages or warnings. |errors| may contain error messages even when
41 // |CheckPolicySettings()| returns true.
42 virtual bool CheckPolicySettings(const PolicyMap& policies,
43 PolicyErrorMap* errors) = 0;
45 // Processes the policies handled by this ConfigurationPolicyHandler and sets
46 // the appropriate preferences in |prefs|.
47 virtual void ApplyPolicySettings(const PolicyMap& policies,
48 PrefValueMap* prefs) = 0;
50 // Modifies the values of some of the policies in |policies| so that they
51 // are more suitable to display to the user. This can be used to remove
52 // sensitive values such as passwords, or to pretty-print values.
53 // The base implementation just converts DictionaryValue policies to a
54 // StringValue representation.
55 virtual void PrepareForDisplaying(PolicyMap* policies) const;
58 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler);
61 // Abstract class derived from ConfigurationPolicyHandler that should be
62 // subclassed to handle a single policy (not a combination of policies).
63 class TypeCheckingPolicyHandler : public ConfigurationPolicyHandler {
65 TypeCheckingPolicyHandler(const char* policy_name,
66 base::Value::Type value_type);
67 virtual ~TypeCheckingPolicyHandler();
69 // ConfigurationPolicyHandler methods:
70 virtual bool CheckPolicySettings(const PolicyMap& policies,
71 PolicyErrorMap* errors) OVERRIDE;
73 const char* policy_name() const;
76 // Runs policy checks and returns the policy value if successful.
77 bool CheckAndGetValue(const PolicyMap& policies,
78 PolicyErrorMap* errors,
82 // The name of the policy.
83 const char* policy_name_;
85 // The type the value of the policy should have.
86 base::Value::Type value_type_;
88 DISALLOW_COPY_AND_ASSIGN(TypeCheckingPolicyHandler);
91 // Abstract class derived from TypeCheckingPolicyHandler that ensures an int
92 // policy's value lies in an allowed range. Either clamps or rejects values
94 class IntRangePolicyHandlerBase : public TypeCheckingPolicyHandler {
96 IntRangePolicyHandlerBase(const char* policy_name,
101 // ConfigurationPolicyHandler:
102 virtual bool CheckPolicySettings(const PolicyMap& policies,
103 PolicyErrorMap* errors) OVERRIDE;
106 virtual ~IntRangePolicyHandlerBase();
108 // Ensures that the value is in the allowed range. Returns false if the value
109 // cannot be parsed or lies outside the allowed range and clamping is
111 bool EnsureInRange(const base::Value* input,
113 PolicyErrorMap* errors);
116 // The minimum value allowed.
119 // The maximum value allowed.
122 // Whether to clamp values lying outside the allowed range instead of
126 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase);
129 // ConfigurationPolicyHandler for policies that map directly to a preference.
130 class SimplePolicyHandler : public TypeCheckingPolicyHandler {
132 SimplePolicyHandler(const char* policy_name,
133 const char* pref_path,
134 base::Value::Type value_type);
135 virtual ~SimplePolicyHandler();
137 // ConfigurationPolicyHandler methods:
138 virtual void ApplyPolicySettings(const PolicyMap& policies,
139 PrefValueMap* prefs) OVERRIDE;
142 // The DictionaryValue path of the preference the policy maps to.
143 const char* pref_path_;
145 DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler);
148 // A policy handler implementation that maps a string enum list to an int enum
149 // list as specified by a mapping table.
150 class StringToIntEnumListPolicyHandler : public TypeCheckingPolicyHandler {
152 struct MappingEntry {
153 const char* enum_value;
157 StringToIntEnumListPolicyHandler(const char* policy_name,
158 const char* pref_path,
159 const MappingEntry* mapping_begin,
160 const MappingEntry* mapping_end);
162 // ConfigurationPolicyHandler methods:
163 virtual bool CheckPolicySettings(const PolicyMap& policies,
164 PolicyErrorMap* errors) OVERRIDE;
165 virtual void ApplyPolicySettings(const PolicyMap& policies,
166 PrefValueMap* prefs) OVERRIDE;
169 // Attempts to convert the list in |input| to |output| according to the table,
170 // returns false on errors.
171 bool Convert(const base::Value* input,
172 base::ListValue* output,
173 PolicyErrorMap* errors);
175 // Name of the pref to write.
176 const char* pref_path_;
178 // The mapping table.
179 const MappingEntry* mapping_begin_;
180 const MappingEntry* mapping_end_;
182 DISALLOW_COPY_AND_ASSIGN(StringToIntEnumListPolicyHandler);
185 // A policy handler implementation that ensures an int policy's value lies in an
187 class IntRangePolicyHandler : public IntRangePolicyHandlerBase {
189 IntRangePolicyHandler(const char* policy_name,
190 const char* pref_path,
194 virtual ~IntRangePolicyHandler();
196 // ConfigurationPolicyHandler:
197 virtual void ApplyPolicySettings(const PolicyMap& policies,
198 PrefValueMap* prefs) OVERRIDE;
201 // Name of the pref to write.
202 const char* pref_path_;
204 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandler);
207 // A policy handler implementation that maps an int percentage value to a
209 class IntPercentageToDoublePolicyHandler : public IntRangePolicyHandlerBase {
211 IntPercentageToDoublePolicyHandler(const char* policy_name,
212 const char* pref_path,
216 virtual ~IntPercentageToDoublePolicyHandler();
218 // ConfigurationPolicyHandler:
219 virtual void ApplyPolicySettings(const PolicyMap& policies,
220 PrefValueMap* prefs) OVERRIDE;
223 // Name of the pref to write.
224 const char* pref_path_;
226 DISALLOW_COPY_AND_ASSIGN(IntPercentageToDoublePolicyHandler);
229 } // namespace policy
231 #endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_