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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Indexed Database.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
|
|
|
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2011-01-06 22:21:36 -08:00
|
|
|
#include "nsContentUtils.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-01-03 07:27:39 -08:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
#include "IDBEvents.h"
|
2010-06-28 09:46:21 -07:00
|
|
|
#include "IDBTransaction.h"
|
2011-08-26 01:21:35 -07:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBTransaction* gCurrentTransaction = nsnull;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
const PRUint32 kProgressHandlerGranularity = 1000;
|
|
|
|
|
|
|
|
NS_STACK_CLASS
|
|
|
|
class TransactionPoolEventTarget : public nsIEventTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
TransactionPoolEventTarget(IDBTransaction* aTransaction)
|
2010-06-23 12:46:08 -07:00
|
|
|
: mTransaction(aTransaction)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction* mTransaction;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
// This inline is just so that we always clear aBuffers appropriately even if
|
|
|
|
// something fails.
|
|
|
|
inline
|
|
|
|
nsresult
|
2011-12-15 23:34:24 -08:00
|
|
|
ConvertCloneReadInfosToArrayInternal(
|
2011-01-06 22:21:36 -08:00
|
|
|
JSContext* aCx,
|
2011-12-15 23:34:24 -08:00
|
|
|
nsTArray<StructuredCloneReadInfo>& aReadInfos,
|
2011-01-06 22:21:36 -08:00
|
|
|
jsval* aResult)
|
|
|
|
{
|
|
|
|
JSObject* array = JS_NewArrayObject(aCx, 0, nsnull);
|
|
|
|
if (!array) {
|
|
|
|
NS_WARNING("Failed to make array!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
if (!aReadInfos.IsEmpty()) {
|
2012-03-06 15:52:55 -08:00
|
|
|
if (!JS_SetArrayLength(aCx, array, uint32_t(aReadInfos.Length()))) {
|
2011-01-06 22:21:36 -08:00
|
|
|
NS_WARNING("Failed to set array length!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-16 06:42:09 -08:00
|
|
|
for (uint32_t index = 0, count = aReadInfos.Length(); index < count;
|
2011-12-15 23:34:24 -08:00
|
|
|
index++) {
|
|
|
|
StructuredCloneReadInfo& readInfo = aReadInfos[index];
|
2011-01-06 22:21:36 -08:00
|
|
|
|
|
|
|
jsval val;
|
2011-12-15 23:34:24 -08:00
|
|
|
if (!IDBObjectStore::DeserializeValue(aCx, readInfo, &val)) {
|
2011-01-06 22:21:36 -08:00
|
|
|
NS_WARNING("Failed to decode!");
|
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetElement(aCx, array, index, &val)) {
|
|
|
|
NS_WARNING("Failed to set array element!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = OBJECT_TO_JSVAL(array);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-10-22 04:56:19 -07:00
|
|
|
HelperBase::~HelperBase()
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
IDBRequest* request;
|
|
|
|
mRequest.forget(&request);
|
|
|
|
|
|
|
|
if (request) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread!");
|
|
|
|
|
|
|
|
if (mainThread) {
|
|
|
|
NS_ProxyRelease(mainThread, static_cast<nsIDOMEventTarget*>(request));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsresult
|
|
|
|
HelperBase::WrapNative(JSContext* aCx,
|
|
|
|
nsISupports* aNative,
|
|
|
|
jsval* aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aCx, "Null context!");
|
|
|
|
NS_ASSERTION(aNative, "Null pointer!");
|
|
|
|
NS_ASSERTION(aResult, "Null pointer!");
|
|
|
|
NS_ASSERTION(mRequest, "Null request!");
|
|
|
|
|
2012-01-24 02:03:37 -08:00
|
|
|
JSObject* global = mRequest->GetParentObject();
|
|
|
|
NS_ASSERTION(global, "This should never be null!");
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
nsresult rv =
|
2012-01-24 02:03:37 -08:00
|
|
|
nsContentUtils::WrapNative(aCx, global, aNative, aResult);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-22 04:56:19 -07:00
|
|
|
void
|
|
|
|
HelperBase::ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
mRequest = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:49 -07:00
|
|
|
AsyncConnectionHelper::AsyncConnectionHelper(IDBDatabase* aDatabase,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest)
|
2011-10-20 09:10:56 -07:00
|
|
|
: HelperBase(aRequest),
|
|
|
|
mDatabase(aDatabase),
|
2010-11-10 15:25:44 -08:00
|
|
|
mResultCode(NS_OK),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDispatched(false)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
AsyncConnectionHelper::AsyncConnectionHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest)
|
2011-10-20 09:10:56 -07:00
|
|
|
: HelperBase(aRequest),
|
|
|
|
mDatabase(aTransaction->mDatabase),
|
2010-06-23 12:46:08 -07:00
|
|
|
mTransaction(aTransaction),
|
2010-11-10 15:25:44 -08:00
|
|
|
mResultCode(NS_OK),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDispatched(false)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncConnectionHelper::~AsyncConnectionHelper()
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase* database;
|
2010-06-23 12:46:08 -07:00
|
|
|
mDatabase.forget(&database);
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
IDBTransaction* transaction;
|
2010-06-23 12:46:08 -07:00
|
|
|
mTransaction.forget(&transaction);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread!");
|
|
|
|
|
|
|
|
if (mainThread) {
|
|
|
|
if (database) {
|
|
|
|
NS_ProxyRelease(mainThread, static_cast<nsIIDBDatabase*>(database));
|
|
|
|
}
|
|
|
|
if (transaction) {
|
|
|
|
NS_ProxyRelease(mainThread,
|
|
|
|
static_cast<nsIIDBTransaction*>(transaction));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mOldProgressHandler, "Should not have anything here!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(AsyncConnectionHelper, nsIRunnable,
|
|
|
|
mozIStorageProgressHandler)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncConnectionHelper::Run()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
2010-12-15 13:21:17 -08:00
|
|
|
if (mTransaction &&
|
2011-11-07 22:27:03 -08:00
|
|
|
mTransaction->IsAborted()) {
|
|
|
|
// Always fire a "error" event with ABORT_ERR if the transaction was
|
|
|
|
// aborted, even if the request succeeded or failed with another error.
|
2010-12-15 13:21:17 -08:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_ABORT_ERR;
|
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:11 -08:00
|
|
|
IDBTransaction* oldTransaction = gCurrentTransaction;
|
|
|
|
gCurrentTransaction = mTransaction;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
if (mRequest) {
|
2011-10-20 09:10:56 -07:00
|
|
|
nsresult rv = mRequest->NotifyHelperCompleted(this);
|
2011-01-26 17:53:02 -08:00
|
|
|
if (NS_SUCCEEDED(mResultCode) && NS_FAILED(rv)) {
|
|
|
|
mResultCode = rv;
|
|
|
|
}
|
2011-01-06 22:21:36 -08:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
// Call OnError if the database had an error or if the OnSuccess handler
|
|
|
|
// has an error.
|
2010-11-10 15:25:44 -08:00
|
|
|
if (NS_FAILED(mResultCode) ||
|
2011-01-06 22:21:36 -08:00
|
|
|
NS_FAILED((mResultCode = OnSuccess()))) {
|
|
|
|
OnError();
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:11 -08:00
|
|
|
NS_ASSERTION(gCurrentTransaction == mTransaction, "Should be unchanged!");
|
|
|
|
gCurrentTransaction = oldTransaction;
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
if (mDispatched && mTransaction) {
|
2010-06-23 12:46:08 -07:00
|
|
|
mTransaction->OnRequestFinished();
|
|
|
|
}
|
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
ReleaseMainThreadObjects();
|
|
|
|
|
|
|
|
NS_ASSERTION(!(mDatabase || mTransaction || mRequest), "Subclass didn't "
|
|
|
|
"call AsyncConnectionHelper::ReleaseMainThreadObjects!");
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<mozIStorageConnection> connection;
|
|
|
|
|
|
|
|
if (mTransaction) {
|
|
|
|
rv = mTransaction->GetOrCreateConnection(getter_AddRefs(connection));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(connection, "This should never be null!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 09:43:00 -08:00
|
|
|
bool setProgressHandler = false;
|
2010-06-23 12:46:08 -07:00
|
|
|
if (connection) {
|
|
|
|
rv = connection->SetProgressHandler(kProgressHandlerGranularity, this,
|
|
|
|
getter_AddRefs(mOldProgressHandler));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetProgressHandler failed!");
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-11-08 09:43:00 -08:00
|
|
|
setProgressHandler = true;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-10-19 10:58:52 -07:00
|
|
|
bool hasSavepoint = false;
|
2010-09-09 15:15:40 -07:00
|
|
|
if (mDatabase) {
|
2012-01-24 02:03:37 -08:00
|
|
|
IndexedDatabaseManager::SetCurrentWindow(mDatabase->GetOwner());
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
// Make the first savepoint.
|
|
|
|
if (mTransaction) {
|
|
|
|
if (!(hasSavepoint = mTransaction->StartSavepoint())) {
|
|
|
|
NS_WARNING("Failed to make savepoint!");
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
mResultCode = DoDatabaseWork(connection);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
if (mDatabase) {
|
2011-12-03 09:10:21 -08:00
|
|
|
IndexedDatabaseManager::SetCurrentWindow(nsnull);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
// Release or roll back the savepoint depending on the error code.
|
|
|
|
if (hasSavepoint) {
|
|
|
|
NS_ASSERTION(mTransaction, "Huh?!");
|
2010-11-10 15:25:44 -08:00
|
|
|
if (NS_SUCCEEDED(mResultCode)) {
|
2010-10-19 10:58:52 -07:00
|
|
|
mTransaction->ReleaseSavepoint();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mTransaction->RollbackSavepoint();
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-09-10 12:12:11 -07:00
|
|
|
// NS_ERROR_NOT_AVAILABLE is our special code for "database is invalidated"
|
|
|
|
// and we should fail with RECOVERABLE_ERR.
|
2010-10-19 10:58:52 -07:00
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
2010-11-10 15:25:44 -08:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_RECOVERABLE_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-11-10 15:25:44 -08:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-08 09:43:00 -08:00
|
|
|
if (setProgressHandler) {
|
2010-06-23 12:46:08 -07:00
|
|
|
nsCOMPtr<mozIStorageProgressHandler> handler;
|
|
|
|
rv = connection->RemoveProgressHandler(getter_AddRefs(handler));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "RemoveProgressHandler failed!");
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-11-08 09:43:00 -08:00
|
|
|
NS_ASSERTION(SameCOMIdentity(handler, static_cast<nsIRunnable*>(this)),
|
|
|
|
"Mismatch!");
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
return NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncConnectionHelper::OnProgress(mozIStorageConnection* aConnection,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-09-10 12:12:11 -07:00
|
|
|
if (mDatabase && mDatabase->IsInvalidated()) {
|
|
|
|
// Someone is trying to delete the database file. Exit lightningfast!
|
2011-10-17 07:59:28 -07:00
|
|
|
*_retval = true;
|
2010-09-10 12:12:11 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
if (mOldProgressHandler) {
|
|
|
|
return mOldProgressHandler->OnProgress(aConnection, _retval);
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*_retval = false;
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AsyncConnectionHelper::Dispatch(nsIEventTarget* aDatabaseThread)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sameThread;
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult rv = aDatabaseThread->IsOnCurrentThread(&sameThread);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "IsOnCurrentThread failed!");
|
|
|
|
NS_ASSERTION(!sameThread, "Dispatching to main thread not supported!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv = Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aDatabaseThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mTransaction) {
|
|
|
|
mTransaction->OnNewRequest();
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mDispatched = true;
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AsyncConnectionHelper::DispatchToTransactionPool()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction, "Only ok to call this with a transaction!");
|
|
|
|
TransactionPoolEventTarget target(mTransaction);
|
|
|
|
return Dispatch(&target);
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
// static
|
|
|
|
IDBTransaction*
|
|
|
|
AsyncConnectionHelper::GetCurrentTransaction()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return gCurrentTransaction;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult
|
|
|
|
AsyncConnectionHelper::Init()
|
|
|
|
{
|
2011-01-06 22:21:36 -08:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
already_AddRefed<nsDOMEvent>
|
|
|
|
AsyncConnectionHelper::CreateSuccessEvent()
|
|
|
|
{
|
2011-11-23 06:15:15 -08:00
|
|
|
return CreateGenericEvent(NS_LITERAL_STRING(SUCCESS_EVT_STR),
|
|
|
|
eDoesNotBubble, eNotCancelable);
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
AsyncConnectionHelper::OnSuccess()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-01-26 17:53:02 -08:00
|
|
|
NS_ASSERTION(mRequest, "Null request!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsRefPtr<nsDOMEvent> event = CreateSuccessEvent();
|
2010-06-23 12:46:08 -07:00
|
|
|
if (!event) {
|
|
|
|
NS_ERROR("Failed to create event!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool dummy;
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult rv = mRequest->DispatchEvent(event, &dummy);
|
2010-12-15 13:20:57 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
nsEvent* internalEvent = event->GetInternalNSEvent();
|
2010-12-15 13:20:57 -08:00
|
|
|
NS_ASSERTION(internalEvent, "This should never be null!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
NS_ASSERTION(!mTransaction ||
|
|
|
|
mTransaction->IsOpen() ||
|
|
|
|
mTransaction->IsAborted(),
|
|
|
|
"How else can this be closed?!");
|
|
|
|
|
2010-12-15 13:20:57 -08:00
|
|
|
if ((internalEvent->flags & NS_EVENT_FLAG_EXCEPTION_THROWN) &&
|
|
|
|
mTransaction &&
|
2010-12-15 13:21:17 -08:00
|
|
|
mTransaction->IsOpen()) {
|
2010-12-15 13:20:57 -08:00
|
|
|
rv = mTransaction->Abort();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-06 22:21:36 -08:00
|
|
|
AsyncConnectionHelper::OnError()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-01-26 17:53:02 -08:00
|
|
|
NS_ASSERTION(mRequest, "Null request!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
// Make an error event and fire it at the target.
|
2011-01-06 22:21:36 -08:00
|
|
|
nsRefPtr<nsDOMEvent> event =
|
2011-11-23 06:15:15 -08:00
|
|
|
CreateGenericEvent(NS_LITERAL_STRING(ERROR_EVT_STR), eDoesBubble,
|
|
|
|
eCancelable);
|
2010-06-23 12:46:08 -07:00
|
|
|
if (!event) {
|
|
|
|
NS_ERROR("Failed to create event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doDefault;
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult rv = mRequest->DispatchEvent(event, &doDefault);
|
2010-12-15 13:20:57 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-12-15 13:21:17 -08:00
|
|
|
NS_ASSERTION(!mTransaction ||
|
|
|
|
mTransaction->IsOpen() ||
|
|
|
|
mTransaction->IsAborted(),
|
|
|
|
"How else can this be closed?!");
|
|
|
|
|
2010-12-15 13:20:57 -08:00
|
|
|
if (doDefault &&
|
|
|
|
mTransaction &&
|
2010-12-15 13:21:17 -08:00
|
|
|
mTransaction->IsOpen() &&
|
2010-12-15 13:20:57 -08:00
|
|
|
NS_FAILED(mTransaction->Abort())) {
|
|
|
|
NS_WARNING("Failed to abort transaction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("DispatchEvent failed!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
AsyncConnectionHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
*aVal = JSVAL_VOID;
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
void
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
mDatabase = nsnull;
|
|
|
|
mTransaction = nsnull;
|
2011-10-22 04:56:19 -07:00
|
|
|
|
|
|
|
HelperBase::ReleaseMainThreadObjects();
|
2010-09-09 21:54:25 -07:00
|
|
|
}
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
// static
|
|
|
|
nsresult
|
2011-12-15 23:34:24 -08:00
|
|
|
AsyncConnectionHelper::ConvertCloneReadInfosToArray(
|
2011-01-06 22:21:36 -08:00
|
|
|
JSContext* aCx,
|
2011-12-15 23:34:24 -08:00
|
|
|
nsTArray<StructuredCloneReadInfo>& aReadInfos,
|
2011-01-06 22:21:36 -08:00
|
|
|
jsval* aResult)
|
|
|
|
{
|
2011-01-26 17:53:02 -08:00
|
|
|
NS_ASSERTION(aCx, "Null context!");
|
|
|
|
NS_ASSERTION(aResult, "Null pointer!");
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult rv = ConvertCloneReadInfosToArrayInternal(aCx, aReadInfos, aResult);
|
2011-01-06 22:21:36 -08:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
for (PRUint32 index = 0; index < aReadInfos.Length(); index++) {
|
|
|
|
aReadInfos[index].mCloneBuffer.clear();
|
2011-01-06 22:21:36 -08:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
aReadInfos.Clear();
|
2011-01-06 22:21:36 -08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
TransactionPoolEventTarget::AddRef()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Don't call me!");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
TransactionPoolEventTarget::Release()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Don't call me!");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(TransactionPoolEventTarget, nsIEventTarget)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionPoolEventTarget::Dispatch(nsIRunnable* aRunnable,
|
|
|
|
PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL, "Unsupported!");
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
|
2010-06-23 12:46:08 -07:00
|
|
|
return pool->Dispatch(mTransaction, aRunnable, false, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
TransactionPoolEventTarget::IsOnCurrentThread(bool* aResult)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aResult = false;
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|