Fix crash on app list start page contents not existing.
[chromium-blink-merge.git] / components / policy / core / browser / configuration_policy_handler.h
blobc8101d77faf7e56363a29a932dcc493c7deef932
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 // This is a convenience version of ApplyPolicySettingsWithParameters()
59 // that leaves out the |parameters|. Anyone extending
60 // ConfigurationPolicyHandler should implement either ApplyPolicySettings or
61 // ApplyPolicySettingsWithParameters.
62 virtual void ApplyPolicySettings(const PolicyMap& policies,
63 PrefValueMap* prefs);
65 // Modifies the values of some of the policies in |policies| so that they
66 // are more suitable to display to the user. This can be used to remove
67 // sensitive values such as passwords, or to pretty-print values.
68 virtual void PrepareForDisplaying(PolicyMap* policies) const;
70 private:
71 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler);
74 // Abstract class derived from ConfigurationPolicyHandler that should be
75 // subclassed to handle a single policy (not a combination of policies).
76 class POLICY_EXPORT TypeCheckingPolicyHandler
77 : public ConfigurationPolicyHandler {
78 public:
79 TypeCheckingPolicyHandler(const char* policy_name,
80 base::Value::Type value_type);
81 ~TypeCheckingPolicyHandler() override;
83 // ConfigurationPolicyHandler methods:
84 bool CheckPolicySettings(const PolicyMap& policies,
85 PolicyErrorMap* errors) override;
87 const char* policy_name() const;
89 protected:
90 // Runs policy checks and returns the policy value if successful.
91 bool CheckAndGetValue(const PolicyMap& policies,
92 PolicyErrorMap* errors,
93 const base::Value** value);
95 private:
96 // The name of the policy.
97 const char* policy_name_;
99 // The type the value of the policy should have.
100 base::Value::Type value_type_;
102 DISALLOW_COPY_AND_ASSIGN(TypeCheckingPolicyHandler);
105 // Abstract class derived from TypeCheckingPolicyHandler that ensures an int
106 // policy's value lies in an allowed range. Either clamps or rejects values
107 // outside the range.
108 class POLICY_EXPORT IntRangePolicyHandlerBase
109 : public TypeCheckingPolicyHandler {
110 public:
111 IntRangePolicyHandlerBase(const char* policy_name,
112 int min,
113 int max,
114 bool clamp);
116 // ConfigurationPolicyHandler:
117 bool CheckPolicySettings(const PolicyMap& policies,
118 PolicyErrorMap* errors) override;
120 protected:
121 ~IntRangePolicyHandlerBase() override;
123 // Ensures that the value is in the allowed range. Returns false if the value
124 // cannot be parsed or lies outside the allowed range and clamping is
125 // disabled.
126 bool EnsureInRange(const base::Value* input,
127 int* output,
128 PolicyErrorMap* errors);
130 private:
131 // The minimum value allowed.
132 int min_;
134 // The maximum value allowed.
135 int max_;
137 // Whether to clamp values lying outside the allowed range instead of
138 // rejecting them.
139 bool clamp_;
141 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase);
144 // ConfigurationPolicyHandler for policies that map directly to a preference.
145 class POLICY_EXPORT SimplePolicyHandler : public TypeCheckingPolicyHandler {
146 public:
147 SimplePolicyHandler(const char* policy_name,
148 const char* pref_path,
149 base::Value::Type value_type);
150 ~SimplePolicyHandler() override;
152 // ConfigurationPolicyHandler methods:
153 void ApplyPolicySettings(const PolicyMap& policies,
154 PrefValueMap* prefs) override;
156 private:
157 // The DictionaryValue path of the preference the policy maps to.
158 const char* pref_path_;
160 DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler);
163 // Base class that encapsulates logic for mapping from a string enum list
164 // to a separate matching type value.
165 class POLICY_EXPORT StringMappingListPolicyHandler
166 : public TypeCheckingPolicyHandler {
167 public:
168 // Data structure representing the map between policy strings and
169 // matching pref values.
170 class POLICY_EXPORT MappingEntry {
171 public:
172 MappingEntry(const char* policy_value, scoped_ptr<base::Value> map);
173 ~MappingEntry();
175 const char* enum_value;
176 scoped_ptr<base::Value> mapped_value;
179 // Callback that generates the map for this instance.
180 typedef base::Callback<void(ScopedVector<MappingEntry>*)> GenerateMapCallback;
182 StringMappingListPolicyHandler(const char* policy_name,
183 const char* pref_path,
184 const GenerateMapCallback& map_generator);
185 ~StringMappingListPolicyHandler() override;
187 // ConfigurationPolicyHandler methods:
188 bool CheckPolicySettings(const PolicyMap& policies,
189 PolicyErrorMap* errors) override;
190 void ApplyPolicySettings(const PolicyMap& policies,
191 PrefValueMap* prefs) override;
193 private:
194 // Attempts to convert the list in |input| to |output| according to the table,
195 // returns false on errors.
196 bool Convert(const base::Value* input,
197 base::ListValue* output,
198 PolicyErrorMap* errors);
200 // Helper method that converts from a policy value string to the associated
201 // pref value.
202 scoped_ptr<base::Value> Map(const std::string& entry_value);
204 // Name of the pref to write.
205 const char* pref_path_;
207 // The callback invoked to generate the map for this instance.
208 GenerateMapCallback map_getter_;
210 // Map of string policy values to local pref values. This is generated lazily
211 // so the generation does not have to happen if no policy is present.
212 ScopedVector<MappingEntry> map_;
214 DISALLOW_COPY_AND_ASSIGN(StringMappingListPolicyHandler);
217 // A policy handler implementation that ensures an int policy's value lies in an
218 // allowed range.
219 class POLICY_EXPORT IntRangePolicyHandler : public IntRangePolicyHandlerBase {
220 public:
221 IntRangePolicyHandler(const char* policy_name,
222 const char* pref_path,
223 int min,
224 int max,
225 bool clamp);
226 ~IntRangePolicyHandler() override;
228 // ConfigurationPolicyHandler:
229 void ApplyPolicySettings(const PolicyMap& policies,
230 PrefValueMap* prefs) override;
232 private:
233 // Name of the pref to write.
234 const char* pref_path_;
236 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandler);
239 // A policy handler implementation that maps an int percentage value to a
240 // double.
241 class POLICY_EXPORT IntPercentageToDoublePolicyHandler
242 : public IntRangePolicyHandlerBase {
243 public:
244 IntPercentageToDoublePolicyHandler(const char* policy_name,
245 const char* pref_path,
246 int min,
247 int max,
248 bool clamp);
249 ~IntPercentageToDoublePolicyHandler() override;
251 // ConfigurationPolicyHandler:
252 void ApplyPolicySettings(const PolicyMap& policies,
253 PrefValueMap* prefs) override;
255 private:
256 // Name of the pref to write.
257 const char* pref_path_;
259 DISALLOW_COPY_AND_ASSIGN(IntPercentageToDoublePolicyHandler);
262 // Like TypeCheckingPolicyHandler, but validates against a schema instead of a
263 // single type. |schema| is the schema used for this policy, and |strategy| is
264 // the strategy used for schema validation errors.
265 class POLICY_EXPORT SchemaValidatingPolicyHandler
266 : public ConfigurationPolicyHandler {
267 public:
268 SchemaValidatingPolicyHandler(const char* policy_name,
269 Schema schema,
270 SchemaOnErrorStrategy strategy);
271 ~SchemaValidatingPolicyHandler() override;
273 // ConfigurationPolicyHandler:
274 bool CheckPolicySettings(const PolicyMap& policies,
275 PolicyErrorMap* errors) override;
277 const char* policy_name() const;
279 protected:
280 // Runs policy checks and returns the policy value if successful.
281 bool CheckAndGetValue(const PolicyMap& policies,
282 PolicyErrorMap* errors,
283 scoped_ptr<base::Value>* output);
285 private:
286 const char* policy_name_;
287 Schema schema_;
288 SchemaOnErrorStrategy strategy_;
290 DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPolicyHandler);
293 // Maps policy to pref like SimplePolicyHandler while ensuring that the value
294 // set matches the schema. |schema| is the schema used for policies, and
295 // |strategy| is the strategy used for schema validation errors. The
296 // |recommended_permission| and |mandatory_permission| flags indicate the levels
297 // at which the policy can be set. A value set at an unsupported level will be
298 // ignored.
299 class POLICY_EXPORT SimpleSchemaValidatingPolicyHandler
300 : public SchemaValidatingPolicyHandler {
301 public:
302 enum MandatoryPermission { MANDATORY_ALLOWED, MANDATORY_PROHIBITED };
303 enum RecommendedPermission { RECOMMENDED_ALLOWED, RECOMMENDED_PROHIBITED };
305 SimpleSchemaValidatingPolicyHandler(
306 const char* policy_name,
307 const char* pref_path,
308 Schema schema,
309 SchemaOnErrorStrategy strategy,
310 RecommendedPermission recommended_permission,
311 MandatoryPermission mandatory_permission);
312 ~SimpleSchemaValidatingPolicyHandler() override;
314 // ConfigurationPolicyHandler:
315 bool CheckPolicySettings(const PolicyMap& policies,
316 PolicyErrorMap* errors) override;
317 void ApplyPolicySettings(const PolicyMap& policies,
318 PrefValueMap* prefs) override;
320 private:
321 const char* pref_path_;
322 const bool allow_recommended_;
323 const bool allow_mandatory_;
325 DISALLOW_COPY_AND_ASSIGN(SimpleSchemaValidatingPolicyHandler);
328 // A policy handler to deprecate multiple legacy policies with a new one.
329 // This handler will completely ignore any of legacy policy values if the new
330 // one is set.
331 class POLICY_EXPORT LegacyPoliciesDeprecatingPolicyHandler
332 : public ConfigurationPolicyHandler {
333 public:
334 LegacyPoliciesDeprecatingPolicyHandler(
335 ScopedVector<ConfigurationPolicyHandler> legacy_policy_handlers,
336 scoped_ptr<SchemaValidatingPolicyHandler> new_policy_handler);
337 ~LegacyPoliciesDeprecatingPolicyHandler() override;
339 // ConfigurationPolicyHandler:
340 bool CheckPolicySettings(const PolicyMap& policies,
341 PolicyErrorMap* errors) override;
342 void ApplyPolicySettings(const PolicyMap& policies,
343 PrefValueMap* prefs) override;
345 private:
346 ScopedVector<ConfigurationPolicyHandler> legacy_policy_handlers_;
347 scoped_ptr<SchemaValidatingPolicyHandler> new_policy_handler_;
349 DISALLOW_COPY_AND_ASSIGN(LegacyPoliciesDeprecatingPolicyHandler);
352 } // namespace policy
354 #endif // COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_HANDLER_H_