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 ***** */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_indexeddb_asyncconnectionhelper_h__
|
|
|
|
#define mozilla_dom_indexeddb_asyncconnectionhelper_h__
|
|
|
|
|
|
|
|
// Only meant to be included in IndexedDB source files, not exported.
|
2011-12-15 23:34:24 -08:00
|
|
|
#include "DatabaseInfo.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "IndexedDatabase.h"
|
2010-06-28 09:46:49 -07:00
|
|
|
#include "IDBDatabase.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "IDBRequest.h"
|
|
|
|
|
|
|
|
#include "mozIStorageProgressHandler.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
class mozIStorageConnection;
|
2011-11-08 09:43:00 -08:00
|
|
|
class nsIEventTarget;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
class IDBTransaction;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
// A common base class for AsyncConnectionHelper and OpenDatabaseHelper that
|
|
|
|
// IDBRequest can use.
|
|
|
|
class HelperBase : public nsIRunnable
|
|
|
|
{
|
|
|
|
friend class IDBRequest;
|
|
|
|
public:
|
|
|
|
virtual nsresult GetResultCode() = 0;
|
|
|
|
|
|
|
|
virtual nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
HelperBase(IDBRequest* aRequest)
|
|
|
|
: mRequest(aRequest)
|
|
|
|
{ }
|
|
|
|
|
2011-10-22 04:56:19 -07:00
|
|
|
virtual ~HelperBase();
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
/**
|
|
|
|
* Helper to wrap a native into a jsval. Uses the global object of the request
|
|
|
|
* to parent the native.
|
|
|
|
*/
|
|
|
|
nsresult WrapNative(JSContext* aCx,
|
|
|
|
nsISupports* aNative,
|
|
|
|
jsval* aResult);
|
|
|
|
|
2011-10-22 04:56:19 -07:00
|
|
|
/**
|
|
|
|
* Gives the subclass a chance to release any objects that must be released
|
|
|
|
* on the main thread, regardless of success or failure. Subclasses that
|
|
|
|
* implement this method *MUST* call the base class implementation as well.
|
|
|
|
*/
|
|
|
|
virtual void ReleaseMainThreadObjects();
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsRefPtr<IDBRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
/**
|
|
|
|
* Must be subclassed. The subclass must implement DoDatabaseWork. It may then
|
|
|
|
* choose to implement OnSuccess and OnError depending on the needs of the
|
|
|
|
* subclass. If the default implementation of OnSuccess is desired then the
|
|
|
|
* subclass can implement GetSuccessResult to properly set the result of the
|
|
|
|
* success event. Call Dispatch to start the database operation. Must be created
|
|
|
|
* and Dispatched from the main thread only. Target thread may not be the main
|
|
|
|
* thread.
|
|
|
|
*/
|
2011-10-20 09:10:56 -07:00
|
|
|
class AsyncConnectionHelper : public HelperBase,
|
2010-06-23 12:46:08 -07:00
|
|
|
public mozIStorageProgressHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_MOZISTORAGEPROGRESSHANDLER
|
|
|
|
|
|
|
|
nsresult Dispatch(nsIEventTarget* aDatabaseThread);
|
|
|
|
|
|
|
|
// Only for transactions!
|
|
|
|
nsresult DispatchToTransactionPool();
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
void SetError(nsresult aErrorCode)
|
2010-09-09 15:15:40 -07:00
|
|
|
{
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ASSERTION(NS_FAILED(aErrorCode), "Not a failure code!");
|
|
|
|
mResultCode = aErrorCode;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
static IDBTransaction* GetCurrentTransaction();
|
|
|
|
|
2011-10-25 05:49:31 -07:00
|
|
|
bool HasTransaction()
|
|
|
|
{
|
|
|
|
return mTransaction;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsISupports* GetSource()
|
|
|
|
{
|
|
|
|
return mRequest ? mRequest->Source() : nsnull;
|
|
|
|
}
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult GetResultCode()
|
|
|
|
{
|
|
|
|
return mResultCode;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
protected:
|
2010-06-28 09:46:49 -07:00
|
|
|
AsyncConnectionHelper(IDBDatabase* aDatabase,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest);
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
AsyncConnectionHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest);
|
|
|
|
|
|
|
|
virtual ~AsyncConnectionHelper();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called on the main thread after Dispatch is called but before the
|
|
|
|
* runnable is actually dispatched to the database thread. Allows the subclass
|
2011-01-06 22:21:36 -08:00
|
|
|
* to initialize itself.
|
2010-06-23 12:46:08 -07:00
|
|
|
*/
|
|
|
|
virtual nsresult Init();
|
|
|
|
|
|
|
|
/**
|
2011-01-06 22:21:36 -08:00
|
|
|
* This callback is run on the database thread.
|
2010-06-23 12:46:08 -07:00
|
|
|
*/
|
2010-11-10 15:25:44 -08:00
|
|
|
virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection) = 0;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
/**
|
|
|
|
* This function returns the event to be dispatched at the request when
|
|
|
|
* OnSuccess is called. A subclass can override this to fire an event other
|
|
|
|
* than "success" at the request.
|
|
|
|
*/
|
|
|
|
virtual already_AddRefed<nsDOMEvent> CreateSuccessEvent();
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
/**
|
2011-01-06 22:21:36 -08:00
|
|
|
* This callback is run on the main thread if DoDatabaseWork returned NS_OK.
|
|
|
|
* The default implementation fires a "success" DOM event with its target set
|
|
|
|
* to the request. Returning anything other than NS_OK from the OnSuccess
|
2010-06-23 12:46:08 -07:00
|
|
|
* callback will trigger the OnError callback.
|
|
|
|
*/
|
2011-01-06 22:21:36 -08:00
|
|
|
virtual nsresult OnSuccess();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
/**
|
2011-01-06 22:21:36 -08:00
|
|
|
* This callback is run on the main thread if DoDatabaseWork or OnSuccess
|
|
|
|
* returned an error code. The default implementation fires an "error" DOM
|
|
|
|
* event with its target set to the request.
|
2010-06-23 12:46:08 -07:00
|
|
|
*/
|
2011-01-06 22:21:36 -08:00
|
|
|
virtual void OnError();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
/**
|
2011-01-06 22:21:36 -08:00
|
|
|
* This function is called by the request on the main thread when script
|
|
|
|
* accesses the result property of the request.
|
2010-06-23 12:46:08 -07:00
|
|
|
*/
|
2011-01-06 22:21:36 -08:00
|
|
|
virtual nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
/**
|
|
|
|
* Gives the subclass a chance to release any objects that must be released
|
|
|
|
* on the main thread, regardless of success or failure. Subclasses that
|
|
|
|
* implement this method *MUST* call the base class implementation as well.
|
|
|
|
*/
|
|
|
|
virtual void ReleaseMainThreadObjects();
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
/**
|
|
|
|
* Helper to make a JS array object out of an array of clone buffers.
|
|
|
|
*/
|
2011-12-15 23:34:24 -08:00
|
|
|
static nsresult 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);
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
protected:
|
2010-06-28 09:46:49 -07:00
|
|
|
nsRefPtr<IDBDatabase> mDatabase;
|
2010-06-28 09:46:21 -07:00
|
|
|
nsRefPtr<IDBTransaction> mTransaction;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<mozIStorageProgressHandler> mOldProgressHandler;
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult mResultCode;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDispatched;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
#endif // mozilla_dom_indexeddb_asyncconnectionhelper_h__
|