| 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 "base/win/registry.h" | 5 #include "base/win/registry.h" |
| 6 | 6 |
| 7 #include <shlwapi.h> | 7 #include <shlwapi.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/thread_restrictions.h" | 10 #include "base/thread_restrictions.h" |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 | 237 |
| 238 HKEY subkey = NULL; | 238 HKEY subkey = NULL; |
| 239 LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey); | 239 LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey); |
| 240 | 240 |
| 241 Close(); | 241 Close(); |
| 242 | 242 |
| 243 key_ = subkey; | 243 key_ = subkey; |
| 244 return (result == ERROR_SUCCESS); | 244 return (result == ERROR_SUCCESS); |
| 245 } | 245 } |
| 246 | 246 |
| 247 DWORD RegKey::ValueCount() { | 247 DWORD RegKey::ValueCount() const { |
| 248 base::ThreadRestrictions::AssertIOAllowed(); | 248 base::ThreadRestrictions::AssertIOAllowed(); |
| 249 DWORD count = 0; | 249 DWORD count = 0; |
| 250 HRESULT result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, | 250 HRESULT result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, |
| 251 NULL, &count, NULL, NULL, NULL, NULL); | 251 NULL, &count, NULL, NULL, NULL, NULL); |
| 252 return (result != ERROR_SUCCESS) ? 0 : count; | 252 return (result != ERROR_SUCCESS) ? 0 : count; |
| 253 } | 253 } |
| 254 | 254 |
| 255 bool RegKey::ReadName(int index, std::wstring* name) { | 255 bool RegKey::ReadName(int index, std::wstring* name) const { |
| 256 base::ThreadRestrictions::AssertIOAllowed(); | 256 base::ThreadRestrictions::AssertIOAllowed(); |
| 257 wchar_t buf[256]; | 257 wchar_t buf[256]; |
| 258 DWORD bufsize = arraysize(buf); | 258 DWORD bufsize = arraysize(buf); |
| 259 LRESULT r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, | 259 LRESULT r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, |
| 260 NULL, NULL); | 260 NULL, NULL); |
| 261 if (r != ERROR_SUCCESS) | 261 if (r != ERROR_SUCCESS) |
| 262 return false; | 262 return false; |
| 263 if (name) | 263 if (name) |
| 264 *name = buf; | 264 *name = buf; |
| 265 return true; | 265 return true; |
| 266 } | 266 } |
| 267 | 267 |
| 268 bool RegKey::ValueExists(const wchar_t* name) { | 268 bool RegKey::ValueExists(const wchar_t* name) { |
| 269 base::ThreadRestrictions::AssertIOAllowed(); | 269 base::ThreadRestrictions::AssertIOAllowed(); |
| 270 if (!key_) | 270 if (!key_) |
| 271 return false; | 271 return false; |
| 272 HRESULT result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL); | 272 HRESULT result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL); |
| 273 return (result == ERROR_SUCCESS); | 273 return (result == ERROR_SUCCESS); |
| 274 } | 274 } |
| 275 | 275 |
| 276 bool RegKey::ReadValue(const wchar_t* name, void* data, | 276 bool RegKey::ReadValue(const wchar_t* name, void* data, |
| 277 DWORD* dsize, DWORD* dtype) { | 277 DWORD* dsize, DWORD* dtype) const { |
| 278 base::ThreadRestrictions::AssertIOAllowed(); | 278 base::ThreadRestrictions::AssertIOAllowed(); |
| 279 if (!key_) | 279 if (!key_) |
| 280 return false; | 280 return false; |
| 281 HRESULT result = RegQueryValueEx(key_, name, 0, dtype, | 281 HRESULT result = RegQueryValueEx(key_, name, 0, dtype, |
| 282 reinterpret_cast<LPBYTE>(data), dsize); | 282 reinterpret_cast<LPBYTE>(data), dsize); |
| 283 return (result == ERROR_SUCCESS); | 283 return (result == ERROR_SUCCESS); |
| 284 } | 284 } |
| 285 | 285 |
| 286 bool RegKey::ReadValue(const wchar_t* name, std::wstring* value) { | 286 bool RegKey::ReadValue(const wchar_t* name, std::wstring* value) const { |
| 287 base::ThreadRestrictions::AssertIOAllowed(); | 287 base::ThreadRestrictions::AssertIOAllowed(); |
| 288 DCHECK(value); | 288 DCHECK(value); |
| 289 const size_t kMaxStringLength = 1024; // This is after expansion. | 289 const size_t kMaxStringLength = 1024; // This is after expansion. |
| 290 // Use the one of the other forms of ReadValue if 1024 is too small for you. | 290 // Use the one of the other forms of ReadValue if 1024 is too small for you. |
| 291 wchar_t raw_value[kMaxStringLength]; | 291 wchar_t raw_value[kMaxStringLength]; |
| 292 DWORD type = REG_SZ, size = sizeof(raw_value); | 292 DWORD type = REG_SZ, size = sizeof(raw_value); |
| 293 if (ReadValue(name, raw_value, &size, &type)) { | 293 if (ReadValue(name, raw_value, &size, &type)) { |
| 294 if (type == REG_SZ) { | 294 if (type == REG_SZ) { |
| 295 *value = raw_value; | 295 *value = raw_value; |
| 296 } else if (type == REG_EXPAND_SZ) { | 296 } else if (type == REG_EXPAND_SZ) { |
| 297 wchar_t expanded[kMaxStringLength]; | 297 wchar_t expanded[kMaxStringLength]; |
| 298 size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength); | 298 size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength); |
| 299 // Success: returns the number of wchar_t's copied | 299 // Success: returns the number of wchar_t's copied |
| 300 // Fail: buffer too small, returns the size required | 300 // Fail: buffer too small, returns the size required |
| 301 // Fail: other, returns 0 | 301 // Fail: other, returns 0 |
| 302 if (size == 0 || size > kMaxStringLength) | 302 if (size == 0 || size > kMaxStringLength) |
| 303 return false; | 303 return false; |
| 304 *value = expanded; | 304 *value = expanded; |
| 305 } else { | 305 } else { |
| 306 // Not a string. Oops. | 306 // Not a string. Oops. |
| 307 return false; | 307 return false; |
| 308 } | 308 } |
| 309 return true; | 309 return true; |
| 310 } | 310 } |
| 311 | 311 |
| 312 return false; | 312 return false; |
| 313 } | 313 } |
| 314 | 314 |
| 315 bool RegKey::ReadValueDW(const wchar_t* name, DWORD* value) { | 315 bool RegKey::ReadValueDW(const wchar_t* name, DWORD* value) const { |
| 316 DCHECK(value); | 316 DCHECK(value); |
| 317 DWORD type = REG_DWORD; | 317 DWORD type = REG_DWORD; |
| 318 DWORD size = sizeof(DWORD); | 318 DWORD size = sizeof(DWORD); |
| 319 DWORD result = 0; | 319 DWORD result = 0; |
| 320 if (ReadValue(name, &result, &size, &type) && | 320 if (ReadValue(name, &result, &size, &type) && |
| 321 (type == REG_DWORD || type == REG_BINARY) && | 321 (type == REG_DWORD || type == REG_BINARY) && |
| 322 size == sizeof(DWORD)) { | 322 size == sizeof(DWORD)) { |
| 323 *value = result; | 323 *value = result; |
| 324 return true; | 324 return true; |
| 325 } | 325 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) { | 407 if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) { |
| 408 StartWatching(); | 408 StartWatching(); |
| 409 return true; | 409 return true; |
| 410 } | 410 } |
| 411 } | 411 } |
| 412 return false; | 412 return false; |
| 413 } | 413 } |
| 414 | 414 |
| 415 } // namespace win | 415 } // namespace win |
| 416 } // namespace base | 416 } // namespace base |
| OLD | NEW |