Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(513)

Side by Side Diff: chrome/browser/policy/config_dir_policy_provider.cc

Issue 10545033: Removed the PolicyDefinitionList from the ConfigurationPolicyProvider interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/policy/config_dir_policy_provider.h"
6
7 #include <algorithm>
8 #include <set>
9 #include <string>
10
11 #include "base/file_path.h"
12 #include "base/file_util.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/logging.h"
15 #include "base/platform_file.h"
16 #include "base/stl_util.h"
17 #include "chrome/browser/policy/policy_bundle.h"
18
19 namespace policy {
20
21 ConfigDirPolicyProviderDelegate::ConfigDirPolicyProviderDelegate(
22 const FilePath& config_dir,
23 PolicyLevel level,
24 PolicyScope scope)
25 : FileBasedPolicyProvider::ProviderDelegate(config_dir),
26 level_(level),
27 scope_(scope) {}
28
29 scoped_ptr<PolicyBundle> ConfigDirPolicyProviderDelegate::Load() {
30 // Enumerate the files and sort them lexicographically.
31 std::set<FilePath> files;
32 file_util::FileEnumerator file_enumerator(config_file_path(), false,
33 file_util::FileEnumerator::FILES);
34 for (FilePath config_file_path = file_enumerator.Next();
35 !config_file_path.empty(); config_file_path = file_enumerator.Next())
36 files.insert(config_file_path);
37
38 // Start with an empty dictionary and merge the files' contents.
39 // The files are processed in reverse order because |MergeFrom| gives priority
40 // to existing keys, but the ConfigDirPolicyProvider gives priority to the
41 // last file in lexicographic order.
42 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
43 for (std::set<FilePath>::reverse_iterator config_file_iter = files.rbegin();
44 config_file_iter != files.rend(); ++config_file_iter) {
45 JSONFileValueSerializer deserializer(*config_file_iter);
46 deserializer.set_allow_trailing_comma(true);
47 int error_code = 0;
48 std::string error_msg;
49 scoped_ptr<base::Value> value(
50 deserializer.Deserialize(&error_code, &error_msg));
51 if (!value.get()) {
52 LOG(WARNING) << "Failed to read configuration file "
53 << config_file_iter->value() << ": " << error_msg;
54 continue;
55 }
56 base::DictionaryValue* dictionary_value = NULL;
57 if (!value->GetAsDictionary(&dictionary_value)) {
58 LOG(WARNING) << "Expected JSON dictionary in configuration file "
59 << config_file_iter->value();
60 continue;
61 }
62
63 // Detach the "3rdparty" node.
64 base::Value* third_party = NULL;
65 if (dictionary_value->Remove("3rdparty", &third_party)) {
66 Merge3rdPartyPolicy(bundle.get(), third_party);
67 delete third_party;
68 }
69
70 // Add chrome policy.
71 PolicyMap policy_map;
72 policy_map.LoadFrom(dictionary_value, level_, scope_);
73 bundle->Get(POLICY_DOMAIN_CHROME, "").MergeFrom(policy_map);
74 }
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 }
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
110 // for that domain/component namespace.
111
112 const base::DictionaryValue* domains_dictionary;
113 if (!value->GetAsDictionary(&domains_dictionary)) {
114 LOG(WARNING) << "3rdparty value is not a dictionary!";
115 return;
116 }
117
118 // Helper to lookup a domain given its string name.
119 std::map<std::string, PolicyDomain> supported_domains;
120 supported_domains["extensions"] = POLICY_DOMAIN_EXTENSIONS;
121
122 for (base::DictionaryValue::Iterator domains_it(*domains_dictionary);
123 domains_it.HasNext(); domains_it.Advance()) {
124 if (!ContainsKey(supported_domains, domains_it.key())) {
125 LOG(WARNING) << "Unsupported 3rd party policy domain: "
126 << domains_it.key();
127 continue;
128 }
129
130 const base::DictionaryValue* components_dictionary;
131 if (!domains_it.value().GetAsDictionary(&components_dictionary)) {
132 LOG(WARNING) << "3rdparty/" << domains_it.key()
133 << " value is not a dictionary!";
134 continue;
135 }
136
137 PolicyDomain domain = supported_domains[domains_it.key()];
138 for (base::DictionaryValue::Iterator components_it(*components_dictionary);
139 components_it.HasNext(); components_it.Advance()) {
140 const base::DictionaryValue* policy_dictionary;
141 if (!components_it.value().GetAsDictionary(&policy_dictionary)) {
142 LOG(WARNING) << "3rdparty/" << domains_it.key() << "/"
143 << components_it.key() << " value is not a dictionary!";
144 continue;
145 }
146
147 PolicyMap policy;
148 policy.LoadFrom(policy_dictionary, level_, scope_);
149 bundle->Get(domain, components_it.key()).MergeFrom(policy);
150 }
151 }
152 }
153
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698