bug 468744 - finalize sqlite statements before closing db to avoid sqlite warnings; r=sdwilsh
authorDan Witte <dwitte@gmail.com>
Wed, 10 Dec 2008 15:54:57 -0800
changeset 22579 fd7c994ec1e9ddceec2a5b7cf778dc3a5f99edc4
parent 22577 1ea7f41ebd97f695fde508c4d94a678bc50db963
child 22580 4f38b5babb800a840ecb64aef103e33e1583edb0
push id4085
push userdwitte@stanford.edu
push dateThu, 11 Dec 2008 00:21:54 +0000
treeherdermozilla-central@4f38b5babb80 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssdwilsh
bugs468744
milestone1.9.2a1pre
bug 468744 - finalize sqlite statements before closing db to avoid sqlite warnings; r=sdwilsh
netwerk/cookie/src/nsCookieService.cpp
netwerk/cookie/src/nsCookieService.h
--- a/netwerk/cookie/src/nsCookieService.cpp
+++ b/netwerk/cookie/src/nsCookieService.cpp
@@ -404,21 +404,21 @@ nsCookieService::GetSingleton()
 NS_IMPL_ISUPPORTS5(nsCookieService,
                    nsICookieService,
                    nsICookieManager,
                    nsICookieManager2,
                    nsIObserver,
                    nsISupportsWeakReference)
 
 nsCookieService::nsCookieService()
- : mCookieCount(0)
+ : mHostTable(&mDefaultHostTable)
+ , mCookieCount(0)
  , mCookiesPermissions(BEHAVIOR_ACCEPT)
  , mMaxNumberOfCookies(kMaxNumberOfCookies)
  , mMaxCookiesPerHost(kMaxCookiesPerHost)
- , mHostTable(&mDefaultHostTable)
 {
 }
 
 nsresult
 nsCookieService::Init()
 {
   if (!mHostTable->Init()) {
     return NS_ERROR_OUT_OF_MEMORY;
@@ -467,16 +467,19 @@ nsCookieService::Init()
   }
 
   return NS_OK;
 }
 
 nsresult
 nsCookieService::InitDB()
 {
+  // null out any existing connection
+  CloseDB();
+
   nsCOMPtr<nsIFile> cookieFile;
   nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(cookieFile));
   if (NS_FAILED(rv)) return rv;
 
   cookieFile->AppendNative(NS_LITERAL_CSTRING(kCookieFileName));
 
   nsCOMPtr<mozIStorageService> storage = do_GetService("@mozilla.org/storage/service;1");
   if (!storage)
@@ -613,18 +616,31 @@ nsCookieService::CreateTable()
 
   // create the table
   return mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     "CREATE TABLE moz_cookies ("
     "id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,"
     "expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER)"));
 }
 
+void
+nsCookieService::CloseDB()
+{
+  // finalize our statements and close the db connection.
+  // since we own these objects, nulling the pointers is sufficient here.
+  mStmtInsert = nsnull;
+  mStmtDelete = nsnull;
+  mStmtUpdate = nsnull;
+  mDBConn = nsnull;
+}
+
 nsCookieService::~nsCookieService()
 {
+  CloseDB();
+
   gCookieService = nsnull;
 }
 
 NS_IMETHODIMP
 nsCookieService::Observe(nsISupports     *aSubject,
                          const char      *aTopic,
                          const PRUnichar *aData)
 {
@@ -638,18 +654,17 @@ nsCookieService::Observe(nsISupports    
       if (!nsCRT::strcmp(aData, NS_LITERAL_STRING("shutdown-cleanse").get())) {
         // clear the cookie file
         nsresult rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM moz_cookies"));
         if (NS_FAILED(rv))
           NS_WARNING("db delete failed");
       }
 
       // Close the DB connection before changing
-      mDBConn->Close();
-      mDBConn = nsnull;
+      CloseDB();
     }
 
   } else if (!strcmp(aTopic, "profile-do-change")) {
     // the profile has already changed; init the db from the new location
     InitDB();
 
   } else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
     nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
@@ -659,20 +674,17 @@ nsCookieService::Observe(nsISupports    
     if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_ENTER).Equals(aData)) {
       // backup the existing in-memory DB
       if (mPrivateHostTable.IsInitialized() || mPrivateHostTable.Init()) {
         mHostTable = &mPrivateHostTable;
         mCookieCount = mHostTable->Count();
         NotifyChanged(nsnull, NS_LITERAL_STRING("reload").get());
       }
       // close the connection to the on-disk DB
-      mStmtInsert = nsnull;
-      mStmtDelete = nsnull;
-      mStmtUpdate = nsnull;
-      mDBConn = nsnull;
+      CloseDB();
       // continue to use the in-memory DB
     } else if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData)) {
       // open the connection to the on-disk DB
       InitDB();
       // restore the in-memory DB as it was prior to private browsing
       if (mPrivateHostTable.IsInitialized())
         mPrivateHostTable.Clear();
       mHostTable = &mDefaultHostTable;
@@ -826,18 +838,19 @@ nsCookieService::RemoveAll()
 
   // clear the cookie file
   if (mDBConn) {
     nsresult rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM moz_cookies"));
     if (NS_FAILED(rv)) {
       // Database must be corrupted, so remove it completely.
       nsCOMPtr<nsIFile> dbFile;
       mDBConn->GetDatabaseFile(getter_AddRefs(dbFile));
-      mDBConn->Close();
+      CloseDB();
       dbFile->Remove(PR_FALSE);
+
       InitDB();
     }
   }
 
   NotifyChanged(nsnull, NS_LITERAL_STRING("cleared").get());
   return NS_OK;
 }
 
--- a/netwerk/cookie/src/nsCookieService.h
+++ b/netwerk/cookie/src/nsCookieService.h
@@ -161,16 +161,17 @@ class nsCookieService : public nsICookie
     virtual ~nsCookieService();
     static nsCookieService*       GetSingleton();
     nsresult                      Init();
 
   protected:
     void                          PrefChanged(nsIPrefBranch *aPrefBranch);
     nsresult                      InitDB();
     nsresult                      CreateTable();
+    void                          CloseDB();
     nsresult                      Read();
     void                          GetCookieInternal(nsIURI *aHostURI, nsIChannel *aChannel, PRBool aHttpBound, char **aCookie);
     nsresult                      SetCookieStringInternal(nsIURI *aHostURI, nsIPrompt *aPrompt, const char *aCookieHeader, const char *aServerTime, nsIChannel *aChannel, PRBool aFromHttp);
     PRBool                        SetCookieInternal(nsIURI *aHostURI, nsIChannel *aChannel, nsDependentCString &aCookieHeader, PRInt64 aServerTime, PRBool aFromHttp);
     void                          AddInternal(nsCookie *aCookie, PRInt64 aCurrentTime, nsIURI *aHostURI, const char *aCookieHeader, PRBool aFromHttp);
     void                          RemoveCookieFromList(nsListIter &aIter);
     PRBool                        AddCookieToList(nsCookie *aCookie, PRBool aWriteToDB = PR_TRUE);
     void                          UpdateCookieInList(nsCookie *aCookie, PRInt64 aLastAccessed);