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