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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { | |
60 base::ThreadRestrictions::AssertIOAllowed(); | |
61 DCHECK(rootkey && subkey && access); | |
62 Close(); | |
63 | |
64 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_); | |
65 return result; | |
66 } | |
67 | |
68 LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) { | 59 LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) { |
69 base::ThreadRestrictions::AssertIOAllowed(); | 60 base::ThreadRestrictions::AssertIOAllowed(); |
70 DCHECK(name && access); | 61 DCHECK(name && access); |
71 | 62 |
72 HKEY subkey = NULL; | 63 HKEY subkey = NULL; |
73 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, | 64 LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, |
74 access, NULL, &subkey, NULL); | 65 access, NULL, &subkey, NULL); |
75 Close(); | 66 Close(); |
76 | 67 |
77 key_ = subkey; | 68 key_ = subkey; |
78 return result; | 69 return result; |
79 } | 70 } |
80 | 71 |
81 LONG RegKey::OpenKey(const wchar_t* name, REGSAM access) { | 72 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { |
82 base::ThreadRestrictions::AssertIOAllowed(); | 73 base::ThreadRestrictions::AssertIOAllowed(); |
83 DCHECK(name && access); | 74 DCHECK(rootkey && subkey && access); |
| 75 Close(); |
| 76 |
| 77 LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_); |
| 78 return result; |
| 79 } |
| 80 |
| 81 LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) { |
| 82 base::ThreadRestrictions::AssertIOAllowed(); |
| 83 DCHECK(relative_key_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_, relative_key_name, 0, access, &subkey); |
87 | 87 |
| 88 // We have to close the current opened key before replacing it with the new |
| 89 // one. |
88 Close(); | 90 Close(); |
89 | 91 |
90 key_ = subkey; | 92 key_ = subkey; |
91 return result; | 93 return result; |
92 } | 94 } |
93 | 95 |
94 void RegKey::Close() { | 96 void RegKey::Close() { |
95 base::ThreadRestrictions::AssertIOAllowed(); | 97 base::ThreadRestrictions::AssertIOAllowed(); |
96 StopWatching(); | 98 StopWatching(); |
97 if (key_) { | 99 if (key_) { |
98 ::RegCloseKey(key_); | 100 ::RegCloseKey(key_); |
99 key_ = NULL; | 101 key_ = NULL; |
100 } | 102 } |
101 } | 103 } |
102 | 104 |
103 DWORD RegKey::ValueCount() const { | 105 bool RegKey::HasValue(const wchar_t* name) const { |
| 106 base::ThreadRestrictions::AssertIOAllowed(); |
| 107 return RegQueryValueEx(key_, name, 0, NULL, NULL, NULL) == ERROR_SUCCESS; |
| 108 } |
| 109 |
| 110 DWORD RegKey::GetValueCount() const { |
104 base::ThreadRestrictions::AssertIOAllowed(); | 111 base::ThreadRestrictions::AssertIOAllowed(); |
105 DWORD count = 0; | 112 DWORD count = 0; |
106 LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, | 113 LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, |
107 NULL, NULL, NULL, NULL); | 114 NULL, NULL, NULL, NULL); |
108 return (result != ERROR_SUCCESS) ? 0 : count; | 115 return (result == ERROR_SUCCESS) ? count : 0; |
109 } | 116 } |
110 | 117 |
111 LONG RegKey::ReadName(int index, std::wstring* name) const { | 118 LONG RegKey::GetValueNameAt(int index, std::wstring* name) const { |
112 base::ThreadRestrictions::AssertIOAllowed(); | 119 base::ThreadRestrictions::AssertIOAllowed(); |
113 wchar_t buf[256]; | 120 wchar_t buf[256]; |
114 DWORD bufsize = arraysize(buf); | 121 DWORD bufsize = arraysize(buf); |
115 LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL); | 122 LONG r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, NULL, NULL); |
116 if (r == ERROR_SUCCESS) | 123 if (r == ERROR_SUCCESS) |
117 *name = buf; | 124 *name = buf; |
118 | 125 |
119 return r; | 126 return r; |
120 } | 127 } |
121 | 128 |
122 LONG RegKey::DeleteKey(const wchar_t* name) { | 129 LONG RegKey::DeleteKey(const wchar_t* name) { |
123 base::ThreadRestrictions::AssertIOAllowed(); | 130 base::ThreadRestrictions::AssertIOAllowed(); |
124 DCHECK(key_); | 131 DCHECK(key_); |
125 DCHECK(name); | 132 DCHECK(name); |
126 LONG result = SHDeleteKey(key_, name); | 133 LONG result = SHDeleteKey(key_, name); |
127 return result; | 134 return result; |
128 } | 135 } |
129 | 136 |
130 LONG RegKey::DeleteValue(const wchar_t* value_name) { | 137 LONG RegKey::DeleteValue(const wchar_t* value_name) { |
131 base::ThreadRestrictions::AssertIOAllowed(); | 138 base::ThreadRestrictions::AssertIOAllowed(); |
132 DCHECK(key_); | 139 DCHECK(key_); |
133 DCHECK(value_name); | 140 DCHECK(value_name); |
134 LONG result = RegDeleteValue(key_, value_name); | 141 LONG result = RegDeleteValue(key_, value_name); |
135 return result; | 142 return result; |
136 } | 143 } |
137 | 144 |
138 bool RegKey::ValueExists(const wchar_t* name) const { | |
139 base::ThreadRestrictions::AssertIOAllowed(); | |
140 LONG result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL); | |
141 return result == ERROR_SUCCESS; | |
142 } | |
143 | |
144 LONG RegKey::ReadValue(const wchar_t* name, void* data, DWORD* dsize, | 145 LONG RegKey::ReadValue(const wchar_t* name, void* data, DWORD* dsize, |
145 DWORD* dtype) const { | 146 DWORD* dtype) const { |
146 base::ThreadRestrictions::AssertIOAllowed(); | 147 base::ThreadRestrictions::AssertIOAllowed(); |
147 LONG result = RegQueryValueEx(key_, name, 0, dtype, | 148 LONG result = RegQueryValueEx(key_, name, 0, dtype, |
148 reinterpret_cast<LPBYTE>(data), dsize); | 149 reinterpret_cast<LPBYTE>(data), dsize); |
149 return result; | 150 return result; |
150 } | 151 } |
151 | 152 |
152 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* value) const { | 153 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* value) const { |
153 base::ThreadRestrictions::AssertIOAllowed(); | 154 base::ThreadRestrictions::AssertIOAllowed(); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 if (ERROR_SUCCESS == r) | 403 if (ERROR_SUCCESS == r) |
403 return true; | 404 return true; |
404 } | 405 } |
405 | 406 |
406 name_[0] = '\0'; | 407 name_[0] = '\0'; |
407 return false; | 408 return false; |
408 } | 409 } |
409 | 410 |
410 } // namespace win | 411 } // namespace win |
411 } // namespace base | 412 } // namespace base |
OLD | NEW |