2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
2001-03-23 02:34:41 +03:00
|
|
|
|
2013-12-11 03:10:01 +04:00
|
|
|
#ifndef nsPrefBranch_h
|
|
|
|
#define nsPrefBranch_h
|
|
|
|
|
2001-03-23 02:34:41 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2001-10-23 00:54:48 +04:00
|
|
|
#include "nsIObserver.h"
|
2001-03-23 02:34:41 +03:00
|
|
|
#include "nsIPrefBranch.h"
|
2001-03-29 04:48:30 +04:00
|
|
|
#include "nsIPrefBranchInternal.h"
|
|
|
|
#include "nsIPrefLocalizedString.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
2001-03-23 02:34:41 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2002-04-09 18:18:39 +04:00
|
|
|
#include "nsIRelativeFilePref.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2001-03-23 02:34:41 +03:00
|
|
|
#include "nsString.h"
|
2009-01-22 07:15:34 +03:00
|
|
|
#include "nsTArray.h"
|
2001-06-19 05:40:19 +04:00
|
|
|
#include "nsWeakReference.h"
|
2010-07-17 01:10:01 +04:00
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsCRT.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2010-07-17 01:10:01 +04:00
|
|
|
|
2013-09-11 22:20:22 +04:00
|
|
|
namespace mozilla {
|
2013-11-06 07:58:20 +04:00
|
|
|
class PreferenceServiceReporter;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2013-09-11 22:20:22 +04:00
|
|
|
|
2010-07-17 01:10:01 +04:00
|
|
|
class nsPrefBranch;
|
|
|
|
|
|
|
|
class PrefCallback : public PLDHashEntryHdr {
|
2013-11-06 07:58:20 +04:00
|
|
|
friend class mozilla::PreferenceServiceReporter;
|
2010-07-17 01:10:01 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef PrefCallback* KeyType;
|
|
|
|
typedef const PrefCallback* KeyTypePointer;
|
|
|
|
|
|
|
|
static const PrefCallback* KeyToPointer(PrefCallback *aKey)
|
|
|
|
{
|
|
|
|
return aKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(const PrefCallback *aKey)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t hash = mozilla::HashString(aKey->mDomain);
|
2012-03-13 02:53:18 +04:00
|
|
|
return mozilla::AddToHash(hash, aKey->mCanonical);
|
2010-07-17 01:10:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Create a PrefCallback with a strong reference to its observer.
|
|
|
|
PrefCallback(const char *aDomain, nsIObserver *aObserver,
|
|
|
|
nsPrefBranch *aBranch)
|
|
|
|
: mDomain(aDomain),
|
|
|
|
mBranch(aBranch),
|
2012-07-30 18:20:58 +04:00
|
|
|
mWeakRef(nullptr),
|
2010-07-17 01:10:01 +04:00
|
|
|
mStrongRef(aObserver)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PrefCallback);
|
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aObserver);
|
|
|
|
mCanonical = canonical;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a PrefCallback with a weak reference to its observer.
|
|
|
|
PrefCallback(const char *aDomain,
|
|
|
|
nsISupportsWeakReference *aObserver,
|
|
|
|
nsPrefBranch *aBranch)
|
|
|
|
: mDomain(aDomain),
|
|
|
|
mBranch(aBranch),
|
|
|
|
mWeakRef(do_GetWeakReference(aObserver)),
|
2012-07-30 18:20:58 +04:00
|
|
|
mStrongRef(nullptr)
|
2010-07-17 01:10:01 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PrefCallback);
|
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aObserver);
|
|
|
|
mCanonical = canonical;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy constructor needs to be explicit or the linker complains.
|
2014-09-03 02:24:24 +04:00
|
|
|
explicit PrefCallback(const PrefCallback *&aCopy)
|
2010-07-17 01:10:01 +04:00
|
|
|
: mDomain(aCopy->mDomain),
|
|
|
|
mBranch(aCopy->mBranch),
|
|
|
|
mWeakRef(aCopy->mWeakRef),
|
|
|
|
mStrongRef(aCopy->mStrongRef),
|
|
|
|
mCanonical(aCopy->mCanonical)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PrefCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PrefCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PrefCallback);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(const PrefCallback *aKey) const
|
2010-07-17 01:10:01 +04:00
|
|
|
{
|
|
|
|
// We want to be able to look up a weakly-referencing PrefCallback after
|
|
|
|
// its observer has died so we can remove it from the table. Once the
|
|
|
|
// callback's observer dies, its canonical pointer is stale -- in
|
|
|
|
// particular, we may have allocated a new observer in the same spot in
|
|
|
|
// memory! So we can't just compare canonical pointers to determine
|
|
|
|
// whether aKey refers to the same observer as this.
|
|
|
|
//
|
|
|
|
// Our workaround is based on the way we use this hashtable: When we ask
|
|
|
|
// the hashtable to remove a PrefCallback whose weak reference has
|
|
|
|
// expired, we use as the key for removal the same object as was inserted
|
|
|
|
// into the hashtable. Thus we can say that if one of the keys' weak
|
|
|
|
// references has expired, the two keys are equal iff they're the same
|
|
|
|
// object.
|
|
|
|
|
|
|
|
if (IsExpired() || aKey->IsExpired())
|
|
|
|
return this == aKey;
|
|
|
|
|
|
|
|
if (mCanonical != aKey->mCanonical)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-17 01:10:01 +04:00
|
|
|
|
|
|
|
return mDomain.Equals(aKey->mDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefCallback *GetKey() const
|
|
|
|
{
|
|
|
|
return const_cast<PrefCallback*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a reference to the callback's observer, or null if the observer was
|
|
|
|
// weakly referenced and has been destroyed.
|
|
|
|
already_AddRefed<nsIObserver> GetObserver() const
|
|
|
|
{
|
|
|
|
if (!IsWeak()) {
|
2011-09-17 00:22:44 +04:00
|
|
|
nsCOMPtr<nsIObserver> copy = mStrongRef;
|
|
|
|
return copy.forget();
|
2010-07-17 01:10:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryReferent(mWeakRef);
|
|
|
|
return observer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& GetDomain() const
|
|
|
|
{
|
|
|
|
return mDomain;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrefBranch* GetPrefBranch() const
|
|
|
|
{
|
|
|
|
return mBranch;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has this callback's weak reference died?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsExpired() const
|
2010-07-17 01:10:01 +04:00
|
|
|
{
|
|
|
|
if (!IsWeak())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-17 01:10:01 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> observer(do_QueryReferent(mWeakRef));
|
|
|
|
return !observer;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2010-07-17 01:10:01 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mDomain;
|
|
|
|
nsPrefBranch *mBranch;
|
|
|
|
|
|
|
|
// Exactly one of mWeakRef and mStrongRef should be non-null.
|
|
|
|
nsWeakPtr mWeakRef;
|
|
|
|
nsCOMPtr<nsIObserver> mStrongRef;
|
|
|
|
|
|
|
|
// We need a canonical nsISupports pointer, per bug 578392.
|
|
|
|
nsISupports *mCanonical;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsWeak() const
|
2010-07-17 01:10:01 +04:00
|
|
|
{
|
|
|
|
return !!mWeakRef;
|
|
|
|
}
|
|
|
|
};
|
2001-03-23 02:34:41 +03:00
|
|
|
|
2015-03-21 21:35:18 +03:00
|
|
|
class nsPrefBranch final : public nsIPrefBranchInternal,
|
|
|
|
public nsIObserver,
|
|
|
|
public nsSupportsWeakReference
|
2001-03-23 02:34:41 +03:00
|
|
|
{
|
2013-11-06 07:58:20 +04:00
|
|
|
friend class mozilla::PreferenceServiceReporter;
|
2001-03-23 02:34:41 +03:00
|
|
|
public:
|
2014-03-06 05:58:01 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2001-03-23 02:34:41 +03:00
|
|
|
NS_DECL_NSIPREFBRANCH
|
2005-02-23 16:59:17 +03:00
|
|
|
NS_DECL_NSIPREFBRANCH2
|
2001-10-23 00:54:48 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
2001-03-23 02:34:41 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPrefBranch(const char *aPrefRoot, bool aDefaultBranch);
|
2001-03-23 02:34:41 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetRootLength() { return mPrefRootLength; }
|
2001-12-06 22:49:38 +03:00
|
|
|
|
2010-07-17 01:10:01 +04:00
|
|
|
nsresult RemoveObserverFromMap(const char *aDomain, nsISupports *aObserver);
|
|
|
|
|
2013-12-11 03:10:01 +04:00
|
|
|
static void NotifyObserver(const char *newpref, void *data);
|
2010-07-15 04:39:22 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
|
2012-10-11 19:36:21 +04:00
|
|
|
|
2016-04-22 22:35:01 +03:00
|
|
|
static void ReportToConsole(const nsAString& aMessage);
|
|
|
|
|
2001-03-23 02:34:41 +03:00
|
|
|
protected:
|
2014-07-01 02:11:48 +04:00
|
|
|
virtual ~nsPrefBranch();
|
|
|
|
|
2010-07-15 04:39:22 +04:00
|
|
|
nsPrefBranch() /* disallow use of this constructer */
|
2016-01-12 21:16:59 +03:00
|
|
|
: mPrefRootLength(0)
|
|
|
|
, mIsDefault(false)
|
|
|
|
, mFreeingObserverList(false)
|
|
|
|
{}
|
2001-03-23 02:34:41 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult GetDefaultFromPropertiesFile(const char *aPrefName, char16_t **return_buf);
|
2013-06-04 16:21:06 +04:00
|
|
|
// As SetCharPref, but without any check on the length of |aValue|
|
|
|
|
nsresult SetCharPrefInternal(const char *aPrefName, const char *aValue);
|
|
|
|
// Reject strings that are more than 1Mb, warn if strings are more than 16kb
|
|
|
|
nsresult CheckSanityOfStringLength(const char* aPrefName, const nsAString& aValue);
|
|
|
|
nsresult CheckSanityOfStringLength(const char* aPrefName, const char* aValue);
|
|
|
|
nsresult CheckSanityOfStringLength(const char* aPrefName, const uint32_t aLength);
|
2010-07-17 01:10:01 +04:00
|
|
|
void RemoveExpiredCallback(PrefCallback *aCallback);
|
2001-03-23 02:34:41 +03:00
|
|
|
const char *getPrefName(const char *aPrefName);
|
2001-10-23 00:54:48 +04:00
|
|
|
void freeObserverList(void);
|
2001-03-23 02:34:41 +03:00
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mPrefRootLength;
|
2009-01-22 07:15:34 +03:00
|
|
|
nsCString mPrefRoot;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mIsDefault;
|
2001-03-23 02:34:41 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mFreeingObserverList;
|
2010-07-17 01:10:01 +04:00
|
|
|
nsClassHashtable<PrefCallback, PrefCallback> mObservers;
|
2001-03-23 02:34:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-21 21:35:18 +03:00
|
|
|
class nsPrefLocalizedString final : public nsIPrefLocalizedString,
|
|
|
|
public nsISupportsString
|
2001-03-23 02:34:41 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsPrefLocalizedString();
|
|
|
|
|
2014-03-06 05:58:01 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2002-08-06 04:53:19 +04:00
|
|
|
NS_FORWARD_NSISUPPORTSSTRING(mUnicodeString->)
|
2001-04-04 00:11:54 +04:00
|
|
|
NS_FORWARD_NSISUPPORTSPRIMITIVE(mUnicodeString->)
|
|
|
|
|
|
|
|
nsresult Init();
|
2001-03-23 02:34:41 +03:00
|
|
|
|
|
|
|
private:
|
2014-07-01 02:11:48 +04:00
|
|
|
virtual ~nsPrefLocalizedString();
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD GetData(char16_t**) override;
|
|
|
|
NS_IMETHOD SetData(const char16_t* aData) override;
|
|
|
|
NS_IMETHOD SetDataWithLength(uint32_t aLength, const char16_t *aData) override;
|
2010-07-17 01:10:01 +04:00
|
|
|
|
2002-08-06 04:53:19 +04:00
|
|
|
nsCOMPtr<nsISupportsString> mUnicodeString;
|
2001-03-23 02:34:41 +03:00
|
|
|
};
|
|
|
|
|
2002-04-09 18:18:39 +04:00
|
|
|
|
|
|
|
class nsRelativeFilePref : public nsIRelativeFilePref
|
|
|
|
{
|
|
|
|
public:
|
2014-03-06 05:58:01 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2002-04-09 18:18:39 +04:00
|
|
|
NS_DECL_NSIRELATIVEFILEPREF
|
2014-07-01 02:11:48 +04:00
|
|
|
|
|
|
|
nsRelativeFilePref();
|
|
|
|
|
2002-04-09 18:18:39 +04:00
|
|
|
private:
|
2014-07-01 02:11:48 +04:00
|
|
|
virtual ~nsRelativeFilePref();
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> mFile;
|
2008-06-30 22:46:47 +04:00
|
|
|
nsCString mRelativeToKey;
|
2002-04-09 18:18:39 +04:00
|
|
|
};
|
2013-12-11 03:10:01 +04:00
|
|
|
|
|
|
|
#endif
|