2014-09-08 20:41:22 +04: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/. */
|
2004-11-25 02:21:14 +03:00
|
|
|
|
|
|
|
#include "nsHashPropertyBag.h"
|
|
|
|
#include "nsArray.h"
|
|
|
|
#include "nsArrayEnumerator.h"
|
2005-03-22 21:16:53 +03:00
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsIProperty.h"
|
|
|
|
#include "nsVariant.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2004-11-25 02:21:14 +03:00
|
|
|
|
|
|
|
/*
|
2014-09-08 20:41:22 +04:00
|
|
|
* nsHashPropertyBagBase implementation.
|
2004-11-25 02:21:14 +03:00
|
|
|
*/
|
|
|
|
|
2006-08-20 20:42:08 +04:00
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::HasKey(const nsAString& aName, bool* aResult)
|
2006-08-20 20:42:08 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
*aResult = mPropertyHash.Get(aName, nullptr);
|
|
|
|
return NS_OK;
|
2006-08-20 20:42:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::Get(const nsAString& aName, nsIVariant** aResult)
|
2006-08-20 20:42:08 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!mPropertyHash.Get(aName, aResult)) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
}
|
2006-08-20 20:42:08 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_OK;
|
2006-08-20 20:42:08 +04:00
|
|
|
}
|
|
|
|
|
2004-11-25 02:21:14 +03:00
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetProperty(const nsAString& aName, nsIVariant** aResult)
|
2004-11-25 02:21:14 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
bool isFound = mPropertyHash.Get(aName, aResult);
|
|
|
|
if (!isFound) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2004-11-25 02:21:14 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_OK;
|
2004-11-25 02:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::SetProperty(const nsAString& aName, nsIVariant* aValue)
|
2004-11-25 02:21:14 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aValue)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2004-11-25 02:21:14 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
mPropertyHash.Put(aName, aValue);
|
2004-11-25 02:21:14 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_OK;
|
2004-11-25 02:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::DeleteProperty(const nsAString& aName)
|
2004-11-25 02:21:14 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
// is it too much to ask for ns*Hashtable to return
|
|
|
|
// a boolean indicating whether RemoveEntry succeeded
|
|
|
|
// or not?!?!
|
|
|
|
bool isFound = mPropertyHash.Get(aName, nullptr);
|
|
|
|
if (!isFound) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then from the hash
|
|
|
|
mPropertyHash.Remove(aName);
|
|
|
|
|
|
|
|
return NS_OK;
|
2004-11-25 02:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsSimpleProperty class and impl; used for GetEnumerator
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsSimpleProperty final : public nsIProperty
|
2014-07-01 02:11:53 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
~nsSimpleProperty() {}
|
2014-07-01 02:11:53 +04:00
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
public:
|
2014-07-09 19:15:21 +04:00
|
|
|
nsSimpleProperty(const nsAString& aName, nsIVariant* aValue)
|
|
|
|
: mName(aName)
|
|
|
|
, mValue(aValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIPROPERTY
|
2008-02-21 23:39:20 +03:00
|
|
|
protected:
|
2014-07-09 19:15:21 +04:00
|
|
|
nsString mName;
|
|
|
|
nsCOMPtr<nsIVariant> mValue;
|
2008-02-21 23:39:20 +03:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsSimpleProperty, nsIProperty)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2004-11-25 02:21:14 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleProperty::GetName(nsAString& aName)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
2004-11-25 02:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 19:15:21 +04:00
|
|
|
nsSimpleProperty::GetValue(nsIVariant** aValue)
|
2004-11-25 02:21:14 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IF_ADDREF(*aValue = mValue);
|
|
|
|
return NS_OK;
|
2004-11-25 02:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// end nsSimpleProperty
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult)
|
2004-11-25 02:21:14 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIMutableArray> propertyArray = nsArray::Create();
|
|
|
|
if (!propertyArray) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2004-11-25 02:21:14 +03:00
|
|
|
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = mPropertyHash.Iter(); !iter.Done(); iter.Next()) {
|
2015-07-20 15:21:28 +03:00
|
|
|
const nsAString& key = iter.Key();
|
|
|
|
nsIVariant* data = iter.UserData();
|
2015-07-10 02:54:59 +03:00
|
|
|
nsSimpleProperty* sprop = new nsSimpleProperty(key, data);
|
|
|
|
propertyArray->AppendElement(sprop, false);
|
|
|
|
}
|
2004-11-25 02:21:14 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_NewArrayEnumerator(aResult, propertyArray);
|
2004-11-25 02:21:14 +03:00
|
|
|
}
|
|
|
|
|
2005-03-22 21:16:53 +03:00
|
|
|
#define IMPL_GETSETPROPERTY_AS(Name, Type) \
|
|
|
|
NS_IMETHODIMP \
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetPropertyAs ## Name (const nsAString & prop, Type *_retval) \
|
2005-03-22 21:16:53 +03:00
|
|
|
{ \
|
|
|
|
nsIVariant* v = mPropertyHash.GetWeak(prop); \
|
|
|
|
if (!v) \
|
|
|
|
return NS_ERROR_NOT_AVAILABLE; \
|
|
|
|
return v->GetAs ## Name(_retval); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
NS_IMETHODIMP \
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::SetPropertyAs ## Name (const nsAString & prop, Type value) \
|
2005-03-22 21:16:53 +03:00
|
|
|
{ \
|
|
|
|
nsCOMPtr<nsIWritableVariant> var = new nsVariant(); \
|
|
|
|
var->SetAs ## Name(value); \
|
|
|
|
return SetProperty(prop, var); \
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
IMPL_GETSETPROPERTY_AS(Int32, int32_t)
|
|
|
|
IMPL_GETSETPROPERTY_AS(Uint32, uint32_t)
|
|
|
|
IMPL_GETSETPROPERTY_AS(Int64, int64_t)
|
|
|
|
IMPL_GETSETPROPERTY_AS(Uint64, uint64_t)
|
2005-03-22 21:16:53 +03:00
|
|
|
IMPL_GETSETPROPERTY_AS(Double, double)
|
2011-09-29 10:19:26 +04:00
|
|
|
IMPL_GETSETPROPERTY_AS(Bool, bool)
|
2005-03-22 21:16:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetPropertyAsAString(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
nsAString& aResult)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIVariant* v = mPropertyHash.GetWeak(aProp);
|
|
|
|
if (!v) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
return v->GetAsAString(aResult);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetPropertyAsACString(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
nsACString& aResult)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIVariant* v = mPropertyHash.GetWeak(aProp);
|
|
|
|
if (!v) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
return v->GetAsACString(aResult);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetPropertyAsAUTF8String(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
nsACString& aResult)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIVariant* v = mPropertyHash.GetWeak(aProp);
|
|
|
|
if (!v) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
return v->GetAsAUTF8String(aResult);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::GetPropertyAsInterface(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
const nsIID& aIID,
|
|
|
|
void** aResult)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIVariant* v = mPropertyHash.GetWeak(aProp);
|
|
|
|
if (!v) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsISupports> val;
|
|
|
|
nsresult rv = v->GetAsISupports(getter_AddRefs(val));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (!val) {
|
|
|
|
// We have a value, but it's null
|
|
|
|
*aResult = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return val->QueryInterface(aIID, aResult);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::SetPropertyAsAString(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
const nsAString& aValue)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIWritableVariant> var = new nsVariant();
|
|
|
|
var->SetAsAString(aValue);
|
|
|
|
return SetProperty(aProp, var);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::SetPropertyAsACString(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
const nsACString& aValue)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIWritableVariant> var = new nsVariant();
|
|
|
|
var->SetAsACString(aValue);
|
|
|
|
return SetProperty(aProp, var);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::SetPropertyAsAUTF8String(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
const nsACString& aValue)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIWritableVariant> var = new nsVariant();
|
|
|
|
var->SetAsAUTF8String(aValue);
|
|
|
|
return SetProperty(aProp, var);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-08 20:41:22 +04:00
|
|
|
nsHashPropertyBagBase::SetPropertyAsInterface(const nsAString& aProp,
|
2015-07-17 03:50:43 +03:00
|
|
|
nsISupports* aValue)
|
2005-03-22 21:16:53 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIWritableVariant> var = new nsVariant();
|
|
|
|
var->SetAsISupports(aValue);
|
|
|
|
return SetProperty(aProp, var);
|
2005-03-22 21:16:53 +03:00
|
|
|
}
|
|
|
|
|
2014-09-08 20:41:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nsHashPropertyBag implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsHashPropertyBag)
|
|
|
|
NS_IMPL_RELEASE(nsHashPropertyBag)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsHashPropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
|
|
|
|
NS_INTERFACE_MAP_END
|
2014-09-08 20:41:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsHashPropertyBagCC implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsHashPropertyBagCC, mPropertyHash)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHashPropertyBagCC)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHashPropertyBagCC)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHashPropertyBagCC)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
|
|
|
|
NS_INTERFACE_MAP_END
|