2010-09-10 19:12:11 +00: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 11:12:37 +00: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 19:12:11 +00:00
|
|
|
|
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
|
2012-06-29 16:48:34 +00:00
|
|
|
#include "nsIConsoleService.h"
|
2013-05-10 21:22:01 +00:00
|
|
|
#include "nsIDiskSpaceWatcher.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "nsIFile.h"
|
2012-06-03 16:33:52 +00:00
|
|
|
#include "nsIFileStorage.h"
|
2013-05-10 22:30:49 +00:00
|
|
|
#include "nsIObserverService.h"
|
2012-06-29 16:48:34 +00:00
|
|
|
#include "nsIScriptError.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-09-25 23:10:57 +00:00
|
|
|
#include "jsapi.h"
|
2013-03-26 11:13:17 +00:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-06-05 08:11:23 +00:00
|
|
|
#include "mozilla/CondVar.h"
|
2013-09-25 11:21:20 +00:00
|
|
|
#include "mozilla/ContentEvents.h"
|
2014-02-23 05:01:12 +00:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2013-09-11 04:18:36 +00:00
|
|
|
#include "mozilla/dom/quota/OriginOrPatternString.h"
|
2012-12-17 19:25:10 +00:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2013-03-26 11:13:17 +00:00
|
|
|
#include "mozilla/dom/quota/Utilities.h"
|
2012-11-10 18:32:37 +00:00
|
|
|
#include "mozilla/dom/TabContext.h"
|
2014-03-18 04:48:21 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-05-10 22:30:49 +00:00
|
|
|
#include "mozilla/Services.h"
|
2014-03-02 02:37:34 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-08-26 08:21:35 +00:00
|
|
|
#include "mozilla/storage.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
#include "IDBEvents.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "IDBFactory.h"
|
2012-01-03 15:27:39 +00:00
|
|
|
#include "IDBKeyRange.h"
|
2013-03-26 11:13:17 +00:00
|
|
|
#include "IDBRequest.h"
|
2012-06-07 17:44:17 +00:00
|
|
|
|
2013-09-25 23:10:57 +00:00
|
|
|
// Bindings for ResolveConstructors
|
|
|
|
#include "mozilla/dom/IDBCursorBinding.h"
|
|
|
|
#include "mozilla/dom/IDBDatabaseBinding.h"
|
|
|
|
#include "mozilla/dom/IDBFactoryBinding.h"
|
2013-09-28 11:25:46 +00:00
|
|
|
#include "mozilla/dom/IDBFileHandleBinding.h"
|
|
|
|
#include "mozilla/dom/IDBKeyRangeBinding.h"
|
2013-09-25 23:10:57 +00:00
|
|
|
#include "mozilla/dom/IDBIndexBinding.h"
|
|
|
|
#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 11:25:46 +00:00
|
|
|
#define IDB_STR "indexedDB"
|
|
|
|
|
2013-05-10 21:22:01 +00: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"
|
|
|
|
|
2010-09-10 19:12:11 +00:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2014-03-18 04:48:20 +00:00
|
|
|
using namespace mozilla;
|
2012-11-10 18:32:37 +00:00
|
|
|
using namespace mozilla::dom;
|
2013-03-26 11:13:17 +00:00
|
|
|
USING_QUOTA_NAMESPACE
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
2010-09-10 19:12:11 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-12-09 05:31:48 +00:00
|
|
|
mozilla::StaticRefPtr<IndexedDatabaseManager> gDBManager;
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2014-02-07 06:17:07 +00:00
|
|
|
mozilla::Atomic<bool> gInitialized(false);
|
|
|
|
mozilla::Atomic<bool> gClosed(false);
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
class AsyncDeleteFileRunnable MOZ_FINAL : public nsIRunnable
|
2012-10-23 16:31:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
AsyncDeleteFileRunnable(FileManager* aFileManager, int64_t aFileId);
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
private:
|
|
|
|
nsRefPtr<FileManager> mFileManager;
|
|
|
|
int64_t mFileId;
|
2012-10-23 16:31:19 +00:00
|
|
|
};
|
|
|
|
|
2013-06-05 08:11:23 +00:00
|
|
|
class GetFileReferencesHelper MOZ_FINAL : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-06-05 08:11:23 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
GetFileReferencesHelper(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
2013-06-05 08:11:23 +00:00
|
|
|
const nsAString& aDatabaseName,
|
|
|
|
int64_t aFileId)
|
2013-09-11 04:18:36 +00:00
|
|
|
: mPersistenceType(aPersistenceType),
|
|
|
|
mOrigin(aOrigin),
|
|
|
|
mDatabaseName(aDatabaseName),
|
|
|
|
mFileId(aFileId),
|
2013-06-05 08:11:23 +00: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:
|
2013-09-11 04:18:36 +00:00
|
|
|
PersistenceType mPersistenceType;
|
2013-06-05 08:11:23 +00: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 04:18:36 +00:00
|
|
|
struct MOZ_STACK_CLASS InvalidateInfo
|
2011-12-16 07:34:24 +00:00
|
|
|
{
|
2013-09-11 04:18:36 +00:00
|
|
|
InvalidateInfo(PersistenceType aPersistenceType, const nsACString& aPattern)
|
|
|
|
: persistenceType(aPersistenceType), pattern(aPattern)
|
|
|
|
{ }
|
2011-12-16 07:34:24 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
PersistenceType persistenceType;
|
|
|
|
const nsACString& pattern;
|
|
|
|
};
|
2011-12-16 07:34:24 +00:00
|
|
|
|
2010-09-10 19:12:11 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
IndexedDatabaseManager::IndexedDatabaseManager()
|
2012-12-19 17:45:57 +00:00
|
|
|
: mFileMutex("IndexedDatabaseManager.mFileMutex")
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDatabaseManager::~IndexedDatabaseManager()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
bool IndexedDatabaseManager::sIsMainProcess = false;
|
2014-02-07 06:17:07 +00:00
|
|
|
mozilla::Atomic<bool> IndexedDatabaseManager::sLowDiskSpaceMode(false);
|
2012-06-01 17:21:12 +00:00
|
|
|
|
2010-10-19 17:58:33 +00:00
|
|
|
// static
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager*
|
2010-10-19 17:58:33 +00:00
|
|
|
IndexedDatabaseManager::GetOrCreate()
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
if (IsClosed()) {
|
|
|
|
NS_ERROR("Calling GetOrCreate() after shutdown!");
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 05:31:48 +00:00
|
|
|
if (!gDBManager) {
|
2012-06-01 17:21:12 +00:00
|
|
|
sIsMainProcess = XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-08-26 08:21:35 +00:00
|
|
|
|
2014-03-02 02:37:34 +00:00
|
|
|
if (sIsMainProcess && Preferences::GetBool("disk_space_watcher.enabled", false)) {
|
2013-05-10 21:22:01 +00: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 06:17:07 +00:00
|
|
|
sLowDiskSpaceMode = isDiskFull;
|
2013-05-10 21:22:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("GetIsDiskFull failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("No disk space watcher component available!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
nsRefPtr<IndexedDatabaseManager> instance(new IndexedDatabaseManager());
|
2010-10-19 17:58:33 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
nsresult rv = instance->Init();
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2014-02-07 06:17:07 +00:00
|
|
|
if (gInitialized.exchange(true)) {
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_ERROR("Initialized more than once?!");
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
2011-08-26 08:21:35 +00:00
|
|
|
|
2013-12-09 05:31:48 +00:00
|
|
|
gDBManager = instance;
|
2013-03-26 11:13:17 +00:00
|
|
|
|
2013-12-09 05:31:48 +00:00
|
|
|
ClearOnShutdown(&gDBManager);
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 05:31:48 +00:00
|
|
|
return gDBManager;
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:33 +00:00
|
|
|
// static
|
2010-09-10 19:12:11 +00:00
|
|
|
IndexedDatabaseManager*
|
2010-10-19 17:58:33 +00:00
|
|
|
IndexedDatabaseManager::Get()
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2010-10-19 17:58:33 +00:00
|
|
|
// Does not return an owning reference.
|
2013-12-09 05:31:48 +00:00
|
|
|
return gDBManager;
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:33 +00:00
|
|
|
// static
|
|
|
|
IndexedDatabaseManager*
|
|
|
|
IndexedDatabaseManager::FactoryCreate()
|
|
|
|
{
|
|
|
|
// Returns a raw pointer that carries an owning reference! Lame, but the
|
|
|
|
// singleton factory macros force this.
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager* mgr = GetOrCreate();
|
|
|
|
NS_IF_ADDREF(mgr);
|
|
|
|
return mgr;
|
2010-10-19 17:58:33 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 23:52:27 +00:00
|
|
|
nsresult
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager::Init()
|
2012-04-30 23:52:27 +00:00
|
|
|
{
|
2013-05-10 21:22:01 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-04-30 23:52:27 +00:00
|
|
|
|
2013-05-10 21:22:01 +00: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-10 22:22:16 +00:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2013-05-10 21:22:01 +00:00
|
|
|
NS_ENSURE_STATE(obs);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
obs->AddObserver(this, DISKSPACEWATCHER_OBSERVER_TOPIC, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-04-30 23:52:27 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::Destroy()
|
2012-06-01 17:21:12 +00:00
|
|
|
{
|
2013-03-26 11:13:17 +00: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 06:17:07 +00:00
|
|
|
if (gInitialized && gClosed.exchange(true)) {
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_ERROR("Shutdown more than once?!");
|
|
|
|
}
|
2012-06-01 17:21:12 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
delete this;
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 16:48:34 +00:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::FireWindowOnError(nsPIDOMWindow* aOwner,
|
2014-03-18 04:48:20 +00:00
|
|
|
EventChainPostVisitor& aVisitor)
|
2012-06-29 16:48:34 +00: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);
|
|
|
|
|
|
|
|
if (!type.EqualsLiteral(ERROR_EVT_STR)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-19 22:18:32 +00:00
|
|
|
nsCOMPtr<EventTarget> eventTarget =
|
|
|
|
aVisitor.mDOMEvent->InternalDOMEvent()->GetTarget();
|
2012-06-29 16:48:34 +00:00
|
|
|
|
2013-07-31 22:28:15 +00:00
|
|
|
IDBRequest* request = static_cast<IDBRequest*>(eventTarget.get());
|
2012-06-29 16:48:34 +00:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2013-05-18 17:52:06 +00:00
|
|
|
ErrorResult ret;
|
|
|
|
nsRefPtr<DOMError> error = request->GetError(ret);
|
|
|
|
if (ret.Failed()) {
|
|
|
|
return ret.ErrorCode();
|
|
|
|
}
|
2012-06-29 16:48:34 +00:00
|
|
|
|
|
|
|
nsString errorName;
|
|
|
|
if (error) {
|
2013-05-18 17:52:06 +00:00
|
|
|
error->GetName(errorName);
|
2012-06-29 16:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 17:48:43 +00:00
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
RootedDictionary<ErrorEventInit> init(cx);
|
2014-02-23 05:01:12 +00:00
|
|
|
request->FillScriptErrorEvent(init);
|
2012-09-09 23:29:12 +00:00
|
|
|
|
2014-02-23 05:01:12 +00:00
|
|
|
init.mMessage = errorName;
|
|
|
|
init.mCancelable = true;
|
|
|
|
init.mBubbles = true;
|
2012-06-29 16:48:34 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(aOwner));
|
|
|
|
NS_ASSERTION(sgo, "How can this happen?!");
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-02-23 05:01:12 +00:00
|
|
|
if (NS_FAILED(sgo->HandleScriptError(init, &status))) {
|
2012-06-29 16:48:34 +00: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-09 23:29:12 +00:00
|
|
|
if (NS_FAILED(scriptError->InitWithWindowID(errorName,
|
2014-02-23 05:01:12 +00:00
|
|
|
init.mFilename,
|
|
|
|
EmptyString(), init.mLineno,
|
2012-07-20 11:16:17 +00:00
|
|
|
0, 0,
|
2012-06-29 16:48:34 +00: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 16:31:19 +00:00
|
|
|
// static
|
|
|
|
bool
|
2012-11-10 18:32:37 +00:00
|
|
|
IndexedDatabaseManager::TabContextMayAccessOrigin(const TabContext& aContext,
|
|
|
|
const nsACString& aOrigin)
|
2012-11-10 01:14:40 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aOrigin.IsEmpty(), "Empty origin!");
|
|
|
|
|
2012-11-10 18:32:37 +00: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 01:14:40 +00:00
|
|
|
nsAutoCString pattern;
|
2013-03-26 11:13:17 +00:00
|
|
|
QuotaManager::GetOriginPatternStringMaybeIgnoreBrowser(
|
|
|
|
aContext.OwnOrContainingAppId(),
|
|
|
|
aContext.IsBrowserElement(),
|
|
|
|
pattern);
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2012-11-10 01:14:40 +00:00
|
|
|
return PatternMatchesOrigin(pattern, aOrigin);
|
2012-10-23 16:31:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 11:25:46 +00:00
|
|
|
// static
|
|
|
|
bool
|
2013-11-19 07:36:12 +00:00
|
|
|
IndexedDatabaseManager::DefineIndexedDB(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
2013-09-28 11:25:46 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-11-19 07:36:12 +00: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 11:25:46 +00:00
|
|
|
|
2013-11-19 07:36:12 +00:00
|
|
|
if (!IDBCursorBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBCursorWithValueBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBDatabaseBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBFactoryBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBFileHandleBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBIndexBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBKeyRangeBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBObjectStoreBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBOpenDBRequestBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBRequestBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBTransactionBinding::GetConstructorObject(aCx, aGlobal) ||
|
2013-11-19 07:36:33 +00:00
|
|
|
!IDBVersionChangeEventBinding::GetConstructorObject(aCx, aGlobal))
|
|
|
|
{
|
2013-11-19 07:36:12 +00:00
|
|
|
return false;
|
2013-09-28 11:25:46 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 07:36:12 +00:00
|
|
|
nsRefPtr<IDBFactory> factory;
|
|
|
|
if (NS_FAILED(IDBFactory::Create(aCx, aGlobal, nullptr,
|
|
|
|
getter_AddRefs(factory)))) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-28 11:25:46 +00:00
|
|
|
|
2013-11-19 07:36:12 +00:00
|
|
|
MOZ_ASSERT(factory, "This should never fail for chrome!");
|
2013-09-28 11:25:46 +00:00
|
|
|
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Rooted<JS::Value> indexedDB(aCx);
|
2013-11-19 07:36:12 +00:00
|
|
|
if (!WrapNewBindingObject(aCx, aGlobal, factory, &indexedDB)) {
|
2013-09-28 11:25:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_DefineProperty(aCx, aGlobal, IDB_STR, indexedDB, nullptr, nullptr,
|
|
|
|
JSPROP_ENUMERATE);
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
// static
|
2010-09-10 19:12:11 +00:00
|
|
|
bool
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager::IsClosed()
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2014-02-07 06:17:07 +00:00
|
|
|
return gClosed;
|
2013-03-26 11:13:17 +00:00
|
|
|
}
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
#ifdef DEBUG
|
2013-05-10 21:22:01 +00:00
|
|
|
// static
|
2013-03-26 11:13:17 +00:00
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::IsMainProcess()
|
|
|
|
{
|
2013-12-09 05:31:48 +00:00
|
|
|
NS_ASSERTION(gDBManager,
|
2013-03-26 11:13:17 +00:00
|
|
|
"IsMainProcess() called before indexedDB has been initialized!");
|
|
|
|
NS_ASSERTION((XRE_GetProcessType() == GeckoProcessType_Default) ==
|
|
|
|
sIsMainProcess, "XRE_GetProcessType changed its tune!");
|
|
|
|
return sIsMainProcess;
|
|
|
|
}
|
2013-05-10 21:22:01 +00:00
|
|
|
|
|
|
|
//static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::InLowDiskSpaceMode()
|
|
|
|
{
|
2013-12-09 05:31:48 +00:00
|
|
|
NS_ASSERTION(gDBManager,
|
2013-05-10 21:22:01 +00:00
|
|
|
"InLowDiskSpaceMode() called before indexedDB has been "
|
|
|
|
"initialized!");
|
2014-02-07 06:17:07 +00:00
|
|
|
return sLowDiskSpaceMode;
|
2013-05-10 21:22:01 +00:00
|
|
|
}
|
2013-03-26 11:13:17 +00:00
|
|
|
#endif
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
already_AddRefed<FileManager>
|
2013-09-11 04:18:36 +00:00
|
|
|
IndexedDatabaseManager::GetFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
2013-03-26 11:13:17 +00:00
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aOrigin, &info)) {
|
2013-03-26 11:13:17 +00:00
|
|
|
return nullptr;
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
2010-10-19 17:58:42 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
nsRefPtr<FileManager> fileManager =
|
|
|
|
info->GetFileManager(aPersistenceType, aDatabaseName);
|
2013-03-26 11:13:17 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
return fileManager.forget();
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:33 +00:00
|
|
|
void
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager::AddFileManager(FileManager* aFileManager)
|
2010-10-19 17:58:33 +00:00
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_ASSERTION(aFileManager, "Null file manager!");
|
2010-10-19 17:58:33 +00:00
|
|
|
|
2013-09-11 04:18:36 +00: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 17:58:33 +00:00
|
|
|
}
|
2013-03-26 11:13:17 +00:00
|
|
|
|
2013-09-11 04:18:36 +00: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 17:58:33 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::InvalidateAllFileManagers()
|
2010-10-19 17:58:52 +00:00
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
mFileManagerInfos.Enumerate(InvalidateAndRemoveFileManagers, nullptr);
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 12:53:12 +00:00
|
|
|
void
|
2013-09-11 04:18:36 +00:00
|
|
|
IndexedDatabaseManager::InvalidateFileManagers(
|
|
|
|
PersistenceType aPersistenceType,
|
|
|
|
const OriginOrPatternString& aOriginOrPattern)
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
2013-09-11 04:18:36 +00:00
|
|
|
NS_ASSERTION(!aOriginOrPattern.IsEmpty(), "Empty pattern!");
|
2013-06-05 08:11:23 +00:00
|
|
|
|
2013-09-11 04:18:36 +00: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 11:13:17 +00:00
|
|
|
}
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
void
|
2013-09-11 04:18:36 +00:00
|
|
|
IndexedDatabaseManager::InvalidateFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aOrigin,
|
2013-03-26 11:13:17 +00:00
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aOrigin, &info)) {
|
2013-03-26 11:13:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
info->InvalidateAndRemoveFileManager(aPersistenceType, aDatabaseName);
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
if (!info->HasFileManagers()) {
|
|
|
|
mFileManagerInfos.Remove(aOrigin);
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
2010-10-19 17:58:42 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
nsresult
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager::AsyncDeleteFile(FileManager* aFileManager,
|
|
|
|
int64_t aFileId)
|
2010-10-19 17:58:52 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-20 01:50:39 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aFileManager);
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
2012-06-20 01:50:39 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
// See if we're currently clearing the storages for this origin. If so then
|
|
|
|
// we pretend that we've already deleted everything.
|
|
|
|
if (quotaManager->IsClearOriginPending(aFileManager->Origin())) {
|
|
|
|
return NS_OK;
|
2012-06-20 01:50:39 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
nsRefPtr<AsyncDeleteFileRunnable> runnable =
|
|
|
|
new AsyncDeleteFileRunnable(aFileManager, aFileId);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
quotaManager->IOThread()->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
2012-06-20 01:50:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 17:58:52 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-05 08:11:23 +00:00
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::BlockAndGetFileReferences(
|
2013-09-11 04:18:36 +00: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 08:11:23 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<GetFileReferencesHelper> helper =
|
2013-09-11 04:18:36 +00:00
|
|
|
new GetFileReferencesHelper(aPersistenceType, aOrigin, aDatabaseName,
|
|
|
|
aFileId);
|
2013-06-05 08:11:23 +00:00
|
|
|
|
|
|
|
nsresult rv = helper->DispatchAndReturnFileReferences(aRefCnt, aDBRefCnt,
|
|
|
|
aSliceRefCnt, aResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_IMPL_ADDREF(IndexedDatabaseManager)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(IndexedDatabaseManager, Destroy())
|
2013-05-10 21:22:01 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE2(IndexedDatabaseManager, nsIIndexedDatabaseManager,
|
|
|
|
nsIObserver)
|
2011-12-16 07:34:24 +00:00
|
|
|
|
2012-01-03 15:27:39 +00:00
|
|
|
NS_IMETHODIMP
|
2014-01-09 17:39:36 +00:00
|
|
|
IndexedDatabaseManager::InitWindowless(JS::Handle<JS::Value> aGlobal, JSContext* aCx)
|
2012-01-03 15:27:39 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_NOT_AVAILABLE);
|
2012-01-24 10:03:37 +00:00
|
|
|
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JSVAL_TO_OBJECT(aGlobal));
|
2013-09-28 11:25:46 +00:00
|
|
|
if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
|
|
|
|
NS_WARNING("Passed object is not a global object!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-08-30 00:50:28 +00:00
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
bool hasIndexedDB;
|
2013-09-28 11:25:46 +00:00
|
|
|
if (!JS_HasProperty(aCx, global, IDB_STR, &hasIndexedDB)) {
|
2012-08-30 00:50:28 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasIndexedDB) {
|
|
|
|
NS_WARNING("Passed object already has an 'indexedDB' property!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-11-19 07:36:12 +00:00
|
|
|
if (!DefineIndexedDB(aCx, global)) {
|
2012-01-03 15:27:39 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-10 21:22:01 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* aData)
|
2013-05-10 21:22:01 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsMainProcess(), "Wrong process!");
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-05-10 23:01:55 +00:00
|
|
|
if (!strcmp(aTopic, DISKSPACEWATCHER_OBSERVER_TOPIC)) {
|
2013-05-10 21:22:01 +00:00
|
|
|
NS_ASSERTION(aData, "No data?!");
|
|
|
|
|
|
|
|
const nsDependentString data(aData);
|
|
|
|
|
|
|
|
if (data.EqualsLiteral(LOW_DISK_SPACE_DATA_FULL)) {
|
2014-02-07 06:17:07 +00:00
|
|
|
sLowDiskSpaceMode = true;
|
2013-05-10 21:22:01 +00:00
|
|
|
}
|
|
|
|
else if (data.EqualsLiteral(LOW_DISK_SPACE_DATA_FREE)) {
|
2014-02-07 06:17:07 +00:00
|
|
|
sLowDiskSpaceMode = false;
|
2013-05-10 21:22:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown data value!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Unknown topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2013-09-11 04:18:36 +00: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 18:51:33 +00:00
|
|
|
AsyncDeleteFileRunnable::AsyncDeleteFileRunnable(FileManager* aFileManager,
|
|
|
|
int64_t aFileId)
|
|
|
|
: mFileManager(aFileManager), mFileId(aFileId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(AsyncDeleteFileRunnable,
|
|
|
|
nsIRunnable)
|
2011-12-16 07:34:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-26 11:13:17 +00:00
|
|
|
AsyncDeleteFileRunnable::Run()
|
2011-12-16 07:34:24 +00:00
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
2011-12-16 07:34:24 +00:00
|
|
|
|
2012-08-24 18:51:33 +00: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 19:25:10 +00:00
|
|
|
nsresult rv;
|
|
|
|
int64_t fileSize;
|
2012-08-24 18:51:33 +00:00
|
|
|
|
2012-12-17 19:25:10 +00:00
|
|
|
if (mFileManager->Privilege() != Chrome) {
|
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2011-12-16 07:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 19:25:10 +00:00
|
|
|
rv = file->Remove(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2012-06-11 21:59:27 +00:00
|
|
|
|
2012-12-17 19:25:10 +00:00
|
|
|
if (mFileManager->Privilege() != Chrome) {
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
quotaManager->DecreaseUsageForOrigin(mFileManager->Type(),
|
|
|
|
mFileManager->Group(),
|
|
|
|
mFileManager->Origin(), fileSize);
|
2012-06-11 21:59:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 18:51:33 +00: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 07:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-05 08:11:23 +00: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;
|
|
|
|
}
|
|
|
|
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(GetFileReferencesHelper,
|
|
|
|
nsIRunnable)
|
2013-06-05 08:11:23 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
GetFileReferencesHelper::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never fail!");
|
|
|
|
|
|
|
|
nsRefPtr<FileManager> fileManager =
|
2013-09-11 04:18:36 +00:00
|
|
|
mgr->GetFileManager(mPersistenceType, mOrigin, mDatabaseName);
|
2013-06-05 08:11:23 +00: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;
|
|
|
|
}
|