| 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/policy_loader_win.h" | 5 #include "chrome/browser/policy/policy_loader_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include <userenv.h> | 11 #include <userenv.h> |
| 12 | 12 |
| 13 // userenv.dll is required for RegisterGPNotification(). | 13 // userenv.dll is required for RegisterGPNotification(). |
| 14 #pragma comment(lib, "userenv.lib") | 14 #pragma comment(lib, "userenv.lib") |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/json/json_reader.h" | 17 #include "base/json/json_reader.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/string16.h" | 20 #include "base/string16.h" |
| 21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
| 22 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 23 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "base/win/registry.h" | 24 #include "base/win/registry.h" |
| 25 #include "chrome/browser/policy/policy_bundle.h" | 25 #include "chrome/browser/policy/policy_bundle.h" |
| 26 #include "chrome/browser/policy/policy_map.h" | 26 #include "chrome/browser/policy/policy_map.h" |
| 27 #include "chrome/common/json_schema_constants.h" |
| 27 #include "policy/policy_constants.h" | 28 #include "policy/policy_constants.h" |
| 28 | 29 |
| 30 namespace schema = json_schema_constants; |
| 31 |
| 29 using base::win::RegKey; | 32 using base::win::RegKey; |
| 30 using base::win::RegistryKeyIterator; | 33 using base::win::RegistryKeyIterator; |
| 31 using base::win::RegistryValueIterator; | 34 using base::win::RegistryValueIterator; |
| 32 using namespace policy::registry_constants; | 35 using namespace policy::registry_constants; |
| 33 | 36 |
| 34 namespace policy { | 37 namespace policy { |
| 35 | 38 |
| 36 namespace registry_constants { | 39 namespace registry_constants { |
| 37 const wchar_t kPathSep[] = L"\\"; | 40 const wchar_t kPathSep[] = L"\\"; |
| 38 const wchar_t kThirdParty[] = L"3rdparty"; | 41 const wchar_t kThirdParty[] = L"3rdparty"; |
| 39 const wchar_t kMandatory[] = L"policy"; | 42 const wchar_t kMandatory[] = L"policy"; |
| 40 const wchar_t kRecommended[] = L"recommended"; | 43 const wchar_t kRecommended[] = L"recommended"; |
| 41 const wchar_t kSchema[] = L"schema"; | 44 const wchar_t kSchema[] = L"schema"; |
| 42 const char kType[] = "type"; | |
| 43 const char kProperties[] = "properties"; | |
| 44 const char kAdditionalProperties[] = "additionalProperties"; | |
| 45 const char kItems[] = "items"; | |
| 46 } // namespace registry_constants | 45 } // namespace registry_constants |
| 47 | 46 |
| 48 namespace { | 47 namespace { |
| 49 | 48 |
| 50 // Map of registry hives to their corresponding policy scope, in decreasing | 49 // Map of registry hives to their corresponding policy scope, in decreasing |
| 51 // order of priority. | 50 // order of priority. |
| 52 const struct { | 51 const struct { |
| 53 HKEY hive; | 52 HKEY hive; |
| 54 PolicyScope scope; | 53 PolicyScope scope; |
| 55 } kHives[] = { | 54 } kHives[] = { |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 // Returns the Value type described in |schema|, or |default_type| if not found. | 226 // Returns the Value type described in |schema|, or |default_type| if not found. |
| 228 base::Value::Type GetType(const base::DictionaryValue* schema, | 227 base::Value::Type GetType(const base::DictionaryValue* schema, |
| 229 base::Value::Type default_type) { | 228 base::Value::Type default_type) { |
| 230 // JSON-schema types to base::Value::Type mapping. | 229 // JSON-schema types to base::Value::Type mapping. |
| 231 static const struct { | 230 static const struct { |
| 232 // JSON schema type. | 231 // JSON schema type. |
| 233 const char* schema_type; | 232 const char* schema_type; |
| 234 // Correspondent value type. | 233 // Correspondent value type. |
| 235 base::Value::Type value_type; | 234 base::Value::Type value_type; |
| 236 } kSchemaToValueTypeMap[] = { | 235 } kSchemaToValueTypeMap[] = { |
| 237 { "array", base::Value::TYPE_LIST }, | 236 { schema::kArray, base::Value::TYPE_LIST }, |
| 238 { "boolean", base::Value::TYPE_BOOLEAN }, | 237 { schema::kBoolean, base::Value::TYPE_BOOLEAN }, |
| 239 { "integer", base::Value::TYPE_INTEGER }, | 238 { schema::kInteger, base::Value::TYPE_INTEGER }, |
| 240 { "null", base::Value::TYPE_NULL }, | 239 { schema::kNull, base::Value::TYPE_NULL }, |
| 241 { "number", base::Value::TYPE_DOUBLE }, | 240 { schema::kNumber, base::Value::TYPE_DOUBLE }, |
| 242 { "object", base::Value::TYPE_DICTIONARY }, | 241 { schema::kObject, base::Value::TYPE_DICTIONARY }, |
| 243 { "string", base::Value::TYPE_STRING }, | 242 { schema::kString, base::Value::TYPE_STRING }, |
| 244 }; | 243 }; |
| 245 | 244 |
| 246 if (!schema) | 245 if (!schema) |
| 247 return default_type; | 246 return default_type; |
| 248 std::string type; | 247 std::string type; |
| 249 if (!schema->GetString(kType, &type)) | 248 if (!schema->GetString(schema::kType, &type)) |
| 250 return default_type; | 249 return default_type; |
| 251 for (size_t i = 0; i < arraysize(kSchemaToValueTypeMap); ++i) { | 250 for (size_t i = 0; i < arraysize(kSchemaToValueTypeMap); ++i) { |
| 252 if (type == kSchemaToValueTypeMap[i].schema_type) | 251 if (type == kSchemaToValueTypeMap[i].schema_type) |
| 253 return kSchemaToValueTypeMap[i].value_type; | 252 return kSchemaToValueTypeMap[i].value_type; |
| 254 } | 253 } |
| 255 return default_type; | 254 return default_type; |
| 256 } | 255 } |
| 257 | 256 |
| 258 // Returns the default type for registry entries of |reg_type|, when there is | 257 // Returns the default type for registry entries of |reg_type|, when there is |
| 259 // no schema defined type for a policy. | 258 // no schema defined type for a policy. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 270 const base::DictionaryValue* entry = NULL; | 269 const base::DictionaryValue* entry = NULL; |
| 271 dictionary->GetDictionary(name, &entry); | 270 dictionary->GetDictionary(name, &entry); |
| 272 return entry; | 271 return entry; |
| 273 } | 272 } |
| 274 | 273 |
| 275 // Returns the schema for property |name| given the |schema| of an object. | 274 // Returns the schema for property |name| given the |schema| of an object. |
| 276 // Returns the "additionalProperties" schema if no specific schema for | 275 // Returns the "additionalProperties" schema if no specific schema for |
| 277 // |name| is present. Returns NULL if no schema is found. | 276 // |name| is present. Returns NULL if no schema is found. |
| 278 const base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema, | 277 const base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema, |
| 279 const std::string& name) { | 278 const std::string& name) { |
| 280 const base::DictionaryValue* properties = GetEntry(schema, kProperties); | 279 const base::DictionaryValue* properties = |
| 280 GetEntry(schema, schema::kProperties); |
| 281 const base::DictionaryValue* sub_schema = GetEntry(properties, name); | 281 const base::DictionaryValue* sub_schema = GetEntry(properties, name); |
| 282 if (sub_schema) | 282 if (sub_schema) |
| 283 return sub_schema; | 283 return sub_schema; |
| 284 // "additionalProperties" can be a boolean, but that case is ignored. | 284 // "additionalProperties" can be a boolean, but that case is ignored. |
| 285 return GetEntry(schema, kAdditionalProperties); | 285 return GetEntry(schema, schema::kAdditionalProperties); |
| 286 } | 286 } |
| 287 | 287 |
| 288 // Converts string |value| to another |type|, if possible. | 288 // Converts string |value| to another |type|, if possible. |
| 289 base::Value* ConvertComponentStringValue(const string16& value, | 289 base::Value* ConvertComponentStringValue(const string16& value, |
| 290 base::Value::Type type) { | 290 base::Value::Type type) { |
| 291 switch (type) { | 291 switch (type) { |
| 292 case base::Value::TYPE_NULL: | 292 case base::Value::TYPE_NULL: |
| 293 return base::Value::CreateNullValue(); | 293 return base::Value::CreateNullValue(); |
| 294 | 294 |
| 295 case base::Value::TYPE_BOOLEAN: { | 295 case base::Value::TYPE_BOOLEAN: { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 const base::DictionaryValue* schema) { | 398 const base::DictionaryValue* schema) { |
| 399 // The sub-elements are indexed from 1 to N. They can be represented as | 399 // The sub-elements are indexed from 1 to N. They can be represented as |
| 400 // registry values or registry keys though; use |schema| first to try to | 400 // registry values or registry keys though; use |schema| first to try to |
| 401 // determine the right type, and if that fails default to STRING. | 401 // determine the right type, and if that fails default to STRING. |
| 402 | 402 |
| 403 RegKey key(hive, path.c_str(), KEY_READ); | 403 RegKey key(hive, path.c_str(), KEY_READ); |
| 404 if (!key.Valid()) | 404 if (!key.Valid()) |
| 405 return NULL; | 405 return NULL; |
| 406 | 406 |
| 407 // Get the schema for list items. | 407 // Get the schema for list items. |
| 408 schema = GetEntry(schema, kItems); | 408 schema = GetEntry(schema, schema::kItems); |
| 409 base::Value::Type type = GetType(schema, base::Value::TYPE_STRING); | 409 base::Value::Type type = GetType(schema, base::Value::TYPE_STRING); |
| 410 base::ListValue* list = new base::ListValue(); | 410 base::ListValue* list = new base::ListValue(); |
| 411 for (int i = 1; ; ++i) { | 411 for (int i = 1; ; ++i) { |
| 412 string16 name = base::IntToString16(i); | 412 string16 name = base::IntToString16(i); |
| 413 base::Value* value = NULL; | 413 base::Value* value = NULL; |
| 414 if (type == base::Value::TYPE_DICTIONARY) { | 414 if (type == base::Value::TYPE_DICTIONARY) { |
| 415 value = | 415 value = |
| 416 ReadComponentDictionaryValue(hive, path + kPathSep + name, schema); | 416 ReadComponentDictionaryValue(hive, path + kPathSep + name, schema); |
| 417 } else if (type == base::Value::TYPE_LIST) { | 417 } else if (type == base::Value::TYPE_LIST) { |
| 418 value = ReadComponentListValue(hive, path + kPathSep + name, schema); | 418 value = ReadComponentListValue(hive, path + kPathSep + name, schema); |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 683 | 683 |
| 684 void PolicyLoaderWin::OnObjectSignaled(HANDLE object) { | 684 void PolicyLoaderWin::OnObjectSignaled(HANDLE object) { |
| 685 DCHECK(object == user_policy_changed_event_.handle() || | 685 DCHECK(object == user_policy_changed_event_.handle() || |
| 686 object == machine_policy_changed_event_.handle()) | 686 object == machine_policy_changed_event_.handle()) |
| 687 << "unexpected object signaled policy reload, obj = " | 687 << "unexpected object signaled policy reload, obj = " |
| 688 << std::showbase << std::hex << object; | 688 << std::showbase << std::hex << object; |
| 689 Reload(false); | 689 Reload(false); |
| 690 } | 690 } |
| 691 | 691 |
| 692 } // namespace policy | 692 } // namespace policy |
| OLD | NEW |