Bug 368310 - Remove the obsolete Windows "A" style API calls in nsWindowsRegKey

patch by Ryan Jones <bugs@ryan-jones.com>
r+sr=bsmedberg


git-svn-id: svn://10.0.0.236/trunk@219290 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
asqueella%gmail.com 2007-01-31 21:02:48 +00:00
parent 1cb9ae458b
commit d4ac305796

View File

@ -67,8 +67,7 @@ public:
// According to MSDN, the following limits apply (in characters excluding room
// for terminating null character):
#define MAX_KEY_NAME_LEN 255
#define MAX_VALUE_NAME_LEN_W 16383
#define MAX_VALUE_NAME_LEN_A 255
#define MAX_VALUE_NAME_LEN 16383
class nsWindowsRegKey : public nsIWindowsRegKey
{
@ -81,8 +80,6 @@ public:
, mWatchEvent(NULL)
, mWatchRecursive(FALSE)
{
if (sUseUnicode == -1)
GlobalInit();
}
private:
@ -94,36 +91,8 @@ private:
HKEY mKey;
HANDLE mWatchEvent;
BOOL mWatchRecursive;
static int sUseUnicode;
static void GlobalInit();
};
int
nsWindowsRegKey::sUseUnicode = -1; // undetermined
void
nsWindowsRegKey::GlobalInit()
{
#ifdef DEBUG
// In debug builds, allow explicit use of ANSI methods for testing purposes.
if (getenv("WINAPI_USE_ANSI")) {
sUseUnicode = PR_FALSE;
return;
}
#endif
// Find out if we are running on a unicode enabled version of Windows
OSVERSIONINFOA osvi = {0};
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (!GetVersionExA(&osvi)) {
sUseUnicode = PR_FALSE;
} else {
sUseUnicode = (osvi.dwPlatformId >= VER_PLATFORM_WIN32_NT);
}
}
NS_IMPL_ISUPPORTS1(nsWindowsRegKey, nsIWindowsRegKey)
NS_IMETHODIMP
@ -160,15 +129,8 @@ nsWindowsRegKey::Open(PRUint32 rootKey, const nsAString &path, PRUint32 mode)
{
Close();
LONG rv;
if (sUseUnicode) {
rv = RegOpenKeyExW((HKEY) rootKey, PromiseFlatString(path).get(), 0,
(REGSAM) mode, &mKey);
} else {
rv = RegOpenKeyExA((HKEY) rootKey, PromiseNativeString(path).get(), 0,
(REGSAM) mode, &mKey);
}
LONG rv = RegOpenKeyExW((HKEY) rootKey, PromiseFlatString(path).get(), 0,
(REGSAM) mode, &mKey);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -179,16 +141,9 @@ nsWindowsRegKey::Create(PRUint32 rootKey, const nsAString &path, PRUint32 mode)
Close();
DWORD disposition;
LONG rv;
if (sUseUnicode) {
rv = RegCreateKeyExW((HKEY) rootKey, PromiseFlatString(path).get(), 0,
NULL, REG_OPTION_NON_VOLATILE, (REGSAM) mode, NULL,
&mKey, &disposition);
} else {
rv = RegCreateKeyExA((HKEY) rootKey, PromiseNativeString(path).get(), 0,
NULL, REG_OPTION_NON_VOLATILE, (REGSAM) mode, NULL,
&mKey, &disposition);
}
LONG rv = RegCreateKeyExW((HKEY) rootKey, PromiseFlatString(path).get(), 0,
NULL, REG_OPTION_NON_VOLATILE, (REGSAM) mode, NULL,
&mKey, &disposition);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -234,11 +189,8 @@ nsWindowsRegKey::GetChildCount(PRUint32 *result)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
// We just use the 'A' version of this function here since there are no
// string parameters that we care about.
DWORD numSubKeys;
LONG rv = RegQueryInfoKeyA(mKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL,
LONG rv = RegQueryInfoKeyW(mKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL,
NULL, NULL, NULL, NULL, NULL);
NS_ENSURE_STATE(rv == ERROR_SUCCESS);
@ -252,30 +204,17 @@ nsWindowsRegKey::GetChildName(PRUint32 index, nsAString &result)
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
FILETIME lastWritten;
LONG rv;
if (sUseUnicode) {
PRUnichar nameBuf[MAX_KEY_NAME_LEN + 1];
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
PRUnichar nameBuf[MAX_KEY_NAME_LEN + 1];
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
rv = RegEnumKeyExW(mKey, index, nameBuf, &nameLen, NULL, NULL, NULL,
&lastWritten);
if (rv != ERROR_SUCCESS)
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
LONG rv = RegEnumKeyExW(mKey, index, nameBuf, &nameLen, NULL, NULL, NULL,
&lastWritten);
if (rv != ERROR_SUCCESS)
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
result.Assign(nameBuf, nameLen);
} else {
char nameBuf[MAX_KEY_NAME_LEN + 1];
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
rv = RegEnumKeyExA(mKey, index, nameBuf, &nameLen, NULL, NULL, NULL,
&lastWritten);
if (rv != ERROR_SUCCESS)
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
NS_CopyNativeToUnicode(nsDependentCString(nameBuf, nameLen), result);
}
result.Assign(nameBuf, nameLen);
return NS_OK;
}
@ -288,15 +227,9 @@ nsWindowsRegKey::HasChild(const nsAString &name, PRBool *result)
// rights. Perhaps there is a more efficient way to do this?
HKEY key;
LONG rv;
LONG rv = RegOpenKeyExW(mKey, PromiseFlatString(name).get(), 0,
STANDARD_RIGHTS_READ, &key);
if (sUseUnicode) {
rv = RegOpenKeyExW(mKey, PromiseFlatString(name).get(), 0,
STANDARD_RIGHTS_READ, &key);
} else {
rv = RegOpenKeyExA(mKey, PromiseNativeString(name).get(), 0,
STANDARD_RIGHTS_READ, &key);
}
if (*result = (rv == ERROR_SUCCESS && key))
RegCloseKey(key);
@ -308,11 +241,8 @@ nsWindowsRegKey::GetValueCount(PRUint32 *result)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
// We just use the 'A' version of this function here since there are no
// string parameters that we care about.
DWORD numValues;
LONG rv = RegQueryInfoKeyA(mKey, NULL, NULL, NULL, NULL, NULL, NULL,
LONG rv = RegQueryInfoKeyW(mKey, NULL, NULL, NULL, NULL, NULL, NULL,
&numValues, NULL, NULL, NULL, NULL);
NS_ENSURE_STATE(rv == ERROR_SUCCESS);
@ -325,27 +255,15 @@ nsWindowsRegKey::GetValueName(PRUint32 index, nsAString &result)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
if (sUseUnicode) {
PRUnichar nameBuf[MAX_VALUE_NAME_LEN_W];
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
PRUnichar nameBuf[MAX_VALUE_NAME_LEN];
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
LONG rv = RegEnumValueW(mKey, index, nameBuf, &nameLen, NULL, NULL, NULL,
NULL);
if (rv != ERROR_SUCCESS)
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
LONG rv = RegEnumValueW(mKey, index, nameBuf, &nameLen, NULL, NULL, NULL,
NULL);
if (rv != ERROR_SUCCESS)
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
result.Assign(nameBuf, nameLen);
} else {
char nameBuf[MAX_VALUE_NAME_LEN_A];
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
LONG rv = RegEnumValueA(mKey, index, nameBuf, &nameLen, NULL, NULL, NULL,
NULL);
if (rv != ERROR_SUCCESS)
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
NS_CopyNativeToUnicode(nsDependentCString(nameBuf, nameLen), result);
}
result.Assign(nameBuf, nameLen);
return NS_OK;
}
@ -355,14 +273,8 @@ nsWindowsRegKey::HasValue(const nsAString &name, PRBool *result)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
if (sUseUnicode) {
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL, NULL,
NULL);
} else {
rv = RegQueryValueExA(mKey, PromiseNativeString(name).get(), 0, NULL, NULL,
NULL);
}
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL, NULL,
NULL);
*result = (rv == ERROR_SUCCESS);
return NS_OK;
@ -373,12 +285,7 @@ nsWindowsRegKey::RemoveChild(const nsAString &name)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
if (sUseUnicode) {
rv = RegDeleteKeyW(mKey, PromiseFlatString(name).get());
} else {
rv = RegDeleteKeyA(mKey, PromiseNativeString(name).get());
}
LONG rv = RegDeleteKeyW(mKey, PromiseFlatString(name).get());
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -388,12 +295,7 @@ nsWindowsRegKey::RemoveValue(const nsAString &name)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
if (sUseUnicode) {
rv = RegDeleteValueW(mKey, PromiseFlatString(name).get());
} else {
rv = RegDeleteValueA(mKey, PromiseNativeString(name).get());
}
LONG rv = RegDeleteValueW(mKey, PromiseFlatString(name).get());
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -403,14 +305,8 @@ nsWindowsRegKey::GetValueType(const nsAString &name, PRUint32 *result)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
if (sUseUnicode) {
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0,
(LPDWORD) result, NULL, NULL);
} else {
rv = RegQueryValueExA(mKey, PromiseNativeString(name).get(), 0,
(LPDWORD) result, NULL, NULL);
}
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0,
(LPDWORD) result, NULL, NULL);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -421,71 +317,39 @@ nsWindowsRegKey::ReadStringValue(const nsAString &name, nsAString &result)
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
DWORD type, size;
LONG rv;
if (sUseUnicode) {
const nsString &flatName = PromiseFlatString(name);
rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, NULL, &size);
if (rv != ERROR_SUCCESS)
return NS_ERROR_FAILURE;
const nsString &flatName = PromiseFlatString(name);
// This must be a string type in order to fetch the value as a string.
// We're being a bit forgiving here by allowing types other than REG_SZ.
NS_ENSURE_STATE(type == REG_SZ ||
type == REG_EXPAND_SZ ||
type == REG_MULTI_SZ);
LONG rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, NULL, &size);
if (rv != ERROR_SUCCESS)
return NS_ERROR_FAILURE;
// The buffer size must be a multiple of 2.
NS_ENSURE_STATE(size % 2 == 0);
// This must be a string type in order to fetch the value as a string.
// We're being a bit forgiving here by allowing types other than REG_SZ.
NS_ENSURE_STATE(type == REG_SZ ||
type == REG_EXPAND_SZ ||
type == REG_MULTI_SZ);
if (size == 0) {
result.Truncate();
return NS_OK;
}
// The buffer size must be a multiple of 2.
NS_ENSURE_STATE(size % 2 == 0);
// |size| includes room for the terminating null character
DWORD resultLen = size / 2 - 1;
result.SetLength(resultLen);
nsAString::iterator begin;
result.BeginWriting(begin);
if (begin.size_forward() != resultLen)
return NS_ERROR_OUT_OF_MEMORY;
rv = RegQueryValueExW(mKey, flatName.get(), 0, NULL, (LPBYTE) begin.get(),
&size);
} else {
PromiseNativeString nativeName(name);
rv = RegQueryValueExA(mKey, nativeName.get(), 0, &type, NULL, &size);
if (rv != ERROR_SUCCESS)
return NS_ERROR_FAILURE;
// This must be a string type in order to fetch the value as a string.
// We're being a bit forgiving here by allowing types other than REG_SZ.
NS_ENSURE_STATE(type == REG_SZ ||
type == REG_EXPAND_SZ ||
type == REG_MULTI_SZ);
if (size == 0) {
result.Truncate();
return NS_OK;
}
nsCAutoString buf;
buf.SetLength(size - 1);
nsACString::iterator begin;
buf.BeginWriting(begin);
if (begin.size_forward() != (size - 1))
return NS_ERROR_OUT_OF_MEMORY;
rv = RegQueryValueExA(mKey, nativeName.get(), 0, NULL,
(LPBYTE) begin.get(), &size);
if (rv == ERROR_SUCCESS)
NS_CopyNativeToUnicode(buf, result);
if (size == 0) {
result.Truncate();
return NS_OK;
}
// |size| includes room for the terminating null character
DWORD resultLen = size / 2 - 1;
result.SetLength(resultLen);
nsAString::iterator begin;
result.BeginWriting(begin);
if (begin.size_forward() != resultLen)
return NS_ERROR_OUT_OF_MEMORY;
rv = RegQueryValueExW(mKey, flatName.get(), 0, NULL, (LPBYTE) begin.get(),
&size);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -495,15 +359,8 @@ nsWindowsRegKey::ReadIntValue(const nsAString &name, PRUint32 *result)
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
DWORD size = sizeof(*result);
LONG rv;
if (sUseUnicode) {
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL,
(LPBYTE) result, &size);
} else {
rv = RegQueryValueExA(mKey, PromiseNativeString(name).get(), 0, NULL,
(LPBYTE) result, &size);
}
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL,
(LPBYTE) result, &size);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -514,15 +371,8 @@ nsWindowsRegKey::ReadInt64Value(const nsAString &name, PRUint64 *result)
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
DWORD size = sizeof(*result);
LONG rv;
if (sUseUnicode) {
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL,
(LPBYTE) result, &size);
} else {
rv = RegQueryValueExA(mKey, PromiseNativeString(name).get(), 0, NULL,
(LPBYTE) result, &size);
}
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL,
(LPBYTE) result, &size);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -533,15 +383,9 @@ nsWindowsRegKey::ReadBinaryValue(const nsAString &name, nsACString &result)
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
DWORD size;
LONG rv;
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0,
NULL, NULL, &size);
if (sUseUnicode) {
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0,
NULL, NULL, &size);
} else {
rv = RegQueryValueExA(mKey, PromiseNativeString(name).get(), 0,
NULL, NULL, &size);
}
if (rv != ERROR_SUCCESS)
return NS_ERROR_FAILURE;
@ -551,13 +395,8 @@ nsWindowsRegKey::ReadBinaryValue(const nsAString &name, nsACString &result)
if (begin.size_forward() != size)
return NS_ERROR_OUT_OF_MEMORY;
if (sUseUnicode) {
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL,
(LPBYTE) begin.get(), &size);
} else {
rv = RegQueryValueExA(mKey, PromiseNativeString(name).get(), 0, NULL,
(LPBYTE) begin.get(), &size);
}
rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, NULL,
(LPBYTE) begin.get(), &size);
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -567,23 +406,12 @@ nsWindowsRegKey::WriteStringValue(const nsAString &name, const nsAString &value)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
// Need to indicate complete size of buffer including null terminator.
const nsString &flatValue = PromiseFlatString(value);
if (sUseUnicode) {
const nsString &flatValue = PromiseFlatString(value);
rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_SZ,
(const BYTE *) flatValue.get(),
(flatValue.Length() + 1) * sizeof(PRUnichar));
} else {
PromiseNativeString nativeValue(value);
rv = RegSetValueExA(mKey, PromiseNativeString(name).get(), 0, REG_SZ,
(const BYTE *) nativeValue.get(),
(nativeValue.Length() + 1) * sizeof(char));
}
LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_SZ,
(const BYTE *) flatValue.get(),
(flatValue.Length() + 1) * sizeof(PRUnichar));
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -593,15 +421,8 @@ nsWindowsRegKey::WriteIntValue(const nsAString &name, PRUint32 value)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
if (sUseUnicode) {
rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_DWORD,
(const BYTE *) &value, sizeof(value));
} else {
rv = RegSetValueExA(mKey, PromiseNativeString(name).get(), 0, REG_DWORD,
(const BYTE *) &value, sizeof(value));
}
LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_DWORD,
(const BYTE *) &value, sizeof(value));
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -611,15 +432,8 @@ nsWindowsRegKey::WriteInt64Value(const nsAString &name, PRUint64 value)
{
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
LONG rv;
if (sUseUnicode) {
rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_QWORD,
(const BYTE *) &value, sizeof(value));
} else {
rv = RegSetValueExA(mKey, PromiseNativeString(name).get(), 0, REG_QWORD,
(const BYTE *) &value, sizeof(value));
}
LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_QWORD,
(const BYTE *) &value, sizeof(value));
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}
@ -630,15 +444,8 @@ nsWindowsRegKey::WriteBinaryValue(const nsAString &name, const nsACString &value
NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED);
const nsCString &flatValue = PromiseFlatCString(value);
LONG rv;
if (sUseUnicode) {
rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_BINARY,
(const BYTE *) flatValue.get(), flatValue.Length());
} else {
rv = RegSetValueExA(mKey, PromiseNativeString(name).get(), 0, REG_BINARY,
(const BYTE *) flatValue.get(), flatValue.Length());
}
LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_BINARY,
(const BYTE *) flatValue.get(), flatValue.Length());
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
}