michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "nsWindowsRegKey.h" michael@0: #include "nsString.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: // According to MSDN, the following limits apply (in characters excluding room michael@0: // for terminating null character): michael@0: #define MAX_KEY_NAME_LEN 255 michael@0: #define MAX_VALUE_NAME_LEN 16383 michael@0: michael@0: class nsWindowsRegKey MOZ_FINAL : public nsIWindowsRegKey michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIWINDOWSREGKEY michael@0: michael@0: nsWindowsRegKey() michael@0: : mKey(nullptr) michael@0: , mWatchEvent(nullptr) michael@0: , mWatchRecursive(FALSE) michael@0: { michael@0: } michael@0: michael@0: private: michael@0: ~nsWindowsRegKey() michael@0: { michael@0: Close(); michael@0: } michael@0: michael@0: HKEY mKey; michael@0: HANDLE mWatchEvent; michael@0: BOOL mWatchRecursive; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsWindowsRegKey, nsIWindowsRegKey) michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::GetKey(HKEY *key) michael@0: { michael@0: *key = mKey; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::SetKey(HKEY key) michael@0: { michael@0: // We do not close the older key! michael@0: StopWatching(); michael@0: michael@0: mKey = key; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::Close() michael@0: { michael@0: StopWatching(); michael@0: michael@0: if (mKey) { michael@0: RegCloseKey(mKey); michael@0: mKey = nullptr; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::Open(uint32_t rootKey, const nsAString &path, uint32_t mode) michael@0: { michael@0: Close(); michael@0: michael@0: LONG rv = RegOpenKeyExW((HKEY)(intptr_t) rootKey, PromiseFlatString(path).get(), 0, michael@0: (REGSAM) mode, &mKey); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::Create(uint32_t rootKey, const nsAString &path, uint32_t mode) michael@0: { michael@0: Close(); michael@0: michael@0: DWORD disposition; michael@0: LONG rv = RegCreateKeyExW((HKEY)(intptr_t) rootKey, PromiseFlatString(path).get(), 0, michael@0: nullptr, REG_OPTION_NON_VOLATILE, (REGSAM) mode, nullptr, michael@0: &mKey, &disposition); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::OpenChild(const nsAString &path, uint32_t mode, michael@0: nsIWindowsRegKey **result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: nsCOMPtr child = new nsWindowsRegKey(); michael@0: michael@0: nsresult rv = child->Open((uintptr_t) mKey, path, mode); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: child.swap(*result); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::CreateChild(const nsAString &path, uint32_t mode, michael@0: nsIWindowsRegKey **result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: nsCOMPtr child = new nsWindowsRegKey(); michael@0: michael@0: nsresult rv = child->Create((uintptr_t) mKey, path, mode); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: child.swap(*result); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::GetChildCount(uint32_t *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: DWORD numSubKeys; michael@0: LONG rv = RegQueryInfoKeyW(mKey, nullptr, nullptr, nullptr, &numSubKeys, michael@0: nullptr, nullptr, nullptr, nullptr, nullptr, michael@0: nullptr, nullptr); michael@0: if (rv != ERROR_SUCCESS) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *result = numSubKeys; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::GetChildName(uint32_t index, nsAString &result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: FILETIME lastWritten; michael@0: michael@0: wchar_t nameBuf[MAX_KEY_NAME_LEN + 1]; michael@0: DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]); michael@0: michael@0: LONG rv = RegEnumKeyExW(mKey, index, nameBuf, &nameLen, nullptr, nullptr, michael@0: nullptr, &lastWritten); michael@0: if (rv != ERROR_SUCCESS) michael@0: return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here? michael@0: michael@0: result.Assign(nameBuf, nameLen); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::HasChild(const nsAString &name, bool *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: // Check for the existence of a child key by opening the key with minimal michael@0: // rights. Perhaps there is a more efficient way to do this? michael@0: michael@0: HKEY key; michael@0: LONG rv = RegOpenKeyExW(mKey, PromiseFlatString(name).get(), 0, michael@0: STANDARD_RIGHTS_READ, &key); michael@0: michael@0: if ((*result = (rv == ERROR_SUCCESS && key))) michael@0: RegCloseKey(key); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::GetValueCount(uint32_t *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: DWORD numValues; michael@0: LONG rv = RegQueryInfoKeyW(mKey, nullptr, nullptr, nullptr, nullptr, michael@0: nullptr, nullptr, &numValues, nullptr, nullptr, michael@0: nullptr, nullptr); michael@0: if (rv != ERROR_SUCCESS) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *result = numValues; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::GetValueName(uint32_t index, nsAString &result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: wchar_t nameBuf[MAX_VALUE_NAME_LEN]; michael@0: DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]); michael@0: michael@0: LONG rv = RegEnumValueW(mKey, index, nameBuf, &nameLen, nullptr, nullptr, michael@0: nullptr, nullptr); michael@0: if (rv != ERROR_SUCCESS) michael@0: return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here? michael@0: michael@0: result.Assign(nameBuf, nameLen); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::HasValue(const nsAString &name, bool *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, michael@0: nullptr, nullptr); michael@0: michael@0: *result = (rv == ERROR_SUCCESS); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::RemoveChild(const nsAString &name) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LONG rv = RegDeleteKeyW(mKey, PromiseFlatString(name).get()); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::RemoveValue(const nsAString &name) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LONG rv = RegDeleteValueW(mKey, PromiseFlatString(name).get()); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::GetValueType(const nsAString &name, uint32_t *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, michael@0: (LPDWORD) result, nullptr, nullptr); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::ReadStringValue(const nsAString &name, nsAString &result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: DWORD type, size; michael@0: michael@0: const nsString &flatName = PromiseFlatString(name); michael@0: michael@0: LONG rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, nullptr, &size); michael@0: if (rv != ERROR_SUCCESS) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // This must be a string type in order to fetch the value as a string. michael@0: // We're being a bit forgiving here by allowing types other than REG_SZ. michael@0: if (type != REG_SZ && type == REG_EXPAND_SZ && type == REG_MULTI_SZ) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // The buffer size must be a multiple of 2. michael@0: if (size % 2 != 0) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: if (size == 0) { michael@0: result.Truncate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // |size| may or may not include the terminating null character. michael@0: DWORD resultLen = size / 2; michael@0: michael@0: result.SetLength(resultLen); michael@0: nsAString::iterator begin; michael@0: result.BeginWriting(begin); michael@0: if (begin.size_forward() != resultLen) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, (LPBYTE) begin.get(), michael@0: &size); michael@0: michael@0: if (!result.CharAt(resultLen-1)) { michael@0: // The string passed to us had a null terminator in the final position. michael@0: result.Truncate(resultLen-1); michael@0: } michael@0: michael@0: // Expand the environment variables if needed michael@0: if (type == REG_EXPAND_SZ) { michael@0: const nsString &flatSource = PromiseFlatString(result); michael@0: resultLen = ExpandEnvironmentStringsW(flatSource.get(), nullptr, 0); michael@0: if (resultLen > 1) { michael@0: nsAutoString expandedResult; michael@0: // |resultLen| includes the terminating null character michael@0: --resultLen; michael@0: expandedResult.SetLength(resultLen); michael@0: nsAString::iterator begin; michael@0: expandedResult.BeginWriting(begin); michael@0: if (begin.size_forward() != resultLen) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: resultLen = ExpandEnvironmentStringsW(flatSource.get(), michael@0: wwc(begin.get()), michael@0: resultLen + 1); michael@0: if (resultLen <= 0) { michael@0: rv = ERROR_UNKNOWN_FEATURE; michael@0: result.Truncate(); michael@0: } else { michael@0: rv = ERROR_SUCCESS; michael@0: result = expandedResult; michael@0: } michael@0: } else if (resultLen == 1) { michael@0: // It apparently expands to nothing (just a null terminator). michael@0: result.Truncate(); michael@0: } michael@0: } michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::ReadIntValue(const nsAString &name, uint32_t *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: DWORD size = sizeof(*result); michael@0: LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, michael@0: (LPBYTE) result, &size); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::ReadInt64Value(const nsAString &name, uint64_t *result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: DWORD size = sizeof(*result); michael@0: LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, michael@0: (LPBYTE) result, &size); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::ReadBinaryValue(const nsAString &name, nsACString &result) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: DWORD size; michael@0: LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, michael@0: nullptr, nullptr, &size); michael@0: michael@0: if (rv != ERROR_SUCCESS) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (!size) { michael@0: result.Truncate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: result.SetLength(size); michael@0: nsACString::iterator begin; michael@0: result.BeginWriting(begin); michael@0: if (begin.size_forward() != size) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, michael@0: (LPBYTE) begin.get(), &size); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::WriteStringValue(const nsAString &name, const nsAString &value) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: // Need to indicate complete size of buffer including null terminator. michael@0: const nsString &flatValue = PromiseFlatString(value); michael@0: michael@0: LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_SZ, michael@0: (const BYTE *) flatValue.get(), michael@0: (flatValue.Length() + 1) * sizeof(char16_t)); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::WriteIntValue(const nsAString &name, uint32_t value) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_DWORD, michael@0: (const BYTE *) &value, sizeof(value)); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::WriteInt64Value(const nsAString &name, uint64_t value) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_QWORD, michael@0: (const BYTE *) &value, sizeof(value)); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::WriteBinaryValue(const nsAString &name, const nsACString &value) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: const nsCString &flatValue = PromiseFlatCString(value); michael@0: LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_BINARY, michael@0: (const BYTE *) flatValue.get(), flatValue.Length()); michael@0: michael@0: return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::StartWatching(bool recurse) michael@0: { michael@0: if (NS_WARN_IF(!mKey)) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: if (mWatchEvent) michael@0: return NS_OK; michael@0: michael@0: mWatchEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr); michael@0: if (!mWatchEvent) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: DWORD filter = REG_NOTIFY_CHANGE_NAME | michael@0: REG_NOTIFY_CHANGE_ATTRIBUTES | michael@0: REG_NOTIFY_CHANGE_LAST_SET | michael@0: REG_NOTIFY_CHANGE_SECURITY; michael@0: michael@0: LONG rv = RegNotifyChangeKeyValue(mKey, recurse, filter, mWatchEvent, TRUE); michael@0: if (rv != ERROR_SUCCESS) { michael@0: StopWatching(); michael@0: // On older versions of Windows, this call is not implemented, so simply michael@0: // return NS_OK in those cases and pretend that the watching is happening. michael@0: return (rv == ERROR_CALL_NOT_IMPLEMENTED) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mWatchRecursive = recurse; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::StopWatching() michael@0: { michael@0: if (mWatchEvent) { michael@0: CloseHandle(mWatchEvent); michael@0: mWatchEvent = nullptr; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::HasChanged(bool *result) michael@0: { michael@0: if (mWatchEvent && WaitForSingleObject(mWatchEvent, 0) == WAIT_OBJECT_0) { michael@0: // An event only gets signaled once, then it's done, so we have to set up michael@0: // another event to watch. michael@0: StopWatching(); michael@0: StartWatching(mWatchRecursive); michael@0: *result = true; michael@0: } else { michael@0: *result = false; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWindowsRegKey::IsWatching(bool *result) michael@0: { michael@0: *result = (mWatchEvent != nullptr); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: nsresult michael@0: NS_NewWindowsRegKey(nsIWindowsRegKey **result) michael@0: { michael@0: nsRefPtr key = new nsWindowsRegKey(); michael@0: key.forget(result); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: nsresult michael@0: nsWindowsRegKeyConstructor(nsISupports *delegate, const nsIID &iid, michael@0: void **result) michael@0: { michael@0: if (delegate) michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: michael@0: nsCOMPtr key; michael@0: nsresult rv = NS_NewWindowsRegKey(getter_AddRefs(key)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = key->QueryInterface(iid, result); michael@0: return rv; michael@0: }