Bug 663036 part.1 Make Preferences instance with service constructor r=roc
authorMasayuki Nakano <masayuki@d-toybox.com>
Sun, 12 Jun 2011 11:30:15 +0900
changeset 71202 d5ce62d06d206d176092674829a203f0dc5fd4e1
parent 71201 3f771ec754bbc2666b59d57bad4aa05a0fd6b961
child 71203 a1cebab4dc0ff0fee1a4f89e98fbaa99ea9a61a3
push idunknown
push userunknown
push dateunknown
reviewersroc
bugs663036
milestone7.0a1
Bug 663036 part.1 Make Preferences instance with service constructor r=roc
modules/libpref/public/Preferences.h
modules/libpref/src/Preferences.cpp
modules/libpref/src/nsPrefsFactory.cpp
--- a/modules/libpref/public/Preferences.h
+++ b/modules/libpref/public/Preferences.h
@@ -81,17 +81,17 @@ public:
   Preferences();
   virtual ~Preferences();
 
   nsresult Init();
 
   /**
    * Returns the singleton instance which is addreffed.
    */
-  static Preferences* GetInstance();
+  static Preferences* GetInstanceForService();
 
   /**
    * Finallizes global members.
    */
   static void Shutdown();
 
   /**
    * Returns shared pref service instance
@@ -260,14 +260,14 @@ private:
   nsCOMPtr<nsIFile>        mCurrentFile;
 
   static Preferences*      sPreferences;
   static PRBool            sShutdown;
 
   /**
    * Init static members.  TRUE if it succeeded.  Otherwise, FALSE.
    */
-  static PRBool InitStaticMembers();
+  static PRBool InitStaticMembers(PRBool aForService = PR_FALSE);
 };
 
 } // namespace mozilla
 
 #endif // mozilla_Preferences_h
--- a/modules/libpref/src/Preferences.cpp
+++ b/modules/libpref/src/Preferences.cpp
@@ -186,38 +186,48 @@ struct CacheData {
 };
 
 static nsTArray<nsAutoPtr<CacheData> >* gCacheData = nsnull;
 static nsRefPtrHashtable<ValueObserverHashKey,
                          ValueObserver>* gObserverTable = nsnull;
 
 // static
 Preferences*
-Preferences::GetInstance()
+Preferences::GetInstanceForService()
 {
   if (sPreferences) {
     NS_ADDREF(sPreferences);
     return sPreferences;
   }
 
   NS_ENSURE_TRUE(!sShutdown, nsnull);
 
-  InitStaticMembers();
+  InitStaticMembers(PR_TRUE);
   NS_IF_ADDREF(sPreferences);
   return sPreferences;
 }
 
 // static
 PRBool
-Preferences::InitStaticMembers()
+Preferences::InitStaticMembers(PRBool aForService)
 {
   if (sShutdown || sPreferences) {
     return sPreferences != nsnull;
   }
 
+  // If InitStaticMembers() isn't called for getting nsIPrefService,
+  // some global components needed by Preferences::Init() may not have been
+  // initialized yet.  Therefore, we must create the singleton instance via
+  // service manager.
+  if (!aForService) {
+    nsCOMPtr<nsIPrefService> prefService =
+      do_GetService(NS_PREFSERVICE_CONTRACTID);
+    return sPreferences != nsnull;
+  }
+
   sPreferences = new Preferences();
   NS_ADDREF(sPreferences);
   if (NS_FAILED(sPreferences->Init()) || !sPreferences->mRootBranch) {
     NS_RELEASE(sPreferences);
     return PR_FALSE;
   }
 
   gCacheData = new nsTArray<nsAutoPtr<CacheData> >();
--- a/modules/libpref/src/nsPrefsFactory.cpp
+++ b/modules/libpref/src/nsPrefsFactory.cpp
@@ -38,17 +38,18 @@
 
 #include "mozilla/ModuleUtils.h"
 #include "mozilla/Preferences.h"
 #include "nsPrefBranch.h"
 #include "prefapi.h"
 
 using namespace mozilla;
 
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(Preferences, Preferences::GetInstance)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(Preferences,
+                                         Preferences::GetInstanceForService)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrefLocalizedString, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsRelativeFilePref)
 
 static NS_DEFINE_CID(kPrefServiceCID, NS_PREFSERVICE_CID);
 static NS_DEFINE_CID(kPrefLocalizedStringCID, NS_PREFLOCALIZEDSTRING_CID);
 static NS_DEFINE_CID(kRelativeFilePrefCID, NS_RELATIVEFILEPREF_CID);
  
 static mozilla::Module::CIDEntry kPrefCIDs[] = {