2010-06-23 23:46:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
#ifndef mozilla_dom_indexeddb_idbtransaction_h__
|
|
|
|
#define mozilla_dom_indexeddb_idbtransaction_h__
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
#include "mozilla/dom/indexedDB/IndexedDatabase.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
#include "mozIStorageConnection.h"
|
|
|
|
#include "mozIStorageStatement.h"
|
|
|
|
#include "mozIStorageFunction.h"
|
2010-06-28 20:46:21 +04:00
|
|
|
#include "nsIIDBTransaction.h"
|
2012-06-25 23:15:17 +04:00
|
|
|
#include "nsIDOMDOMError.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
2011-12-16 11:34:24 +04:00
|
|
|
#include "nsClassHashtable.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsInterfaceHashtable.h"
|
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
#include "mozilla/dom/indexedDB/IDBDatabase.h"
|
|
|
|
#include "mozilla/dom/indexedDB/IDBWrapperCache.h"
|
|
|
|
#include "mozilla/dom/indexedDB/FileInfo.h"
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
class nsIThread;
|
|
|
|
|
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
class AsyncConnectionHelper;
|
|
|
|
class CommitHelper;
|
2012-06-25 23:15:17 +04:00
|
|
|
class IDBRequest;
|
2012-06-01 21:21:12 +04:00
|
|
|
class IndexedDBDatabaseChild;
|
|
|
|
class IndexedDBTransactionChild;
|
|
|
|
class IndexedDBTransactionParent;
|
2010-06-23 23:46:08 +04:00
|
|
|
struct ObjectStoreInfo;
|
|
|
|
class TransactionThreadPool;
|
2011-12-16 11:34:24 +04:00
|
|
|
class UpdateRefcountFunction;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
class IDBTransactionListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_IMETHOD_(nsrefcnt) AddRef() = 0;
|
|
|
|
NS_IMETHOD_(nsrefcnt) Release() = 0;
|
|
|
|
|
2012-06-26 19:09:25 +04:00
|
|
|
// Called just before dispatching the final events on the transaction.
|
|
|
|
virtual nsresult NotifyTransactionPreComplete(IDBTransaction* aTransaction) = 0;
|
|
|
|
// Called just after dispatching the final events on the transaction.
|
|
|
|
virtual nsresult NotifyTransactionPostComplete(IDBTransaction* aTransaction) = 0;
|
2011-10-20 20:10:56 +04:00
|
|
|
};
|
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
class IDBTransaction : public IDBWrapperCache,
|
2010-11-19 01:19:19 +03:00
|
|
|
public nsIIDBTransaction,
|
2012-04-07 00:40:10 +04:00
|
|
|
public nsIRunnable
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
friend class AsyncConnectionHelper;
|
|
|
|
friend class CommitHelper;
|
2012-06-01 21:21:12 +04:00
|
|
|
friend class IndexedDBDatabaseChild;
|
2010-11-16 00:49:49 +03:00
|
|
|
friend class ThreadObserver;
|
2010-06-23 23:46:08 +04:00
|
|
|
friend class TransactionThreadPool;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIIDBTRANSACTION
|
2012-04-07 00:40:10 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBTransaction, IDBWrapperCache)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-03-13 08:44:45 +04:00
|
|
|
enum Mode
|
|
|
|
{
|
|
|
|
READ_ONLY = 0,
|
|
|
|
READ_WRITE,
|
2012-06-01 21:21:12 +04:00
|
|
|
VERSION_CHANGE,
|
|
|
|
|
|
|
|
// Only needed for IPC serialization helper, should never be used in code.
|
|
|
|
MODE_INVALID
|
2012-03-13 08:44:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ReadyState
|
|
|
|
{
|
|
|
|
INITIAL = 0,
|
|
|
|
LOADING,
|
|
|
|
COMMITTING,
|
|
|
|
DONE
|
|
|
|
};
|
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
static already_AddRefed<IDBTransaction>
|
2010-09-10 02:15:38 +04:00
|
|
|
Create(IDBDatabase* aDatabase,
|
2010-06-23 23:46:08 +04:00
|
|
|
nsTArray<nsString>& aObjectStoreNames,
|
2012-03-13 08:44:45 +04:00
|
|
|
Mode aMode,
|
2012-06-01 21:21:12 +04:00
|
|
|
bool aDispatchDelayed)
|
|
|
|
{
|
|
|
|
return CreateInternal(aDatabase, aObjectStoreNames, aMode, aDispatchDelayed,
|
|
|
|
false);
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-06-24 06:18:00 +04:00
|
|
|
// nsIDOMEventTarget
|
2010-11-11 02:26:03 +03:00
|
|
|
virtual nsresult PreHandleEvent(nsEventChainPreVisitor& aVisitor);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
void OnNewRequest();
|
|
|
|
void OnRequestFinished();
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
void RemoveObjectStore(const nsAString& aName);
|
2011-12-04 21:39:01 +04:00
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
void SetTransactionListener(IDBTransactionListener* aListener);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
bool StartSavepoint();
|
|
|
|
nsresult ReleaseSavepoint();
|
|
|
|
void RollbackSavepoint();
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
// Only meant to be called on mStorageThread!
|
|
|
|
nsresult GetOrCreateConnection(mozIStorageConnection** aConnection);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
already_AddRefed<mozIStorageStatement>
|
|
|
|
GetCachedStatement(const nsACString& aQuery);
|
|
|
|
|
|
|
|
template<int N>
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
|
|
|
GetCachedStatement(const char (&aQuery)[N])
|
|
|
|
{
|
2011-12-17 04:40:47 +04:00
|
|
|
return GetCachedStatement(NS_LITERAL_CSTRING(aQuery));
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
bool IsOpen() const;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-06-26 19:09:25 +04:00
|
|
|
bool IsFinished() const
|
|
|
|
{
|
|
|
|
return mReadyState > LOADING;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
bool IsWriteAllowed() const
|
|
|
|
{
|
2012-03-13 08:44:45 +04:00
|
|
|
return mMode == READ_WRITE || mMode == VERSION_CHANGE;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
bool IsAborted() const
|
|
|
|
{
|
2012-06-01 21:21:12 +04:00
|
|
|
return NS_FAILED(mAbortCode);
|
2010-12-16 00:21:17 +03:00
|
|
|
}
|
|
|
|
|
2012-03-13 08:44:45 +04:00
|
|
|
// 'Get' prefix is to avoid name collisions with the enum
|
|
|
|
Mode GetMode()
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
|
|
|
return mMode;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
IDBDatabase* Database()
|
2010-08-27 00:57:25 +04:00
|
|
|
{
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_ASSERTION(mDatabase, "This should never be null!");
|
|
|
|
return mDatabase;
|
2010-08-27 00:57:25 +04:00
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
DatabaseInfo* DBInfo() const
|
|
|
|
{
|
|
|
|
return mDatabaseInfo;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:40 +03:00
|
|
|
already_AddRefed<IDBObjectStore>
|
|
|
|
GetOrCreateObjectStore(const nsAString& aName,
|
2012-06-01 21:21:12 +04:00
|
|
|
ObjectStoreInfo* aObjectStoreInfo,
|
|
|
|
bool aCreating);
|
2010-11-11 02:25:40 +03:00
|
|
|
|
2012-06-03 20:33:52 +04:00
|
|
|
already_AddRefed<FileInfo> GetFileInfo(nsIDOMBlob* aBlob);
|
|
|
|
void AddFileInfo(nsIDOMBlob* aBlob, FileInfo* aFileInfo);
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
void ClearCreatedFileInfos();
|
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
void
|
|
|
|
SetActor(IndexedDBTransactionChild* aActorChild)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aActorChild || !mActorChild, "Shouldn't have more than one!");
|
|
|
|
mActorChild = aActorChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetActor(IndexedDBTransactionParent* aActorParent)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aActorParent || !mActorParent,
|
|
|
|
"Shouldn't have more than one!");
|
|
|
|
mActorParent = aActorParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDBTransactionChild*
|
|
|
|
GetActorChild() const
|
|
|
|
{
|
|
|
|
return mActorChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ObjectStoreInternal(const nsAString& aName,
|
|
|
|
IDBObjectStore** _retval);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AbortWithCode(nsresult aAbortCode);
|
|
|
|
|
2012-06-25 23:15:17 +04:00
|
|
|
nsresult
|
|
|
|
Abort(IDBRequest* aRequest);
|
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsresult
|
|
|
|
GetAbortCode() const
|
|
|
|
{
|
|
|
|
return mAbortCode;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
2012-06-01 21:21:12 +04:00
|
|
|
// Should only be called directly through IndexedDBDatabaseChild.
|
|
|
|
static already_AddRefed<IDBTransaction>
|
|
|
|
CreateInternal(IDBDatabase* aDatabase,
|
|
|
|
nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
Mode aMode,
|
|
|
|
bool aDispatchDelayed,
|
|
|
|
bool aIsVersionChangeTransactionChild);
|
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
IDBTransaction();
|
|
|
|
~IDBTransaction();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsresult CommitOrRollback();
|
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
nsRefPtr<IDBDatabase> mDatabase;
|
2011-12-17 04:40:47 +04:00
|
|
|
nsRefPtr<DatabaseInfo> mDatabaseInfo;
|
2012-06-25 23:15:17 +04:00
|
|
|
nsCOMPtr<nsIDOMDOMError> mError;
|
2010-06-23 23:46:08 +04:00
|
|
|
nsTArray<nsString> mObjectStoreNames;
|
2012-03-13 08:44:45 +04:00
|
|
|
ReadyState mReadyState;
|
|
|
|
Mode mMode;
|
2010-06-23 23:46:08 +04:00
|
|
|
PRUint32 mPendingRequests;
|
|
|
|
|
|
|
|
// Only touched on the main thread.
|
2012-01-29 00:03:35 +04:00
|
|
|
NS_DECL_EVENT_HANDLER(error)
|
|
|
|
NS_DECL_EVENT_HANDLER(complete)
|
|
|
|
NS_DECL_EVENT_HANDLER(abort)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsInterfaceHashtable<nsCStringHashKey, mozIStorageStatement>
|
|
|
|
mCachedStatements;
|
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
nsRefPtr<IDBTransactionListener> mListener;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// Only touched on the database thread.
|
|
|
|
nsCOMPtr<mozIStorageConnection> mConnection;
|
|
|
|
|
|
|
|
// Only touched on the database thread.
|
|
|
|
PRUint32 mSavepointCount;
|
|
|
|
|
2010-11-11 02:25:40 +03:00
|
|
|
nsTArray<nsRefPtr<IDBObjectStore> > mCreatedObjectStores;
|
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsRefPtr<UpdateRefcountFunction> mUpdateFileRefcountFunction;
|
2012-06-03 20:33:52 +04:00
|
|
|
nsRefPtrHashtable<nsISupportsHashKey, FileInfo> mCreatedFileInfos;
|
2012-06-01 21:21:12 +04:00
|
|
|
|
|
|
|
IndexedDBTransactionChild* mActorChild;
|
|
|
|
IndexedDBTransactionParent* mActorParent;
|
|
|
|
|
|
|
|
nsresult mAbortCode;
|
2010-11-16 00:49:49 +03:00
|
|
|
bool mCreating;
|
2010-12-16 00:21:17 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool mFiredCompleteOrAbort;
|
|
|
|
#endif
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2012-05-15 08:50:29 +04:00
|
|
|
class CommitHelper MOZ_FINAL : public nsIRunnable
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
CommitHelper(IDBTransaction* aTransaction,
|
2011-12-17 04:40:47 +04:00
|
|
|
IDBTransactionListener* aListener,
|
|
|
|
const nsTArray<nsRefPtr<IDBObjectStore> >& mUpdatedObjectStores);
|
2012-06-01 21:21:12 +04:00
|
|
|
CommitHelper(IDBTransaction* aTransaction,
|
|
|
|
nsresult aAbortCode);
|
2010-10-19 21:58:52 +04:00
|
|
|
~CommitHelper();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool AddDoomedObject(nsCOMPtr<T>& aCOMPtr)
|
|
|
|
{
|
|
|
|
if (aCOMPtr) {
|
|
|
|
if (!mDoomedObjects.AppendElement(do_QueryInterface(aCOMPtr))) {
|
|
|
|
NS_ERROR("Out of memory!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aCOMPtr = nsnull;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-12-17 04:40:47 +04:00
|
|
|
// Writes new autoincrement counts to database
|
|
|
|
nsresult WriteAutoIncrementCounts();
|
|
|
|
|
|
|
|
// Updates counts after a successful commit
|
|
|
|
void CommitAutoIncrementCounts();
|
|
|
|
|
|
|
|
// Reverts counts when a transaction is aborted
|
|
|
|
void RevertAutoIncrementCounts();
|
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
nsRefPtr<IDBTransaction> mTransaction;
|
2011-10-20 20:10:56 +04:00
|
|
|
nsRefPtr<IDBTransactionListener> mListener;
|
2010-06-23 23:46:08 +04:00
|
|
|
nsCOMPtr<mozIStorageConnection> mConnection;
|
2011-12-16 11:34:24 +04:00
|
|
|
nsRefPtr<UpdateRefcountFunction> mUpdateFileRefcountFunction;
|
2010-06-23 23:46:08 +04:00
|
|
|
nsAutoTArray<nsCOMPtr<nsISupports>, 10> mDoomedObjects;
|
2011-12-17 04:40:47 +04:00
|
|
|
nsAutoTArray<nsRefPtr<IDBObjectStore>, 10> mAutoIncrementObjectStores;
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsresult mAbortCode;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2012-05-15 08:50:29 +04:00
|
|
|
class UpdateRefcountFunction MOZ_FINAL : public mozIStorageFunction
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_MOZISTORAGEFUNCTION
|
|
|
|
|
|
|
|
UpdateRefcountFunction(FileManager* aFileManager)
|
|
|
|
: mFileManager(aFileManager)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~UpdateRefcountFunction()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
void ClearFileInfoEntries()
|
|
|
|
{
|
|
|
|
mFileInfoEntries.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UpdateDatabase(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
DatabaseUpdateFunction function(aConnection);
|
|
|
|
|
|
|
|
mFileInfoEntries.EnumerateRead(DatabaseUpdateCallback, &function);
|
|
|
|
|
|
|
|
return function.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateFileInfos()
|
|
|
|
{
|
|
|
|
mFileInfoEntries.EnumerateRead(FileInfoUpdateCallback, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class FileInfoEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileInfoEntry(FileInfo* aFileInfo)
|
|
|
|
: mFileInfo(aFileInfo), mDelta(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~FileInfoEntry()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsRefPtr<FileInfo> mFileInfo;
|
|
|
|
PRInt32 mDelta;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum UpdateType {
|
|
|
|
eIncrement,
|
|
|
|
eDecrement
|
|
|
|
};
|
|
|
|
|
|
|
|
class DatabaseUpdateFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DatabaseUpdateFunction(mozIStorageConnection* aConnection)
|
|
|
|
: mConnection(aConnection), mErrorCode(NS_OK)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool Update(PRInt64 aId, PRInt32 aDelta);
|
|
|
|
nsresult ErrorCode()
|
|
|
|
{
|
|
|
|
return mErrorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult UpdateInternal(PRInt64 aId, PRInt32 aDelta);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> mConnection;
|
|
|
|
nsCOMPtr<mozIStorageStatement> mUpdateStatement;
|
|
|
|
nsCOMPtr<mozIStorageStatement> mInsertStatement;
|
|
|
|
|
|
|
|
nsresult mErrorCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult ProcessValue(mozIStorageValueArray* aValues,
|
|
|
|
PRInt32 aIndex,
|
|
|
|
UpdateType aUpdateType);
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
DatabaseUpdateCallback(const PRUint64& aKey,
|
|
|
|
FileInfoEntry* aValue,
|
|
|
|
void* aUserArg);
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
FileInfoUpdateCallback(const PRUint64& aKey,
|
|
|
|
FileInfoEntry* aValue,
|
|
|
|
void* aUserArg);
|
|
|
|
|
|
|
|
FileManager* mFileManager;
|
|
|
|
nsClassHashtable<nsUint64HashKey, FileInfoEntry> mFileInfoEntries;
|
|
|
|
};
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
#endif // mozilla_dom_indexeddb_idbtransaction_h__
|