Update V8 to version 4.6.62.
[chromium-blink-merge.git] / components / policy / core / browser / configuration_policy_handler.h
blob1fdddc47d6be6d578b2ee83a8f227b2419a7ced7
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_
8 #include <string>
9 #include <vector>
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"
20 class PrefValueMap;
22 namespace policy {
24 class PolicyErrorMap;
25 struct PolicyHandlerParameters;
26 class PolicyMap;
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 {
38 public:
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,
56 PrefValueMap* prefs);
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;
63 protected:
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;
73 private:
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 {
81 public:
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;
92 protected:
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);
98 private:
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 {
113 public:
114 IntRangePolicyHandlerBase(const char* policy_name,
115 int min,
116 int max,
117 bool clamp);
119 // ConfigurationPolicyHandler:
120 bool CheckPolicySettings(const PolicyMap& policies,
121 PolicyErrorMap* errors) override;
123 protected:
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
128 // disabled.
129 bool EnsureInRange(const base::Value* input,
130 int* output,
131 PolicyErrorMap* errors);
133 private:
134 // The minimum value allowed.
135 int min_;
137 // The maximum value allowed.
138 int max_;
140 // Whether to clamp values lying outside the allowed range instead of
141 // rejecting them.
142 bool clamp_;
144 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase);
147 // ConfigurationPolicyHandler for policies that map directly to a preference.
148 class POLICY_EXPORT SimplePolicyHandler : public TypeCheckingPolicyHandler {
149 public:
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;
159 private:
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 {
170 public:
171 // Data structure representing the map between policy strings and
172 // matching pref values.
173 class POLICY_EXPORT MappingEntry {
174 public:
175 MappingEntry(const char* policy_value, scoped_ptr<base::Value> map);
176 ~MappingEntry();
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;
196 private:
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
204 // pref value.
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
221 // allowed range.
222 class POLICY_EXPORT IntRangePolicyHandler : public IntRangePolicyHandlerBase {
223 public:
224 IntRangePolicyHandler(const char* policy_name,
225 const char* pref_path,
226 int min,
227 int max,
228 bool clamp);
229 ~IntRangePolicyHandler() override;
231 // ConfigurationPolicyHandler:
232 void ApplyPolicySettings(const PolicyMap& policies,
233 PrefValueMap* prefs) override;
235 private:
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
243 // double.
244 class POLICY_EXPORT IntPercentageToDoublePolicyHandler
245 : public IntRangePolicyHandlerBase {
246 public:
247 IntPercentageToDoublePolicyHandler(const char* policy_name,
248 const char* pref_path,
249 int min,
250 int max,
251 bool clamp);
252 ~IntPercentageToDoublePolicyHandler() override;
254 // ConfigurationPolicyHandler:
255 void ApplyPolicySettings(const PolicyMap& policies,
256 PrefValueMap* prefs) override;
258 private:
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 {
270 public:
271 SchemaValidatingPolicyHandler(const char* policy_name,
272 Schema schema,
273 SchemaOnErrorStrategy strategy);
274 ~SchemaValidatingPolicyHandler() override;
276 // ConfigurationPolicyHandler:
277 bool CheckPolicySettings(const PolicyMap& policies,
278 PolicyErrorMap* errors) override;
280 const char* policy_name() const;
282 protected:
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);
288 private:
289 const char* policy_name_;
290 Schema schema_;
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
301 // ignored.
302 class POLICY_EXPORT SimpleSchemaValidatingPolicyHandler
303 : public SchemaValidatingPolicyHandler {
304 public:
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,
311 Schema schema,
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;
323 private:
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
333 // one is set.
334 class POLICY_EXPORT LegacyPoliciesDeprecatingPolicyHandler
335 : public ConfigurationPolicyHandler {
336 public:
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;
350 protected:
351 void ApplyPolicySettings(const PolicyMap& policies,
352 PrefValueMap* prefs) override;
354 private:
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_