Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: components/policy/core/browser/configuration_policy_handler.cc

Issue 341723002: Refactored StringToIntEnumListPolicyHandler to be more general purpose (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review feedback. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/policy/core/browser/configuration_policy_handler.h" 5 #include "components/policy/core/browser/configuration_policy_handler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 144
145 value = std::min(std::max(value, min_), max_); 145 value = std::min(std::max(value, min_), max_);
146 } 146 }
147 147
148 if (output) 148 if (output)
149 *output = value; 149 *output = value;
150 return true; 150 return true;
151 } 151 }
152 152
153 153
154 // StringToIntEnumListPolicyHandler implementation ----------------------------- 154 // StringMappingListPolicyHandler implementation -----------------------------
155 155
156 StringToIntEnumListPolicyHandler::StringToIntEnumListPolicyHandler( 156 StringMappingListPolicyHandler::MappingEntry::MappingEntry(
157 const char* policy_value, scoped_ptr<base::Value> map)
158 : enum_value(policy_value), mapped_value(map.Pass()) {}
159
160 StringMappingListPolicyHandler::MappingEntry::~MappingEntry() {}
161
162 StringMappingListPolicyHandler::StringMappingListPolicyHandler(
157 const char* policy_name, 163 const char* policy_name,
158 const char* pref_path, 164 const char* pref_path,
159 const MappingEntry* mapping_begin, 165 const GenerateMapCallback& callback)
160 const MappingEntry* mapping_end)
161 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST), 166 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST),
162 pref_path_(pref_path), 167 pref_path_(pref_path),
163 mapping_begin_(mapping_begin), 168 map_getter_(callback) {}
164 mapping_end_(mapping_end) {}
165 169
166 bool StringToIntEnumListPolicyHandler::CheckPolicySettings( 170 StringMappingListPolicyHandler::~StringMappingListPolicyHandler() {}
171
172 bool StringMappingListPolicyHandler::CheckPolicySettings(
167 const PolicyMap& policies, 173 const PolicyMap& policies,
168 PolicyErrorMap* errors) { 174 PolicyErrorMap* errors) {
169 const base::Value* value; 175 const base::Value* value;
170 return CheckAndGetValue(policies, errors, &value) && 176 return CheckAndGetValue(policies, errors, &value) &&
171 Convert(value, NULL, errors); 177 Convert(value, NULL, errors);
172 } 178 }
173 179
174 void StringToIntEnumListPolicyHandler::ApplyPolicySettings( 180 void StringMappingListPolicyHandler::ApplyPolicySettings(
175 const PolicyMap& policies, 181 const PolicyMap& policies,
176 PrefValueMap* prefs) { 182 PrefValueMap* prefs) {
177 if (!pref_path_) 183 if (!pref_path_)
178 return; 184 return;
179 const base::Value* value = policies.GetValue(policy_name()); 185 const base::Value* value = policies.GetValue(policy_name());
180 scoped_ptr<base::ListValue> list(new base::ListValue()); 186 scoped_ptr<base::ListValue> list(new base::ListValue());
181 if (value && Convert(value, list.get(), NULL)) 187 if (value && Convert(value, list.get(), NULL))
182 prefs->SetValue(pref_path_, list.release()); 188 prefs->SetValue(pref_path_, list.release());
183 } 189 }
184 190
185 bool StringToIntEnumListPolicyHandler::Convert(const base::Value* input, 191 bool StringMappingListPolicyHandler::Convert(const base::Value* input,
186 base::ListValue* output, 192 base::ListValue* output,
187 PolicyErrorMap* errors) { 193 PolicyErrorMap* errors) {
188 if (!input) 194 if (!input)
189 return true; 195 return true;
190 196
191 const base::ListValue* list_value = NULL; 197 const base::ListValue* list_value = NULL;
192 if (!input->GetAsList(&list_value)) { 198 if (!input->GetAsList(&list_value)) {
193 NOTREACHED(); 199 NOTREACHED();
194 return false; 200 return false;
195 } 201 }
196 202
197 for (base::ListValue::const_iterator entry(list_value->begin()); 203 for (base::ListValue::const_iterator entry(list_value->begin());
198 entry != list_value->end(); ++entry) { 204 entry != list_value->end(); ++entry) {
199 std::string entry_value; 205 std::string entry_value;
200 if (!(*entry)->GetAsString(&entry_value)) { 206 if (!(*entry)->GetAsString(&entry_value)) {
201 if (errors) { 207 if (errors) {
202 errors->AddError(policy_name(), 208 errors->AddError(policy_name(),
203 entry - list_value->begin(), 209 entry - list_value->begin(),
204 IDS_POLICY_TYPE_ERROR, 210 IDS_POLICY_TYPE_ERROR,
205 ValueTypeToString(base::Value::TYPE_STRING)); 211 ValueTypeToString(base::Value::TYPE_STRING));
206 } 212 }
207 continue; 213 continue;
208 } 214 }
209 bool found = false; 215
210 for (const MappingEntry* mapping_entry(mapping_begin_); 216 scoped_ptr<base::Value> mapped_value = Map(entry_value);
211 mapping_entry != mapping_end_; ++mapping_entry) { 217 if (mapped_value) {
212 if (mapping_entry->enum_value == entry_value) { 218 if (output)
213 found = true; 219 output->Append(mapped_value.release());
214 if (output) 220 } else {
215 output->AppendInteger(mapping_entry->int_value);
216 break;
217 }
218 }
219 if (!found) {
220 if (errors) { 221 if (errors) {
221 errors->AddError(policy_name(), 222 errors->AddError(policy_name(),
222 entry - list_value->begin(), 223 entry - list_value->begin(),
223 IDS_POLICY_OUT_OF_RANGE_ERROR); 224 IDS_POLICY_OUT_OF_RANGE_ERROR);
224 } 225 }
225 } 226 }
226 } 227 }
227 228
228 return true; 229 return true;
229 } 230 }
230 231
232 scoped_ptr<base::Value> StringMappingListPolicyHandler::Map(
233 const std::string& entry_value) {
234 // Lazily generate the map of policy strings to mapped values.
235 if (map_.empty())
236 map_getter_.Run(&map_);
237
238 scoped_ptr<base::Value> return_value;
239 for (ScopedVector<MappingEntry>::const_iterator it = map_.begin();
240 it != map_.end(); ++it) {
241 const MappingEntry* mapping_entry = *it;
242 if (mapping_entry->enum_value == entry_value) {
243 return_value = make_scoped_ptr(mapping_entry->mapped_value->DeepCopy());
244 break;
245 }
246 }
247 return return_value.Pass();
248 }
231 249
232 // IntRangePolicyHandler implementation ---------------------------------------- 250 // IntRangePolicyHandler implementation ----------------------------------------
233 251
234 IntRangePolicyHandler::IntRangePolicyHandler(const char* policy_name, 252 IntRangePolicyHandler::IntRangePolicyHandler(const char* policy_name,
235 const char* pref_path, 253 const char* pref_path,
236 int min, 254 int min,
237 int max, 255 int max,
238 bool clamp) 256 bool clamp)
239 : IntRangePolicyHandlerBase(policy_name, min, max, clamp), 257 : IntRangePolicyHandlerBase(policy_name, min, max, clamp),
240 pref_path_(pref_path) { 258 pref_path_(pref_path) {
241 } 259 }
242 260
243 IntRangePolicyHandler::~IntRangePolicyHandler() { 261 IntRangePolicyHandler::~IntRangePolicyHandler() {
244 } 262 }
245 263
246 void IntRangePolicyHandler::ApplyPolicySettings(const PolicyMap& policies, 264 void IntRangePolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
247 PrefValueMap* prefs) { 265 PrefValueMap* prefs) {
248 if (!pref_path_) 266 if (!pref_path_)
249 return; 267 return;
250 const base::Value* value = policies.GetValue(policy_name()); 268 const base::Value* value = policies.GetValue(policy_name());
251 int value_in_range; 269 int value_in_range;
252 if (value && EnsureInRange(value, &value_in_range, NULL)) { 270 if (value && EnsureInRange(value, &value_in_range, NULL)) {
253 prefs->SetValue(pref_path_, 271 prefs->SetValue(pref_path_,
254 base::Value::CreateIntegerValue(value_in_range)); 272 new base::FundamentalValue(value_in_range));
255 } 273 }
256 } 274 }
257 275
258 276
259 // IntPercentageToDoublePolicyHandler implementation --------------------------- 277 // IntPercentageToDoublePolicyHandler implementation ---------------------------
260 278
261 IntPercentageToDoublePolicyHandler::IntPercentageToDoublePolicyHandler( 279 IntPercentageToDoublePolicyHandler::IntPercentageToDoublePolicyHandler(
262 const char* policy_name, 280 const char* policy_name,
263 const char* pref_path, 281 const char* pref_path,
264 int min, 282 int min,
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 for (handler = legacy_policy_handlers_.begin(); 482 for (handler = legacy_policy_handlers_.begin();
465 handler != legacy_policy_handlers_.end(); 483 handler != legacy_policy_handlers_.end();
466 ++handler) { 484 ++handler) {
467 if ((*handler)->CheckPolicySettings(policies, &scoped_errors)) 485 if ((*handler)->CheckPolicySettings(policies, &scoped_errors))
468 (*handler)->ApplyPolicySettings(policies, prefs); 486 (*handler)->ApplyPolicySettings(policies, prefs);
469 } 487 }
470 } 488 }
471 } 489 }
472 490
473 } // namespace policy 491 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698