2010-09-10 23:12:11 +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-09-10 23:12:11 +04:00
|
|
|
|
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
|
2012-06-29 20:48:34 +04:00
|
|
|
#include "nsIConsoleService.h"
|
2013-05-11 01:22:01 +04:00
|
|
|
#include "nsIDiskSpaceWatcher.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "nsIFile.h"
|
2013-05-11 02:30:49 +04:00
|
|
|
#include "nsIObserverService.h"
|
2012-06-29 20:48:34 +04:00
|
|
|
#include "nsIScriptError.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2013-09-26 03:10:57 +04:00
|
|
|
#include "jsapi.h"
|
2013-03-26 15:13:17 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-06-05 12:11:23 +04:00
|
|
|
#include "mozilla/CondVar.h"
|
2013-09-25 15:21:20 +04:00
|
|
|
#include "mozilla/ContentEvents.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-10-08 20:15:22 +04:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
2014-02-23 09:01:12 +04:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/PBlobChild.h"
|
2013-09-11 08:18:36 +04:00
|
|
|
#include "mozilla/dom/quota/OriginOrPatternString.h"
|
2012-12-17 23:25:10 +04:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2013-03-26 15:13:17 +04:00
|
|
|
#include "mozilla/dom/quota/Utilities.h"
|
2012-11-10 22:32:37 +04:00
|
|
|
#include "mozilla/dom/TabContext.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2013-05-11 02:30:49 +04:00
|
|
|
#include "mozilla/Services.h"
|
2014-03-02 06:37:34 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-08-26 12:21:35 +04:00
|
|
|
#include "mozilla/storage.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
#include "IDBEvents.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "IDBFactory.h"
|
2012-01-03 19:27:39 +04:00
|
|
|
#include "IDBKeyRange.h"
|
2013-03-26 15:13:17 +04:00
|
|
|
#include "IDBRequest.h"
|
2012-06-07 21:44:17 +04:00
|
|
|
|
2013-09-26 03:10:57 +04:00
|
|
|
// Bindings for ResolveConstructors
|
|
|
|
#include "mozilla/dom/IDBCursorBinding.h"
|
|
|
|
#include "mozilla/dom/IDBDatabaseBinding.h"
|
|
|
|
#include "mozilla/dom/IDBFactoryBinding.h"
|
|
|
|
#include "mozilla/dom/IDBIndexBinding.h"
|
2014-06-12 07:35:29 +04:00
|
|
|
#include "mozilla/dom/IDBKeyRangeBinding.h"
|
|
|
|
#include "mozilla/dom/IDBMutableFileBinding.h"
|
2013-09-26 03:10:57 +04:00
|
|
|
#include "mozilla/dom/IDBObjectStoreBinding.h"
|
|
|
|
#include "mozilla/dom/IDBOpenDBRequestBinding.h"
|
|
|
|
#include "mozilla/dom/IDBRequestBinding.h"
|
|
|
|
#include "mozilla/dom/IDBTransactionBinding.h"
|
|
|
|
#include "mozilla/dom/IDBVersionChangeEventBinding.h"
|
|
|
|
|
2013-09-28 15:25:46 +04:00
|
|
|
#define IDB_STR "indexedDB"
|
|
|
|
|
2013-05-11 01:22:01 +04:00
|
|
|
// The two possible values for the data argument when receiving the disk space
|
|
|
|
// observer notification.
|
|
|
|
#define LOW_DISK_SPACE_DATA_FULL "full"
|
|
|
|
#define LOW_DISK_SPACE_DATA_FREE "free"
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
using namespace mozilla::dom::quota;
|
2013-09-11 08:18:36 +04:00
|
|
|
|
|
|
|
class FileManagerInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
already_AddRefed<FileManager>
|
|
|
|
GetFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsAString& aName) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
AddFileManager(FileManager* aFileManager);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasFileManagers() const
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
return !mPersistentStorageFileManagers.IsEmpty() ||
|
|
|
|
!mTemporaryStorageFileManagers.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateAllFileManagers() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateAndRemoveFileManagers(PersistenceType aPersistenceType);
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateAndRemoveFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsAString& aName);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<nsRefPtr<FileManager> >&
|
|
|
|
GetArray(PersistenceType aPersistenceType);
|
|
|
|
|
|
|
|
const nsTArray<nsRefPtr<FileManager> >&
|
|
|
|
GetImmutableArray(PersistenceType aPersistenceType) const
|
|
|
|
{
|
|
|
|
return const_cast<FileManagerInfo*>(this)->GetArray(aPersistenceType);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager> > mPersistentStorageFileManagers;
|
|
|
|
nsTArray<nsRefPtr<FileManager> > mTemporaryStorageFileManagers;
|
|
|
|
};
|
|
|
|
|
2014-09-18 03:36:01 +04:00
|
|
|
namespace {
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
const char kTestingPref[] = "dom.indexedDB.testing";
|
|
|
|
|
2013-12-09 09:31:48 +04:00
|
|
|
mozilla::StaticRefPtr<IndexedDatabaseManager> gDBManager;
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2014-02-07 10:17:07 +04:00
|
|
|
mozilla::Atomic<bool> gInitialized(false);
|
|
|
|
mozilla::Atomic<bool> gClosed(false);
|
2014-09-27 03:21:57 +04:00
|
|
|
mozilla::Atomic<bool> gTestingMode(false);
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
class AsyncDeleteFileRunnable MOZ_FINAL : public nsIRunnable
|
2012-10-23 20:31:19 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
AsyncDeleteFileRunnable(FileManager* aFileManager, int64_t aFileId);
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
private:
|
2014-06-23 23:56:07 +04:00
|
|
|
~AsyncDeleteFileRunnable() {}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
nsRefPtr<FileManager> mFileManager;
|
|
|
|
int64_t mFileId;
|
2012-10-23 20:31:19 +04:00
|
|
|
};
|
|
|
|
|
2013-06-05 12:11:23 +04:00
|
|
|
class GetFileReferencesHelper MOZ_FINAL : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-06-05 12:11:23 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
GetFileReferencesHelper(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
2013-06-05 12:11:23 +04:00
|
|
|
const nsAString& aDatabaseName,
|
|
|
|
int64_t aFileId)
|
2013-09-11 08:18:36 +04:00
|
|
|
: mPersistenceType(aPersistenceType),
|
|
|
|
mOrigin(aOrigin),
|
|
|
|
mDatabaseName(aDatabaseName),
|
|
|
|
mFileId(aFileId),
|
2013-06-05 12:11:23 +04:00
|
|
|
mMutex(IndexedDatabaseManager::FileMutex()),
|
|
|
|
mCondVar(mMutex, "GetFileReferencesHelper::mCondVar"),
|
|
|
|
mMemRefCnt(-1),
|
|
|
|
mDBRefCnt(-1),
|
|
|
|
mSliceRefCnt(-1),
|
|
|
|
mResult(false),
|
|
|
|
mWaiting(true)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchAndReturnFileReferences(int32_t* aMemRefCnt,
|
|
|
|
int32_t* aDBRefCnt,
|
|
|
|
int32_t* aSliceRefCnt,
|
|
|
|
bool* aResult);
|
|
|
|
|
|
|
|
private:
|
2014-06-23 23:56:07 +04:00
|
|
|
~GetFileReferencesHelper() {}
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
PersistenceType mPersistenceType;
|
2013-06-05 12:11:23 +04:00
|
|
|
nsCString mOrigin;
|
|
|
|
nsString mDatabaseName;
|
|
|
|
int64_t mFileId;
|
|
|
|
|
|
|
|
mozilla::Mutex& mMutex;
|
|
|
|
mozilla::CondVar mCondVar;
|
|
|
|
int32_t mMemRefCnt;
|
|
|
|
int32_t mDBRefCnt;
|
|
|
|
int32_t mSliceRefCnt;
|
|
|
|
bool mResult;
|
|
|
|
bool mWaiting;
|
|
|
|
};
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
struct MOZ_STACK_CLASS InvalidateInfo
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2013-09-11 08:18:36 +04:00
|
|
|
InvalidateInfo(PersistenceType aPersistenceType, const nsACString& aPattern)
|
|
|
|
: persistenceType(aPersistenceType), pattern(aPattern)
|
|
|
|
{ }
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
PersistenceType persistenceType;
|
|
|
|
const nsACString& pattern;
|
|
|
|
};
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
void
|
|
|
|
TestingPrefChangedCallback(const char* aPrefName, void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!strcmp(aPrefName, kTestingPref));
|
|
|
|
MOZ_ASSERT(!aClosure);
|
|
|
|
|
|
|
|
gTestingMode = Preferences::GetBool(aPrefName);
|
|
|
|
}
|
|
|
|
|
2010-09-10 23:12:11 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
IndexedDatabaseManager::IndexedDatabaseManager()
|
2012-12-19 21:45:57 +04:00
|
|
|
: mFileMutex("IndexedDatabaseManager.mFileMutex")
|
2010-09-10 23:12:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDatabaseManager::~IndexedDatabaseManager()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
bool IndexedDatabaseManager::sIsMainProcess = false;
|
2014-02-07 10:17:07 +04:00
|
|
|
mozilla::Atomic<bool> IndexedDatabaseManager::sLowDiskSpaceMode(false);
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
// static
|
2013-03-26 15:13:17 +04:00
|
|
|
IndexedDatabaseManager*
|
2010-10-19 21:58:33 +04:00
|
|
|
IndexedDatabaseManager::GetOrCreate()
|
2010-09-10 23:12:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
if (IsClosed()) {
|
|
|
|
NS_ERROR("Calling GetOrCreate() after shutdown!");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2013-12-09 09:31:48 +04:00
|
|
|
if (!gDBManager) {
|
2012-06-01 21:21:12 +04:00
|
|
|
sIsMainProcess = XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-08-26 12:21:35 +04:00
|
|
|
|
2014-03-02 06:37:34 +04:00
|
|
|
if (sIsMainProcess && Preferences::GetBool("disk_space_watcher.enabled", false)) {
|
2013-05-11 01:22:01 +04:00
|
|
|
// See if we're starting up in low disk space conditions.
|
|
|
|
nsCOMPtr<nsIDiskSpaceWatcher> watcher =
|
|
|
|
do_GetService(DISKSPACEWATCHER_CONTRACTID);
|
|
|
|
if (watcher) {
|
|
|
|
bool isDiskFull;
|
|
|
|
if (NS_SUCCEEDED(watcher->GetIsDiskFull(&isDiskFull))) {
|
2014-02-07 10:17:07 +04:00
|
|
|
sLowDiskSpaceMode = isDiskFull;
|
2013-05-11 01:22:01 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("GetIsDiskFull failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("No disk space watcher component available!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
nsRefPtr<IndexedDatabaseManager> instance(new IndexedDatabaseManager());
|
2010-10-19 21:58:33 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
nsresult rv = instance->Init();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2014-02-07 10:17:07 +04:00
|
|
|
if (gInitialized.exchange(true)) {
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_ERROR("Initialized more than once?!");
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
2011-08-26 12:21:35 +04:00
|
|
|
|
2013-12-09 09:31:48 +04:00
|
|
|
gDBManager = instance;
|
2013-03-26 15:13:17 +04:00
|
|
|
|
2013-12-09 09:31:48 +04:00
|
|
|
ClearOnShutdown(&gDBManager);
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2013-12-09 09:31:48 +04:00
|
|
|
return gDBManager;
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
// static
|
2010-09-10 23:12:11 +04:00
|
|
|
IndexedDatabaseManager*
|
2010-10-19 21:58:33 +04:00
|
|
|
IndexedDatabaseManager::Get()
|
2010-09-10 23:12:11 +04:00
|
|
|
{
|
2010-10-19 21:58:33 +04:00
|
|
|
// Does not return an owning reference.
|
2013-12-09 09:31:48 +04:00
|
|
|
return gDBManager;
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2012-05-01 03:52:27 +04:00
|
|
|
nsresult
|
2013-03-26 15:13:17 +04:00
|
|
|
IndexedDatabaseManager::Init()
|
2012-05-01 03:52:27 +04:00
|
|
|
{
|
2013-05-11 01:22:01 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-05-01 03:52:27 +04:00
|
|
|
|
2013-05-11 01:22:01 +04:00
|
|
|
// Make sure that the quota manager is up.
|
|
|
|
QuotaManager* qm = QuotaManager::GetOrCreate();
|
|
|
|
NS_ENSURE_STATE(qm);
|
|
|
|
|
|
|
|
// During Init() we can't yet call IsMainProcess(), just check sIsMainProcess
|
|
|
|
// directly.
|
|
|
|
if (sIsMainProcess) {
|
|
|
|
// Must initialize the storage service on the main thread.
|
|
|
|
nsCOMPtr<mozIStorageService> ss =
|
|
|
|
do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_STATE(ss);
|
|
|
|
|
2013-05-11 02:22:16 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2013-05-11 01:22:01 +04:00
|
|
|
NS_ENSURE_STATE(obs);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
obs->AddObserver(this, DISKSPACEWATCHER_OBSERVER_TOPIC, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-05-01 03:52:27 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
Preferences::RegisterCallbackAndCall(TestingPrefChangedCallback,
|
|
|
|
kTestingPref);
|
|
|
|
|
2012-05-01 03:52:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::Destroy()
|
2012-06-01 21:21:12 +04:00
|
|
|
{
|
2013-03-26 15:13:17 +04:00
|
|
|
// Setting the closed flag prevents the service from being recreated.
|
|
|
|
// Don't set it though if there's no real instance created.
|
2014-02-07 10:17:07 +04:00
|
|
|
if (gInitialized && gClosed.exchange(true)) {
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_ERROR("Shutdown more than once?!");
|
|
|
|
}
|
2012-06-01 21:21:12 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
Preferences::UnregisterCallback(TestingPrefChangedCallback, kTestingPref);
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
delete this;
|
2012-06-01 21:21:12 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 20:48:34 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::FireWindowOnError(nsPIDOMWindow* aOwner,
|
2014-03-18 08:48:20 +04:00
|
|
|
EventChainPostVisitor& aVisitor)
|
2012-06-29 20:48:34 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aVisitor.mDOMEvent, NS_ERROR_UNEXPECTED);
|
|
|
|
if (!aOwner) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString type;
|
|
|
|
nsresult rv = aVisitor.mDOMEvent->GetType(type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (nsDependentString(kErrorEventType) != type) {
|
2012-06-29 20:48:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-20 02:18:32 +04:00
|
|
|
nsCOMPtr<EventTarget> eventTarget =
|
|
|
|
aVisitor.mDOMEvent->InternalDOMEvent()->GetTarget();
|
2012-06-29 20:48:34 +04:00
|
|
|
|
2013-08-01 02:28:15 +04:00
|
|
|
IDBRequest* request = static_cast<IDBRequest*>(eventTarget.get());
|
2012-06-29 20:48:34 +04:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2013-05-18 21:52:06 +04:00
|
|
|
ErrorResult ret;
|
|
|
|
nsRefPtr<DOMError> error = request->GetError(ret);
|
|
|
|
if (ret.Failed()) {
|
|
|
|
return ret.ErrorCode();
|
|
|
|
}
|
2012-06-29 20:48:34 +04:00
|
|
|
|
|
|
|
nsString errorName;
|
|
|
|
if (error) {
|
2013-05-18 21:52:06 +04:00
|
|
|
error->GetName(errorName);
|
2012-06-29 20:48:34 +04:00
|
|
|
}
|
|
|
|
|
2014-03-20 21:48:43 +04:00
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
RootedDictionary<ErrorEventInit> init(cx);
|
2014-02-23 09:01:12 +04:00
|
|
|
request->FillScriptErrorEvent(init);
|
2012-09-10 03:29:12 +04:00
|
|
|
|
2014-02-23 09:01:12 +04:00
|
|
|
init.mMessage = errorName;
|
|
|
|
init.mCancelable = true;
|
|
|
|
init.mBubbles = true;
|
2012-06-29 20:48:34 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(aOwner));
|
|
|
|
NS_ASSERTION(sgo, "How can this happen?!");
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-02-23 09:01:12 +04:00
|
|
|
if (NS_FAILED(sgo->HandleScriptError(init, &status))) {
|
2012-06-29 20:48:34 +04:00
|
|
|
NS_WARNING("Failed to dispatch script error event");
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool preventDefaultCalled = status == nsEventStatus_eConsumeNoDefault;
|
|
|
|
if (preventDefaultCalled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log an error to the error console.
|
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-10 03:29:12 +04:00
|
|
|
if (NS_FAILED(scriptError->InitWithWindowID(errorName,
|
2014-02-23 09:01:12 +04:00
|
|
|
init.mFilename,
|
|
|
|
EmptyString(), init.mLineno,
|
2012-07-20 15:16:17 +04:00
|
|
|
0, 0,
|
2012-06-29 20:48:34 +04:00
|
|
|
"IndexedDB",
|
|
|
|
aOwner->WindowID()))) {
|
|
|
|
NS_WARNING("Failed to init script error!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return consoleService->LogMessage(scriptError);
|
|
|
|
}
|
|
|
|
|
2012-10-23 20:31:19 +04:00
|
|
|
// static
|
|
|
|
bool
|
2012-11-10 22:32:37 +04:00
|
|
|
IndexedDatabaseManager::TabContextMayAccessOrigin(const TabContext& aContext,
|
|
|
|
const nsACString& aOrigin)
|
2012-11-10 05:14:40 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aOrigin.IsEmpty(), "Empty origin!");
|
|
|
|
|
2012-11-10 22:32:37 +04:00
|
|
|
// If aContext is for a browser element, it's allowed only to access other
|
|
|
|
// browser elements. But if aContext is not for a browser element, it may
|
|
|
|
// access both browser and non-browser elements.
|
2012-11-10 05:14:40 +04:00
|
|
|
nsAutoCString pattern;
|
2013-03-26 15:13:17 +04:00
|
|
|
QuotaManager::GetOriginPatternStringMaybeIgnoreBrowser(
|
|
|
|
aContext.OwnOrContainingAppId(),
|
|
|
|
aContext.IsBrowserElement(),
|
|
|
|
pattern);
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2012-11-10 05:14:40 +04:00
|
|
|
return PatternMatchesOrigin(pattern, aOrigin);
|
2012-10-23 20:31:19 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 15:25:46 +04:00
|
|
|
// static
|
|
|
|
bool
|
2013-11-19 11:36:12 +04:00
|
|
|
IndexedDatabaseManager::DefineIndexedDB(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
2013-09-28 15:25:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-11-19 11:36:12 +04:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsCallerChrome(), "Only for chrome!");
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(aGlobal)->flags & JSCLASS_DOM_GLOBAL,
|
|
|
|
"Passed object is not a global object!");
|
2013-09-28 15:25:46 +04:00
|
|
|
|
2013-11-19 11:36:12 +04:00
|
|
|
if (!IDBCursorBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBCursorWithValueBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBDatabaseBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBFactoryBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBIndexBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBKeyRangeBinding::GetConstructorObject(aCx, aGlobal) ||
|
2014-06-12 07:35:29 +04:00
|
|
|
!IDBMutableFileBinding::GetConstructorObject(aCx, aGlobal) ||
|
2013-11-19 11:36:12 +04:00
|
|
|
!IDBObjectStoreBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBOpenDBRequestBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBRequestBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBTransactionBinding::GetConstructorObject(aCx, aGlobal) ||
|
2013-11-19 11:36:33 +04:00
|
|
|
!IDBVersionChangeEventBinding::GetConstructorObject(aCx, aGlobal))
|
|
|
|
{
|
2013-11-19 11:36:12 +04:00
|
|
|
return false;
|
2013-09-28 15:25:46 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 11:36:12 +04:00
|
|
|
nsRefPtr<IDBFactory> factory;
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_FAILED(IDBFactory::CreateForChromeJS(aCx,
|
|
|
|
aGlobal,
|
|
|
|
getter_AddRefs(factory)))) {
|
2013-11-19 11:36:12 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-28 15:25:46 +04:00
|
|
|
|
2013-11-19 11:36:12 +04:00
|
|
|
MOZ_ASSERT(factory, "This should never fail for chrome!");
|
2013-09-28 15:25:46 +04:00
|
|
|
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JS::Value> indexedDB(aCx);
|
2014-04-09 02:27:19 +04:00
|
|
|
js::AssertSameCompartment(aCx, aGlobal);
|
|
|
|
if (!WrapNewBindingObject(aCx, factory, &indexedDB)) {
|
2013-09-28 15:25:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-22 06:11:17 +04:00
|
|
|
return JS_DefineProperty(aCx, aGlobal, IDB_STR, indexedDB, JSPROP_ENUMERATE);
|
2013-09-28 15:25:46 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
// static
|
2010-09-10 23:12:11 +04:00
|
|
|
bool
|
2013-03-26 15:13:17 +04:00
|
|
|
IndexedDatabaseManager::IsClosed()
|
2010-09-10 23:12:11 +04:00
|
|
|
{
|
2014-02-07 10:17:07 +04:00
|
|
|
return gClosed;
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
#ifdef DEBUG
|
2013-05-11 01:22:01 +04:00
|
|
|
// static
|
2013-03-26 15:13:17 +04:00
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::IsMainProcess()
|
|
|
|
{
|
2013-12-09 09:31:48 +04:00
|
|
|
NS_ASSERTION(gDBManager,
|
2013-03-26 15:13:17 +04:00
|
|
|
"IsMainProcess() called before indexedDB has been initialized!");
|
|
|
|
NS_ASSERTION((XRE_GetProcessType() == GeckoProcessType_Default) ==
|
|
|
|
sIsMainProcess, "XRE_GetProcessType changed its tune!");
|
|
|
|
return sIsMainProcess;
|
|
|
|
}
|
2013-05-11 01:22:01 +04:00
|
|
|
|
|
|
|
//static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::InLowDiskSpaceMode()
|
|
|
|
{
|
2013-12-09 09:31:48 +04:00
|
|
|
NS_ASSERTION(gDBManager,
|
2013-05-11 01:22:01 +04:00
|
|
|
"InLowDiskSpaceMode() called before indexedDB has been "
|
|
|
|
"initialized!");
|
2014-02-07 10:17:07 +04:00
|
|
|
return sLowDiskSpaceMode;
|
2013-05-11 01:22:01 +04:00
|
|
|
}
|
2013-03-26 15:13:17 +04:00
|
|
|
#endif
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::InTestingMode()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gDBManager,
|
|
|
|
"InTestingMode() called before indexedDB has been initialized!");
|
|
|
|
|
|
|
|
return gTestingMode;
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
already_AddRefed<FileManager>
|
2013-09-11 08:18:36 +04:00
|
|
|
IndexedDatabaseManager::GetFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
2013-03-26 15:13:17 +04:00
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aOrigin, &info)) {
|
2013-03-26 15:13:17 +04:00
|
|
|
return nullptr;
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
2010-10-19 21:58:42 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
nsRefPtr<FileManager> fileManager =
|
|
|
|
info->GetFileManager(aPersistenceType, aDatabaseName);
|
2013-03-26 15:13:17 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
return fileManager.forget();
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
void
|
2013-03-26 15:13:17 +04:00
|
|
|
IndexedDatabaseManager::AddFileManager(FileManager* aFileManager)
|
2010-10-19 21:58:33 +04:00
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_ASSERTION(aFileManager, "Null file manager!");
|
2010-10-19 21:58:33 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aFileManager->Origin(), &info)) {
|
|
|
|
info = new FileManagerInfo();
|
|
|
|
mFileManagerInfos.Put(aFileManager->Origin(), info);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->AddFileManager(aFileManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
PLDHashOperator
|
|
|
|
IndexedDatabaseManager::InvalidateAndRemoveFileManagers(
|
|
|
|
const nsACString& aKey,
|
|
|
|
nsAutoPtr<FileManagerInfo>& aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
NS_ASSERTION(!aKey.IsEmpty(), "Empty key!");
|
|
|
|
NS_ASSERTION(aValue, "Null pointer!");
|
|
|
|
|
|
|
|
if (!aUserArg) {
|
|
|
|
aValue->InvalidateAllFileManagers();
|
|
|
|
return PL_DHASH_REMOVE;
|
2010-10-19 21:58:33 +04:00
|
|
|
}
|
2013-03-26 15:13:17 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
InvalidateInfo* info = static_cast<InvalidateInfo*>(aUserArg);
|
|
|
|
|
|
|
|
if (PatternMatchesOrigin(info->pattern, aKey)) {
|
|
|
|
aValue->InvalidateAndRemoveFileManagers(info->persistenceType);
|
|
|
|
|
|
|
|
if (!aValue->HasFileManagers()) {
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
2010-10-19 21:58:33 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::InvalidateAllFileManagers()
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
mFileManagerInfos.Enumerate(InvalidateAndRemoveFileManagers, nullptr);
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 16:53:12 +04:00
|
|
|
void
|
2013-09-11 08:18:36 +04:00
|
|
|
IndexedDatabaseManager::InvalidateFileManagers(
|
|
|
|
PersistenceType aPersistenceType,
|
|
|
|
const OriginOrPatternString& aOriginOrPattern)
|
2010-09-10 23:12:11 +04:00
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
2013-09-11 08:18:36 +04:00
|
|
|
NS_ASSERTION(!aOriginOrPattern.IsEmpty(), "Empty pattern!");
|
2013-06-05 12:11:23 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
if (aOriginOrPattern.IsOrigin()) {
|
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aOriginOrPattern, &info)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->InvalidateAndRemoveFileManagers(aPersistenceType);
|
|
|
|
|
|
|
|
if (!info->HasFileManagers()) {
|
|
|
|
mFileManagerInfos.Remove(aOriginOrPattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
InvalidateInfo info(aPersistenceType, aOriginOrPattern);
|
|
|
|
mFileManagerInfos.Enumerate(InvalidateAndRemoveFileManagers, &info);
|
|
|
|
}
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
void
|
2013-09-11 08:18:36 +04:00
|
|
|
IndexedDatabaseManager::InvalidateFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
2013-03-26 15:13:17 +04:00
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aOrigin, &info)) {
|
2013-03-26 15:13:17 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
info->InvalidateAndRemoveFileManager(aPersistenceType, aDatabaseName);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
if (!info->HasFileManagers()) {
|
|
|
|
mFileManagerInfos.Remove(aOrigin);
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
2010-10-19 21:58:42 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsresult
|
2013-03-26 15:13:17 +04:00
|
|
|
IndexedDatabaseManager::AsyncDeleteFile(FileManager* aFileManager,
|
|
|
|
int64_t aFileId)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-20 05:50:39 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aFileManager);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
2012-06-20 05:50:39 +04:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
// See if we're currently clearing the storages for this origin. If so then
|
|
|
|
// we pretend that we've already deleted everything.
|
2014-04-15 00:48:49 +04:00
|
|
|
if (quotaManager->IsClearOriginPending(
|
|
|
|
aFileManager->Origin(),
|
|
|
|
Nullable<PersistenceType>(aFileManager->Type()))) {
|
2013-03-26 15:13:17 +04:00
|
|
|
return NS_OK;
|
2012-06-20 05:50:39 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
nsRefPtr<AsyncDeleteFileRunnable> runnable =
|
|
|
|
new AsyncDeleteFileRunnable(aFileManager, aFileId);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
quotaManager->IOThread()->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
2012-06-20 05:50:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-05 12:11:23 +04:00
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::BlockAndGetFileReferences(
|
2013-09-11 08:18:36 +04:00
|
|
|
PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName,
|
|
|
|
int64_t aFileId,
|
|
|
|
int32_t* aRefCnt,
|
|
|
|
int32_t* aDBRefCnt,
|
|
|
|
int32_t* aSliceRefCnt,
|
|
|
|
bool* aResult)
|
2013-06-05 12:11:23 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_WARN_IF(!InTestingMode())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2013-06-05 12:11:23 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (IsMainProcess()) {
|
|
|
|
nsRefPtr<GetFileReferencesHelper> helper =
|
|
|
|
new GetFileReferencesHelper(aPersistenceType, aOrigin, aDatabaseName,
|
|
|
|
aFileId);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
helper->DispatchAndReturnFileReferences(aRefCnt, aDBRefCnt,
|
|
|
|
aSliceRefCnt, aResult);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ContentChild* contentChild = ContentChild::GetSingleton();
|
|
|
|
if (NS_WARN_IF(!contentChild)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!contentChild->SendGetFileReferences(aPersistenceType,
|
|
|
|
nsCString(aOrigin),
|
|
|
|
nsString(aDatabaseName),
|
|
|
|
aFileId,
|
|
|
|
aRefCnt,
|
|
|
|
aDBRefCnt,
|
|
|
|
aSliceRefCnt,
|
|
|
|
aResult)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 12:11:23 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_IMPL_ADDREF(IndexedDatabaseManager)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(IndexedDatabaseManager, Destroy())
|
2014-06-17 23:01:47 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(IndexedDatabaseManager, nsIObserver)
|
2012-01-03 19:27:39 +04:00
|
|
|
|
2013-05-11 01:22:01 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2013-05-11 01:22:01 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsMainProcess(), "Wrong process!");
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-05-11 03:01:55 +04:00
|
|
|
if (!strcmp(aTopic, DISKSPACEWATCHER_OBSERVER_TOPIC)) {
|
2013-05-11 01:22:01 +04:00
|
|
|
NS_ASSERTION(aData, "No data?!");
|
|
|
|
|
|
|
|
const nsDependentString data(aData);
|
|
|
|
|
|
|
|
if (data.EqualsLiteral(LOW_DISK_SPACE_DATA_FULL)) {
|
2014-02-07 10:17:07 +04:00
|
|
|
sLowDiskSpaceMode = true;
|
2013-05-11 01:22:01 +04:00
|
|
|
}
|
|
|
|
else if (data.EqualsLiteral(LOW_DISK_SPACE_DATA_FREE)) {
|
2014-02-07 10:17:07 +04:00
|
|
|
sLowDiskSpaceMode = false;
|
2013-05-11 01:22:01 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown data value!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Unknown topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
already_AddRefed<FileManager>
|
|
|
|
FileManagerInfo::GetFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsAString& aName) const
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
const nsTArray<nsRefPtr<FileManager> >& managers =
|
|
|
|
GetImmutableArray(aPersistenceType);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < managers.Length(); i++) {
|
|
|
|
const nsRefPtr<FileManager>& fileManager = managers[i];
|
|
|
|
|
|
|
|
if (fileManager->DatabaseName() == aName) {
|
|
|
|
nsRefPtr<FileManager> result = fileManager;
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileManagerInfo::AddFileManager(FileManager* aFileManager)
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager> >& managers = GetArray(aFileManager->Type());
|
|
|
|
|
|
|
|
NS_ASSERTION(!managers.Contains(aFileManager), "Adding more than once?!");
|
|
|
|
|
|
|
|
managers.AppendElement(aFileManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileManagerInfo::InvalidateAllFileManagers() const
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < mPersistentStorageFileManagers.Length(); i++) {
|
|
|
|
mPersistentStorageFileManagers[i]->Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < mTemporaryStorageFileManagers.Length(); i++) {
|
|
|
|
mTemporaryStorageFileManagers[i]->Invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileManagerInfo::InvalidateAndRemoveFileManagers(
|
|
|
|
PersistenceType aPersistenceType)
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager > >& managers = GetArray(aPersistenceType);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < managers.Length(); i++) {
|
|
|
|
managers[i]->Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
managers.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileManagerInfo::InvalidateAndRemoveFileManager(
|
|
|
|
PersistenceType aPersistenceType,
|
|
|
|
const nsAString& aName)
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager > >& managers = GetArray(aPersistenceType);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < managers.Length(); i++) {
|
|
|
|
nsRefPtr<FileManager>& fileManager = managers[i];
|
|
|
|
if (fileManager->DatabaseName() == aName) {
|
|
|
|
fileManager->Invalidate();
|
|
|
|
managers.RemoveElementAt(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager> >&
|
|
|
|
FileManagerInfo::GetArray(PersistenceType aPersistenceType)
|
|
|
|
{
|
|
|
|
switch (aPersistenceType) {
|
|
|
|
case PERSISTENCE_TYPE_PERSISTENT:
|
|
|
|
return mPersistentStorageFileManagers;
|
|
|
|
case PERSISTENCE_TYPE_TEMPORARY:
|
|
|
|
return mTemporaryStorageFileManagers;
|
|
|
|
|
|
|
|
case PERSISTENCE_TYPE_INVALID:
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Bad storage type value!");
|
|
|
|
return mPersistentStorageFileManagers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 22:51:33 +04:00
|
|
|
AsyncDeleteFileRunnable::AsyncDeleteFileRunnable(FileManager* aFileManager,
|
|
|
|
int64_t aFileId)
|
|
|
|
: mFileManager(aFileManager), mFileId(aFileId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(AsyncDeleteFileRunnable,
|
|
|
|
nsIRunnable)
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-26 15:13:17 +04:00
|
|
|
AsyncDeleteFileRunnable::Run()
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2013-06-05 12:11:23 +04:00
|
|
|
AssertIsOnIOThread();
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2012-08-24 22:51:33 +04:00
|
|
|
nsCOMPtr<nsIFile> directory = mFileManager->GetDirectory();
|
|
|
|
NS_ENSURE_TRUE(directory, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = mFileManager->GetFileForId(directory, mFileId);
|
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
|
|
|
|
|
2012-12-17 23:25:10 +04:00
|
|
|
nsresult rv;
|
|
|
|
int64_t fileSize;
|
2012-08-24 22:51:33 +04:00
|
|
|
|
2012-12-17 23:25:10 +04:00
|
|
|
if (mFileManager->Privilege() != Chrome) {
|
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-12-17 23:25:10 +04:00
|
|
|
rv = file->Remove(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2012-06-12 01:59:27 +04:00
|
|
|
|
2012-12-17 23:25:10 +04:00
|
|
|
if (mFileManager->Privilege() != Chrome) {
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
quotaManager->DecreaseUsageForOrigin(mFileManager->Type(),
|
|
|
|
mFileManager->Group(),
|
|
|
|
mFileManager->Origin(), fileSize);
|
2012-06-12 01:59:27 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 22:51:33 +04:00
|
|
|
directory = mFileManager->GetJournalDirectory();
|
|
|
|
NS_ENSURE_TRUE(directory, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
file = mFileManager->GetFileForId(directory, mFileId);
|
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
rv = file->Remove(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-05 12:11:23 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetFileReferencesHelper::DispatchAndReturnFileReferences(int32_t* aMemRefCnt,
|
|
|
|
int32_t* aDBRefCnt,
|
|
|
|
int32_t* aSliceRefCnt,
|
|
|
|
bool* aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
quotaManager->IOThread()->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock autolock(mMutex);
|
|
|
|
while (mWaiting) {
|
|
|
|
mCondVar.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
*aMemRefCnt = mMemRefCnt;
|
|
|
|
*aDBRefCnt = mDBRefCnt;
|
|
|
|
*aSliceRefCnt = mSliceRefCnt;
|
|
|
|
*aResult = mResult;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(GetFileReferencesHelper,
|
|
|
|
nsIRunnable)
|
2013-06-05 12:11:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
GetFileReferencesHelper::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never fail!");
|
|
|
|
|
|
|
|
nsRefPtr<FileManager> fileManager =
|
2013-09-11 08:18:36 +04:00
|
|
|
mgr->GetFileManager(mPersistenceType, mOrigin, mDatabaseName);
|
2013-06-05 12:11:23 +04:00
|
|
|
|
|
|
|
if (fileManager) {
|
|
|
|
nsRefPtr<FileInfo> fileInfo = fileManager->GetFileInfo(mFileId);
|
|
|
|
|
|
|
|
if (fileInfo) {
|
|
|
|
fileInfo->GetReferences(&mMemRefCnt, &mDBRefCnt, &mSliceRefCnt);
|
|
|
|
|
|
|
|
if (mMemRefCnt != -1) {
|
|
|
|
// We added an extra temp ref, so account for that accordingly.
|
|
|
|
mMemRefCnt--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mResult = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mMutex);
|
|
|
|
NS_ASSERTION(mWaiting, "Huh?!");
|
|
|
|
|
|
|
|
mWaiting = false;
|
|
|
|
mCondVar.Notify();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|