michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/win/registry.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/strings/string_util.h" michael@0: #include "base/threading/thread_restrictions.h" michael@0: michael@0: #pragma comment(lib, "shlwapi.lib") // for SHDeleteKey michael@0: michael@0: namespace base { michael@0: namespace win { michael@0: michael@0: namespace { michael@0: michael@0: // RegEnumValue() reports the number of characters from the name that were michael@0: // written to the buffer, not how many there are. This constant is the maximum michael@0: // name size, such that a buffer with this size should read any name. michael@0: const DWORD MAX_REGISTRY_NAME_SIZE = 16384; michael@0: michael@0: // Registry values are read as BYTE* but can have wchar_t* data whose last michael@0: // wchar_t is truncated. This function converts the reported |byte_size| to michael@0: // a size in wchar_t that can store a truncated wchar_t if necessary. michael@0: inline DWORD to_wchar_size(DWORD byte_size) { michael@0: return (byte_size + sizeof(wchar_t) - 1) / sizeof(wchar_t); michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: // RegKey ---------------------------------------------------------------------- michael@0: michael@0: RegKey::RegKey() michael@0: : key_(NULL), michael@0: watch_event_(0) { michael@0: } michael@0: michael@0: RegKey::RegKey(HKEY key) michael@0: : key_(key), michael@0: watch_event_(0) { michael@0: } michael@0: michael@0: RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access) michael@0: : key_(NULL), michael@0: watch_event_(0) { michael@0: if (rootkey) { michael@0: if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) michael@0: Create(rootkey, subkey, access); michael@0: else michael@0: Open(rootkey, subkey, access); michael@0: } else { michael@0: DCHECK(!subkey); michael@0: } michael@0: } michael@0: michael@0: RegKey::~RegKey() { michael@0: Close(); michael@0: } michael@0: michael@0: LONG RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) { michael@0: DWORD disposition_value; michael@0: return CreateWithDisposition(rootkey, subkey, &disposition_value, access); michael@0: } michael@0: michael@0: LONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, michael@0: DWORD* disposition, REGSAM access) { michael@0: DCHECK(rootkey && subkey && access && disposition); michael@0: Close(); michael@0: michael@0: LONG result = RegCreateKeyEx(rootkey, subkey, 0, NULL, michael@0: REG_OPTION_NON_VOLATILE, access, NULL, &key_, michael@0: disposition); michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) { michael@0: DCHECK(name && access); michael@0: HKEY subkey = NULL; michael@0: LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, michael@0: access, NULL, &subkey, NULL); michael@0: Close(); michael@0: michael@0: key_ = subkey; michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { michael@0: DCHECK(rootkey && subkey && access); michael@0: Close(); michael@0: michael@0: LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_); michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) { michael@0: DCHECK(relative_key_name && access); michael@0: HKEY subkey = NULL; michael@0: LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey); michael@0: michael@0: // We have to close the current opened key before replacing it with the new michael@0: // one. michael@0: Close(); michael@0: michael@0: key_ = subkey; michael@0: return result; michael@0: } michael@0: michael@0: void RegKey::Close() { michael@0: StopWatching(); michael@0: if (key_) { michael@0: ::RegCloseKey(key_); michael@0: key_ = NULL; michael@0: } michael@0: } michael@0: michael@0: void RegKey::Set(HKEY key) { michael@0: if (key_ != key) { michael@0: Close(); michael@0: key_ = key; michael@0: } michael@0: } michael@0: michael@0: HKEY RegKey::Take() { michael@0: StopWatching(); michael@0: HKEY key = key_; michael@0: key_ = NULL; michael@0: return key; michael@0: } michael@0: michael@0: bool RegKey::HasValue(const wchar_t* name) const { michael@0: return RegQueryValueEx(key_, name, 0, NULL, NULL, NULL) == ERROR_SUCCESS; michael@0: } michael@0: michael@0: DWORD RegKey::GetValueCount() const { michael@0: DWORD count = 0; michael@0: LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, michael@0: NULL, NULL, NULL, NULL); michael@0: return (result == ERROR_SUCCESS) ? count : 0; michael@0: } michael@0: michael@0: LONG RegKey::GetValueNameAt(int index, std::wstring* name) const { michael@0: wchar_t buf[256]; michael@0: DWORD bufsize = arraysize(buf); michael@0: LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL); michael@0: if (r == ERROR_SUCCESS) michael@0: *name = buf; michael@0: michael@0: return r; michael@0: } michael@0: michael@0: LONG RegKey::DeleteKey(const wchar_t* name) { michael@0: DCHECK(key_); michael@0: DCHECK(name); michael@0: LONG result = SHDeleteKey(key_, name); michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::DeleteValue(const wchar_t* value_name) { michael@0: DCHECK(key_); michael@0: LONG result = RegDeleteValue(key_, value_name); michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* out_value) const { michael@0: DCHECK(out_value); michael@0: DWORD type = REG_DWORD; michael@0: DWORD size = sizeof(DWORD); michael@0: DWORD local_value = 0; michael@0: LONG result = ReadValue(name, &local_value, &size, &type); michael@0: if (result == ERROR_SUCCESS) { michael@0: if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) michael@0: *out_value = local_value; michael@0: else michael@0: result = ERROR_CANTREAD; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::ReadInt64(const wchar_t* name, int64* out_value) const { michael@0: DCHECK(out_value); michael@0: DWORD type = REG_QWORD; michael@0: int64 local_value = 0; michael@0: DWORD size = sizeof(local_value); michael@0: LONG result = ReadValue(name, &local_value, &size, &type); michael@0: if (result == ERROR_SUCCESS) { michael@0: if ((type == REG_QWORD || type == REG_BINARY) && michael@0: size == sizeof(local_value)) michael@0: *out_value = local_value; michael@0: else michael@0: result = ERROR_CANTREAD; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::ReadValue(const wchar_t* name, std::wstring* out_value) const { michael@0: DCHECK(out_value); michael@0: const size_t kMaxStringLength = 1024; // This is after expansion. michael@0: // Use the one of the other forms of ReadValue if 1024 is too small for you. michael@0: wchar_t raw_value[kMaxStringLength]; michael@0: DWORD type = REG_SZ, size = sizeof(raw_value); michael@0: LONG result = ReadValue(name, raw_value, &size, &type); michael@0: if (result == ERROR_SUCCESS) { michael@0: if (type == REG_SZ) { michael@0: *out_value = raw_value; michael@0: } else if (type == REG_EXPAND_SZ) { michael@0: wchar_t expanded[kMaxStringLength]; michael@0: size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength); michael@0: // Success: returns the number of wchar_t's copied michael@0: // Fail: buffer too small, returns the size required michael@0: // Fail: other, returns 0 michael@0: if (size == 0 || size > kMaxStringLength) { michael@0: result = ERROR_MORE_DATA; michael@0: } else { michael@0: *out_value = expanded; michael@0: } michael@0: } else { michael@0: // Not a string. Oops. michael@0: result = ERROR_CANTREAD; michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::ReadValue(const wchar_t* name, michael@0: void* data, michael@0: DWORD* dsize, michael@0: DWORD* dtype) const { michael@0: LONG result = RegQueryValueEx(key_, name, 0, dtype, michael@0: reinterpret_cast(data), dsize); michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::ReadValues(const wchar_t* name, michael@0: std::vector* values) { michael@0: values->clear(); michael@0: michael@0: DWORD type = REG_MULTI_SZ; michael@0: DWORD size = 0; michael@0: LONG result = ReadValue(name, NULL, &size, &type); michael@0: if (FAILED(result) || size == 0) michael@0: return result; michael@0: michael@0: if (type != REG_MULTI_SZ) michael@0: return ERROR_CANTREAD; michael@0: michael@0: std::vector buffer(size / sizeof(wchar_t)); michael@0: result = ReadValue(name, &buffer[0], &size, NULL); michael@0: if (FAILED(result) || size == 0) michael@0: return result; michael@0: michael@0: // Parse the double-null-terminated list of strings. michael@0: // Note: This code is paranoid to not read outside of |buf|, in the case where michael@0: // it may not be properly terminated. michael@0: const wchar_t* entry = &buffer[0]; michael@0: const wchar_t* buffer_end = entry + (size / sizeof(wchar_t)); michael@0: while (entry < buffer_end && entry[0] != '\0') { michael@0: const wchar_t* entry_end = std::find(entry, buffer_end, L'\0'); michael@0: values->push_back(std::wstring(entry, entry_end)); michael@0: entry = entry_end + 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: LONG RegKey::WriteValue(const wchar_t* name, DWORD in_value) { michael@0: return WriteValue( michael@0: name, &in_value, static_cast(sizeof(in_value)), REG_DWORD); michael@0: } michael@0: michael@0: LONG RegKey::WriteValue(const wchar_t * name, const wchar_t* in_value) { michael@0: return WriteValue(name, in_value, michael@0: static_cast(sizeof(*in_value) * (wcslen(in_value) + 1)), REG_SZ); michael@0: } michael@0: michael@0: LONG RegKey::WriteValue(const wchar_t* name, michael@0: const void* data, michael@0: DWORD dsize, michael@0: DWORD dtype) { michael@0: DCHECK(data || !dsize); michael@0: michael@0: LONG result = RegSetValueEx(key_, name, 0, dtype, michael@0: reinterpret_cast(const_cast(data)), dsize); michael@0: return result; michael@0: } michael@0: michael@0: LONG RegKey::StartWatching() { michael@0: DCHECK(key_); michael@0: if (!watch_event_) michael@0: watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); michael@0: michael@0: DWORD filter = REG_NOTIFY_CHANGE_NAME | michael@0: REG_NOTIFY_CHANGE_ATTRIBUTES | michael@0: REG_NOTIFY_CHANGE_LAST_SET | michael@0: REG_NOTIFY_CHANGE_SECURITY; michael@0: michael@0: // Watch the registry key for a change of value. michael@0: LONG result = RegNotifyChangeKeyValue(key_, TRUE, filter, watch_event_, TRUE); michael@0: if (result != ERROR_SUCCESS) { michael@0: CloseHandle(watch_event_); michael@0: watch_event_ = 0; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: bool RegKey::HasChanged() { michael@0: if (watch_event_) { michael@0: if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) { michael@0: StartWatching(); michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: LONG RegKey::StopWatching() { michael@0: LONG result = ERROR_INVALID_HANDLE; michael@0: if (watch_event_) { michael@0: CloseHandle(watch_event_); michael@0: watch_event_ = 0; michael@0: result = ERROR_SUCCESS; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: // RegistryValueIterator ------------------------------------------------------ michael@0: michael@0: RegistryValueIterator::RegistryValueIterator(HKEY root_key, michael@0: const wchar_t* folder_key) michael@0: : name_(MAX_PATH, L'\0'), michael@0: value_(MAX_PATH, L'\0') { michael@0: LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); michael@0: if (result != ERROR_SUCCESS) { michael@0: key_ = NULL; michael@0: } else { michael@0: DWORD count = 0; michael@0: result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, michael@0: NULL, NULL, NULL, NULL); michael@0: michael@0: if (result != ERROR_SUCCESS) { michael@0: ::RegCloseKey(key_); michael@0: key_ = NULL; michael@0: } else { michael@0: index_ = count - 1; michael@0: } michael@0: } michael@0: michael@0: Read(); michael@0: } michael@0: michael@0: RegistryValueIterator::~RegistryValueIterator() { michael@0: if (key_) michael@0: ::RegCloseKey(key_); michael@0: } michael@0: michael@0: DWORD RegistryValueIterator::ValueCount() const { michael@0: DWORD count = 0; michael@0: LONG result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, michael@0: &count, NULL, NULL, NULL, NULL); michael@0: if (result != ERROR_SUCCESS) michael@0: return 0; michael@0: michael@0: return count; michael@0: } michael@0: michael@0: bool RegistryValueIterator::Valid() const { michael@0: return key_ != NULL && index_ >= 0; michael@0: } michael@0: michael@0: void RegistryValueIterator::operator++() { michael@0: --index_; michael@0: Read(); michael@0: } michael@0: michael@0: bool RegistryValueIterator::Read() { michael@0: if (Valid()) { michael@0: DWORD capacity = static_cast(name_.capacity()); michael@0: DWORD name_size = capacity; michael@0: // |value_size_| is in bytes. Reserve the last character for a NUL. michael@0: value_size_ = static_cast((value_.size() - 1) * sizeof(wchar_t)); michael@0: LONG result = ::RegEnumValue( michael@0: key_, index_, WriteInto(&name_, name_size), &name_size, NULL, &type_, michael@0: reinterpret_cast(vector_as_array(&value_)), &value_size_); michael@0: michael@0: if (result == ERROR_MORE_DATA) { michael@0: // Registry key names are limited to 255 characters and fit within michael@0: // MAX_PATH (which is 260) but registry value names can use up to 16,383 michael@0: // characters and the value itself is not limited michael@0: // (from http://msdn.microsoft.com/en-us/library/windows/desktop/ michael@0: // ms724872(v=vs.85).aspx). michael@0: // Resize the buffers and retry if their size caused the failure. michael@0: DWORD value_size_in_wchars = to_wchar_size(value_size_); michael@0: if (value_size_in_wchars + 1 > value_.size()) michael@0: value_.resize(value_size_in_wchars + 1, L'\0'); michael@0: value_size_ = static_cast((value_.size() - 1) * sizeof(wchar_t)); michael@0: name_size = name_size == capacity ? MAX_REGISTRY_NAME_SIZE : capacity; michael@0: result = ::RegEnumValue( michael@0: key_, index_, WriteInto(&name_, name_size), &name_size, NULL, &type_, michael@0: reinterpret_cast(vector_as_array(&value_)), &value_size_); michael@0: } michael@0: michael@0: if (result == ERROR_SUCCESS) { michael@0: DCHECK_LT(to_wchar_size(value_size_), value_.size()); michael@0: value_[to_wchar_size(value_size_)] = L'\0'; michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: name_[0] = L'\0'; michael@0: value_[0] = L'\0'; michael@0: value_size_ = 0; michael@0: return false; michael@0: } michael@0: michael@0: // RegistryKeyIterator -------------------------------------------------------- michael@0: michael@0: RegistryKeyIterator::RegistryKeyIterator(HKEY root_key, michael@0: const wchar_t* folder_key) { michael@0: LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); michael@0: if (result != ERROR_SUCCESS) { michael@0: key_ = NULL; michael@0: } else { michael@0: DWORD count = 0; michael@0: LONG result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL, michael@0: NULL, NULL, NULL, NULL, NULL); michael@0: michael@0: if (result != ERROR_SUCCESS) { michael@0: ::RegCloseKey(key_); michael@0: key_ = NULL; michael@0: } else { michael@0: index_ = count - 1; michael@0: } michael@0: } michael@0: michael@0: Read(); michael@0: } michael@0: michael@0: RegistryKeyIterator::~RegistryKeyIterator() { michael@0: if (key_) michael@0: ::RegCloseKey(key_); michael@0: } michael@0: michael@0: DWORD RegistryKeyIterator::SubkeyCount() const { michael@0: DWORD count = 0; michael@0: LONG result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL, michael@0: NULL, NULL, NULL, NULL, NULL); michael@0: if (result != ERROR_SUCCESS) michael@0: return 0; michael@0: michael@0: return count; michael@0: } michael@0: michael@0: bool RegistryKeyIterator::Valid() const { michael@0: return key_ != NULL && index_ >= 0; michael@0: } michael@0: michael@0: void RegistryKeyIterator::operator++() { michael@0: --index_; michael@0: Read(); michael@0: } michael@0: michael@0: bool RegistryKeyIterator::Read() { michael@0: if (Valid()) { michael@0: DWORD ncount = arraysize(name_); michael@0: FILETIME written; michael@0: LONG r = ::RegEnumKeyEx(key_, index_, name_, &ncount, NULL, NULL, michael@0: NULL, &written); michael@0: if (ERROR_SUCCESS == r) michael@0: return true; michael@0: } michael@0: michael@0: name_[0] = '\0'; michael@0: return false; michael@0: } michael@0: michael@0: } // namespace win michael@0: } // namespace base