| 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/user_policy_cache.h" | 5 #include "chrome/browser/policy/user_policy_cache.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "chrome/browser/policy/enterprise_metrics.h" | 14 #include "chrome/browser/policy/enterprise_metrics.h" |
| 15 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
| 16 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 16 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 18 #include "chrome/browser/policy/proto/old_generic_format.pb.h" | 18 #include "chrome/browser/policy/proto/old_generic_format.pb.h" |
| 19 #include "policy/configuration_policy_type.h" | 19 #include "policy/configuration_policy_type.h" |
| 20 #include "policy/policy_constants.h" | 20 #include "policy/policy_constants.h" |
| 21 | 21 |
| 22 namespace em = enterprise_management; | 22 namespace em = enterprise_management; |
| 23 | 23 |
| 24 namespace policy { | 24 namespace policy { |
| 25 | 25 |
| 26 // Decodes a CloudPolicySettings object into two maps with mandatory and | 26 // Decodes a CloudPolicySettings object into a PolicyMap. All the policies will |
| 27 // recommended settings, respectively. The implementation is generated code | 27 // be POLICY_SCOPE_USER. The PolicyLevel is decoded from the protobuf. |
| 28 // in policy/cloud_policy_generated.cc. | 28 // The implementation is generated code in policy/cloud_policy_generated.cc. |
| 29 void DecodePolicy(const em::CloudPolicySettings& policy, | 29 void DecodePolicy(const em::CloudPolicySettings& policy, PolicyMap* map); |
| 30 PolicyMap* mandatory, PolicyMap* recommended); | |
| 31 | 30 |
| 32 UserPolicyCache::UserPolicyCache(const FilePath& backing_file_path, | 31 UserPolicyCache::UserPolicyCache(const FilePath& backing_file_path, |
| 33 bool wait_for_policy_fetch) | 32 bool wait_for_policy_fetch) |
| 34 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 33 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| 35 disk_cache_ready_(false), | 34 disk_cache_ready_(false), |
| 36 fetch_ready_(!wait_for_policy_fetch) { | 35 fetch_ready_(!wait_for_policy_fetch) { |
| 37 disk_cache_ = new UserPolicyDiskCache(weak_ptr_factory_.GetWeakPtr(), | 36 disk_cache_ = new UserPolicyDiskCache(weak_ptr_factory_.GetWeakPtr(), |
| 38 backing_file_path); | 37 backing_file_path); |
| 39 } | 38 } |
| 40 | 39 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 set_last_policy_refresh_time(timestamp); | 98 set_last_policy_refresh_time(timestamp); |
| 100 } | 99 } |
| 101 } | 100 } |
| 102 | 101 |
| 103 // Ready to feed policy up the chain! | 102 // Ready to feed policy up the chain! |
| 104 disk_cache_ready_ = true; | 103 disk_cache_ready_ = true; |
| 105 CheckIfReady(); | 104 CheckIfReady(); |
| 106 } | 105 } |
| 107 | 106 |
| 108 bool UserPolicyCache::DecodePolicyData(const em::PolicyData& policy_data, | 107 bool UserPolicyCache::DecodePolicyData(const em::PolicyData& policy_data, |
| 109 PolicyMap* mandatory, | 108 PolicyMap* policies) { |
| 110 PolicyMap* recommended) { | |
| 111 // TODO(jkummerow): Verify policy_data.device_token(). Needs final | 109 // TODO(jkummerow): Verify policy_data.device_token(). Needs final |
| 112 // specification which token we're actually sending / expecting to get back. | 110 // specification which token we're actually sending / expecting to get back. |
| 113 em::CloudPolicySettings policy; | 111 em::CloudPolicySettings policy; |
| 114 if (!policy.ParseFromString(policy_data.policy_value())) { | 112 if (!policy.ParseFromString(policy_data.policy_value())) { |
| 115 LOG(WARNING) << "Failed to parse CloudPolicySettings protobuf."; | 113 LOG(WARNING) << "Failed to parse CloudPolicySettings protobuf."; |
| 116 return false; | 114 return false; |
| 117 } | 115 } |
| 118 DecodePolicy(policy, mandatory, recommended); | 116 DecodePolicy(policy, policies); |
| 119 MaybeDecodeOldstylePolicy(policy_data.policy_value(), mandatory, recommended); | 117 MaybeDecodeOldstylePolicy(policy_data.policy_value(), policies); |
| 120 return true; | 118 return true; |
| 121 } | 119 } |
| 122 | 120 |
| 123 void UserPolicyCache::CheckIfReady() { | 121 void UserPolicyCache::CheckIfReady() { |
| 124 if (!IsReady() && disk_cache_ready_ && fetch_ready_) | 122 if (!IsReady() && disk_cache_ready_ && fetch_ready_) |
| 125 SetReady(); | 123 SetReady(); |
| 126 } | 124 } |
| 127 | 125 |
| 128 // Everything below is only needed for supporting old-style GenericNamedValue | 126 // Everything below is only needed for supporting old-style GenericNamedValue |
| 129 // based policy data and can be removed once this support is no longer needed. | 127 // based policy data and can be removed once this support is no longer needed. |
| 130 | 128 |
| 131 using google::protobuf::RepeatedField; | 129 using google::protobuf::RepeatedField; |
| 132 using google::protobuf::RepeatedPtrField; | 130 using google::protobuf::RepeatedPtrField; |
| 133 | 131 |
| 134 void UserPolicyCache::MaybeDecodeOldstylePolicy( | 132 void UserPolicyCache::MaybeDecodeOldstylePolicy( |
| 135 const std::string& policy_data, | 133 const std::string& policy_data, |
| 136 PolicyMap* mandatory, | 134 PolicyMap* policies) { |
| 137 PolicyMap* recommended) { | |
| 138 // Return immediately if we already have policy information in the maps. | 135 // Return immediately if we already have policy information in the maps. |
| 139 if (!mandatory->empty() || !recommended->empty()) | 136 if (!policies->empty()) |
| 140 return; | 137 return; |
| 141 em::LegacyChromeSettingsProto policy; | 138 em::LegacyChromeSettingsProto policy; |
| 142 // Return if the input string doesn't match the protobuf definition. | 139 // Return if the input string doesn't match the protobuf definition. |
| 143 if (!policy.ParseFromString(policy_data)) | 140 if (!policy.ParseFromString(policy_data)) |
| 144 return; | 141 return; |
| 145 // Return if there's no old-style policy to decode. | 142 // Return if there's no old-style policy to decode. |
| 146 if (policy.named_value_size() == 0) | 143 if (policy.named_value_size() == 0) |
| 147 return; | 144 return; |
| 148 | 145 |
| 149 // Inspect GenericNamedValues and decode them. | 146 // Inspect GenericNamedValues and decode them. |
| 150 DictionaryValue result; | 147 DictionaryValue result; |
| 151 RepeatedPtrField<em::GenericNamedValue>::const_iterator named_value; | 148 RepeatedPtrField<em::GenericNamedValue>::const_iterator named_value; |
| 152 for (named_value = policy.named_value().begin(); | 149 for (named_value = policy.named_value().begin(); |
| 153 named_value != policy.named_value().end(); | 150 named_value != policy.named_value().end(); |
| 154 ++named_value) { | 151 ++named_value) { |
| 155 if (named_value->has_value()) { | 152 if (named_value->has_value()) { |
| 156 Value* decoded_value = DecodeValue(named_value->value()); | 153 Value* decoded_value = DecodeValue(named_value->value()); |
| 157 if (decoded_value) | 154 if (decoded_value) |
| 158 result.Set(named_value->name(), decoded_value); | 155 result.Set(named_value->name(), decoded_value); |
| 159 } | 156 } |
| 160 } | 157 } |
| 161 mandatory->LoadFrom(&result, GetChromePolicyDefinitionList()); | 158 policies->LoadFrom(&result, |
| 159 GetChromePolicyDefinitionList(), |
| 160 POLICY_LEVEL_MANDATORY, |
| 161 POLICY_SCOPE_USER); |
| 162 } | 162 } |
| 163 | 163 |
| 164 Value* UserPolicyCache::DecodeIntegerValue( | 164 Value* UserPolicyCache::DecodeIntegerValue( |
| 165 google::protobuf::int64 value) const { | 165 google::protobuf::int64 value) const { |
| 166 if (value < std::numeric_limits<int>::min() || | 166 if (value < std::numeric_limits<int>::min() || |
| 167 value > std::numeric_limits<int>::max()) { | 167 value > std::numeric_limits<int>::max()) { |
| 168 LOG(WARNING) << "Integer value " << value | 168 LOG(WARNING) << "Integer value " << value |
| 169 << " out of numeric limits, ignoring."; | 169 << " out of numeric limits, ignoring."; |
| 170 return NULL; | 170 return NULL; |
| 171 } | 171 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 return list; | 236 return list; |
| 237 } | 237 } |
| 238 default: | 238 default: |
| 239 NOTREACHED() << "Unhandled value type"; | 239 NOTREACHED() << "Unhandled value type"; |
| 240 } | 240 } |
| 241 | 241 |
| 242 return NULL; | 242 return NULL; |
| 243 } | 243 } |
| 244 | 244 |
| 245 } // namespace policy | 245 } // namespace policy |
| OLD | NEW |