michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 sts=2 expandtab michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "StorageBaseStatementInternal.h" michael@0: michael@0: #include "nsProxyRelease.h" michael@0: michael@0: #include "mozStorageBindingParamsArray.h" michael@0: #include "mozStorageStatementData.h" michael@0: #include "mozStorageAsyncStatementExecution.h" michael@0: michael@0: namespace mozilla { michael@0: namespace storage { michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Local Classes michael@0: michael@0: /** michael@0: * Used to finalize an asynchronous statement on the background thread. michael@0: */ michael@0: class AsyncStatementFinalizer : public nsRunnable michael@0: { michael@0: public: michael@0: /** michael@0: * Constructor for the event. michael@0: * michael@0: * @param aStatement michael@0: * We need the AsyncStatement to be able to get at the sqlite3_stmt; michael@0: * we only access/create it on the async thread. michael@0: * @param aConnection michael@0: * We need the connection to know what thread to release the statement michael@0: * on. We release the statement on that thread since releasing the michael@0: * statement might end up releasing the connection too. michael@0: */ michael@0: AsyncStatementFinalizer(StorageBaseStatementInternal *aStatement, michael@0: Connection *aConnection) michael@0: : mStatement(aStatement) michael@0: , mConnection(aConnection) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: if (mStatement->mAsyncStatement) { michael@0: (void)::sqlite3_finalize(mStatement->mAsyncStatement); michael@0: mStatement->mAsyncStatement = nullptr; michael@0: } michael@0: (void)::NS_ProxyRelease(mConnection->threadOpenedOn, mStatement); michael@0: return NS_OK; michael@0: } michael@0: private: michael@0: nsRefPtr mStatement; michael@0: nsRefPtr mConnection; michael@0: }; michael@0: michael@0: /** michael@0: * Finalize a sqlite3_stmt on the background thread for a statement whose michael@0: * destructor was invoked and the statement was non-null. michael@0: */ michael@0: class LastDitchSqliteStatementFinalizer : public nsRunnable michael@0: { michael@0: public: michael@0: /** michael@0: * Event constructor. michael@0: * michael@0: * @param aConnection michael@0: * Used to keep the connection alive. If we failed to do this, it michael@0: * is possible that the statement going out of scope invoking us michael@0: * might have the last reference to the connection and so trigger michael@0: * an attempt to close the connection which is doomed to fail michael@0: * (because the asynchronous execution thread must exist which will michael@0: * trigger the failure case). michael@0: * @param aStatement michael@0: * The sqlite3_stmt to finalize. This object takes ownership / michael@0: * responsibility for the instance and all other references to it michael@0: * should be forgotten. michael@0: */ michael@0: LastDitchSqliteStatementFinalizer(nsRefPtr &aConnection, michael@0: sqlite3_stmt *aStatement) michael@0: : mConnection(aConnection) michael@0: , mAsyncStatement(aStatement) michael@0: { michael@0: NS_PRECONDITION(aConnection, "You must provide a Connection"); michael@0: } michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: (void)::sqlite3_finalize(mAsyncStatement); michael@0: mAsyncStatement = nullptr; michael@0: michael@0: // Because of our ambiguous nsISupports we cannot use the NS_ProxyRelease michael@0: // template helpers. michael@0: Connection *rawConnection = nullptr; michael@0: mConnection.swap(rawConnection); michael@0: (void)::NS_ProxyRelease( michael@0: rawConnection->threadOpenedOn, michael@0: NS_ISUPPORTS_CAST(mozIStorageConnection *, rawConnection)); michael@0: return NS_OK; michael@0: } michael@0: private: michael@0: nsRefPtr mConnection; michael@0: sqlite3_stmt *mAsyncStatement; michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// StorageBaseStatementInternal michael@0: michael@0: StorageBaseStatementInternal::StorageBaseStatementInternal() michael@0: : mAsyncStatement(nullptr) michael@0: { michael@0: } michael@0: michael@0: void michael@0: StorageBaseStatementInternal::asyncFinalize() michael@0: { michael@0: nsIEventTarget *target = mDBConnection->getAsyncExecutionTarget(); michael@0: if (target) { michael@0: // Attempt to finalize asynchronously michael@0: nsCOMPtr event = michael@0: new AsyncStatementFinalizer(this, mDBConnection); michael@0: michael@0: // Dispatch. Note that dispatching can fail, typically if michael@0: // we have a race condition with asyncClose(). It's ok, michael@0: // let asyncClose() win. michael@0: (void)target->Dispatch(event, NS_DISPATCH_NORMAL); michael@0: } michael@0: // If we cannot get the background thread, michael@0: // mozStorageConnection::AsyncClose() has already been called and michael@0: // the statement either has been or will be cleaned up by michael@0: // internalClose(). michael@0: } michael@0: michael@0: void michael@0: StorageBaseStatementInternal::destructorAsyncFinalize() michael@0: { michael@0: if (!mAsyncStatement) michael@0: return; michael@0: michael@0: // If we reach this point, our owner has not finalized this michael@0: // statement, yet we are being destructed. If possible, we want to michael@0: // auto-finalize it early, to release the resources early. michael@0: nsIEventTarget *target = mDBConnection->getAsyncExecutionTarget(); michael@0: if (target) { michael@0: // If we can get the async execution target, we can indeed finalize michael@0: // the statement, as the connection is still open. michael@0: bool isAsyncThread = false; michael@0: (void)target->IsOnCurrentThread(&isAsyncThread); michael@0: michael@0: nsCOMPtr event = michael@0: new LastDitchSqliteStatementFinalizer(mDBConnection, mAsyncStatement); michael@0: if (isAsyncThread) { michael@0: (void)event->Run(); michael@0: } else { michael@0: (void)target->Dispatch(event, NS_DISPATCH_NORMAL); michael@0: } michael@0: } michael@0: michael@0: // We might not be able to dispatch to the background thread, michael@0: // presumably because it is being shutdown. Since said shutdown will michael@0: // finalize the statement, we just need to clean-up around here. michael@0: mAsyncStatement = nullptr; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: StorageBaseStatementInternal::NewBindingParamsArray( michael@0: mozIStorageBindingParamsArray **_array michael@0: ) michael@0: { michael@0: nsCOMPtr array = new BindingParamsArray(this); michael@0: NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: array.forget(_array); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: StorageBaseStatementInternal::ExecuteAsync( michael@0: mozIStorageStatementCallback *aCallback, michael@0: mozIStoragePendingStatement **_stmt michael@0: ) michael@0: { michael@0: // We used to call Connection::ExecuteAsync but it takes a michael@0: // mozIStorageBaseStatement signature because it is also a public API. Since michael@0: // our 'this' has no static concept of mozIStorageBaseStatement and Connection michael@0: // would just QI it back across to a StorageBaseStatementInternal and the michael@0: // actual logic is very simple, we now roll our own. michael@0: nsTArray stmts(1); michael@0: StatementData data; michael@0: nsresult rv = getAsynchronousStatementData(data); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: NS_ENSURE_TRUE(stmts.AppendElement(data), NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: // Dispatch to the background michael@0: return AsyncExecuteStatements::execute(stmts, mDBConnection, michael@0: mNativeConnection, aCallback, _stmt); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: StorageBaseStatementInternal::EscapeStringForLIKE( michael@0: const nsAString &aValue, michael@0: const char16_t aEscapeChar, michael@0: nsAString &_escapedString michael@0: ) michael@0: { michael@0: const char16_t MATCH_ALL('%'); michael@0: const char16_t MATCH_ONE('_'); michael@0: michael@0: _escapedString.Truncate(0); michael@0: michael@0: for (uint32_t i = 0; i < aValue.Length(); i++) { michael@0: if (aValue[i] == aEscapeChar || aValue[i] == MATCH_ALL || michael@0: aValue[i] == MATCH_ONE) { michael@0: _escapedString += aEscapeChar; michael@0: } michael@0: _escapedString += aValue[i]; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace storage michael@0: } // namespace mozilla