Bug 660743 storage should use mozilla::Preferences r=roc+sdwilsh
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 02 Jun 2011 10:05:12 +0900
changeset 71135 82a5618aa8c656d88f58bc6385cf19472dba2853
parent 71134 f81b4d9534f5f51f1b66654f3ec60400ad3cf903
child 71136 a4107cfe6b211679caf99d37a50f4084a755dc18
push id45
push userffxbld
push dateThu, 22 Sep 2011 17:29:26 +0000
treeherdermozilla-release@b3273da80b44 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs660743
milestone7.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 660743 storage should use mozilla::Preferences r=roc+sdwilsh
storage/src/VacuumManager.cpp
storage/src/VacuumManager.h
storage/src/mozStorageService.cpp
--- a/storage/src/VacuumManager.cpp
+++ b/storage/src/VacuumManager.cpp
@@ -35,16 +35,17 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "VacuumManager.h"
 
 #include "mozilla/Services.h"
+#include "mozilla/Preferences.h"
 #include "nsIObserverService.h"
 #include "nsPrintfCString.h"
 #include "nsIFile.h"
 #include "nsThreadUtils.h"
 #include "prlog.h"
 
 #include "mozStorageConnection.h"
 #include "mozIStorageStatement.h"
@@ -132,25 +133,23 @@ NS_IMPL_ISUPPORTS1(
 //////////////////////////////////////////////////////////////////////////////// 
 //// Vacuumer declaration.
 
 class Vacuumer : public BaseCallback
 {
 public:
   NS_DECL_MOZISTORAGESTATEMENTCALLBACK
 
-  Vacuumer(mozIStorageVacuumParticipant *aParticipant,
-           nsIPrefBranch *aPrefBranch);
+  Vacuumer(mozIStorageVacuumParticipant *aParticipant);
 
   bool execute();
   nsresult notifyCompletion(bool aSucceeded);
 
 private:
   nsCOMPtr<mozIStorageVacuumParticipant> mParticipant;
-  nsCOMPtr<nsIPrefBranch> mPrefBranch;
   nsCString mDBFilename;
   nsCOMPtr<mozIStorageConnection> mDBConn;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
 // NotifyCallback
 /**
  * This class handles an async statement execution and notifies completion
@@ -183,20 +182,18 @@ NotifyCallback::HandleCompletion(PRUint1
   nsresult rv = mVacuumer->notifyCompletion(mVacuumSucceeded);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Vacuumer implementation.
 
-Vacuumer::Vacuumer(mozIStorageVacuumParticipant *aParticipant,
-                   nsIPrefBranch *aPrefBranch)
+Vacuumer::Vacuumer(mozIStorageVacuumParticipant *aParticipant)
   : mParticipant(aParticipant)
-  , mPrefBranch(aPrefBranch)
 {
 }
 
 bool
 Vacuumer::execute()
 {
   NS_PRECONDITION(NS_IsMainThread(), "Must be running on the main thread!");
 
@@ -269,17 +266,19 @@ Vacuumer::execute()
   rv = databaseFile->GetLeafName(databaseFilename);
   NS_ENSURE_SUCCESS(rv, false);
   mDBFilename = NS_ConvertUTF16toUTF8(databaseFilename);
   NS_ASSERTION(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
 
   // Check interval from last vacuum.
   PRInt32 now = static_cast<PRInt32>(PR_Now() / PR_USEC_PER_SEC);
   PRInt32 lastVacuum;
-  rv = mPrefBranch->GetIntPref(mDBFilename.get(), &lastVacuum);
+  nsCAutoString prefName(PREF_VACUUM_BRANCH);
+  prefName += mDBFilename;
+  rv = Preferences::GetInt(prefName.get(), &lastVacuum);
   if (NS_SUCCEEDED(rv) && (now - lastVacuum) < VACUUM_INTERVAL_SECONDS &&
       !canOptimizePageSize) {
     // This database was vacuumed recently and has optimal page size, skip it. 
     return false;
   }
 
   // Notify that we are about to start vacuuming.  The participant can opt-out
   // if it cannot handle a vacuum at this time, and then we'll move to the next
@@ -373,17 +372,19 @@ Vacuumer::HandleResult(mozIStorageResult
 
 NS_IMETHODIMP
 Vacuumer::HandleCompletion(PRUint16 aReason)
 {
   if (aReason == REASON_FINISHED) {
     // Update last vacuum time.
     PRInt32 now = static_cast<PRInt32>(PR_Now() / PR_USEC_PER_SEC);
     NS_ASSERTION(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
-    (void)mPrefBranch->SetIntPref(mDBFilename.get(), now);
+    nsCAutoString prefName(PREF_VACUUM_BRANCH);
+    prefName += mDBFilename;
+    (void)Preferences::SetInt(prefName.get(), now);
   }
 
   notifyCompletion(aReason == REASON_FINISHED);
 
   return NS_OK;
 }
 
 nsresult
@@ -419,45 +420,28 @@ VacuumManager::getSingleton()
 {
   if (gVacuumManager) {
     NS_ADDREF(gVacuumManager);
     return gVacuumManager;
   }
   gVacuumManager = new VacuumManager();
   if (gVacuumManager) {
     NS_ADDREF(gVacuumManager);
-    if (NS_FAILED(gVacuumManager->initialize())) {
-      NS_RELEASE(gVacuumManager);
-    }
   }
   return gVacuumManager;
 }
 
 VacuumManager::VacuumManager()
   : mParticipants("vacuum-participant")
 {
   NS_ASSERTION(!gVacuumManager,
                "Attempting to create two instances of the service!");
   gVacuumManager = this;
 }
 
-nsresult
-VacuumManager::initialize()
-{
-  NS_PRECONDITION(NS_IsMainThread(), "Must be running on the main thread!");
-
-  // Used to store last vacuum times.
-  nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
-  NS_ENSURE_STATE(prefs);
-  prefs->GetBranch(PREF_VACUUM_BRANCH, getter_AddRefs(mPrefBranch));
-  NS_ENSURE_STATE(mPrefBranch);
-
-  return NS_OK;
-}
-
 VacuumManager::~VacuumManager()
 {
   // Remove the static reference to the service.  Check to make sure its us
   // in case somebody creates an extra instance of the service.
   NS_ASSERTION(gVacuumManager == this,
                "Deleting a non-singleton instance of the service");
   if (gVacuumManager == this) {
     gVacuumManager = nsnull;
@@ -474,29 +458,29 @@ VacuumManager::Observe(nsISupports *aSub
 {
   if (strcmp(aTopic, OBSERVER_TOPIC_IDLE_DAILY) == 0) {
     // Try to run vacuum on all registered entries.  Will stop at the first
     // successful one.
     const nsCOMArray<mozIStorageVacuumParticipant> &entries =
       mParticipants.GetEntries();
     // If there are more entries than what a month can contain, we could end up
     // skipping some, since we run daily.  So we use a starting index.
-    PRInt32 startIndex = 0, index;
-    (void)mPrefBranch->GetIntPref(NS_LITERAL_CSTRING("index").get(), &startIndex);
+    static const char* kPrefName = PREF_VACUUM_BRANCH "index";
+    PRInt32 startIndex = Preferences::GetInt(kPrefName, 0);
     if (startIndex >= entries.Count()) {
       startIndex = 0;
     }
+    PRInt32 index;
     for (index = startIndex; index < entries.Count(); ++index) {
-      nsCOMPtr<Vacuumer> vacuum = new Vacuumer(entries[index], mPrefBranch);
-      NS_ENSURE_STATE(vacuum);
+      nsCOMPtr<Vacuumer> vacuum = new Vacuumer(entries[index]);
       // Only vacuum one database per day.
       if (vacuum->execute()) {
         break;
       }
     }
-    (void)mPrefBranch->SetIntPref(NS_LITERAL_CSTRING("index").get(), index);
+    (void)Preferences::SetInt(kPrefName, index);
   }
 
   return NS_OK;
 }
 
 } // namespace storage
 } // namespace mozilla
--- a/storage/src/VacuumManager.h
+++ b/storage/src/VacuumManager.h
@@ -40,17 +40,16 @@
 #ifndef mozilla_storage_VacuumManager_h__
 #define mozilla_storage_VacuumManager_h__
 
 #include "nsCOMPtr.h"
 #include "nsIObserver.h"
 #include "mozIStorageStatementCallback.h"
 #include "mozIStorageVacuumParticipant.h"
 #include "nsCategoryCache.h"
-#include "nsIPrefService.h"
 
 namespace mozilla {
 namespace storage {
 
 class VacuumManager : public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
@@ -58,29 +57,21 @@ public:
 
   VacuumManager();
 
   /**
    * Obtains the VacuumManager object.
    */
   static VacuumManager * getSingleton();
 
-  /**
-   * Initializes the VacuumManager object.  Must be called just once.
-   */
-  nsresult initialize();
-
 private:
   ~VacuumManager();
 
   static VacuumManager *gVacuumManager;
 
   // Cache of components registered in "vacuum-participant" category.
   nsCategoryCache<mozIStorageVacuumParticipant> mParticipants;
-
-  // Pref branch for PREF_VACUUM_BRANCH.
-  nsCOMPtr<nsIPrefBranch> mPrefBranch;
 };
 
 } // namespace storage
 } // namespace mozilla
 
 #endif
--- a/storage/src/mozStorageService.cpp
+++ b/storage/src/mozStorageService.cpp
@@ -49,18 +49,17 @@
 #include "nsEmbedCID.h"
 #include "nsThreadUtils.h"
 #include "mozStoragePrivateHelpers.h"
 #include "nsILocale.h"
 #include "nsILocaleService.h"
 #include "nsIXPConnect.h"
 #include "nsIObserverService.h"
 #include "mozilla/Services.h"
-#include "nsIPrefService.h"
-#include "nsIPrefBranch.h"
+#include "mozilla/Preferences.h"
 
 #include "sqlite3.h"
 #include "test_quota.c"
 
 #include "nsIPromptService.h"
 #include "nsIMemoryReporter.h"
 
 #include "mozilla/FunctionTimer.h"
@@ -180,20 +179,18 @@ public:
 
     // We cache XPConnect for our language helpers.  XPConnect can only be
     // used on the main thread.
     (void)CallGetService(nsIXPConnect::GetCID(), mXPConnectPtr);
 
     // We need to obtain the toolkit.storage.synchronous preferences on the main
     // thread because the preference service can only be accessed there.  This
     // is cached in the service for all future Open[Unshared]Database calls.
-    nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID));
-    PRInt32 synchronous = PREF_TS_SYNCHRONOUS_DEFAULT;
-    if (pref)
-      (void)pref->GetIntPref(PREF_TS_SYNCHRONOUS, &synchronous);
+    PRInt32 synchronous =
+      Preferences::GetInt(PREF_TS_SYNCHRONOUS, PREF_TS_SYNCHRONOUS_DEFAULT);
     ::PR_ATOMIC_SET(mSynchronousPrefValPtr, synchronous);
 
     // Register our SQLite memory reporter.  Registration can only happen on
     // the main thread (otherwise you'll get cryptic crashes).
     NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(StorageSQLiteMemoryUsed));
 
     return NS_OK;
   }