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