OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/configuration_policy_provider_delegate_win.h" | 5 #include "chrome/browser/policy/configuration_policy_provider_delegate_win.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "base/win/registry.h" | 9 #include "base/win/registry.h" |
10 #include "chrome/common/policy_constants.h" | 10 #include "chrome/common/policy_constants.h" |
11 | 11 |
12 using base::win::RegKey; | 12 using base::win::RegKey; |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 bool ReadRegistryStringValue(RegKey* key, const string16& name, | 16 bool ReadRegistryStringValue(RegKey* key, const string16& name, |
17 string16* result) { | 17 string16* result) { |
18 DWORD value_size = 0; | 18 DWORD value_size = 0; |
19 DWORD key_type = 0; | 19 DWORD key_type = 0; |
20 scoped_array<uint8> buffer; | 20 scoped_array<uint8> buffer; |
21 | 21 |
22 if (!key->ReadValue(name.c_str(), 0, &value_size, &key_type)) | 22 if (key->ReadValue(name.c_str(), 0, &value_size, &key_type) != ERROR_SUCCESS) |
23 return false; | 23 return false; |
24 if (key_type != REG_SZ) | 24 if (key_type != REG_SZ) |
25 return false; | 25 return false; |
26 | 26 |
27 // According to the Microsoft documentation, the string | 27 // According to the Microsoft documentation, the string |
28 // buffer may not be explicitly 0-terminated. Allocate a | 28 // buffer may not be explicitly 0-terminated. Allocate a |
29 // slightly larger buffer and pre-fill to zeros to guarantee | 29 // slightly larger buffer and pre-fill to zeros to guarantee |
30 // the 0-termination. | 30 // the 0-termination. |
31 buffer.reset(new uint8[value_size + 2]); | 31 buffer.reset(new uint8[value_size + 2]); |
32 memset(buffer.get(), 0, value_size + 2); | 32 memset(buffer.get(), 0, value_size + 2); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 } | 82 } |
83 default: | 83 default: |
84 NOTREACHED(); | 84 NOTREACHED(); |
85 } | 85 } |
86 } | 86 } |
87 return result; | 87 return result; |
88 } | 88 } |
89 | 89 |
90 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyString( | 90 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyString( |
91 const string16& name, string16* result) const { | 91 const string16& name, string16* result) const { |
92 string16 path = string16(kRegistrySubKey); | 92 RegKey policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); |
93 RegKey policy_key; | |
94 // First try the global policy. | 93 // First try the global policy. |
95 if (policy_key.Open(HKEY_LOCAL_MACHINE, path.c_str(), KEY_READ)) { | 94 if (ReadRegistryStringValue(&policy_key, name, result)) |
96 if (ReadRegistryStringValue(&policy_key, name, result)) | 95 return true; |
97 return true; | 96 |
98 policy_key.Close(); | |
99 } | |
100 // Fall back on user-specific policy. | 97 // Fall back on user-specific policy. |
101 if (!policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ)) | 98 if (policy_key.Open(HKEY_CURRENT_USER, kRegistrySubKey, |
| 99 KEY_READ) != ERROR_SUCCESS) |
102 return false; | 100 return false; |
103 return ReadRegistryStringValue(&policy_key, name, result); | 101 return ReadRegistryStringValue(&policy_key, name, result); |
104 } | 102 } |
105 | 103 |
106 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyStringList( | 104 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyStringList( |
107 const string16& key, ListValue* result) const { | 105 const string16& key, ListValue* result) const { |
108 string16 path = string16(kRegistrySubKey); | 106 string16 path = string16(kRegistrySubKey); |
109 path += ASCIIToUTF16("\\") + key; | 107 path += ASCIIToUTF16("\\") + key; |
110 RegKey policy_key; | 108 RegKey policy_key; |
111 if (!policy_key.Open(HKEY_LOCAL_MACHINE, path.c_str(), KEY_READ)) { | 109 LONG ret = policy_key.Open(HKEY_LOCAL_MACHINE, path.c_str(), KEY_READ); |
112 policy_key.Close(); | 110 if (ret != ERROR_SUCCESS) { |
113 // Fall back on user-specific policy. | 111 // Fall back on user-specific policy. |
114 if (!policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ)) | 112 ret = policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ); |
| 113 if (ret != ERROR_SUCCESS) |
115 return false; | 114 return false; |
116 } | 115 } |
117 string16 policy_string; | 116 string16 policy_string; |
118 int index = 0; | 117 int index = 0; |
119 while (ReadRegistryStringValue(&policy_key, base::IntToString16(++index), | 118 while (ReadRegistryStringValue(&policy_key, base::IntToString16(++index), |
120 &policy_string)) { | 119 &policy_string)) { |
121 result->Append(Value::CreateStringValue(policy_string)); | 120 result->Append(Value::CreateStringValue(policy_string)); |
122 } | 121 } |
123 return true; | 122 return true; |
124 } | 123 } |
125 | 124 |
126 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyBoolean( | 125 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyBoolean( |
127 const string16& value_name, bool* result) const { | 126 const string16& value_name, bool* result) const { |
128 DWORD value; | 127 uint32 local_result = 0; |
129 RegKey hkcu_policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); | 128 bool ret = GetRegistryPolicyInteger(value_name, &local_result); |
130 if (hkcu_policy_key.ReadValueDW(value_name.c_str(), &value)) { | 129 *result = local_result != 0; |
131 *result = value != 0; | 130 return ret; |
132 return true; | |
133 } | |
134 | |
135 RegKey hklm_policy_key(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ); | |
136 if (hklm_policy_key.ReadValueDW(value_name.c_str(), &value)) { | |
137 *result = value != 0; | |
138 return true; | |
139 } | |
140 return false; | |
141 } | 131 } |
142 | 132 |
143 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyInteger( | 133 bool ConfigurationPolicyProviderDelegateWin::GetRegistryPolicyInteger( |
144 const string16& value_name, uint32* result) const { | 134 const string16& value_name, uint32* result) const { |
145 DWORD value; | 135 DWORD value = 0; |
146 RegKey hkcu_policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); | 136 RegKey policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); |
147 if (hkcu_policy_key.ReadValueDW(value_name.c_str(), &value)) { | 137 if (policy_key.ReadValueDW(value_name.c_str(), &value) == ERROR_SUCCESS) { |
148 *result = value; | 138 *result = value; |
149 return true; | 139 return true; |
150 } | 140 } |
151 | 141 |
152 RegKey hklm_policy_key(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ); | 142 LONG ret = policy_key.Open(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ); |
153 if (hklm_policy_key.ReadValueDW(value_name.c_str(), &value)) { | 143 if (ret == ERROR_SUCCESS) { |
154 *result = value; | 144 if (policy_key.ReadValueDW(value_name.c_str(), &value) == ERROR_SUCCESS) { |
155 return true; | 145 *result = value; |
| 146 return true; |
| 147 } |
156 } | 148 } |
157 return false; | 149 return false; |
158 } | 150 } |
159 | 151 |
160 } // namespace policy | 152 } // namespace policy |
OLD | NEW |