1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/policy_loader_win.h"
#include <string>
#include <string.h>
#include <userenv.h>
// userenv.dll is required for RegisterGPNotification().
#pragma comment(lib, "userenv.lib")
#include "base/basictypes.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "base/win/registry.h"
#include "chrome/browser/policy/policy_bundle.h"
#include "chrome/browser/policy/policy_map.h"
#include "policy/policy_constants.h"
using base::win::RegKey;
using base::win::RegistryKeyIterator;
using base::win::RegistryValueIterator;
using namespace policy::registry_constants;
namespace policy {
namespace registry_constants {
const wchar_t kPathSep[] = L"\\";
const wchar_t kThirdParty[] = L"3rdparty";
const wchar_t kMandatory[] = L"policy";
const wchar_t kRecommended[] = L"recommended";
const wchar_t kSchema[] = L"schema";
const char kType[] = "type";
const char kProperties[] = "properties";
const char kAdditionalProperties[] = "additionalProperties";
const char kItems[] = "items";
} // namespace registry_constants
namespace {
// Map of registry hives to their corresponding policy scope, in decreasing
// order of priority.
const struct {
HKEY hive;
PolicyScope scope;
} kHives[] = {
{ HKEY_LOCAL_MACHINE, POLICY_SCOPE_MACHINE },
{ HKEY_CURRENT_USER, POLICY_SCOPE_USER },
};
// Determines the registry key with the highest priority that contains
// the |key_path| key with a |value_name| value inside.
// |key_path| is a suffix to the Chrome mandatory or recommended registry key,
// and can be empty to lookup values directly at that key.
// |value_name| is the name of a value that should exist at that path. If it
// is empty, then only the existence of the path is verified.
// Returns true if |key| was updated to point to a key with a |key_path| suffix
// and optional |value_name| value inside. In that case, |level| and |scope|
// will contain the appropriate values for the key found.
// Returns false otherwise.
bool LoadHighestPriorityKey(const string16& key_path,
const string16& value_name,
RegKey* key,
PolicyLevel* level,
PolicyScope* scope) {
// |kKeyPaths| is in decreasing order of priority.
static const struct {
const wchar_t* path;
PolicyLevel level;
} kKeyPaths[] = {
{ kRegistryMandatorySubKey, POLICY_LEVEL_MANDATORY },
{ kRegistryRecommendedSubKey, POLICY_LEVEL_RECOMMENDED },
};
// Lookup at the mandatory path for both user and machine policies first, and
// then at the recommended path.
for (size_t k = 0; k < arraysize(kKeyPaths); ++k) {
for (size_t h = 0; h < arraysize(kHives); ++h) {
string16 path(kKeyPaths[k].path);
if (!key_path.empty())
path += kPathSep + key_path;
key->Open(kHives[h].hive, path.c_str(), KEY_READ);
if (!key->Valid())
continue;
if (value_name.empty() || key->HasValue(value_name.c_str())) {
*level = kKeyPaths[k].level;
*scope = kHives[h].scope;
return true;
}
}
}
return false;
}
// Reads a REG_SZ string at |key| named |name| into |result|. Returns false if
// the string could not be read.
bool ReadRegistryString(RegKey* key,
const string16& name,
string16* result) {
DWORD value_size = 0;
DWORD key_type = 0;
scoped_array<uint8> buffer;
if (key->ReadValue(name.c_str(), 0, &value_size, &key_type) != ERROR_SUCCESS)
return false;
if (key_type != REG_SZ)
return false;
// According to the Microsoft documentation, the string
// buffer may not be explicitly 0-terminated. Allocate a
// slightly larger buffer and pre-fill to zeros to guarantee
// the 0-termination.
buffer.reset(new uint8[value_size + 2]);
memset(buffer.get(), 0, value_size + 2);
key->ReadValue(name.c_str(), buffer.get(), &value_size, NULL);
result->assign(reinterpret_cast<const wchar_t*>(buffer.get()));
return true;
}
// Reads a REG_DWORD integer at |key| named |name| into |result|. Returns false
// if the value could no be read.
bool ReadRegistryInteger(RegKey* key,
const string16& name,
uint32* result) {
DWORD dword;
if (key->ReadValueDW(name.c_str(), &dword) != ERROR_SUCCESS)
return false;
*result = dword;
return true;
}
// Returns the Value for a Chrome string policy named |name|, or NULL if
// it wasn't found. The caller owns the returned value.
base::Value* ReadChromeStringValue(const string16& name,
PolicyLevel* level,
PolicyScope* scope) {
RegKey key;
if (!LoadHighestPriorityKey(string16(), name, &key, level, scope))
return NULL;
string16 value;
if (!ReadRegistryString(&key, name, &value))
return NULL;
return base::Value::CreateStringValue(value);
}
// Returns the Value for a Chrome string list policy named |name|,
// or NULL if it wasn't found. The caller owns the returned value.
base::Value* ReadChromeStringListValue(const string16& name,
PolicyLevel* level,
PolicyScope* scope) {
RegKey key;
if (!LoadHighestPriorityKey(name, string16(), &key, level, scope))
return NULL;
base::ListValue* result = new base::ListValue();
string16 value;
int index = 0;
while (ReadRegistryString(&key, base::IntToString16(++index), &value))
result->Append(base::Value::CreateStringValue(value));
return result;
}
// Returns the Value for a Chrome boolean policy named |name|,
// or NULL if it wasn't found. The caller owns the returned value.
base::Value* ReadChromeBooleanValue(const string16& name,
PolicyLevel* level,
PolicyScope* scope) {
RegKey key;
if (!LoadHighestPriorityKey(string16(), name, &key, level, scope))
return NULL;
uint32 value;
if (!ReadRegistryInteger(&key, name, &value))
return NULL;
return base::Value::CreateBooleanValue(value != 0u);
}
// Returns the Value for a Chrome integer policy named |name|,
// or NULL if it wasn't found. The caller owns the returned value.
base::Value* ReadChromeIntegerValue(const string16& name,
PolicyLevel* level,
PolicyScope* scope) {
RegKey key;
if (!LoadHighestPriorityKey(string16(), name, &key, level, scope))
return NULL;
uint32 value;
if (!ReadRegistryInteger(&key, name, &value))
return NULL;
return base::Value::CreateIntegerValue(value);
}
// Returns the Value for a Chrome dictionary policy named |name|,
// or NULL if it wasn't found. The caller owns the returned value.
base::Value* ReadChromeDictionaryValue(const string16& name,
PolicyLevel* level,
PolicyScope* scope) {
// Dictionaries are encoded as JSON strings on Windows.
//
// A dictionary could be stored as a subkey, with each of its entries
// within that subkey. However, it would be impossible to recover the
// type for some of those entries:
// - Booleans are stored as DWORDS and are indistinguishable from
// integers;
// - Lists are stored as a subkey, with entries mapping 0 to N-1 to
// their value. This is indistinguishable from a Dictionary with
// integer keys.
//
// The GPO policy editor also has a limited data entry form that doesn't
// support dictionaries.
RegKey key;
if (!LoadHighestPriorityKey(string16(), name, &key, level, scope))
return NULL;
string16 value;
if (!ReadRegistryString(&key, name, &value))
return NULL;
return base::JSONReader::Read(UTF16ToUTF8(value));
}
// Returns the Value type described in |schema|, or |default_type| if not found.
base::Value::Type GetType(const base::DictionaryValue* schema,
base::Value::Type default_type) {
// JSON-schema types to base::Value::Type mapping.
static const struct {
// JSON schema type.
const char* schema_type;
// Correspondent value type.
base::Value::Type value_type;
} kSchemaToValueTypeMap[] = {
{ "array", base::Value::TYPE_LIST },
{ "boolean", base::Value::TYPE_BOOLEAN },
{ "integer", base::Value::TYPE_INTEGER },
{ "null", base::Value::TYPE_NULL },
{ "number", base::Value::TYPE_DOUBLE },
{ "object", base::Value::TYPE_DICTIONARY },
{ "string", base::Value::TYPE_STRING },
};
if (!schema)
return default_type;
std::string type;
if (!schema->GetString(kType, &type))
return default_type;
for (size_t i = 0; i < arraysize(kSchemaToValueTypeMap); ++i) {
if (type == kSchemaToValueTypeMap[i].schema_type)
return kSchemaToValueTypeMap[i].value_type;
}
return default_type;
}
// Returns the default type for registry entries of |reg_type|, when there is
// no schema defined type for a policy.
base::Value::Type GetDefaultFor(DWORD reg_type) {
return reg_type == REG_DWORD ? base::Value::TYPE_INTEGER :
base::Value::TYPE_STRING;
}
// Returns the entry with key |name| in |dictionary| (can be NULL), or NULL.
base::DictionaryValue* GetEntry(const base::DictionaryValue* dictionary,
const std::string& name) {
if (!dictionary)
return NULL;
base::DictionaryValue* entry = NULL;
dictionary->GetDictionary(name, &entry);
return entry;
}
// Returns the schema for property |name| given the |schema| of an object.
// Returns the "additionalProperties" schema if no specific schema for
// |name| is present. Returns NULL if no schema is found.
base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema,
const std::string& name) {
base::DictionaryValue* properties = GetEntry(schema, kProperties);
base::DictionaryValue* sub_schema = GetEntry(properties, name);
if (sub_schema)
return sub_schema;
// "additionalProperties" can be a boolean, but that case is ignored.
return GetEntry(schema, kAdditionalProperties);
}
// Converts string |value| to another |type|, if possible.
base::Value* ConvertComponentStringValue(const string16& value,
base::Value::Type type) {
switch (type) {
case base::Value::TYPE_NULL:
return base::Value::CreateNullValue();
case base::Value::TYPE_BOOLEAN: {
int int_value;
if (base::StringToInt(value, &int_value))
return base::Value::CreateBooleanValue(int_value != 0);
return NULL;
}
case base::Value::TYPE_INTEGER: {
int int_value;
if (base::StringToInt(value, &int_value))
return base::Value::CreateIntegerValue(int_value);
return NULL;
}
case base::Value::TYPE_DOUBLE: {
double double_value;
if (base::StringToDouble(UTF16ToUTF8(value), &double_value))
return base::Value::CreateDoubleValue(double_value);
DLOG(WARNING) << "Failed to read policy value as double: " << value;
return NULL;
}
case base::Value::TYPE_STRING:
return base::Value::CreateStringValue(value);
case base::Value::TYPE_DICTIONARY:
case base::Value::TYPE_LIST:
return base::JSONReader::Read(UTF16ToUTF8(value));
case base::Value::TYPE_BINARY:
DLOG(WARNING) << "Cannot convert REG_SZ entry to type " << type;
return NULL;
}
NOTREACHED();
return NULL;
}
// Converts an integer |value| to another |type|, if possible.
base::Value* ConvertComponentIntegerValue(uint32 value,
base::Value::Type type) {
switch (type) {
case base::Value::TYPE_BOOLEAN:
return base::Value::CreateBooleanValue(value != 0);
case base::Value::TYPE_INTEGER:
return base::Value::CreateIntegerValue(value);
case base::Value::TYPE_DOUBLE:
return base::Value::CreateDoubleValue(value);
case base::Value::TYPE_NULL:
case base::Value::TYPE_STRING:
case base::Value::TYPE_BINARY:
case base::Value::TYPE_DICTIONARY:
case base::Value::TYPE_LIST:
DLOG(WARNING) << "Cannot convert REG_DWORD entry to type " << type;
return NULL;
}
NOTREACHED();
return NULL;
}
// Reads a simple (non-Dictionary, non-Array) value from the registry |key|
// named |name| with registry type |reg_type| as a value of type |type|.
// Returns NULL if the value could not be loaded or converted.
base::Value* ReadComponentSimpleValue(RegKey* key,
const string16& name,
DWORD reg_type,
base::Value::Type type) {
switch (reg_type) {
case REG_SZ: {
string16 value;
if (ReadRegistryString(key, name, &value))
return ConvertComponentStringValue(value, type);
break;
}
case REG_DWORD: {
uint32 value;
if (ReadRegistryInteger(key, name, &value))
return ConvertComponentIntegerValue(value, type);
break;
}
default:
DLOG(WARNING) << "Registry type not supported for key " << name;
break;
}
NOTREACHED();
return NULL;
}
// Forward declaration for ReadComponentListValue().
base::DictionaryValue* ReadComponentDictionaryValue(
HKEY hive,
const string16& path,
const base::DictionaryValue* schema);
// Loads the list at |path| in the given |hive|. |schema| is a JSON schema
// (http://json-schema.org/) that describes the expected type of the list.
// Ownership of the result is transferred to the caller.
base::ListValue* ReadComponentListValue(HKEY hive,
const string16& path,
const base::DictionaryValue* schema) {
// The sub-elements are indexed from 1 to N. They can be represented as
// registry values or registry keys though; use |schema| first to try to
// determine the right type, and if that fails default to STRING.
RegKey key(hive, path.c_str(), KEY_READ);
if (!key.Valid())
return NULL;
// Get the schema for list items.
schema = GetEntry(schema, kItems);
base::Value::Type type = GetType(schema, base::Value::TYPE_STRING);
base::ListValue* list = new base::ListValue();
for (int i = 1; ; ++i) {
string16 name = base::IntToString16(i);
base::Value* value = NULL;
if (type == base::Value::TYPE_DICTIONARY) {
value =
ReadComponentDictionaryValue(hive, path + kPathSep + name, schema);
} else if (type == base::Value::TYPE_LIST) {
value = ReadComponentListValue(hive, path + kPathSep + name, schema);
} else {
DWORD reg_type;
key.ReadValue(name.c_str(), NULL, NULL, ®_type);
value = ReadComponentSimpleValue(&key, name, reg_type, type);
}
if (!value)
break;
list->Append(value);
}
return list;
}
// Loads the dictionary at |path| in the given |hive|. |schema| is a JSON
// schema (http://json-schema.org/) that describes the expected types for the
// dictionary entries. When the type for a certain entry isn't described in the
// schema, a default conversion takes place. |schema| can be NULL.
// Ownership of the result is transferred to the caller.
base::DictionaryValue* ReadComponentDictionaryValue(
HKEY hive,
const string16& path,
const base::DictionaryValue* schema) {
// A "value" in the registry is like a file in a filesystem, and a "key" is
// like a directory, that contains other "values" and "keys".
// Unfortunately it is possible to have a name both as a "value" and a "key".
// In those cases, the sub "key" will be ignored; this choice is arbitrary.
// First iterate over all the "values" in |path| and convert them; then
// recurse into each "key" in |path| and convert them as dictionaries.
RegKey key(hive, path.c_str(), KEY_READ);
if (!key.Valid())
return NULL;
base::DictionaryValue* dict = new base::DictionaryValue();
for (RegistryValueIterator it(hive, path.c_str()); it.Valid(); ++it) {
string16 name16(it.Name());
std::string name(UTF16ToUTF8(name16));
const base::DictionaryValue* sub_schema = GetSchemaFor(schema, name);
base::Value::Type type = GetType(sub_schema, GetDefaultFor(it.Type()));
base::Value* value =
ReadComponentSimpleValue(&key, name16, it.Type(), type);
if (value)
dict->Set(name, value);
}
for (RegistryKeyIterator it(hive, path.c_str()); it.Valid(); ++it) {
string16 name16(it.Name());
std::string name(UTF16ToUTF8(name16));
if (dict->HasKey(name)) {
DLOG(WARNING) << "Ignoring registry key because a value exists with the "
"same name: " << path << kPathSep << name;
continue;
}
base::DictionaryValue* sub_schema = GetSchemaFor(schema, name);
base::Value::Type type = GetType(sub_schema, base::Value::TYPE_DICTIONARY);
base::Value* value = NULL;
const string16 sub_path = path + kPathSep + name16;
if (type == base::Value::TYPE_DICTIONARY) {
value = ReadComponentDictionaryValue(hive, sub_path, sub_schema);
} else if (type == base::Value::TYPE_LIST) {
value = ReadComponentListValue(hive, sub_path, sub_schema);
} else {
DLOG(WARNING) << "Can't read a simple type in registry key at " << path;
}
if (value)
dict->Set(name, value);
}
return dict;
}
// Reads a JSON schema from the given |registry_value|, at the given
// |registry_key| in |hive|. |registry_value| must be a string (REG_SZ), and
// is decoded as JSON data. Returns NULL on failure. Ownership is transferred
// to the caller.
base::DictionaryValue* ReadRegistrySchema(HKEY hive,
const string16& registry_key,
const string16& registry_value) {
RegKey key(hive, registry_key.c_str(), KEY_READ);
string16 schema;
if (!ReadRegistryString(&key, registry_value, &schema))
return NULL;
// A JSON schema is represented in JSON too.
scoped_ptr<base::Value> value(base::JSONReader::Read(UTF16ToUTF8(schema)));
if (!value.get())
return NULL;
base::DictionaryValue* dict = NULL;
if (!value->GetAsDictionary(&dict))
return NULL;
// The top-level entry must be an object, and each of its properties maps
// a policy name to its schema.
if (GetType(dict, base::Value::TYPE_DICTIONARY) !=
base::Value::TYPE_DICTIONARY) {
DLOG(WARNING) << "schema top-level type isn't \"object\"";
return NULL;
}
value.release();
return dict;
}
} // namespace
PolicyLoaderWin::PolicyLoaderWin(const PolicyDefinitionList* policy_list)
: is_initialized_(false),
policy_list_(policy_list),
user_policy_changed_event_(false, false),
machine_policy_changed_event_(false, false),
user_policy_watcher_failed_(false),
machine_policy_watcher_failed_(false) {
if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) {
DPLOG(WARNING) << "Failed to register user group policy notification";
user_policy_watcher_failed_ = true;
}
if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) {
DPLOG(WARNING) << "Failed to register machine group policy notification.";
machine_policy_watcher_failed_ = true;
}
}
PolicyLoaderWin::~PolicyLoaderWin() {
user_policy_watcher_.StopWatching();
machine_policy_watcher_.StopWatching();
}
void PolicyLoaderWin::InitOnFile() {
is_initialized_ = true;
SetupWatches();
}
scoped_ptr<PolicyBundle> PolicyLoaderWin::Load() {
scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
LoadChromePolicy(&bundle->Get(POLICY_DOMAIN_CHROME, std::string()));
Load3rdPartyPolicies(bundle.get());
return bundle.Pass();
}
void PolicyLoaderWin::LoadChromePolicy(PolicyMap* chrome_policies) {
// Reset the watches BEFORE reading the individual policies to avoid
// missing a change notification.
if (is_initialized_)
SetupWatches();
const PolicyDefinitionList::Entry* current;
for (current = policy_list_->begin; current != policy_list_->end; ++current) {
const string16 name(ASCIIToUTF16(current->name));
PolicyLevel level = POLICY_LEVEL_MANDATORY;
PolicyScope scope = POLICY_SCOPE_MACHINE;
base::Value* value = NULL;
switch (current->value_type) {
case base::Value::TYPE_STRING:
value = ReadChromeStringValue(name, &level, &scope);
break;
case base::Value::TYPE_LIST:
value = ReadChromeStringListValue(name, &level, &scope);
break;
case base::Value::TYPE_BOOLEAN:
value = ReadChromeBooleanValue(name, &level, &scope);
break;
case base::Value::TYPE_INTEGER:
value = ReadChromeIntegerValue(name, &level, &scope);
break;
case base::Value::TYPE_DICTIONARY:
value = ReadChromeDictionaryValue(name, &level, &scope);
break;
default:
NOTREACHED();
}
if (value)
chrome_policies->Set(current->name, level, scope, value);
}
}
void PolicyLoaderWin::Load3rdPartyPolicies(PolicyBundle* bundle) {
// Each 3rd party namespace can have policies on both HKLM and HKCU. They
// should be merged, giving priority to HKLM for policies with the same name.
// Map of known domain name to their enum values.
static const struct {
const char* name;
PolicyDomain domain;
} kDomains[] = {
{ "extensions", POLICY_DOMAIN_EXTENSIONS },
};
// Map of policy paths to their corresponding policy level, in decreasing
// order of priority.
static const struct {
const char* path;
PolicyLevel level;
} kKeyPaths[] = {
{ "policy", POLICY_LEVEL_MANDATORY },
{ "recommended", POLICY_LEVEL_RECOMMENDED },
};
// Path where policies for components are stored.
const string16 kPathPrefix = string16(kRegistryMandatorySubKey) + kPathSep +
kThirdParty + kPathSep;
for (size_t h = 0; h < arraysize(kHives); ++h) {
HKEY hkey = kHives[h].hive;
for (size_t d = 0; d < arraysize(kDomains); ++d) {
// Each subkey under this domain is a component of that domain.
// |domain_path| == SOFTWARE\Policies\Chromium\3rdparty\<domain>
string16 domain_path = kPathPrefix + ASCIIToUTF16(kDomains[d].name);
for (RegistryKeyIterator domain_iterator(hkey, domain_path.c_str());
domain_iterator.Valid(); ++domain_iterator) {
string16 component(domain_iterator.Name());
string16 component_path = domain_path + kPathSep + component;
// Load the schema for this component's policy, if present.
scoped_ptr<base::DictionaryValue> schema(
ReadRegistrySchema(hkey, component_path, kSchema));
for (size_t k = 0; k < arraysize(kKeyPaths); ++k) {
string16 path =
component_path + kPathSep + ASCIIToUTF16(kKeyPaths[k].path);
scoped_ptr<base::DictionaryValue> dictionary(
ReadComponentDictionaryValue(hkey, path, schema.get()));
if (dictionary.get()) {
PolicyMap policies;
policies.LoadFrom(
dictionary.get(), kKeyPaths[k].level, kHives[h].scope);
// LoadFrom() overwrites any existing values. Use a temporary map
// and then use MergeFrom(), that only overwrites values with lower
// priority.
bundle->Get(kDomains[d].domain, UTF16ToUTF8(component))
.MergeFrom(policies);
}
}
}
}
}
}
void PolicyLoaderWin::SetupWatches() {
DCHECK(is_initialized_);
if (!user_policy_watcher_failed_ &&
!user_policy_watcher_.GetWatchedObject() &&
!user_policy_watcher_.StartWatching(
user_policy_changed_event_.handle(), this)) {
DLOG(WARNING) << "Failed to start watch for user policy change event";
user_policy_watcher_failed_ = true;
}
if (!machine_policy_watcher_failed_ &&
!machine_policy_watcher_.GetWatchedObject() &&
!machine_policy_watcher_.StartWatching(
machine_policy_changed_event_.handle(), this)) {
DLOG(WARNING) << "Failed to start watch for machine policy change event";
machine_policy_watcher_failed_ = true;
}
}
void PolicyLoaderWin::OnObjectSignaled(HANDLE object) {
DCHECK(object == user_policy_changed_event_.handle() ||
object == machine_policy_changed_event_.handle())
<< "unexpected object signaled policy reload, obj = "
<< std::showbase << std::hex << object;
Reload(false);
}
} // namespace policy
|