- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / configuration_policy_handler.h
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.
4
5 #ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_
6 #define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14
15 class PrefValueMap;
16
17 namespace policy {
18
19 class PolicyErrorMap;
20 class PolicyMap;
21
22 // Maps a policy type to a preference path, and to the expected value type.
23 struct PolicyToPreferenceMapEntry {
24   const char* const policy_name;
25   const char* const preference_path;
26   const base::Value::Type value_type;
27 };
28
29 // An abstract super class that subclasses should implement to map policies to
30 // their corresponding preferences, and to check whether the policies are valid.
31 class ConfigurationPolicyHandler {
32  public:
33   static std::string ValueTypeToString(Value::Type type);
34
35   ConfigurationPolicyHandler();
36   virtual ~ConfigurationPolicyHandler();
37
38   // Returns whether the policy settings handled by this
39   // ConfigurationPolicyHandler can be applied.  Fills |errors| with error
40   // messages or warnings.  |errors| may contain error messages even when
41   // |CheckPolicySettings()| returns true.
42   virtual bool CheckPolicySettings(const PolicyMap& policies,
43                                    PolicyErrorMap* errors) = 0;
44
45   // Processes the policies handled by this ConfigurationPolicyHandler and sets
46   // the appropriate preferences in |prefs|.
47   virtual void ApplyPolicySettings(const PolicyMap& policies,
48                                    PrefValueMap* prefs) = 0;
49
50   // Modifies the values of some of the policies in |policies| so that they
51   // are more suitable to display to the user. This can be used to remove
52   // sensitive values such as passwords, or to pretty-print values.
53   // The base implementation just converts DictionaryValue policies to a
54   // StringValue representation.
55   virtual void PrepareForDisplaying(PolicyMap* policies) const;
56
57  private:
58   DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler);
59 };
60
61 // Abstract class derived from ConfigurationPolicyHandler that should be
62 // subclassed to handle a single policy (not a combination of policies).
63 class TypeCheckingPolicyHandler : public ConfigurationPolicyHandler {
64  public:
65   TypeCheckingPolicyHandler(const char* policy_name,
66                             base::Value::Type value_type);
67   virtual ~TypeCheckingPolicyHandler();
68
69   // ConfigurationPolicyHandler methods:
70   virtual bool CheckPolicySettings(const PolicyMap& policies,
71                                    PolicyErrorMap* errors) OVERRIDE;
72
73   const char* policy_name() const;
74
75  protected:
76   // Runs policy checks and returns the policy value if successful.
77   bool CheckAndGetValue(const PolicyMap& policies,
78                         PolicyErrorMap* errors,
79                         const Value** value);
80
81  private:
82   // The name of the policy.
83   const char* policy_name_;
84
85   // The type the value of the policy should have.
86   base::Value::Type value_type_;
87
88   DISALLOW_COPY_AND_ASSIGN(TypeCheckingPolicyHandler);
89 };
90
91 // Abstract class derived from TypeCheckingPolicyHandler that ensures an int
92 // policy's value lies in an allowed range. Either clamps or rejects values
93 // outside the range.
94 class IntRangePolicyHandlerBase : public TypeCheckingPolicyHandler {
95  public:
96   IntRangePolicyHandlerBase(const char* policy_name,
97                             int min,
98                             int max,
99                             bool clamp);
100
101   // ConfigurationPolicyHandler:
102   virtual bool CheckPolicySettings(const PolicyMap& policies,
103                                    PolicyErrorMap* errors) OVERRIDE;
104
105  protected:
106   virtual ~IntRangePolicyHandlerBase();
107
108   // Ensures that the value is in the allowed range. Returns false if the value
109   // cannot be parsed or lies outside the allowed range and clamping is
110   // disabled.
111   bool EnsureInRange(const base::Value* input,
112                      int* output,
113                      PolicyErrorMap* errors);
114
115  private:
116   // The minimum value allowed.
117   int min_;
118
119   // The maximum value allowed.
120   int max_;
121
122   // Whether to clamp values lying outside the allowed range instead of
123   // rejecting them.
124   bool clamp_;
125
126   DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandlerBase);
127 };
128
129 // ConfigurationPolicyHandler for policies that map directly to a preference.
130 class SimplePolicyHandler : public TypeCheckingPolicyHandler {
131  public:
132   SimplePolicyHandler(const char* policy_name,
133                       const char* pref_path,
134                       base::Value::Type value_type);
135   virtual ~SimplePolicyHandler();
136
137   // ConfigurationPolicyHandler methods:
138   virtual void ApplyPolicySettings(const PolicyMap& policies,
139                                    PrefValueMap* prefs) OVERRIDE;
140
141  private:
142   // The DictionaryValue path of the preference the policy maps to.
143   const char* pref_path_;
144
145   DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler);
146 };
147
148 // A policy handler implementation that maps a string enum list to an int enum
149 // list as specified by a mapping table.
150 class StringToIntEnumListPolicyHandler : public TypeCheckingPolicyHandler {
151  public:
152   struct MappingEntry {
153     const char* enum_value;
154     int int_value;
155   };
156
157   StringToIntEnumListPolicyHandler(const char* policy_name,
158                                    const char* pref_path,
159                                    const MappingEntry* mapping_begin,
160                                    const MappingEntry* mapping_end);
161
162   // ConfigurationPolicyHandler methods:
163   virtual bool CheckPolicySettings(const PolicyMap& policies,
164                                    PolicyErrorMap* errors) OVERRIDE;
165   virtual void ApplyPolicySettings(const PolicyMap& policies,
166                                    PrefValueMap* prefs) OVERRIDE;
167
168  private:
169   // Attempts to convert the list in |input| to |output| according to the table,
170   // returns false on errors.
171   bool Convert(const base::Value* input,
172                base::ListValue* output,
173                PolicyErrorMap* errors);
174
175   // Name of the pref to write.
176   const char* pref_path_;
177
178   // The mapping table.
179   const MappingEntry* mapping_begin_;
180   const MappingEntry* mapping_end_;
181
182   DISALLOW_COPY_AND_ASSIGN(StringToIntEnumListPolicyHandler);
183 };
184
185 // A policy handler implementation that ensures an int policy's value lies in an
186 // allowed range.
187 class IntRangePolicyHandler : public IntRangePolicyHandlerBase {
188  public:
189   IntRangePolicyHandler(const char* policy_name,
190                         const char* pref_path,
191                         int min,
192                         int max,
193                         bool clamp);
194   virtual ~IntRangePolicyHandler();
195
196   // ConfigurationPolicyHandler:
197   virtual void ApplyPolicySettings(const PolicyMap& policies,
198                                    PrefValueMap* prefs) OVERRIDE;
199
200  private:
201   // Name of the pref to write.
202   const char* pref_path_;
203
204   DISALLOW_COPY_AND_ASSIGN(IntRangePolicyHandler);
205 };
206
207 // A policy handler implementation that maps an int percentage value to a
208 // double.
209 class IntPercentageToDoublePolicyHandler : public IntRangePolicyHandlerBase {
210  public:
211   IntPercentageToDoublePolicyHandler(const char* policy_name,
212                                      const char* pref_path,
213                                      int min,
214                                      int max,
215                                      bool clamp);
216   virtual ~IntPercentageToDoublePolicyHandler();
217
218   // ConfigurationPolicyHandler:
219   virtual void ApplyPolicySettings(const PolicyMap& policies,
220                                    PrefValueMap* prefs) OVERRIDE;
221
222  private:
223   // Name of the pref to write.
224   const char* pref_path_;
225
226   DISALLOW_COPY_AND_ASSIGN(IntPercentageToDoublePolicyHandler);
227 };
228
229 }  // namespace policy
230
231 #endif  // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_HANDLER_H_