summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authortfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-18 17:02:14 +0000
committertfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-18 17:02:14 +0000
commitac33674d8fe3a73a9cbf19807e999bd2bb787361 (patch)
tree73e0fbb054b38adc4d40c82224ecc64400c85257 /base
parent6b073e60cf5e57e8ea1952787a32be3a21e66496 (diff)
downloadchromium_src-ac33674d8fe3a73a9cbf19807e999bd2bb787361.zip
chromium_src-ac33674d8fe3a73a9cbf19807e999bd2bb787361.tar.gz
chromium_src-ac33674d8fe3a73a9cbf19807e999bd2bb787361.tar.bz2
base/win: small improvement to RegKey API.
- Rename ValueExists() to HasValue(). - Rename ValueCount() to GetValueCount(). - Rename ReadName() to GetValueNameAt(). R=maruel@chromium.org,willchan@chromium.org Review URL: http://codereview.chromium.org/8337006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106074 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/win/registry.cc43
-rw-r--r--base/win/registry.h22
-rw-r--r--base/win/registry_unittest.cc18
3 files changed, 45 insertions, 38 deletions
diff --git a/base/win/registry.cc b/base/win/registry.cc
index d105a4c..fa8f422 100644
--- a/base/win/registry.cc
+++ b/base/win/registry.cc
@@ -56,15 +56,6 @@ LONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
return result;
}
-LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
- base::ThreadRestrictions::AssertIOAllowed();
- DCHECK(rootkey && subkey && access);
- Close();
-
- LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_);
- return result;
-}
-
LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) {
base::ThreadRestrictions::AssertIOAllowed();
DCHECK(name && access);
@@ -78,13 +69,24 @@ LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) {
return result;
}
-LONG RegKey::OpenKey(const wchar_t* name, REGSAM access) {
+LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
base::ThreadRestrictions::AssertIOAllowed();
- DCHECK(name && access);
+ DCHECK(rootkey && subkey && access);
+ Close();
+
+ LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &key_);
+ return result;
+}
+
+LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ DCHECK(relative_key_name && access);
HKEY subkey = NULL;
- LONG result = RegOpenKeyEx(key_, name, 0, access, &subkey);
+ LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey);
+ // We have to close the current opened key before replacing it with the new
+ // one.
Close();
key_ = subkey;
@@ -100,15 +102,20 @@ void RegKey::Close() {
}
}
-DWORD RegKey::ValueCount() const {
+bool RegKey::HasValue(const wchar_t* name) const {
+ base::ThreadRestrictions::AssertIOAllowed();
+ return RegQueryValueEx(key_, name, 0, NULL, NULL, NULL) == ERROR_SUCCESS;
+}
+
+DWORD RegKey::GetValueCount() const {
base::ThreadRestrictions::AssertIOAllowed();
DWORD count = 0;
LONG result = RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count,
NULL, NULL, NULL, NULL);
- return (result != ERROR_SUCCESS) ? 0 : count;
+ return (result == ERROR_SUCCESS) ? count : 0;
}
-LONG RegKey::ReadName(int index, std::wstring* name) const {
+LONG RegKey::GetValueNameAt(int index, std::wstring* name) const {
base::ThreadRestrictions::AssertIOAllowed();
wchar_t buf[256];
DWORD bufsize = arraysize(buf);
@@ -135,12 +142,6 @@ LONG RegKey::DeleteValue(const wchar_t* value_name) {
return result;
}
-bool RegKey::ValueExists(const wchar_t* name) const {
- base::ThreadRestrictions::AssertIOAllowed();
- LONG result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL);
- return result == ERROR_SUCCESS;
-}
-
LONG RegKey::ReadValue(const wchar_t* name, void* data, DWORD* dsize,
DWORD* dtype) const {
base::ThreadRestrictions::AssertIOAllowed();
diff --git a/base/win/registry.h b/base/win/registry.h
index 078e6e0..33052ff 100644
--- a/base/win/registry.h
+++ b/base/win/registry.h
@@ -33,20 +33,28 @@ class BASE_EXPORT RegKey {
LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
DWORD* disposition, REGSAM access);
- LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access);
-
// Creates a subkey or open it if it already exists.
LONG CreateKey(const wchar_t* name, REGSAM access);
- // Opens a subkey
- LONG OpenKey(const wchar_t* name, REGSAM access);
+ // Opens an existing reg key.
+ LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access);
+
+ // Opens an existing reg key, given the relative key name.
+ LONG OpenKey(const wchar_t* relative_key_name, REGSAM access);
+ // Closes this reg key.
void Close();
- DWORD ValueCount() const;
+ // Returns false if this key does not have the specified value, of if an error
+ // occurrs while attempting to access it.
+ bool HasValue(const wchar_t* value_name) const;
+
+ // Returns the number of values for this key, of 0 if the number cannot be
+ // determined.
+ DWORD GetValueCount() const;
// Determine the nth value's name.
- LONG ReadName(int index, std::wstring* name) const;
+ LONG GetValueNameAt(int index, std::wstring* name) const;
// True while the key is valid.
bool Valid() const { return key_ != NULL; }
@@ -58,8 +66,6 @@ class BASE_EXPORT RegKey {
// Deletes a single value within the key.
LONG DeleteValue(const wchar_t* name);
- bool ValueExists(const wchar_t* name) const;
-
LONG ReadValue(const wchar_t* name, void* data, DWORD* dsize,
DWORD* dtype) const;
LONG ReadValue(const wchar_t* name, std::wstring* value) const;
diff --git a/base/win/registry_unittest.cc b/base/win/registry_unittest.cc
index a7961e9..1737786 100644
--- a/base/win/registry_unittest.cc
+++ b/base/win/registry_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -60,10 +60,10 @@ TEST_F(RegistryTest, ValueTest) {
ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kDWORDValueName, kDWORDData));
ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kInt64ValueName, &kInt64Data,
sizeof(kInt64Data), REG_QWORD));
- EXPECT_EQ(3U, key.ValueCount());
- EXPECT_TRUE(key.ValueExists(kStringValueName));
- EXPECT_TRUE(key.ValueExists(kDWORDValueName));
- EXPECT_TRUE(key.ValueExists(kInt64ValueName));
+ EXPECT_EQ(3U, key.GetValueCount());
+ EXPECT_TRUE(key.HasValue(kStringValueName));
+ EXPECT_TRUE(key.HasValue(kDWORDValueName));
+ EXPECT_TRUE(key.HasValue(kInt64ValueName));
// Test Read
std::wstring string_value;
@@ -89,10 +89,10 @@ TEST_F(RegistryTest, ValueTest) {
ASSERT_EQ(ERROR_SUCCESS, key.DeleteValue(kStringValueName));
ASSERT_EQ(ERROR_SUCCESS, key.DeleteValue(kDWORDValueName));
ASSERT_EQ(ERROR_SUCCESS, key.DeleteValue(kInt64ValueName));
- EXPECT_EQ(0U, key.ValueCount());
- EXPECT_FALSE(key.ValueExists(kStringValueName));
- EXPECT_FALSE(key.ValueExists(kDWORDValueName));
- EXPECT_FALSE(key.ValueExists(kInt64ValueName));
+ EXPECT_EQ(0U, key.GetValueCount());
+ EXPECT_FALSE(key.HasValue(kStringValueName));
+ EXPECT_FALSE(key.HasValue(kDWORDValueName));
+ EXPECT_FALSE(key.HasValue(kInt64ValueName));
}
}