2010-06-23 19:46:08 +00: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 11:12:37 +00: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 19:46:08 +00:00
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "AsyncConnectionHelper.h"
|
|
|
|
|
2012-12-19 17:45:57 +00:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2011-01-07 06:21:36 +00:00
|
|
|
#include "nsContentUtils.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-01-03 15:27:39 +00:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
#include "IDBEvents.h"
|
2010-06-28 16:46:21 +00:00
|
|
|
#include "IDBTransaction.h"
|
2011-08-26 08:21:35 +00:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2013-03-16 06:58:50 +00:00
|
|
|
#include "ProfilerHelpers.h"
|
2014-01-28 00:37:05 +00:00
|
|
|
#include "ReportInternalError.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
#include "ipc/IndexedDBChild.h"
|
2012-11-10 03:29:07 +00:00
|
|
|
#include "ipc/IndexedDBParent.h"
|
2012-06-01 17:21:12 +00:00
|
|
|
|
2013-10-02 03:46:04 +00:00
|
|
|
using namespace mozilla;
|
2010-06-23 19:46:08 +00:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-12-19 17:45:57 +00:00
|
|
|
using mozilla::dom::quota::QuotaManager;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
IDBTransaction* gCurrentTransaction = nullptr;
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t kProgressHandlerGranularity = 1000;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2013-04-12 03:20:18 +00:00
|
|
|
class MOZ_STACK_CLASS TransactionPoolEventTarget : public StackBasedEventTarget
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
TransactionPoolEventTarget(IDBTransaction* aTransaction)
|
2010-06-23 19:46:08 +00:00
|
|
|
: mTransaction(aTransaction)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction* mTransaction;
|
2010-06-23 19:46:08 +00:00
|
|
|
};
|
|
|
|
|
2011-01-07 06:21:36 +00:00
|
|
|
// This inline is just so that we always clear aBuffers appropriately even if
|
|
|
|
// something fails.
|
|
|
|
inline
|
|
|
|
nsresult
|
2011-12-16 07:34:24 +00:00
|
|
|
ConvertCloneReadInfosToArrayInternal(
|
2011-01-07 06:21:36 +00:00
|
|
|
JSContext* aCx,
|
2011-12-16 07:34:24 +00:00
|
|
|
nsTArray<StructuredCloneReadInfo>& aReadInfos,
|
2013-06-21 13:12:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> aResult)
|
2011-01-07 06:21:36 +00:00
|
|
|
{
|
2014-02-12 10:50:46 +00:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, 0));
|
2011-01-07 06:21:36 +00:00
|
|
|
if (!array) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_WARNING("Failed to make array!");
|
2011-01-07 06:21:36 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-16 07:34:24 +00:00
|
|
|
if (!aReadInfos.IsEmpty()) {
|
2012-03-06 23:52:55 +00:00
|
|
|
if (!JS_SetArrayLength(aCx, array, uint32_t(aReadInfos.Length()))) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_WARNING("Failed to set array length!");
|
2011-01-07 06:21:36 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-16 14:42:09 +00:00
|
|
|
for (uint32_t index = 0, count = aReadInfos.Length(); index < count;
|
2011-12-16 07:34:24 +00:00
|
|
|
index++) {
|
|
|
|
StructuredCloneReadInfo& readInfo = aReadInfos[index];
|
2011-01-07 06:21:36 +00:00
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2013-06-21 13:12:46 +00:00
|
|
|
if (!IDBObjectStore::DeserializeValue(aCx, readInfo, &val)) {
|
2011-01-07 06:21:36 +00:00
|
|
|
NS_WARNING("Failed to decode!");
|
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
|
2014-01-25 09:31:17 +00:00
|
|
|
if (!JS_SetElement(aCx, array, index, val)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_WARNING("Failed to set array element!");
|
2011-01-07 06:21:36 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 13:12:46 +00:00
|
|
|
aResult.setObject(*array);
|
2011-01-07 06:21:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-10-22 11:56:19 +00: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) {
|
2013-04-19 22:18:33 +00:00
|
|
|
NS_ProxyRelease(mainThread, static_cast<EventTarget*>(request));
|
2011-10-22 11:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-20 16:10:56 +00:00
|
|
|
nsresult
|
|
|
|
HelperBase::WrapNative(JSContext* aCx,
|
|
|
|
nsISupports* aNative,
|
2013-06-21 13:12:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> aResult)
|
2011-10-20 16:10:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aCx, "Null context!");
|
|
|
|
NS_ASSERTION(aNative, "Null pointer!");
|
2013-06-21 13:12:46 +00:00
|
|
|
NS_ASSERTION(aResult.address(), "Null pointer!");
|
2011-10-20 16:10:56 +00:00
|
|
|
NS_ASSERTION(mRequest, "Null request!");
|
|
|
|
|
2014-04-10 04:58:41 +00:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, aNative, aResult);
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-10-20 16:10:56 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-22 11:56:19 +00:00
|
|
|
void
|
|
|
|
HelperBase::ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mRequest = nullptr;
|
2011-10-22 11:56:19 +00:00
|
|
|
}
|
|
|
|
|
2010-06-28 16:46:49 +00:00
|
|
|
AsyncConnectionHelper::AsyncConnectionHelper(IDBDatabase* aDatabase,
|
2010-06-23 19:46:08 +00:00
|
|
|
IDBRequest* aRequest)
|
2011-10-20 16:10:56 +00:00
|
|
|
: HelperBase(aRequest),
|
|
|
|
mDatabase(aDatabase),
|
2010-11-10 23:25:44 +00:00
|
|
|
mResultCode(NS_OK),
|
2011-10-17 14:59:28 +00:00
|
|
|
mDispatched(false)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
AsyncConnectionHelper::AsyncConnectionHelper(IDBTransaction* aTransaction,
|
2010-06-23 19:46:08 +00:00
|
|
|
IDBRequest* aRequest)
|
2011-10-20 16:10:56 +00:00
|
|
|
: HelperBase(aRequest),
|
|
|
|
mDatabase(aTransaction->mDatabase),
|
2010-06-23 19:46:08 +00:00
|
|
|
mTransaction(aTransaction),
|
2010-11-10 23:25:44 +00:00
|
|
|
mResultCode(NS_OK),
|
2011-10-17 14:59:28 +00:00
|
|
|
mDispatched(false)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncConnectionHelper::~AsyncConnectionHelper()
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
2010-06-28 16:46:49 +00:00
|
|
|
IDBDatabase* database;
|
2010-06-23 19:46:08 +00:00
|
|
|
mDatabase.forget(&database);
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction* transaction;
|
2010-06-23 19:46:08 +00: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) {
|
2013-07-31 15:48:46 +00:00
|
|
|
NS_ProxyRelease(mainThread, static_cast<IDBWrapperCache*>(database));
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
if (transaction) {
|
2013-07-29 17:12:21 +00:00
|
|
|
NS_ProxyRelease(mainThread, static_cast<IDBWrapperCache*>(transaction));
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mOldProgressHandler, "Should not have anything here!");
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(AsyncConnectionHelper, nsIRunnable,
|
|
|
|
mozIStorageProgressHandler)
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncConnectionHelper::Run()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
2014-05-23 21:12:29 +00:00
|
|
|
PROFILER_MAIN_THREAD_LABEL("AsyncConnectionHelper", "Run",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
if (mTransaction &&
|
2011-11-08 06:27:03 +00: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 21:21:17 +00:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_ABORT_ERR;
|
|
|
|
}
|
|
|
|
|
2010-12-15 21:21:11 +00:00
|
|
|
IDBTransaction* oldTransaction = gCurrentTransaction;
|
|
|
|
gCurrentTransaction = mTransaction;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
ChildProcessSendResult sendResult =
|
|
|
|
IndexedDatabaseManager::IsMainProcess() ?
|
|
|
|
MaybeSendResponseToChildProcess(mResultCode) :
|
|
|
|
Success_NotSent;
|
|
|
|
|
2012-11-10 03:29:07 +00:00
|
|
|
switch (sendResult) {
|
|
|
|
case Success_Sent: {
|
|
|
|
if (mRequest) {
|
|
|
|
mRequest->NotifyHelperSentResultsToChildProcess(NS_OK);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Success_NotSent: {
|
|
|
|
if (mRequest) {
|
|
|
|
nsresult rv = mRequest->NotifyHelperCompleted(this);
|
|
|
|
if (NS_SUCCEEDED(mResultCode) && NS_FAILED(rv)) {
|
|
|
|
mResultCode = rv;
|
|
|
|
}
|
2013-03-16 06:58:50 +00:00
|
|
|
|
|
|
|
IDB_PROFILER_MARK("IndexedDB Request %llu: Running main thread "
|
|
|
|
"response (rv = %lu)",
|
|
|
|
"IDBRequest[%llu] MT Done",
|
|
|
|
mRequest->GetSerialNumber(), mResultCode);
|
2012-11-10 03:29:07 +00:00
|
|
|
}
|
2012-06-01 17:21:12 +00:00
|
|
|
|
2012-11-10 03:29:07 +00:00
|
|
|
// Call OnError if the database had an error or if the OnSuccess
|
|
|
|
// handler has an error.
|
|
|
|
if (NS_FAILED(mResultCode) ||
|
|
|
|
NS_FAILED((mResultCode = OnSuccess()))) {
|
|
|
|
OnError();
|
|
|
|
}
|
|
|
|
break;
|
2011-01-27 01:53:02 +00:00
|
|
|
}
|
2012-11-10 03:29:07 +00:00
|
|
|
|
|
|
|
case Success_ActorDisconnected: {
|
|
|
|
// Nothing needs to be done here.
|
|
|
|
break;
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
2012-11-10 03:29:07 +00:00
|
|
|
|
|
|
|
case Error: {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_WARNING("MaybeSendResultsToChildProcess failed!");
|
2012-11-10 03:29:07 +00:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
if (mRequest) {
|
|
|
|
mRequest->NotifyHelperSentResultsToChildProcess(mResultCode);
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
2012-11-10 03:29:07 +00:00
|
|
|
break;
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
2011-01-07 06:21:36 +00:00
|
|
|
|
2012-11-10 03:29:07 +00:00
|
|
|
default:
|
2013-06-29 01:38:30 +00:00
|
|
|
MOZ_CRASH("Unknown value for ChildProcessSendResult!");
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 21:21:11 +00:00
|
|
|
NS_ASSERTION(gCurrentTransaction == mTransaction, "Should be unchanged!");
|
|
|
|
gCurrentTransaction = oldTransaction;
|
2010-10-19 17:58:52 +00:00
|
|
|
|
|
|
|
if (mDispatched && mTransaction) {
|
2010-06-23 19:46:08 +00:00
|
|
|
mTransaction->OnRequestFinished();
|
|
|
|
}
|
|
|
|
|
2010-09-10 04:54:25 +00:00
|
|
|
ReleaseMainThreadObjects();
|
|
|
|
|
|
|
|
NS_ASSERTION(!(mDatabase || mTransaction || mRequest), "Subclass didn't "
|
|
|
|
"call AsyncConnectionHelper::ReleaseMainThreadObjects!");
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
|
2014-05-23 21:12:29 +00:00
|
|
|
PROFILER_LABEL("AsyncConnectionHelper", "Run",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
|
|
|
IDB_PROFILER_MARK_IF(mRequest,
|
|
|
|
"IndexedDB Request %llu: Beginning database work",
|
|
|
|
"IDBRequest[%llu] DT Start",
|
|
|
|
mRequest->GetSerialNumber());
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
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 17:43:00 +00:00
|
|
|
bool setProgressHandler = false;
|
2010-06-23 19:46:08 +00: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 17:43:00 +00:00
|
|
|
setProgressHandler = true;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-10-19 17:58:52 +00:00
|
|
|
bool hasSavepoint = false;
|
2010-09-09 22:15:40 +00:00
|
|
|
if (mDatabase) {
|
2012-12-19 17:45:57 +00:00
|
|
|
QuotaManager::SetCurrentWindow(mDatabase->GetOwner());
|
2010-10-19 17:58:52 +00:00
|
|
|
|
|
|
|
// Make the first savepoint.
|
|
|
|
if (mTransaction) {
|
|
|
|
if (!(hasSavepoint = mTransaction->StartSavepoint())) {
|
|
|
|
NS_WARNING("Failed to make savepoint!");
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 22:15:40 +00:00
|
|
|
}
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2010-11-10 23:25:44 +00:00
|
|
|
mResultCode = DoDatabaseWork(connection);
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2010-09-09 22:15:40 +00:00
|
|
|
if (mDatabase) {
|
2010-10-19 17:58:52 +00:00
|
|
|
// Release or roll back the savepoint depending on the error code.
|
|
|
|
if (hasSavepoint) {
|
|
|
|
NS_ASSERTION(mTransaction, "Huh?!");
|
2010-11-10 23:25:44 +00:00
|
|
|
if (NS_SUCCEEDED(mResultCode)) {
|
2010-10-19 17:58:52 +00:00
|
|
|
mTransaction->ReleaseSavepoint();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mTransaction->RollbackSavepoint();
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 16:26:58 +00:00
|
|
|
|
|
|
|
// Don't unset this until we're sure that all SQLite activity has
|
|
|
|
// completed!
|
2012-12-19 17:45:57 +00:00
|
|
|
QuotaManager::SetCurrentWindow(nullptr);
|
2010-09-09 22:15:40 +00:00
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-09-10 19:12:11 +00:00
|
|
|
// NS_ERROR_NOT_AVAILABLE is our special code for "database is invalidated"
|
|
|
|
// and we should fail with RECOVERABLE_ERR.
|
2010-10-19 17:58:52 +00:00
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
2010-11-10 23:25:44 +00:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_RECOVERABLE_ERR;
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2010-11-10 23:25:44 +00:00
|
|
|
mResultCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 17:43:00 +00:00
|
|
|
if (setProgressHandler) {
|
2010-06-23 19:46:08 +00: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 17:43:00 +00:00
|
|
|
NS_ASSERTION(SameCOMIdentity(handler, static_cast<nsIRunnable*>(this)),
|
|
|
|
"Mismatch!");
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-16 06:58:50 +00:00
|
|
|
IDB_PROFILER_MARK_IF(mRequest,
|
|
|
|
"IndexedDB Request %llu: Finished database work "
|
|
|
|
"(rv = %lu)",
|
|
|
|
"IDBRequest[%llu] DT Done", mRequest->GetSerialNumber(),
|
|
|
|
mResultCode);
|
|
|
|
|
2014-05-23 19:53:17 +00:00
|
|
|
return NS_DispatchToMainThread(this);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncConnectionHelper::OnProgress(mozIStorageConnection* aConnection,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* _retval)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2010-09-10 19:12:11 +00:00
|
|
|
if (mDatabase && mDatabase->IsInvalidated()) {
|
|
|
|
// Someone is trying to delete the database file. Exit lightningfast!
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = true;
|
2010-09-10 19:12:11 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
if (mOldProgressHandler) {
|
|
|
|
return mOldProgressHandler->OnProgress(aConnection, _retval);
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = false;
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-01 17:21:12 +00:00
|
|
|
AsyncConnectionHelper::Dispatch(nsIEventTarget* aTarget)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsresult rv = Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
rv = aTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mTransaction) {
|
|
|
|
mTransaction->OnNewRequest();
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mDispatched = true;
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AsyncConnectionHelper::DispatchToTransactionPool()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction, "Only ok to call this with a transaction!");
|
|
|
|
TransactionPoolEventTarget target(mTransaction);
|
|
|
|
return Dispatch(&target);
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
AsyncConnectionHelper::SetCurrentTransaction(IDBTransaction* aTransaction)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aTransaction || !gCurrentTransaction,
|
|
|
|
"Stepping on another transaction!");
|
|
|
|
|
|
|
|
gCurrentTransaction = aTransaction;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
// static
|
|
|
|
IDBTransaction*
|
|
|
|
AsyncConnectionHelper::GetCurrentTransaction()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return gCurrentTransaction;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
nsresult
|
|
|
|
AsyncConnectionHelper::Init()
|
|
|
|
{
|
2011-01-07 06:21:36 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-09 11:34:29 +00:00
|
|
|
already_AddRefed<nsIDOMEvent>
|
|
|
|
AsyncConnectionHelper::CreateSuccessEvent(mozilla::dom::EventTarget* aOwner)
|
2011-10-20 16:10:56 +00:00
|
|
|
{
|
2013-03-09 11:34:29 +00:00
|
|
|
return CreateGenericEvent(mRequest, NS_LITERAL_STRING(SUCCESS_EVT_STR),
|
2011-11-23 14:15:15 +00:00
|
|
|
eDoesNotBubble, eNotCancelable);
|
2011-10-20 16:10:56 +00:00
|
|
|
}
|
|
|
|
|
2010-11-10 23:25:44 +00:00
|
|
|
nsresult
|
2011-01-07 06:21:36 +00:00
|
|
|
AsyncConnectionHelper::OnSuccess()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-01-27 01:53:02 +00:00
|
|
|
NS_ASSERTION(mRequest, "Null request!");
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-05-23 21:12:29 +00:00
|
|
|
PROFILER_MAIN_THREAD_LABEL("AsyncConnectionHelper", "OnSuccess",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2013-03-09 11:34:29 +00:00
|
|
|
nsRefPtr<nsIDOMEvent> event = CreateSuccessEvent(mRequest);
|
2010-06-23 19:46:08 +00:00
|
|
|
if (!event) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_WARNING("Failed to create event!");
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool dummy;
|
2011-01-07 06:21:36 +00:00
|
|
|
nsresult rv = mRequest->DispatchEvent(event, &dummy);
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-12-15 21:20:57 +00:00
|
|
|
|
2013-10-02 03:46:04 +00:00
|
|
|
WidgetEvent* internalEvent = event->GetInternalNSEvent();
|
2010-12-15 21:20:57 +00:00
|
|
|
NS_ASSERTION(internalEvent, "This should never be null!");
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
NS_ASSERTION(!mTransaction ||
|
|
|
|
mTransaction->IsOpen() ||
|
|
|
|
mTransaction->IsAborted(),
|
|
|
|
"How else can this be closed?!");
|
|
|
|
|
2012-12-16 01:26:04 +00:00
|
|
|
if (internalEvent->mFlags.mExceptionHasBeenRisen &&
|
2010-12-15 21:20:57 +00:00
|
|
|
mTransaction &&
|
2010-12-15 21:21:17 +00:00
|
|
|
mTransaction->IsOpen()) {
|
2012-06-29 16:48:34 +00:00
|
|
|
rv = mTransaction->Abort(NS_ERROR_DOM_INDEXEDDB_ABORT_ERR);
|
2010-12-15 21:20:57 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_OK;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-07 06:21:36 +00:00
|
|
|
AsyncConnectionHelper::OnError()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-01-27 01:53:02 +00:00
|
|
|
NS_ASSERTION(mRequest, "Null request!");
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-05-23 21:12:29 +00:00
|
|
|
PROFILER_MAIN_THREAD_LABEL("AsyncConnectionHelper", "OnError",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
// Make an error event and fire it at the target.
|
2013-03-09 11:34:29 +00:00
|
|
|
nsRefPtr<nsIDOMEvent> event =
|
|
|
|
CreateGenericEvent(mRequest, NS_LITERAL_STRING(ERROR_EVT_STR), eDoesBubble,
|
2011-11-23 14:15:15 +00:00
|
|
|
eCancelable);
|
2010-06-23 19:46:08 +00:00
|
|
|
if (!event) {
|
|
|
|
NS_ERROR("Failed to create event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool doDefault;
|
2011-01-07 06:21:36 +00:00
|
|
|
nsresult rv = mRequest->DispatchEvent(event, &doDefault);
|
2010-12-15 21:20:57 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-12-15 21:21:17 +00:00
|
|
|
NS_ASSERTION(!mTransaction ||
|
|
|
|
mTransaction->IsOpen() ||
|
|
|
|
mTransaction->IsAborted(),
|
|
|
|
"How else can this be closed?!");
|
|
|
|
|
2013-10-02 03:46:04 +00:00
|
|
|
WidgetEvent* internalEvent = event->GetInternalNSEvent();
|
2012-06-29 16:48:34 +00:00
|
|
|
NS_ASSERTION(internalEvent, "This should never be null!");
|
|
|
|
|
2012-12-16 01:26:04 +00:00
|
|
|
if (internalEvent->mFlags.mExceptionHasBeenRisen &&
|
2012-06-29 16:48:34 +00:00
|
|
|
mTransaction &&
|
|
|
|
mTransaction->IsOpen() &&
|
2012-06-29 16:48:34 +00:00
|
|
|
NS_FAILED(mTransaction->Abort(NS_ERROR_DOM_INDEXEDDB_ABORT_ERR))) {
|
2012-06-29 16:48:34 +00:00
|
|
|
NS_WARNING("Failed to abort transaction!");
|
|
|
|
}
|
|
|
|
|
2010-12-15 21:20:57 +00:00
|
|
|
if (doDefault &&
|
|
|
|
mTransaction &&
|
2010-12-15 21:21:17 +00:00
|
|
|
mTransaction->IsOpen() &&
|
2012-06-25 19:15:17 +00:00
|
|
|
NS_FAILED(mTransaction->Abort(mRequest))) {
|
2010-12-15 21:20:57 +00:00
|
|
|
NS_WARNING("Failed to abort transaction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("DispatchEvent failed!");
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-10 23:25:44 +00:00
|
|
|
nsresult
|
2011-01-07 06:21:36 +00:00
|
|
|
AsyncConnectionHelper::GetSuccessResult(JSContext* aCx,
|
2013-06-21 13:12:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-06-21 13:12:46 +00:00
|
|
|
aVal.setUndefined();
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_OK;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-09-10 04:54:25 +00:00
|
|
|
void
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mDatabase = nullptr;
|
|
|
|
mTransaction = nullptr;
|
2011-10-22 11:56:19 +00:00
|
|
|
|
|
|
|
HelperBase::ReleaseMainThreadObjects();
|
2010-09-10 04:54:25 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 19:09:33 +00:00
|
|
|
AsyncConnectionHelper::ChildProcessSendResult
|
|
|
|
AsyncConnectionHelper::MaybeSendResponseToChildProcess(nsresult aResultCode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
|
|
|
|
// If there's no request, there could never have been an actor, and so there
|
|
|
|
// is nothing to do.
|
|
|
|
if (!mRequest) {
|
|
|
|
return Success_NotSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDBTransaction* trans = GetCurrentTransaction();
|
|
|
|
// We may not have a transaction, e.g. for deleteDatabase
|
|
|
|
if (!trans) {
|
|
|
|
return Success_NotSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we shutting down the child?
|
2012-11-10 03:29:07 +00:00
|
|
|
IndexedDBDatabaseParent* dbActor = trans->Database()->GetActorParent();
|
|
|
|
if (dbActor && dbActor->IsDisconnected()) {
|
2012-10-05 19:09:33 +00:00
|
|
|
return Success_ActorDisconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
|
|
|
|
if (!actor) {
|
|
|
|
return Success_NotSent;
|
|
|
|
}
|
|
|
|
|
2013-03-16 06:58:50 +00:00
|
|
|
IDB_PROFILER_MARK("IndexedDB Request %llu: Sending response to child "
|
|
|
|
"process (rv = %lu)",
|
|
|
|
"IDBRequest[%llu] MT Done",
|
|
|
|
mRequest->GetSerialNumber(), aResultCode);
|
|
|
|
|
2012-10-05 19:09:33 +00:00
|
|
|
return SendResponseToChildProcess(aResultCode);
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
nsresult
|
|
|
|
AsyncConnectionHelper::OnParentProcessRequestComplete(
|
|
|
|
const ResponseValue& aResponseValue)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
|
|
|
|
if (aResponseValue.type() == ResponseValue::Tnsresult) {
|
|
|
|
NS_ASSERTION(NS_FAILED(aResponseValue.get_nsresult()), "Huh?");
|
|
|
|
SetError(aResponseValue.get_nsresult());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsresult rv = UnpackResponseFromParentProcess(aResponseValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Run();
|
|
|
|
}
|
|
|
|
|
2011-01-07 06:21:36 +00:00
|
|
|
// static
|
|
|
|
nsresult
|
2013-02-01 19:41:11 +00:00
|
|
|
AsyncConnectionHelper::ConvertToArrayAndCleanup(
|
2012-06-01 17:21:12 +00:00
|
|
|
JSContext* aCx,
|
|
|
|
nsTArray<StructuredCloneReadInfo>& aReadInfos,
|
2013-06-21 13:12:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> aResult)
|
2011-01-07 06:21:36 +00:00
|
|
|
{
|
2011-01-27 01:53:02 +00:00
|
|
|
NS_ASSERTION(aCx, "Null context!");
|
2013-06-21 13:12:46 +00:00
|
|
|
NS_ASSERTION(aResult.address(), "Null pointer!");
|
2011-01-27 01:53:02 +00:00
|
|
|
|
2011-12-16 07:34:24 +00:00
|
|
|
nsresult rv = ConvertCloneReadInfosToArrayInternal(aCx, aReadInfos, aResult);
|
2011-01-07 06:21:36 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < aReadInfos.Length(); index++) {
|
2011-12-16 07:34:24 +00:00
|
|
|
aReadInfos[index].mCloneBuffer.clear();
|
2011-01-07 06:21:36 +00:00
|
|
|
}
|
2011-12-16 07:34:24 +00:00
|
|
|
aReadInfos.Clear();
|
2011-01-07 06:21:36 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2012-06-01 17:21:12 +00:00
|
|
|
StackBasedEventTarget::AddRef()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("Don't call me!");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2012-06-01 17:21:12 +00:00
|
|
|
StackBasedEventTarget::Release()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("Don't call me!");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
StackBasedEventTarget::QueryInterface(REFNSIID aIID,
|
|
|
|
void** aInstancePtr)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Don't call me!");
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-14 16:31:17 +00:00
|
|
|
ImmediateRunEventTarget::Dispatch(nsIRunnable* aRunnable,
|
|
|
|
uint32_t aFlags)
|
2012-06-01 17:21:12 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
|
2013-03-14 16:31:17 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
runnable->Run();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return NS_OK;
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-14 16:31:17 +00:00
|
|
|
ImmediateRunEventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
2012-06-01 17:21:12 +00:00
|
|
|
{
|
2013-03-14 16:31:17 +00:00
|
|
|
*aIsOnCurrentThread = true;
|
2012-06-01 17:21:12 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionPoolEventTarget::Dispatch(nsIRunnable* aRunnable,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFlags)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL, "Unsupported!");
|
|
|
|
|
2011-11-02 12:53:12 +00:00
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
|
2012-06-01 17:21:12 +00:00
|
|
|
NS_ENSURE_TRUE(pool, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
nsresult rv = pool->Dispatch(mTransaction, aRunnable, false, nullptr);
|
2012-06-01 17:21:12 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionPoolEventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
|
|
|
{
|
|
|
|
*aIsOnCurrentThread = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NoDispatchEventTarget::Dispatch(nsIRunnable* aRunnable,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFlags)
|
2012-06-01 17:21:12 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = aRunnable;
|
|
|
|
return NS_OK;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-01 17:21:12 +00:00
|
|
|
NoDispatchEventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2012-06-01 17:21:12 +00:00
|
|
|
*aIsOnCurrentThread = true;
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|