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 COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_HANDLER_H_
6 #define COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_HANDLER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/values.h"
16 #include "components/policy/core/common/schema.h"
17 #include "components/policy/policy_export.h"
24 struct PolicyHandlerParameters
;
27 // Maps a policy type to a preference path, and to the expected value type.
28 struct POLICY_EXPORT PolicyToPreferenceMapEntry
{
29 const char* const policy_name
;
30 const char* const preference_path
;
31 const base::Value::Type value_type
;
34 // An abstract super class that subclasses should implement to map policies to
35 // their corresponding preferences, and to check whether the policies are valid.
36 class POLICY_EXPORT ConfigurationPolicyHandler
{
38 static std::string
ValueTypeToString(base::Value::Type type
);
40 ConfigurationPolicyHandler();
41 virtual ~ConfigurationPolicyHandler();
43 // Returns whether the policy settings handled by this
44 // ConfigurationPolicyHandler can be applied. Fills |errors| with error
45 // messages or warnings. |errors| may contain error messages even when
46 // |CheckPolicySettings()| returns true.
47 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
48 PolicyErrorMap
* errors
) = 0;
50 // Processes the policies handled by this ConfigurationPolicyHandler and sets
51 // the appropriate preferences in |prefs|.
52 virtual void ApplyPolicySettingsWithParameters(
53 const PolicyMap
& policies
,
54 const PolicyHandlerParameters
& parameters
,
57 // This is a convenience version of ApplyPolicySettingsWithParameters()
58 // that leaves out the |parameters|. Anyone extending
59 // ConfigurationPolicyHandler should implement either ApplyPolicySettings or
60 // ApplyPolicySettingsWithParameters.
61 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
64 // Modifies the values of some of the policies in |policies| so that they
65 // are more suitable to display to the user. This can be used to remove
66 // sensitive values such as passwords, or to pretty-print values.
67 virtual void PrepareForDisplaying(PolicyMap
* policies
) const;
70 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler
);
73 // Abstract class derived from ConfigurationPolicyHandler that should be
74 // subclassed to handle a single policy (not a combination of policies).
75 class POLICY_EXPORT TypeCheckingPolicyHandler
76 : public ConfigurationPolicyHandler
{
78 TypeCheckingPolicyHandler(const char* policy_name
,
79 base::Value::Type value_type
);
80 virtual ~TypeCheckingPolicyHandler();
82 // ConfigurationPolicyHandler methods:
83 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
84 PolicyErrorMap
* errors
) OVERRIDE
;
86 const char* policy_name() const;
89 // Runs policy checks and returns the policy value if successful.
90 bool CheckAndGetValue(const PolicyMap
& policies
,
91 PolicyErrorMap
* errors
,
92 const base::Value
** value
);
95 // The name of the policy.
96 const char* policy_name_
;
98 // The type the value of the policy should have.
99 base::Value::Type value_type_
;
101 DISALLOW_COPY_AND_ASSIGN(TypeCheckingPolicyHandler
);
104 // Abstract class derived from TypeCheckingPolicyHandler that ensures an int
105 // policy's value lies in an allowed range. Either clamps or rejects values
106 // outside the range.
107 class POLICY_EXPORT IntRangePolicyHandlerBase
108 : public TypeCheckingPolicyHandler
{
110 IntRangePolicyHandlerBase(const char* policy_name
,
115 // ConfigurationPolicyHandler:
116 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
117 PolicyErrorMap
* errors
) OVERRIDE
;
120 virtual ~IntRangePolicyHandlerBase();
122 // Ensures that the value is in the allowed range. Returns false if the value
123 // cannot be parsed or lies outside the allowed range and clamping is
125 bool EnsureInRange(const base::Value
* input
,
127 PolicyErrorMap
* errors
);
130 // The minimum value allowed.
133 // The maximum value allowed.
136 // Whether to clamp values lying outside the allowed range instead of
140 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase
);
143 // ConfigurationPolicyHandler for policies that map directly to a preference.
144 class POLICY_EXPORT SimplePolicyHandler
: public TypeCheckingPolicyHandler
{
146 SimplePolicyHandler(const char* policy_name
,
147 const char* pref_path
,
148 base::Value::Type value_type
);
149 virtual ~SimplePolicyHandler();
151 // ConfigurationPolicyHandler methods:
152 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
153 PrefValueMap
* prefs
) OVERRIDE
;
156 // The DictionaryValue path of the preference the policy maps to.
157 const char* pref_path_
;
159 DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler
);
162 // A policy handler implementation that maps a string enum list to an int enum
163 // list as specified by a mapping table.
164 class POLICY_EXPORT StringToIntEnumListPolicyHandler
165 : public TypeCheckingPolicyHandler
{
167 struct POLICY_EXPORT MappingEntry
{
168 const char* enum_value
;
172 StringToIntEnumListPolicyHandler(const char* policy_name
,
173 const char* pref_path
,
174 const MappingEntry
* mapping_begin
,
175 const MappingEntry
* mapping_end
);
177 // ConfigurationPolicyHandler methods:
178 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
179 PolicyErrorMap
* errors
) OVERRIDE
;
180 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
181 PrefValueMap
* prefs
) OVERRIDE
;
184 // Attempts to convert the list in |input| to |output| according to the table,
185 // returns false on errors.
186 bool Convert(const base::Value
* input
,
187 base::ListValue
* output
,
188 PolicyErrorMap
* errors
);
190 // Name of the pref to write.
191 const char* pref_path_
;
193 // The mapping table.
194 const MappingEntry
* mapping_begin_
;
195 const MappingEntry
* mapping_end_
;
197 DISALLOW_COPY_AND_ASSIGN(StringToIntEnumListPolicyHandler
);
200 // A policy handler implementation that ensures an int policy's value lies in an
202 class POLICY_EXPORT IntRangePolicyHandler
: public IntRangePolicyHandlerBase
{
204 IntRangePolicyHandler(const char* policy_name
,
205 const char* pref_path
,
209 virtual ~IntRangePolicyHandler();
211 // ConfigurationPolicyHandler:
212 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
213 PrefValueMap
* prefs
) OVERRIDE
;
216 // Name of the pref to write.
217 const char* pref_path_
;
219 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandler
);
222 // A policy handler implementation that maps an int percentage value to a
224 class POLICY_EXPORT IntPercentageToDoublePolicyHandler
225 : public IntRangePolicyHandlerBase
{
227 IntPercentageToDoublePolicyHandler(const char* policy_name
,
228 const char* pref_path
,
232 virtual ~IntPercentageToDoublePolicyHandler();
234 // ConfigurationPolicyHandler:
235 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
236 PrefValueMap
* prefs
) OVERRIDE
;
239 // Name of the pref to write.
240 const char* pref_path_
;
242 DISALLOW_COPY_AND_ASSIGN(IntPercentageToDoublePolicyHandler
);
245 // Like TypeCheckingPolicyHandler, but validates against a schema instead of a
246 // single type. |schema| is the schema used for this policy, and |strategy| is
247 // the strategy used for schema validation errors.
248 class POLICY_EXPORT SchemaValidatingPolicyHandler
249 : public ConfigurationPolicyHandler
{
251 SchemaValidatingPolicyHandler(const char* policy_name
,
253 SchemaOnErrorStrategy strategy
);
254 virtual ~SchemaValidatingPolicyHandler();
256 // ConfigurationPolicyHandler:
257 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
258 PolicyErrorMap
* errors
) OVERRIDE
;
260 const char* policy_name() const;
263 // Runs policy checks and returns the policy value if successful.
264 bool CheckAndGetValue(const PolicyMap
& policies
,
265 PolicyErrorMap
* errors
,
266 scoped_ptr
<base::Value
>* output
);
269 const char* policy_name_
;
271 SchemaOnErrorStrategy strategy_
;
273 DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPolicyHandler
);
276 // A policy handler to deprecate multiple legacy policies with a new one.
277 // This handler will completely ignore any of legacy policy values if the new
279 class POLICY_EXPORT LegacyPoliciesDeprecatingPolicyHandler
280 : public ConfigurationPolicyHandler
{
282 LegacyPoliciesDeprecatingPolicyHandler(
283 ScopedVector
<ConfigurationPolicyHandler
> legacy_policy_handlers
,
284 scoped_ptr
<SchemaValidatingPolicyHandler
> new_policy_handler
);
285 virtual ~LegacyPoliciesDeprecatingPolicyHandler();
287 // ConfigurationPolicyHandler:
288 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
289 PolicyErrorMap
* errors
) OVERRIDE
;
290 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
291 PrefValueMap
* prefs
) OVERRIDE
;
294 ScopedVector
<ConfigurationPolicyHandler
> legacy_policy_handlers_
;
295 scoped_ptr
<SchemaValidatingPolicyHandler
> new_policy_handler_
;
297 DISALLOW_COPY_AND_ASSIGN(LegacyPoliciesDeprecatingPolicyHandler
);
300 } // namespace policy
302 #endif // COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_HANDLER_H_