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: */
|
2012-05-21 15:12:37 +04: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 23:46:08 +04:00
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
#include "IDBDatabase.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "FileInfo.h"
|
|
|
|
#include "FileManager.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "IDBEvents.h"
|
2012-08-30 04:50:28 +04:00
|
|
|
#include "IDBFactory.h"
|
2010-10-19 21:58:52 +04:00
|
|
|
#include "IDBIndex.h"
|
2014-06-12 07:35:29 +04:00
|
|
|
#include "IDBMutableFile.h"
|
2010-06-28 22:51:06 +04:00
|
|
|
#include "IDBObjectStore.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "IDBRequest.h"
|
2010-06-28 20:46:21 +04:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-28 20:44:30 +04:00
|
|
|
#include "IDBFactory.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/DOMStringList.h"
|
|
|
|
#include "mozilla/dom/DOMStringListBinding.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/IDBDatabaseBinding.h"
|
|
|
|
#include "mozilla/dom/IDBObjectStoreBinding.h"
|
|
|
|
#include "mozilla/dom/indexedDB/PBackgroundIDBDatabaseFileChild.h"
|
|
|
|
#include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/ipc/nsIRemoteBlob.h"
|
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
|
|
|
#include "mozilla/ipc/FileDescriptor.h"
|
|
|
|
#include "mozilla/ipc/InputStreamParams.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2014-11-14 05:20:38 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2014-11-14 05:20:38 +03:00
|
|
|
#include "nsIScriptError.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-03-16 10:58:50 +04:00
|
|
|
#include "ProfilerHelpers.h"
|
2014-01-28 04:37:05 +04:00
|
|
|
#include "ReportInternalError.h"
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// Include this last to avoid path problems on Windows.
|
|
|
|
#include "ActorsChild.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2013-05-18 05:48:25 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
using namespace mozilla::dom::quota;
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
using namespace mozilla::services;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const char kCycleCollectionObserverTopic[] = "cycle-collector-end";
|
|
|
|
const char kMemoryPressureObserverTopic[] = "memory-pressure";
|
|
|
|
const char kWindowObserverTopic[] = "inner-window-destroyed";
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// XXX This should either be ported to PBackground or removed someday.
|
|
|
|
class CreateFileHelper MOZ_FINAL
|
|
|
|
: public nsRunnable
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<IDBDatabase> mDatabase;
|
|
|
|
nsRefPtr<IDBRequest> mRequest;
|
|
|
|
nsRefPtr<FileInfo> mFileInfo;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const nsString mName;
|
|
|
|
const nsString mType;
|
|
|
|
const nsString mDatabaseName;
|
|
|
|
const nsCString mOrigin;
|
2011-01-07 09:21:36 +03:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const PersistenceType mPersistenceType;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsresult mResultCode;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
public:
|
2014-09-27 03:21:57 +04:00
|
|
|
static nsresult
|
|
|
|
CreateAndDispatch(IDBDatabase* aDatabase,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aType);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
private:
|
2012-06-03 20:33:52 +04:00
|
|
|
CreateFileHelper(IDBDatabase* aDatabase,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
const nsAString& aName,
|
2014-09-27 03:21:57 +04:00
|
|
|
const nsAString& aType,
|
|
|
|
const nsACString& aOrigin);
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
~CreateFileHelper()
|
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsresult
|
|
|
|
DoDatabaseWork();
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
DoMainThreadWork(nsresult aResultCode);
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2012-06-03 20:33:52 +04:00
|
|
|
};
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
class DatabaseFile MOZ_FINAL
|
|
|
|
: public PBackgroundIDBDatabaseFileChild
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase* mDatabase;
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
public:
|
|
|
|
explicit DatabaseFile(IDBDatabase* aDatabase)
|
|
|
|
: mDatabase(aDatabase)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(aDatabase);
|
|
|
|
aDatabase->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
MOZ_COUNT_CTOR(DatabaseFile);
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
private:
|
|
|
|
~DatabaseFile()
|
2014-09-13 20:12:19 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(!mDatabase);
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(DatabaseFile);
|
2014-09-13 20:12:19 +04:00
|
|
|
}
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
virtual void
|
|
|
|
ActorDestroy(ActorDestroyReason aWhy) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDatabase);
|
|
|
|
mDatabase->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (aWhy != Deletion) {
|
|
|
|
nsRefPtr<IDBDatabase> database = mDatabase;
|
|
|
|
database->NoteFinishedFileActor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mDatabase = nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
2010-10-19 21:58:52 +04:00
|
|
|
};
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
class IDBDatabase::Observer MOZ_FINAL
|
|
|
|
: public nsIObserver
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase* mWeakDatabase;
|
|
|
|
const uint64_t mWindowId;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
public:
|
|
|
|
Observer(IDBDatabase* aDatabase, uint64_t aWindowId)
|
|
|
|
: mWeakDatabase(aDatabase)
|
|
|
|
, mWindowId(aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aDatabase);
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
Revoke()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mWeakDatabase = nullptr;
|
|
|
|
}
|
2013-03-26 15:13:17 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
private:
|
|
|
|
~Observer()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mWeakDatabase);
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::IDBDatabase(IDBWrapperCache* aOwnerCache,
|
|
|
|
IDBFactory* aFactory,
|
|
|
|
BackgroundDatabaseChild* aActor,
|
|
|
|
DatabaseSpec* aSpec)
|
|
|
|
: IDBWrapperCache(aOwnerCache)
|
|
|
|
, mFactory(aFactory)
|
|
|
|
, mSpec(aSpec)
|
|
|
|
, mBackgroundActor(aActor)
|
|
|
|
, mClosed(false)
|
|
|
|
, mInvalidated(false)
|
2013-03-26 15:13:17 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(aOwnerCache);
|
|
|
|
MOZ_ASSERT(aFactory);
|
|
|
|
aFactory->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aActor);
|
|
|
|
MOZ_ASSERT(aSpec);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-28 20:46:49 +04:00
|
|
|
IDBDatabase::~IDBDatabase()
|
2013-07-09 21:30:58 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mBackgroundActor);
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<IDBDatabase>
|
|
|
|
IDBDatabase::Create(IDBWrapperCache* aOwnerCache,
|
|
|
|
IDBFactory* aFactory,
|
|
|
|
BackgroundDatabaseChild* aActor,
|
|
|
|
DatabaseSpec* aSpec)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(aOwnerCache);
|
|
|
|
MOZ_ASSERT(aFactory);
|
|
|
|
aFactory->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aActor);
|
|
|
|
MOZ_ASSERT(aSpec);
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<IDBDatabase> db =
|
|
|
|
new IDBDatabase(aOwnerCache, aFactory, aActor, aSpec);
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
db->SetScriptOwner(aOwnerCache->GetScriptOwner());
|
2010-10-19 21:58:39 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (nsPIDOMWindow* window = aFactory->GetParentObject()) {
|
|
|
|
MOZ_ASSERT(window->IsInnerWindow());
|
2014-04-04 03:57:40 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
uint64_t windowId = window->WindowID();
|
2012-06-20 05:50:39 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<Observer> observer = new Observer(db, windowId);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = GetObserverService();
|
|
|
|
MOZ_ASSERT(obsSvc);
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// This topic must be successfully registered.
|
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
obsSvc->AddObserver(observer, kWindowObserverTopic, false)))) {
|
|
|
|
observer->Revoke();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// These topics are not crucial.
|
|
|
|
if (NS_FAILED(obsSvc->AddObserver(observer,
|
|
|
|
kCycleCollectionObserverTopic,
|
|
|
|
false)) ||
|
|
|
|
NS_FAILED(obsSvc->AddObserver(observer,
|
|
|
|
kMemoryPressureObserverTopic,
|
|
|
|
false))) {
|
|
|
|
NS_WARNING("Failed to add additional memory observers!");
|
|
|
|
}
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
db->mObserver.swap(observer);
|
|
|
|
}
|
2014-09-18 03:36:01 +04:00
|
|
|
}
|
2012-10-05 23:09:33 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
return db.forget();
|
2014-09-18 03:36:01 +04:00
|
|
|
}
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2012-10-05 23:09:33 +04:00
|
|
|
void
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::AssertIsOnOwningThread() const
|
2012-10-05 23:09:33 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(mFactory);
|
|
|
|
mFactory->AssertIsOnOwningThread();
|
2014-09-18 03:36:01 +04:00
|
|
|
}
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
#endif // DEBUG
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::CloseInternal()
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
if (!mClosed) {
|
2012-08-10 05:06:21 +04:00
|
|
|
mClosed = true;
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
ExpireFileActors(/* aExpireAll */ true);
|
|
|
|
|
|
|
|
if (mObserver) {
|
|
|
|
mObserver->Revoke();
|
2011-11-08 04:15:45 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = GetObserverService();
|
|
|
|
if (obsSvc) {
|
|
|
|
// These might not have been registered.
|
|
|
|
obsSvc->RemoveObserver(mObserver, kCycleCollectionObserverTopic);
|
|
|
|
obsSvc->RemoveObserver(mObserver, kMemoryPressureObserverTopic);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
obsSvc->RemoveObserver(mObserver, kWindowObserverTopic)));
|
2011-11-08 04:15:45 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mObserver = nullptr;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (mBackgroundActor && !mInvalidated) {
|
|
|
|
mBackgroundActor->SendClose();
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::InvalidateInternal()
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
InvalidateMutableFiles();
|
2014-11-14 05:20:38 +03:00
|
|
|
AbortTransactions(/* aShouldWarn */ true);
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
CloseInternal();
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2011-10-25 16:49:31 +04:00
|
|
|
void
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::EnterSetVersionTransaction(uint64_t aNewVersion)
|
2011-10-25 16:49:31 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aNewVersion);
|
|
|
|
MOZ_ASSERT(!RunningVersionChangeTransaction());
|
|
|
|
MOZ_ASSERT(mSpec);
|
|
|
|
MOZ_ASSERT(!mPreviousSpec);
|
2012-06-29 20:48:34 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mPreviousSpec = new DatabaseSpec(*mSpec);
|
2012-06-29 20:48:34 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mSpec->metadata().version() = aNewVersion;
|
2011-10-25 16:49:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBDatabase::ExitSetVersionTransaction()
|
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (mPreviousSpec) {
|
|
|
|
mPreviousSpec = nullptr;
|
|
|
|
}
|
2011-10-25 16:49:31 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 20:48:34 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::RevertToPreviousState()
|
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(RunningVersionChangeTransaction());
|
|
|
|
MOZ_ASSERT(mPreviousSpec);
|
2012-06-29 20:48:34 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// Hold the current spec alive until RefreshTransactionsSpecEnumerator has
|
|
|
|
// finished!
|
|
|
|
nsAutoPtr<DatabaseSpec> currentSpec(mSpec.forget());
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mSpec = mPreviousSpec.forget();
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
RefreshSpec(/* aMayDelete */ true);
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::RefreshSpec(bool aMayDelete)
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
class MOZ_STACK_CLASS Helper MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PLDHashOperator
|
|
|
|
RefreshTransactionsSpec(nsPtrHashKey<IDBTransaction>* aTransaction,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTransaction);
|
|
|
|
aTransaction->GetKey()->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aClosure);
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
bool mayDelete = *static_cast<bool*>(aClosure);
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<IDBTransaction> transaction = aTransaction->GetKey();
|
|
|
|
transaction->RefreshSpec(mayDelete);
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
return PL_DHASH_NEXT;
|
2013-07-31 19:48:46 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
};
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mTransactions.EnumerateEntries(Helper::RefreshTransactionsSpec, &aMayDelete);
|
2014-09-13 20:12:19 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsPIDOMWindow*
|
|
|
|
IDBDatabase::GetParentObject() const
|
|
|
|
{
|
|
|
|
return mFactory->GetParentObject();
|
|
|
|
}
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const nsString&
|
|
|
|
IDBDatabase::Name() const
|
2014-09-18 03:36:01 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mSpec);
|
|
|
|
|
|
|
|
return mSpec->metadata().name();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
uint64_t
|
|
|
|
IDBDatabase::Version() const
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mSpec);
|
|
|
|
|
|
|
|
return mSpec->metadata().version();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 21:54:21 +04:00
|
|
|
already_AddRefed<DOMStringList>
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::ObjectStoreNames() const
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mSpec);
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const nsTArray<ObjectStoreSpec>& objectStores = mSpec->objectStores();
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<DOMStringList> list = new DOMStringList();
|
|
|
|
|
|
|
|
if (!objectStores.IsEmpty()) {
|
|
|
|
nsTArray<nsString>& listNames = list->StringArray();
|
|
|
|
listNames.SetCapacity(objectStores.Length());
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < objectStores.Length(); index++) {
|
|
|
|
listNames.InsertElementSorted(objectStores[index].metadata().name());
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
return list.forget();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
already_AddRefed<nsIDocument>
|
|
|
|
IDBDatabase::GetOwnerDocument() const
|
|
|
|
{
|
|
|
|
if (nsPIDOMWindow* window = GetOwner()) {
|
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
|
|
|
|
return doc.forget();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
already_AddRefed<IDBObjectStore>
|
|
|
|
IDBDatabase::CreateObjectStore(
|
2014-09-27 03:21:57 +04:00
|
|
|
const nsAString& aName,
|
2013-07-31 19:48:46 +04:00
|
|
|
const IDBObjectStoreParameters& aOptionalParameters,
|
|
|
|
ErrorResult& aRv)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBTransaction* transaction = IDBTransaction::GetCurrent();
|
2010-11-11 02:25:44 +03:00
|
|
|
|
|
|
|
if (!transaction ||
|
2012-03-13 08:44:45 +04:00
|
|
|
transaction->GetMode() != IDBTransaction::VERSION_CHANGE) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
|
|
|
|
return nullptr;
|
2010-11-11 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(transaction->IsOpen());
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2013-05-18 05:48:25 +04:00
|
|
|
KeyPath keyPath(0);
|
2014-12-11 21:07:44 +03:00
|
|
|
if (NS_FAILED(KeyPath::Parse(aOptionalParameters.mKeyPath, &keyPath))) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
2010-12-21 19:02:01 +03:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsTArray<ObjectStoreSpec>& objectStores = mSpec->objectStores();
|
|
|
|
for (uint32_t count = objectStores.Length(), index = 0;
|
|
|
|
index < count;
|
|
|
|
index++) {
|
|
|
|
if (aName == objectStores[index].metadata().name()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-12-04 21:39:01 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
if (!keyPath.IsAllowedForObjectStore(aOptionalParameters.mAutoIncrement)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
2011-11-21 18:56:24 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const ObjectStoreSpec* oldSpecElements =
|
|
|
|
objectStores.IsEmpty() ? nullptr : objectStores.Elements();
|
|
|
|
|
|
|
|
ObjectStoreSpec* newSpec = objectStores.AppendElement();
|
|
|
|
newSpec->metadata() =
|
|
|
|
ObjectStoreMetadata(transaction->NextObjectStoreId(), nsString(aName),
|
|
|
|
keyPath, aOptionalParameters.mAutoIncrement);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (oldSpecElements &&
|
|
|
|
oldSpecElements != objectStores.Elements()) {
|
|
|
|
MOZ_ASSERT(objectStores.Length() > 1);
|
|
|
|
|
|
|
|
// Array got moved, update the spec pointers for all live objectStores and
|
|
|
|
// indexes.
|
|
|
|
RefreshSpec(/* aMayDelete */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDBObjectStore> objectStore =
|
|
|
|
transaction->CreateObjectStore(*newSpec);
|
|
|
|
MOZ_ASSERT(objectStore);
|
|
|
|
|
2014-10-16 08:56:52 +04:00
|
|
|
// Don't do this in the macro because we always need to increment the serial
|
|
|
|
// number to keep in sync with the parent.
|
|
|
|
const uint64_t requestSerialNumber = IDBRequest::NextSerialNumber();
|
|
|
|
|
|
|
|
IDB_LOG_MARK("IndexedDB %s: Child Transaction[%lld] Request[%llu]: "
|
|
|
|
"database(%s).transaction(%s).createObjectStore(%s)",
|
|
|
|
"IndexedDB %s: C T[%lld] R[%llu]: "
|
|
|
|
"IDBDatabase.createObjectStore()",
|
|
|
|
IDB_LOG_ID_STRING(),
|
|
|
|
transaction->LoggingSerialNumber(),
|
|
|
|
requestSerialNumber,
|
|
|
|
IDB_LOG_STRINGIFY(this),
|
|
|
|
IDB_LOG_STRINGIFY(transaction),
|
|
|
|
IDB_LOG_STRINGIFY(objectStore));
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
return objectStore.forget();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::DeleteObjectStore(const nsAString& aName, ErrorResult& aRv)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBTransaction* transaction = IDBTransaction::GetCurrent();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (!transaction ||
|
2012-03-13 08:44:45 +04:00
|
|
|
transaction->GetMode() != IDBTransaction::VERSION_CHANGE) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
|
|
|
|
return;
|
2010-11-11 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(transaction->IsOpen());
|
|
|
|
|
|
|
|
nsTArray<ObjectStoreSpec>& specArray = mSpec->objectStores();
|
|
|
|
|
|
|
|
int64_t objectStoreId = 0;
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
for (uint32_t specCount = specArray.Length(), specIndex = 0;
|
|
|
|
specIndex < specCount;
|
|
|
|
specIndex++) {
|
|
|
|
const ObjectStoreMetadata& metadata = specArray[specIndex].metadata();
|
|
|
|
MOZ_ASSERT(metadata.id());
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (aName == metadata.name()) {
|
|
|
|
objectStoreId = metadata.id();
|
|
|
|
|
|
|
|
// Must do this before altering the metadata array!
|
|
|
|
transaction->DeleteObjectStore(objectStoreId);
|
|
|
|
|
|
|
|
specArray.RemoveElementAt(specIndex);
|
|
|
|
|
|
|
|
RefreshSpec(/* aMayDelete */ false);
|
|
|
|
break;
|
2013-07-31 19:48:46 +04:00
|
|
|
}
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (!objectStoreId) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR);
|
|
|
|
return;
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-10-16 08:56:52 +04:00
|
|
|
// Don't do this in the macro because we always need to increment the serial
|
|
|
|
// number to keep in sync with the parent.
|
|
|
|
const uint64_t requestSerialNumber = IDBRequest::NextSerialNumber();
|
|
|
|
|
|
|
|
IDB_LOG_MARK("IndexedDB %s: Child Transaction[%lld] Request[%llu]: "
|
|
|
|
"database(%s).transaction(%s).deleteObjectStore(\"%s\")",
|
|
|
|
"IndexedDB %s: C T[%lld] R[%llu]: "
|
|
|
|
"IDBDatabase.deleteObjectStore()",
|
|
|
|
IDB_LOG_ID_STRING(),
|
|
|
|
transaction->LoggingSerialNumber(),
|
|
|
|
requestSerialNumber,
|
|
|
|
IDB_LOG_STRINGIFY(this),
|
|
|
|
IDB_LOG_STRINGIFY(transaction),
|
|
|
|
NS_ConvertUTF16toUTF8(aName).get());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
already_AddRefed<IDBTransaction>
|
|
|
|
IDBDatabase::Transaction(const nsAString& aStoreName,
|
|
|
|
IDBTransactionMode aMode,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
Sequence<nsString> storeNames;
|
|
|
|
if (!storeNames.AppendElement(aStoreName)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Transaction(storeNames, aMode, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBTransaction>
|
2013-07-31 19:48:46 +04:00
|
|
|
IDBDatabase::Transaction(const Sequence<nsString>& aStoreNames,
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBTransactionMode aMode,
|
|
|
|
ErrorResult& aRv)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
if (QuotaManager::IsShuttingDown()) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
2010-10-19 21:58:42 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mClosed) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
|
|
|
|
return nullptr;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (RunningVersionChangeTransaction()) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
|
|
|
|
return nullptr;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
if (aStoreNames.IsEmpty()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
2011-11-03 05:03:15 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBTransaction::Mode mode;
|
2013-07-31 19:48:46 +04:00
|
|
|
switch (aMode) {
|
|
|
|
case IDBTransactionMode::Readonly:
|
2014-09-27 03:21:57 +04:00
|
|
|
mode = IDBTransaction::READ_ONLY;
|
2013-07-31 19:48:46 +04:00
|
|
|
break;
|
|
|
|
case IDBTransactionMode::Readwrite:
|
2014-09-27 03:21:57 +04:00
|
|
|
mode = IDBTransaction::READ_WRITE;
|
2013-07-31 19:48:46 +04:00
|
|
|
break;
|
|
|
|
case IDBTransactionMode::Versionchange:
|
2014-09-27 03:21:57 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
2013-07-31 19:48:46 +04:00
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown mode!");
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const nsTArray<ObjectStoreSpec>& objectStores = mSpec->objectStores();
|
|
|
|
const uint32_t nameCount = aStoreNames.Length();
|
|
|
|
|
|
|
|
nsTArray<nsString> sortedStoreNames;
|
|
|
|
sortedStoreNames.SetCapacity(nameCount);
|
|
|
|
|
|
|
|
// Check to make sure the object store names we collected actually exist.
|
|
|
|
for (uint32_t nameIndex = 0; nameIndex < nameCount; nameIndex++) {
|
|
|
|
const nsString& name = aStoreNames[nameIndex];
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (uint32_t objCount = objectStores.Length(), objIndex = 0;
|
|
|
|
objIndex < objCount;
|
|
|
|
objIndex++) {
|
|
|
|
if (objectStores[objIndex].metadata().name() == name) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR);
|
|
|
|
return nullptr;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
sortedStoreNames.InsertElementSorted(name);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// Remove any duplicates.
|
|
|
|
for (uint32_t nameIndex = nameCount - 1; nameIndex > 0; nameIndex--) {
|
|
|
|
if (sortedStoreNames[nameIndex] == sortedStoreNames[nameIndex - 1]) {
|
|
|
|
sortedStoreNames.RemoveElementAt(nameIndex);
|
|
|
|
}
|
2014-09-18 03:36:01 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<IDBTransaction> transaction =
|
|
|
|
IDBTransaction::Create(this, sortedStoreNames, mode);
|
|
|
|
MOZ_ASSERT(transaction);
|
|
|
|
|
|
|
|
BackgroundTransactionChild* actor =
|
|
|
|
new BackgroundTransactionChild(transaction);
|
|
|
|
|
2014-10-16 08:56:52 +04:00
|
|
|
IDB_LOG_MARK("IndexedDB %s: Child Transaction[%lld]: "
|
|
|
|
"database(%s).transaction(%s)",
|
|
|
|
"IndexedDB %s: C T[%lld]: IDBDatabase.transaction()",
|
|
|
|
IDB_LOG_ID_STRING(),
|
|
|
|
transaction->LoggingSerialNumber(),
|
|
|
|
IDB_LOG_STRINGIFY(this),
|
|
|
|
IDB_LOG_STRINGIFY(transaction));
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ALWAYS_TRUE(
|
|
|
|
mBackgroundActor->SendPBackgroundIDBTransactionConstructor(actor,
|
|
|
|
sortedStoreNames,
|
|
|
|
mode));
|
|
|
|
|
|
|
|
transaction->SetBackgroundActor(actor);
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
return transaction.forget();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
StorageType
|
|
|
|
IDBDatabase::Storage() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mSpec);
|
|
|
|
|
|
|
|
return PersistenceTypeToStorage(mSpec->metadata().persistenceType());
|
|
|
|
}
|
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
already_AddRefed<IDBRequest>
|
2014-06-12 07:35:29 +04:00
|
|
|
IDBDatabase::CreateMutableFile(const nsAString& aName,
|
|
|
|
const Optional<nsAString>& aType,
|
|
|
|
ErrorResult& aRv)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
if (!IndexedDatabaseManager::IsMainProcess() || !NS_IsMainThread()) {
|
|
|
|
IDB_WARNING("Not supported!");
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
2012-11-09 01:41:23 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
if (QuotaManager::IsShuttingDown()) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mClosed) {
|
2013-07-31 19:48:46 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
|
|
|
|
return nullptr;
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:10 +04:00
|
|
|
nsRefPtr<IDBRequest> request = IDBRequest::Create(this, nullptr);
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsString type;
|
|
|
|
if (aType.WasPassed()) {
|
|
|
|
type = aType.Value();
|
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-10-16 08:56:52 +04:00
|
|
|
mFactory->IncrementParentLoggingRequestSerialNumber();
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
aRv = CreateFileHelper::CreateAndDispatch(this, request, aName, type);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2013-07-31 19:48:46 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2013-07-31 19:48:46 +04:00
|
|
|
return request.forget();
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::RegisterTransaction(IDBTransaction* aTransaction)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aTransaction);
|
|
|
|
aTransaction->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mTransactions.Contains(aTransaction));
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mTransactions.PutEntry(aTransaction);
|
|
|
|
}
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::UnregisterTransaction(IDBTransaction* aTransaction)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aTransaction);
|
|
|
|
aTransaction->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mTransactions.Contains(aTransaction));
|
2013-07-31 19:48:46 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mTransactions.RemoveEntry(aTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-14 05:20:38 +03:00
|
|
|
IDBDatabase::AbortTransactions(bool aShouldWarn)
|
2014-09-27 03:21:57 +04:00
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS Helper MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void
|
2014-11-14 05:20:38 +03:00
|
|
|
AbortTransactions(nsTHashtable<nsPtrHashKey<IDBTransaction>>& aTable,
|
|
|
|
nsTArray<nsRefPtr<IDBTransaction>>& aAbortedTransactions)
|
2014-09-27 03:21:57 +04:00
|
|
|
{
|
|
|
|
const uint32_t count = aTable.Count();
|
|
|
|
if (!count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-14 05:20:38 +03:00
|
|
|
nsAutoTArray<nsRefPtr<IDBTransaction>, 20> transactions;
|
2014-09-27 03:21:57 +04:00
|
|
|
transactions.SetCapacity(count);
|
|
|
|
|
|
|
|
aTable.EnumerateEntries(Collect, &transactions);
|
|
|
|
|
|
|
|
MOZ_ASSERT(transactions.Length() == count);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2014-11-14 05:20:38 +03:00
|
|
|
nsRefPtr<IDBTransaction> transaction = Move(transactions[index]);
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(transaction);
|
|
|
|
|
|
|
|
transaction->Abort(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2014-11-14 05:20:38 +03:00
|
|
|
|
|
|
|
// We only care about warning for write transactions.
|
|
|
|
if (transaction->GetMode() != IDBTransaction::READ_ONLY) {
|
|
|
|
aAbortedTransactions.AppendElement(Move(transaction));
|
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static PLDHashOperator
|
|
|
|
Collect(nsPtrHashKey<IDBTransaction>* aTransaction, void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTransaction);
|
|
|
|
aTransaction->GetKey()->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto* array = static_cast<nsTArray<nsRefPtr<IDBTransaction>>*>(aClosure);
|
|
|
|
array->AppendElement(aTransaction->GetKey());
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-14 05:20:38 +03:00
|
|
|
nsAutoTArray<nsRefPtr<IDBTransaction>, 5> abortedTransactions;
|
|
|
|
Helper::AbortTransactions(mTransactions, abortedTransactions);
|
|
|
|
|
|
|
|
if (aShouldWarn && !abortedTransactions.IsEmpty()) {
|
|
|
|
static const char kWarningMessage[] = "IndexedDBTransactionAbortNavigation";
|
|
|
|
|
|
|
|
for (uint32_t count = abortedTransactions.Length(), index = 0;
|
|
|
|
index < count;
|
|
|
|
index++) {
|
|
|
|
nsRefPtr<IDBTransaction>& transaction = abortedTransactions[index];
|
|
|
|
MOZ_ASSERT(transaction);
|
|
|
|
|
|
|
|
nsString filename;
|
|
|
|
uint32_t lineNo;
|
|
|
|
transaction->GetCallerLocation(filename, &lineNo);
|
|
|
|
|
|
|
|
LogWarning(kWarningMessage, filename, lineNo);
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
PBackgroundIDBDatabaseFileChild*
|
2014-10-08 20:15:23 +04:00
|
|
|
IDBDatabase::GetOrCreateFileActorForBlob(File* aBlob)
|
2012-12-17 23:25:10 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aBlob);
|
|
|
|
MOZ_ASSERT(mBackgroundActor);
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
// We use the File's nsIWeakReference as the key to the table because
|
2014-09-27 03:21:57 +04:00
|
|
|
// a) it is unique per blob, b) it is reference-counted so that we can
|
2014-10-08 20:15:23 +04:00
|
|
|
// guarantee that it stays alive, and c) it doesn't hold the actual File
|
2014-09-27 03:21:57 +04:00
|
|
|
// alive.
|
2014-10-08 20:15:22 +04:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = aBlob;
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_GetWeakReference(blob);
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(weakRef);
|
|
|
|
|
|
|
|
PBackgroundIDBDatabaseFileChild* actor = nullptr;
|
|
|
|
|
|
|
|
if (!mFileActors.Get(weakRef, &actor)) {
|
2014-10-08 20:15:23 +04:00
|
|
|
FileImpl* blobImpl = aBlob->Impl();
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
if (mReceivedBlobs.GetEntry(weakRef)) {
|
|
|
|
// This blob was previously retrieved from the database.
|
|
|
|
nsCOMPtr<nsIRemoteBlob> remoteBlob = do_QueryObject(blobImpl);
|
|
|
|
MOZ_ASSERT(remoteBlob);
|
|
|
|
|
|
|
|
BlobChild* blobChild = remoteBlob->GetBlobChild();
|
|
|
|
MOZ_ASSERT(blobChild);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
PBackgroundChild* backgroundManager = blobChild->GetBackgroundManager();
|
|
|
|
MOZ_ASSERT(backgroundManager);
|
|
|
|
|
|
|
|
PBackgroundChild* thisManager = mBackgroundActor->Manager()->Manager();
|
|
|
|
MOZ_ASSERT(thisManager);
|
|
|
|
|
|
|
|
MOZ_ASSERT(thisManager == backgroundManager);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
auto* dbFile = new DatabaseFile(this);
|
|
|
|
|
|
|
|
actor =
|
|
|
|
mBackgroundActor->SendPBackgroundIDBDatabaseFileConstructor(dbFile,
|
|
|
|
blobChild);
|
|
|
|
if (NS_WARN_IF(!actor)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure that the input stream we get here is one that can actually be
|
|
|
|
// serialized to PBackground.
|
|
|
|
PBackgroundChild* backgroundManager =
|
|
|
|
mBackgroundActor->Manager()->Manager();
|
|
|
|
MOZ_ASSERT(backgroundManager);
|
|
|
|
|
|
|
|
auto* blobChild =
|
|
|
|
static_cast<BlobChild*>(
|
|
|
|
BackgroundChild::GetOrCreateActorForBlob(backgroundManager, aBlob));
|
|
|
|
MOZ_ASSERT(blobChild);
|
|
|
|
|
|
|
|
auto* dbFile = new DatabaseFile(this);
|
|
|
|
|
|
|
|
actor =
|
|
|
|
mBackgroundActor->SendPBackgroundIDBDatabaseFileConstructor(dbFile,
|
|
|
|
blobChild);
|
|
|
|
if (NS_WARN_IF(!actor)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(actor);
|
|
|
|
|
|
|
|
mFileActors.Put(weakRef, actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(actor);
|
|
|
|
|
|
|
|
return actor;
|
2012-12-17 23:25:10 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::NoteFinishedFileActor(PBackgroundIDBDatabaseFileChild* aFileActor)
|
2013-03-26 15:13:17 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aFileActor);
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS Helper MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PLDHashOperator
|
|
|
|
Remove(nsISupports* aKey,
|
|
|
|
PBackgroundIDBDatabaseFileChild*& aValue,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aKey);
|
|
|
|
MOZ_ASSERT(aValue);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
if (aValue == static_cast<PBackgroundIDBDatabaseFileChild*>(aClosure)) {
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mFileActors.Enumerate(&Helper::Remove, aFileActor);
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
2014-10-08 20:15:23 +04:00
|
|
|
IDBDatabase::NoteReceivedBlob(File* aBlob)
|
2013-03-26 15:13:17 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aBlob);
|
|
|
|
MOZ_ASSERT(mBackgroundActor);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2014-10-08 20:15:23 +04:00
|
|
|
nsRefPtr<FileImpl> blobImpl = aBlob->Impl();
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRemoteBlob> remoteBlob = do_QueryObject(blobImpl);
|
|
|
|
MOZ_ASSERT(remoteBlob);
|
|
|
|
|
|
|
|
BlobChild* blobChild = remoteBlob->GetBlobChild();
|
|
|
|
MOZ_ASSERT(blobChild);
|
|
|
|
|
|
|
|
PBackgroundChild* backgroundManager = blobChild->GetBackgroundManager();
|
|
|
|
MOZ_ASSERT(backgroundManager);
|
|
|
|
|
|
|
|
PBackgroundChild* thisManager = mBackgroundActor->Manager()->Manager();
|
|
|
|
MOZ_ASSERT(thisManager);
|
|
|
|
|
|
|
|
MOZ_ASSERT(thisManager == backgroundManager);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = aBlob;
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_GetWeakReference(blob);
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(weakRef);
|
|
|
|
|
|
|
|
// It's ok if this entry already exists in the table.
|
|
|
|
mReceivedBlobs.PutEntry(weakRef);
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::DelayedMaybeExpireFileActors()
|
2013-03-26 15:13:17 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (!mBackgroundActor || !mFileActors.Count()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethodWithArg<bool>(this,
|
|
|
|
&IDBDatabase::ExpireFileActors,
|
|
|
|
/* aExpireAll */ false);
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(runnable)));
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
nsresult
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::GetQuotaInfo(nsACString& aOrigin,
|
|
|
|
PersistenceType* aPersistenceType)
|
2010-11-11 02:26:03 +03:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
using mozilla::dom::quota::QuotaManager;
|
|
|
|
|
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (aPersistenceType) {
|
2014-11-28 11:44:12 +03:00
|
|
|
*aPersistenceType = mSpec->metadata().persistenceType();
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(*aPersistenceType != PERSISTENCE_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrincipalInfo* principalInfo = mFactory->GetPrincipalInfo();
|
|
|
|
MOZ_ASSERT(principalInfo);
|
|
|
|
|
|
|
|
switch (principalInfo->type()) {
|
|
|
|
case PrincipalInfo::TNullPrincipalInfo:
|
|
|
|
MOZ_CRASH("Is this needed?!");
|
|
|
|
|
|
|
|
case PrincipalInfo::TSystemPrincipalInfo:
|
|
|
|
QuotaManager::GetInfoForChrome(nullptr, &aOrigin, nullptr, nullptr);
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
case PrincipalInfo::TContentPrincipalInfo: {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
PrincipalInfoToPrincipal(*principalInfo, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = QuotaManager::GetInfoFromPrincipal(principal,
|
|
|
|
nullptr,
|
|
|
|
&aOrigin,
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown PrincipalInfo type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Should never get here!");
|
2010-11-11 02:26:03 +03:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::ExpireFileActors(bool aExpireAll)
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS Helper MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PLDHashOperator
|
|
|
|
MaybeExpireFileActors(nsISupports* aKey,
|
|
|
|
PBackgroundIDBDatabaseFileChild*& aValue,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aKey);
|
|
|
|
MOZ_ASSERT(aValue);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
const bool expiringAll = *static_cast<bool*>(aClosure);
|
|
|
|
|
|
|
|
bool shouldExpire = expiringAll;
|
|
|
|
if (!shouldExpire) {
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(aKey);
|
|
|
|
MOZ_ASSERT(weakRef);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> referent = do_QueryReferent(weakRef);
|
|
|
|
shouldExpire = !referent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldExpire) {
|
|
|
|
PBackgroundIDBDatabaseFileChild::Send__delete__(aValue);
|
|
|
|
|
|
|
|
if (!expiringAll) {
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
MaybeExpireReceivedBlobs(nsISupportsHashKey* aKey,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aKey);
|
|
|
|
MOZ_ASSERT(!aClosure);
|
|
|
|
|
|
|
|
nsISupports* key = aKey->GetKey();
|
|
|
|
MOZ_ASSERT(key);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(key);
|
|
|
|
MOZ_ASSERT(weakRef);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> referent = do_QueryReferent(weakRef);
|
|
|
|
if (!referent) {
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mBackgroundActor && mFileActors.Count()) {
|
|
|
|
mFileActors.Enumerate(&Helper::MaybeExpireFileActors, &aExpireAll);
|
|
|
|
if (aExpireAll) {
|
|
|
|
mFileActors.Clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!mFileActors.Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReceivedBlobs.Count()) {
|
|
|
|
if (aExpireAll) {
|
|
|
|
mReceivedBlobs.Clear();
|
|
|
|
} else {
|
|
|
|
mReceivedBlobs.EnumerateEntries(&Helper::MaybeExpireReceivedBlobs,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::NoteLiveMutableFile(IDBMutableFile* aMutableFile)
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aMutableFile);
|
|
|
|
MOZ_ASSERT(!mLiveMutableFiles.Contains(aMutableFile));
|
|
|
|
|
|
|
|
mLiveMutableFiles.AppendElement(aMutableFile);
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::NoteFinishedMutableFile(IDBMutableFile* aMutableFile)
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
// This should always happen in the main process but occasionally it is called
|
|
|
|
// after the IndexedDatabaseManager has already shut down.
|
|
|
|
// MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aMutableFile);
|
|
|
|
|
|
|
|
// It's ok if this is called more than once, so don't assert that aMutableFile
|
|
|
|
// is in the list already.
|
|
|
|
|
|
|
|
mLiveMutableFiles.RemoveElement(aMutableFile);
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-27 03:21:57 +04:00
|
|
|
IDBDatabase::InvalidateMutableFiles()
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mLiveMutableFiles.IsEmpty()) {
|
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
|
|
|
|
for (uint32_t count = mLiveMutableFiles.Length(), index = 0;
|
|
|
|
index < count;
|
|
|
|
index++) {
|
|
|
|
mLiveMutableFiles[index]->Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
mLiveMutableFiles.Clear();
|
|
|
|
}
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::Invalidate()
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertIsOnOwningThread();
|
2013-03-16 10:58:50 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (!mInvalidated) {
|
|
|
|
mInvalidated = true;
|
2013-03-16 10:58:50 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
InvalidateInternal();
|
2013-05-11 01:22:01 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
2014-11-14 05:20:38 +03:00
|
|
|
void
|
|
|
|
IDBDatabase::LogWarning(const char* aMessageName,
|
|
|
|
const nsAString& aFilename,
|
|
|
|
uint32_t aLineNumber)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aMessageName);
|
|
|
|
|
|
|
|
// For now this is main-thread only.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsXPIDLString localizedMessage;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
aMessageName,
|
|
|
|
localizedMessage)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString category;
|
|
|
|
if (mFactory->IsChrome()) {
|
|
|
|
category.AssignLiteral("chrome ");
|
|
|
|
} else {
|
|
|
|
category.AssignLiteral("content ");
|
|
|
|
}
|
|
|
|
category.AppendLiteral("javascript");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(consoleService);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
|
|
|
|
MOZ_ASSERT(consoleService);
|
|
|
|
|
|
|
|
if (mFactory->GetParentObject()) {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
scriptError->InitWithWindowID(localizedMessage,
|
|
|
|
aFilename,
|
|
|
|
/* aSourceLine */ EmptyString(),
|
|
|
|
aLineNumber,
|
|
|
|
/* aColumnNumber */ 0,
|
|
|
|
nsIScriptError::warningFlag,
|
|
|
|
category,
|
|
|
|
mFactory->InnerWindowID())));
|
|
|
|
} else {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
scriptError->Init(localizedMessage,
|
|
|
|
aFilename,
|
|
|
|
/* aSourceLine */ EmptyString(),
|
|
|
|
aLineNumber,
|
|
|
|
/* aColumnNumber */ 0,
|
|
|
|
nsIScriptError::warningFlag,
|
|
|
|
category.get())));
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(consoleService->LogMessage(scriptError)));
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBDatabase, IDBWrapperCache)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBDatabase, IDBWrapperCache)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBDatabase)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(IDBWrapperCache)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBDatabase)
|
2013-05-11 01:22:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBDatabase, IDBWrapperCache)
|
|
|
|
tmp->AssertIsOnOwningThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFactory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBDatabase, IDBWrapperCache)
|
|
|
|
tmp->AssertIsOnOwningThread();
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// Don't unlink mFactory!
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// We've been unlinked, at the very least we should be able to prevent further
|
|
|
|
// transactions from starting and unblock any other SetVersion callers.
|
|
|
|
tmp->CloseInternal();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
IDBDatabase::LastRelease()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
CloseInternal();
|
|
|
|
|
|
|
|
if (mBackgroundActor) {
|
|
|
|
mBackgroundActor->SendDeleteMeInternal();
|
|
|
|
MOZ_ASSERT(!mBackgroundActor, "SendDeleteMeInternal should have cleared!");
|
2011-12-20 14:59:07 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
2011-12-20 14:59:07 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsresult
|
|
|
|
IDBDatabase::PostHandleEvent(EventChainPostVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
return IndexedDatabaseManager::FireWindowOnError(GetOwner(), aVisitor);
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
JSObject*
|
|
|
|
IDBDatabase::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return IDBDatabaseBinding::Wrap(aCx, this);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
CreateFileHelper::CreateFileHelper(IDBDatabase* aDatabase,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
const nsACString& aOrigin)
|
|
|
|
: mDatabase(aDatabase)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
, mName(aName)
|
|
|
|
, mType(aType)
|
|
|
|
, mDatabaseName(aDatabase->Name())
|
|
|
|
, mOrigin(aOrigin)
|
|
|
|
, mPersistenceType(aDatabase->Spec()->metadata().persistenceType())
|
|
|
|
, mResultCode(NS_OK)
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aDatabase);
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
MOZ_ASSERT(mPersistenceType != PERSISTENCE_TYPE_INVALID);
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// static
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2014-09-27 03:21:57 +04:00
|
|
|
CreateFileHelper::CreateAndDispatch(IDBDatabase* aDatabase,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aType)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aDatabase);
|
|
|
|
aDatabase->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aDatabase->Factory());
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
MOZ_ASSERT(!QuotaManager::IsShuttingDown());
|
|
|
|
|
|
|
|
nsCString origin;
|
|
|
|
nsresult rv = aDatabase->GetQuotaInfo(origin, nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-03-16 10:58:50 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(!origin.IsEmpty());
|
2013-03-16 10:58:50 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<CreateFileHelper> helper =
|
|
|
|
new CreateFileHelper(aDatabase, aRequest, aName, aType, origin);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
MOZ_ASSERT(quotaManager);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> ioThread = quotaManager->IOThread();
|
|
|
|
MOZ_ASSERT(ioThread);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(ioThread->Dispatch(helper, NS_DISPATCH_NORMAL)))) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return 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
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
nsresult
|
2014-09-27 03:21:57 +04:00
|
|
|
CreateFileHelper::DoDatabaseWork()
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(!mFileInfo);
|
2013-03-16 10:58:50 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
PROFILER_LABEL("IndexedDB",
|
|
|
|
"CreateFileHelper::DoDatabaseWork",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 10:58:50 +04:00
|
|
|
|
2013-05-11 01:22:01 +04:00
|
|
|
if (IndexedDatabaseManager::InLowDiskSpaceMode()) {
|
|
|
|
NS_WARNING("Refusing to create file because disk space is low!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_QUOTA_ERR;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
IndexedDatabaseManager* idbManager = IndexedDatabaseManager::Get();
|
|
|
|
MOZ_ASSERT(idbManager);
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<FileManager> fileManager =
|
|
|
|
idbManager->GetFileManager(mPersistenceType, mOrigin, mDatabaseName);
|
|
|
|
MOZ_ASSERT(fileManager);
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<FileInfo> fileInfo = fileManager->GetNewFileInfo();
|
|
|
|
if (NS_WARN_IF(!fileInfo)) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2012-08-24 22:51:33 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const int64_t fileId = fileInfo->Id();
|
2012-08-24 22:51:33 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIFile> journalDirectory = fileManager->EnsureJournalDirectory();
|
|
|
|
if (NS_WARN_IF(!journalDirectory)) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2012-08-24 22:51:33 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIFile> journalFile =
|
|
|
|
fileManager->GetFileForId(journalDirectory, fileId);
|
|
|
|
if (NS_WARN_IF(!journalFile)) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2012-08-24 22:51:33 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsresult rv = journalFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIFile> fileDirectory = fileManager->GetDirectory();
|
|
|
|
if (NS_WARN_IF(!fileDirectory)) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = fileManager->GetFileForId(fileDirectory, fileId);
|
|
|
|
if (NS_WARN_IF(!file)) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2012-08-24 22:51:33 +04:00
|
|
|
rv = file->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
mFileInfo.swap(fileInfo);
|
2014-09-13 20:12:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
CreateFileHelper::DoMainThreadWork(nsresult aResultCode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mDatabase->IsInvalidated()) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
aResultCode = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDBMutableFile> mutableFile;
|
|
|
|
if (NS_SUCCEEDED(aResultCode)) {
|
|
|
|
mutableFile =
|
|
|
|
IDBMutableFile::Create(mDatabase, mName, mType, mFileInfo.forget());
|
|
|
|
MOZ_ASSERT(mutableFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchMutableFileResult(mRequest, aResultCode, mutableFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(CreateFileHelper, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CreateFileHelper::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
DoMainThreadWork(mResultCode);
|
|
|
|
|
|
|
|
mDatabase = nullptr;
|
|
|
|
mRequest = nullptr;
|
|
|
|
mFileInfo = nullptr;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mResultCode));
|
|
|
|
|
|
|
|
nsresult rv = DoDatabaseWork();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mResultCode = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(this)));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(IDBDatabase::Observer, nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBDatabase::
|
|
|
|
Observer::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aTopic);
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (!strcmp(aTopic, kWindowObserverTopic)) {
|
|
|
|
if (mWeakDatabase) {
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> supportsInt = do_QueryInterface(aSubject);
|
|
|
|
MOZ_ASSERT(supportsInt);
|
|
|
|
|
|
|
|
uint64_t windowId;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(supportsInt->GetData(&windowId)));
|
|
|
|
|
|
|
|
if (windowId == mWindowId) {
|
|
|
|
nsRefPtr<IDBDatabase> database = mWeakDatabase;
|
|
|
|
mWeakDatabase = nullptr;
|
|
|
|
|
|
|
|
database->InvalidateInternal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, kCycleCollectionObserverTopic) ||
|
|
|
|
!strcmp(aTopic, kMemoryPressureObserverTopic)) {
|
|
|
|
if (mWeakDatabase) {
|
|
|
|
nsRefPtr<IDBDatabase> database = mWeakDatabase;
|
|
|
|
|
|
|
|
database->ExpireFileActors(/* aExpireAll */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Unknown observer topic!");
|
|
|
|
return NS_OK;
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|