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/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/values.h"
17 #include "components/policy/core/common/schema.h"
18 #include "components/policy/policy_export.h"
25 struct PolicyHandlerParameters
;
28 // Maps a policy type to a preference path, and to the expected value type.
29 struct POLICY_EXPORT PolicyToPreferenceMapEntry
{
30 const char* const policy_name
;
31 const char* const preference_path
;
32 const base::Value::Type value_type
;
35 // An abstract super class that subclasses should implement to map policies to
36 // their corresponding preferences, and to check whether the policies are valid.
37 class POLICY_EXPORT ConfigurationPolicyHandler
{
39 static std::string
ValueTypeToString(base::Value::Type type
);
41 ConfigurationPolicyHandler();
42 virtual ~ConfigurationPolicyHandler();
44 // Returns whether the policy settings handled by this
45 // ConfigurationPolicyHandler can be applied. Fills |errors| with error
46 // messages or warnings. |errors| may contain error messages even when
47 // |CheckPolicySettings()| returns true.
48 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
49 PolicyErrorMap
* errors
) = 0;
51 // Processes the policies handled by this ConfigurationPolicyHandler and sets
52 // the appropriate preferences in |prefs|.
53 virtual void ApplyPolicySettingsWithParameters(
54 const PolicyMap
& policies
,
55 const PolicyHandlerParameters
& parameters
,
58 // Modifies the values of some of the policies in |policies| so that they
59 // are more suitable to display to the user. This can be used to remove
60 // sensitive values such as passwords, or to pretty-print values.
61 virtual void PrepareForDisplaying(PolicyMap
* policies
) const;
64 // This is a convenience version of ApplyPolicySettingsWithParameters()
65 // for derived classes that leaves out the |parameters|. Anyone extending
66 // ConfigurationPolicyHandler should implement either
67 // ApplyPolicySettingsWithParameters directly and implement
68 // ApplyPolicySettings with a NOTREACHED or implement only
69 // ApplyPolicySettings.
70 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
71 PrefValueMap
* prefs
) = 0;
74 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler
);
77 // Abstract class derived from ConfigurationPolicyHandler that should be
78 // subclassed to handle a single policy (not a combination of policies).
79 class POLICY_EXPORT TypeCheckingPolicyHandler
80 : public ConfigurationPolicyHandler
{
82 TypeCheckingPolicyHandler(const char* policy_name
,
83 base::Value::Type value_type
);
84 ~TypeCheckingPolicyHandler() override
;
86 // ConfigurationPolicyHandler methods:
87 bool CheckPolicySettings(const PolicyMap
& policies
,
88 PolicyErrorMap
* errors
) override
;
90 const char* policy_name() const;
93 // Runs policy checks and returns the policy value if successful.
94 bool CheckAndGetValue(const PolicyMap
& policies
,
95 PolicyErrorMap
* errors
,
96 const base::Value
** value
);
99 // The name of the policy.
100 const char* policy_name_
;
102 // The type the value of the policy should have.
103 base::Value::Type value_type_
;
105 DISALLOW_COPY_AND_ASSIGN(TypeCheckingPolicyHandler
);
108 // Abstract class derived from TypeCheckingPolicyHandler that ensures an int
109 // policy's value lies in an allowed range. Either clamps or rejects values
110 // outside the range.
111 class POLICY_EXPORT IntRangePolicyHandlerBase
112 : public TypeCheckingPolicyHandler
{
114 IntRangePolicyHandlerBase(const char* policy_name
,
119 // ConfigurationPolicyHandler:
120 bool CheckPolicySettings(const PolicyMap
& policies
,
121 PolicyErrorMap
* errors
) override
;
124 ~IntRangePolicyHandlerBase() override
;
126 // Ensures that the value is in the allowed range. Returns false if the value
127 // cannot be parsed or lies outside the allowed range and clamping is
129 bool EnsureInRange(const base::Value
* input
,
131 PolicyErrorMap
* errors
);
134 // The minimum value allowed.
137 // The maximum value allowed.
140 // Whether to clamp values lying outside the allowed range instead of
144 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase
);
147 // ConfigurationPolicyHandler for policies that map directly to a preference.
148 class POLICY_EXPORT SimplePolicyHandler
: public TypeCheckingPolicyHandler
{
150 SimplePolicyHandler(const char* policy_name
,
151 const char* pref_path
,
152 base::Value::Type value_type
);
153 ~SimplePolicyHandler() override
;
155 // ConfigurationPolicyHandler methods:
156 void ApplyPolicySettings(const PolicyMap
& policies
,
157 PrefValueMap
* prefs
) override
;
160 // The DictionaryValue path of the preference the policy maps to.
161 const char* pref_path_
;
163 DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler
);
166 // Base class that encapsulates logic for mapping from a string enum list
167 // to a separate matching type value.
168 class POLICY_EXPORT StringMappingListPolicyHandler
169 : public TypeCheckingPolicyHandler
{
171 // Data structure representing the map between policy strings and
172 // matching pref values.
173 class POLICY_EXPORT MappingEntry
{
175 MappingEntry(const char* policy_value
, scoped_ptr
<base::Value
> map
);
178 const char* enum_value
;
179 scoped_ptr
<base::Value
> mapped_value
;
182 // Callback that generates the map for this instance.
183 typedef base::Callback
<void(ScopedVector
<MappingEntry
>*)> GenerateMapCallback
;
185 StringMappingListPolicyHandler(const char* policy_name
,
186 const char* pref_path
,
187 const GenerateMapCallback
& map_generator
);
188 ~StringMappingListPolicyHandler() override
;
190 // ConfigurationPolicyHandler methods:
191 bool CheckPolicySettings(const PolicyMap
& policies
,
192 PolicyErrorMap
* errors
) override
;
193 void ApplyPolicySettings(const PolicyMap
& policies
,
194 PrefValueMap
* prefs
) override
;
197 // Attempts to convert the list in |input| to |output| according to the table,
198 // returns false on errors.
199 bool Convert(const base::Value
* input
,
200 base::ListValue
* output
,
201 PolicyErrorMap
* errors
);
203 // Helper method that converts from a policy value string to the associated
205 scoped_ptr
<base::Value
> Map(const std::string
& entry_value
);
207 // Name of the pref to write.
208 const char* pref_path_
;
210 // The callback invoked to generate the map for this instance.
211 GenerateMapCallback map_getter_
;
213 // Map of string policy values to local pref values. This is generated lazily
214 // so the generation does not have to happen if no policy is present.
215 ScopedVector
<MappingEntry
> map_
;
217 DISALLOW_COPY_AND_ASSIGN(StringMappingListPolicyHandler
);
220 // A policy handler implementation that ensures an int policy's value lies in an
222 class POLICY_EXPORT IntRangePolicyHandler
: public IntRangePolicyHandlerBase
{
224 IntRangePolicyHandler(const char* policy_name
,
225 const char* pref_path
,
229 ~IntRangePolicyHandler() override
;
231 // ConfigurationPolicyHandler:
232 void ApplyPolicySettings(const PolicyMap
& policies
,
233 PrefValueMap
* prefs
) override
;
236 // Name of the pref to write.
237 const char* pref_path_
;
239 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandler
);
242 // A policy handler implementation that maps an int percentage value to a
244 class POLICY_EXPORT IntPercentageToDoublePolicyHandler
245 : public IntRangePolicyHandlerBase
{
247 IntPercentageToDoublePolicyHandler(const char* policy_name
,
248 const char* pref_path
,
252 ~IntPercentageToDoublePolicyHandler() override
;
254 // ConfigurationPolicyHandler:
255 void ApplyPolicySettings(const PolicyMap
& policies
,
256 PrefValueMap
* prefs
) override
;
259 // Name of the pref to write.
260 const char* pref_path_
;
262 DISALLOW_COPY_AND_ASSIGN(IntPercentageToDoublePolicyHandler
);
265 // Like TypeCheckingPolicyHandler, but validates against a schema instead of a
266 // single type. |schema| is the schema used for this policy, and |strategy| is
267 // the strategy used for schema validation errors.
268 class POLICY_EXPORT SchemaValidatingPolicyHandler
269 : public ConfigurationPolicyHandler
{
271 SchemaValidatingPolicyHandler(const char* policy_name
,
273 SchemaOnErrorStrategy strategy
);
274 ~SchemaValidatingPolicyHandler() override
;
276 // ConfigurationPolicyHandler:
277 bool CheckPolicySettings(const PolicyMap
& policies
,
278 PolicyErrorMap
* errors
) override
;
280 const char* policy_name() const;
283 // Runs policy checks and returns the policy value if successful.
284 bool CheckAndGetValue(const PolicyMap
& policies
,
285 PolicyErrorMap
* errors
,
286 scoped_ptr
<base::Value
>* output
);
289 const char* policy_name_
;
291 SchemaOnErrorStrategy strategy_
;
293 DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPolicyHandler
);
296 // Maps policy to pref like SimplePolicyHandler while ensuring that the value
297 // set matches the schema. |schema| is the schema used for policies, and
298 // |strategy| is the strategy used for schema validation errors. The
299 // |recommended_permission| and |mandatory_permission| flags indicate the levels
300 // at which the policy can be set. A value set at an unsupported level will be
302 class POLICY_EXPORT SimpleSchemaValidatingPolicyHandler
303 : public SchemaValidatingPolicyHandler
{
305 enum MandatoryPermission
{ MANDATORY_ALLOWED
, MANDATORY_PROHIBITED
};
306 enum RecommendedPermission
{ RECOMMENDED_ALLOWED
, RECOMMENDED_PROHIBITED
};
308 SimpleSchemaValidatingPolicyHandler(
309 const char* policy_name
,
310 const char* pref_path
,
312 SchemaOnErrorStrategy strategy
,
313 RecommendedPermission recommended_permission
,
314 MandatoryPermission mandatory_permission
);
315 ~SimpleSchemaValidatingPolicyHandler() override
;
317 // ConfigurationPolicyHandler:
318 bool CheckPolicySettings(const PolicyMap
& policies
,
319 PolicyErrorMap
* errors
) override
;
320 void ApplyPolicySettings(const PolicyMap
& policies
,
321 PrefValueMap
* prefs
) override
;
324 const char* pref_path_
;
325 const bool allow_recommended_
;
326 const bool allow_mandatory_
;
328 DISALLOW_COPY_AND_ASSIGN(SimpleSchemaValidatingPolicyHandler
);
331 // A policy handler to deprecate multiple legacy policies with a new one.
332 // This handler will completely ignore any of legacy policy values if the new
334 class POLICY_EXPORT LegacyPoliciesDeprecatingPolicyHandler
335 : public ConfigurationPolicyHandler
{
337 LegacyPoliciesDeprecatingPolicyHandler(
338 ScopedVector
<ConfigurationPolicyHandler
> legacy_policy_handlers
,
339 scoped_ptr
<SchemaValidatingPolicyHandler
> new_policy_handler
);
340 ~LegacyPoliciesDeprecatingPolicyHandler() override
;
342 // ConfigurationPolicyHandler:
343 bool CheckPolicySettings(const PolicyMap
& policies
,
344 PolicyErrorMap
* errors
) override
;
345 void ApplyPolicySettingsWithParameters(
346 const policy::PolicyMap
& policies
,
347 const policy::PolicyHandlerParameters
& parameters
,
348 PrefValueMap
* prefs
) override
;
351 void ApplyPolicySettings(const PolicyMap
& policies
,
352 PrefValueMap
* prefs
) override
;
355 ScopedVector
<ConfigurationPolicyHandler
> legacy_policy_handlers_
;
356 scoped_ptr
<SchemaValidatingPolicyHandler
> new_policy_handler_
;
358 DISALLOW_COPY_AND_ASSIGN(LegacyPoliciesDeprecatingPolicyHandler
);
361 } // namespace policy
363 #endif // COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_HANDLER_H_