Chromium Code Reviews| 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_loader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_path.h" | |
| 12 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 13 #include "base/json/json_file_value_serializer.h" | 12 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/logging.h" | 13 #include "base/logging.h" |
| 15 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 16 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 17 #include "chrome/browser/policy/policy_bundle.h" | |
| 18 | 16 |
| 19 namespace policy { | 17 namespace policy { |
| 20 | 18 |
| 21 ConfigDirPolicyProviderDelegate::ConfigDirPolicyProviderDelegate( | 19 namespace { |
| 22 const FilePath& config_dir, | 20 |
| 23 PolicyLevel level, | 21 // Subdirectories that contain the mandatory and recommended policies. |
| 24 PolicyScope scope) | 22 const char kMandatoryConfigDir[] = "managed"; |
|
Mattias Nissler (ping if slow)
2012/06/04 09:36:36
why not make these FILE_PATH_LITERAL?
Joao da Silva
2012/06/05 09:16:28
Done.
| |
| 25 : FileBasedPolicyProvider::ProviderDelegate(config_dir), | 23 const char kRecommendedConfigDir[] = "recommended"; |
| 26 level_(level), | 24 |
| 25 } // namespace | |
| 26 | |
| 27 ConfigDirPolicyLoader::ConfigDirPolicyLoader(const FilePath& config_dir, | |
| 28 PolicyScope scope) | |
| 29 : config_dir_(config_dir), | |
| 27 scope_(scope) {} | 30 scope_(scope) {} |
| 28 | 31 |
| 29 scoped_ptr<PolicyBundle> ConfigDirPolicyProviderDelegate::Load() { | 32 ConfigDirPolicyLoader::~ConfigDirPolicyLoader() {} |
| 33 | |
| 34 // static | |
| 35 AsyncPolicyProvider* ConfigDirPolicyLoader::CreateProvider( | |
| 36 const PolicyDefinitionList* policy_list, | |
| 37 PolicyScope scope, | |
| 38 const FilePath& config_dir) { | |
| 39 ConfigDirPolicyLoader* loader = new ConfigDirPolicyLoader(config_dir, scope); | |
| 40 return new AsyncPolicyProvider(policy_list, loader); | |
| 41 } | |
| 42 | |
| 43 void ConfigDirPolicyLoader::InitOnFile() { | |
| 44 WatchPath(config_dir_.AppendASCII(kMandatoryConfigDir)); | |
| 45 WatchPath(config_dir_.AppendASCII(kRecommendedConfigDir)); | |
| 46 } | |
| 47 | |
| 48 scoped_ptr<PolicyBundle> ConfigDirPolicyLoader::Load() { | |
| 49 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
| 50 LoadFromPath(config_dir_.AppendASCII(kMandatoryConfigDir), | |
| 51 POLICY_LEVEL_MANDATORY, | |
| 52 bundle.get()); | |
| 53 LoadFromPath(config_dir_.AppendASCII(kRecommendedConfigDir), | |
| 54 POLICY_LEVEL_RECOMMENDED, | |
| 55 bundle.get()); | |
| 56 return bundle.Pass(); | |
| 57 } | |
| 58 | |
| 59 base::Time ConfigDirPolicyLoader::LastModificationTime() { | |
| 60 static const char* paths[] = { | |
|
Mattias Nissler (ping if slow)
2012/06/04 09:36:36
should be kPaths, or even better: kConfigDirSuffix
Joao da Silva
2012/06/05 09:16:28
Done.
| |
| 61 kMandatoryConfigDir, | |
| 62 kRecommendedConfigDir, | |
| 63 }; | |
| 64 | |
| 65 base::Time last_modification = base::Time(); | |
| 66 base::PlatformFileInfo info; | |
| 67 | |
| 68 for (size_t i = 0; i < arraysize(paths); ++i) { | |
| 69 FilePath path(config_dir_.AppendASCII(paths[i])); | |
| 70 | |
| 71 // Skip if the file doesn't exist, or it isn't a directory. | |
| 72 if (!file_util::GetFileInfo(path, &info) || !info.is_directory) | |
| 73 continue; | |
| 74 | |
| 75 // Enumerate the files and find the most recent modification timestamp. | |
| 76 file_util::FileEnumerator file_enumerator(path, false, | |
| 77 file_util::FileEnumerator::FILES); | |
| 78 for (FilePath config_file = file_enumerator.Next(); | |
| 79 !config_file.empty(); | |
| 80 config_file = file_enumerator.Next()) { | |
| 81 if (file_util::GetFileInfo(config_file, &info) && !info.is_directory) | |
| 82 last_modification = std::max(last_modification, info.last_modified); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 return last_modification; | |
| 87 } | |
| 88 | |
| 89 void ConfigDirPolicyLoader::LoadFromPath(const FilePath& path, | |
| 90 PolicyLevel level, | |
| 91 PolicyBundle* bundle) { | |
| 30 // Enumerate the files and sort them lexicographically. | 92 // Enumerate the files and sort them lexicographically. |
| 31 std::set<FilePath> files; | 93 std::set<FilePath> files; |
| 32 file_util::FileEnumerator file_enumerator(config_file_path(), false, | 94 file_util::FileEnumerator file_enumerator(path, false, |
| 33 file_util::FileEnumerator::FILES); | 95 file_util::FileEnumerator::FILES); |
| 34 for (FilePath config_file_path = file_enumerator.Next(); | 96 for (FilePath config_file_path = file_enumerator.Next(); |
| 35 !config_file_path.empty(); config_file_path = file_enumerator.Next()) | 97 !config_file_path.empty(); config_file_path = file_enumerator.Next()) |
| 36 files.insert(config_file_path); | 98 files.insert(config_file_path); |
| 37 | 99 |
| 38 // Start with an empty dictionary and merge the files' contents. | 100 // Start with an empty dictionary and merge the files' contents. |
| 39 // The files are processed in reverse order because |MergeFrom| gives priority | 101 // The files are processed in reverse order because |MergeFrom| gives priority |
| 40 // to existing keys, but the ConfigDirPolicyProvider gives priority to the | 102 // to existing keys, but the ConfigDirPolicyProvider gives priority to the |
| 41 // last file in lexicographic order. | 103 // last file in lexicographic order. |
| 42 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
| 43 for (std::set<FilePath>::reverse_iterator config_file_iter = files.rbegin(); | 104 for (std::set<FilePath>::reverse_iterator config_file_iter = files.rbegin(); |
| 44 config_file_iter != files.rend(); ++config_file_iter) { | 105 config_file_iter != files.rend(); ++config_file_iter) { |
| 45 JSONFileValueSerializer deserializer(*config_file_iter); | 106 JSONFileValueSerializer deserializer(*config_file_iter); |
| 46 deserializer.set_allow_trailing_comma(true); | 107 deserializer.set_allow_trailing_comma(true); |
| 47 int error_code = 0; | 108 int error_code = 0; |
| 48 std::string error_msg; | 109 std::string error_msg; |
| 49 scoped_ptr<base::Value> value( | 110 scoped_ptr<base::Value> value( |
| 50 deserializer.Deserialize(&error_code, &error_msg)); | 111 deserializer.Deserialize(&error_code, &error_msg)); |
| 51 if (!value.get()) { | 112 if (!value.get()) { |
| 52 LOG(WARNING) << "Failed to read configuration file " | 113 LOG(WARNING) << "Failed to read configuration file " |
| 53 << config_file_iter->value() << ": " << error_msg; | 114 << config_file_iter->value() << ": " << error_msg; |
| 54 continue; | 115 continue; |
| 55 } | 116 } |
| 56 base::DictionaryValue* dictionary_value = NULL; | 117 base::DictionaryValue* dictionary_value = NULL; |
| 57 if (!value->GetAsDictionary(&dictionary_value)) { | 118 if (!value->GetAsDictionary(&dictionary_value)) { |
| 58 LOG(WARNING) << "Expected JSON dictionary in configuration file " | 119 LOG(WARNING) << "Expected JSON dictionary in configuration file " |
| 59 << config_file_iter->value(); | 120 << config_file_iter->value(); |
| 60 continue; | 121 continue; |
| 61 } | 122 } |
| 62 | 123 |
| 63 // Detach the "3rdparty" node. | 124 // Detach the "3rdparty" node. |
| 64 base::Value* third_party = NULL; | 125 base::Value* third_party = NULL; |
| 65 if (dictionary_value->Remove("3rdparty", &third_party)) { | 126 if (dictionary_value->Remove("3rdparty", &third_party)) { |
| 66 Merge3rdPartyPolicy(bundle.get(), third_party); | 127 Merge3rdPartyPolicy(third_party, level, bundle); |
| 67 delete third_party; | 128 delete third_party; |
| 68 } | 129 } |
| 69 | 130 |
| 70 // Add chrome policy. | 131 // Add chrome policy. |
| 71 PolicyMap policy_map; | 132 PolicyMap policy_map; |
| 72 policy_map.LoadFrom(dictionary_value, level_, scope_); | 133 policy_map.LoadFrom(dictionary_value, level, scope_); |
| 73 bundle->Get(POLICY_DOMAIN_CHROME, "").MergeFrom(policy_map); | 134 bundle->Get(POLICY_DOMAIN_CHROME, "").MergeFrom(policy_map); |
| 74 } | 135 } |
| 75 | |
| 76 return bundle.Pass(); | |
| 77 } | 136 } |
| 78 | 137 |
| 79 base::Time ConfigDirPolicyProviderDelegate::GetLastModification() { | 138 void ConfigDirPolicyLoader::Merge3rdPartyPolicy( |
| 80 base::Time last_modification = base::Time(); | 139 const base::Value* policies, |
| 81 base::PlatformFileInfo file_info; | 140 PolicyLevel level, |
| 82 | 141 PolicyBundle* bundle) { |
| 83 // If the path does not exist or points to a directory, it's safe to load. | 142 // The first-level entries in |policies| are PolicyDomains. The second-level |
| 84 if (!file_util::GetFileInfo(config_file_path(), &file_info) || | |
| 85 !file_info.is_directory) { | |
| 86 return last_modification; | |
| 87 } | |
| 88 | |
| 89 // Enumerate the files and find the most recent modification timestamp. | |
| 90 file_util::FileEnumerator file_enumerator(config_file_path(), | |
| 91 false, | |
| 92 file_util::FileEnumerator::FILES); | |
| 93 for (FilePath config_file = file_enumerator.Next(); | |
| 94 !config_file.empty(); | |
| 95 config_file = file_enumerator.Next()) { | |
| 96 if (file_util::GetFileInfo(config_file, &file_info) && | |
| 97 !file_info.is_directory) { | |
| 98 last_modification = std::max(last_modification, file_info.last_modified); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 return last_modification; | |
| 103 } | |
| 104 | |
| 105 void ConfigDirPolicyProviderDelegate::Merge3rdPartyPolicy( | |
| 106 PolicyBundle* bundle, | |
| 107 const base::Value* value) { | |
| 108 // The first-level entries in |value| are PolicyDomains. The second-level | |
| 109 // entries are component IDs, and the third-level entries are the policies | 143 // entries are component IDs, and the third-level entries are the policies |
| 110 // for that domain/component namespace. | 144 // for that domain/component namespace. |
| 111 | 145 |
| 112 const base::DictionaryValue* domains_dictionary; | 146 const base::DictionaryValue* domains_dictionary; |
| 113 if (!value->GetAsDictionary(&domains_dictionary)) { | 147 if (!policies->GetAsDictionary(&domains_dictionary)) { |
| 114 LOG(WARNING) << "3rdparty value is not a dictionary!"; | 148 LOG(WARNING) << "3rdparty value is not a dictionary!"; |
| 115 return; | 149 return; |
| 116 } | 150 } |
| 117 | 151 |
| 118 // Helper to lookup a domain given its string name. | 152 // Helper to lookup a domain given its string name. |
| 119 std::map<std::string, PolicyDomain> supported_domains; | 153 std::map<std::string, PolicyDomain> supported_domains; |
| 120 supported_domains["extensions"] = POLICY_DOMAIN_EXTENSIONS; | 154 supported_domains["extensions"] = POLICY_DOMAIN_EXTENSIONS; |
| 121 | 155 |
| 122 for (base::DictionaryValue::Iterator domains_it(*domains_dictionary); | 156 for (base::DictionaryValue::Iterator domains_it(*domains_dictionary); |
| 123 domains_it.HasNext(); domains_it.Advance()) { | 157 domains_it.HasNext(); domains_it.Advance()) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 138 for (base::DictionaryValue::Iterator components_it(*components_dictionary); | 172 for (base::DictionaryValue::Iterator components_it(*components_dictionary); |
| 139 components_it.HasNext(); components_it.Advance()) { | 173 components_it.HasNext(); components_it.Advance()) { |
| 140 const base::DictionaryValue* policy_dictionary; | 174 const base::DictionaryValue* policy_dictionary; |
| 141 if (!components_it.value().GetAsDictionary(&policy_dictionary)) { | 175 if (!components_it.value().GetAsDictionary(&policy_dictionary)) { |
| 142 LOG(WARNING) << "3rdparty/" << domains_it.key() << "/" | 176 LOG(WARNING) << "3rdparty/" << domains_it.key() << "/" |
| 143 << components_it.key() << " value is not a dictionary!"; | 177 << components_it.key() << " value is not a dictionary!"; |
| 144 continue; | 178 continue; |
| 145 } | 179 } |
| 146 | 180 |
| 147 PolicyMap policy; | 181 PolicyMap policy; |
| 148 policy.LoadFrom(policy_dictionary, level_, scope_); | 182 policy.LoadFrom(policy_dictionary, level, scope_); |
| 149 bundle->Get(domain, components_it.key()).MergeFrom(policy); | 183 bundle->Get(domain, components_it.key()).MergeFrom(policy); |
| 150 } | 184 } |
| 151 } | 185 } |
| 152 } | 186 } |
| 153 | 187 |
| 154 ConfigDirPolicyProvider::ConfigDirPolicyProvider( | |
| 155 const PolicyDefinitionList* policy_list, | |
| 156 PolicyLevel level, | |
| 157 PolicyScope scope, | |
| 158 const FilePath& config_dir) | |
| 159 : FileBasedPolicyProvider( | |
| 160 policy_list, | |
| 161 new ConfigDirPolicyProviderDelegate(config_dir, level, scope)) {} | |
| 162 | |
| 163 } // namespace policy | 188 } // namespace policy |
| OLD | NEW |