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

Side by Side Diff: chrome/browser/policy/configuration_policy_handler.cc

Issue 12217068: Add policies to control Chrome OS power management (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 7 years, 10 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 "chrome/browser/policy/configuration_policy_handler.h" 5 #include "chrome/browser/policy/configuration_policy_handler.h"
6 6
7 #include <algorithm>
7 #include <string> 8 #include <string>
8 9
9 #include "base/file_path.h" 10 #include "base/file_path.h"
10 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/prefs/pref_value_map.h" 13 #include "base/prefs/pref_value_map.h"
13 #include "base/stl_util.h" 14 #include "base/stl_util.h"
14 #include "base/string16.h" 15 #include "base/string16.h"
15 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
16 #include "base/string_util.h" 17 #include "base/string_util.h"
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 *value = policies.GetValue(policy_name_); 206 *value = policies.GetValue(policy_name_);
206 if (*value && !(*value)->IsType(value_type_)) { 207 if (*value && !(*value)->IsType(value_type_)) {
207 errors->AddError(policy_name_, 208 errors->AddError(policy_name_,
208 IDS_POLICY_TYPE_ERROR, 209 IDS_POLICY_TYPE_ERROR,
209 ValueTypeToString(value_type_)); 210 ValueTypeToString(value_type_));
210 return false; 211 return false;
211 } 212 }
212 return true; 213 return true;
213 } 214 }
214 215
216 // IntRangePolicyHandlerBase implementation ------------------------------------
217
218 IntRangePolicyHandlerBase::IntRangePolicyHandlerBase(
219 const char* policy_name,
220 int min,
221 int max,
222 bool clamp)
223 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_INTEGER),
224 min_(min),
225 max_(max),
226 clamp_(clamp) {
227 }
228
229 bool IntRangePolicyHandlerBase::CheckPolicySettings(const PolicyMap& policies,
230 PolicyErrorMap* errors) {
231 const base::Value* value;
232 return CheckAndGetValue(policies, errors, &value) &&
233 EnsureInRange(value, NULL, errors);
234 }
235
236 IntRangePolicyHandlerBase::~IntRangePolicyHandlerBase() {
237 }
238
239 bool IntRangePolicyHandlerBase::EnsureInRange(const base::Value* input,
240 int* output,
241 PolicyErrorMap* errors) {
242 if (!input)
243 return true;
244
245 int value;
246 if (!input->GetAsInteger(&value)) {
247 NOTREACHED();
248 return false;
249 }
250
251 if (value < min_ || value > max_) {
252 if (errors) {
253 errors->AddError(policy_name(),
254 IDS_POLICY_OUT_OF_RANGE_ERROR,
255 base::IntToString(value));
256 }
257
258 if (!clamp_)
259 return false;
260
261 value = std::min(std::max(value, min_), max_);
262 }
263
264 if (output)
265 *output = value;
266 return true;
267 }
268
215 // StringToIntEnumListPolicyHandler implementation ----------------------------- 269 // StringToIntEnumListPolicyHandler implementation -----------------------------
216 270
217 StringToIntEnumListPolicyHandler::StringToIntEnumListPolicyHandler( 271 StringToIntEnumListPolicyHandler::StringToIntEnumListPolicyHandler(
218 const char* policy_name, 272 const char* policy_name,
219 const char* pref_path, 273 const char* pref_path,
220 const MappingEntry* mapping_begin, 274 const MappingEntry* mapping_begin,
221 const MappingEntry* mapping_end) 275 const MappingEntry* mapping_end)
222 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST), 276 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST),
223 pref_path_(pref_path), 277 pref_path_(pref_path),
224 mapping_begin_(mapping_begin), 278 mapping_begin_(mapping_begin),
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 errors->AddError(policy_name(), 334 errors->AddError(policy_name(),
281 entry - list_value->begin(), 335 entry - list_value->begin(),
282 IDS_POLICY_OUT_OF_RANGE_ERROR); 336 IDS_POLICY_OUT_OF_RANGE_ERROR);
283 } 337 }
284 } 338 }
285 } 339 }
286 340
287 return true; 341 return true;
288 } 342 }
289 343
344 // IntRangePolicyHandler implementation ----------------------------------------
345
346 IntRangePolicyHandler::IntRangePolicyHandler(const char* policy_name,
347 const char* pref_path,
348 int min,
349 int max,
350 bool clamp)
351 : IntRangePolicyHandlerBase(policy_name, min, max, clamp),
352 pref_path_(pref_path) {
353 }
354
355 IntRangePolicyHandler::~IntRangePolicyHandler() {
356 }
357
358 void IntRangePolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
359 PrefValueMap* prefs) {
360 const base::Value* value = policies.GetValue(policy_name());
361 int value_in_range;
362 if (value && EnsureInRange(value, &value_in_range, NULL)) {
363 prefs->SetValue(pref_path_,
364 base::Value::CreateIntegerValue(value_in_range));
365 }
366 }
367
368 // IntPercentageToDoublePolicyHandler implementation ---------------------------
369
370 IntPercentageToDoublePolicyHandler::IntPercentageToDoublePolicyHandler(
371 const char* policy_name,
372 const char* pref_path,
373 int min,
374 int max,
375 bool clamp)
376 : IntRangePolicyHandlerBase(policy_name, min, max, clamp),
377 pref_path_(pref_path) {
378 }
379
380 IntPercentageToDoublePolicyHandler::~IntPercentageToDoublePolicyHandler() {
381 }
382
383 void IntPercentageToDoublePolicyHandler::ApplyPolicySettings(
384 const PolicyMap& policies,
385 PrefValueMap* prefs) {
386 const base::Value* value = policies.GetValue(policy_name());
387 int percentage;
388 if (value && EnsureInRange(value, &percentage, NULL)) {
389 prefs->SetValue(pref_path_, base::Value::CreateDoubleValue(
390 static_cast<double>(percentage) / 100.));
391 }
392 }
393
290 // ExtensionListPolicyHandler implementation ----------------------------------- 394 // ExtensionListPolicyHandler implementation -----------------------------------
291 395
292 ExtensionListPolicyHandler::ExtensionListPolicyHandler(const char* policy_name, 396 ExtensionListPolicyHandler::ExtensionListPolicyHandler(const char* policy_name,
293 const char* pref_path, 397 const char* pref_path,
294 bool allow_wildcards) 398 bool allow_wildcards)
295 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST), 399 : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST),
296 pref_path_(pref_path), 400 pref_path_(pref_path),
297 allow_wildcards_(allow_wildcards) {} 401 allow_wildcards_(allow_wildcards) {}
298 402
299 ExtensionListPolicyHandler::~ExtensionListPolicyHandler() {} 403 ExtensionListPolicyHandler::~ExtensionListPolicyHandler() {}
(...skipping 1161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1461 errors->AddError(policy_name(), 1565 errors->AddError(policy_name(),
1462 IDS_POLICY_OUT_OF_RANGE_ERROR, 1566 IDS_POLICY_OUT_OF_RANGE_ERROR,
1463 base::IntToString(restore_value)); 1567 base::IntToString(restore_value));
1464 } 1568 }
1465 } 1569 }
1466 } 1570 }
1467 return true; 1571 return true;
1468 } 1572 }
1469 1573
1470 } // namespace policy 1574 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/configuration_policy_handler.h ('k') | chrome/browser/policy/configuration_policy_handler_list.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698