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

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

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