| 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 "chrome/browser/policy/config_dir_policy_provider.h" | 5 #include "chrome/browser/policy/config_dir_policy_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> |
| 9 | 10 |
| 10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 12 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 15 #include "chrome/browser/policy/policy_map.h" | 16 #include "chrome/browser/policy/policy_bundle.h" |
| 16 | 17 |
| 17 namespace policy { | 18 namespace policy { |
| 18 | 19 |
| 19 ConfigDirPolicyProviderDelegate::ConfigDirPolicyProviderDelegate( | 20 ConfigDirPolicyProviderDelegate::ConfigDirPolicyProviderDelegate( |
| 20 const FilePath& config_dir, | 21 const FilePath& config_dir, |
| 21 PolicyLevel level, | 22 PolicyLevel level, |
| 22 PolicyScope scope) | 23 PolicyScope scope) |
| 23 : FileBasedPolicyProvider::ProviderDelegate(config_dir), | 24 : FileBasedPolicyProvider::ProviderDelegate(config_dir), |
| 24 level_(level), | 25 level_(level), |
| 25 scope_(scope) {} | 26 scope_(scope) {} |
| 26 | 27 |
| 27 PolicyMap* ConfigDirPolicyProviderDelegate::Load() { | 28 scoped_ptr<PolicyBundle> ConfigDirPolicyProviderDelegate::Load() { |
| 28 // Enumerate the files and sort them lexicographically. | 29 // Enumerate the files and sort them lexicographically. |
| 29 std::set<FilePath> files; | 30 std::set<FilePath> files; |
| 30 file_util::FileEnumerator file_enumerator(config_file_path(), false, | 31 file_util::FileEnumerator file_enumerator(config_file_path(), false, |
| 31 file_util::FileEnumerator::FILES); | 32 file_util::FileEnumerator::FILES); |
| 32 for (FilePath config_file_path = file_enumerator.Next(); | 33 for (FilePath config_file_path = file_enumerator.Next(); |
| 33 !config_file_path.empty(); config_file_path = file_enumerator.Next()) | 34 !config_file_path.empty(); config_file_path = file_enumerator.Next()) |
| 34 files.insert(config_file_path); | 35 files.insert(config_file_path); |
| 35 | 36 |
| 36 // Start with an empty dictionary and merge the files' contents. | 37 // Start with an empty dictionary and merge the files' contents. |
| 37 // The files are processed in reverse order because |MergeFrom| gives priority | 38 // The files are processed in reverse order because |MergeFrom| gives priority |
| 38 // to existing keys, but the ConfigDirPolicyProvider gives priority to the | 39 // to existing keys, but the ConfigDirPolicyProvider gives priority to the |
| 39 // last file in lexicographic order. | 40 // last file in lexicographic order. |
| 40 PolicyMap* policy = new PolicyMap; | 41 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
| 41 for (std::set<FilePath>::reverse_iterator config_file_iter = files.rbegin(); | 42 for (std::set<FilePath>::reverse_iterator config_file_iter = files.rbegin(); |
| 42 config_file_iter != files.rend(); ++config_file_iter) { | 43 config_file_iter != files.rend(); ++config_file_iter) { |
| 43 JSONFileValueSerializer deserializer(*config_file_iter); | 44 JSONFileValueSerializer deserializer(*config_file_iter); |
| 44 deserializer.set_allow_trailing_comma(true); | 45 deserializer.set_allow_trailing_comma(true); |
| 45 int error_code = 0; | 46 int error_code = 0; |
| 46 std::string error_msg; | 47 std::string error_msg; |
| 47 scoped_ptr<Value> value(deserializer.Deserialize(&error_code, &error_msg)); | 48 scoped_ptr<Value> value(deserializer.Deserialize(&error_code, &error_msg)); |
| 48 if (!value.get()) { | 49 if (!value.get()) { |
| 49 LOG(WARNING) << "Failed to read configuration file " | 50 LOG(WARNING) << "Failed to read configuration file " |
| 50 << config_file_iter->value() << ": " << error_msg; | 51 << config_file_iter->value() << ": " << error_msg; |
| 51 continue; | 52 continue; |
| 52 } | 53 } |
| 53 DictionaryValue* dictionary_value = NULL; | 54 DictionaryValue* dictionary_value = NULL; |
| 54 if (!value->GetAsDictionary(&dictionary_value)) { | 55 if (!value->GetAsDictionary(&dictionary_value)) { |
| 55 LOG(WARNING) << "Expected JSON dictionary in configuration file " | 56 LOG(WARNING) << "Expected JSON dictionary in configuration file " |
| 56 << config_file_iter->value(); | 57 << config_file_iter->value(); |
| 57 continue; | 58 continue; |
| 58 } | 59 } |
| 59 PolicyMap file_policy; | 60 PolicyMap file_policy; |
| 60 file_policy.LoadFrom(dictionary_value, level_, scope_); | 61 file_policy.LoadFrom(dictionary_value, level_, scope_); |
| 61 policy->MergeFrom(file_policy); | 62 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).MergeFrom(file_policy); |
| 62 } | 63 } |
| 63 | 64 |
| 64 return policy; | 65 return bundle.Pass(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 base::Time ConfigDirPolicyProviderDelegate::GetLastModification() { | 68 base::Time ConfigDirPolicyProviderDelegate::GetLastModification() { |
| 68 base::Time last_modification = base::Time(); | 69 base::Time last_modification = base::Time(); |
| 69 base::PlatformFileInfo file_info; | 70 base::PlatformFileInfo file_info; |
| 70 | 71 |
| 71 // If the path does not exist or points to a directory, it's safe to load. | 72 // If the path does not exist or points to a directory, it's safe to load. |
| 72 if (!file_util::GetFileInfo(config_file_path(), &file_info) || | 73 if (!file_util::GetFileInfo(config_file_path(), &file_info) || |
| 73 !file_info.is_directory) { | 74 !file_info.is_directory) { |
| 74 return last_modification; | 75 return last_modification; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 93 ConfigDirPolicyProvider::ConfigDirPolicyProvider( | 94 ConfigDirPolicyProvider::ConfigDirPolicyProvider( |
| 94 const PolicyDefinitionList* policy_list, | 95 const PolicyDefinitionList* policy_list, |
| 95 PolicyLevel level, | 96 PolicyLevel level, |
| 96 PolicyScope scope, | 97 PolicyScope scope, |
| 97 const FilePath& config_dir) | 98 const FilePath& config_dir) |
| 98 : FileBasedPolicyProvider( | 99 : FileBasedPolicyProvider( |
| 99 policy_list, | 100 policy_list, |
| 100 new ConfigDirPolicyProviderDelegate(config_dir, level, scope)) {} | 101 new ConfigDirPolicyProviderDelegate(config_dir, level, scope)) {} |
| 101 | 102 |
| 102 } // namespace policy | 103 } // namespace policy |
| OLD | NEW |