| 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: |
| 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_INVALID_ESCAPE: |
| 41 case base::JSONReader::JSON_SYNTAX_ERROR: |
| 42 case base::JSONReader::JSON_UNEXPECTED_TOKEN: |
| 43 case base::JSONReader::JSON_TRAILING_COMMA: |
| 44 case base::JSONReader::JSON_TOO_MUCH_NESTING: |
| 45 case base::JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT: |
| 46 case base::JSONReader::JSON_UNSUPPORTED_ENCODING: |
| 47 case base::JSONReader::JSON_UNQUOTED_DICTIONARY_KEY: |
| 48 return POLICY_LOAD_STATUS_PARSE_ERROR; |
| 49 case base::JSONReader::JSON_NO_ERROR: |
| 50 NOTREACHED(); |
| 51 return POLICY_LOAD_STATUS_STARTED; |
| 52 } |
| 53 NOTREACHED() << "Invalid status " << status; |
| 54 return POLICY_LOAD_STATUS_PARSE_ERROR; |
| 55 } |
| 56 |
| 30 } // namespace | 57 } // namespace |
| 31 | 58 |
| 32 ConfigDirPolicyLoader::ConfigDirPolicyLoader(const base::FilePath& config_dir, | 59 ConfigDirPolicyLoader::ConfigDirPolicyLoader(const base::FilePath& config_dir, |
| 33 PolicyScope scope) | 60 PolicyScope scope) |
| 34 : config_dir_(config_dir), | 61 : config_dir_(config_dir), |
| 35 scope_(scope) {} | 62 scope_(scope) {} |
| 36 | 63 |
| 37 ConfigDirPolicyLoader::~ConfigDirPolicyLoader() {} | 64 ConfigDirPolicyLoader::~ConfigDirPolicyLoader() {} |
| 38 | 65 |
| 39 void ConfigDirPolicyLoader::InitOnFile() { | 66 void ConfigDirPolicyLoader::InitOnFile() { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 PolicyLevel level, | 117 PolicyLevel level, |
| 91 PolicyBundle* bundle) { | 118 PolicyBundle* bundle) { |
| 92 // Enumerate the files and sort them lexicographically. | 119 // Enumerate the files and sort them lexicographically. |
| 93 std::set<base::FilePath> files; | 120 std::set<base::FilePath> files; |
| 94 file_util::FileEnumerator file_enumerator(path, false, | 121 file_util::FileEnumerator file_enumerator(path, false, |
| 95 file_util::FileEnumerator::FILES); | 122 file_util::FileEnumerator::FILES); |
| 96 for (base::FilePath config_file_path = file_enumerator.Next(); | 123 for (base::FilePath config_file_path = file_enumerator.Next(); |
| 97 !config_file_path.empty(); config_file_path = file_enumerator.Next()) | 124 !config_file_path.empty(); config_file_path = file_enumerator.Next()) |
| 98 files.insert(config_file_path); | 125 files.insert(config_file_path); |
| 99 | 126 |
| 127 PolicyLoadStatusSample status; |
| 128 if (files.empty()) { |
| 129 status.Add(POLICY_LOAD_STATUS_NO_POLICY); |
| 130 return; |
| 131 } |
| 132 |
| 100 // Start with an empty dictionary and merge the files' contents. | 133 // Start with an empty dictionary and merge the files' contents. |
| 101 // The files are processed in reverse order because |MergeFrom| gives priority | 134 // The files are processed in reverse order because |MergeFrom| gives priority |
| 102 // to existing keys, but the ConfigDirPolicyProvider gives priority to the | 135 // to existing keys, but the ConfigDirPolicyProvider gives priority to the |
| 103 // last file in lexicographic order. | 136 // last file in lexicographic order. |
| 104 for (std::set<base::FilePath>::reverse_iterator config_file_iter = | 137 for (std::set<base::FilePath>::reverse_iterator config_file_iter = |
| 105 files.rbegin(); config_file_iter != files.rend(); | 138 files.rbegin(); config_file_iter != files.rend(); |
| 106 ++config_file_iter) { | 139 ++config_file_iter) { |
| 107 JSONFileValueSerializer deserializer(*config_file_iter); | 140 JSONFileValueSerializer deserializer(*config_file_iter); |
| 108 deserializer.set_allow_trailing_comma(true); | 141 deserializer.set_allow_trailing_comma(true); |
| 109 int error_code = 0; | 142 int error_code = 0; |
| 110 std::string error_msg; | 143 std::string error_msg; |
| 111 scoped_ptr<base::Value> value( | 144 scoped_ptr<base::Value> value( |
| 112 deserializer.Deserialize(&error_code, &error_msg)); | 145 deserializer.Deserialize(&error_code, &error_msg)); |
| 113 if (!value.get()) { | 146 if (!value.get()) { |
| 114 LOG(WARNING) << "Failed to read configuration file " | 147 LOG(WARNING) << "Failed to read configuration file " |
| 115 << config_file_iter->value() << ": " << error_msg; | 148 << config_file_iter->value() << ": " << error_msg; |
| 149 status.Add(JsonErrorToPolicyLoadStatus(error_code)); |
| 116 continue; | 150 continue; |
| 117 } | 151 } |
| 118 base::DictionaryValue* dictionary_value = NULL; | 152 base::DictionaryValue* dictionary_value = NULL; |
| 119 if (!value->GetAsDictionary(&dictionary_value)) { | 153 if (!value->GetAsDictionary(&dictionary_value)) { |
| 120 LOG(WARNING) << "Expected JSON dictionary in configuration file " | 154 LOG(WARNING) << "Expected JSON dictionary in configuration file " |
| 121 << config_file_iter->value(); | 155 << config_file_iter->value(); |
| 156 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
| 122 continue; | 157 continue; |
| 123 } | 158 } |
| 124 | 159 |
| 125 // Detach the "3rdparty" node. | 160 // Detach the "3rdparty" node. |
| 126 base::Value* third_party = NULL; | 161 base::Value* third_party = NULL; |
| 127 if (dictionary_value->Remove("3rdparty", &third_party)) { | 162 if (dictionary_value->Remove("3rdparty", &third_party)) { |
| 128 Merge3rdPartyPolicy(third_party, level, bundle); | 163 Merge3rdPartyPolicy(third_party, level, bundle); |
| 129 delete third_party; | 164 delete third_party; |
| 130 } | 165 } |
| 131 | 166 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 223 } |
| 189 } | 224 } |
| 190 | 225 |
| 191 void ConfigDirPolicyLoader::OnFileUpdated(const base::FilePath& path, | 226 void ConfigDirPolicyLoader::OnFileUpdated(const base::FilePath& path, |
| 192 bool error) { | 227 bool error) { |
| 193 if (!error) | 228 if (!error) |
| 194 Reload(false); | 229 Reload(false); |
| 195 } | 230 } |
| 196 | 231 |
| 197 } // namespace policy | 232 } // namespace policy |
| OLD | NEW |