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

Side by Side Diff: chrome/browser/configuration_policy_provider_win.cc

Issue 2119005: [Win] Implement core mechanism to honor Windows Group Policy (Closed)
Patch Set: merge with latest Created 10 years, 7 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
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/configuration_policy_provider_win.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/registry.h"
11 #include "base/scoped_ptr.h"
12 #include "base/sys_string_conversions.h"
13 #include "base/values.h"
14
15 const wchar_t WinConfigurationPolicyProvider::kHomepageRegistryValueName[] =
16 L"Homepage";
17 const wchar_t WinConfigurationPolicyProvider::
18 kHomepageIsNewTabPageRegistryValueName[] = L"HomepageIsNewTabPage";
19 const wchar_t WinConfigurationPolicyProvider::kCookiesModeRegistryValueName[] =
20 L"CookiesMode";
21
22 #if defined(GOOGLE_CHROME_BUILD)
23 const wchar_t WinConfigurationPolicyProvider::kPolicyRegistrySubKey[] =
24 L"SOFTWARE\\Policies\\Google\\Google Chrome";
25 #else
26 const wchar_t WinConfigurationPolicyProvider::kPolicyRegistrySubKey[] =
27 L"SOFTWARE\\Policies\\Chromium";
28 #endif
29
30 WinConfigurationPolicyProvider::WinConfigurationPolicyProvider() {
31 }
32
33 bool WinConfigurationPolicyProvider::GetRegistryPolicyString(
34 const wchar_t* value_name, string16* result) {
35 DWORD value_size = 0;
36 DWORD key_type = 0;
37 scoped_array<uint8> buffer;
38 RegKey hkcu_policy_key(HKEY_LOCAL_MACHINE, kPolicyRegistrySubKey);
39 if (hkcu_policy_key.ReadValue(value_name, 0, &value_size, &key_type)) {
40 if (key_type != REG_SZ)
41 return false;
42 // According to the Microsoft documentation, the string
43 // buffer may not be explicitly 0-terminated. Allocate a
44 // slightly larger buffer and prefill to zeros to guarantee
45 // the 0-termination.
46 buffer.reset(new uint8[value_size + 2]);
47 memset(buffer.get(), 0, value_size + 2);
48 hkcu_policy_key.ReadValue(value_name, buffer.get(), &value_size);
49 } else {
50 RegKey hklm_policy_key(HKEY_CURRENT_USER, kPolicyRegistrySubKey);
51 if (hklm_policy_key.ReadValue(value_name, 0, &value_size, &key_type)) {
52 if (key_type != REG_SZ)
53 return false;
54 // According to the Microsoft documentation, the string
55 // buffer may not be explicitly 0-terminated. Allocate a
56 // slightly larger buffer and prefill to zeros to guarantee
57 // the 0-termination.
58 buffer.reset(new uint8[value_size + 2]);
59 memset(buffer.get(), 0, value_size + 2);
60 hklm_policy_key.ReadValue(value_name, buffer.get(), &value_size);
61 } else {
62 return false;
63 }
64 }
65
66 result->assign(reinterpret_cast<const wchar_t*>(buffer.get()));
67 return true;
68 }
69
70 bool WinConfigurationPolicyProvider::GetRegistryPolicyBoolean(
71 const wchar_t* value_name, bool* result) {
72 DWORD value;
73 RegKey hkcu_policy_key(HKEY_LOCAL_MACHINE, kPolicyRegistrySubKey);
74 if (hkcu_policy_key.ReadValueDW(value_name, &value)) {
75 *result = value != 0;
76 return true;
77 }
78
79 RegKey hklm_policy_key(HKEY_CURRENT_USER, kPolicyRegistrySubKey);
80 if (hklm_policy_key.ReadValueDW(value_name, &value)) {
81 *result = value != 0;
82 return true;
83 }
84 return false;
85 }
86
87 bool WinConfigurationPolicyProvider::GetRegistryPolicyInteger(
88 const wchar_t* value_name, uint32* result) {
89 DWORD value;
90 RegKey hkcu_policy_key(HKEY_LOCAL_MACHINE, kPolicyRegistrySubKey);
91 if (hkcu_policy_key.ReadValueDW(value_name, &value)) {
92 *result = value;
93 return true;
94 }
95
96 RegKey hklm_policy_key(HKEY_CURRENT_USER, kPolicyRegistrySubKey);
97 if (hklm_policy_key.ReadValueDW(value_name, &value)) {
98 *result = value;
99 return true;
100 }
101 return false;
102 }
103
104 const WinConfigurationPolicyProvider::RegistryPolicyMapEntry
105 WinConfigurationPolicyProvider::registry_to_policy_map_[] = {
106 { Value::TYPE_STRING,
107 ConfigurationPolicyStore::kPolicyHomePage,
108 kHomepageRegistryValueName },
109 { Value::TYPE_BOOLEAN,
110 ConfigurationPolicyStore::kPolicyHomepageIsNewTabPage,
111 kHomepageIsNewTabPageRegistryValueName },
112 { Value::TYPE_INTEGER,
113 ConfigurationPolicyStore::kPolicyCookiesMode,
114 kCookiesModeRegistryValueName },
115 };
116
117 bool WinConfigurationPolicyProvider::Provide(
118 ConfigurationPolicyStore* store) {
119 const RegistryPolicyMapEntry* current;
120 const RegistryPolicyMapEntry* end = registry_to_policy_map_ +
121 arraysize(registry_to_policy_map_);
122
123 for (current = registry_to_policy_map_; current != end; ++current) {
124 std::wstring string_value;
125 uint32 int_value;
126 bool bool_value;
127 switch (current->value_type) {
128 case Value::TYPE_STRING:
129 if (GetRegistryPolicyString(current->registry_value_name,
130 &string_value)) {
131 store->Apply(
132 current->policy_type,
133 Value::CreateStringValueFromUTF16(string_value));
134 }
135 break;
136 case Value::TYPE_BOOLEAN:
137 if (GetRegistryPolicyBoolean(current->registry_value_name,
138 &bool_value)) {
139 store->Apply(current->policy_type,
140 Value::CreateBooleanValue(bool_value));
141 }
142 break;
143 case Value::TYPE_INTEGER:
144 if (GetRegistryPolicyInteger(current->registry_value_name,
145 &int_value)) {
146 store->Apply(current->policy_type,
147 Value::CreateIntegerValue(int_value));
148 }
149 break;
150 default:
151 NOTREACHED();
152 return false;
153 }
154 }
155
156 return true;
157 }
158
OLDNEW
« no previous file with comments | « chrome/browser/configuration_policy_provider_win.h ('k') | chrome/browser/configuration_policy_provider_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698