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_loader.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/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/json/json_file_value_serializer.h" | 14 #include "base/json/json_file_value_serializer.h" |
15 #include "base/json/json_reader.h" | |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
18 #include "chrome/browser/policy/policy_bundle.h" | 19 #include "chrome/browser/policy/policy_bundle.h" |
20 #include "chrome/browser/policy/policy_load_status.h" | |
19 | 21 |
20 namespace policy { | 22 namespace policy { |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 // Subdirectories that contain the mandatory and recommended policies. | 26 // Subdirectories that contain the mandatory and recommended policies. |
25 const base::FilePath::CharType kMandatoryConfigDir[] = | 27 const base::FilePath::CharType kMandatoryConfigDir[] = |
26 FILE_PATH_LITERAL("managed"); | 28 FILE_PATH_LITERAL("managed"); |
27 const base::FilePath::CharType kRecommendedConfigDir[] = | 29 const base::FilePath::CharType kRecommendedConfigDir[] = |
28 FILE_PATH_LITERAL("recommended"); | 30 FILE_PATH_LITERAL("recommended"); |
29 | 31 |
32 PolicyLoadStatus JsonErrorToPolicyLoadStatus(int status) { | |
33 switch (status) { | |
34 case JSONFileValueSerializer::JSON_ACCESS_DENIED: | |
Joao da Silva
2013/04/16 17:31:04
POLICY_LOAD_STATUS_INACCESSIBLE?
Mattias Nissler (ping if slow)
2013/04/16 18:51:14
Well, INACCESSIBLE was meant as "we decided we can
| |
35 case JSONFileValueSerializer::JSON_CANNOT_READ_FILE: | |
36 case JSONFileValueSerializer::JSON_FILE_LOCKED: | |
37 return POLICY_LOAD_STATUS_READ_ERROR; | |
38 case JSONFileValueSerializer::JSON_NO_SUCH_FILE: | |
39 return POLICY_LOAD_STATUS_MISSING; | |
40 case base::JSONReader::JSON_NO_ERROR: | |
Joao da Silva
2013/04/16 17:31:04
I'm assuming this is the non-DictionaryValue case,
Mattias Nissler (ping if slow)
2013/04/16 18:51:14
Exactly. I've made this more obvious now by adding
| |
41 case base::JSONReader::JSON_INVALID_ESCAPE: | |
42 case base::JSONReader::JSON_SYNTAX_ERROR: | |
43 case base::JSONReader::JSON_UNEXPECTED_TOKEN: | |
44 case base::JSONReader::JSON_TRAILING_COMMA: | |
45 case base::JSONReader::JSON_TOO_MUCH_NESTING: | |
46 case base::JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT: | |
47 case base::JSONReader::JSON_UNSUPPORTED_ENCODING: | |
48 case base::JSONReader::JSON_UNQUOTED_DICTIONARY_KEY: | |
49 return POLICY_LOAD_STATUS_PARSE_ERROR; | |
50 } | |
51 NOTREACHED() << "Invalid status " << status; | |
52 return POLICY_LOAD_STATUS_PARSE_ERROR; | |
53 } | |
54 | |
30 } // namespace | 55 } // namespace |
31 | 56 |
32 ConfigDirPolicyLoader::ConfigDirPolicyLoader(const base::FilePath& config_dir, | 57 ConfigDirPolicyLoader::ConfigDirPolicyLoader(const base::FilePath& config_dir, |
33 PolicyScope scope) | 58 PolicyScope scope) |
34 : config_dir_(config_dir), | 59 : config_dir_(config_dir), |
35 scope_(scope) {} | 60 scope_(scope) {} |
36 | 61 |
37 ConfigDirPolicyLoader::~ConfigDirPolicyLoader() {} | 62 ConfigDirPolicyLoader::~ConfigDirPolicyLoader() {} |
38 | 63 |
39 void ConfigDirPolicyLoader::InitOnFile() { | 64 void ConfigDirPolicyLoader::InitOnFile() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 PolicyLevel level, | 115 PolicyLevel level, |
91 PolicyBundle* bundle) { | 116 PolicyBundle* bundle) { |
92 // Enumerate the files and sort them lexicographically. | 117 // Enumerate the files and sort them lexicographically. |
93 std::set<base::FilePath> files; | 118 std::set<base::FilePath> files; |
94 file_util::FileEnumerator file_enumerator(path, false, | 119 file_util::FileEnumerator file_enumerator(path, false, |
95 file_util::FileEnumerator::FILES); | 120 file_util::FileEnumerator::FILES); |
96 for (base::FilePath config_file_path = file_enumerator.Next(); | 121 for (base::FilePath config_file_path = file_enumerator.Next(); |
97 !config_file_path.empty(); config_file_path = file_enumerator.Next()) | 122 !config_file_path.empty(); config_file_path = file_enumerator.Next()) |
98 files.insert(config_file_path); | 123 files.insert(config_file_path); |
99 | 124 |
125 PolicyLoadStatusSample status(scope_, level); | |
Joao da Silva
2013/04/16 17:31:04
Shouldn't this be inside the for loop? Otherwise w
Mattias Nissler (ping if slow)
2013/04/16 18:51:14
True. Now I realize why I had PolicyLoadStatus don
Joao da Silva
2013/04/16 19:32:57
This skews the errors to reads ratio. 10 files out
Mattias Nissler (ping if slow)
2013/04/17 10:16:35
I'm not following. If we have 10 files in the mand
| |
126 if (files.empty()) { | |
127 status.Add(POLICY_LOAD_STATUS_NO_POLICY); | |
Joao da Silva
2013/04/16 17:31:04
Why do we sample this case?
Mattias Nissler (ping if slow)
2013/04/16 18:51:14
The idea was to get a rough idea on the number of
| |
128 return; | |
129 } | |
130 | |
100 // Start with an empty dictionary and merge the files' contents. | 131 // Start with an empty dictionary and merge the files' contents. |
101 // The files are processed in reverse order because |MergeFrom| gives priority | 132 // The files are processed in reverse order because |MergeFrom| gives priority |
102 // to existing keys, but the ConfigDirPolicyProvider gives priority to the | 133 // to existing keys, but the ConfigDirPolicyProvider gives priority to the |
103 // last file in lexicographic order. | 134 // last file in lexicographic order. |
104 for (std::set<base::FilePath>::reverse_iterator config_file_iter = | 135 for (std::set<base::FilePath>::reverse_iterator config_file_iter = |
105 files.rbegin(); config_file_iter != files.rend(); | 136 files.rbegin(); config_file_iter != files.rend(); |
106 ++config_file_iter) { | 137 ++config_file_iter) { |
107 JSONFileValueSerializer deserializer(*config_file_iter); | 138 JSONFileValueSerializer deserializer(*config_file_iter); |
108 deserializer.set_allow_trailing_comma(true); | 139 deserializer.set_allow_trailing_comma(true); |
109 int error_code = 0; | 140 int error_code = 0; |
110 std::string error_msg; | 141 std::string error_msg; |
111 scoped_ptr<base::Value> value( | 142 scoped_ptr<base::Value> value( |
112 deserializer.Deserialize(&error_code, &error_msg)); | 143 deserializer.Deserialize(&error_code, &error_msg)); |
113 if (!value.get()) { | 144 if (!value.get()) { |
114 LOG(WARNING) << "Failed to read configuration file " | 145 LOG(WARNING) << "Failed to read configuration file " |
115 << config_file_iter->value() << ": " << error_msg; | 146 << config_file_iter->value() << ": " << error_msg; |
147 status.Add(JsonErrorToPolicyLoadStatus(error_code)); | |
116 continue; | 148 continue; |
117 } | 149 } |
118 base::DictionaryValue* dictionary_value = NULL; | 150 base::DictionaryValue* dictionary_value = NULL; |
119 if (!value->GetAsDictionary(&dictionary_value)) { | 151 if (!value->GetAsDictionary(&dictionary_value)) { |
120 LOG(WARNING) << "Expected JSON dictionary in configuration file " | 152 LOG(WARNING) << "Expected JSON dictionary in configuration file " |
121 << config_file_iter->value(); | 153 << config_file_iter->value(); |
154 status.Add(JsonErrorToPolicyLoadStatus(error_code)); | |
122 continue; | 155 continue; |
123 } | 156 } |
124 | 157 |
125 // Detach the "3rdparty" node. | 158 // Detach the "3rdparty" node. |
126 base::Value* third_party = NULL; | 159 base::Value* third_party = NULL; |
127 if (dictionary_value->Remove("3rdparty", &third_party)) { | 160 if (dictionary_value->Remove("3rdparty", &third_party)) { |
128 Merge3rdPartyPolicy(third_party, level, bundle); | 161 Merge3rdPartyPolicy(third_party, level, bundle); |
129 delete third_party; | 162 delete third_party; |
130 } | 163 } |
131 | 164 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 } | 221 } |
189 } | 222 } |
190 | 223 |
191 void ConfigDirPolicyLoader::OnFileUpdated(const base::FilePath& path, | 224 void ConfigDirPolicyLoader::OnFileUpdated(const base::FilePath& path, |
192 bool error) { | 225 bool error) { |
193 if (!error) | 226 if (!error) |
194 Reload(false); | 227 Reload(false); |
195 } | 228 } |
196 | 229 |
197 } // namespace policy | 230 } // namespace policy |
OLD | NEW |