| 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_win.h" | 5 #include "chrome/browser/policy/configuration_policy_provider_win.h" |
| 6 | 6 |
| 7 #include <userenv.h> | 7 #include <userenv.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/object_watcher.h" | 12 #include "base/object_watcher.h" |
| 13 #include "base/registry.h" | |
| 14 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
| 15 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 16 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
| 17 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 18 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
| 19 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 20 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "base/win/registry.h" |
| 21 #include "chrome/common/policy_constants.h" | 21 #include "chrome/common/policy_constants.h" |
| 22 | 22 |
| 23 using base::win::RegKey; |
| 24 |
| 23 namespace policy { | 25 namespace policy { |
| 24 | 26 |
| 27 namespace { |
| 28 |
| 29 bool ReadRegistryStringValue(RegKey* key, const string16& name, |
| 30 string16* result) { |
| 31 DWORD value_size = 0; |
| 32 DWORD key_type = 0; |
| 33 scoped_array<uint8> buffer; |
| 34 |
| 35 if (!key->ReadValue(name.c_str(), 0, &value_size, &key_type)) |
| 36 return false; |
| 37 if (key_type != REG_SZ) |
| 38 return false; |
| 39 |
| 40 // According to the Microsoft documentation, the string |
| 41 // buffer may not be explicitly 0-terminated. Allocate a |
| 42 // slightly larger buffer and pre-fill to zeros to guarantee |
| 43 // the 0-termination. |
| 44 buffer.reset(new uint8[value_size + 2]); |
| 45 memset(buffer.get(), 0, value_size + 2); |
| 46 key->ReadValue(name.c_str(), buffer.get(), &value_size, NULL); |
| 47 result->assign(reinterpret_cast<const wchar_t*>(buffer.get())); |
| 48 return true; |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 25 // Period at which to run the reload task in case the group policy change | 53 // Period at which to run the reload task in case the group policy change |
| 26 // watchers fail. | 54 // watchers fail. |
| 27 const int kReloadIntervalMinutes = 15; | 55 const int kReloadIntervalMinutes = 15; |
| 28 | 56 |
| 29 ConfigurationPolicyProviderWin::GroupPolicyChangeWatcher:: | 57 ConfigurationPolicyProviderWin::GroupPolicyChangeWatcher:: |
| 30 GroupPolicyChangeWatcher( | 58 GroupPolicyChangeWatcher( |
| 31 base::WeakPtr<ConfigurationPolicyProviderWin> provider, | 59 base::WeakPtr<ConfigurationPolicyProviderWin> provider, |
| 32 int reload_interval_minutes) | 60 int reload_interval_minutes) |
| 33 : provider_(provider), | 61 : provider_(provider), |
| 34 user_policy_changed_event_(false, false), | 62 user_policy_changed_event_(false, false), |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (ReadRegistryStringValue(&policy_key, name, result)) | 176 if (ReadRegistryStringValue(&policy_key, name, result)) |
| 149 return true; | 177 return true; |
| 150 policy_key.Close(); | 178 policy_key.Close(); |
| 151 } | 179 } |
| 152 // Fall back on user-specific policy. | 180 // Fall back on user-specific policy. |
| 153 if (!policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ)) | 181 if (!policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ)) |
| 154 return false; | 182 return false; |
| 155 return ReadRegistryStringValue(&policy_key, name, result); | 183 return ReadRegistryStringValue(&policy_key, name, result); |
| 156 } | 184 } |
| 157 | 185 |
| 158 bool ConfigurationPolicyProviderWin::ReadRegistryStringValue( | |
| 159 RegKey* key, const string16& name, string16* result) { | |
| 160 DWORD value_size = 0; | |
| 161 DWORD key_type = 0; | |
| 162 scoped_array<uint8> buffer; | |
| 163 | |
| 164 if (!key->ReadValue(name.c_str(), 0, &value_size, &key_type)) | |
| 165 return false; | |
| 166 if (key_type != REG_SZ) | |
| 167 return false; | |
| 168 | |
| 169 // According to the Microsoft documentation, the string | |
| 170 // buffer may not be explicitly 0-terminated. Allocate a | |
| 171 // slightly larger buffer and pre-fill to zeros to guarantee | |
| 172 // the 0-termination. | |
| 173 buffer.reset(new uint8[value_size + 2]); | |
| 174 memset(buffer.get(), 0, value_size + 2); | |
| 175 key->ReadValue(name.c_str(), buffer.get(), &value_size, NULL); | |
| 176 result->assign(reinterpret_cast<const wchar_t*>(buffer.get())); | |
| 177 return true; | |
| 178 } | |
| 179 | |
| 180 bool ConfigurationPolicyProviderWin::GetRegistryPolicyStringList( | 186 bool ConfigurationPolicyProviderWin::GetRegistryPolicyStringList( |
| 181 const string16& key, ListValue* result) { | 187 const string16& key, ListValue* result) { |
| 182 string16 path = string16(kRegistrySubKey); | 188 string16 path = string16(kRegistrySubKey); |
| 183 path += ASCIIToUTF16("\\") + key; | 189 path += ASCIIToUTF16("\\") + key; |
| 184 RegKey policy_key; | 190 RegKey policy_key; |
| 185 if (!policy_key.Open(HKEY_LOCAL_MACHINE, path.c_str(), KEY_READ)) { | 191 if (!policy_key.Open(HKEY_LOCAL_MACHINE, path.c_str(), KEY_READ)) { |
| 186 policy_key.Close(); | 192 policy_key.Close(); |
| 187 // Fall back on user-specific policy. | 193 // Fall back on user-specific policy. |
| 188 if (!policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ)) | 194 if (!policy_key.Open(HKEY_CURRENT_USER, path.c_str(), KEY_READ)) |
| 189 return false; | 195 return false; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 default: | 277 default: |
| 272 NOTREACHED(); | 278 NOTREACHED(); |
| 273 return false; | 279 return false; |
| 274 } | 280 } |
| 275 } | 281 } |
| 276 | 282 |
| 277 return true; | 283 return true; |
| 278 } | 284 } |
| 279 | 285 |
| 280 } // namespace policy | 286 } // namespace policy |
| OLD | NEW |