Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: base/win/registry.cc

Issue 6346010: Remove sanity check in RegKey to prevent usage of checking... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/win/registry.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 21 matching lines...) Expand all
32 Open(rootkey, subkey, access); 32 Open(rootkey, subkey, access);
33 } else { 33 } else {
34 DCHECK(!subkey); 34 DCHECK(!subkey);
35 } 35 }
36 } 36 }
37 37
38 RegKey::~RegKey() { 38 RegKey::~RegKey() {
39 Close(); 39 Close();
40 } 40 }
41 41
42 GONG RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) { 42 LONG RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
43 DWORD disposition_value; 43 DWORD disposition_value;
44 return CreateWithDisposition(rootkey, subkey, &disposition_value, access); 44 return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
45 } 45 }
46 46
47 GONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, 47 LONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
48 DWORD* disposition, REGSAM access) { 48 DWORD* disposition, REGSAM access) {
49 base::ThreadRestrictions::AssertIOAllowed(); 49 base::ThreadRestrictions::AssertIOAllowed();
50 DCHECK(rootkey && subkey && access && disposition); 50 DCHECK(rootkey && subkey && access && disposition);
51 Close(); 51 Close();
52 52
53 LONG result = RegCreateKeyEx(rootkey, subkey, 0, NULL, 53 LONG result = RegCreateKeyEx(rootkey, subkey, 0, NULL,
54 REG_OPTION_NON_VOLATILE, access, NULL, &key_, 54 REG_OPTION_NON_VOLATILE, access, NULL, &key_,
55 disposition); 55 disposition);
56 return result; 56 return result;
57 } 57 }
58 58
59 GONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { 59 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
60 base::ThreadRestrictions::AssertIOAllowed(); 60 base::ThreadRestrictions::AssertIOAllowed();
61 DCHECK(rootkey && subkey && access); 61 DCHECK(rootkey && subkey && access);
62 Close(); 62 Close();
63 63
64 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_); 64 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_);
65 return result; 65 return result;
66 } 66 }
67 67
68 GONG RegKey::CreateKey(const wchar_t* name, REGSAM access) { 68 LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) {
69 base::ThreadRestrictions::AssertIOAllowed(); 69 base::ThreadRestrictions::AssertIOAllowed();
70 DCHECK(name && access); 70 DCHECK(name && access);
71 71
72 HKEY subkey = NULL; 72 HKEY subkey = NULL;
73 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, 73 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE,
74 access, NULL, &subkey, NULL); 74 access, NULL, &subkey, NULL);
75 Close(); 75 Close();
76 76
77 key_ = subkey; 77 key_ = subkey;
78 return result; 78 return result;
79 } 79 }
80 80
81 GONG RegKey::OpenKey(const wchar_t* name, REGSAM access) { 81 LONG RegKey::OpenKey(const wchar_t* name, REGSAM access) {
82 base::ThreadRestrictions::AssertIOAllowed(); 82 base::ThreadRestrictions::AssertIOAllowed();
83 DCHECK(name && access); 83 DCHECK(name && access);
84 84
85 HKEY subkey = NULL; 85 HKEY subkey = NULL;
86 LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey); 86 LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey);
87 87
88 Close(); 88 Close();
89 89
90 key_ = subkey; 90 key_ = subkey;
91 return result; 91 return result;
92 } 92 }
93 93
94 void RegKey::Close() { 94 void RegKey::Close() {
95 base::ThreadRestrictions::AssertIOAllowed(); 95 base::ThreadRestrictions::AssertIOAllowed();
96 StopWatching(); 96 StopWatching();
97 if (key_) { 97 if (key_) {
98 ::RegCloseKey(key_); 98 ::RegCloseKey(key_);
99 key_ = NULL; 99 key_ = NULL;
100 } 100 }
101 } 101 }
102 102
103 DWORD RegKey::ValueCount() const { 103 DWORD RegKey::ValueCount() const {
104 base::ThreadRestrictions::AssertIOAllowed(); 104 base::ThreadRestrictions::AssertIOAllowed();
105 DWORD count = 0; 105 DWORD count = 0;
106 LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, 106 LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count,
107 NULL, NULL, NULL, NULL); 107 NULL, NULL, NULL, NULL);
108 return (result != ERROR_SUCCESS) ? 0 : count; 108 return (result != ERROR_SUCCESS) ? 0 : count;
109 } 109 }
110 110
111 GONG RegKey::ReadName(int index, std::wstring* name) const { 111 LONG RegKey::ReadName(int index, std::wstring* name) const {
112 base::ThreadRestrictions::AssertIOAllowed(); 112 base::ThreadRestrictions::AssertIOAllowed();
113 wchar_t buf[256]; 113 wchar_t buf[256];
114 DWORD bufsize = arraysize(buf); 114 DWORD bufsize = arraysize(buf);
115 LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL); 115 LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL);
116 if (r == ERROR_SUCCESS) 116 if (r == ERROR_SUCCESS)
117 *name = buf; 117 *name = buf;
118 118
119 return r; 119 return r;
120 } 120 }
121 121
122 GONG RegKey::DeleteKey(const wchar_t* name) { 122 LONG RegKey::DeleteKey(const wchar_t* name) {
123 base::ThreadRestrictions::AssertIOAllowed(); 123 base::ThreadRestrictions::AssertIOAllowed();
124 DCHECK(key_); 124 DCHECK(key_);
125 DCHECK(name); 125 DCHECK(name);
126 LONG result = SHDeleteKey(key_, name); 126 LONG result = SHDeleteKey(key_, name);
127 return result; 127 return result;
128 } 128 }
129 129
130 GONG RegKey::DeleteValue(const wchar_t* value_name) { 130 LONG RegKey::DeleteValue(const wchar_t* value_name) {
131 base::ThreadRestrictions::AssertIOAllowed(); 131 base::ThreadRestrictions::AssertIOAllowed();
132 DCHECK(key_); 132 DCHECK(key_);
133 DCHECK(value_name); 133 DCHECK(value_name);
134 LONG result = RegDeleteValue(key_, value_name); 134 LONG result = RegDeleteValue(key_, value_name);
135 return result; 135 return result;
136 } 136 }
137 137
138 bool RegKey::ValueExists(const wchar_t* name) const { 138 bool RegKey::ValueExists(const wchar_t* name) const {
139 base::ThreadRestrictions::AssertIOAllowed(); 139 base::ThreadRestrictions::AssertIOAllowed();
140 LONG result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL); 140 LONG result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL);
141 return result == ERROR_SUCCESS; 141 return result == ERROR_SUCCESS;
142 } 142 }
143 143
144 GONG RegKey::ReadValue(const wchar_t* name, void* data, DWORD* dsize, 144 LONG RegKey::ReadValue(const wchar_t* name, void* data, DWORD* dsize,
145 DWORD* dtype) const { 145 DWORD* dtype) const {
146 base::ThreadRestrictions::AssertIOAllowed(); 146 base::ThreadRestrictions::AssertIOAllowed();
147 LONG result = RegQueryValueEx(key_, name, 0, dtype, 147 LONG result = RegQueryValueEx(key_, name, 0, dtype,
148 reinterpret_cast<LPBYTE>(data), dsize); 148 reinterpret_cast<LPBYTE>(data), dsize);
149 return result; 149 return result;
150 } 150 }
151 151
152 GONG RegKey::ReadValue(const wchar_t* name, std::wstring* value) const { 152 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* value) const {
153 base::ThreadRestrictions::AssertIOAllowed(); 153 base::ThreadRestrictions::AssertIOAllowed();
154 DCHECK(value); 154 DCHECK(value);
155 const size_t kMaxStringLength = 1024; // This is after expansion. 155 const size_t kMaxStringLength = 1024; // This is after expansion.
156 // Use the one of the other forms of ReadValue if 1024 is too small for you. 156 // Use the one of the other forms of ReadValue if 1024 is too small for you.
157 wchar_t raw_value[kMaxStringLength]; 157 wchar_t raw_value[kMaxStringLength];
158 DWORD type = REG_SZ, size = sizeof(raw_value); 158 DWORD type = REG_SZ, size = sizeof(raw_value);
159 LONG result = ReadValue(name, raw_value, &size, &type); 159 LONG result = ReadValue(name, raw_value, &size, &type);
160 if (result == ERROR_SUCCESS) { 160 if (result == ERROR_SUCCESS) {
161 if (type == REG_SZ) { 161 if (type == REG_SZ) {
162 *value = raw_value; 162 *value = raw_value;
(...skipping 10 matching lines...) Expand all
173 } 173 }
174 } else { 174 } else {
175 // Not a string. Oops. 175 // Not a string. Oops.
176 result = ERROR_CANTREAD; 176 result = ERROR_CANTREAD;
177 } 177 }
178 } 178 }
179 179
180 return result; 180 return result;
181 } 181 }
182 182
183 GONG RegKey::ReadValueDW(const wchar_t* name, DWORD* value) const { 183 LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* value) const {
184 DCHECK(value); 184 DCHECK(value);
185 DWORD type = REG_DWORD; 185 DWORD type = REG_DWORD;
186 DWORD size = sizeof(DWORD); 186 DWORD size = sizeof(DWORD);
187 DWORD local_value = 0; 187 DWORD local_value = 0;
188 LONG result = ReadValue(name, &local_value, &size, &type); 188 LONG result = ReadValue(name, &local_value, &size, &type);
189 if (result == ERROR_SUCCESS) { 189 if (result == ERROR_SUCCESS) {
190 if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) { 190 if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) {
191 *value = local_value; 191 *value = local_value;
192 } else { 192 } else {
193 result = ERROR_CANTREAD; 193 result = ERROR_CANTREAD;
194 } 194 }
195 } 195 }
196 196
197 return result; 197 return result;
198 } 198 }
199 199
200 GONG RegKey::ReadInt64(const wchar_t* name, int64* value) const { 200 LONG RegKey::ReadInt64(const wchar_t* name, int64* value) const {
201 DCHECK(value); 201 DCHECK(value);
202 DWORD type = REG_QWORD; 202 DWORD type = REG_QWORD;
203 int64 local_value = 0; 203 int64 local_value = 0;
204 DWORD size = sizeof(local_value); 204 DWORD size = sizeof(local_value);
205 LONG result = ReadValue(name, &local_value, &size, &type); 205 LONG result = ReadValue(name, &local_value, &size, &type);
206 if (result == ERROR_SUCCESS) { 206 if (result == ERROR_SUCCESS) {
207 if ((type == REG_QWORD || type == REG_BINARY) && 207 if ((type == REG_QWORD || type == REG_BINARY) &&
208 size == sizeof(local_value)) { 208 size == sizeof(local_value)) {
209 *value = local_value; 209 *value = local_value;
210 } else { 210 } else {
211 result = ERROR_CANTREAD; 211 result = ERROR_CANTREAD;
212 } 212 }
213 } 213 }
214 214
215 return result; 215 return result;
216 } 216 }
217 217
218 GONG RegKey::WriteValue(const wchar_t* name, const void * data, 218 LONG RegKey::WriteValue(const wchar_t* name, const void * data,
219 DWORD dsize, DWORD dtype) { 219 DWORD dsize, DWORD dtype) {
220 base::ThreadRestrictions::AssertIOAllowed(); 220 base::ThreadRestrictions::AssertIOAllowed();
221 DCHECK(data); 221 DCHECK(data);
222 222
223 LONG result = RegSetValueEx(key_, name, 0, dtype, 223 LONG result = RegSetValueEx(key_, name, 0, dtype,
224 reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize); 224 reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize);
225 return result; 225 return result;
226 } 226 }
227 227
228 GONG RegKey::WriteValue(const wchar_t * name, const wchar_t* value) { 228 LONG RegKey::WriteValue(const wchar_t * name, const wchar_t* value) {
229 return WriteValue(name, value, 229 return WriteValue(name, value,
230 static_cast<DWORD>(sizeof(*value) * (wcslen(value) + 1)), REG_SZ); 230 static_cast<DWORD>(sizeof(*value) * (wcslen(value) + 1)), REG_SZ);
231 } 231 }
232 232
233 GONG RegKey::WriteValue(const wchar_t* name, DWORD value) { 233 LONG RegKey::WriteValue(const wchar_t* name, DWORD value) {
234 return WriteValue(name, &value, static_cast<DWORD>(sizeof(value)), REG_DWORD); 234 return WriteValue(name, &value, static_cast<DWORD>(sizeof(value)), REG_DWORD);
235 } 235 }
236 236
237 GONG RegKey::StartWatching() { 237 LONG RegKey::StartWatching() {
238 DCHECK(key_); 238 DCHECK(key_);
239 if (!watch_event_) 239 if (!watch_event_)
240 watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); 240 watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL);
241 241
242 DWORD filter = REG_NOTIFY_CHANGE_NAME | 242 DWORD filter = REG_NOTIFY_CHANGE_NAME |
243 REG_NOTIFY_CHANGE_ATTRIBUTES | 243 REG_NOTIFY_CHANGE_ATTRIBUTES |
244 REG_NOTIFY_CHANGE_LAST_SET | 244 REG_NOTIFY_CHANGE_LAST_SET |
245 REG_NOTIFY_CHANGE_SECURITY; 245 REG_NOTIFY_CHANGE_SECURITY;
246 246
247 // Watch the registry key for a change of value. 247 // Watch the registry key for a change of value.
248 LONG result = RegNotifyChangeKeyValue(key_, TRUE, filter, watch_event_, TRUE); 248 LONG result = RegNotifyChangeKeyValue(key_, TRUE, filter, watch_event_, TRUE);
249 if (result != ERROR_SUCCESS) { 249 if (result != ERROR_SUCCESS) {
250 CloseHandle(watch_event_); 250 CloseHandle(watch_event_);
251 watch_event_ = 0; 251 watch_event_ = 0;
252 } 252 }
253 253
254 return result; 254 return result;
255 } 255 }
256 256
257 bool RegKey::HasChanged() { 257 bool RegKey::HasChanged() {
258 if (watch_event_) { 258 if (watch_event_) {
259 if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) { 259 if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) {
260 StartWatching(); 260 StartWatching();
261 return true; 261 return true;
262 } 262 }
263 } 263 }
264 return false; 264 return false;
265 } 265 }
266 266
267 GONG RegKey::StopWatching() { 267 LONG RegKey::StopWatching() {
268 LONG result = ERROR_INVALID_HANDLE; 268 LONG result = ERROR_INVALID_HANDLE;
269 if (watch_event_) { 269 if (watch_event_) {
270 CloseHandle(watch_event_); 270 CloseHandle(watch_event_);
271 watch_event_ = 0; 271 watch_event_ = 0;
272 result = ERROR_SUCCESS; 272 result = ERROR_SUCCESS;
273 } 273 }
274 return result; 274 return result;
275 } 275 }
276 276
277 // RegistryValueIterator ------------------------------------------------------ 277 // RegistryValueIterator ------------------------------------------------------
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 if (ERROR_SUCCESS == r) 402 if (ERROR_SUCCESS == r)
403 return true; 403 return true;
404 } 404 }
405 405
406 name_[0] = '\0'; 406 name_[0] = '\0';
407 return false; 407 return false;
408 } 408 }
409 409
410 } // namespace win 410 } // namespace win
411 } // namespace base 411 } // namespace base
OLDNEW
« no previous file with comments | « base/win/registry.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698