2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlwapi.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsWindowsRegKey.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-01-10 00:39:40 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-08-05 08:16:53 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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 16383
|
|
|
|
|
2013-01-10 00:39:40 -08:00
|
|
|
class nsWindowsRegKey MOZ_FINAL : public nsIWindowsRegKey
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWINDOWSREGKEY
|
|
|
|
|
|
|
|
nsWindowsRegKey()
|
2013-10-10 13:41:00 -07:00
|
|
|
: mKey(nullptr)
|
|
|
|
, mWatchEvent(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mWatchRecursive(FALSE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsWindowsRegKey()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
HKEY mKey;
|
|
|
|
HANDLE mWatchEvent;
|
|
|
|
BOOL mWatchRecursive;
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsWindowsRegKey, nsIWindowsRegKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::GetKey(HKEY* aKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
*aKey = mKey;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::SetKey(HKEY aKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
// We do not close the older aKey!
|
2007-03-22 10:30:00 -07:00
|
|
|
StopWatching();
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
mKey = aKey;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWindowsRegKey::Close()
|
|
|
|
{
|
|
|
|
StopWatching();
|
|
|
|
|
|
|
|
if (mKey) {
|
|
|
|
RegCloseKey(mKey);
|
2013-10-10 13:41:00 -07:00
|
|
|
mKey = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::Open(uint32_t aRootKey, const nsAString& aPath,
|
|
|
|
uint32_t aMode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegOpenKeyExW((HKEY)(intptr_t)aRootKey,
|
|
|
|
PromiseFlatString(aPath).get(), 0, (REGSAM)aMode,
|
|
|
|
&mKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::Create(uint32_t aRootKey, const nsAString& aPath,
|
|
|
|
uint32_t aMode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
|
|
|
DWORD disposition;
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegCreateKeyExW((HKEY)(intptr_t)aRootKey,
|
|
|
|
PromiseFlatString(aPath).get(), 0, nullptr,
|
|
|
|
REG_OPTION_NON_VOLATILE, (REGSAM)aMode, nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
&mKey, &disposition);
|
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::OpenChild(const nsAString& aPath, uint32_t aMode,
|
|
|
|
nsIWindowsRegKey** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWindowsRegKey> child = new nsWindowsRegKey();
|
2014-07-09 08:15:21 -07:00
|
|
|
|
|
|
|
nsresult rv = child->Open((uintptr_t)mKey, aPath, aMode);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
child.swap(*aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::CreateChild(const nsAString& aPath, uint32_t aMode,
|
|
|
|
nsIWindowsRegKey** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWindowsRegKey> child = new nsWindowsRegKey();
|
2014-07-09 08:15:21 -07:00
|
|
|
|
|
|
|
nsresult rv = child->Create((uintptr_t)mKey, aPath, aMode);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
child.swap(*aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::GetChildCount(uint32_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DWORD numSubKeys;
|
2013-10-10 13:41:00 -07:00
|
|
|
LONG rv = RegQueryInfoKeyW(mKey, nullptr, nullptr, nullptr, &numSubKeys,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (rv != ERROR_SUCCESS) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
*aResult = numSubKeys;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::GetChildName(uint32_t aIndex, nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
FILETIME lastWritten;
|
|
|
|
|
2013-12-04 04:19:09 -08:00
|
|
|
wchar_t nameBuf[MAX_KEY_NAME_LEN + 1];
|
2007-03-22 10:30:00 -07:00
|
|
|
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegEnumKeyExW(mKey, aIndex, nameBuf, &nameLen, nullptr, nullptr,
|
2013-10-10 13:41:00 -07:00
|
|
|
nullptr, &lastWritten);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (rv != ERROR_SUCCESS) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aResult.Assign(nameBuf, nameLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::HasChild(const nsAString& aName, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-13 05:19:50 -07:00
|
|
|
// Check for the existence of a child key by opening the key with minimal
|
2007-03-22 10:30:00 -07:00
|
|
|
// rights. Perhaps there is a more efficient way to do this?
|
|
|
|
|
|
|
|
HKEY key;
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegOpenKeyExW(mKey, PromiseFlatString(aName).get(), 0,
|
2007-03-22 10:30:00 -07:00
|
|
|
STANDARD_RIGHTS_READ, &key);
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if ((*aResult = (rv == ERROR_SUCCESS && key))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
RegCloseKey(key);
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::GetValueCount(uint32_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DWORD numValues;
|
2013-10-10 13:41:00 -07:00
|
|
|
LONG rv = RegQueryInfoKeyW(mKey, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, &numValues, nullptr, nullptr,
|
|
|
|
nullptr, nullptr);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (rv != ERROR_SUCCESS) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
*aResult = numValues;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::GetValueName(uint32_t aIndex, nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-04 04:19:09 -08:00
|
|
|
wchar_t nameBuf[MAX_VALUE_NAME_LEN];
|
2007-03-22 10:30:00 -07:00
|
|
|
DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]);
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegEnumValueW(mKey, aIndex, nameBuf, &nameLen, nullptr, nullptr,
|
2013-10-10 13:41:00 -07:00
|
|
|
nullptr, nullptr);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (rv != ERROR_SUCCESS) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE; // XXX what's the best error code here?
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.Assign(nameBuf, nameLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::HasValue(const nsAString& aName, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0, nullptr,
|
2013-10-10 13:41:00 -07:00
|
|
|
nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
*aResult = (rv == ERROR_SUCCESS);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::RemoveChild(const nsAString& aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegDeleteKeyW(mKey, PromiseFlatString(aName).get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::RemoveValue(const nsAString& aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegDeleteValueW(mKey, PromiseFlatString(aName).get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::GetValueType(const nsAString& aName, uint32_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0,
|
|
|
|
(LPDWORD)aResult, nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::ReadStringValue(const nsAString& aName, nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DWORD type, size;
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
const nsString& flatName = PromiseFlatString(aName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-10 13:41:00 -07:00
|
|
|
LONG rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, nullptr, &size);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (rv != ERROR_SUCCESS) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// 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.
|
2014-07-09 08:15:21 -07:00
|
|
|
if (type != REG_SZ && type == REG_EXPAND_SZ && type == REG_MULTI_SZ) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// The buffer size must be a multiple of 2.
|
2014-07-09 08:15:21 -07:00
|
|
|
if (size % 2 != 0) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (size == 0) {
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.Truncate();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-19 06:09:41 -08:00
|
|
|
// |size| may or may not include the terminating null character.
|
|
|
|
DWORD resultLen = size / 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.SetLength(resultLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString::iterator begin;
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.BeginWriting(begin);
|
|
|
|
if (begin.size_forward() != resultLen) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, (LPBYTE)begin.get(),
|
2007-03-22 10:30:00 -07:00
|
|
|
&size);
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if (!aResult.CharAt(resultLen - 1)) {
|
2013-11-19 06:09:41 -08:00
|
|
|
// The string passed to us had a null terminator in the final position.
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.Truncate(resultLen - 1);
|
2013-11-19 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2011-02-17 13:18:53 -08:00
|
|
|
// Expand the environment variables if needed
|
|
|
|
if (type == REG_EXPAND_SZ) {
|
2014-07-09 08:15:21 -07:00
|
|
|
const nsString& flatSource = PromiseFlatString(aResult);
|
2013-10-10 13:41:00 -07:00
|
|
|
resultLen = ExpandEnvironmentStringsW(flatSource.get(), nullptr, 0);
|
2014-04-26 01:03:00 -07:00
|
|
|
if (resultLen > 1) {
|
2011-02-17 13:18:53 -08:00
|
|
|
nsAutoString expandedResult;
|
2011-02-22 08:05:26 -08:00
|
|
|
// |resultLen| includes the terminating null character
|
|
|
|
--resultLen;
|
2011-02-17 13:18:53 -08:00
|
|
|
expandedResult.SetLength(resultLen);
|
|
|
|
nsAString::iterator begin;
|
|
|
|
expandedResult.BeginWriting(begin);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (begin.size_forward() != resultLen) {
|
2011-02-17 13:18:53 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2011-02-17 13:18:53 -08:00
|
|
|
|
|
|
|
resultLen = ExpandEnvironmentStringsW(flatSource.get(),
|
2014-02-06 08:42:58 -08:00
|
|
|
wwc(begin.get()),
|
2011-02-22 08:05:26 -08:00
|
|
|
resultLen + 1);
|
2011-02-17 13:18:53 -08:00
|
|
|
if (resultLen <= 0) {
|
|
|
|
rv = ERROR_UNKNOWN_FEATURE;
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.Truncate();
|
2011-02-22 08:05:26 -08:00
|
|
|
} else {
|
2011-02-17 13:18:53 -08:00
|
|
|
rv = ERROR_SUCCESS;
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult = expandedResult;
|
2011-02-17 13:18:53 -08:00
|
|
|
}
|
2014-04-26 01:03:00 -07:00
|
|
|
} else if (resultLen == 1) {
|
|
|
|
// It apparently expands to nothing (just a null terminator).
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.Truncate();
|
2011-02-17 13:18:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::ReadIntValue(const nsAString& aName, uint32_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
DWORD size = sizeof(*aResult);
|
|
|
|
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0, nullptr,
|
|
|
|
(LPBYTE)aResult, &size);
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::ReadInt64Value(const nsAString& aName, uint64_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
DWORD size = sizeof(*aResult);
|
|
|
|
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0, nullptr,
|
|
|
|
(LPBYTE)aResult, &size);
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::ReadBinaryValue(const nsAString& aName, nsACString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DWORD size;
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0,
|
2013-10-10 13:41:00 -07:00
|
|
|
nullptr, nullptr, &size);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if (rv != ERROR_SUCCESS) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-26 01:03:00 -07:00
|
|
|
if (!size) {
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.Truncate();
|
2014-04-26 01:03:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.SetLength(size);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsACString::iterator begin;
|
2014-07-09 08:15:21 -07:00
|
|
|
aResult.BeginWriting(begin);
|
|
|
|
if (begin.size_forward() != size) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0, nullptr,
|
|
|
|
(LPBYTE)begin.get(), &size);
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::WriteStringValue(const nsAString& aName,
|
|
|
|
const nsAString& aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Need to indicate complete size of buffer including null terminator.
|
2014-07-09 08:15:21 -07:00
|
|
|
const nsString& flatValue = PromiseFlatString(aValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegSetValueExW(mKey, PromiseFlatString(aName).get(), 0, REG_SZ,
|
|
|
|
(const BYTE*)flatValue.get(),
|
2014-01-04 07:02:17 -08:00
|
|
|
(flatValue.Length() + 1) * sizeof(char16_t));
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::WriteIntValue(const nsAString& aName, uint32_t aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegSetValueExW(mKey, PromiseFlatString(aName).get(), 0, REG_DWORD,
|
|
|
|
(const BYTE*)&aValue, sizeof(aValue));
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::WriteInt64Value(const nsAString& aName, uint64_t aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegSetValueExW(mKey, PromiseFlatString(aName).get(), 0, REG_QWORD,
|
|
|
|
(const BYTE*)&aValue, sizeof(aValue));
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::WriteBinaryValue(const nsAString& aName,
|
|
|
|
const nsACString& aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
const nsCString& flatValue = PromiseFlatCString(aValue);
|
|
|
|
LONG rv = RegSetValueExW(mKey, PromiseFlatString(aName).get(), 0, REG_BINARY,
|
|
|
|
(const BYTE*)flatValue.get(), flatValue.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::StartWatching(bool aRecurse)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(!mKey)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if (mWatchEvent) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
|
|
|
|
2013-10-10 13:41:00 -07:00
|
|
|
mWatchEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (!mWatchEvent) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
DWORD filter = REG_NOTIFY_CHANGE_NAME |
|
|
|
|
REG_NOTIFY_CHANGE_ATTRIBUTES |
|
|
|
|
REG_NOTIFY_CHANGE_LAST_SET |
|
|
|
|
REG_NOTIFY_CHANGE_SECURITY;
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
LONG rv = RegNotifyChangeKeyValue(mKey, aRecurse, filter, mWatchEvent, TRUE);
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
mWatchRecursive = aRecurse;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWindowsRegKey::StopWatching()
|
|
|
|
{
|
|
|
|
if (mWatchEvent) {
|
|
|
|
CloseHandle(mWatchEvent);
|
2013-10-10 13:41:00 -07:00
|
|
|
mWatchEvent = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::HasChanged(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
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);
|
2014-07-09 08:15:21 -07:00
|
|
|
*aResult = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2014-07-09 08:15:21 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKey::IsWatching(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
*aResult = (mWatchEvent != nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2014-07-09 08:15:21 -07:00
|
|
|
NS_NewWindowsRegKey(nsIWindowsRegKey** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-09 08:11:31 -07:00
|
|
|
nsRefPtr<nsWindowsRegKey> key = new nsWindowsRegKey();
|
2014-07-09 08:15:21 -07:00
|
|
|
key.forget(aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2014-07-09 08:15:21 -07:00
|
|
|
nsWindowsRegKeyConstructor(nsISupports* aDelegate, const nsIID& aIID,
|
|
|
|
void** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (aDelegate) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWindowsRegKey> key;
|
|
|
|
nsresult rv = NS_NewWindowsRegKey(getter_AddRefs(key));
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = key->QueryInterface(aIID, aResult);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|