| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 135 } |
| 136 | 136 |
| 137 LONG RegKey::DeleteValue(const wchar_t* value_name) { | 137 LONG RegKey::DeleteValue(const wchar_t* value_name) { |
| 138 base::ThreadRestrictions::AssertIOAllowed(); | 138 base::ThreadRestrictions::AssertIOAllowed(); |
| 139 DCHECK(key_); | 139 DCHECK(key_); |
| 140 DCHECK(value_name); | 140 DCHECK(value_name); |
| 141 LONG result = RegDeleteValue(key_, value_name); | 141 LONG result = RegDeleteValue(key_, value_name); |
| 142 return result; | 142 return result; |
| 143 } | 143 } |
| 144 | 144 |
| 145 LONG RegKey::ReadValue(const wchar_t* name, void* data, DWORD* dsize, | 145 LONG RegKey::ReadValue(const wchar_t* name, DWORD* out_value) const { |
| 146 DWORD* dtype) const { | 146 DCHECK(out_value); |
| 147 base::ThreadRestrictions::AssertIOAllowed(); | 147 DWORD type = REG_DWORD; |
| 148 LONG result = RegQueryValueEx(key_, name, 0, dtype, | 148 DWORD size = sizeof(DWORD); |
| 149 reinterpret_cast<LPBYTE>(data), dsize); | 149 DWORD local_value = 0; |
| 150 LONG result = ReadValue(name, &local_value, &size, &type); |
| 151 if (result == ERROR_SUCCESS) { |
| 152 if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) |
| 153 *out_value = local_value; |
| 154 else |
| 155 result = ERROR_CANTREAD; |
| 156 } |
| 157 |
| 150 return result; | 158 return result; |
| 151 } | 159 } |
| 152 | 160 |
| 153 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* value) const { | 161 LONG RegKey::ReadValue(const wchar_t* name, int64* out_value) const { |
| 162 DCHECK(out_value); |
| 163 DWORD type = REG_QWORD; |
| 164 int64 local_value = 0; |
| 165 DWORD size = sizeof(local_value); |
| 166 LONG result = ReadValue(name, &local_value, &size, &type); |
| 167 if (result == ERROR_SUCCESS) { |
| 168 if ((type == REG_QWORD || type == REG_BINARY) && |
| 169 size == sizeof(local_value)) |
| 170 *out_value = local_value; |
| 171 else |
| 172 result = ERROR_CANTREAD; |
| 173 } |
| 174 |
| 175 return result; |
| 176 } |
| 177 |
| 178 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* out_value) const { |
| 154 base::ThreadRestrictions::AssertIOAllowed(); | 179 base::ThreadRestrictions::AssertIOAllowed(); |
| 155 DCHECK(value); | 180 DCHECK(out_value); |
| 156 const size_t kMaxStringLength = 1024; // This is after expansion. | 181 const size_t kMaxStringLength = 1024; // This is after expansion. |
| 157 // Use the one of the other forms of ReadValue if 1024 is too small for you. | 182 // Use the one of the other forms of ReadValue if 1024 is too small for you. |
| 158 wchar_t raw_value[kMaxStringLength]; | 183 wchar_t raw_value[kMaxStringLength]; |
| 159 DWORD type = REG_SZ, size = sizeof(raw_value); | 184 DWORD type = REG_SZ, size = sizeof(raw_value); |
| 160 LONG result = ReadValue(name, raw_value, &size, &type); | 185 LONG result = ReadValue(name, raw_value, &size, &type); |
| 161 if (result == ERROR_SUCCESS) { | 186 if (result == ERROR_SUCCESS) { |
| 162 if (type == REG_SZ) { | 187 if (type == REG_SZ) { |
| 163 *value = raw_value; | 188 *out_value = raw_value; |
| 164 } else if (type == REG_EXPAND_SZ) { | 189 } else if (type == REG_EXPAND_SZ) { |
| 165 wchar_t expanded[kMaxStringLength]; | 190 wchar_t expanded[kMaxStringLength]; |
| 166 size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength); | 191 size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength); |
| 167 // Success: returns the number of wchar_t's copied | 192 // Success: returns the number of wchar_t's copied |
| 168 // Fail: buffer too small, returns the size required | 193 // Fail: buffer too small, returns the size required |
| 169 // Fail: other, returns 0 | 194 // Fail: other, returns 0 |
| 170 if (size == 0 || size > kMaxStringLength) { | 195 if (size == 0 || size > kMaxStringLength) { |
| 171 result = ERROR_MORE_DATA; | 196 result = ERROR_MORE_DATA; |
| 172 } else { | 197 } else { |
| 173 *value = expanded; | 198 *out_value = expanded; |
| 174 } | 199 } |
| 175 } else { | 200 } else { |
| 176 // Not a string. Oops. | 201 // Not a string. Oops. |
| 177 result = ERROR_CANTREAD; | 202 result = ERROR_CANTREAD; |
| 178 } | 203 } |
| 179 } | 204 } |
| 180 | 205 |
| 181 return result; | 206 return result; |
| 182 } | 207 } |
| 183 | 208 |
| 184 LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* value) const { | 209 LONG RegKey::ReadValue(const wchar_t* name, |
| 185 DCHECK(value); | 210 void* data, |
| 186 DWORD type = REG_DWORD; | 211 DWORD* dsize, |
| 187 DWORD size = sizeof(DWORD); | 212 DWORD* dtype) const { |
| 188 DWORD local_value = 0; | 213 base::ThreadRestrictions::AssertIOAllowed(); |
| 189 LONG result = ReadValue(name, &local_value, &size, &type); | 214 LONG result = RegQueryValueEx(key_, name, 0, dtype, |
| 190 if (result == ERROR_SUCCESS) { | 215 reinterpret_cast<LPBYTE>(data), dsize); |
| 191 if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) { | |
| 192 *value = local_value; | |
| 193 } else { | |
| 194 result = ERROR_CANTREAD; | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 return result; | 216 return result; |
| 199 } | 217 } |
| 200 | 218 |
| 201 LONG RegKey::ReadInt64(const wchar_t* name, int64* value) const { | 219 LONG RegKey::WriteValue(const wchar_t* name, DWORD in_value) { |
| 202 DCHECK(value); | 220 return WriteValue( |
| 203 DWORD type = REG_QWORD; | 221 name, &in_value, static_cast<DWORD>(sizeof(in_value)), REG_DWORD); |
| 204 int64 local_value = 0; | |
| 205 DWORD size = sizeof(local_value); | |
| 206 LONG result = ReadValue(name, &local_value, &size, &type); | |
| 207 if (result == ERROR_SUCCESS) { | |
| 208 if ((type == REG_QWORD || type == REG_BINARY) && | |
| 209 size == sizeof(local_value)) { | |
| 210 *value = local_value; | |
| 211 } else { | |
| 212 result = ERROR_CANTREAD; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 return result; | |
| 217 } | 222 } |
| 218 | 223 |
| 219 LONG RegKey::WriteValue(const wchar_t* name, const void * data, | 224 LONG RegKey::WriteValue(const wchar_t * name, const wchar_t* in_value) { |
| 220 DWORD dsize, DWORD dtype) { | 225 return WriteValue(name, in_value, |
| 226 static_cast<DWORD>(sizeof(*in_value) * (wcslen(in_value) + 1)), REG_SZ); |
| 227 } |
| 228 |
| 229 LONG RegKey::WriteValue(const wchar_t* name, |
| 230 const void* data, |
| 231 DWORD dsize, |
| 232 DWORD dtype) { |
| 221 base::ThreadRestrictions::AssertIOAllowed(); | 233 base::ThreadRestrictions::AssertIOAllowed(); |
| 222 DCHECK(data || !dsize); | 234 DCHECK(data || !dsize); |
| 223 | 235 |
| 224 LONG result = RegSetValueEx(key_, name, 0, dtype, | 236 LONG result = RegSetValueEx(key_, name, 0, dtype, |
| 225 reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize); | 237 reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize); |
| 226 return result; | 238 return result; |
| 227 } | 239 } |
| 228 | 240 |
| 229 LONG RegKey::WriteValue(const wchar_t * name, const wchar_t* value) { | |
| 230 return WriteValue(name, value, | |
| 231 static_cast<DWORD>(sizeof(*value) * (wcslen(value) + 1)), REG_SZ); | |
| 232 } | |
| 233 | |
| 234 LONG RegKey::WriteValue(const wchar_t* name, DWORD value) { | |
| 235 return WriteValue(name, &value, static_cast<DWORD>(sizeof(value)), REG_DWORD); | |
| 236 } | |
| 237 | |
| 238 LONG RegKey::StartWatching() { | 241 LONG RegKey::StartWatching() { |
| 239 DCHECK(key_); | 242 DCHECK(key_); |
| 240 if (!watch_event_) | 243 if (!watch_event_) |
| 241 watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); | 244 watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 242 | 245 |
| 243 DWORD filter = REG_NOTIFY_CHANGE_NAME | | 246 DWORD filter = REG_NOTIFY_CHANGE_NAME | |
| 244 REG_NOTIFY_CHANGE_ATTRIBUTES | | 247 REG_NOTIFY_CHANGE_ATTRIBUTES | |
| 245 REG_NOTIFY_CHANGE_LAST_SET | | 248 REG_NOTIFY_CHANGE_LAST_SET | |
| 246 REG_NOTIFY_CHANGE_SECURITY; | 249 REG_NOTIFY_CHANGE_SECURITY; |
| 247 | 250 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 if (ERROR_SUCCESS == r) | 406 if (ERROR_SUCCESS == r) |
| 404 return true; | 407 return true; |
| 405 } | 408 } |
| 406 | 409 |
| 407 name_[0] = '\0'; | 410 name_[0] = '\0'; |
| 408 return false; | 411 return false; |
| 409 } | 412 } |
| 410 | 413 |
| 411 } // namespace win | 414 } // namespace win |
| 412 } // namespace base | 415 } // namespace base |
| OLD | NEW |