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"
|
2012-06-03 20:33:52 +04:00
|
|
|
#include "nsIFileStorage.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-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"
|
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"
|
2013-05-11 02:30:49 +04:00
|
|
|
#include "mozilla/Services.h"
|
2011-08-26 12:21:35 +04:00
|
|
|
#include "mozilla/storage.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "nsContentUtils.h"
|
2012-06-29 20:48:34 +04:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#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-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"
|
|
|
|
|
2010-09-10 23:12:11 +04:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-11-10 22:32:37 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-03-26 15:13:17 +04:00
|
|
|
USING_QUOTA_NAMESPACE
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2013-09-11 08:18:36 +04: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 23:12:11 +04:00
|
|
|
namespace {
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
mozilla::StaticRefPtr<IndexedDatabaseManager> gInstance;
|
2012-10-23 20:31:19 +04:00
|
|
|
|
2013-08-12 13:51:49 +04:00
|
|
|
mozilla::Atomic<int32_t> gInitialized(0);
|
|
|
|
mozilla::Atomic<int32_t> gClosed(0);
|
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:
|
|
|
|
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:
|
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
|
|
|
|
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;
|
2013-08-12 13:51:49 +04:00
|
|
|
mozilla::Atomic<int32_t> IndexedDatabaseManager::sLowDiskSpaceMode(0);
|
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-03-26 15:13:17 +04:00
|
|
|
if (!gInstance) {
|
2012-06-01 21:21:12 +04:00
|
|
|
sIsMainProcess = XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-08-26 12:21:35 +04:00
|
|
|
|
2013-05-11 01:22:01 +04:00
|
|
|
if (sIsMainProcess) {
|
|
|
|
// 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))) {
|
|
|
|
sLowDiskSpaceMode = isDiskFull ? 1 : 0;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2013-08-12 13:51:49 +04:00
|
|
|
if (gInitialized.exchange(1)) {
|
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
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
gInstance = instance;
|
2013-03-26 15:13:17 +04:00
|
|
|
|
|
|
|
ClearOnShutdown(&gInstance);
|
2010-09-10 23:12:11 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
return gInstance;
|
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.
|
2010-09-10 23:12:11 +04:00
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:33 +04: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 15:13:17 +04:00
|
|
|
IndexedDatabaseManager* mgr = GetOrCreate();
|
|
|
|
NS_IF_ADDREF(mgr);
|
|
|
|
return mgr;
|
2010-10-19 21:58:33 +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
|
|
|
|
|
|
|
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.
|
2013-08-12 13:51:49 +04:00
|
|
|
if (!!gInitialized && gClosed.exchange(1)) {
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_ERROR("Shutdown more than once?!");
|
|
|
|
}
|
2012-06-01 21:21:12 +04:00
|
|
|
|
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,
|
|
|
|
nsEventChainPostVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptErrorEvent event(true, NS_LOAD_ERROR);
|
|
|
|
request->FillScriptErrorEvent(&event);
|
2012-09-10 03:29:12 +04:00
|
|
|
NS_ABORT_IF_FALSE(event.fileName,
|
|
|
|
"FillScriptErrorEvent should give us a non-null string "
|
|
|
|
"for our error's fileName");
|
|
|
|
|
2012-06-29 20:48:34 +04:00
|
|
|
event.errorMsg = errorName.get();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(aOwner));
|
|
|
|
NS_ASSERTION(sgo, "How can this happen?!");
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
if (NS_FAILED(sgo->HandleScriptError(&event, &status))) {
|
|
|
|
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,
|
|
|
|
nsDependentString(event.fileName),
|
|
|
|
EmptyString(), event.lineNr,
|
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-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
|
|
|
{
|
2013-03-26 15:13:17 +04:00
|
|
|
return !!gClosed;
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(gInstance,
|
|
|
|
"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()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(gInstance,
|
|
|
|
"InLowDiskSpaceMode() called before indexedDB has been "
|
|
|
|
"initialized!");
|
|
|
|
return !!sLowDiskSpaceMode;
|
|
|
|
}
|
2013-03-26 15:13:17 +04:00
|
|
|
#endif
|
2010-09-10 23:12:11 +04:00
|
|
|
|
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.
|
|
|
|
if (quotaManager->IsClearOriginPending(aFileManager->Origin())) {
|
|
|
|
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
|
|
|
{
|
|
|
|
nsRefPtr<GetFileReferencesHelper> helper =
|
2013-09-11 08:18:36 +04:00
|
|
|
new GetFileReferencesHelper(aPersistenceType, aOrigin, aDatabaseName,
|
|
|
|
aFileId);
|
2013-06-05 12:11:23 +04:00
|
|
|
|
|
|
|
nsresult rv = helper->DispatchAndReturnFileReferences(aRefCnt, aDBRefCnt,
|
|
|
|
aSliceRefCnt, aResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
NS_IMPL_ADDREF(IndexedDatabaseManager)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(IndexedDatabaseManager, Destroy())
|
2013-05-11 01:22:01 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE2(IndexedDatabaseManager, nsIIndexedDatabaseManager,
|
|
|
|
nsIObserver)
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2012-01-03 19:27:39 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::InitWindowless(const jsval& aObj, JSContext* aCx)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_NOT_AVAILABLE);
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_ENSURE_ARG(!JSVAL_IS_PRIMITIVE(aObj));
|
|
|
|
|
2013-05-02 13:12:46 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JSVAL_TO_OBJECT(aObj));
|
2012-08-30 04:50:28 +04:00
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool hasIndexedDB;
|
2012-08-30 04:50:28 +04:00
|
|
|
if (!JS_HasProperty(aCx, obj, "indexedDB", &hasIndexedDB)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasIndexedDB) {
|
|
|
|
NS_WARNING("Passed object already has an 'indexedDB' property!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS_GetGlobalForObject(aCx, obj));
|
2012-08-30 04:50:28 +04:00
|
|
|
NS_ASSERTION(global, "What?! No global!");
|
2012-01-24 14:03:37 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsRefPtr<IDBFactory> factory;
|
2012-08-02 10:02:29 +04:00
|
|
|
nsresult rv =
|
|
|
|
IDBFactory::Create(aCx, global, nullptr, getter_AddRefs(factory));
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(factory, "This should never fail for chrome!");
|
2012-01-03 19:27:39 +04:00
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> indexedDBVal(aCx);
|
|
|
|
rv = nsContentUtils::WrapNative(aCx, obj, factory, indexedDBVal.address());
|
2012-01-03 19:27:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!JS_DefineProperty(aCx, obj, "indexedDB", indexedDBVal, nullptr,
|
|
|
|
nullptr, JSPROP_ENUMERATE)) {
|
2012-01-03 19:27:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JSObject*> keyrangeObj(aCx,
|
|
|
|
JS_NewObject(aCx, nullptr, nullptr, nullptr));
|
2012-01-03 19:27:39 +04:00
|
|
|
NS_ENSURE_TRUE(keyrangeObj, NS_ERROR_OUT_OF_MEMORY);
|
2012-01-24 14:03:37 +04:00
|
|
|
|
2012-01-03 19:27:39 +04:00
|
|
|
if (!IDBKeyRange::DefineConstructors(aCx, keyrangeObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineProperty(aCx, obj, "IDBKeyRange", OBJECT_TO_JSVAL(keyrangeObj),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, nullptr, JSPROP_ENUMERATE)) {
|
2012-01-03 19:27:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-11 01:22:01 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
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)) {
|
2013-08-12 13:51:49 +04:00
|
|
|
sLowDiskSpaceMode = 1;
|
2013-05-11 01:22:01 +04:00
|
|
|
}
|
|
|
|
else if (data.EqualsLiteral(LOW_DISK_SPACE_DATA_FREE)) {
|
2013-08-12 13:51:49 +04:00
|
|
|
sLowDiskSpaceMode = 0;
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(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;
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(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;
|
|
|
|
}
|