Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/task.h" | 12 #include "base/task.h" |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 12 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 16 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 14 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 15 #include "content/browser/browser_thread.h" | 18 #include "content/browser/browser_thread.h" |
| 16 #include "policy/configuration_policy_type.h" | 19 #include "policy/configuration_policy_type.h" |
| 17 | 20 |
| 18 namespace policy { | 21 namespace policy { |
| 19 | 22 |
| 20 // Decodes a CloudPolicySettings object into two maps with mandatory and | 23 // Decodes a CloudPolicySettings object into two maps with mandatory and |
| 21 // recommended settings, respectively. The implementation is generated code | 24 // recommended settings, respectively. The implementation is generated code |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 PolicyMap* mandatory, | 143 PolicyMap* mandatory, |
| 141 PolicyMap* recommended) { | 144 PolicyMap* recommended) { |
| 142 // TODO(jkummerow): Verify policy_data.device_token(). Needs final | 145 // TODO(jkummerow): Verify policy_data.device_token(). Needs final |
| 143 // specification which token we're actually sending / expecting to get back. | 146 // specification which token we're actually sending / expecting to get back. |
| 144 em::CloudPolicySettings policy; | 147 em::CloudPolicySettings policy; |
| 145 if (!policy.ParseFromString(policy_data.policy_value())) { | 148 if (!policy.ParseFromString(policy_data.policy_value())) { |
| 146 LOG(WARNING) << "Failed to parse CloudPolicySettings protobuf."; | 149 LOG(WARNING) << "Failed to parse CloudPolicySettings protobuf."; |
| 147 return false; | 150 return false; |
| 148 } | 151 } |
| 149 DecodePolicy(policy, mandatory, recommended); | 152 DecodePolicy(policy, mandatory, recommended); |
| 153 MaybeDecodeOldstylePolicy(policy, mandatory, recommended); | |
|
Mattias Nissler (ping if slow)
2011/04/13 17:53:02
Instead of hacking the field at position 2 into Cl
Jakob Kummerow
2011/04/14 17:17:00
Good idea. Done.
| |
| 150 return true; | 154 return true; |
| 151 } | 155 } |
| 152 | 156 |
| 157 // Everything below is only needed for supporting old-style GenericNamedValue | |
| 158 // based policy data and can be removed once this support is no longer needed. | |
| 159 | |
| 160 using google::protobuf::RepeatedField; | |
| 161 using google::protobuf::RepeatedPtrField; | |
| 162 | |
| 163 class PolicyMapProxy : public ConfigurationPolicyStoreInterface { | |
| 164 public: | |
| 165 // Does not take ownership of |policy_map|, and callers need to make sure | |
| 166 // that |policy_map| outlives this PolicyMapProxy. | |
| 167 explicit PolicyMapProxy(PolicyMap* policy_map) | |
| 168 : policy_map_(policy_map) {} | |
| 169 virtual ~PolicyMapProxy() {} | |
| 170 virtual void Apply(ConfigurationPolicyType policy, Value* value) { | |
| 171 policy_map_->Set(policy, value); | |
| 172 } | |
| 173 | |
| 174 private: | |
| 175 PolicyMap* policy_map_; | |
|
Mattias Nissler (ping if slow)
2011/04/13 17:53:02
DISALLOW_COPY_AND_ASSIGN
Jakob Kummerow
2011/04/14 17:17:00
Done.
| |
| 176 }; | |
| 177 | |
| 178 void UserPolicyCache::MaybeDecodeOldstylePolicy( | |
| 179 const em::CloudPolicySettings& policy, | |
| 180 PolicyMap* mandatory, | |
| 181 PolicyMap* recommended) { | |
| 182 // Return immediately if we already have policy information in the maps. | |
| 183 if (!mandatory->empty() || !recommended->empty()) | |
| 184 return; | |
| 185 // Return if there's no old-style policy to decode. | |
| 186 if (policy.named_value_size() == 0) | |
| 187 return; | |
| 188 | |
| 189 // Inspect GenericNamedValues and decode them. | |
| 190 DictionaryValue result; | |
| 191 RepeatedPtrField<em::GenericNamedValue>::const_iterator named_value; | |
| 192 for (named_value = policy.named_value().begin(); | |
| 193 named_value != policy.named_value().end(); | |
| 194 ++named_value) { | |
| 195 if (named_value->has_value()) { | |
| 196 Value* decoded_value = DecodeValue(named_value->value()); | |
| 197 if (decoded_value) | |
| 198 result.Set(named_value->name(), decoded_value); | |
| 199 } | |
| 200 } | |
| 201 // Hack: Let one of the providers do the transformation from DictionaryValue | |
| 202 // to PolicyMap, since they have the required code anyway. | |
| 203 PolicyMapProxy map_proxy(mandatory); | |
| 204 GetManagedPolicyProvider()->ApplyPolicyValueTree(&result, &map_proxy); | |
| 205 } | |
| 206 | |
| 207 Value* UserPolicyCache::DecodeIntegerValue( | |
| 208 google::protobuf::int64 value) const { | |
| 209 if (value < std::numeric_limits<int>::min() || | |
| 210 value > std::numeric_limits<int>::max()) { | |
| 211 LOG(WARNING) << "Integer value " << value | |
| 212 << " out of numeric limits, ignoring."; | |
| 213 return NULL; | |
| 214 } | |
| 215 | |
| 216 return Value::CreateIntegerValue(static_cast<int>(value)); | |
| 217 } | |
| 218 | |
| 219 Value* UserPolicyCache::DecodeValue(const em::GenericValue& value) const { | |
| 220 if (!value.has_value_type()) | |
| 221 return NULL; | |
| 222 | |
| 223 switch (value.value_type()) { | |
| 224 case em::GenericValue::VALUE_TYPE_BOOL: | |
| 225 if (value.has_bool_value()) | |
| 226 return Value::CreateBooleanValue(value.bool_value()); | |
| 227 return NULL; | |
| 228 case em::GenericValue::VALUE_TYPE_INT64: | |
| 229 if (value.has_int64_value()) | |
| 230 return DecodeIntegerValue(value.int64_value()); | |
| 231 return NULL; | |
| 232 case em::GenericValue::VALUE_TYPE_STRING: | |
| 233 if (value.has_string_value()) | |
| 234 return Value::CreateStringValue(value.string_value()); | |
| 235 return NULL; | |
| 236 case em::GenericValue::VALUE_TYPE_DOUBLE: | |
| 237 if (value.has_double_value()) | |
| 238 return Value::CreateDoubleValue(value.double_value()); | |
| 239 return NULL; | |
| 240 case em::GenericValue::VALUE_TYPE_BYTES: | |
| 241 if (value.has_bytes_value()) { | |
| 242 std::string bytes = value.bytes_value(); | |
| 243 return BinaryValue::CreateWithCopiedBuffer(bytes.c_str(), bytes.size()); | |
| 244 } | |
| 245 return NULL; | |
| 246 case em::GenericValue::VALUE_TYPE_BOOL_ARRAY: { | |
| 247 ListValue* list = new ListValue; | |
| 248 RepeatedField<bool>::const_iterator i; | |
| 249 for (i = value.bool_array().begin(); i != value.bool_array().end(); ++i) | |
| 250 list->Append(Value::CreateBooleanValue(*i)); | |
| 251 return list; | |
| 252 } | |
| 253 case em::GenericValue::VALUE_TYPE_INT64_ARRAY: { | |
| 254 ListValue* list = new ListValue; | |
| 255 RepeatedField<google::protobuf::int64>::const_iterator i; | |
| 256 for (i = value.int64_array().begin(); | |
| 257 i != value.int64_array().end(); ++i) { | |
| 258 Value* int_value = DecodeIntegerValue(*i); | |
| 259 if (int_value) | |
| 260 list->Append(int_value); | |
| 261 } | |
| 262 return list; | |
| 263 } | |
| 264 case em::GenericValue::VALUE_TYPE_STRING_ARRAY: { | |
| 265 ListValue* list = new ListValue; | |
| 266 RepeatedPtrField<std::string>::const_iterator i; | |
| 267 for (i = value.string_array().begin(); | |
| 268 i != value.string_array().end(); ++i) | |
| 269 list->Append(Value::CreateStringValue(*i)); | |
| 270 return list; | |
| 271 } | |
| 272 case em::GenericValue::VALUE_TYPE_DOUBLE_ARRAY: { | |
| 273 ListValue* list = new ListValue; | |
| 274 RepeatedField<double>::const_iterator i; | |
| 275 for (i = value.double_array().begin(); | |
| 276 i != value.double_array().end(); ++i) | |
| 277 list->Append(Value::CreateDoubleValue(*i)); | |
| 278 return list; | |
| 279 } | |
| 280 default: | |
| 281 NOTREACHED() << "Unhandled value type"; | |
| 282 } | |
| 283 | |
| 284 return NULL; | |
| 285 } | |
| 286 | |
| 153 } // namespace policy | 287 } // namespace policy |
| OLD | NEW |