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
|
|
|
|
|
|
|
#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"
|
2012-06-01 10:21:12 -07:00
|
|
|
#include "nsIEventTarget.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
class mozIStorageConnection;
|
|
|
|
|
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
class AutoSetCurrentTransaction;
|
2010-06-28 09:46:21 -07:00
|
|
|
class IDBTransaction;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
namespace ipc {
|
|
|
|
class ResponseValue;
|
|
|
|
}
|
|
|
|
|
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;
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
public:
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
virtual nsresult GetResultCode() = 0;
|
|
|
|
|
|
|
|
virtual nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal) = 0;
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
IDBRequest* GetRequest() const
|
|
|
|
{
|
|
|
|
return mRequest;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
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
|
|
|
|
{
|
2012-06-01 10:21:12 -07:00
|
|
|
friend class AutoSetCurrentTransaction;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
public:
|
2012-06-01 10:21:12 -07:00
|
|
|
typedef ipc::ResponseValue ResponseValue;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_MOZISTORAGEPROGRESSHANDLER
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual nsresult Dispatch(nsIEventTarget* aDatabaseThread);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2012-11-09 19:29:07 -08:00
|
|
|
bool HasTransaction() const
|
|
|
|
{
|
|
|
|
return !!mTransaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDBTransaction* GetTransaction() const
|
2011-10-25 05:49:31 -07:00
|
|
|
{
|
|
|
|
return mTransaction;
|
|
|
|
}
|
|
|
|
|
2012-11-09 19:29:07 -08:00
|
|
|
nsISupports* GetSource() const
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return mRequest ? mRequest->Source() : nullptr;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual nsresult GetResultCode() MOZ_OVERRIDE
|
2011-01-06 22:21:36 -08:00
|
|
|
{
|
|
|
|
return mResultCode;
|
|
|
|
}
|
|
|
|
|
2012-10-05 12:09:33 -07:00
|
|
|
enum ChildProcessSendResult
|
|
|
|
{
|
|
|
|
// The result was successfully sent to the child process
|
|
|
|
Success_Sent = 0,
|
|
|
|
|
|
|
|
// The result was not sent, because this is not an out-of-process request.
|
|
|
|
Success_NotSent,
|
|
|
|
|
|
|
|
// The result was not sent, because the actor has been disconnected
|
|
|
|
// (if the child process has shut down or crashed).
|
|
|
|
Success_ActorDisconnected,
|
|
|
|
|
|
|
|
// An error occurred.
|
|
|
|
Error
|
|
|
|
};
|
|
|
|
|
|
|
|
ChildProcessSendResult
|
|
|
|
MaybeSendResponseToChildProcess(nsresult aResultCode);
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual nsresult OnParentProcessRequestComplete(
|
|
|
|
const ResponseValue& aResponseValue);
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
UnpackResponseFromParentProcess(const ResponseValue& aResponseValue) = 0;
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-03-09 03:34:29 -08:00
|
|
|
virtual already_AddRefed<nsIDOMEvent> CreateSuccessEvent(
|
|
|
|
mozilla::dom::EventTarget* aOwner);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
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,
|
2012-06-01 10:21:12 -07:00
|
|
|
jsval* aVal) MOZ_OVERRIDE;
|
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.
|
|
|
|
*/
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
|
2010-09-09 21:54:25 -07:00
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
/**
|
|
|
|
* Helper to make a JS array object out of an array of clone buffers.
|
|
|
|
*/
|
2013-02-01 11:41:11 -08:00
|
|
|
static nsresult ConvertToArrayAndCleanup(
|
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);
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
/**
|
|
|
|
* This should only be called by AutoSetCurrentTransaction.
|
|
|
|
*/
|
|
|
|
static void SetCurrentTransaction(IDBTransaction* aTransaction);
|
|
|
|
|
2012-10-05 12:09:33 -07:00
|
|
|
/**
|
|
|
|
* Allows the subclass to send its results to the child process. Will only
|
|
|
|
* be called if all of the IPC infrastructure is available (there is an
|
|
|
|
* actor, the child is stil alive and hasn't begun shutting down).
|
|
|
|
*/
|
|
|
|
virtual ChildProcessSendResult
|
|
|
|
SendResponseToChildProcess(nsresult aResultCode) = 0;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_STACK_CLASS
|
|
|
|
class StackBasedEventTarget : public nsIEventTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
};
|
|
|
|
|
2013-03-14 09:31:17 -07:00
|
|
|
class ImmediateRunEventTarget : public StackBasedEventTarget
|
2012-06-01 10:21:12 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
};
|
|
|
|
|
|
|
|
class NoDispatchEventTarget : public StackBasedEventTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
};
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
#endif // mozilla_dom_indexeddb_asyncconnectionhelper_h__
|