LCOV - code coverage report
Current view: directory - dom/indexedDB - IDBTransaction.h (source / functions) Found Hit Coverage
Test: app.info Lines: 39 27 69.2 %
Date: 2012-06-02 Functions: 15 11 73.3 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* vim: set ts=2 et sw=2 tw=80: */
       3                 : /* ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is Indexed Database.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * The Mozilla Foundation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2010
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Ben Turner <bent.mozilla@gmail.com>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef mozilla_dom_indexeddb_idbtransaction_h__
      41                 : #define mozilla_dom_indexeddb_idbtransaction_h__
      42                 : 
      43                 : #include "mozilla/dom/indexedDB/IndexedDatabase.h"
      44                 : 
      45                 : #include "mozIStorageConnection.h"
      46                 : #include "mozIStorageStatement.h"
      47                 : #include "mozIStorageFunction.h"
      48                 : #include "nsIIDBTransaction.h"
      49                 : #include "nsIRunnable.h"
      50                 : #include "nsIThreadInternal.h"
      51                 : 
      52                 : #include "nsAutoPtr.h"
      53                 : #include "nsClassHashtable.h"
      54                 : #include "nsHashKeys.h"
      55                 : #include "nsInterfaceHashtable.h"
      56                 : 
      57                 : #include "mozilla/dom/indexedDB/IDBDatabase.h"
      58                 : #include "mozilla/dom/indexedDB/IDBWrapperCache.h"
      59                 : #include "mozilla/dom/indexedDB/FileInfo.h"
      60                 : 
      61                 : class nsIThread;
      62                 : 
      63                 : BEGIN_INDEXEDDB_NAMESPACE
      64                 : 
      65                 : class AsyncConnectionHelper;
      66                 : class CommitHelper;
      67                 : struct ObjectStoreInfo;
      68                 : class TransactionThreadPool;
      69                 : class UpdateRefcountFunction;
      70                 : 
      71                 : class IDBTransactionListener
      72                 : {
      73                 : public:
      74                 :   NS_IMETHOD_(nsrefcnt) AddRef() = 0;
      75                 :   NS_IMETHOD_(nsrefcnt) Release() = 0;
      76                 : 
      77                 :   virtual nsresult NotifyTransactionComplete(IDBTransaction* aTransaction) = 0;
      78                 : };
      79                 : 
      80                 : class IDBTransaction : public IDBWrapperCache,
      81                 :                        public nsIIDBTransaction,
      82                 :                        public nsIThreadObserver
      83                 : {
      84                 :   friend class AsyncConnectionHelper;
      85                 :   friend class CommitHelper;
      86                 :   friend class ThreadObserver;
      87                 :   friend class TransactionThreadPool;
      88                 : 
      89                 : public:
      90                 :   NS_DECL_ISUPPORTS_INHERITED
      91                 :   NS_DECL_NSIIDBTRANSACTION
      92                 :   NS_DECL_NSITHREADOBSERVER
      93                 : 
      94            2502 :   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBTransaction, IDBWrapperCache)
      95                 : 
      96                 :   enum Mode
      97                 :   {
      98                 :     READ_ONLY = 0,
      99                 :     READ_WRITE,
     100                 :     VERSION_CHANGE
     101                 :   };
     102                 : 
     103                 :   enum ReadyState
     104                 :   {
     105                 :     INITIAL = 0,
     106                 :     LOADING,
     107                 :     COMMITTING,
     108                 :     DONE
     109                 :   };
     110                 : 
     111                 :   static already_AddRefed<IDBTransaction>
     112                 :   Create(IDBDatabase* aDatabase,
     113                 :          nsTArray<nsString>& aObjectStoreNames,
     114                 :          Mode aMode,
     115                 :          bool aDispatchDelayed);
     116                 : 
     117                 :   // nsIDOMEventTarget
     118                 :   virtual nsresult PreHandleEvent(nsEventChainPreVisitor& aVisitor);
     119                 : 
     120                 :   void OnNewRequest();
     121                 :   void OnRequestFinished();
     122                 : 
     123                 :   void RemoveObjectStore(const nsAString& aName);
     124                 : 
     125                 :   void SetTransactionListener(IDBTransactionListener* aListener);
     126                 : 
     127                 :   bool StartSavepoint();
     128                 :   nsresult ReleaseSavepoint();
     129                 :   void RollbackSavepoint();
     130                 : 
     131                 :   // Only meant to be called on mStorageThread!
     132                 :   nsresult GetOrCreateConnection(mozIStorageConnection** aConnection);
     133                 : 
     134                 :   already_AddRefed<mozIStorageStatement>
     135                 :   GetCachedStatement(const nsACString& aQuery);
     136                 : 
     137                 :   template<int N>
     138                 :   already_AddRefed<mozIStorageStatement>
     139                 :   GetCachedStatement(const char (&aQuery)[N])
     140                 :   {
     141                 :     return GetCachedStatement(NS_LITERAL_CSTRING(aQuery));
     142                 :   }
     143                 : 
     144                 :   bool IsOpen() const;
     145                 : 
     146                 :   bool IsWriteAllowed() const
     147                 :   {
     148                 :     return mMode == READ_WRITE || mMode == VERSION_CHANGE;
     149                 :   }
     150                 : 
     151                 :   bool IsAborted() const
     152                 :   {
     153                 :     return mAborted;
     154                 :   }
     155                 : 
     156                 :   // 'Get' prefix is to avoid name collisions with the enum
     157               0 :   Mode GetMode()
     158                 :   {
     159               0 :     return mMode;
     160                 :   }
     161                 : 
     162               0 :   IDBDatabase* Database()
     163                 :   {
     164               0 :     NS_ASSERTION(mDatabase, "This should never be null!");
     165               0 :     return mDatabase;
     166                 :   }
     167                 : 
     168                 :   DatabaseInfo* DBInfo() const
     169                 :   {
     170                 :     return mDatabaseInfo;
     171                 :   }
     172                 : 
     173                 :   already_AddRefed<IDBObjectStore>
     174                 :   GetOrCreateObjectStore(const nsAString& aName,
     175                 :                          ObjectStoreInfo* aObjectStoreInfo);
     176                 : 
     177                 :   void OnNewFileInfo(FileInfo* aFileInfo);
     178                 : 
     179                 :   void ClearCreatedFileInfos();
     180                 : 
     181                 : private:
     182                 :   IDBTransaction();
     183                 :   ~IDBTransaction();
     184                 : 
     185                 :   nsresult CommitOrRollback();
     186                 : 
     187                 :   nsRefPtr<IDBDatabase> mDatabase;
     188                 :   nsRefPtr<DatabaseInfo> mDatabaseInfo;
     189                 :   nsTArray<nsString> mObjectStoreNames;
     190                 :   ReadyState mReadyState;
     191                 :   Mode mMode;
     192                 :   PRUint32 mPendingRequests;
     193                 :   PRUint32 mCreatedRecursionDepth;
     194                 : 
     195                 :   // Only touched on the main thread.
     196                 :   NS_DECL_EVENT_HANDLER(error)
     197                 :   NS_DECL_EVENT_HANDLER(complete)
     198                 :   NS_DECL_EVENT_HANDLER(abort)
     199                 : 
     200                 :   nsInterfaceHashtable<nsCStringHashKey, mozIStorageStatement>
     201                 :     mCachedStatements;
     202                 : 
     203                 :   nsRefPtr<IDBTransactionListener> mListener;
     204                 : 
     205                 :   // Only touched on the database thread.
     206                 :   nsCOMPtr<mozIStorageConnection> mConnection;
     207                 : 
     208                 :   // Only touched on the database thread.
     209                 :   PRUint32 mSavepointCount;
     210                 : 
     211                 :   nsTArray<nsRefPtr<IDBObjectStore> > mCreatedObjectStores;
     212                 : 
     213                 :   bool mAborted;
     214                 :   bool mCreating;
     215                 : 
     216                 : #ifdef DEBUG
     217                 :   bool mFiredCompleteOrAbort;
     218                 : #endif
     219                 : 
     220                 :   nsRefPtr<UpdateRefcountFunction> mUpdateFileRefcountFunction;
     221                 :   nsTArray<nsRefPtr<FileInfo> > mCreatedFileInfos;
     222                 : };
     223                 : 
     224                 : class CommitHelper : public nsIRunnable
     225                 : {
     226                 : public:
     227                 :   NS_DECL_ISUPPORTS
     228                 :   NS_DECL_NSIRUNNABLE
     229                 : 
     230                 :   CommitHelper(IDBTransaction* aTransaction,
     231                 :                IDBTransactionListener* aListener,
     232                 :                const nsTArray<nsRefPtr<IDBObjectStore> >& mUpdatedObjectStores);
     233                 :   ~CommitHelper();
     234                 : 
     235                 :   template<class T>
     236            2053 :   bool AddDoomedObject(nsCOMPtr<T>& aCOMPtr)
     237                 :   {
     238            2053 :     if (aCOMPtr) {
     239            2053 :       if (!mDoomedObjects.AppendElement(do_QueryInterface(aCOMPtr))) {
     240               0 :         NS_ERROR("Out of memory!");
     241               0 :         return false;
     242                 :       }
     243            2053 :       aCOMPtr = nsnull;
     244                 :     }
     245            2053 :     return true;
     246                 :   }
     247                 : 
     248                 : private:
     249                 :   // Writes new autoincrement counts to database
     250                 :   nsresult WriteAutoIncrementCounts();
     251                 : 
     252                 :   // Updates counts after a successful commit
     253                 :   void CommitAutoIncrementCounts();
     254                 : 
     255                 :   // Reverts counts when a transaction is aborted
     256                 :   void RevertAutoIncrementCounts();
     257                 : 
     258                 :   nsRefPtr<IDBTransaction> mTransaction;
     259                 :   nsRefPtr<IDBTransactionListener> mListener;
     260                 :   nsCOMPtr<mozIStorageConnection> mConnection;
     261                 :   nsRefPtr<UpdateRefcountFunction> mUpdateFileRefcountFunction;
     262                 :   nsAutoTArray<nsCOMPtr<nsISupports>, 10> mDoomedObjects;
     263                 :   nsAutoTArray<nsRefPtr<IDBObjectStore>, 10> mAutoIncrementObjectStores;
     264                 : 
     265                 :   bool mAborted;
     266                 : };
     267                 : 
     268                 : class UpdateRefcountFunction : public mozIStorageFunction
     269                 : {
     270                 : public:
     271                 :   NS_DECL_ISUPPORTS
     272                 :   NS_DECL_MOZISTORAGEFUNCTION
     273                 : 
     274             367 :   UpdateRefcountFunction(FileManager* aFileManager)
     275             367 :   : mFileManager(aFileManager)
     276             367 :   { }
     277                 : 
     278             367 :   ~UpdateRefcountFunction()
     279             367 :   { }
     280                 : 
     281                 :   nsresult Init();
     282                 : 
     283             367 :   void ClearFileInfoEntries()
     284                 :   {
     285             367 :     mFileInfoEntries.Clear();
     286             367 :   }
     287                 : 
     288             362 :   nsresult UpdateDatabase(mozIStorageConnection* aConnection)
     289                 :   {
     290             724 :     DatabaseUpdateFunction function(aConnection);
     291                 : 
     292             362 :     mFileInfoEntries.EnumerateRead(DatabaseUpdateCallback, &function);
     293                 : 
     294             362 :     return function.ErrorCode();
     295                 :   }
     296                 : 
     297             362 :   void UpdateFileInfos()
     298                 :   {
     299             362 :     mFileInfoEntries.EnumerateRead(FileInfoUpdateCallback, nsnull);
     300             362 :   }
     301                 : 
     302                 : private:
     303                 :   class FileInfoEntry
     304                 :   {
     305                 :   public:
     306               0 :     FileInfoEntry(FileInfo* aFileInfo)
     307               0 :     : mFileInfo(aFileInfo), mDelta(0)
     308               0 :     { }
     309                 : 
     310               0 :     ~FileInfoEntry()
     311               0 :     { }
     312                 : 
     313                 :     nsRefPtr<FileInfo> mFileInfo;
     314                 :     PRInt32 mDelta;
     315                 :   };
     316                 : 
     317                 :   enum UpdateType {
     318                 :     eIncrement,
     319                 :     eDecrement
     320                 :   };
     321                 : 
     322                 :   class DatabaseUpdateFunction
     323             362 :   {
     324                 :   public:
     325             362 :     DatabaseUpdateFunction(mozIStorageConnection* aConnection)
     326             362 :     : mConnection(aConnection), mErrorCode(NS_OK)
     327             362 :     { }
     328                 : 
     329                 :     bool Update(PRInt64 aId, PRInt32 aDelta);
     330             362 :     nsresult ErrorCode()
     331                 :     {
     332             362 :       return mErrorCode;
     333                 :     }
     334                 : 
     335                 :   private:
     336                 :     nsresult UpdateInternal(PRInt64 aId, PRInt32 aDelta);
     337                 : 
     338                 :     nsCOMPtr<mozIStorageConnection> mConnection;
     339                 :     nsCOMPtr<mozIStorageStatement> mUpdateStatement;
     340                 :     nsCOMPtr<mozIStorageStatement> mInsertStatement;
     341                 : 
     342                 :     nsresult mErrorCode;
     343                 :   };
     344                 : 
     345                 :   nsresult ProcessValue(mozIStorageValueArray* aValues,
     346                 :                         PRInt32 aIndex,
     347                 :                         UpdateType aUpdateType);
     348                 : 
     349                 :   static PLDHashOperator
     350                 :   DatabaseUpdateCallback(const PRUint64& aKey,
     351                 :                          FileInfoEntry* aValue,
     352                 :                          void* aUserArg);
     353                 : 
     354                 :   static PLDHashOperator
     355                 :   FileInfoUpdateCallback(const PRUint64& aKey,
     356                 :                          FileInfoEntry* aValue,
     357                 :                          void* aUserArg);
     358                 : 
     359                 :   FileManager* mFileManager;
     360                 :   nsClassHashtable<nsUint64HashKey, FileInfoEntry> mFileInfoEntries;
     361                 : };
     362                 : 
     363                 : END_INDEXEDDB_NAMESPACE
     364                 : 
     365                 : #endif // mozilla_dom_indexeddb_idbtransaction_h__

Generated by: LCOV version 1.7