2010-06-23 12:46:08 -07: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 04:12:37 -07: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 12:46:08 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-04-06 13:40:10 -07:00
|
|
|
#include "nsIAppShell.h"
|
2010-08-26 13:57:25 -07:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
|
2012-06-25 12:15:17 -07:00
|
|
|
#include "DOMError.h"
|
2012-12-19 09:45:57 -08:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "mozilla/storage.h"
|
2012-01-24 02:03:37 -08:00
|
|
|
#include "nsContentUtils.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2011-12-15 23:34:24 -08:00
|
|
|
#include "nsDOMLists.h"
|
2010-11-10 15:26:03 -08:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-08-26 13:57:25 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-04-06 13:40:10 -07:00
|
|
|
#include "nsWidgetsCID.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-15 13:49:49 -08:00
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-09-09 15:15:40 -07:00
|
|
|
#include "DatabaseInfo.h"
|
2010-06-28 11:51:06 -07:00
|
|
|
#include "IDBCursor.h"
|
2010-09-09 15:15:40 -07:00
|
|
|
#include "IDBEvents.h"
|
2010-06-28 09:44:30 -07:00
|
|
|
#include "IDBFactory.h"
|
2010-09-09 15:15:40 -07:00
|
|
|
#include "IDBObjectStore.h"
|
2011-08-26 01:21:35 -07:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
#include "ipc/IndexedDBChild.h"
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
#define SAVEPOINT_NAME "savepoint"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-12-19 09:45:57 -08:00
|
|
|
using mozilla::dom::quota::QuotaManager;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-04-06 13:40:10 -07:00
|
|
|
NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
PLDHashOperator
|
|
|
|
DoomCachedStatements(const nsACString& aQuery,
|
|
|
|
nsCOMPtr<mozIStorageStatement>& aStatement,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
CommitHelper* helper = static_cast<CommitHelper*>(aUserArg);
|
|
|
|
helper->AddDoomedObject(aStatement);
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
2011-11-18 07:21:04 -08:00
|
|
|
// This runnable doesn't actually do anything beyond "prime the pump" and get
|
|
|
|
// transactions in the right order on the transaction thread pool.
|
|
|
|
class StartTransactionRunnable : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
// NOP
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Could really use those NS_REFCOUNTING_HAHA_YEAH_RIGHT macros here.
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) StartTransactionRunnable::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) StartTransactionRunnable::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(StartTransactionRunnable, nsIRunnable)
|
|
|
|
|
|
|
|
StartTransactionRunnable gStartTransactionRunnable;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
// static
|
2010-06-28 09:46:21 -07:00
|
|
|
already_AddRefed<IDBTransaction>
|
2012-06-01 10:21:12 -07:00
|
|
|
IDBTransaction::CreateInternal(IDBDatabase* aDatabase,
|
|
|
|
nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
Mode aMode,
|
|
|
|
bool aDispatchDelayed,
|
|
|
|
bool aIsVersionChangeTransactionChild)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess() || !aDispatchDelayed,
|
|
|
|
"No support for delayed-dispatch transactions in child "
|
|
|
|
"process!");
|
|
|
|
NS_ASSERTION(!aIsVersionChangeTransactionChild ||
|
|
|
|
(!IndexedDatabaseManager::IsMainProcess() &&
|
|
|
|
aMode == IDBTransaction::VERSION_CHANGE),
|
|
|
|
"Busted logic!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
nsRefPtr<IDBTransaction> transaction = new IDBTransaction();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-03-12 17:56:07 -07:00
|
|
|
transaction->BindToOwner(aDatabase);
|
2012-11-29 14:54:14 -08:00
|
|
|
transaction->SetScriptOwner(aDatabase->GetScriptOwner());
|
2010-06-23 12:46:08 -07:00
|
|
|
transaction->mDatabase = aDatabase;
|
|
|
|
transaction->mMode = aMode;
|
2011-12-16 16:40:47 -08:00
|
|
|
transaction->mDatabaseInfo = aDatabase->Info();
|
2012-06-01 10:21:12 -07:00
|
|
|
transaction->mObjectStoreNames.AppendElements(aObjectStoreNames);
|
2012-06-29 09:48:34 -07:00
|
|
|
transaction->mObjectStoreNames.Sort();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
IndexedDBTransactionChild* actor = nullptr;
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
transaction->mCreatedFileInfos.Init();
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (IndexedDatabaseManager::IsMainProcess()) {
|
|
|
|
transaction->mCachedStatements.Init();
|
|
|
|
|
|
|
|
if (aMode != IDBTransaction::VERSION_CHANGE) {
|
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(pool, nullptr);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
pool->Dispatch(transaction, &gStartTransactionRunnable, false, nullptr);
|
2012-06-01 10:21:12 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2012-06-01 10:21:12 -07:00
|
|
|
else if (!aIsVersionChangeTransactionChild) {
|
|
|
|
IndexedDBDatabaseChild* dbActor = aDatabase->GetActorChild();
|
|
|
|
NS_ASSERTION(dbActor, "Must have an actor here!");
|
|
|
|
|
|
|
|
ipc::NormalTransactionParams params;
|
|
|
|
params.names().AppendElements(aObjectStoreNames);
|
|
|
|
params.mode() = aMode;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
actor = new IndexedDBTransactionChild();
|
|
|
|
|
|
|
|
dbActor->SendPIndexedDBTransactionConstructor(actor, params);
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-15 13:49:49 -08:00
|
|
|
if (!aDispatchDelayed) {
|
2012-04-06 13:40:10 -07:00
|
|
|
nsCOMPtr<nsIAppShell> appShell = do_GetService(kAppShellCID);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(appShell, nullptr);
|
2010-11-18 14:19:19 -08:00
|
|
|
|
2012-04-06 13:40:10 -07:00
|
|
|
nsresult rv = appShell->RunBeforeNextEvent(transaction);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-11-18 14:19:19 -08:00
|
|
|
|
2010-11-15 13:49:49 -08:00
|
|
|
transaction->mCreating = true;
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (actor) {
|
|
|
|
NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
actor->SetTransaction(transaction);
|
2011-11-18 07:21:04 -08:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::IDBTransaction()
|
2012-03-12 21:44:45 -07:00
|
|
|
: mReadyState(IDBTransaction::INITIAL),
|
|
|
|
mMode(IDBTransaction::READ_ONLY),
|
2010-06-23 12:46:08 -07:00
|
|
|
mPendingRequests(0),
|
|
|
|
mSavepointCount(0),
|
2012-07-30 07:20:58 -07:00
|
|
|
mActorChild(nullptr),
|
|
|
|
mActorParent(nullptr),
|
2012-06-01 10:21:12 -07:00
|
|
|
mAbortCode(NS_OK),
|
2010-11-15 13:49:49 -08:00
|
|
|
mCreating(false)
|
2010-12-15 13:21:17 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mFiredCompleteOrAbort(false)
|
|
|
|
#endif
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::~IDBTransaction()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mPendingRequests, "Should have no pending requests here!");
|
|
|
|
NS_ASSERTION(!mSavepointCount, "Should have released them all!");
|
|
|
|
NS_ASSERTION(!mConnection, "Should have called CommitOrRollback!");
|
2010-11-15 13:49:49 -08:00
|
|
|
NS_ASSERTION(!mCreating, "Should have been cleared already!");
|
2010-12-15 13:21:17 -08:00
|
|
|
NS_ASSERTION(mFiredCompleteOrAbort, "Should have fired event!");
|
2012-01-24 02:03:37 -08:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(!mActorParent, "Actor parent owns us, how can we be dying?!");
|
|
|
|
if (mActorChild) {
|
|
|
|
NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
mActorChild->Send__delete__(mActorChild);
|
|
|
|
NS_ASSERTION(!mActorChild, "Should have cleared in Send__delete__!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::OnNewRequest()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
if (!mPendingRequests) {
|
2012-03-12 21:44:45 -07:00
|
|
|
NS_ASSERTION(mReadyState == IDBTransaction::INITIAL,
|
2010-06-23 12:46:08 -07:00
|
|
|
"Reusing a transaction!");
|
2012-03-12 21:44:45 -07:00
|
|
|
mReadyState = IDBTransaction::LOADING;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
++mPendingRequests;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::OnRequestFinished()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(mPendingRequests, "Mismatched calls!");
|
|
|
|
--mPendingRequests;
|
|
|
|
if (!mPendingRequests) {
|
2012-08-14 02:10:41 -07:00
|
|
|
NS_ASSERTION(NS_FAILED(mAbortCode) || mReadyState == IDBTransaction::LOADING,
|
2011-01-27 13:47:35 -08:00
|
|
|
"Bad state!");
|
|
|
|
mReadyState = IDBTransaction::COMMITTING;
|
2010-06-23 12:46:08 -07:00
|
|
|
CommitOrRollback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 19:29:07 -08:00
|
|
|
void
|
|
|
|
IDBTransaction::OnRequestDisconnected()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(mPendingRequests, "Mismatched calls!");
|
|
|
|
--mPendingRequests;
|
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
void
|
2011-12-16 16:40:47 -08:00
|
|
|
IDBTransaction::RemoveObjectStore(const nsAString& aName)
|
2011-12-04 09:39:01 -08:00
|
|
|
{
|
2012-03-12 21:44:45 -07:00
|
|
|
NS_ASSERTION(mMode == IDBTransaction::VERSION_CHANGE,
|
2011-12-16 16:40:47 -08:00
|
|
|
"Only remove object stores on VERSION_CHANGE transactions");
|
|
|
|
|
|
|
|
mDatabaseInfo->RemoveObjectStore(aName);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mCreatedObjectStores.Length(); i++) {
|
2011-12-04 09:39:01 -08:00
|
|
|
if (mCreatedObjectStores[i]->Name() == aName) {
|
2012-06-29 09:48:34 -07:00
|
|
|
nsRefPtr<IDBObjectStore> objectStore = mCreatedObjectStores[i];
|
2011-12-04 09:39:01 -08:00
|
|
|
mCreatedObjectStores.RemoveElementAt(i);
|
2012-06-29 09:48:34 -07:00
|
|
|
mDeletedObjectStores.AppendElement(objectStore);
|
2011-12-04 09:39:01 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
void
|
|
|
|
IDBTransaction::SetTransactionListener(IDBTransactionListener* aListener)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mListener, "Shouldn't already have a listener!");
|
|
|
|
mListener = aListener;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::CommitOrRollback()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 17:46:37 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (!IndexedDatabaseManager::IsMainProcess()) {
|
|
|
|
NS_ASSERTION(mActorChild, "Must have an actor!");
|
|
|
|
|
|
|
|
mActorChild->SendAllRequestsFinished();
|
2012-06-19 18:50:39 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CommitHelper> helper =
|
|
|
|
new CommitHelper(this, mListener, mCreatedObjectStores);
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
|
|
|
|
NS_ENSURE_STATE(pool);
|
|
|
|
|
|
|
|
mCachedStatements.Enumerate(DoomCachedStatements, helper);
|
|
|
|
NS_ASSERTION(!mCachedStatements.Count(), "Statements left!");
|
|
|
|
|
|
|
|
nsresult rv = pool->Dispatch(this, helper, true, helper);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::StartSavepoint()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(mConnection, "No connection!");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SAVEPOINT " SAVEPOINT_NAME
|
|
|
|
));
|
|
|
|
NS_ENSURE_TRUE(stmt, false);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsresult rv = stmt->Execute();
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
++mSavepointCount;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::ReleaseSavepoint()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(mConnection, "No connection!");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_ASSERTION(mSavepointCount, "Mismatch!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"RELEASE SAVEPOINT " SAVEPOINT_NAME
|
|
|
|
));
|
2012-07-27 06:59:29 -07:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_OK);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->Execute();
|
2012-07-27 06:59:29 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
--mSavepointCount;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::RollbackSavepoint()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(mConnection, "No connection!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mSavepointCount == 1, "Mismatch!");
|
|
|
|
mSavepointCount = 0;
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"ROLLBACK TO SAVEPOINT " SAVEPOINT_NAME
|
|
|
|
));
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(stmt);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->Execute();
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::GetOrCreateConnection(mozIStorageConnection** aResult)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
if (mDatabase->IsInvalidated()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
if (!mConnection) {
|
|
|
|
nsCOMPtr<mozIStorageConnection> connection =
|
2012-12-17 11:25:10 -08:00
|
|
|
IDBFactory::GetConnection(mDatabase->FilePath(),
|
|
|
|
mDatabase->Origin());
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_TRUE(connection, NS_ERROR_FAILURE);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsRefPtr<UpdateRefcountFunction> function;
|
2011-01-13 12:11:26 -08:00
|
|
|
nsCString beginTransaction;
|
2012-03-12 21:44:45 -07:00
|
|
|
if (mMode != IDBTransaction::READ_ONLY) {
|
2011-12-15 23:34:24 -08:00
|
|
|
function = new UpdateRefcountFunction(Database()->Manager());
|
|
|
|
NS_ENSURE_TRUE(function, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
rv = function->Init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = connection->CreateFunction(
|
|
|
|
NS_LITERAL_CSTRING("update_refcount"), 2, function);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-01-13 12:11:26 -08:00
|
|
|
beginTransaction.AssignLiteral("BEGIN IMMEDIATE TRANSACTION;");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
beginTransaction.AssignLiteral("BEGIN TRANSACTION;");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = connection->CreateStatement(beginTransaction, getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-01-13 12:11:26 -08:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
function.swap(mUpdateFileRefcountFunction);
|
2010-06-23 12:46:08 -07:00
|
|
|
connection.swap(mConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> result(mConnection);
|
|
|
|
result.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::GetCachedStatement(const nsACString& aQuery)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aQuery.IsEmpty(), "Empty sql statement!");
|
|
|
|
NS_ASSERTION(mConnection, "No connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
|
|
|
|
if (!mCachedStatements.Get(aQuery, getter_AddRefs(stmt))) {
|
|
|
|
nsresult rv = mConnection->CreateStatement(aQuery, getter_AddRefs(stmt));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCString error;
|
|
|
|
error.AppendLiteral("The statement `");
|
|
|
|
error.Append(aQuery);
|
|
|
|
error.AppendLiteral("` failed to compile with the error message `");
|
|
|
|
nsCString msg;
|
|
|
|
(void)mConnection->GetLastErrorString(msg);
|
|
|
|
error.Append(msg);
|
|
|
|
error.AppendLiteral("`.");
|
|
|
|
NS_ERROR(error.get());
|
|
|
|
}
|
|
|
|
#endif
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
mCachedStatements.Put(aQuery, stmt);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return stmt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-12-15 13:21:17 -08:00
|
|
|
IDBTransaction::IsOpen() const
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-11-15 13:49:49 -08:00
|
|
|
|
|
|
|
// If we haven't started anything then we're open.
|
2012-03-12 21:44:45 -07:00
|
|
|
if (mReadyState == IDBTransaction::INITIAL) {
|
2010-11-15 13:49:49 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already started then we need to check to see if we still have the
|
|
|
|
// mCreating flag set. If we do (i.e. we haven't returned to the event loop
|
|
|
|
// from the time we were created) then we are open. Otherwise check the
|
|
|
|
// currently running transaction to see if it's the same. We only allow other
|
|
|
|
// requests to be made if this transaction is currently running.
|
2012-03-12 21:44:45 -07:00
|
|
|
if (mReadyState == IDBTransaction::LOADING) {
|
2010-11-15 13:49:49 -08:00
|
|
|
if (mCreating) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AsyncConnectionHelper::GetCurrentTransaction() == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:40 -08:00
|
|
|
already_AddRefed<IDBObjectStore>
|
|
|
|
IDBTransaction::GetOrCreateObjectStore(const nsAString& aName,
|
2012-06-01 10:21:12 -07:00
|
|
|
ObjectStoreInfo* aObjectStoreInfo,
|
|
|
|
bool aCreating)
|
2010-11-10 15:25:40 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aObjectStoreInfo, "Null pointer!");
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(!aCreating || GetMode() == IDBTransaction::VERSION_CHANGE,
|
|
|
|
"How else can we create here?!");
|
2010-11-10 15:25:40 -08:00
|
|
|
|
|
|
|
nsRefPtr<IDBObjectStore> retval;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < mCreatedObjectStores.Length(); index++) {
|
2010-11-10 15:25:40 -08:00
|
|
|
nsRefPtr<IDBObjectStore>& objectStore = mCreatedObjectStores[index];
|
|
|
|
if (objectStore->Name() == aName) {
|
|
|
|
retval = objectStore;
|
|
|
|
return retval.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
retval = IDBObjectStore::Create(this, aObjectStoreInfo, mDatabaseInfo->id,
|
|
|
|
aCreating);
|
2010-11-10 15:25:40 -08:00
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
mCreatedObjectStores.AppendElement(retval);
|
2010-11-10 15:25:40 -08:00
|
|
|
|
|
|
|
return retval.forget();
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
already_AddRefed<FileInfo>
|
|
|
|
IDBTransaction::GetFileInfo(nsIDOMBlob* aBlob)
|
|
|
|
{
|
|
|
|
nsRefPtr<FileInfo> fileInfo;
|
|
|
|
mCreatedFileInfos.Get(aBlob, getter_AddRefs(fileInfo));
|
|
|
|
return fileInfo.forget();
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
void
|
2012-06-03 09:33:52 -07:00
|
|
|
IDBTransaction::AddFileInfo(nsIDOMBlob* aBlob, FileInfo* aFileInfo)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
2012-06-03 09:33:52 -07:00
|
|
|
mCreatedFileInfos.Put(aBlob, aFileInfo);
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBTransaction::ClearCreatedFileInfos()
|
|
|
|
{
|
|
|
|
mCreatedFileInfos.Clear();
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
nsresult
|
2012-06-29 09:48:34 -07:00
|
|
|
IDBTransaction::AbortInternal(nsresult aAbortCode,
|
|
|
|
already_AddRefed<nsIDOMDOMError> aError)
|
2012-06-01 10:21:12 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-06-29 09:48:34 -07:00
|
|
|
nsCOMPtr<nsIDOMDOMError> error = aError;
|
|
|
|
|
2012-06-26 08:09:25 -07:00
|
|
|
if (IsFinished()) {
|
2012-06-01 10:21:12 -07:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mActorChild) {
|
|
|
|
NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
mActorChild->SendAbort(aAbortCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool needToCommitOrRollback = mReadyState == IDBTransaction::INITIAL;
|
|
|
|
|
|
|
|
mAbortCode = aAbortCode;
|
|
|
|
mReadyState = IDBTransaction::DONE;
|
2012-06-29 09:48:34 -07:00
|
|
|
mError = error.forget();
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2012-06-29 09:48:34 -07:00
|
|
|
if (GetMode() == IDBTransaction::VERSION_CHANGE) {
|
|
|
|
// If a version change transaction is aborted, we must revert the world
|
|
|
|
// back to its previous state.
|
|
|
|
mDatabase->RevertToPreviousState();
|
|
|
|
|
|
|
|
DatabaseInfo* dbInfo = mDatabase->Info();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mCreatedObjectStores.Length(); i++) {
|
2012-06-29 09:48:34 -07:00
|
|
|
nsRefPtr<IDBObjectStore>& objectStore = mCreatedObjectStores[i];
|
|
|
|
ObjectStoreInfo* info = dbInfo->GetObjectStore(objectStore->Name());
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
info = new ObjectStoreInfo(*objectStore->Info());
|
|
|
|
info->indexes.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
objectStore->SetInfo(info);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mDeletedObjectStores.Length(); i++) {
|
2012-06-29 09:48:34 -07:00
|
|
|
nsRefPtr<IDBObjectStore>& objectStore = mDeletedObjectStores[i];
|
|
|
|
ObjectStoreInfo* info = dbInfo->GetObjectStore(objectStore->Name());
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
info = new ObjectStoreInfo(*objectStore->Info());
|
|
|
|
info->indexes.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
objectStore->SetInfo(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// and then the db must be closed
|
2012-06-01 10:21:12 -07:00
|
|
|
mDatabase->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire the abort event if there are no outstanding requests. Otherwise the
|
|
|
|
// abort event will be fired when all outstanding requests finish.
|
|
|
|
if (needToCommitOrRollback) {
|
|
|
|
return CommitOrRollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-25 12:15:17 -07:00
|
|
|
nsresult
|
|
|
|
IDBTransaction::Abort(IDBRequest* aRequest)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aRequest, "This is undesirable.");
|
|
|
|
|
2012-06-29 09:48:34 -07:00
|
|
|
nsCOMPtr<nsIDOMDOMError> error;
|
|
|
|
aRequest->GetError(getter_AddRefs(error));
|
|
|
|
|
|
|
|
return AbortInternal(aRequest->GetErrorCode(), error.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
IDBTransaction::Abort(nsresult aErrorCode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-25 12:15:17 -07:00
|
|
|
|
2012-06-29 09:48:34 -07:00
|
|
|
return AbortInternal(aErrorCode, DOMError::CreateForNSResult(aErrorCode));
|
2012-06-25 12:15:17 -07:00
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBTransaction)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBTransaction,
|
2012-01-24 02:03:37 -08:00
|
|
|
IDBWrapperCache)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDatabase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mError);
|
2010-11-10 15:25:40 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < tmp->mCreatedObjectStores.Length(); i++) {
|
2010-11-10 15:25:40 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCreatedObjectStores[i]");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIIDBObjectStore*>(
|
|
|
|
tmp->mCreatedObjectStores[i].get()));
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < tmp->mDeletedObjectStores.Length(); i++) {
|
2012-06-29 09:48:34 -07:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mDeletedObjectStores[i]");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIIDBObjectStore*>(
|
|
|
|
tmp->mDeletedObjectStores[i].get()));
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-01-24 02:03:37 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBTransaction, IDBWrapperCache)
|
2010-09-09 15:15:38 -07:00
|
|
|
// Don't unlink mDatabase!
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mError);
|
2010-11-10 15:25:40 -08:00
|
|
|
|
|
|
|
tmp->mCreatedObjectStores.Clear();
|
2012-06-29 09:48:34 -07:00
|
|
|
tmp->mDeletedObjectStores.Clear();
|
2010-11-10 15:25:40 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBTransaction)
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBTransaction)
|
2012-04-06 13:40:10 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
|
2010-06-28 09:46:21 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBTransaction)
|
2012-01-24 02:03:37 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(IDBWrapperCache)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-01-24 02:03:37 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBTransaction, IDBWrapperCache)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBTransaction, IDBWrapperCache)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
DOMCI_DATA(IDBTransaction, IDBTransaction)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-08-30 20:45:16 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(IDBTransaction, error)
|
|
|
|
NS_IMPL_EVENT_HANDLER(IDBTransaction, complete)
|
|
|
|
NS_IMPL_EVENT_HANDLER(IDBTransaction, abort)
|
2012-01-24 02:03:37 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::GetDb(nsIIDBDatabase** aDB)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ADDREF(*aDB = mDatabase);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-03-12 21:44:45 -07:00
|
|
|
IDBTransaction::GetMode(nsAString& aMode)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-03-13 13:25:05 -07:00
|
|
|
switch (mMode) {
|
2012-03-12 21:44:45 -07:00
|
|
|
case READ_ONLY:
|
|
|
|
aMode.AssignLiteral("readonly");
|
|
|
|
break;
|
|
|
|
case READ_WRITE:
|
|
|
|
aMode.AssignLiteral("readwrite");
|
|
|
|
break;
|
|
|
|
case VERSION_CHANGE:
|
|
|
|
aMode.AssignLiteral("versionchange");
|
2012-06-01 10:21:12 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_INVALID:
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Bad mode value!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2012-03-12 21:44:45 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-25 12:15:17 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBTransaction::GetError(nsIDOMDOMError** aError)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (IsOpen()) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aError = mError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::GetObjectStoreNames(nsIDOMDOMStringList** aObjectStores)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsAutoTArray<nsString, 10> stackArray;
|
2010-06-23 12:46:08 -07:00
|
|
|
nsTArray<nsString>* arrayOfNames;
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (mMode == IDBTransaction::VERSION_CHANGE) {
|
2011-12-16 16:40:47 -08:00
|
|
|
mDatabaseInfo->GetObjectStoreNames(stackArray);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
arrayOfNames = &stackArray;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arrayOfNames = &mObjectStoreNames;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = arrayOfNames->Length();
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_TRUE(list->Add(arrayOfNames->ElementAt(index)),
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2010-11-10 15:25:44 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
list.forget(aObjectStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::ObjectStore(const nsAString& aName,
|
2010-06-28 11:51:06 -07:00
|
|
|
nsIIDBObjectStore** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
nsRefPtr<IDBObjectStore> objectStore;
|
|
|
|
nsresult rv = ObjectStoreInternal(aName, getter_AddRefs(objectStore));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
objectStore.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
IDBTransaction::ObjectStoreInternal(const nsAString& aName,
|
|
|
|
IDBObjectStore** _retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-06-26 08:09:25 -07:00
|
|
|
if (IsFinished()) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
ObjectStoreInfo* info = nullptr;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-03-12 21:44:45 -07:00
|
|
|
if (mMode == IDBTransaction::VERSION_CHANGE ||
|
2010-10-19 10:58:52 -07:00
|
|
|
mObjectStoreNames.Contains(aName)) {
|
2011-12-16 16:40:47 -08:00
|
|
|
info = mDatabaseInfo->GetObjectStore(aName);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!info) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
nsRefPtr<IDBObjectStore> objectStore =
|
|
|
|
GetOrCreateObjectStore(aName, info, false);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(objectStore, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
objectStore.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction::Abort()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return AbortInternal(NS_ERROR_DOM_INDEXEDDB_ABORT_ERR, nullptr);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:26:03 -08:00
|
|
|
nsresult
|
|
|
|
IDBTransaction::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
aVisitor.mCanHandle = true;
|
2010-11-10 15:26:03 -08:00
|
|
|
aVisitor.mParentTarget = mDatabase;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-15 13:49:49 -08:00
|
|
|
NS_IMETHODIMP
|
2012-04-06 13:40:10 -07:00
|
|
|
IDBTransaction::Run()
|
2010-11-15 13:49:49 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-04-06 13:40:10 -07:00
|
|
|
// We're back at the event loop, no longer newborn.
|
|
|
|
mCreating = false;
|
|
|
|
|
|
|
|
// Maybe set the readyState to DONE if there were no requests generated.
|
|
|
|
if (mReadyState == IDBTransaction::INITIAL) {
|
|
|
|
mReadyState = IDBTransaction::DONE;
|
2010-11-15 13:49:49 -08:00
|
|
|
|
2012-04-06 13:40:10 -07:00
|
|
|
if (NS_FAILED(CommitOrRollback())) {
|
|
|
|
NS_WARNING("Failed to commit!");
|
2010-11-18 14:19:19 -08:00
|
|
|
}
|
2010-11-15 13:49:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
CommitHelper::CommitHelper(
|
|
|
|
IDBTransaction* aTransaction,
|
|
|
|
IDBTransactionListener* aListener,
|
|
|
|
const nsTArray<nsRefPtr<IDBObjectStore> >& aUpdatedObjectStores)
|
2010-09-09 15:15:38 -07:00
|
|
|
: mTransaction(aTransaction),
|
2011-10-20 09:10:56 -07:00
|
|
|
mListener(aListener),
|
2012-06-01 10:21:12 -07:00
|
|
|
mAbortCode(aTransaction->mAbortCode)
|
2010-09-09 15:15:38 -07:00
|
|
|
{
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
mConnection.swap(aTransaction->mConnection);
|
2011-12-15 23:34:24 -08:00
|
|
|
mUpdateFileRefcountFunction.swap(aTransaction->mUpdateFileRefcountFunction);
|
2011-12-16 16:40:47 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aUpdatedObjectStores.Length(); i++) {
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo* info = aUpdatedObjectStores[i]->Info();
|
|
|
|
if (info->comittedAutoIncrementId != info->nextAutoIncrementId) {
|
|
|
|
mAutoIncrementObjectStores.AppendElement(aUpdatedObjectStores[i]);
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 15:15:38 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
CommitHelper::CommitHelper(IDBTransaction* aTransaction,
|
|
|
|
nsresult aAbortCode)
|
|
|
|
: mTransaction(aTransaction),
|
|
|
|
mAbortCode(aAbortCode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
CommitHelper::~CommitHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(CommitHelper, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CommitHelper::Run()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
NS_ASSERTION(mDoomedObjects.IsEmpty(), "Didn't release doomed objects!");
|
|
|
|
|
2012-03-12 21:44:45 -07:00
|
|
|
mTransaction->mReadyState = IDBTransaction::DONE;
|
2010-10-19 17:46:37 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
// Release file infos on the main thread, so they will eventually get
|
|
|
|
// destroyed on correct thread.
|
|
|
|
mTransaction->ClearCreatedFileInfos();
|
|
|
|
if (mUpdateFileRefcountFunction) {
|
|
|
|
mUpdateFileRefcountFunction->ClearFileInfoEntries();
|
2012-07-30 07:20:58 -07:00
|
|
|
mUpdateFileRefcountFunction = nullptr;
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2012-08-14 02:10:41 -07:00
|
|
|
if (NS_FAILED(mAbortCode)) {
|
2012-03-12 21:44:45 -07:00
|
|
|
if (mTransaction->GetMode() == IDBTransaction::VERSION_CHANGE) {
|
2011-12-16 16:40:47 -08:00
|
|
|
// This will make the database take a snapshot of it's DatabaseInfo
|
|
|
|
mTransaction->Database()->Close();
|
|
|
|
// Then remove the info from the hash as it contains invalid data.
|
|
|
|
DatabaseInfo::Remove(mTransaction->Database()->Id());
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2011-11-23 06:15:15 -08:00
|
|
|
event = CreateGenericEvent(NS_LITERAL_STRING(ABORT_EVT_STR),
|
|
|
|
eDoesBubble, eNotCancelable);
|
2012-06-25 12:15:17 -07:00
|
|
|
|
|
|
|
// The transaction may already have an error object (e.g. if one of the
|
|
|
|
// requests failed). If it doesn't, and it wasn't aborted
|
|
|
|
// programmatically, create one now.
|
|
|
|
if (!mTransaction->mError &&
|
|
|
|
mAbortCode != NS_ERROR_DOM_INDEXEDDB_ABORT_ERR) {
|
|
|
|
mTransaction->mError = DOMError::CreateForNSResult(mAbortCode);
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-11-23 06:15:15 -08:00
|
|
|
event = CreateGenericEvent(NS_LITERAL_STRING(COMPLETE_EVT_STR),
|
|
|
|
eDoesNotBubble, eNotCancelable);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-06-26 08:09:25 -07:00
|
|
|
if (mListener) {
|
|
|
|
mListener->NotifyTransactionPreComplete(mTransaction);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool dummy;
|
2010-06-23 12:46:08 -07:00
|
|
|
if (NS_FAILED(mTransaction->DispatchEvent(event, &dummy))) {
|
|
|
|
NS_WARNING("Dispatch failed!");
|
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
mTransaction->mFiredCompleteOrAbort = true;
|
|
|
|
#endif
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
if (mListener) {
|
2012-06-26 08:09:25 -07:00
|
|
|
mListener->NotifyTransactionPostComplete(mTransaction);
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mTransaction = nullptr;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
IDBDatabase* database = mTransaction->Database();
|
|
|
|
if (database->IsInvalidated()) {
|
2012-06-01 10:21:12 -07:00
|
|
|
mAbortCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (mConnection) {
|
2012-12-19 09:45:57 -08:00
|
|
|
QuotaManager::SetCurrentWindow(database->GetOwner());
|
2010-09-09 15:15:40 -07:00
|
|
|
|
2012-08-14 02:10:41 -07:00
|
|
|
if (NS_SUCCEEDED(mAbortCode) && mUpdateFileRefcountFunction &&
|
2012-08-24 11:51:33 -07:00
|
|
|
NS_FAILED(mUpdateFileRefcountFunction->WillCommit(mConnection))) {
|
2012-06-01 10:21:12 -07:00
|
|
|
mAbortCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:10:41 -07:00
|
|
|
if (NS_SUCCEEDED(mAbortCode) && NS_FAILED(WriteAutoIncrementCounts())) {
|
2012-06-01 10:21:12 -07:00
|
|
|
mAbortCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2011-12-16 16:40:47 -08:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:10:41 -07:00
|
|
|
if (NS_SUCCEEDED(mAbortCode)) {
|
2011-09-28 03:04:21 -07:00
|
|
|
NS_NAMED_LITERAL_CSTRING(release, "COMMIT TRANSACTION");
|
2012-06-25 12:15:18 -07:00
|
|
|
nsresult rv = mConnection->ExecuteSimpleSQL(release);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-12-15 23:34:24 -08:00
|
|
|
if (mUpdateFileRefcountFunction) {
|
2012-08-24 11:51:33 -07:00
|
|
|
mUpdateFileRefcountFunction->DidCommit();
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
2011-12-16 16:40:47 -08:00
|
|
|
CommitAutoIncrementCounts();
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
2012-06-25 12:15:18 -07:00
|
|
|
else if (rv == NS_ERROR_FILE_NO_DEVICE_SPACE) {
|
|
|
|
// mozstorage translates SQLITE_FULL to NS_ERROR_FILE_NO_DEVICE_SPACE,
|
|
|
|
// which we know better as NS_ERROR_DOM_INDEXEDDB_QUOTA_ERR.
|
|
|
|
mAbortCode = NS_ERROR_DOM_INDEXEDDB_QUOTA_ERR;
|
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
else {
|
2012-06-01 10:21:12 -07:00
|
|
|
mAbortCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-12-15 13:21:17 -08:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:10:41 -07:00
|
|
|
if (NS_FAILED(mAbortCode)) {
|
2012-08-24 11:51:33 -07:00
|
|
|
if (mUpdateFileRefcountFunction) {
|
|
|
|
mUpdateFileRefcountFunction->DidAbort();
|
|
|
|
}
|
2011-12-16 16:40:47 -08:00
|
|
|
RevertAutoIncrementCounts();
|
2010-12-15 13:21:17 -08:00
|
|
|
NS_NAMED_LITERAL_CSTRING(rollback, "ROLLBACK TRANSACTION");
|
|
|
|
if (NS_FAILED(mConnection->ExecuteSimpleSQL(rollback))) {
|
|
|
|
NS_WARNING("Failed to rollback transaction!");
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDoomedObjects.Clear();
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (mConnection) {
|
2011-12-15 23:34:24 -08:00
|
|
|
if (mUpdateFileRefcountFunction) {
|
|
|
|
nsresult rv = mConnection->RemoveFunction(
|
|
|
|
NS_LITERAL_CSTRING("update_refcount"));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to remove function!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
mConnection->Close();
|
2012-07-30 07:20:58 -07:00
|
|
|
mConnection = nullptr;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-12-19 09:45:57 -08:00
|
|
|
QuotaManager::SetCurrentWindow(nullptr);
|
2010-12-15 13:21:17 -08:00
|
|
|
}
|
2010-09-09 15:15:40 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
nsresult
|
|
|
|
CommitHelper::WriteAutoIncrementCounts()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAutoIncrementObjectStores.Length(); i++) {
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo* info = mAutoIncrementObjectStores[i]->Info();
|
|
|
|
if (!stmt) {
|
|
|
|
rv = mConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE object_store SET auto_increment = :ai "
|
|
|
|
"WHERE id = :osid;"), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stmt->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), info->id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("ai"),
|
|
|
|
info->nextAutoIncrementId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommitHelper::CommitAutoIncrementCounts()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAutoIncrementObjectStores.Length(); i++) {
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo* info = mAutoIncrementObjectStores[i]->Info();
|
|
|
|
info->comittedAutoIncrementId = info->nextAutoIncrementId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommitHelper::RevertAutoIncrementCounts()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAutoIncrementObjectStores.Length(); i++) {
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo* info = mAutoIncrementObjectStores[i]->Info();
|
|
|
|
info->nextAutoIncrementId = info->comittedAutoIncrementId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult
|
|
|
|
UpdateRefcountFunction::Init()
|
|
|
|
{
|
2012-05-18 10:30:49 -07:00
|
|
|
mFileInfoEntries.Init();
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(UpdateRefcountFunction, mozIStorageFunction)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UpdateRefcountFunction::OnFunctionCall(mozIStorageValueArray* aValues,
|
|
|
|
nsIVariant** _retval)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*_retval = nullptr;
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numEntries;
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult rv = aValues->GetNumEntries(&numEntries);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(numEntries == 2, "unexpected number of arguments");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type1 = mozIStorageValueArray::VALUE_TYPE_NULL;
|
2011-12-15 23:34:24 -08:00
|
|
|
aValues->GetTypeOfIndex(0, &type1);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type2 = mozIStorageValueArray::VALUE_TYPE_NULL;
|
2011-12-15 23:34:24 -08:00
|
|
|
aValues->GetTypeOfIndex(1, &type2);
|
|
|
|
|
|
|
|
NS_ASSERTION(!(type1 == mozIStorageValueArray::VALUE_TYPE_NULL &&
|
|
|
|
type2 == mozIStorageValueArray::VALUE_TYPE_NULL),
|
|
|
|
"Shouldn't be called!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = ProcessValue(aValues, 0, eDecrement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ProcessValue(aValues, 1, eIncrement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
nsresult
|
|
|
|
UpdateRefcountFunction::WillCommit(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
DatabaseUpdateFunction function(aConnection, this);
|
|
|
|
|
|
|
|
mFileInfoEntries.EnumerateRead(DatabaseUpdateCallback, &function);
|
|
|
|
|
|
|
|
nsresult rv = function.ErrorCode();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CreateJournals();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateRefcountFunction::DidCommit()
|
|
|
|
{
|
|
|
|
mFileInfoEntries.EnumerateRead(FileInfoUpdateCallback, nullptr);
|
|
|
|
|
|
|
|
nsresult rv = RemoveJournals(mJournalsToRemoveAfterCommit);
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2012-08-24 11:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateRefcountFunction::DidAbort()
|
|
|
|
{
|
|
|
|
nsresult rv = RemoveJournals(mJournalsToRemoveAfterAbort);
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2012-08-24 11:51:33 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult
|
|
|
|
UpdateRefcountFunction::ProcessValue(mozIStorageValueArray* aValues,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndex,
|
2011-12-15 23:34:24 -08:00
|
|
|
UpdateType aUpdateType)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type;
|
2011-12-15 23:34:24 -08:00
|
|
|
aValues->GetTypeOfIndex(aIndex, &type);
|
|
|
|
if (type == mozIStorageValueArray::VALUE_TYPE_NULL) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString ids;
|
|
|
|
aValues->GetString(aIndex, ids);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<int64_t> fileIds;
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult rv = IDBObjectStore::ConvertFileIdsToArray(ids, fileIds);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < fileIds.Length(); i++) {
|
|
|
|
int64_t id = fileIds.ElementAt(i);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
FileInfoEntry* entry;
|
|
|
|
if (!mFileInfoEntries.Get(id, &entry)) {
|
|
|
|
nsRefPtr<FileInfo> fileInfo = mFileManager->GetFileInfo(id);
|
|
|
|
NS_ASSERTION(fileInfo, "Shouldn't be null!");
|
|
|
|
|
|
|
|
nsAutoPtr<FileInfoEntry> newEntry(new FileInfoEntry(fileInfo));
|
2012-05-18 10:30:49 -07:00
|
|
|
mFileInfoEntries.Put(id, newEntry);
|
2011-12-15 23:34:24 -08:00
|
|
|
entry = newEntry.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aUpdateType) {
|
|
|
|
case eIncrement:
|
|
|
|
entry->mDelta++;
|
|
|
|
break;
|
|
|
|
case eDecrement:
|
|
|
|
entry->mDelta--;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown update type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
nsresult
|
|
|
|
UpdateRefcountFunction::CreateJournals()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> journalDirectory = mFileManager->GetJournalDirectory();
|
|
|
|
NS_ENSURE_TRUE(journalDirectory, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mJournalsToCreateBeforeCommit.Length(); i++) {
|
|
|
|
int64_t id = mJournalsToCreateBeforeCommit[i];
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file =
|
|
|
|
mFileManager->GetFileForId(journalDirectory, id);
|
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv = file->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mJournalsToRemoveAfterAbort.AppendElement(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
UpdateRefcountFunction::RemoveJournals(const nsTArray<int64_t>& aJournals)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> journalDirectory = mFileManager->GetJournalDirectory();
|
|
|
|
NS_ENSURE_TRUE(journalDirectory, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < aJournals.Length(); index++) {
|
|
|
|
nsCOMPtr<nsIFile> file =
|
|
|
|
mFileManager->GetFileForId(journalDirectory, aJournals[index]);
|
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (NS_FAILED(file->Remove(false))) {
|
|
|
|
NS_WARNING("Failed to removed journal!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
UpdateRefcountFunction::DatabaseUpdateCallback(const uint64_t& aKey,
|
2011-12-15 23:34:24 -08:00
|
|
|
FileInfoEntry* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
if (!aValue->mDelta) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseUpdateFunction* function =
|
|
|
|
static_cast<DatabaseUpdateFunction*>(aUserArg);
|
|
|
|
|
|
|
|
if (!function->Update(aKey, aValue->mDelta)) {
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
UpdateRefcountFunction::FileInfoUpdateCallback(const uint64_t& aKey,
|
2011-12-15 23:34:24 -08:00
|
|
|
FileInfoEntry* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
if (aValue->mDelta) {
|
|
|
|
aValue->mFileInfo->UpdateDBRefs(aValue->mDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
UpdateRefcountFunction::DatabaseUpdateFunction::Update(int64_t aId,
|
|
|
|
int32_t aDelta)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
nsresult rv = UpdateInternal(aId, aDelta);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mErrorCode = rv;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
UpdateRefcountFunction::DatabaseUpdateFunction::UpdateInternal(int64_t aId,
|
|
|
|
int32_t aDelta)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mUpdateStatement) {
|
|
|
|
rv = mConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE file SET refcount = refcount + :delta WHERE id = :id"
|
|
|
|
), getter_AddRefs(mUpdateStatement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozStorageStatementScoper updateScoper(mUpdateStatement);
|
|
|
|
|
|
|
|
rv = mUpdateStatement->BindInt32ByName(NS_LITERAL_CSTRING("delta"), aDelta);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mUpdateStatement->BindInt64ByName(NS_LITERAL_CSTRING("id"), aId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mUpdateStatement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t rows;
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = mConnection->GetAffectedRows(&rows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (rows > 0) {
|
2012-08-24 11:51:33 -07:00
|
|
|
if (!mSelectStatement) {
|
|
|
|
rv = mConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT id FROM file where id = :id"
|
|
|
|
), getter_AddRefs(mSelectStatement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozStorageStatementScoper selectScoper(mSelectStatement);
|
|
|
|
|
|
|
|
rv = mSelectStatement->BindInt64ByName(NS_LITERAL_CSTRING("id"), aId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool hasResult;
|
|
|
|
rv = mSelectStatement->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!hasResult) {
|
|
|
|
// Don't have to create the journal here, we can create all at once,
|
|
|
|
// just before commit
|
|
|
|
mFunction->mJournalsToCreateBeforeCommit.AppendElement(aId);
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mInsertStatement) {
|
|
|
|
rv = mConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO file (id, refcount) VALUES(:id, :delta)"
|
|
|
|
), getter_AddRefs(mInsertStatement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozStorageStatementScoper insertScoper(mInsertStatement);
|
|
|
|
|
|
|
|
rv = mInsertStatement->BindInt64ByName(NS_LITERAL_CSTRING("id"), aId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mInsertStatement->BindInt32ByName(NS_LITERAL_CSTRING("delta"), aDelta);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mInsertStatement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
mFunction->mJournalsToRemoveAfterCommit.AppendElement(aId);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|