2010-06-23 23:46:08 +04: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 20:46:49 +04:00
|
|
|
#include "IDBDatabase.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-21 19:02:01 +03:00
|
|
|
#include "jscntxt.h"
|
2010-09-10 02:15:40 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
2010-11-11 02:26:03 +03:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-12-21 19:02:01 +03:00
|
|
|
#include "nsJSUtils.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-09-10 02:15:40 +04:00
|
|
|
#include "CheckQuotaHelper.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "DatabaseInfo.h"
|
|
|
|
#include "IDBEvents.h"
|
2010-10-19 21:58:52 +04:00
|
|
|
#include "IDBIndex.h"
|
2010-06-28 22:51:06 +04:00
|
|
|
#include "IDBObjectStore.h"
|
2010-06-28 20:46:21 +04:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-28 20:44:30 +04:00
|
|
|
#include "IDBFactory.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "LazyIdleThread.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "TransactionThreadPool.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const PRUint32 kDefaultDatabaseTimeoutSeconds = 30;
|
|
|
|
|
2010-09-10 02:15:40 +04:00
|
|
|
PRUint32 gDatabaseInstanceCount = 0;
|
|
|
|
mozilla::Mutex* gPromptHelpersMutex = nsnull;
|
|
|
|
|
|
|
|
// Protected by gPromptHelpersMutex.
|
|
|
|
nsTArray<nsRefPtr<CheckQuotaHelper> >* gPromptHelpers = nsnull;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
class SetVersionHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
SetVersionHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
|
|
|
const nsAString& aVersion)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mVersion(aVersion)
|
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params
|
|
|
|
nsString mVersion;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CreateObjectStoreHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
CreateObjectStoreHelper(IDBTransaction* aTransaction,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBObjectStore* aObjectStore)
|
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mObjectStore(aObjectStore)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
|
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction->IsAborted(), "How else can this fail?!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
private:
|
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
class DeleteObjectStoreHelper : public AsyncConnectionHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteObjectStoreHelper(IDBTransaction* aTransaction,
|
2010-10-19 21:58:52 +04:00
|
|
|
PRInt64 aObjectStoreId)
|
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mObjectStoreId(aObjectStoreId)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
|
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction->IsAborted(), "How else can this fail?!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
PRInt64 mObjectStoreId;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_STACK_CLASS
|
2010-06-23 23:46:08 +04:00
|
|
|
class AutoFree
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoFree(void* aPtr) : mPtr(aPtr) { }
|
|
|
|
~AutoFree() { NS_Free(mPtr); }
|
|
|
|
private:
|
|
|
|
void* mPtr;
|
|
|
|
};
|
|
|
|
|
2010-10-19 21:58:52 +04: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 23:46:08 +04: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 20:46:49 +04:00
|
|
|
already_AddRefed<IDBDatabase>
|
2010-09-10 02:15:38 +04:00
|
|
|
IDBDatabase::Create(nsIScriptContext* aScriptContext,
|
|
|
|
nsPIDOMWindow* aOwner,
|
|
|
|
DatabaseInfo* aDatabaseInfo,
|
|
|
|
const nsACString& aASCIIOrigin)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabaseInfo, "Null pointer!");
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_ASSERTION(!aASCIIOrigin.IsEmpty(), "Empty origin!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
nsRefPtr<IDBDatabase> db(new IDBDatabase());
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
db->mScriptContext = aScriptContext;
|
|
|
|
db->mOwner = aOwner;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
db->mDatabaseId = aDatabaseInfo->id;
|
|
|
|
db->mName = aDatabaseInfo->name;
|
|
|
|
db->mFilePath = aDatabaseInfo->filePath;
|
2010-09-10 02:15:38 +04:00
|
|
|
db->mASCIIOrigin = aASCIIOrigin;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
2010-09-10 23:12:11 +04:00
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
|
|
|
if (!mgr->RegisterDatabase(db)) {
|
2010-10-19 21:58:42 +04:00
|
|
|
// Either out of memory or shutting down.
|
2010-09-10 23:12:11 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
return db.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::IDBDatabase()
|
2010-09-10 23:12:11 +04:00
|
|
|
: mDatabaseId(0),
|
2010-10-19 21:58:42 +04:00
|
|
|
mInvalidated(0),
|
2010-10-19 21:58:52 +04:00
|
|
|
mRegistered(false),
|
|
|
|
mClosed(false)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-09-10 23:12:11 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-09-10 02:15:40 +04:00
|
|
|
if (!gDatabaseInstanceCount++) {
|
|
|
|
NS_ASSERTION(!gPromptHelpersMutex, "Should be null!");
|
|
|
|
gPromptHelpersMutex = new mozilla::Mutex("IDBDatabase gPromptHelpersMutex");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::~IDBDatabase()
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-09-10 23:12:11 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 21:58:42 +04:00
|
|
|
if (mRegistered) {
|
2010-10-19 21:58:52 +04:00
|
|
|
CloseInternal();
|
|
|
|
|
2010-10-19 21:58:42 +04:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
if (mgr) {
|
|
|
|
mgr->UnregisterDatabase(this);
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
if (mDatabaseId && !mInvalidated) {
|
2010-06-23 23:46:08 +04: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-10 02:15:38 +04:00
|
|
|
|
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
}
|
2010-09-10 02:15:40 +04:00
|
|
|
|
|
|
|
if (!--gDatabaseInstanceCount) {
|
|
|
|
NS_ASSERTION(gPromptHelpersMutex, "Should not be null!");
|
|
|
|
|
|
|
|
delete gPromptHelpers;
|
|
|
|
gPromptHelpers = nsnull;
|
|
|
|
|
|
|
|
delete gPromptHelpersMutex;
|
|
|
|
gPromptHelpersMutex = nsnull;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-09-10 02:15:40 +04: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 23:12:11 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::Invalidate()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 21:58:39 +04:00
|
|
|
NS_ASSERTION(gPromptHelpersMutex, "This should never be null!");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
// Make sure we're closed too.
|
|
|
|
Close();
|
|
|
|
|
2010-10-19 21:58:39 +04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-28 23:58:49 +04:00
|
|
|
if (!PR_ATOMIC_SET(&mInvalidated, 1)) {
|
2010-10-19 21:58:52 +04:00
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-10-19 21:58:33 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(info->referenceCount, "Bad reference count!");
|
|
|
|
if (--info->referenceCount == 0) {
|
|
|
|
DatabaseInfo::Remove(mDatabaseId);
|
|
|
|
}
|
2010-10-19 21:58:33 +04:00
|
|
|
}
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBDatabase::IsInvalidated()
|
|
|
|
{
|
|
|
|
return !!mInvalidated;
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04: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-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBDatabase)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBDatabase,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnVersionChangeListener)
|
2010-09-10 02:15:38 +04: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-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnVersionChangeListener)
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
// Do some cleanup.
|
|
|
|
tmp->OnUnlink();
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBDatabase)
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBDatabase)
|
2010-06-28 20:46:49 +04:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBDatabase)
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBDatabase, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBDatabase, nsDOMEventTargetHelper)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
DOMCI_DATA(IDBDatabase, IDBDatabase)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::GetName(nsAString& aName)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::GetVersion(nsAString& aVersion)
|
2010-06-23 23:46:08 +04: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 20:46:49 +04:00
|
|
|
IDBDatabase::GetObjectStoreNames(nsIDOMDOMStringList** aObjectStores)
|
2010-06-23 23:46:08 +04: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-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
PRUint32 count = objectStoreNames.Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(list->Add(objectStoreNames[index]),
|
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
list.forget(aObjectStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::CreateObjectStore(const nsAString& aName,
|
2010-12-21 19:02:01 +03:00
|
|
|
const jsval& aOptions,
|
|
|
|
JSContext* aCx,
|
2010-10-19 21:58:52 +04:00
|
|
|
nsIIDBObjectStore** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (aName.IsEmpty()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
// XXX Update spec for a real error code here.
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
|
|
|
|
|
|
|
if (!transaction ||
|
|
|
|
transaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
DatabaseInfo* databaseInfo;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &databaseInfo)) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (databaseInfo->ContainsStoreName(aName)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-21 19:02:01 +03:00
|
|
|
nsString keyPath;
|
|
|
|
bool autoIncrement = false;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_VOID(aOptions) && !JSVAL_IS_NULL(aOptions)) {
|
|
|
|
if (JSVAL_IS_PRIMITIVE(aOptions)) {
|
|
|
|
// XXX Update spec for a real code here
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(JSVAL_IS_OBJECT(aOptions), "Huh?!");
|
|
|
|
JSObject* options = JSVAL_TO_OBJECT(aOptions);
|
|
|
|
|
|
|
|
js::AutoIdArray ids(aCx, JS_Enumerate(aCx, options));
|
|
|
|
if (!ids) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < ids.length(); index++) {
|
|
|
|
jsid id = ids[index];
|
|
|
|
|
|
|
|
if (id != nsDOMClassInfo::sKeyPath_id &&
|
|
|
|
id != nsDOMClassInfo::sAutoIncrement_id) {
|
|
|
|
// XXX Update spec for a real code here
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval val;
|
|
|
|
if (!JS_GetPropertyById(aCx, options, id, &val)) {
|
|
|
|
NS_WARNING("JS_GetPropertyById failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == nsDOMClassInfo::sKeyPath_id) {
|
|
|
|
JSString* str = JS_ValueToString(aCx, val);
|
|
|
|
if (!str) {
|
|
|
|
NS_WARNING("JS_ValueToString failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2010-12-30 03:25:04 +03:00
|
|
|
nsDependentJSString dependentKeyPath;
|
|
|
|
if (!dependentKeyPath.init(aCx, str)) {
|
|
|
|
NS_WARNING("Initializing keyPath failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
keyPath = dependentKeyPath;
|
2010-12-21 19:02:01 +03:00
|
|
|
}
|
|
|
|
else if (id == nsDOMClassInfo::sAutoIncrement_id) {
|
|
|
|
JSBool boolVal;
|
|
|
|
if (!JS_ValueToBoolean(aCx, val, &boolVal)) {
|
|
|
|
NS_WARNING("JS_ValueToBoolean failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
autoIncrement = !!boolVal;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Shouldn't be able to get here!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsAutoPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo());
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
newInfo->name = aName;
|
|
|
|
newInfo->id = databaseInfo->nextObjectStoreId++;
|
|
|
|
newInfo->keyPath = keyPath;
|
2010-12-21 19:02:01 +03:00
|
|
|
newInfo->autoIncrement = autoIncrement;
|
2010-10-19 21:58:52 +04:00
|
|
|
newInfo->databaseId = mDatabaseId;
|
|
|
|
|
|
|
|
if (!ObjectStoreInfo::Put(newInfo)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_WARNING("Put failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 21:58:52 +04: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-11 02:25:40 +03:00
|
|
|
transaction->GetOrCreateObjectStore(aName, objectStoreInfo);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(objectStore, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-27 00:57:25 +04:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsRefPtr<CreateObjectStoreHelper> helper =
|
2010-10-19 21:58:52 +04:00
|
|
|
new CreateObjectStoreHelper(transaction, objectStore);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
autoRemove.forget();
|
|
|
|
|
|
|
|
objectStore.forget(_retval);
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-11 02:25:57 +03:00
|
|
|
IDBDatabase::DeleteObjectStore(const nsAString& aName)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (!transaction ||
|
|
|
|
transaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
2010-11-11 02:25:44 +03: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 21:58:52 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
nsRefPtr<DeleteObjectStoreHelper> helper =
|
|
|
|
new DeleteObjectStoreHelper(transaction, objectStoreInfo->id);
|
2010-06-23 23:46:08 +04:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
ObjectStoreInfo::Remove(mDatabaseId, aName);
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::SetVersion(const nsAString& aVersion,
|
2010-08-27 00:57:25 +04:00
|
|
|
JSContext* aCx,
|
2010-06-28 20:46:49 +04:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mClosed) {
|
2010-11-11 02:25:44 +03:00
|
|
|
// XXX Update spec for a real error code here.
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabaseId, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
// Lock the whole database.
|
2010-06-23 23:46:08 +04:00
|
|
|
nsTArray<nsString> storesToOpen;
|
2010-06-28 20:46:21 +04:00
|
|
|
nsRefPtr<IDBTransaction> transaction =
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBTransaction::Create(this, storesToOpen, IDBTransaction::VERSION_CHANGE,
|
2010-11-16 00:49:49 +03:00
|
|
|
kDefaultDatabaseTimeoutSeconds, true);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(transaction, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBVersionChangeRequest> request =
|
2011-06-24 06:18:00 +04:00
|
|
|
IDBVersionChangeRequest::Create(static_cast<nsIDOMEventTarget*>(this),
|
2010-11-11 02:26:03 +03:00
|
|
|
ScriptContext(), Owner(), transaction);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-27 00:57:25 +04:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsRefPtr<SetVersionHelper> helper =
|
|
|
|
new SetVersionHelper(transaction, request, aVersion);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
|
|
|
nsresult rv = mgr->SetDatabaseVersion(this, request, aVersion, helper);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::Transaction(nsIVariant* aStoreNames,
|
|
|
|
PRUint16 aMode,
|
|
|
|
PRUint32 aTimeout,
|
2010-08-27 00:57:25 +04:00
|
|
|
JSContext* aCx,
|
2010-06-28 20:46:49 +04:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBTransaction** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 21:58:42 +04:00
|
|
|
if (IndexedDatabaseManager::IsShuttingDown()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 21:58:42 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mClosed) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
if (aOptionalArgCount) {
|
|
|
|
if (aMode != nsIIDBTransaction::READ_WRITE &&
|
2011-01-23 21:16:00 +03:00
|
|
|
aMode != nsIIDBTransaction::READ_ONLY) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aMode = nsIIDBTransaction::READ_ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOptionalArgCount <= 1) {
|
|
|
|
aTimeout = kDefaultDatabaseTimeoutSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 type;
|
|
|
|
nsresult rv = aStoreNames->GetDataType(&type);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04: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-11 02:25:44 +03:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS: {
|
|
|
|
// Single name
|
|
|
|
nsString name;
|
|
|
|
rv = aStoreNames->GetAsAString(name);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (!info->ContainsStoreName(name)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storesToOpen.AppendElement(name)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_ARRAY: {
|
|
|
|
nsTArray<nsString> names;
|
|
|
|
rv = ConvertVariantToStringArray(aStoreNames, names);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
PRUint32 nameCount = names.Length();
|
|
|
|
for (PRUint32 nameIndex = 0; nameIndex < nameCount; nameIndex++) {
|
|
|
|
nsString& name = names[nameIndex];
|
|
|
|
|
|
|
|
if (!info->ContainsStoreName(name)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storesToOpen.AppendElement(name)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04: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-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_Free(iid);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDOMStringList> stringList(do_QueryInterface(supports));
|
|
|
|
if (!stringList) {
|
|
|
|
// We don't support anything other than nsIDOMDOMStringList.
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 stringCount;
|
|
|
|
rv = stringList->GetLength(&stringCount);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
for (PRUint32 stringIndex = 0; stringIndex < stringCount; stringIndex++) {
|
|
|
|
nsString name;
|
|
|
|
rv = stringList->Item(stringIndex, name);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (!info->ContainsStoreName(name)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storesToOpen.AppendElement(name)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
nsRefPtr<IDBTransaction> transaction =
|
2010-09-10 02:15:38 +04:00
|
|
|
IDBTransaction::Create(this, storesToOpen, aMode,
|
2010-06-28 20:46:21 +04:00
|
|
|
kDefaultDatabaseTimeoutSeconds);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(transaction, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
transaction.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04: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-11 02:26:03 +03: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) {
|
2011-01-07 09:21:36 +03:00
|
|
|
nsString type;
|
|
|
|
nsresult rv = aVisitor.mDOMEvent->GetType(type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (type.EqualsLiteral(ERROR_EVT_STR)) {
|
|
|
|
nsRefPtr<nsDOMEvent> duplicateEvent = CreateGenericEvent(type);
|
|
|
|
NS_ENSURE_STATE(duplicateEvent);
|
2010-11-11 02:26:03 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(mOwner));
|
|
|
|
NS_ASSERTION(target, "How can this happen?!");
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool dummy;
|
2011-01-07 09:21:36 +03:00
|
|
|
rv = target->DispatchEvent(duplicateEvent, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-11 02:26:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04: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-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("version"), mVersion);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(stmt->Execute())) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-01-27 04:53:02 +03:00
|
|
|
SetVersionHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabase->Id(), &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
info->version = mVersion;
|
|
|
|
|
2011-06-24 06:18:00 +04:00
|
|
|
nsresult rv = WrapNative(aCx, NS_ISUPPORTS_CAST(nsIDOMEventTarget*,
|
2011-01-27 04:53:02 +03:00
|
|
|
mTransaction),
|
|
|
|
aVal);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-01-07 09:21:36 +03:00
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
CreateObjectStoreHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
2010-10-19 21:58:52 +04: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-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"),
|
|
|
|
mObjectStore->Id());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("name"), mObjectStore->Name());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("key_path"),
|
|
|
|
mObjectStore->KeyPath());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("auto_increment"),
|
2010-10-19 21:58:52 +04:00
|
|
|
mObjectStore->IsAutoIncrement() ? 1 : 0);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->Execute();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteObjectStoreHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-10-19 21:58:52 +04:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(NS_LITERAL_CSTRING(
|
2010-06-23 23:46:08 +04:00
|
|
|
"DELETE FROM object_store "
|
2010-10-19 21:58:52 +04:00
|
|
|
"WHERE id = :id "
|
|
|
|
));
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"), mObjectStoreId);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->Execute();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|