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 ***** */
|
|
|
|
|
2010-06-28 09:46:49 -07:00
|
|
|
#include "IDBDatabase.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
2010-11-10 15:26:03 -08:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-09-09 15:15:40 -07:00
|
|
|
#include "CheckQuotaHelper.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "DatabaseInfo.h"
|
|
|
|
#include "IDBEvents.h"
|
2010-10-19 10:58:52 -07:00
|
|
|
#include "IDBIndex.h"
|
2010-06-28 11:51:06 -07:00
|
|
|
#include "IDBObjectStore.h"
|
2010-06-28 09:46:21 -07:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-28 09:44:30 -07:00
|
|
|
#include "IDBFactory.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "LazyIdleThread.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "TransactionThreadPool.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const PRUint32 kDefaultDatabaseTimeoutSeconds = 30;
|
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
PRUint32 gDatabaseInstanceCount = 0;
|
|
|
|
mozilla::Mutex* gPromptHelpersMutex = nsnull;
|
|
|
|
|
|
|
|
// Protected by gPromptHelpersMutex.
|
|
|
|
nsTArray<nsRefPtr<CheckQuotaHelper> >* gPromptHelpers = nsnull;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
class SetVersionHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 09:46:21 -07:00
|
|
|
SetVersionHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest,
|
|
|
|
const nsAString& aVersion)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mVersion(aVersion)
|
|
|
|
{ }
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult GetSuccessResult(nsIWritableVariant* aResult);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params
|
|
|
|
nsString mVersion;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CreateObjectStoreHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 09:46:21 -07:00
|
|
|
CreateObjectStoreHelper(IDBTransaction* aTransaction,
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBObjectStore* aObjectStore)
|
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mObjectStore(aObjectStore)
|
2010-06-23 12:46:08 -07:00
|
|
|
{ }
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
|
|
|
void OnError(nsIDOMEventTarget* aTarget, nsresult aErrorCode);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2010-11-10 15:25:57 -08:00
|
|
|
class DeleteObjectStoreHelper : public AsyncConnectionHelper
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteObjectStoreHelper(IDBTransaction* aTransaction,
|
2010-10-19 10:58:52 -07:00
|
|
|
PRInt64 aObjectStoreId)
|
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mObjectStoreId(aObjectStoreId)
|
2010-06-23 12:46:08 -07:00
|
|
|
{ }
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
|
|
|
void OnError(nsIDOMEventTarget* aTarget, nsresult aErrorCode);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params.
|
2010-10-19 10:58:52 -07:00
|
|
|
PRInt64 mObjectStoreId;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_STACK_CLASS
|
2010-06-23 12:46:08 -07:00
|
|
|
class AutoFree
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoFree(void* aPtr) : mPtr(aPtr) { }
|
|
|
|
~AutoFree() { NS_Free(mPtr); }
|
|
|
|
private:
|
|
|
|
void* mPtr;
|
|
|
|
};
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_STACK_CLASS
|
|
|
|
class AutoRemoveObjectStore
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoRemoveObjectStore(PRUint32 aId, const nsAString& aName)
|
|
|
|
: mId(aId), mName(aName)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~AutoRemoveObjectStore()
|
|
|
|
{
|
|
|
|
if (mId) {
|
|
|
|
ObjectStoreInfo::Remove(mId, mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forget()
|
|
|
|
{
|
|
|
|
mId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint32 mId;
|
|
|
|
nsString mName;
|
|
|
|
};
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
inline
|
|
|
|
nsresult
|
|
|
|
ConvertVariantToStringArray(nsIVariant* aVariant,
|
|
|
|
nsTArray<nsString>& aStringArray)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRUint16 type;
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(aVariant->GetDataType(&type)) &&
|
|
|
|
type == nsIDataType::VTYPE_ARRAY, "Bad arg!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRUint16 valueType;
|
|
|
|
nsIID iid;
|
|
|
|
PRUint32 valueCount;
|
|
|
|
void* rawArray;
|
|
|
|
|
|
|
|
nsresult rv = aVariant->GetAsArray(&valueType, &iid, &valueCount, &rawArray);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
AutoFree af(rawArray);
|
|
|
|
|
|
|
|
// Just delete anything that we don't expect and return.
|
|
|
|
if (valueType != nsIDataType::VTYPE_WCHAR_STR) {
|
|
|
|
switch (valueType) {
|
|
|
|
case nsIDataType::VTYPE_ID:
|
|
|
|
case nsIDataType::VTYPE_CHAR_STR: {
|
|
|
|
char** charArray = reinterpret_cast<char**>(rawArray);
|
|
|
|
for (PRUint32 index = 0; index < valueCount; index++) {
|
|
|
|
if (charArray[index]) {
|
|
|
|
NS_Free(charArray[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_INTERFACE:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE_IS: {
|
|
|
|
nsISupports** supportsArray = reinterpret_cast<nsISupports**>(rawArray);
|
|
|
|
for (PRUint32 index = 0; index < valueCount; index++) {
|
|
|
|
NS_IF_RELEASE(supportsArray[index]);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
// The other types are primitives that do not need to be freed.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar** strings = reinterpret_cast<PRUnichar**>(rawArray);
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < valueCount; index++) {
|
|
|
|
nsString* newString = aStringArray.AppendElement();
|
|
|
|
|
|
|
|
if (!newString) {
|
|
|
|
NS_ERROR("Out of memory?");
|
|
|
|
for (; index < valueCount; index++) {
|
|
|
|
NS_Free(strings[index]);
|
|
|
|
}
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
newString->Adopt(strings[index], -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// static
|
2010-06-28 09:46:49 -07:00
|
|
|
already_AddRefed<IDBDatabase>
|
2010-09-09 15:15:38 -07:00
|
|
|
IDBDatabase::Create(nsIScriptContext* aScriptContext,
|
|
|
|
nsPIDOMWindow* aOwner,
|
|
|
|
DatabaseInfo* aDatabaseInfo,
|
|
|
|
const nsACString& aASCIIOrigin)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabaseInfo, "Null pointer!");
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_ASSERTION(!aASCIIOrigin.IsEmpty(), "Empty origin!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 09:46:49 -07:00
|
|
|
nsRefPtr<IDBDatabase> db(new IDBDatabase());
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
db->mScriptContext = aScriptContext;
|
|
|
|
db->mOwner = aOwner;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
db->mDatabaseId = aDatabaseInfo->id;
|
|
|
|
db->mName = aDatabaseInfo->name;
|
|
|
|
db->mFilePath = aDatabaseInfo->filePath;
|
2010-09-09 15:15:38 -07:00
|
|
|
db->mASCIIOrigin = aASCIIOrigin;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
|
|
|
if (!mgr->RegisterDatabase(db)) {
|
2010-10-19 10:58:42 -07:00
|
|
|
// Either out of memory or shutting down.
|
2010-09-10 12:12:11 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
return db.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::IDBDatabase()
|
2010-09-10 12:12:11 -07:00
|
|
|
: mDatabaseId(0),
|
2010-10-19 10:58:42 -07:00
|
|
|
mInvalidated(0),
|
2010-10-19 10:58:52 -07:00
|
|
|
mRegistered(false),
|
|
|
|
mClosed(false)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
if (!gDatabaseInstanceCount++) {
|
|
|
|
NS_ASSERTION(!gPromptHelpersMutex, "Should be null!");
|
|
|
|
gPromptHelpersMutex = new mozilla::Mutex("IDBDatabase gPromptHelpersMutex");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::~IDBDatabase()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
if (mRegistered) {
|
2010-10-19 10:58:52 -07:00
|
|
|
CloseInternal();
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
if (mgr) {
|
|
|
|
mgr->UnregisterDatabase(this);
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
if (mDatabaseId && !mInvalidated) {
|
2010-06-23 12:46:08 -07:00
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(info->referenceCount, "Bad reference count!");
|
|
|
|
if (--info->referenceCount == 0) {
|
|
|
|
DatabaseInfo::Remove(mDatabaseId);
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 15:15:38 -07:00
|
|
|
|
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
}
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
if (!--gDatabaseInstanceCount) {
|
|
|
|
NS_ASSERTION(gPromptHelpersMutex, "Should not be null!");
|
|
|
|
|
|
|
|
delete gPromptHelpers;
|
|
|
|
gPromptHelpers = nsnull;
|
|
|
|
|
|
|
|
delete gPromptHelpersMutex;
|
|
|
|
gPromptHelpersMutex = nsnull;
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
bool
|
|
|
|
IDBDatabase::IsQuotaDisabled()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(gPromptHelpersMutex, "This should never be null!");
|
|
|
|
|
|
|
|
MutexAutoLock lock(*gPromptHelpersMutex);
|
|
|
|
|
|
|
|
if (!gPromptHelpers) {
|
|
|
|
gPromptHelpers = new nsAutoTArray<nsRefPtr<CheckQuotaHelper>, 10>();
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckQuotaHelper* foundHelper = nsnull;
|
|
|
|
|
|
|
|
PRUint32 count = gPromptHelpers->Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
|
|
|
nsRefPtr<CheckQuotaHelper>& helper = gPromptHelpers->ElementAt(index);
|
|
|
|
if (helper->WindowSerial() == Owner()->GetSerial()) {
|
|
|
|
foundHelper = helper;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundHelper) {
|
|
|
|
nsRefPtr<CheckQuotaHelper>* newHelper = gPromptHelpers->AppendElement();
|
|
|
|
if (!newHelper) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*newHelper = new CheckQuotaHelper(this, *gPromptHelpersMutex);
|
|
|
|
foundHelper = *newHelper;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Unlock before calling out to XPCOM.
|
|
|
|
MutexAutoUnlock unlock(*gPromptHelpersMutex);
|
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToMainThread(foundHelper, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundHelper->PromptAndReturnQuotaIsDisabled();
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
void
|
|
|
|
IDBDatabase::Invalidate()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 10:58:39 -07:00
|
|
|
NS_ASSERTION(gPromptHelpersMutex, "This should never be null!");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
// Make sure we're closed too.
|
|
|
|
Close();
|
|
|
|
|
2010-10-19 10:58:39 -07:00
|
|
|
// Cancel any quota prompts that are currently being displayed.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(*gPromptHelpersMutex);
|
|
|
|
|
|
|
|
if (gPromptHelpers) {
|
|
|
|
PRUint32 count = gPromptHelpers->Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
|
|
|
nsRefPtr<CheckQuotaHelper>& helper = gPromptHelpers->ElementAt(index);
|
|
|
|
if (helper->WindowSerial() == Owner()->GetSerial()) {
|
|
|
|
helper->Cancel();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (!PR_AtomicSet(&mInvalidated, 1)) {
|
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_ASSERTION(info->referenceCount, "Bad reference count!");
|
|
|
|
if (--info->referenceCount == 0) {
|
|
|
|
DatabaseInfo::Remove(mDatabaseId);
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBDatabase::IsInvalidated()
|
|
|
|
{
|
|
|
|
return !!mInvalidated;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void
|
|
|
|
IDBDatabase::CloseInternal()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mClosed) {
|
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
if (mgr) {
|
|
|
|
mgr->OnDatabaseClosed(this);
|
|
|
|
}
|
|
|
|
mClosed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBDatabase::IsClosed()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
return mClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBDatabase::OnUnlink()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mOwner, "Should have been cleared already!");
|
|
|
|
|
|
|
|
// We've been unlinked, at the very least we should be able to prevent further
|
|
|
|
// transactions from starting and unblock any other SetVersion callers.
|
|
|
|
Close();
|
|
|
|
|
|
|
|
// No reason for the IndexedDatabaseManager to track us any longer.
|
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
if (mgr) {
|
|
|
|
mgr->UnregisterDatabase(this);
|
|
|
|
|
|
|
|
// Don't try to unregister again in the destructor.
|
|
|
|
mRegistered = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBDatabase)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBDatabase,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnVersionChangeListener)
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBDatabase,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener)
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnVersionChangeListener)
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
// Do some cleanup.
|
|
|
|
tmp->OnUnlink();
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBDatabase)
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBDatabase)
|
2010-06-28 09:46:49 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBDatabase)
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBDatabase, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBDatabase, nsDOMEventTargetHelper)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 09:46:49 -07:00
|
|
|
DOMCI_DATA(IDBDatabase, IDBDatabase)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::GetName(nsAString& aName)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::GetVersion(nsAString& aVersion)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
aVersion.Assign(info->version);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::GetObjectStoreNames(nsIDOMDOMStringList** aObjectStores)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<nsString, 10> objectStoreNames;
|
|
|
|
if (!info->GetObjectStoreNames(objectStoreNames)) {
|
|
|
|
NS_WARNING("Couldn't get names!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
PRUint32 count = objectStoreNames.Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(list->Add(objectStoreNames[index]),
|
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
list.forget(aObjectStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::CreateObjectStore(const nsAString& aName,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
PRBool aAutoIncrement,
|
2010-10-19 10:58:52 -07:00
|
|
|
nsIIDBObjectStore** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (aName.IsEmpty()) {
|
2010-11-10 15:25:44 -08:00
|
|
|
// XXX Update spec for a real error code here.
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// XPConnect makes "null" into a void string, we need an empty string.
|
|
|
|
nsString keyPath(aKeyPath);
|
|
|
|
if (keyPath.IsVoid()) {
|
|
|
|
keyPath.Truncate();
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
|
|
|
|
|
|
|
if (!transaction ||
|
|
|
|
transaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
DatabaseInfo* databaseInfo;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &databaseInfo)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (databaseInfo->ContainsStoreName(aName)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsAutoPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo());
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
newInfo->name = aName;
|
|
|
|
newInfo->id = databaseInfo->nextObjectStoreId++;
|
|
|
|
newInfo->keyPath = keyPath;
|
|
|
|
newInfo->autoIncrement = aAutoIncrement;
|
|
|
|
newInfo->databaseId = mDatabaseId;
|
|
|
|
|
|
|
|
if (!ObjectStoreInfo::Put(newInfo)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_WARNING("Put failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
ObjectStoreInfo* objectStoreInfo = newInfo.forget();
|
|
|
|
|
|
|
|
// Don't leave this in the hash if we fail below!
|
|
|
|
AutoRemoveObjectStore autoRemove(mDatabaseId, aName);
|
|
|
|
|
|
|
|
nsRefPtr<IDBObjectStore> objectStore =
|
2010-11-10 15:25:40 -08:00
|
|
|
transaction->GetOrCreateObjectStore(aName, objectStoreInfo);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(objectStore, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:25 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsRefPtr<CreateObjectStoreHelper> helper =
|
2010-10-19 10:58:52 -07:00
|
|
|
new CreateObjectStoreHelper(transaction, objectStore);
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
autoRemove.forget();
|
|
|
|
|
|
|
|
objectStore.forget(_retval);
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-10 15:25:57 -08:00
|
|
|
IDBDatabase::DeleteObjectStore(const nsAString& aName)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (!transaction ||
|
|
|
|
transaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo* objectStoreInfo;
|
|
|
|
if (!ObjectStoreInfo::Get(mDatabaseId, aName, &objectStoreInfo)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:57 -08:00
|
|
|
nsRefPtr<DeleteObjectStoreHelper> helper =
|
|
|
|
new DeleteObjectStoreHelper(transaction, objectStoreInfo->id);
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
ObjectStoreInfo::Remove(mDatabaseId, aName);
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::SetVersion(const nsAString& aVersion,
|
2010-08-26 13:57:25 -07:00
|
|
|
JSContext* aCx,
|
2010-06-28 09:46:49 -07:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (mClosed) {
|
2010-11-10 15:25:44 -08:00
|
|
|
// XXX Update spec for a real error code here.
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
// Lock the whole database.
|
2010-06-23 12:46:08 -07:00
|
|
|
nsTArray<nsString> storesToOpen;
|
2010-06-28 09:46:21 -07:00
|
|
|
nsRefPtr<IDBTransaction> transaction =
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBTransaction::Create(this, storesToOpen, IDBTransaction::VERSION_CHANGE,
|
2010-11-15 13:49:49 -08:00
|
|
|
kDefaultDatabaseTimeoutSeconds, true);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(transaction, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<IDBVersionChangeRequest> request =
|
|
|
|
IDBVersionChangeRequest::Create(static_cast<nsPIDOMEventTarget*>(this),
|
2010-11-10 15:26:03 -08:00
|
|
|
ScriptContext(), Owner(), transaction);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:25 -07:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsRefPtr<SetVersionHelper> helper =
|
|
|
|
new SetVersionHelper(transaction, request, aVersion);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
|
|
|
nsresult rv = mgr->SetDatabaseVersion(this, request, aVersion, helper);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 09:46:49 -07:00
|
|
|
IDBDatabase::Transaction(nsIVariant* aStoreNames,
|
|
|
|
PRUint16 aMode,
|
|
|
|
PRUint32 aTimeout,
|
2010-08-26 13:57:25 -07:00
|
|
|
JSContext* aCx,
|
2010-06-28 09:46:49 -07:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBTransaction** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
if (IndexedDatabaseManager::IsShuttingDown()) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 10:58:42 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (mClosed) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
if (aOptionalArgCount) {
|
|
|
|
if (aMode != nsIIDBTransaction::READ_WRITE &&
|
|
|
|
aMode != nsIIDBTransaction::READ_ONLY &&
|
|
|
|
aMode != nsIIDBTransaction::SNAPSHOT_READ) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
if (aMode == nsIIDBTransaction::SNAPSHOT_READ) {
|
|
|
|
NS_NOTYETIMPLEMENTED("Implement me!");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aMode = nsIIDBTransaction::READ_ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOptionalArgCount <= 1) {
|
|
|
|
aTimeout = kDefaultDatabaseTimeoutSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 type;
|
|
|
|
nsresult rv = aStoreNames->GetDataType(&type);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsString> storesToOpen;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case nsIDataType::VTYPE_VOID:
|
|
|
|
case nsIDataType::VTYPE_EMPTY:
|
|
|
|
case nsIDataType::VTYPE_EMPTY_ARRAY: {
|
|
|
|
// Empty, request all object stores
|
|
|
|
if (!info->GetObjectStoreNames(storesToOpen)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS: {
|
|
|
|
// Single name
|
|
|
|
nsString name;
|
|
|
|
rv = aStoreNames->GetAsAString(name);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
if (!info->ContainsStoreName(name)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
// XXX Update spec for a real error code here.
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storesToOpen.AppendElement(name)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_ARRAY: {
|
|
|
|
nsTArray<nsString> names;
|
|
|
|
rv = ConvertVariantToStringArray(aStoreNames, names);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
PRUint32 nameCount = names.Length();
|
|
|
|
for (PRUint32 nameIndex = 0; nameIndex < nameCount; nameIndex++) {
|
|
|
|
nsString& name = names[nameIndex];
|
|
|
|
|
|
|
|
if (!info->ContainsStoreName(name)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storesToOpen.AppendElement(name)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ASSERTION(nameCount == storesToOpen.Length(), "Should have bailed!");
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_INTERFACE:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE_IS: {
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
nsID *iid;
|
|
|
|
rv = aStoreNames->GetAsInterface(&iid, getter_AddRefs(supports));
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
NS_Free(iid);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDOMStringList> stringList(do_QueryInterface(supports));
|
|
|
|
if (!stringList) {
|
|
|
|
// We don't support anything other than nsIDOMDOMStringList.
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 stringCount;
|
|
|
|
rv = stringList->GetLength(&stringCount);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
for (PRUint32 stringIndex = 0; stringIndex < stringCount; stringIndex++) {
|
|
|
|
nsString name;
|
|
|
|
rv = stringList->Item(stringIndex, name);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
if (!info->ContainsStoreName(name)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storesToOpen.AppendElement(name)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-06-28 09:46:21 -07:00
|
|
|
nsRefPtr<IDBTransaction> transaction =
|
2010-09-09 15:15:38 -07:00
|
|
|
IDBTransaction::Create(this, storesToOpen, aMode,
|
2010-06-28 09:46:21 -07:00
|
|
|
kDefaultDatabaseTimeoutSeconds);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(transaction, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
transaction.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBDatabase::Close()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
CloseInternal();
|
|
|
|
|
|
|
|
NS_ASSERTION(mClosed, "Should have set the closed flag!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBDatabase::SetOnerror(nsIDOMEventListener* aErrorListener)
|
|
|
|
{
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(ERROR_EVT_STR),
|
|
|
|
mOnErrorListener, aErrorListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBDatabase::GetOnerror(nsIDOMEventListener** aErrorListener)
|
|
|
|
{
|
|
|
|
return GetInnerEventListener(mOnErrorListener, aErrorListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBDatabase::SetOnversionchange(nsIDOMEventListener* aVersionChangeListener)
|
|
|
|
{
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(VERSIONCHANGE_EVT_STR),
|
|
|
|
mOnVersionChangeListener,
|
|
|
|
aVersionChangeListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBDatabase::GetOnversionchange(nsIDOMEventListener** aVersionChangeListener)
|
|
|
|
{
|
|
|
|
return GetInnerEventListener(mOnVersionChangeListener,
|
|
|
|
aVersionChangeListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
IDBDatabase::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aVisitor.mDOMEvent, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
if (aVisitor.mEventStatus != nsEventStatus_eConsumeNoDefault) {
|
|
|
|
nsCOMPtr<nsIDOMEvent> duplicateEvent =
|
|
|
|
IDBErrorEvent::MaybeDuplicate(aVisitor.mDOMEvent);
|
|
|
|
|
|
|
|
if (duplicateEvent) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(mOwner));
|
|
|
|
NS_ASSERTION(target, "How can this happen?!");
|
|
|
|
|
|
|
|
PRBool dummy;
|
|
|
|
target->DispatchEvent(duplicateEvent, &dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
SetVersionHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passing a null connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE database "
|
|
|
|
"SET version = :version"
|
|
|
|
), getter_AddRefs(stmt));
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("version"), mVersion);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(stmt->Execute())) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
SetVersionHelper::GetSuccessResult(nsIWritableVariant* /* aResult */)
|
|
|
|
{
|
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabase->Id(), &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
info->version = mVersion;
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
CreateObjectStoreHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
2010-10-19 10:58:52 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_store (id, name, key_path, auto_increment) "
|
|
|
|
"VALUES (:id, :name, :key_path, :auto_increment)"
|
|
|
|
));
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"),
|
|
|
|
mObjectStore->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("name"), mObjectStore->Name());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("key_path"),
|
|
|
|
mObjectStore->KeyPath());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("auto_increment"),
|
2010-10-19 10:58:52 -07:00
|
|
|
mObjectStore->IsAutoIncrement() ? 1 : 0);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->Execute();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-10-19 10:58:52 -07:00
|
|
|
CreateObjectStoreHelper::OnSuccess(nsIDOMEventTarget* aTarget)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_ASSERTION(!aTarget, "Huh?!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void
|
|
|
|
CreateObjectStoreHelper::OnError(nsIDOMEventTarget* aTarget,
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult aErrorCode)
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aTarget, "Huh?!");
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteObjectStoreHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-10-19 10:58:52 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(NS_LITERAL_CSTRING(
|
2010-06-23 12:46:08 -07:00
|
|
|
"DELETE FROM object_store "
|
2010-10-19 10:58:52 -07:00
|
|
|
"WHERE id = :id "
|
|
|
|
));
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"), mObjectStoreId);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->Execute();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteObjectStoreHelper::OnSuccess(nsIDOMEventTarget* aTarget)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_ASSERTION(!aTarget, "Huh?!");
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
void
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteObjectStoreHelper::OnError(nsIDOMEventTarget* aTarget,
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult aErrorCode)
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("Removing an object store should never fail here!");
|
|
|
|
}
|