diff --git a/xpcom/build/dlldeps.cpp b/xpcom/build/dlldeps.cpp index a8600701deb7..c22a783b223e 100644 --- a/xpcom/build/dlldeps.cpp +++ b/xpcom/build/dlldeps.cpp @@ -91,6 +91,7 @@ #include "nsHashPropertyBag.h" #include "nsStringAPI.h" #include "nsStringBuffer.h" +#include "nsWindowsRegKey.h" void XXXNeverCalled() { @@ -239,4 +240,6 @@ void XXXNeverCalled() b.ToString(0, x); b.ToString(0, y); } + + NS_NewWindowsRegKey(nsnull); } diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp index 9d06b1aaf99c..5225a16e2be2 100644 --- a/xpcom/build/nsXPComInit.cpp +++ b/xpcom/build/nsXPComInit.cpp @@ -114,6 +114,10 @@ #include "SpecialSystemDirectory.h" +#ifdef XP_WIN +#include "nsWindowsRegKey.h" +#endif + #include // Registry Factory creation function defined in nsRegistry.cpp @@ -385,6 +389,10 @@ static const nsModuleComponentInfo components[] = { #define NS_HASH_PROPERTY_BAG_CLASSNAME "Hashtable Property Bag" COMPONENT(HASH_PROPERTY_BAG, nsHashPropertyBagConstructor), + +#ifdef XP_WIN + COMPONENT(WINDOWSREGKEY, nsWindowsRegKeyConstructor), +#endif }; #undef COMPONENT diff --git a/xpcom/ds/Makefile.in b/xpcom/ds/Makefile.in index c31611954bf9..ec1c81906c89 100644 --- a/xpcom/ds/Makefile.in +++ b/xpcom/ds/Makefile.in @@ -164,6 +164,12 @@ SDK_XPIDLSRCS = \ nsISupportsPrimitives.idl \ $(NULL) +ifeq ($(OS_ARCH),WINNT) +CPPSRCS += nsWindowsRegKey.cpp +XPIDLSRCS += nsIWindowsRegKey.idl +EXPORTS += nsWindowsRegKey.h +endif + EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS)) # we don't want the shared lib, but we want to force the creation of a static lib. diff --git a/xpcom/ds/nsIWindowsRegKey.idl b/xpcom/ds/nsIWindowsRegKey.idl new file mode 100644 index 000000000000..9dc3bf41dfd5 --- /dev/null +++ b/xpcom/ds/nsIWindowsRegKey.idl @@ -0,0 +1,361 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Google Inc. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + +native HKEY(HKEY); + +/** + * This interface is designed to provide scriptable access to the Windows + * registry system ("With Great Power Comes Great Responsibility"). The + * interface represents a single key in the registry. + * + * This interface is highly Win32 specific. + */ +[scriptable, uuid(2555b930-d64f-437e-9be7-0a2cb252c1f4)] +interface nsIWindowsRegKey : nsISupports +{ + /** + * Root keys. The values for these keys correspond to the values from + * WinReg.h in the MS Platform SDK. The ROOT_KEY_ prefix corresponds to the + * HKEY_ prefix in the MS Platform SDK. + * + * This interface is not restricted to using only these root keys. + */ + const unsigned long ROOT_KEY_CLASSES_ROOT = 0x80000000; + const unsigned long ROOT_KEY_CURRENT_USER = 0x80000001; + const unsigned long ROOT_KEY_LOCAL_MACHINE = 0x80000002; + + /** + * Values for the mode parameter passed to the open and create methods. + * The values defined here correspond to the REGSAM values defined in + * WinNT.h in the MS Platform SDK, where ACCESS_ is replaced with KEY_. + * + * This interface is not restricted to using only these access types. + */ + const unsigned long ACCESS_BASIC = 0x00020000; + const unsigned long ACCESS_QUERY_VALUE = 0x00000001; + const unsigned long ACCESS_SET_VALUE = 0x00000002; + const unsigned long ACCESS_CREATE_SUB_KEY = 0x00000004; + const unsigned long ACCESS_ENUMERATE_SUB_KEYS = 0x00000008; + const unsigned long ACCESS_NOTIFY = 0x00000010; + const unsigned long ACCESS_READ = ACCESS_BASIC | + ACCESS_QUERY_VALUE | + ACCESS_ENUMERATE_SUB_KEYS | + ACCESS_NOTIFY; + const unsigned long ACCESS_WRITE = ACCESS_BASIC | + ACCESS_SET_VALUE | + ACCESS_CREATE_SUB_KEY; + const unsigned long ACCESS_ALL = ACCESS_READ | + ACCESS_WRITE; + + /** + * Values for the type of a registry value. The numeric values of these + * constants are taken directly from WinNT.h in the MS Platform SDK. + * The Microsoft documentation should be consulted for the exact meaning of + * these value types. + * + * This interface is somewhat restricted to using only these value types. + * There is no method that is directly equivalent to RegQueryValueEx or + * RegSetValueEx. In particular, this interface does not support the + * REG_MULTI_SZ and REG_EXPAND_SZ value types. It is still possible to + * enumerate values that have other types (i.e., getValueType may return a + * type not defined below). + */ + const unsigned long TYPE_NONE = 0; // REG_NONE + const unsigned long TYPE_STRING = 1; // REG_SZ + const unsigned long TYPE_BINARY = 3; // REG_BINARY + const unsigned long TYPE_INT = 4; // REG_DWORD + const unsigned long TYPE_INT64 = 11; // REG_QWORD + + /** + * This attribute exposes the native HKEY and is available to provide C++ + * consumers with the flexibility of making other Windows registry API calls + * that are not exposed via this interface. + * + * It is possible to initialize this object by setting an HKEY on it. In + * that case, it is the responsibility of the consumer setting the HKEY to + * ensure that it is a valid HKEY. + * + * WARNING: Setting the key does not close the old key. + */ + [noscript] attribute HKEY key; + + /** + * This method closes the key. If the key is already closed, then this + * method does nothing. + */ + void close(); + + /** + * This method opens an existing key. This method fails if the key + * does not exist. + * + * NOTE: On 32-bit Windows, it is valid to pass any HKEY as the rootKey + * parameter of this function. However, for compatibility with 64-bit + * Windows, that usage should probably be avoided in favor of openChild. + * + * @param rootKey + * A root key defined above or any valid HKEY on 32-bit Windows. + * @param relPath + * A relative path from the given root key. + * @param mode + * Access mode, which is a bit-wise OR of the ACCESS_ values defined + * above. + */ + void open(in unsigned long rootKey, in AString relPath, in unsigned long mode); + + /** + * This method opens an existing key or creates a new key. + * + * NOTE: On 32-bit Windows, it is valid to pass any HKEY as the rootKey + * parameter of this function. However, for compatibility with 64-bit + * Windows, that usage should probably be avoided in favor of createChild. + * + * @param rootKey + * A root key defined above or any valid HKEY on 32-bit Windows. + * @param relPath + * A relative path from the given root key. + * @param mode + * Access mode, which is a bit-wise OR of the ACCESS_ values defined + * above. + */ + void create(in unsigned long rootKey, in AString relPath, in unsigned long mode); + + /** + * This method opens a subkey relative to this key. This method fails if the + * key does not exist. + * + * @return nsIWindowsRegKey for the newly opened subkey. + */ + nsIWindowsRegKey openChild(in AString relPath, in unsigned long mode); + + /** + * This method opens or creates a subkey relative to this key. + * + * @return nsIWindowsRegKey for the newly opened or created subkey. + */ + nsIWindowsRegKey createChild(in AString relPath, in unsigned long mode); + + /** + * This attribute returns the number of child keys. + */ + readonly attribute unsigned long childCount; + + /** + * This method returns the name of the n'th child key. + * + * @param index + * The index of the requested child key. + */ + AString getChildName(in unsigned long index); + + /** + * This method checks to see if the key has a child by the given name. + * + * @param name + * The name of the requested child key. + */ + boolean hasChild(in AString name); + + /** + * This attribute returns the number of values under this key. + */ + readonly attribute unsigned long valueCount; + + /** + * This method returns the name of the n'th value under this key. + * + * @param index + * The index of the requested value. + */ + AString getValueName(in unsigned long index); + + /** + * This method checks to see if the key has a value by the given name. + * + * @param name + * The name of the requested value. + */ + boolean hasValue(in AString name); + + /** + * This method removes a child key and all of its values. This method will + * fail if the key has any children of its own. + * + * @param relPath + * The relative path from this key to the key to be removed. + */ + void removeChild(in AString relPath); + + /** + * This method removes the value with the given name. + * + * @param name + * The name of the value to be removed. + */ + void removeValue(in AString name); + + /** + * This method returns the type of the value with the given name. The return + * value is one of the "TYPE_" constants defined above. + * + * @param name + * The name of the value to query. + */ + unsigned long getValueType(in AString name); + + /** + * This method reads the string contents of the named value as a Unicode + * string. + * + * @param name + * The name of the value to query. This parameter can be the empty + * string to request the key's default value. + */ + AString readStringValue(in AString name); + + /** + * This method reads the integer contents of the named value. + * + * @param name + * The name of the value to query. + */ + unsigned long readIntValue(in AString name); + + /** + * This method reads the 64-bit integer contents of the named value. + * + * @param name + * The name of the value to query. + */ + unsigned long long readInt64Value(in AString name); + + /** + * This method reads the binary contents of the named value under this key. + * + * JavaScript callers should take care with the result of this method since + * it will be byte-expanded to form a JS string. (The binary data will be + * treated as an ISO-Latin-1 character string, which it is not). + * + * @param name + * The name of the value to query. + */ + ACString readBinaryValue(in AString name); + + /** + * This method writes the unicode string contents of the named value. The + * value will be created if it does not already exist. + * + * @param name + * The name of the value to modify. This parameter can be the empty + * string to modify the key's default value. + * @param data + * The data for the value to modify. + */ + void writeStringValue(in AString name, in AString data); + + /** + * This method writes the integer contents of the named value. The value + * will be created if it does not already exist. + * + * @param name + * The name of the value to modify. + * @param data + * The data for the value to modify. + */ + void writeIntValue(in AString name, in unsigned long data); + + /** + * This method writes the 64-bit integer contents of the named value. The + * value will be created if it does not already exist. + * + * @param name + * The name of the value to modify. + * @param data + * The data for the value to modify. + */ + void writeInt64Value(in AString name, in unsigned long long data); + + /** + * This method writes the binary contents of the named value. The value will + * be created if it does not already exist. + * + * JavaScript callers should take care with the value passed to this method + * since it will be truncated from a JS string (unicode) to a ISO-Latin-1 + * string. (The binary data will be treated as an ISO-Latin-1 character + * string, which it is not). So, JavaScript callers should only pass + * character values in the range \u0000 to \u00FF, or else data loss will + * occur. + * + * @param name + * The name of the value to modify. + * @param data + * The data for the value to modify. + */ + void writeBinaryValue(in AString name, in ACString data); + + /** + * This method starts watching the key to see if any of its values have + * changed. The key must have been opened with mode including ACCESS_NOTIFY. + * If recurse is true, then this key and any of its descendant keys are + * watched. Otherwise, only this key is watched. + * + * @param recurse + * Indicates whether or not to also watch child keys. + */ + void startWatching(in boolean recurse); + + /** + * This method stops any watching of the key initiated by a call to + * startWatching. This method does nothing if the key is not being watched. + */ + void stopWatching(); + + /** + * This method returns true if the key is being watched for changes (i.e., + * if startWatching() was called). + */ + boolean isWatching(); + + /** + * This method returns true if the key has changed and false otherwise. + * This method will always return false if startWatching was not called. + */ + boolean hasChanged(); +}; diff --git a/xpcom/ds/nsWindowsRegKey.cpp b/xpcom/ds/nsWindowsRegKey.cpp new file mode 100644 index 000000000000..3243dbfe17f9 --- /dev/null +++ b/xpcom/ds/nsWindowsRegKey.cpp @@ -0,0 +1,727 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Google Inc. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include +#include +#include "nsWindowsRegKey.h" +#include "nsNativeCharsetUtils.h" +#include "nsString.h" +#include "nsCOMPtr.h" + +//----------------------------------------------------------------------------- + +// This class simplifies conversion from unicode to native charset somewhat. +class PromiseNativeString : public nsCAutoString +{ +public: + PromiseNativeString(const nsAString &input) + { + NS_CopyUnicodeToNative(input, *this); + } +}; + +//----------------------------------------------------------------------------- + +// 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 + +class nsWindowsRegKey : public nsIWindowsRegKey +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIWINDOWSREGKEY + + nsWindowsRegKey() + : mKey(NULL) + , mWatchEvent(NULL) + , mWatchRecursive(FALSE) + { + if (sUseUnicode == -1) + GlobalInit(); + } + +private: + ~nsWindowsRegKey() + { + Close(); + } + + 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("WINREG_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 +nsWindowsRegKey::GetKey(HKEY *key) +{ + *key = mKey; + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::SetKey(HKEY key) +{ + // We do not close the older key! + StopWatching(); + + mKey = key; + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::Close() +{ + StopWatching(); + + if (mKey) { + RegCloseKey(mKey); + mKey = NULL; + } + return NS_OK; +} + +NS_IMETHODIMP +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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsWindowsRegKey::OpenChild(const nsAString &path, PRUint32 mode, + nsIWindowsRegKey **result) +{ + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); + + nsCOMPtr child = new nsWindowsRegKey(); + if (!child) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult rv = child->Open((PRUint32) mKey, path, mode); + if (NS_FAILED(rv)) + return rv; + + child.swap(*result); + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::CreateChild(const nsAString &path, PRUint32 mode, + nsIWindowsRegKey **result) +{ + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); + + nsCOMPtr child = new nsWindowsRegKey(); + if (!child) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult rv = child->Create((PRUint32) mKey, path, mode); + if (NS_FAILED(rv)) + return rv; + + child.swap(*result); + return NS_OK; +} + +NS_IMETHODIMP +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, + NULL, NULL, NULL, NULL, NULL); + NS_ENSURE_STATE(rv == ERROR_SUCCESS); + + *result = numSubKeys; + return NS_OK; +} + +NS_IMETHODIMP +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]); + + 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); + } + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::HasChild(const nsAString &name, PRBool *result) +{ + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); + + // Check for the existance of a child key by opening the key with minimal + // rights. Perhaps there is a more efficient way to do this? + + HKEY key; + LONG rv; + + 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); + + return NS_OK; +} + +NS_IMETHODIMP +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, + &numValues, NULL, NULL, NULL, NULL); + NS_ENSURE_STATE(rv == ERROR_SUCCESS); + + *result = numValues; + return NS_OK; +} + +NS_IMETHODIMP +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]); + + 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); + } + + return NS_OK; +} + +NS_IMETHODIMP +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); + } + + *result = (rv == ERROR_SUCCESS); + return NS_OK; +} + +NS_IMETHODIMP +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()); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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()); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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; + + // 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); + + // The buffer size must be a multiple of 2. + NS_ENSURE_STATE(size % 2 == 0); + + 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); + } 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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsWindowsRegKey::ReadBinaryValue(const nsAString &name, nsACString &result) +{ + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); + + DWORD size; + LONG rv; + + 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; + + result.SetLength(size); + nsACString::iterator begin; + result.BeginWriting(begin); + 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); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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. + + if (sUseUnicode) { + const nsString &flatValue = PromiseFlatString(value); + + rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_SZ, + (const BYTE *) flatValue.get(), + flatValue.Length() + sizeof(PRUnichar)); + } else { + PromiseNativeString nativeValue(value); + + rv = RegSetValueExA(mKey, PromiseNativeString(name).get(), 0, REG_SZ, + (const BYTE *) nativeValue.get(), + nativeValue.Length() + sizeof(char)); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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)); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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)); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +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()); + } + + return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsWindowsRegKey::StartWatching(PRBool recurse) +{ + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); + + if (mWatchEvent) + return NS_OK; + + mWatchEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (!mWatchEvent) + return NS_ERROR_OUT_OF_MEMORY; + + DWORD filter = REG_NOTIFY_CHANGE_NAME | + REG_NOTIFY_CHANGE_ATTRIBUTES | + REG_NOTIFY_CHANGE_LAST_SET | + REG_NOTIFY_CHANGE_SECURITY; + + LONG rv = RegNotifyChangeKeyValue(mKey, recurse, filter, mWatchEvent, TRUE); + if (rv != ERROR_SUCCESS) { + StopWatching(); + // On older versions of Windows, this call is not implemented, so simply + // return NS_OK in those cases and pretend that the watching is happening. + return (rv == ERROR_CALL_NOT_IMPLEMENTED) ? NS_OK : NS_ERROR_FAILURE; + } + + mWatchRecursive = recurse; + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::StopWatching() +{ + if (mWatchEvent) { + CloseHandle(mWatchEvent); + mWatchEvent = NULL; + } + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::HasChanged(PRBool *result) +{ + if (mWatchEvent && WaitForSingleObject(mWatchEvent, 0) == WAIT_OBJECT_0) { + // An event only gets signaled once, then it's done, so we have to set up + // another event to watch. + StopWatching(); + StartWatching(mWatchRecursive); + *result = PR_TRUE; + } else { + *result = PR_FALSE; + } + return NS_OK; +} + +NS_IMETHODIMP +nsWindowsRegKey::IsWatching(PRBool *result) +{ + *result = (mWatchEvent != NULL); + return NS_OK; +} + +//----------------------------------------------------------------------------- + +nsresult +NS_NewWindowsRegKey(nsIWindowsRegKey **result) +{ + *result = new nsWindowsRegKey(); + if (!*result) + return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(*result); + return NS_OK; +} + +//----------------------------------------------------------------------------- + +NS_METHOD +nsWindowsRegKeyConstructor(nsISupports *delegate, const nsIID &iid, + void **result) +{ + if (delegate) + return NS_ERROR_NO_AGGREGATION; + + nsCOMPtr key; + nsresult rv = NS_NewWindowsRegKey(getter_AddRefs(key)); + if (NS_SUCCEEDED(rv)) + rv = key->QueryInterface(iid, result); + return rv; +} diff --git a/xpcom/ds/nsWindowsRegKey.h b/xpcom/ds/nsWindowsRegKey.h new file mode 100644 index 000000000000..1ab245cd0cb0 --- /dev/null +++ b/xpcom/ds/nsWindowsRegKey.h @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Google Inc. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsWindowsRegKey_h__ +#define nsWindowsRegKey_h__ + +//----------------------------------------------------------------------------- + +#include "nsIWindowsRegKey.h" + +/** + * This ContractID may be used to instantiate a windows registry key object + * via the XPCOM component manager. + */ +#define NS_WINDOWSREGKEY_CONTRACTID "@mozilla.org/windows-registry-key;1" + +/** + * This function may be used to instantiate a windows registry key object prior + * to XPCOM being initialized. + */ +extern "C" NS_COM nsresult +NS_NewWindowsRegKey(nsIWindowsRegKey **result); + +//----------------------------------------------------------------------------- + +#ifdef _IMPL_NS_COM + +#define NS_WINDOWSREGKEY_CLASSNAME "nsWindowsRegKey" + +// a53bc624-d577-4839-b8ec-bb5040a52ff4 +#define NS_WINDOWSREGKEY_CID \ + { 0xa53bc624, 0xd577, 0x4839, \ + { 0xb8, 0xec, 0xbb, 0x50, 0x40, 0xa5, 0x2f, 0xf4 } } + +extern NS_METHOD +nsWindowsRegKeyConstructor(nsISupports *outer, const nsIID &iid, void **result); + +#endif // _IMPL_NS_COM + +//----------------------------------------------------------------------------- + +#endif // nsWindowsRegKey_h__ diff --git a/xpcom/tests/TestWinReg.js b/xpcom/tests/TestWinReg.js new file mode 100644 index 000000000000..cee26c2229cd --- /dev/null +++ b/xpcom/tests/TestWinReg.js @@ -0,0 +1,89 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Google Inc. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/* + * This script is intended to be run using xpcshell + */ + +const nsIWindowsRegKey = Components.interfaces.nsIWindowsRegKey; +const BASE_PATH = "SOFTWARE\\Mozilla\\Firefox"; + +function idump(indent, str) +{ + for (var j = 0; j < indent; ++j) + dump(" "); + dump(str); +} + +function list_values(indent, key) { + idump(indent, "{\n"); + var count = key.valueCount; + for (var i = 0; i < count; ++i) { + var vn = key.getValueName(i); + var val = ""; + if (key.getValueType(vn) == nsIWindowsRegKey.TYPE_STRING) { + val = key.readStringValue(vn); + } + if (vn == "") + idump(indent + 1, "(Default): \"" + val + "\"\n"); + else + idump(indent + 1, vn + ": \"" + val + "\"\n"); + } + idump(indent, "}\n"); +} + +function list_children(indent, key) { + list_values(indent, key); + + var count = key.childCount; + for (var i = 0; i < count; ++i) { + var cn = key.getChildName(i); + idump(indent, "[" + cn + "]\n"); + list_children(indent + 1, key.openChild(cn, nsIWindowsRegKey.ACCESS_READ)); + } +} + +// enumerate everything under BASE_PATH +var key = Components.classes["@mozilla.org/windows-registry-key;1"]. + createInstance(nsIWindowsRegKey); +key.open(nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE, BASE_PATH, + nsIWindowsRegKey.ACCESS_READ); +list_children(1, key); + +key.close(); +key.open(nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, BASE_PATH, + nsIWindowsRegKey.ACCESS_READ); +list_children(1, key);