OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #include "chrome/browser/config_dir_policy_provider.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/scoped_ptr.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/common/json_value_serializer.h" |
| 14 |
| 15 ConfigDirPolicyProvider::ConfigDirPolicyProvider(const FilePath& config_dir) |
| 16 : config_dir_(config_dir) { |
| 17 } |
| 18 |
| 19 bool ConfigDirPolicyProvider::Provide(ConfigurationPolicyStore* store) { |
| 20 scoped_ptr<DictionaryValue> policy(ReadPolicies()); |
| 21 DecodePolicyValueTree(policy.get(), store); |
| 22 return true; |
| 23 } |
| 24 |
| 25 DictionaryValue* ConfigDirPolicyProvider::ReadPolicies() { |
| 26 // Enumerate the files and sort them lexicographically. |
| 27 std::set<FilePath> files; |
| 28 file_util::FileEnumerator file_enumerator(config_dir_, false, |
| 29 file_util::FileEnumerator::FILES); |
| 30 for (FilePath config_file_path = file_enumerator.Next(); |
| 31 !config_file_path.empty(); config_file_path = file_enumerator.Next()) |
| 32 files.insert(config_file_path); |
| 33 |
| 34 // Start with an empty dictionary and merge the files' contents. |
| 35 DictionaryValue* policy = new DictionaryValue; |
| 36 for (std::set<FilePath>::iterator config_file_iter = files.begin(); |
| 37 config_file_iter != files.end(); ++config_file_iter) { |
| 38 JSONFileValueSerializer deserializer(*config_file_iter); |
| 39 int error_code = 0; |
| 40 std::string error_msg; |
| 41 scoped_ptr<Value> value(deserializer.Deserialize(&error_code, &error_msg)); |
| 42 if (!value.get()) { |
| 43 LOG(WARNING) << "Failed to read configuration file " |
| 44 << config_file_iter->value() << ": " << error_msg; |
| 45 continue; |
| 46 } |
| 47 if (!value->IsType(Value::TYPE_DICTIONARY)) { |
| 48 LOG(WARNING) << "Expected JSON dictionary in configuration file " |
| 49 << config_file_iter->value(); |
| 50 continue; |
| 51 } |
| 52 policy->MergeDictionary(static_cast<DictionaryValue*>(value.get())); |
| 53 } |
| 54 |
| 55 return policy; |
| 56 } |
OLD | NEW |