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

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

Issue 275103012: Add WOW64 support and DeleteEmptyKey to base::win::registry. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: code review changes Created 6 years, 7 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include <algorithm> 8 #include <algorithm>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_restrictions.h"
13 13 #include "base/win/windows_version.h"
14 #pragma comment(lib, "shlwapi.lib") // for SHDeleteKey
15 14
16 namespace base { 15 namespace base {
17 namespace win { 16 namespace win {
18 17
19 namespace { 18 namespace {
20 19
21 // RegEnumValue() reports the number of characters from the name that were 20 // RegEnumValue() reports the number of characters from the name that were
22 // written to the buffer, not how many there are. This constant is the maximum 21 // written to the buffer, not how many there are. This constant is the maximum
23 // name size, such that a buffer with this size should read any name. 22 // name size, such that a buffer with this size should read any name.
24 const DWORD MAX_REGISTRY_NAME_SIZE = 16384; 23 const DWORD MAX_REGISTRY_NAME_SIZE = 16384;
25 24
26 // Registry values are read as BYTE* but can have wchar_t* data whose last 25 // Registry values are read as BYTE* but can have wchar_t* data whose last
27 // wchar_t is truncated. This function converts the reported |byte_size| to 26 // wchar_t is truncated. This function converts the reported |byte_size| to
28 // a size in wchar_t that can store a truncated wchar_t if necessary. 27 // a size in wchar_t that can store a truncated wchar_t if necessary.
29 inline DWORD to_wchar_size(DWORD byte_size) { 28 inline DWORD to_wchar_size(DWORD byte_size) {
30 return (byte_size + sizeof(wchar_t) - 1) / sizeof(wchar_t); 29 return (byte_size + sizeof(wchar_t) - 1) / sizeof(wchar_t);
31 } 30 }
32 31
32 // Mask to pull WOW64 access flags out of REGSAM access.
33 const REGSAM kWow64AccessMask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
34
33 } // namespace 35 } // namespace
34 36
35 // RegKey ---------------------------------------------------------------------- 37 // RegKey ----------------------------------------------------------------------
36 38
37 RegKey::RegKey() 39 RegKey::RegKey()
38 : key_(NULL), 40 : key_(NULL),
39 watch_event_(0) { 41 watch_event_(0) {
grt (UTC plus 2) 2014/05/17 12:54:33 wow64access_(0)
Will Harris 2014/05/19 21:49:03 Done.
40 } 42 }
41 43
42 RegKey::RegKey(HKEY key) 44 RegKey::RegKey(HKEY key)
43 : key_(key), 45 : key_(key),
44 watch_event_(0) { 46 watch_event_(0) {
grt (UTC plus 2) 2014/05/17 12:54:33 this ctor needs to take the wow64 bits of |key| as
Will Harris 2014/05/19 21:49:03 HEKY is not RegKey - no way to change/modify acces
45 } 47 }
46 48
47 RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access) 49 RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access)
grt (UTC plus 2) 2014/05/17 12:54:33 wow64access_(0) what does it mean for rootkey and
Will Harris 2014/05/19 21:49:03 yes, good spot, I looked through the code and can'
48 : key_(NULL), 50 : key_(NULL),
49 watch_event_(0) { 51 watch_event_(0) {
50 if (rootkey) { 52 if (rootkey) {
51 if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) 53 if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
52 Create(rootkey, subkey, access); 54 Create(rootkey, subkey, access);
53 else 55 else
54 Open(rootkey, subkey, access); 56 Open(rootkey, subkey, access);
55 } else { 57 } else {
56 DCHECK(!subkey); 58 DCHECK(!subkey);
57 } 59 }
(...skipping 11 matching lines...) Expand all
69 LONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, 71 LONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
70 DWORD* disposition, REGSAM access) { 72 DWORD* disposition, REGSAM access) {
71 DCHECK(rootkey && subkey && access && disposition); 73 DCHECK(rootkey && subkey && access && disposition);
72 HKEY subhkey = NULL; 74 HKEY subhkey = NULL;
73 LONG result = RegCreateKeyEx(rootkey, subkey, 0, NULL, 75 LONG result = RegCreateKeyEx(rootkey, subkey, 0, NULL,
74 REG_OPTION_NON_VOLATILE, access, NULL, &subhkey, 76 REG_OPTION_NON_VOLATILE, access, NULL, &subhkey,
75 disposition); 77 disposition);
76 if (result == ERROR_SUCCESS) { 78 if (result == ERROR_SUCCESS) {
77 Close(); 79 Close();
78 key_ = subhkey; 80 key_ = subhkey;
81 wow64access_ = access & kWow64AccessMask;
79 } 82 }
80 83
81 return result; 84 return result;
82 } 85 }
83 86
84 LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) { 87 LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) {
grt (UTC plus 2) 2014/05/17 12:54:33 msdn says "After the application has accessed an a
cpu_(ooo_6.6-7.5) 2014/05/19 19:21:13 very good catch.
Will Harris 2014/05/19 21:49:03 Done.
85 DCHECK(name && access); 88 DCHECK(name && access);
86 HKEY subkey = NULL; 89 HKEY subkey = NULL;
87 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, 90 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE,
88 access, NULL, &subkey, NULL); 91 access, NULL, &subkey, NULL);
89 if (result == ERROR_SUCCESS) { 92 if (result == ERROR_SUCCESS) {
90 Close(); 93 Close();
91 94 key_ = subkey;
92 key_ = subkey; 95 wow64access_ = access & kWow64AccessMask;
93 } 96 }
94 97
95 return result; 98 return result;
96 } 99 }
97 100
98 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { 101 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
99 DCHECK(rootkey && subkey && access); 102 DCHECK(rootkey && subkey && access);
100 HKEY subhkey = NULL; 103 HKEY subhkey = NULL;
101 104
102 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &subhkey); 105 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &subhkey);
103 if (result == ERROR_SUCCESS) { 106 if (result == ERROR_SUCCESS) {
104 Close(); 107 Close();
105 key_ = subhkey; 108 key_ = subhkey;
109 wow64access_ = access & kWow64AccessMask;
106 } 110 }
107 111
108 return result; 112 return result;
109 } 113 }
110 114
111 LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) { 115 LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) {
grt (UTC plus 2) 2014/05/17 12:54:33 same comment here about enforcing that |access| co
Will Harris 2014/05/19 21:49:03 Done.
112 DCHECK(relative_key_name && access); 116 DCHECK(relative_key_name && access);
113 HKEY subkey = NULL; 117 HKEY subkey = NULL;
114 LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey); 118 LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey);
115 119
116 // We have to close the current opened key before replacing it with the new 120 // We have to close the current opened key before replacing it with the new
117 // one. 121 // one.
118 if (result == ERROR_SUCCESS) { 122 if (result == ERROR_SUCCESS) {
119 Close(); 123 Close();
120 key_ = subkey; 124 key_ = subkey;
125 wow64access_ = access & kWow64AccessMask;
121 } 126 }
122 return result; 127 return result;
123 } 128 }
124 129
125 void RegKey::Close() { 130 void RegKey::Close() {
126 StopWatching(); 131 StopWatching();
127 if (key_) { 132 if (key_) {
128 ::RegCloseKey(key_); 133 ::RegCloseKey(key_);
129 key_ = NULL; 134 key_ = NULL;
grt (UTC plus 2) 2014/05/17 12:54:33 wow64access_ = 0;
Will Harris 2014/05/19 21:49:03 Done.
130 } 135 }
131 } 136 }
132 137
133 void RegKey::Set(HKEY key) { 138 void RegKey::Set(HKEY key) {
grt (UTC plus 2) 2014/05/17 12:54:33 this needs to take a REGSAM for the wow64 bits, no
Will Harris 2014/05/19 21:49:03 it's not possible to check that key was opened wit
134 if (key_ != key) { 139 if (key_ != key) {
135 Close(); 140 Close();
136 key_ = key; 141 key_ = key;
137 } 142 }
138 } 143 }
139 144
140 HKEY RegKey::Take() { 145 HKEY RegKey::Take() {
141 StopWatching(); 146 StopWatching();
142 HKEY key = key_; 147 HKEY key = key_;
143 key_ = NULL; 148 key_ = NULL;
grt (UTC plus 2) 2014/05/17 12:54:33 wow64access_ = 0;
Will Harris 2014/05/19 21:49:03 Done.
144 return key; 149 return key;
145 } 150 }
146 151
147 bool RegKey::HasValue(const wchar_t* name) const { 152 bool RegKey::HasValue(const wchar_t* name) const {
148 return RegQueryValueEx(key_, name, 0, NULL, NULL, NULL) == ERROR_SUCCESS; 153 return RegQueryValueEx(key_, name, 0, NULL, NULL, NULL) == ERROR_SUCCESS;
149 } 154 }
150 155
151 DWORD RegKey::GetValueCount() const { 156 DWORD RegKey::GetValueCount() const {
152 DWORD count = 0; 157 DWORD count = 0;
153 LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, 158 LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count,
154 NULL, NULL, NULL, NULL); 159 NULL, NULL, NULL, NULL);
155 return (result == ERROR_SUCCESS) ? count : 0; 160 return (result == ERROR_SUCCESS) ? count : 0;
156 } 161 }
157 162
158 LONG RegKey::GetValueNameAt(int index, std::wstring* name) const { 163 LONG RegKey::GetValueNameAt(int index, std::wstring* name) const {
159 wchar_t buf[256]; 164 wchar_t buf[256];
160 DWORD bufsize = arraysize(buf); 165 DWORD bufsize = arraysize(buf);
161 LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL); 166 LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL);
162 if (r == ERROR_SUCCESS) 167 if (r == ERROR_SUCCESS)
163 *name = buf; 168 *name = buf;
164 169
165 return r; 170 return r;
166 } 171 }
167 172
168 LONG RegKey::DeleteKey(const wchar_t* name) { 173 LONG RegKey::DeleteKey(const wchar_t* name) {
169 DCHECK(key_); 174 DCHECK(key_);
170 DCHECK(name); 175 DCHECK(name);
171 LONG result = SHDeleteKey(key_, name); 176 HKEY subkey;
172 return result; 177
178 // Verify the key exists before attempting delete to replicate previous
179 // behavior.
180 LONG result =
181 RegOpenKeyEx(key_, name, 0, READ_CONTROL | wow64access_, &subkey);
182 if (result != ERROR_SUCCESS)
183 return result;
184 RegCloseKey(subkey);
185 std::wstring keyname(name);
grt (UTC plus 2) 2014/05/17 12:54:33 nit: put this inline with the recursive call: re
Will Harris 2014/05/19 21:49:03 Done.
186
187 return RegDelRecurse(key_, keyname, wow64access_);
173 } 188 }
174 189
175 LONG RegKey::DeleteValue(const wchar_t* value_name) { 190 LONG RegKey::DeleteValue(const wchar_t* value_name) {
176 DCHECK(key_); 191 DCHECK(key_);
177 LONG result = RegDeleteValue(key_, value_name); 192 LONG result = RegDeleteValue(key_, value_name);
178 return result; 193 return result;
179 } 194 }
180 195
181 LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* out_value) const { 196 LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* out_value) const {
182 DCHECK(out_value); 197 DCHECK(out_value);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 LONG RegKey::StopWatching() { 350 LONG RegKey::StopWatching() {
336 LONG result = ERROR_INVALID_HANDLE; 351 LONG result = ERROR_INVALID_HANDLE;
337 if (watch_event_) { 352 if (watch_event_) {
338 CloseHandle(watch_event_); 353 CloseHandle(watch_event_);
339 watch_event_ = 0; 354 watch_event_ = 0;
340 result = ERROR_SUCCESS; 355 result = ERROR_SUCCESS;
341 } 356 }
342 return result; 357 return result;
343 } 358 }
344 359
360 // static
361 LONG RegKey::RegDeleteKeyExWrapper(HKEY hKey,
362 const wchar_t* lpSubKey,
363 REGSAM samDesired,
364 DWORD Reserved) {
365 typedef LSTATUS(WINAPI * RegDeleteKeyExPtr)(
grt (UTC plus 2) 2014/05/17 12:54:33 WINAPI*
Will Harris 2014/05/19 21:49:03 this was git gl format :)
grt (UTC plus 2) 2014/05/20 15:28:52 Oh. How 'bout that. May as well do as it says, alt
366 HKEY hKey, LPCWSTR lpSubKey, REGSAM samDesired, DWORD Reserved);
grt (UTC plus 2) 2014/05/17 12:54:33 nit: remove the parameter names
Will Harris 2014/05/19 21:49:03 Done.
367
368 RegDeleteKeyExPtr reg_delete_key_ex_func =
369 reinterpret_cast<RegDeleteKeyExPtr>(
370 GetProcAddress(GetModuleHandleA("advapi32.dll"), "RegDeleteKeyExW"));
371
372 if (reg_delete_key_ex_func)
373 return reg_delete_key_ex_func(hKey, lpSubKey, samDesired, Reserved);
374
375 // Windows XP does not support RegDeleteKeyEx, so fallback to RegDeleteKey.
376 return RegDeleteKey(hKey, lpSubKey);
377 }
378
379 // static
380 LONG RegKey::RegDelRecurse(HKEY root_key,
381 std::wstring const& name,
grt (UTC plus 2) 2014/05/17 12:54:33 const std::wstring&
Will Harris 2014/05/19 21:49:03 Done.
382 REGSAM access) {
383 LONG result;
384 HKEY target_key;
385
386 size_t name_length = name.length();
grt (UTC plus 2) 2014/05/17 12:54:33 remove this and change line 388 to if (name.empty(
Will Harris 2014/05/19 21:49:03 Done.
387
388 if (name_length == 0) {
389 NOTREACHED();
390 return ERROR_INVALID_PARAMETER;
391 }
392
393 // First, see if the key can be deleted without having to recurse.
394 result = RegDeleteKeyExWrapper(root_key, name.c_str(), access, 0);
grt (UTC plus 2) 2014/05/17 12:54:33 the style guide says to define local vars "as clos
Will Harris 2014/05/19 21:49:03 Done.
395
396 if (result == ERROR_SUCCESS)
397 return result;
398
399 result = RegOpenKeyEx(
grt (UTC plus 2) 2014/05/17 12:54:33 put the definition of target_key just above this l
Will Harris 2014/05/19 21:49:03 Done.
400 root_key, name.c_str(), 0, KEY_ENUMERATE_SUB_KEYS | access, &target_key);
401
402 if (result == ERROR_FILE_NOT_FOUND)
403 return ERROR_SUCCESS;
404 if (result != ERROR_SUCCESS)
405 return result;
406
407 // Copy string before modifying it.
408 std::wstring subkey_name(name);
409
410 // Check for an ending slash and add one if it is missing.
411 if (subkey_name[name_length - 1] != L'\\') {
412 name_length++;
413 subkey_name += L"\\";
414 }
415
416 // Enumerate the keys
417 result = ERROR_SUCCESS;
418 DWORD kMaxKeyNameLength = MAX_PATH;
grt (UTC plus 2) 2014/05/17 12:54:33 const DWORD
Will Harris 2014/05/19 21:49:03 Done.
419
grt (UTC plus 2) 2014/05/17 12:54:33 define this here: const size_t base_key_lenght =
Will Harris 2014/05/19 21:49:03 Done.
420 while (result == ERROR_SUCCESS) {
421 std::wstring key_name;
grt (UTC plus 2) 2014/05/17 12:54:33 move the definition of key_name out of the loop.
Will Harris 2014/05/19 21:49:03 Done.
422 DWORD key_size = kMaxKeyNameLength;
423 result = RegEnumKeyEx(target_key,
424 0,
425 WriteInto(&key_name, kMaxKeyNameLength),
426 &key_size,
427 NULL,
428 NULL,
429 NULL,
430 NULL);
431
432 if (result != ERROR_SUCCESS)
433 break;
434
435 key_name.resize(key_size);
436 subkey_name.resize(name_length);
grt (UTC plus 2) 2014/05/17 12:54:33 name_length -> base_key_lenght
Will Harris 2014/05/19 21:49:03 Done.
437 subkey_name += key_name;
438
439 if (RegDelRecurse(root_key, subkey_name, access) != ERROR_SUCCESS)
440 break;
441 }
442
443 RegCloseKey(target_key);
444
445 // Try again to delete the key.
446 result = RegDeleteKeyExWrapper(root_key, name.c_str(), access, 0);
447
448 return result;
449 }
450
345 // RegistryValueIterator ------------------------------------------------------ 451 // RegistryValueIterator ------------------------------------------------------
346 452
347 RegistryValueIterator::RegistryValueIterator(HKEY root_key, 453 RegistryValueIterator::RegistryValueIterator(HKEY root_key,
348 const wchar_t* folder_key) 454 const wchar_t* folder_key)
349 : name_(MAX_PATH, L'\0'), 455 : name_(MAX_PATH, L'\0'),
350 value_(MAX_PATH, L'\0') { 456 value_(MAX_PATH, L'\0') {
351 LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); 457 LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_);
352 if (result != ERROR_SUCCESS) { 458 if (result != ERROR_SUCCESS) {
353 key_ = NULL; 459 key_ = NULL;
354 } else { 460 } else {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 if (ERROR_SUCCESS == r) 593 if (ERROR_SUCCESS == r)
488 return true; 594 return true;
489 } 595 }
490 596
491 name_[0] = '\0'; 597 name_[0] = '\0';
492 return false; 598 return false;
493 } 599 }
494 600
495 } // namespace win 601 } // namespace win
496 } // namespace base 602 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698