Bug 619487: Assert when preferences are used off the main thread. r=dbaron
authorKyle Huey <khuey@kylehuey.com>
Wed, 05 Mar 2014 17:58:01 -0800
changeset 172174 c50ed275755175aad660d950a0882f6c2ff12af9
parent 172173 b8551123b3dae18333535e76e279555c5383cdf6
child 172175 b077aaea25e77f121ba0dada32f66520cfa5e7fb
push idunknown
push userunknown
push dateunknown
reviewersdbaron
bugs619487
milestone30.0a1
Bug 619487: Assert when preferences are used off the main thread. r=dbaron
modules/libpref/src/Preferences.cpp
modules/libpref/src/nsPrefBranch.h
modules/libpref/src/prefapi.cpp
--- a/modules/libpref/src/Preferences.cpp
+++ b/modules/libpref/src/Preferences.cpp
@@ -397,16 +397,20 @@ Preferences::GetInstanceForService()
   NS_ADDREF(sPreferences);
   return sPreferences;
 }
 
 // static
 bool
 Preferences::InitStaticMembers()
 {
+#ifndef MOZ_B2G
+  MOZ_ASSERT(NS_IsMainThread());
+#endif
+
   if (!sShutdown && !sPreferences) {
     nsCOMPtr<nsIPrefService> prefService =
       do_GetService(NS_PREFSERVICE_CONTRACTID);
   }
 
   return sPreferences != nullptr;
 }
 
--- a/modules/libpref/src/nsPrefBranch.h
+++ b/modules/libpref/src/nsPrefBranch.h
@@ -177,17 +177,17 @@ class PrefCallback : public PLDHashEntry
 };
 
 class nsPrefBranch : public nsIPrefBranchInternal,
                      public nsIObserver,
                      public nsSupportsWeakReference
 {
   friend class mozilla::PreferenceServiceReporter;
 public:
-  NS_DECL_THREADSAFE_ISUPPORTS
+  NS_DECL_ISUPPORTS
   NS_DECL_NSIPREFBRANCH
   NS_DECL_NSIPREFBRANCH2
   NS_DECL_NSIOBSERVER
 
   nsPrefBranch(const char *aPrefRoot, bool aDefaultBranch);
   virtual ~nsPrefBranch();
 
   int32_t GetRootLength() { return mPrefRootLength; }
@@ -230,17 +230,17 @@ private:
 
 class nsPrefLocalizedString : public nsIPrefLocalizedString,
                               public nsISupportsString
 {
 public:
   nsPrefLocalizedString();
   virtual ~nsPrefLocalizedString();
 
-  NS_DECL_THREADSAFE_ISUPPORTS
+  NS_DECL_ISUPPORTS
   NS_FORWARD_NSISUPPORTSSTRING(mUnicodeString->)
   NS_FORWARD_NSISUPPORTSPRIMITIVE(mUnicodeString->)
 
   nsresult Init();
 
 private:
   NS_IMETHOD GetData(char16_t**);
   NS_IMETHOD SetData(const char16_t* aData);
@@ -248,17 +248,17 @@ private:
 
   nsCOMPtr<nsISupportsString> mUnicodeString;
 };
 
 
 class nsRelativeFilePref : public nsIRelativeFilePref
 {
 public:
-  NS_DECL_THREADSAFE_ISUPPORTS
+  NS_DECL_ISUPPORTS
   NS_DECL_NSIRELATIVEFILEPREF
   
                 nsRelativeFilePref();
   virtual       ~nsRelativeFilePref();
   
 private:
   nsCOMPtr<nsIFile> mFile;
   nsCString mRelativeToKey;
--- a/modules/libpref/src/prefapi.cpp
+++ b/modules/libpref/src/prefapi.cpp
@@ -3,16 +3,17 @@
  * 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/. */
 
 #include "base/basictypes.h"
 
 #include "prefapi.h"
 #include "prefapi_private_data.h"
 #include "prefread.h"
+#include "MainThreadUtils.h"
 #include "nsReadableUtils.h"
 #include "nsCRT.h"
 
 #define PL_ARENA_CONST_ALIGN_MASK 3
 #include "plarena.h"
 
 #ifdef _WIN32
   #include "windows.h"
@@ -570,16 +571,20 @@ pref_DeleteItem(PLDHashTable *table, PLD
         return PL_DHASH_REMOVE;
 
     return PL_DHASH_NEXT;
 }
 
 nsresult
 PREF_DeleteBranch(const char *branch_name)
 {
+#ifndef MOZ_B2G
+    MOZ_ASSERT(NS_IsMainThread());
+#endif
+
     int len = (int)strlen(branch_name);
 
     if (!gHashTable.ops)
         return NS_ERROR_NOT_INITIALIZED;
 
     /* The following check insures that if the branch name already has a "."
      * at the end, we don't end up with a "..". This fixes an incompatibility
      * between nsIPref, which needs the period added, and nsIPrefBranch which
@@ -637,16 +642,20 @@ pref_ClearUserPref(PLDHashTable *table, 
         pref_DoCallback(pref->key);
     }
     return nextOp;
 }
 
 nsresult
 PREF_ClearAllUserPrefs()
 {
+#ifndef MOZ_B2G
+    MOZ_ASSERT(NS_IsMainThread());
+#endif
+
     if (!gHashTable.ops)
         return NS_ERROR_NOT_INITIALIZED;
 
     PL_DHashTableEnumerate(&gHashTable, pref_ClearUserPref, nullptr);
 
     gDirty = true;
     return NS_OK;
 }
@@ -711,27 +720,35 @@ static void pref_SetValue(PrefValue* old
         default:
             *oldValue = newValue;
     }
     gDirty = true;
 }
 
 PrefHashEntry* pref_HashTableLookup(const void *key)
 {
+#ifndef MOZ_B2G
+    MOZ_ASSERT(NS_IsMainThread());
+#endif
+
     PrefHashEntry* result =
         static_cast<PrefHashEntry*>(PL_DHashTableOperate(&gHashTable, key, PL_DHASH_LOOKUP));
 
     if (PL_DHASH_ENTRY_IS_FREE(result))
         return nullptr;
 
     return result;
 }
 
 nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, uint32_t flags)
 {
+#ifndef MOZ_B2G
+    MOZ_ASSERT(NS_IsMainThread());
+#endif
+
     if (!gHashTable.ops)
         return NS_ERROR_OUT_OF_MEMORY;
 
     PrefHashEntry* pref = static_cast<PrefHashEntry*>(PL_DHashTableOperate(&gHashTable, key, PL_DHASH_ADD));
 
     if (!pref)
         return NS_ERROR_OUT_OF_MEMORY;