Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: chrome/browser/policy/policy_loader_win.cc

Issue 10696034: Share JSON schema constants. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: ,. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
28 #include "chrome/common/json_schema_validator.h"
27 #include "policy/policy_constants.h" 29 #include "policy/policy_constants.h"
28 30
31 namespace schema = json_schema_constants;
32
29 using base::win::RegKey; 33 using base::win::RegKey;
30 using base::win::RegistryKeyIterator; 34 using base::win::RegistryKeyIterator;
31 using base::win::RegistryValueIterator; 35 using base::win::RegistryValueIterator;
32 using namespace policy::registry_constants; 36 using namespace policy::registry_constants;
33 37
34 namespace policy { 38 namespace policy {
35 39
36 namespace registry_constants { 40 namespace registry_constants {
37 const wchar_t kPathSep[] = L"\\"; 41 const wchar_t kPathSep[] = L"\\";
38 const wchar_t kThirdParty[] = L"3rdparty"; 42 const wchar_t kThirdParty[] = L"3rdparty";
39 const wchar_t kMandatory[] = L"policy"; 43 const wchar_t kMandatory[] = L"policy";
40 const wchar_t kRecommended[] = L"recommended"; 44 const wchar_t kRecommended[] = L"recommended";
41 const wchar_t kSchema[] = L"schema"; 45 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 46 } // namespace registry_constants
47 47
48 namespace { 48 namespace {
49 49
50 // Map of registry hives to their corresponding policy scope, in decreasing 50 // Map of registry hives to their corresponding policy scope, in decreasing
51 // order of priority. 51 // order of priority.
52 const struct { 52 const struct {
53 HKEY hive; 53 HKEY hive;
54 PolicyScope scope; 54 PolicyScope scope;
55 } kHives[] = { 55 } kHives[] = {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 return NULL; 220 return NULL;
221 string16 value; 221 string16 value;
222 if (!ReadRegistryString(&key, name, &value)) 222 if (!ReadRegistryString(&key, name, &value))
223 return NULL; 223 return NULL;
224 return base::JSONReader::Read(UTF16ToUTF8(value)); 224 return base::JSONReader::Read(UTF16ToUTF8(value));
225 } 225 }
226 226
227 // Returns the Value type described in |schema|, or |default_type| if not found. 227 // Returns the Value type described in |schema|, or |default_type| if not found.
228 base::Value::Type GetType(const base::DictionaryValue* schema, 228 base::Value::Type GetType(const base::DictionaryValue* schema,
229 base::Value::Type default_type) { 229 base::Value::Type default_type) {
230 // JSON-schema types to base::Value::Type mapping. 230 std::string schema_type;
231 static const struct { 231 base::Value::Type value_type;
232 // JSON schema type. 232 if (schema &&
233 const char* schema_type; 233 schema->GetString(schema::kType, &schema_type) &&
234 // Correspondent value type. 234 JSONSchemaValidator::GetValueType(schema_type, &value_type)) {
235 base::Value::Type value_type; 235 return value_type;
236 } kSchemaToValueTypeMap[] = {
237 { "array", base::Value::TYPE_LIST },
238 { "boolean", base::Value::TYPE_BOOLEAN },
239 { "integer", base::Value::TYPE_INTEGER },
240 { "null", base::Value::TYPE_NULL },
241 { "number", base::Value::TYPE_DOUBLE },
242 { "object", base::Value::TYPE_DICTIONARY },
243 { "string", base::Value::TYPE_STRING },
244 };
245
246 if (!schema)
247 return default_type;
248 std::string type;
249 if (!schema->GetString(kType, &type))
250 return default_type;
251 for (size_t i = 0; i < arraysize(kSchemaToValueTypeMap); ++i) {
252 if (type == kSchemaToValueTypeMap[i].schema_type)
253 return kSchemaToValueTypeMap[i].value_type;
254 } 236 }
255 return default_type; 237 return default_type;
256 } 238 }
257 239
258 // Returns the default type for registry entries of |reg_type|, when there is 240 // Returns the default type for registry entries of |reg_type|, when there is
259 // no schema defined type for a policy. 241 // no schema defined type for a policy.
260 base::Value::Type GetDefaultFor(DWORD reg_type) { 242 base::Value::Type GetDefaultFor(DWORD reg_type) {
261 return reg_type == REG_DWORD ? base::Value::TYPE_INTEGER : 243 return reg_type == REG_DWORD ? base::Value::TYPE_INTEGER :
262 base::Value::TYPE_STRING; 244 base::Value::TYPE_STRING;
263 } 245 }
264 246
265 // Returns the entry with key |name| in |dictionary| (can be NULL), or NULL. 247 // Returns the entry with key |name| in |dictionary| (can be NULL), or NULL.
266 const base::DictionaryValue* GetEntry(const base::DictionaryValue* dictionary, 248 const base::DictionaryValue* GetEntry(const base::DictionaryValue* dictionary,
267 const std::string& name) { 249 const std::string& name) {
268 if (!dictionary) 250 if (!dictionary)
269 return NULL; 251 return NULL;
270 const base::DictionaryValue* entry = NULL; 252 const base::DictionaryValue* entry = NULL;
271 dictionary->GetDictionary(name, &entry); 253 dictionary->GetDictionary(name, &entry);
272 return entry; 254 return entry;
273 } 255 }
274 256
275 // Returns the schema for property |name| given the |schema| of an object. 257 // Returns the schema for property |name| given the |schema| of an object.
276 // Returns the "additionalProperties" schema if no specific schema for 258 // Returns the "additionalProperties" schema if no specific schema for
277 // |name| is present. Returns NULL if no schema is found. 259 // |name| is present. Returns NULL if no schema is found.
278 const base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema, 260 const base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema,
279 const std::string& name) { 261 const std::string& name) {
280 const base::DictionaryValue* properties = GetEntry(schema, kProperties); 262 const base::DictionaryValue* properties =
263 GetEntry(schema, schema::kProperties);
281 const base::DictionaryValue* sub_schema = GetEntry(properties, name); 264 const base::DictionaryValue* sub_schema = GetEntry(properties, name);
282 if (sub_schema) 265 if (sub_schema)
283 return sub_schema; 266 return sub_schema;
284 // "additionalProperties" can be a boolean, but that case is ignored. 267 // "additionalProperties" can be a boolean, but that case is ignored.
285 return GetEntry(schema, kAdditionalProperties); 268 return GetEntry(schema, schema::kAdditionalProperties);
286 } 269 }
287 270
288 // Converts string |value| to another |type|, if possible. 271 // Converts string |value| to another |type|, if possible.
289 base::Value* ConvertComponentStringValue(const string16& value, 272 base::Value* ConvertComponentStringValue(const string16& value,
290 base::Value::Type type) { 273 base::Value::Type type) {
291 switch (type) { 274 switch (type) {
292 case base::Value::TYPE_NULL: 275 case base::Value::TYPE_NULL:
293 return base::Value::CreateNullValue(); 276 return base::Value::CreateNullValue();
294 277
295 case base::Value::TYPE_BOOLEAN: { 278 case base::Value::TYPE_BOOLEAN: {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 const base::DictionaryValue* schema) { 381 const base::DictionaryValue* schema) {
399 // The sub-elements are indexed from 1 to N. They can be represented as 382 // 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 383 // registry values or registry keys though; use |schema| first to try to
401 // determine the right type, and if that fails default to STRING. 384 // determine the right type, and if that fails default to STRING.
402 385
403 RegKey key(hive, path.c_str(), KEY_READ); 386 RegKey key(hive, path.c_str(), KEY_READ);
404 if (!key.Valid()) 387 if (!key.Valid())
405 return NULL; 388 return NULL;
406 389
407 // Get the schema for list items. 390 // Get the schema for list items.
408 schema = GetEntry(schema, kItems); 391 schema = GetEntry(schema, schema::kItems);
409 base::Value::Type type = GetType(schema, base::Value::TYPE_STRING); 392 base::Value::Type type = GetType(schema, base::Value::TYPE_STRING);
410 base::ListValue* list = new base::ListValue(); 393 base::ListValue* list = new base::ListValue();
411 for (int i = 1; ; ++i) { 394 for (int i = 1; ; ++i) {
412 string16 name = base::IntToString16(i); 395 string16 name = base::IntToString16(i);
413 base::Value* value = NULL; 396 base::Value* value = NULL;
414 if (type == base::Value::TYPE_DICTIONARY) { 397 if (type == base::Value::TYPE_DICTIONARY) {
415 value = 398 value =
416 ReadComponentDictionaryValue(hive, path + kPathSep + name, schema); 399 ReadComponentDictionaryValue(hive, path + kPathSep + name, schema);
417 } else if (type == base::Value::TYPE_LIST) { 400 } else if (type == base::Value::TYPE_LIST) {
418 value = ReadComponentListValue(hive, path + kPathSep + name, schema); 401 value = ReadComponentListValue(hive, path + kPathSep + name, schema);
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 666
684 void PolicyLoaderWin::OnObjectSignaled(HANDLE object) { 667 void PolicyLoaderWin::OnObjectSignaled(HANDLE object) {
685 DCHECK(object == user_policy_changed_event_.handle() || 668 DCHECK(object == user_policy_changed_event_.handle() ||
686 object == machine_policy_changed_event_.handle()) 669 object == machine_policy_changed_event_.handle())
687 << "unexpected object signaled policy reload, obj = " 670 << "unexpected object signaled policy reload, obj = "
688 << std::showbase << std::hex << object; 671 << std::showbase << std::hex << object;
689 Reload(false); 672 Reload(false);
690 } 673 }
691 674
692 } // namespace policy 675 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698