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

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

Issue 4431001: Revert 64960 - Turn on file access checks on Win.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 1 month 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/platform_file_win.cc ('k') | base/win_util.cc » ('j') | 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) 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"
11 10
12 #pragma comment(lib, "shlwapi.lib") // for SHDeleteKey 11 #pragma comment(lib, "shlwapi.lib") // for SHDeleteKey
13 12
14 namespace base { 13 namespace base {
15 namespace win { 14 namespace win {
16 15
17 RegistryValueIterator::RegistryValueIterator(HKEY root_key, 16 RegistryValueIterator::RegistryValueIterator(HKEY root_key,
18 const wchar_t* folder_key) { 17 const wchar_t* folder_key) {
19 base::ThreadRestrictions::AssertIOAllowed();
20
21 LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); 18 LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_);
22 if (result != ERROR_SUCCESS) { 19 if (result != ERROR_SUCCESS) {
23 key_ = NULL; 20 key_ = NULL;
24 } else { 21 } else {
25 DWORD count = 0; 22 DWORD count = 0;
26 result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, 23 result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count,
27 NULL, NULL, NULL, NULL); 24 NULL, NULL, NULL, NULL);
28 25
29 if (result != ERROR_SUCCESS) { 26 if (result != ERROR_SUCCESS) {
30 ::RegCloseKey(key_); 27 ::RegCloseKey(key_);
31 key_ = NULL; 28 key_ = NULL;
32 } else { 29 } else {
33 index_ = count - 1; 30 index_ = count - 1;
34 } 31 }
35 } 32 }
36 33
37 Read(); 34 Read();
38 } 35 }
39 36
40 RegistryValueIterator::~RegistryValueIterator() { 37 RegistryValueIterator::~RegistryValueIterator() {
41 base::ThreadRestrictions::AssertIOAllowed();
42 if (key_) 38 if (key_)
43 ::RegCloseKey(key_); 39 ::RegCloseKey(key_);
44 } 40 }
45 41
46 bool RegistryValueIterator::Valid() const { 42 bool RegistryValueIterator::Valid() const {
47 return key_ != NULL && index_ >= 0; 43 return key_ != NULL && index_ >= 0;
48 } 44 }
49 45
50 void RegistryValueIterator::operator++() { 46 void RegistryValueIterator::operator++() {
51 --index_; 47 --index_;
52 Read(); 48 Read();
53 } 49 }
54 50
55 bool RegistryValueIterator::Read() { 51 bool RegistryValueIterator::Read() {
56 base::ThreadRestrictions::AssertIOAllowed();
57 if (Valid()) { 52 if (Valid()) {
58 DWORD ncount = arraysize(name_); 53 DWORD ncount = arraysize(name_);
59 value_size_ = sizeof(value_); 54 value_size_ = sizeof(value_);
60 LRESULT r = ::RegEnumValue(key_, index_, name_, &ncount, NULL, &type_, 55 LRESULT r = ::RegEnumValue(key_, index_, name_, &ncount, NULL, &type_,
61 reinterpret_cast<BYTE*>(value_), &value_size_); 56 reinterpret_cast<BYTE*>(value_), &value_size_);
62 if (ERROR_SUCCESS == r) 57 if (ERROR_SUCCESS == r)
63 return true; 58 return true;
64 } 59 }
65 60
66 name_[0] = '\0'; 61 name_[0] = '\0';
67 value_[0] = '\0'; 62 value_[0] = '\0';
68 value_size_ = 0; 63 value_size_ = 0;
69 return false; 64 return false;
70 } 65 }
71 66
72 DWORD RegistryValueIterator::ValueCount() const { 67 DWORD RegistryValueIterator::ValueCount() const {
73 base::ThreadRestrictions::AssertIOAllowed();
74 DWORD count = 0; 68 DWORD count = 0;
75 HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, 69 HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL,
76 &count, NULL, NULL, NULL, NULL); 70 &count, NULL, NULL, NULL, NULL);
77 71
78 if (result != ERROR_SUCCESS) 72 if (result != ERROR_SUCCESS)
79 return 0; 73 return 0;
80 74
81 return count; 75 return count;
82 } 76 }
83 77
84 RegistryKeyIterator::RegistryKeyIterator(HKEY root_key, 78 RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
85 const wchar_t* folder_key) { 79 const wchar_t* folder_key) {
86 base::ThreadRestrictions::AssertIOAllowed();
87 LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); 80 LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_);
88 if (result != ERROR_SUCCESS) { 81 if (result != ERROR_SUCCESS) {
89 key_ = NULL; 82 key_ = NULL;
90 } else { 83 } else {
91 DWORD count = 0; 84 DWORD count = 0;
92 HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL, 85 HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL,
93 NULL, NULL, NULL, NULL, NULL); 86 NULL, NULL, NULL, NULL, NULL);
94 87
95 if (result != ERROR_SUCCESS) { 88 if (result != ERROR_SUCCESS) {
96 ::RegCloseKey(key_); 89 ::RegCloseKey(key_);
97 key_ = NULL; 90 key_ = NULL;
98 } else { 91 } else {
99 index_ = count - 1; 92 index_ = count - 1;
100 } 93 }
101 } 94 }
102 95
103 Read(); 96 Read();
104 } 97 }
105 98
106 RegistryKeyIterator::~RegistryKeyIterator() { 99 RegistryKeyIterator::~RegistryKeyIterator() {
107 base::ThreadRestrictions::AssertIOAllowed();
108 if (key_) 100 if (key_)
109 ::RegCloseKey(key_); 101 ::RegCloseKey(key_);
110 } 102 }
111 103
112 bool RegistryKeyIterator::Valid() const { 104 bool RegistryKeyIterator::Valid() const {
113 return key_ != NULL && index_ >= 0; 105 return key_ != NULL && index_ >= 0;
114 } 106 }
115 107
116 void RegistryKeyIterator::operator++() { 108 void RegistryKeyIterator::operator++() {
117 --index_; 109 --index_;
118 Read(); 110 Read();
119 } 111 }
120 112
121 bool RegistryKeyIterator::Read() { 113 bool RegistryKeyIterator::Read() {
122 base::ThreadRestrictions::AssertIOAllowed();
123 if (Valid()) { 114 if (Valid()) {
124 DWORD ncount = arraysize(name_); 115 DWORD ncount = arraysize(name_);
125 FILETIME written; 116 FILETIME written;
126 LRESULT r = ::RegEnumKeyEx(key_, index_, name_, &ncount, NULL, NULL, 117 LRESULT r = ::RegEnumKeyEx(key_, index_, name_, &ncount, NULL, NULL,
127 NULL, &written); 118 NULL, &written);
128 if (ERROR_SUCCESS == r) 119 if (ERROR_SUCCESS == r)
129 return true; 120 return true;
130 } 121 }
131 122
132 name_[0] = '\0'; 123 name_[0] = '\0';
133 return false; 124 return false;
134 } 125 }
135 126
136 DWORD RegistryKeyIterator::SubkeyCount() const { 127 DWORD RegistryKeyIterator::SubkeyCount() const {
137 base::ThreadRestrictions::AssertIOAllowed();
138 DWORD count = 0; 128 DWORD count = 0;
139 HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL, 129 HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL,
140 NULL, NULL, NULL, NULL, NULL); 130 NULL, NULL, NULL, NULL, NULL);
141 131
142 if (result != ERROR_SUCCESS) 132 if (result != ERROR_SUCCESS)
143 return 0; 133 return 0;
144 134
145 return count; 135 return count;
146 } 136 }
147 137
148 RegKey::RegKey() 138 RegKey::RegKey()
149 : key_(NULL), 139 : key_(NULL),
150 watch_event_(0) { 140 watch_event_(0) {
151 } 141 }
152 142
153 RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access) 143 RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access)
154 : key_(NULL), 144 : key_(NULL),
155 watch_event_(0) { 145 watch_event_(0) {
156 base::ThreadRestrictions::AssertIOAllowed();
157 if (rootkey) { 146 if (rootkey) {
158 if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) 147 if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
159 Create(rootkey, subkey, access); 148 Create(rootkey, subkey, access);
160 else 149 else
161 Open(rootkey, subkey, access); 150 Open(rootkey, subkey, access);
162 } else { 151 } else {
163 DCHECK(!subkey); 152 DCHECK(!subkey);
164 } 153 }
165 } 154 }
166 155
167 RegKey::~RegKey() { 156 RegKey::~RegKey() {
168 Close(); 157 Close();
169 } 158 }
170 159
171 void RegKey::Close() { 160 void RegKey::Close() {
172 base::ThreadRestrictions::AssertIOAllowed();
173 StopWatching(); 161 StopWatching();
174 if (key_) { 162 if (key_) {
175 ::RegCloseKey(key_); 163 ::RegCloseKey(key_);
176 key_ = NULL; 164 key_ = NULL;
177 } 165 }
178 } 166 }
179 167
180 bool RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) { 168 bool RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
181 DWORD disposition_value; 169 DWORD disposition_value;
182 return CreateWithDisposition(rootkey, subkey, &disposition_value, access); 170 return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
183 } 171 }
184 172
185 bool RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, 173 bool RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
186 DWORD* disposition, REGSAM access) { 174 DWORD* disposition, REGSAM access) {
187 base::ThreadRestrictions::AssertIOAllowed();
188 DCHECK(rootkey && subkey && access && disposition); 175 DCHECK(rootkey && subkey && access && disposition);
189 Close(); 176 Close();
190 177
191 LONG result = RegCreateKeyEx(rootkey, 178 LONG result = RegCreateKeyEx(rootkey,
192 subkey, 179 subkey,
193 0, 180 0,
194 NULL, 181 NULL,
195 REG_OPTION_NON_VOLATILE, 182 REG_OPTION_NON_VOLATILE,
196 access, 183 access,
197 NULL, 184 NULL,
198 &key_, 185 &key_,
199 disposition); 186 disposition);
200 if (result != ERROR_SUCCESS) { 187 if (result != ERROR_SUCCESS) {
201 key_ = NULL; 188 key_ = NULL;
202 return false; 189 return false;
203 } 190 }
204 191
205 return true; 192 return true;
206 } 193 }
207 194
208 bool RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { 195 bool RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
209 base::ThreadRestrictions::AssertIOAllowed();
210 DCHECK(rootkey && subkey && access); 196 DCHECK(rootkey && subkey && access);
211 Close(); 197 Close();
212 198
213 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_); 199 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_);
214 if (result != ERROR_SUCCESS) { 200 if (result != ERROR_SUCCESS) {
215 key_ = NULL; 201 key_ = NULL;
216 return false; 202 return false;
217 } 203 }
218 return true; 204 return true;
219 } 205 }
220 206
221 bool RegKey::CreateKey(const wchar_t* name, REGSAM access) { 207 bool RegKey::CreateKey(const wchar_t* name, REGSAM access) {
222 base::ThreadRestrictions::AssertIOAllowed();
223 DCHECK(name && access); 208 DCHECK(name && access);
224 209
225 HKEY subkey = NULL; 210 HKEY subkey = NULL;
226 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, 211 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE,
227 access, NULL, &subkey, NULL); 212 access, NULL, &subkey, NULL);
228 Close(); 213 Close();
229 214
230 key_ = subkey; 215 key_ = subkey;
231 return (result == ERROR_SUCCESS); 216 return (result == ERROR_SUCCESS);
232 } 217 }
233 218
234 bool RegKey::OpenKey(const wchar_t* name, REGSAM access) { 219 bool RegKey::OpenKey(const wchar_t* name, REGSAM access) {
235 base::ThreadRestrictions::AssertIOAllowed();
236 DCHECK(name && access); 220 DCHECK(name && access);
237 221
238 HKEY subkey = NULL; 222 HKEY subkey = NULL;
239 LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey); 223 LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey);
240 224
241 Close(); 225 Close();
242 226
243 key_ = subkey; 227 key_ = subkey;
244 return (result == ERROR_SUCCESS); 228 return (result == ERROR_SUCCESS);
245 } 229 }
246 230
247 DWORD RegKey::ValueCount() { 231 DWORD RegKey::ValueCount() {
248 base::ThreadRestrictions::AssertIOAllowed();
249 DWORD count = 0; 232 DWORD count = 0;
250 HRESULT result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, 233 HRESULT result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL,
251 NULL, &count, NULL, NULL, NULL, NULL); 234 NULL, &count, NULL, NULL, NULL, NULL);
252 return (result != ERROR_SUCCESS) ? 0 : count; 235 return (result != ERROR_SUCCESS) ? 0 : count;
253 } 236 }
254 237
255 bool RegKey::ReadName(int index, std::wstring* name) { 238 bool RegKey::ReadName(int index, std::wstring* name) {
256 base::ThreadRestrictions::AssertIOAllowed();
257 wchar_t buf[256]; 239 wchar_t buf[256];
258 DWORD bufsize = arraysize(buf); 240 DWORD bufsize = arraysize(buf);
259 LRESULT r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, 241 LRESULT r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL,
260 NULL, NULL); 242 NULL, NULL);
261 if (r != ERROR_SUCCESS) 243 if (r != ERROR_SUCCESS)
262 return false; 244 return false;
263 if (name) 245 if (name)
264 *name = buf; 246 *name = buf;
265 return true; 247 return true;
266 } 248 }
267 249
268 bool RegKey::ValueExists(const wchar_t* name) { 250 bool RegKey::ValueExists(const wchar_t* name) {
269 base::ThreadRestrictions::AssertIOAllowed();
270 if (!key_) 251 if (!key_)
271 return false; 252 return false;
272 HRESULT result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL); 253 HRESULT result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL);
273 return (result == ERROR_SUCCESS); 254 return (result == ERROR_SUCCESS);
274 } 255 }
275 256
276 bool RegKey::ReadValue(const wchar_t* name, void* data, 257 bool RegKey::ReadValue(const wchar_t* name, void* data,
277 DWORD* dsize, DWORD* dtype) { 258 DWORD* dsize, DWORD* dtype) {
278 base::ThreadRestrictions::AssertIOAllowed();
279 if (!key_) 259 if (!key_)
280 return false; 260 return false;
281 HRESULT result = RegQueryValueEx(key_, name, 0, dtype, 261 HRESULT result = RegQueryValueEx(key_, name, 0, dtype,
282 reinterpret_cast<LPBYTE>(data), dsize); 262 reinterpret_cast<LPBYTE>(data), dsize);
283 return (result == ERROR_SUCCESS); 263 return (result == ERROR_SUCCESS);
284 } 264 }
285 265
286 bool RegKey::ReadValue(const wchar_t* name, std::wstring* value) { 266 bool RegKey::ReadValue(const wchar_t* name, std::wstring* value) {
287 base::ThreadRestrictions::AssertIOAllowed();
288 DCHECK(value); 267 DCHECK(value);
289 const size_t kMaxStringLength = 1024; // This is after expansion. 268 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. 269 // Use the one of the other forms of ReadValue if 1024 is too small for you.
291 wchar_t raw_value[kMaxStringLength]; 270 wchar_t raw_value[kMaxStringLength];
292 DWORD type = REG_SZ, size = sizeof(raw_value); 271 DWORD type = REG_SZ, size = sizeof(raw_value);
293 if (ReadValue(name, raw_value, &size, &type)) { 272 if (ReadValue(name, raw_value, &size, &type)) {
294 if (type == REG_SZ) { 273 if (type == REG_SZ) {
295 *value = raw_value; 274 *value = raw_value;
296 } else if (type == REG_EXPAND_SZ) { 275 } else if (type == REG_EXPAND_SZ) {
297 wchar_t expanded[kMaxStringLength]; 276 wchar_t expanded[kMaxStringLength];
(...skipping 24 matching lines...) Expand all
322 size == sizeof(DWORD)) { 301 size == sizeof(DWORD)) {
323 *value = result; 302 *value = result;
324 return true; 303 return true;
325 } 304 }
326 305
327 return false; 306 return false;
328 } 307 }
329 308
330 bool RegKey::WriteValue(const wchar_t* name, const void * data, 309 bool RegKey::WriteValue(const wchar_t* name, const void * data,
331 DWORD dsize, DWORD dtype) { 310 DWORD dsize, DWORD dtype) {
332 base::ThreadRestrictions::AssertIOAllowed();
333 DCHECK(data); 311 DCHECK(data);
334 312
335 if (!key_) 313 if (!key_)
336 return false; 314 return false;
337 315
338 HRESULT result = RegSetValueEx( 316 HRESULT result = RegSetValueEx(
339 key_, 317 key_,
340 name, 318 name,
341 0, 319 0,
342 dtype, 320 dtype,
343 reinterpret_cast<LPBYTE>(const_cast<void*>(data)), 321 reinterpret_cast<LPBYTE>(const_cast<void*>(data)),
344 dsize); 322 dsize);
345 return (result == ERROR_SUCCESS); 323 return (result == ERROR_SUCCESS);
346 } 324 }
347 325
348 bool RegKey::WriteValue(const wchar_t * name, const wchar_t* value) { 326 bool RegKey::WriteValue(const wchar_t * name, const wchar_t* value) {
349 return WriteValue(name, value, 327 return WriteValue(name, value,
350 static_cast<DWORD>(sizeof(*value) * (wcslen(value) + 1)), REG_SZ); 328 static_cast<DWORD>(sizeof(*value) * (wcslen(value) + 1)), REG_SZ);
351 } 329 }
352 330
353 bool RegKey::WriteValue(const wchar_t* name, DWORD value) { 331 bool RegKey::WriteValue(const wchar_t* name, DWORD value) {
354 return WriteValue(name, &value, 332 return WriteValue(name, &value,
355 static_cast<DWORD>(sizeof(value)), REG_DWORD); 333 static_cast<DWORD>(sizeof(value)), REG_DWORD);
356 } 334 }
357 335
358 bool RegKey::DeleteKey(const wchar_t* name) { 336 bool RegKey::DeleteKey(const wchar_t* name) {
359 base::ThreadRestrictions::AssertIOAllowed();
360 return (!key_) ? false : (ERROR_SUCCESS == SHDeleteKey(key_, name)); 337 return (!key_) ? false : (ERROR_SUCCESS == SHDeleteKey(key_, name));
361 } 338 }
362 339
363 bool RegKey::DeleteValue(const wchar_t* value_name) { 340 bool RegKey::DeleteValue(const wchar_t* value_name) {
364 base::ThreadRestrictions::AssertIOAllowed();
365 DCHECK(value_name); 341 DCHECK(value_name);
366 HRESULT result = RegDeleteValue(key_, value_name); 342 HRESULT result = RegDeleteValue(key_, value_name);
367 return (result == ERROR_SUCCESS); 343 return (result == ERROR_SUCCESS);
368 } 344 }
369 345
370 bool RegKey::StartWatching() { 346 bool RegKey::StartWatching() {
371 if (!watch_event_) 347 if (!watch_event_)
372 watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); 348 watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL);
373 349
374 DWORD filter = REG_NOTIFY_CHANGE_NAME | 350 DWORD filter = REG_NOTIFY_CHANGE_NAME |
(...skipping 27 matching lines...) Expand all
402 if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) { 378 if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) {
403 StartWatching(); 379 StartWatching();
404 return true; 380 return true;
405 } 381 }
406 } 382 }
407 return false; 383 return false;
408 } 384 }
409 385
410 } // namespace win 386 } // namespace win
411 } // namespace base 387 } // namespace base
OLDNEW
« no previous file with comments | « base/platform_file_win.cc ('k') | base/win_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698