OLD | NEW |
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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "components/policy/core/browser/policy_error_map.h" | 20 #include "components/policy/core/browser/policy_error_map.h" |
21 #include "components/policy/core/common/policy_map.h" | 21 #include "components/policy/core/common/policy_map.h" |
22 #include "components/prefs/pref_value_map.h" | 22 #include "components/prefs/pref_value_map.h" |
23 #include "grit/components_strings.h" | 23 #include "grit/components_strings.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 namespace policy { | 26 namespace policy { |
27 | 27 |
28 // ConfigurationPolicyHandler implementation ----------------------------------- | 28 // ConfigurationPolicyHandler implementation ----------------------------------- |
29 | 29 |
30 // static | |
31 std::string ConfigurationPolicyHandler::ValueTypeToString( | |
32 base::Value::Type type) { | |
33 return std::string(base::Value::GetTypeName(type)); | |
34 } | |
35 | |
36 ConfigurationPolicyHandler::ConfigurationPolicyHandler() { | 30 ConfigurationPolicyHandler::ConfigurationPolicyHandler() { |
37 } | 31 } |
38 | 32 |
39 ConfigurationPolicyHandler::~ConfigurationPolicyHandler() { | 33 ConfigurationPolicyHandler::~ConfigurationPolicyHandler() { |
40 } | 34 } |
41 | 35 |
42 void ConfigurationPolicyHandler::PrepareForDisplaying( | 36 void ConfigurationPolicyHandler::PrepareForDisplaying( |
43 PolicyMap* policies) const {} | 37 PolicyMap* policies) const {} |
44 | 38 |
45 void ConfigurationPolicyHandler::ApplyPolicySettingsWithParameters( | 39 void ConfigurationPolicyHandler::ApplyPolicySettingsWithParameters( |
(...skipping 23 matching lines...) Expand all Loading... |
69 PolicyErrorMap* errors) { | 63 PolicyErrorMap* errors) { |
70 const base::Value* value = NULL; | 64 const base::Value* value = NULL; |
71 return CheckAndGetValue(policies, errors, &value); | 65 return CheckAndGetValue(policies, errors, &value); |
72 } | 66 } |
73 | 67 |
74 bool TypeCheckingPolicyHandler::CheckAndGetValue(const PolicyMap& policies, | 68 bool TypeCheckingPolicyHandler::CheckAndGetValue(const PolicyMap& policies, |
75 PolicyErrorMap* errors, | 69 PolicyErrorMap* errors, |
76 const base::Value** value) { | 70 const base::Value** value) { |
77 *value = policies.GetValue(policy_name_); | 71 *value = policies.GetValue(policy_name_); |
78 if (*value && !(*value)->IsType(value_type_)) { | 72 if (*value && !(*value)->IsType(value_type_)) { |
79 errors->AddError(policy_name_, | 73 errors->AddError(policy_name_, IDS_POLICY_TYPE_ERROR, |
80 IDS_POLICY_TYPE_ERROR, | 74 base::Value::GetTypeName(value_type_)); |
81 ValueTypeToString(value_type_)); | |
82 return false; | 75 return false; |
83 } | 76 } |
84 return true; | 77 return true; |
85 } | 78 } |
86 | 79 |
87 | 80 |
88 // IntRangePolicyHandlerBase implementation ------------------------------------ | 81 // IntRangePolicyHandlerBase implementation ------------------------------------ |
89 | 82 |
90 IntRangePolicyHandlerBase::IntRangePolicyHandlerBase( | 83 IntRangePolicyHandlerBase::IntRangePolicyHandlerBase( |
91 const char* policy_name, | 84 const char* policy_name, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 if (!input->GetAsList(&list_value)) { | 180 if (!input->GetAsList(&list_value)) { |
188 NOTREACHED(); | 181 NOTREACHED(); |
189 return false; | 182 return false; |
190 } | 183 } |
191 | 184 |
192 for (base::ListValue::const_iterator entry(list_value->begin()); | 185 for (base::ListValue::const_iterator entry(list_value->begin()); |
193 entry != list_value->end(); ++entry) { | 186 entry != list_value->end(); ++entry) { |
194 std::string entry_value; | 187 std::string entry_value; |
195 if (!(*entry)->GetAsString(&entry_value)) { | 188 if (!(*entry)->GetAsString(&entry_value)) { |
196 if (errors) { | 189 if (errors) { |
197 errors->AddError(policy_name(), | 190 errors->AddError(policy_name(), entry - list_value->begin(), |
198 entry - list_value->begin(), | |
199 IDS_POLICY_TYPE_ERROR, | 191 IDS_POLICY_TYPE_ERROR, |
200 ValueTypeToString(base::Value::TYPE_STRING)); | 192 base::Value::GetTypeName(base::Value::TYPE_STRING)); |
201 } | 193 } |
202 continue; | 194 continue; |
203 } | 195 } |
204 | 196 |
205 std::unique_ptr<base::Value> mapped_value = Map(entry_value); | 197 std::unique_ptr<base::Value> mapped_value = Map(entry_value); |
206 if (mapped_value) { | 198 if (mapped_value) { |
207 if (output) | 199 if (output) |
208 output->Append(std::move(mapped_value)); | 200 output->Append(std::move(mapped_value)); |
209 } else { | 201 } else { |
210 if (errors) { | 202 if (errors) { |
211 errors->AddError(policy_name(), | 203 errors->AddError(policy_name(), |
212 entry - list_value->begin(), | 204 entry - list_value->begin(), |
213 IDS_POLICY_OUT_OF_RANGE_ERROR); | 205 IDS_POLICY_OUT_OF_RANGE_ERROR); |
214 } | 206 } |
215 } | 207 } |
216 } | 208 } |
217 | 209 |
218 return true; | 210 return true; |
219 } | 211 } |
220 | 212 |
221 std::unique_ptr<base::Value> StringMappingListPolicyHandler::Map( | 213 std::unique_ptr<base::Value> StringMappingListPolicyHandler::Map( |
222 const std::string& entry_value) { | 214 const std::string& entry_value) { |
223 // Lazily generate the map of policy strings to mapped values. | 215 // Lazily generate the map of policy strings to mapped values. |
224 if (map_.empty()) | 216 if (map_.empty()) |
225 map_getter_.Run(&map_); | 217 map_getter_.Run(&map_); |
226 | 218 |
227 std::unique_ptr<base::Value> return_value; | 219 std::unique_ptr<base::Value> return_value; |
228 for (ScopedVector<MappingEntry>::const_iterator it = map_.begin(); | 220 for (const auto& mapping_entry : map_) { |
229 it != map_.end(); ++it) { | |
230 const MappingEntry* mapping_entry = *it; | |
231 if (mapping_entry->enum_value == entry_value) { | 221 if (mapping_entry->enum_value == entry_value) { |
232 return_value = base::WrapUnique(mapping_entry->mapped_value->DeepCopy()); | 222 return_value = base::WrapUnique(mapping_entry->mapped_value->DeepCopy()); |
233 break; | 223 break; |
234 } | 224 } |
235 } | 225 } |
236 return return_value; | 226 return return_value; |
237 } | 227 } |
238 | 228 |
239 // IntRangePolicyHandler implementation ---------------------------------------- | 229 // IntRangePolicyHandler implementation ---------------------------------------- |
240 | 230 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 PrefValueMap* prefs) { | 404 PrefValueMap* prefs) { |
415 if (!pref_path_) | 405 if (!pref_path_) |
416 return; | 406 return; |
417 const base::Value* value = policies.GetValue(policy_name()); | 407 const base::Value* value = policies.GetValue(policy_name()); |
418 if (value) | 408 if (value) |
419 prefs->SetValue(pref_path_, value->CreateDeepCopy()); | 409 prefs->SetValue(pref_path_, value->CreateDeepCopy()); |
420 } | 410 } |
421 | 411 |
422 // LegacyPoliciesDeprecatingPolicyHandler implementation ----------------------- | 412 // LegacyPoliciesDeprecatingPolicyHandler implementation ----------------------- |
423 | 413 |
424 // TODO(binjin): Add a new common base class for SchemaValidatingPolicyHandler | |
425 // and TypeCheckingPolicyHandler representing policy handlers for a single | |
426 // policy, and use it as the type of |new_policy_handler|. | |
427 // http://crbug.com/345299 | |
428 LegacyPoliciesDeprecatingPolicyHandler::LegacyPoliciesDeprecatingPolicyHandler( | 414 LegacyPoliciesDeprecatingPolicyHandler::LegacyPoliciesDeprecatingPolicyHandler( |
429 ScopedVector<ConfigurationPolicyHandler> legacy_policy_handlers, | 415 ScopedVector<ConfigurationPolicyHandler> legacy_policy_handlers, |
430 std::unique_ptr<SchemaValidatingPolicyHandler> new_policy_handler) | 416 std::unique_ptr<SchemaValidatingPolicyHandler> new_policy_handler) |
431 : legacy_policy_handlers_(std::move(legacy_policy_handlers)), | 417 : legacy_policy_handlers_(std::move(legacy_policy_handlers)), |
432 new_policy_handler_(std::move(new_policy_handler)) {} | 418 new_policy_handler_(std::move(new_policy_handler)) {} |
433 | 419 |
434 LegacyPoliciesDeprecatingPolicyHandler:: | 420 LegacyPoliciesDeprecatingPolicyHandler:: |
435 ~LegacyPoliciesDeprecatingPolicyHandler() { | 421 ~LegacyPoliciesDeprecatingPolicyHandler() { |
436 } | 422 } |
437 | 423 |
438 bool LegacyPoliciesDeprecatingPolicyHandler::CheckPolicySettings( | 424 bool LegacyPoliciesDeprecatingPolicyHandler::CheckPolicySettings( |
439 const PolicyMap& policies, | 425 const PolicyMap& policies, |
440 PolicyErrorMap* errors) { | 426 PolicyErrorMap* errors) { |
441 if (policies.Get(new_policy_handler_->policy_name())) { | 427 if (policies.Get(new_policy_handler_->policy_name())) |
442 return new_policy_handler_->CheckPolicySettings(policies, errors); | 428 return new_policy_handler_->CheckPolicySettings(policies, errors); |
443 } else { | 429 |
444 // The new policy is not set, fall back to legacy ones. | 430 // The new policy is not set, fall back to legacy ones. |
445 ScopedVector<ConfigurationPolicyHandler>::iterator handler; | 431 ScopedVector<ConfigurationPolicyHandler>::iterator handler; |
446 bool valid_policy_found = false; | 432 bool valid_policy_found = false; |
447 for (handler = legacy_policy_handlers_.begin(); | 433 for (const auto& handler : legacy_policy_handlers_) { |
448 handler != legacy_policy_handlers_.end(); | 434 if (handler->CheckPolicySettings(policies, errors)) |
449 ++handler) { | 435 valid_policy_found = true; |
450 if ((*handler)->CheckPolicySettings(policies, errors)) | |
451 valid_policy_found = true; | |
452 } | |
453 return valid_policy_found; | |
454 } | 436 } |
| 437 return valid_policy_found; |
455 } | 438 } |
456 | 439 |
457 void LegacyPoliciesDeprecatingPolicyHandler::ApplyPolicySettingsWithParameters( | 440 void LegacyPoliciesDeprecatingPolicyHandler::ApplyPolicySettingsWithParameters( |
458 const policy::PolicyMap& policies, | 441 const policy::PolicyMap& policies, |
459 const policy::PolicyHandlerParameters& parameters, | 442 const policy::PolicyHandlerParameters& parameters, |
460 PrefValueMap* prefs) { | 443 PrefValueMap* prefs) { |
461 if (policies.Get(new_policy_handler_->policy_name())) { | 444 if (policies.Get(new_policy_handler_->policy_name())) { |
462 new_policy_handler_->ApplyPolicySettingsWithParameters(policies, parameters, | 445 new_policy_handler_->ApplyPolicySettingsWithParameters(policies, parameters, |
463 prefs); | 446 prefs); |
464 } else { | 447 return; |
465 // The new policy is not set, fall back to legacy ones. | 448 } |
466 PolicyErrorMap scoped_errors; | 449 |
467 ScopedVector<ConfigurationPolicyHandler>::iterator handler; | 450 // The new policy is not set, fall back to legacy ones. |
468 for (handler = legacy_policy_handlers_.begin(); | 451 PolicyErrorMap scoped_errors; |
469 handler != legacy_policy_handlers_.end(); | 452 for (const auto& handler : legacy_policy_handlers_) { |
470 ++handler) { | 453 if (handler->CheckPolicySettings(policies, &scoped_errors)) |
471 if ((*handler)->CheckPolicySettings(policies, &scoped_errors)) { | 454 handler->ApplyPolicySettingsWithParameters(policies, parameters, prefs); |
472 (*handler) | |
473 ->ApplyPolicySettingsWithParameters(policies, parameters, prefs); | |
474 } | |
475 } | |
476 } | 455 } |
477 } | 456 } |
| 457 |
478 void LegacyPoliciesDeprecatingPolicyHandler::ApplyPolicySettings( | 458 void LegacyPoliciesDeprecatingPolicyHandler::ApplyPolicySettings( |
479 const policy::PolicyMap& /* policies */, | 459 const policy::PolicyMap& /* policies */, |
480 PrefValueMap* /* prefs */) { | 460 PrefValueMap* /* prefs */) { |
481 NOTREACHED(); | 461 NOTREACHED(); |
482 } | 462 } |
483 | 463 |
484 } // namespace policy | 464 } // namespace policy |
OLD | NEW |