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"
14 #include "chrome/browser/prefs/incognito_mode_prefs.h"
15 #include "chrome/common/content_settings.h"
24 // An abstract super class that subclasses should implement to map policies to
25 // their corresponding preferences, and to check whether the policies are valid.
26 class ConfigurationPolicyHandler
{
28 static std::string
ValueTypeToString(Value::Type type
);
30 ConfigurationPolicyHandler();
31 virtual ~ConfigurationPolicyHandler();
33 // Returns whether the policy settings handled by this
34 // ConfigurationPolicyHandler can be applied. Fills |errors| with error
35 // messages or warnings. |errors| may contain error messages even when
36 // |CheckPolicySettings()| returns true.
37 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
38 PolicyErrorMap
* errors
) = 0;
40 // Processes the policies handled by this ConfigurationPolicyHandler and sets
41 // the appropriate preferences in |prefs|.
42 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
43 PrefValueMap
* prefs
) = 0;
45 // Modifies the values of some of the policies in |policies| so that they
46 // are more suitable to display to the user. This can be used to remove
47 // sensitive values such as passwords, or to pretty-print values.
48 // The base implementation just converts DictionaryValue policies to a
49 // StringValue representation.
50 virtual void PrepareForDisplaying(PolicyMap
* policies
) const;
53 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler
);
56 // Abstract class derived from ConfigurationPolicyHandler that should be
57 // subclassed to handle a single policy (not a combination of policies).
58 class TypeCheckingPolicyHandler
: public ConfigurationPolicyHandler
{
60 TypeCheckingPolicyHandler(const char* policy_name
,
61 base::Value::Type value_type
);
63 // ConfigurationPolicyHandler methods:
64 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
65 PolicyErrorMap
* errors
) OVERRIDE
;
68 virtual ~TypeCheckingPolicyHandler();
70 // Runs policy checks and returns the policy value if successful.
71 bool CheckAndGetValue(const PolicyMap
& policies
,
72 PolicyErrorMap
* errors
,
75 const char* policy_name() const;
78 // The name of the policy.
79 const char* policy_name_
;
81 // The type the value of the policy should have.
82 base::Value::Type value_type_
;
84 DISALLOW_COPY_AND_ASSIGN(TypeCheckingPolicyHandler
);
87 // Abstract class derived from TypeCheckingPolicyHandler that ensures an int
88 // policy's value lies in an allowed range. Either clamps or rejects values
90 class IntRangePolicyHandlerBase
: public TypeCheckingPolicyHandler
{
92 IntRangePolicyHandlerBase(const char* policy_name
,
97 // ConfigurationPolicyHandler:
98 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
99 PolicyErrorMap
* errors
) OVERRIDE
;
102 virtual ~IntRangePolicyHandlerBase();
104 // Ensures that the value is in the allowed range. Returns false if the value
105 // cannot be parsed or lies outside the allowed range and clamping is
107 bool EnsureInRange(const base::Value
* input
,
109 PolicyErrorMap
* errors
);
112 // The minimum value allowed.
115 // The maximum value allowed.
118 // Whether to clamp values lying outside the allowed range instead of
122 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase
);
125 // ConfigurationPolicyHandler for policies that map directly to a preference.
126 class SimplePolicyHandler
: public TypeCheckingPolicyHandler
{
128 SimplePolicyHandler(const char* policy_name
,
129 const char* pref_path
,
130 base::Value::Type value_type
);
131 virtual ~SimplePolicyHandler();
133 // ConfigurationPolicyHandler methods:
134 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
135 PrefValueMap
* prefs
) OVERRIDE
;
138 // The DictionaryValue path of the preference the policy maps to.
139 const char* pref_path_
;
141 DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler
);
144 // A policy handler implementation that maps a string enum list to an int enum
145 // list as specified by a mapping table.
146 class StringToIntEnumListPolicyHandler
: public TypeCheckingPolicyHandler
{
148 struct MappingEntry
{
149 const char* enum_value
;
153 StringToIntEnumListPolicyHandler(const char* policy_name
,
154 const char* pref_path
,
155 const MappingEntry
* mapping_begin
,
156 const MappingEntry
* mapping_end
);
158 // ConfigurationPolicyHandler methods:
159 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
160 PolicyErrorMap
* errors
) OVERRIDE
;
161 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
162 PrefValueMap
* prefs
) OVERRIDE
;
165 // Attempts to convert the list in |input| to |output| according to the table,
166 // returns false on errors.
167 bool Convert(const base::Value
* input
,
168 base::ListValue
* output
,
169 PolicyErrorMap
* errors
);
171 // Name of the pref to write.
172 const char* pref_path_
;
174 // The mapping table.
175 const MappingEntry
* mapping_begin_
;
176 const MappingEntry
* mapping_end_
;
178 DISALLOW_COPY_AND_ASSIGN(StringToIntEnumListPolicyHandler
);
181 // A policy handler implementation that ensures an int policy's value lies in an
183 class IntRangePolicyHandler
: public IntRangePolicyHandlerBase
{
185 IntRangePolicyHandler(const char* policy_name
,
186 const char* pref_path
,
190 virtual ~IntRangePolicyHandler();
192 // ConfigurationPolicyHandler:
193 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
194 PrefValueMap
* prefs
) OVERRIDE
;
197 // Name of the pref to write.
198 const char* pref_path_
;
200 DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandler
);
203 // A policy handler implementation that maps an int percentage value to a
205 class IntPercentageToDoublePolicyHandler
: public IntRangePolicyHandlerBase
{
207 IntPercentageToDoublePolicyHandler(const char* policy_name
,
208 const char* pref_path
,
212 virtual ~IntPercentageToDoublePolicyHandler();
214 // ConfigurationPolicyHandler:
215 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
216 PrefValueMap
* prefs
) OVERRIDE
;
219 // Name of the pref to write.
220 const char* pref_path_
;
222 DISALLOW_COPY_AND_ASSIGN(IntPercentageToDoublePolicyHandler
);
225 // Implements additional checks for policies that are lists of extension IDs.
226 class ExtensionListPolicyHandler
: public TypeCheckingPolicyHandler
{
228 ExtensionListPolicyHandler(const char* policy_name
,
229 const char* pref_path
,
230 bool allow_wildcards
);
231 virtual ~ExtensionListPolicyHandler();
233 // ConfigurationPolicyHandler methods:
234 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
235 PolicyErrorMap
* errors
) OVERRIDE
;
236 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
237 PrefValueMap
* prefs
) OVERRIDE
;
240 const char* pref_path() const;
242 // Runs sanity checks on the policy value and returns it in |extension_ids|.
243 bool CheckAndGetList(const PolicyMap
& policies
,
244 PolicyErrorMap
* errors
,
245 scoped_ptr
<base::ListValue
>* extension_ids
);
248 const char* pref_path_
;
249 bool allow_wildcards_
;
251 DISALLOW_COPY_AND_ASSIGN(ExtensionListPolicyHandler
);
254 class ExtensionInstallForcelistPolicyHandler
255 : public TypeCheckingPolicyHandler
{
257 ExtensionInstallForcelistPolicyHandler();
258 virtual ~ExtensionInstallForcelistPolicyHandler();
260 // ConfigurationPolicyHandler methods:
261 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
262 PolicyErrorMap
* errors
) OVERRIDE
;
263 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
264 PrefValueMap
* prefs
) OVERRIDE
;
267 // Parses the data in |policy_value| and writes them to |extension_dict|.
268 bool ParseList(const base::Value
* policy_value
,
269 base::DictionaryValue
* extension_dict
,
270 PolicyErrorMap
* errors
);
272 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallForcelistPolicyHandler
);
275 // Implements additional checks for policies that are lists of extension
277 class ExtensionURLPatternListPolicyHandler
: public TypeCheckingPolicyHandler
{
279 ExtensionURLPatternListPolicyHandler(const char* policy_name
,
280 const char* pref_path
);
281 virtual ~ExtensionURLPatternListPolicyHandler();
283 // ConfigurationPolicyHandler methods:
284 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
285 PolicyErrorMap
* errors
) OVERRIDE
;
286 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
287 PrefValueMap
* prefs
) OVERRIDE
;
290 const char* pref_path_
;
292 DISALLOW_COPY_AND_ASSIGN(ExtensionURLPatternListPolicyHandler
);
295 // ConfigurationPolicyHandler for the SyncDisabled policy.
296 class SyncPolicyHandler
: public TypeCheckingPolicyHandler
{
299 virtual ~SyncPolicyHandler();
301 // ConfigurationPolicyHandler methods:
302 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
303 PrefValueMap
* prefs
) OVERRIDE
;
306 DISALLOW_COPY_AND_ASSIGN(SyncPolicyHandler
);
309 // ConfigurationPolicyHandler for the AutofillEnabled policy.
310 class AutofillPolicyHandler
: public TypeCheckingPolicyHandler
{
312 AutofillPolicyHandler();
313 virtual ~AutofillPolicyHandler();
315 // ConfigurationPolicyHandler methods:
316 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
317 PrefValueMap
* prefs
) OVERRIDE
;
320 DISALLOW_COPY_AND_ASSIGN(AutofillPolicyHandler
);
323 #if !defined(OS_ANDROID)
325 // ConfigurationPolicyHandler for the DownloadDirectory policy.
326 class DownloadDirPolicyHandler
: public TypeCheckingPolicyHandler
{
328 DownloadDirPolicyHandler();
329 virtual ~DownloadDirPolicyHandler();
331 // ConfigurationPolicyHandler methods:
332 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
333 PrefValueMap
* prefs
) OVERRIDE
;
336 DISALLOW_COPY_AND_ASSIGN(DownloadDirPolicyHandler
);
339 // ConfigurationPolicyHandler for the DiskCacheDir policy.
340 class DiskCacheDirPolicyHandler
: public TypeCheckingPolicyHandler
{
342 explicit DiskCacheDirPolicyHandler();
343 virtual ~DiskCacheDirPolicyHandler();
345 // ConfigurationPolicyHandler methods:
346 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
347 PrefValueMap
* prefs
) OVERRIDE
;
350 DISALLOW_COPY_AND_ASSIGN(DiskCacheDirPolicyHandler
);
353 #endif // !defined(OS_ANDROID)
355 // ConfigurationPolicyHandler for the FileSelectionDialogsHandler policy.
356 class FileSelectionDialogsHandler
: public TypeCheckingPolicyHandler
{
358 FileSelectionDialogsHandler();
359 virtual ~FileSelectionDialogsHandler();
361 // ConfigurationPolicyHandler methods:
362 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
363 PrefValueMap
* prefs
) OVERRIDE
;
366 DISALLOW_COPY_AND_ASSIGN(FileSelectionDialogsHandler
);
369 // ConfigurationPolicyHandler for the incognito mode policies.
370 class IncognitoModePolicyHandler
: public ConfigurationPolicyHandler
{
372 IncognitoModePolicyHandler();
373 virtual ~IncognitoModePolicyHandler();
375 // ConfigurationPolicyHandler methods:
376 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
377 PolicyErrorMap
* errors
) OVERRIDE
;
378 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
379 PrefValueMap
* prefs
) OVERRIDE
;
382 IncognitoModePrefs::Availability
GetAvailabilityValueAsEnum(
383 const Value
* availability
);
385 DISALLOW_COPY_AND_ASSIGN(IncognitoModePolicyHandler
);
388 // ConfigurationPolicyHandler for the DefaultSearchEncodings policy.
389 class DefaultSearchEncodingsPolicyHandler
: public TypeCheckingPolicyHandler
{
391 DefaultSearchEncodingsPolicyHandler();
392 virtual ~DefaultSearchEncodingsPolicyHandler();
394 // ConfigurationPolicyHandler methods:
395 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
396 PrefValueMap
* prefs
) OVERRIDE
;
399 DISALLOW_COPY_AND_ASSIGN(DefaultSearchEncodingsPolicyHandler
);
402 // ConfigurationPolicyHandler for the default search policies.
403 class DefaultSearchPolicyHandler
: public ConfigurationPolicyHandler
{
405 DefaultSearchPolicyHandler();
406 virtual ~DefaultSearchPolicyHandler();
408 // ConfigurationPolicyHandler methods:
409 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
410 PolicyErrorMap
* errors
) OVERRIDE
;
411 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
412 PrefValueMap
* prefs
) OVERRIDE
;
415 // Calls |CheckPolicySettings()| on each of the handlers in |handlers_|
416 // and returns whether all of the calls succeeded.
417 bool CheckIndividualPolicies(const PolicyMap
& policies
,
418 PolicyErrorMap
* errors
);
420 // Returns whether there is a value for |policy_name| in |policies|.
421 bool HasDefaultSearchPolicy(const PolicyMap
& policies
,
422 const char* policy_name
);
424 // Returns whether any default search policies are specified in |policies|.
425 bool AnyDefaultSearchPoliciesSpecified(const PolicyMap
& policies
);
427 // Returns whether the default search provider is disabled.
428 bool DefaultSearchProviderIsDisabled(const PolicyMap
& policies
);
430 // Returns whether the default search URL is set and valid. On success, both
431 // outparams (which must be non-NULL) are filled with the search URL.
432 bool DefaultSearchURLIsValid(const PolicyMap
& policies
,
433 const Value
** url_value
,
434 std::string
* url_string
);
436 // Make sure that the |path| is present in |prefs_|. If not, set it to
438 void EnsureStringPrefExists(PrefValueMap
* prefs
, const std::string
& path
);
440 // Make sure that the |path| is present in |prefs_| and is a ListValue. If
441 // not, set it to an empty list.
442 void EnsureListPrefExists(PrefValueMap
* prefs
, const std::string
& path
);
444 // The ConfigurationPolicyHandler handlers for each default search policy.
445 std::vector
<ConfigurationPolicyHandler
*> handlers_
;
447 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPolicyHandler
);
450 // ConfigurationPolicyHandler for the proxy policies.
451 class ProxyPolicyHandler
: public ConfigurationPolicyHandler
{
453 // Constants for the "Proxy Server Mode" defined in the policies.
454 // Note that these diverge from internal presentation defined in
455 // ProxyPrefs::ProxyMode for legacy reasons. The following four
456 // PolicyProxyModeType types were not very precise and had overlapping use
459 // Disable Proxy, connect directly.
460 PROXY_SERVER_MODE
= 0,
461 // Auto detect proxy or use specific PAC script if given.
462 PROXY_AUTO_DETECT_PROXY_SERVER_MODE
= 1,
463 // Use manually configured proxy servers (fixed servers).
464 PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
= 2,
465 // Use system proxy server.
466 PROXY_USE_SYSTEM_PROXY_SERVER_MODE
= 3,
471 ProxyPolicyHandler();
472 virtual ~ProxyPolicyHandler();
474 // ConfigurationPolicyHandler methods:
475 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
476 PolicyErrorMap
* errors
) OVERRIDE
;
477 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
478 PrefValueMap
* prefs
) OVERRIDE
;
481 const Value
* GetProxyPolicyValue(const PolicyMap
& policies
,
482 const char* policy_name
);
484 // Converts the deprecated ProxyServerMode policy value to a ProxyMode value
485 // and places the result in |mode_value|. Returns whether the conversion
487 bool CheckProxyModeAndServerMode(const PolicyMap
& policies
,
488 PolicyErrorMap
* errors
,
489 std::string
* mode_value
);
491 DISALLOW_COPY_AND_ASSIGN(ProxyPolicyHandler
);
494 // Handles JavaScript policies.
495 class JavascriptPolicyHandler
: public ConfigurationPolicyHandler
{
497 JavascriptPolicyHandler();
498 virtual ~JavascriptPolicyHandler();
500 // ConfigurationPolicyHandler methods:
501 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
502 PolicyErrorMap
* errors
) OVERRIDE
;
503 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
504 PrefValueMap
* prefs
) OVERRIDE
;
507 DISALLOW_COPY_AND_ASSIGN(JavascriptPolicyHandler
);
510 // Handles URLBlacklist policies.
511 class URLBlacklistPolicyHandler
: public ConfigurationPolicyHandler
{
513 URLBlacklistPolicyHandler();
514 virtual ~URLBlacklistPolicyHandler();
516 // ConfigurationPolicyHandler methods:
517 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
518 PolicyErrorMap
* errors
) OVERRIDE
;
519 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
520 PrefValueMap
* prefs
) OVERRIDE
;
523 DISALLOW_COPY_AND_ASSIGN(URLBlacklistPolicyHandler
);
526 // Handles RestoreOnStartup policy.
527 class RestoreOnStartupPolicyHandler
: public TypeCheckingPolicyHandler
{
529 RestoreOnStartupPolicyHandler();
530 virtual ~RestoreOnStartupPolicyHandler();
532 // ConfigurationPolicyHandler methods:
533 virtual bool CheckPolicySettings(const PolicyMap
& policies
,
534 PolicyErrorMap
* errors
) OVERRIDE
;
535 virtual void ApplyPolicySettings(const PolicyMap
& policies
,
536 PrefValueMap
* prefs
) OVERRIDE
;
539 void ApplyPolicySettingsFromHomePage(const PolicyMap
& policies
,
540 PrefValueMap
* prefs
);
542 DISALLOW_COPY_AND_ASSIGN(RestoreOnStartupPolicyHandler
);
545 } // namespace policy
547 #endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_