Bug 580790 - Connection::initialize can access preferences off of the main thread. r=sdwilsh, sr=bsmedberg, a=betaN+

This commit is contained in:
Dan Witte 2011-01-05 16:32:31 -08:00
Родитель 5fc9ec00e1
Коммит 8fdd15a770
3 изменённых файлов: 130 добавлений и 1 удалений

Просмотреть файл

@ -44,6 +44,7 @@
#endif #endif
#include "nsPrefBranch.h" #include "nsPrefBranch.h"
#include "nsPrefService.h"
#include "nsILocalFile.h" #include "nsILocalFile.h"
#include "nsIObserverService.h" #include "nsIObserverService.h"
#include "nsXPCOM.h" #include "nsXPCOM.h"
@ -52,6 +53,7 @@
#include "nsString.h" #include "nsString.h"
#include "nsReadableUtils.h" #include "nsReadableUtils.h"
#include "nsXPIDLString.h" #include "nsXPIDLString.h"
#include "nsThreadUtils.h"
#include "nsIStringBundle.h" #include "nsIStringBundle.h"
#include "prefapi.h" #include "prefapi.h"
#include "prmem.h" #include "prmem.h"
@ -147,6 +149,10 @@ NS_INTERFACE_MAP_END
NS_IMETHODIMP nsPrefBranch::GetRoot(char **aRoot) NS_IMETHODIMP nsPrefBranch::GetRoot(char **aRoot)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG_POINTER(aRoot); NS_ENSURE_ARG_POINTER(aRoot);
mPrefRoot.Truncate(mPrefRootLength); mPrefRoot.Truncate(mPrefRootLength);
*aRoot = ToNewCString(mPrefRoot); *aRoot = ToNewCString(mPrefRoot);
@ -155,6 +161,10 @@ NS_IMETHODIMP nsPrefBranch::GetRoot(char **aRoot)
NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, PRInt32 *_retval) NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, PRInt32 *_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
const char *pref = getPrefName(aPrefName); const char *pref = getPrefName(aPrefName);
*_retval = PREF_GetPrefType(pref); *_retval = PREF_GetPrefType(pref);
@ -163,6 +173,10 @@ NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, PRInt32 *_retval)
NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, PRBool *_retval) NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, PRBool *_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
const char *pref = getPrefName(aPrefName); const char *pref = getPrefName(aPrefName);
return PREF_GetBoolPref(pref, _retval, mIsDefault); return PREF_GetBoolPref(pref, _retval, mIsDefault);
@ -170,6 +184,10 @@ NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, PRBool *_retval)
NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, PRInt32 aValue) NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, PRInt32 aValue)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot set pref from content process"); NS_ERROR("cannot set pref from content process");
@ -184,6 +202,10 @@ NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, PRInt32 aValue)
NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval) NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
const char *pref = getPrefName(aPrefName); const char *pref = getPrefName(aPrefName);
return PREF_CopyCharPref(pref, _retval, mIsDefault); return PREF_CopyCharPref(pref, _retval, mIsDefault);
@ -191,6 +213,10 @@ NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValue) NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValue)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot set pref from content process"); NS_ERROR("cannot set pref from content process");
@ -206,6 +232,10 @@ NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValu
NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, PRInt32 *_retval) NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, PRInt32 *_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
const char *pref = getPrefName(aPrefName); const char *pref = getPrefName(aPrefName);
return PREF_GetIntPref(pref, _retval, mIsDefault); return PREF_GetIntPref(pref, _retval, mIsDefault);
@ -213,6 +243,10 @@ NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, PRInt32 *_retval)
NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, PRInt32 aValue) NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, PRInt32 aValue)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot set pref from content process"); NS_ERROR("cannot set pref from content process");
@ -227,6 +261,10 @@ NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, PRInt32 aValue)
NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID & aType, void **_retval) NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID & aType, void **_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
nsresult rv; nsresult rv;
@ -388,6 +426,10 @@ NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID &
NS_IMETHODIMP nsPrefBranch::SetComplexValue(const char *aPrefName, const nsIID & aType, nsISupports *aValue) NS_IMETHODIMP nsPrefBranch::SetComplexValue(const char *aPrefName, const nsIID & aType, nsISupports *aValue)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot set pref from content process"); NS_ERROR("cannot set pref from content process");
@ -479,6 +521,10 @@ NS_IMETHODIMP nsPrefBranch::SetComplexValue(const char *aPrefName, const nsIID &
NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName) NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot set pref from content process"); NS_ERROR("cannot set pref from content process");
@ -493,6 +539,10 @@ NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName)
NS_IMETHODIMP nsPrefBranch::PrefHasUserValue(const char *aPrefName, PRBool *_retval) NS_IMETHODIMP nsPrefBranch::PrefHasUserValue(const char *aPrefName, PRBool *_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG_POINTER(_retval); NS_ENSURE_ARG_POINTER(_retval);
NS_ENSURE_ARG(aPrefName); NS_ENSURE_ARG(aPrefName);
const char *pref = getPrefName(aPrefName); const char *pref = getPrefName(aPrefName);
@ -502,6 +552,10 @@ NS_IMETHODIMP nsPrefBranch::PrefHasUserValue(const char *aPrefName, PRBool *_ret
NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName) NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot lock pref from content process"); NS_ERROR("cannot lock pref from content process");
@ -516,6 +570,10 @@ NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName)
NS_IMETHODIMP nsPrefBranch::PrefIsLocked(const char *aPrefName, PRBool *_retval) NS_IMETHODIMP nsPrefBranch::PrefIsLocked(const char *aPrefName, PRBool *_retval)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot check lock pref from content process"); NS_ERROR("cannot check lock pref from content process");
@ -532,6 +590,10 @@ NS_IMETHODIMP nsPrefBranch::PrefIsLocked(const char *aPrefName, PRBool *_retval)
NS_IMETHODIMP nsPrefBranch::UnlockPref(const char *aPrefName) NS_IMETHODIMP nsPrefBranch::UnlockPref(const char *aPrefName)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot unlock pref from content process"); NS_ERROR("cannot unlock pref from content process");
@ -552,6 +614,10 @@ NS_IMETHODIMP nsPrefBranch::ResetBranch(const char *aStartingAt)
NS_IMETHODIMP nsPrefBranch::DeleteBranch(const char *aStartingAt) NS_IMETHODIMP nsPrefBranch::DeleteBranch(const char *aStartingAt)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (GetContentChild()) { if (GetContentChild()) {
NS_ERROR("cannot set pref from content process"); NS_ERROR("cannot set pref from content process");
@ -566,6 +632,10 @@ NS_IMETHODIMP nsPrefBranch::DeleteBranch(const char *aStartingAt)
NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, PRUint32 *aCount, char ***aChildArray) NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, PRUint32 *aCount, char ***aChildArray)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
char **outArray; char **outArray;
PRInt32 numPrefs; PRInt32 numPrefs;
PRInt32 dwIndex; PRInt32 dwIndex;
@ -625,6 +695,10 @@ NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, PRUint32 *aCou
NS_IMETHODIMP nsPrefBranch::AddObserver(const char *aDomain, nsIObserver *aObserver, PRBool aHoldWeak) NS_IMETHODIMP nsPrefBranch::AddObserver(const char *aDomain, nsIObserver *aObserver, PRBool aHoldWeak)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
PrefCallback *pCallback; PrefCallback *pCallback;
const char *pref; const char *pref;
@ -670,6 +744,10 @@ NS_IMETHODIMP nsPrefBranch::AddObserver(const char *aDomain, nsIObserver *aObser
NS_IMETHODIMP nsPrefBranch::RemoveObserver(const char *aDomain, nsIObserver *aObserver) NS_IMETHODIMP nsPrefBranch::RemoveObserver(const char *aDomain, nsIObserver *aObserver)
{ {
if (NS_UNLIKELY(!nsPrefService::CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_ARG(aDomain); NS_ENSURE_ARG(aDomain);
NS_ENSURE_ARG(aObserver); NS_ENSURE_ARG(aObserver);

Просмотреть файл

@ -46,6 +46,7 @@
#include "nsAppDirectoryServiceDefs.h" #include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceDefs.h" #include "nsDirectoryServiceDefs.h"
#include "nsICategoryManager.h" #include "nsICategoryManager.h"
#include "nsIConsoleService.h"
#include "nsCategoryManagerUtils.h" #include "nsCategoryManagerUtils.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "nsIFile.h" #include "nsIFile.h"
@ -60,6 +61,7 @@
#include "nsCOMArray.h" #include "nsCOMArray.h"
#include "nsXPCOMCID.h" #include "nsXPCOMCID.h"
#include "nsAutoPtr.h" #include "nsAutoPtr.h"
#include "nsThreadUtils.h"
#include "nsQuickSort.h" #include "nsQuickSort.h"
#include "prmem.h" #include "prmem.h"
@ -219,6 +221,10 @@ NS_IMETHODIMP nsPrefService::Observe(nsISupports *aSubject, const char *aTopic,
NS_IMETHODIMP nsPrefService::ReadUserPrefs(nsIFile *aFile) NS_IMETHODIMP nsPrefService::ReadUserPrefs(nsIFile *aFile)
{ {
if (NS_UNLIKELY(!CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (XRE_GetProcessType() == GeckoProcessType_Content) { if (XRE_GetProcessType() == GeckoProcessType_Content) {
NS_ERROR("cannot load prefs from content process"); NS_ERROR("cannot load prefs from content process");
@ -242,6 +248,10 @@ NS_IMETHODIMP nsPrefService::ReadUserPrefs(nsIFile *aFile)
NS_IMETHODIMP nsPrefService::ResetPrefs() NS_IMETHODIMP nsPrefService::ResetPrefs()
{ {
if (NS_UNLIKELY(!CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (XRE_GetProcessType() == GeckoProcessType_Content) { if (XRE_GetProcessType() == GeckoProcessType_Content) {
NS_ERROR("cannot set prefs from content process"); NS_ERROR("cannot set prefs from content process");
@ -260,6 +270,10 @@ NS_IMETHODIMP nsPrefService::ResetPrefs()
NS_IMETHODIMP nsPrefService::ResetUserPrefs() NS_IMETHODIMP nsPrefService::ResetUserPrefs()
{ {
if (NS_UNLIKELY(!CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (XRE_GetProcessType() == GeckoProcessType_Content) { if (XRE_GetProcessType() == GeckoProcessType_Content) {
NS_ERROR("cannot set prefs from content process"); NS_ERROR("cannot set prefs from content process");
@ -273,6 +287,10 @@ NS_IMETHODIMP nsPrefService::ResetUserPrefs()
NS_IMETHODIMP nsPrefService::SavePrefFile(nsIFile *aFile) NS_IMETHODIMP nsPrefService::SavePrefFile(nsIFile *aFile)
{ {
if (NS_UNLIKELY(!CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
#ifdef MOZ_IPC #ifdef MOZ_IPC
if (XRE_GetProcessType() == GeckoProcessType_Content) { if (XRE_GetProcessType() == GeckoProcessType_Content) {
NS_ERROR("cannot save prefs from content process"); NS_ERROR("cannot save prefs from content process");
@ -361,6 +379,10 @@ NS_IMETHODIMP nsPrefService::MirrorPreferences(nsTArray<PrefTuple, nsTArrayInfal
NS_IMETHODIMP nsPrefService::GetBranch(const char *aPrefRoot, nsIPrefBranch **_retval) NS_IMETHODIMP nsPrefService::GetBranch(const char *aPrefRoot, nsIPrefBranch **_retval)
{ {
if (NS_UNLIKELY(!CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
nsresult rv; nsresult rv;
if ((nsnull != aPrefRoot) && (*aPrefRoot != '\0')) { if ((nsnull != aPrefRoot) && (*aPrefRoot != '\0')) {
@ -379,6 +401,10 @@ NS_IMETHODIMP nsPrefService::GetBranch(const char *aPrefRoot, nsIPrefBranch **_r
NS_IMETHODIMP nsPrefService::GetDefaultBranch(const char *aPrefRoot, nsIPrefBranch **_retval) NS_IMETHODIMP nsPrefService::GetDefaultBranch(const char *aPrefRoot, nsIPrefBranch **_retval)
{ {
if (NS_UNLIKELY(!CheckAndLogBackgroundThreadUse())) {
return NS_ERROR_UNEXPECTED;
}
nsresult rv; nsresult rv;
// TODO: - cache this stuff and allow consumers to share branches (hold weak references I think) // TODO: - cache this stuff and allow consumers to share branches (hold weak references I think)
@ -933,3 +959,22 @@ static nsresult pref_InitInitialObjects()
return pref_LoadPrefsInDirList(NS_EXT_PREFS_DEFAULTS_DIR_LIST); return pref_LoadPrefsInDirList(NS_EXT_PREFS_DEFAULTS_DIR_LIST);
} }
// static
bool
nsPrefService::CheckAndLogBackgroundThreadUse()
{
if (NS_IsMainThread()) {
return true;
}
NS_WARNING("Cannot be used on a background thread!");
nsCOMPtr<nsIConsoleService> cs =
do_GetService("@mozilla.org/consoleservice;1");
if (cs) {
(void)cs->LogStringMessage(NS_LITERAL_STRING(
"Invalid use of the preferences on a background thread!"
).get());
}
return false;
}

Просмотреть файл

@ -67,7 +67,13 @@ public:
virtual ~nsPrefService(); virtual ~nsPrefService();
nsresult Init(); nsresult Init();
/**
* Checks to make sure we are on the main thread and log if we are not.
*
* @returns true if on the main thread, and false otherwise.
*/
static bool CheckAndLogBackgroundThreadUse();
protected: protected:
nsresult NotifyServiceObservers(const char *aSubject); nsresult NotifyServiceObservers(const char *aSubject);
nsresult UseDefaultPrefFile(); nsresult UseDefaultPrefFile();