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"
|
2012-01-03 15:27:39 +00:00
|
|
|
#include "nsIDOMScriptObjectFactory.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-03-26 11:13:17 +00:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-06-05 08:11:23 +00:00
|
|
|
#include "mozilla/CondVar.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"
|
2013-05-10 22:30:49 +00:00
|
|
|
#include "mozilla/Services.h"
|
2011-08-26 08:21:35 +00:00
|
|
|
#include "mozilla/storage.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-06-29 16:48:34 +00:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2013-07-11 20:21:45 +00:00
|
|
|
#include "pratom.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
|
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-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
|
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
|
|
|
|
2012-01-03 15:27:39 +00:00
|
|
|
static NS_DEFINE_CID(kDOMSOF_CID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
|
|
|
|
|
2010-09-10 19:12:11 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
mozilla::StaticRefPtr<IndexedDatabaseManager> gInstance;
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
int32_t gInitialized = 0;
|
|
|
|
int32_t gClosed = 0;
|
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
|
|
|
|
|
|
|
|
GetFileReferencesHelper(const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName,
|
|
|
|
int64_t aFileId)
|
|
|
|
: mOrigin(aOrigin), mDatabaseName(aDatabaseName), mFileId(aFileId),
|
|
|
|
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:
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2011-12-16 07:34:24 +00:00
|
|
|
PLDHashOperator
|
2012-10-23 16:31:19 +00:00
|
|
|
InvalidateAndRemoveFileManagers(
|
|
|
|
const nsACString& aKey,
|
|
|
|
nsAutoPtr<nsTArray<nsRefPtr<FileManager> > >& aValue,
|
|
|
|
void* aUserArg)
|
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
|
|
|
NS_ASSERTION(!aKey.IsEmpty(), "Empty key!");
|
|
|
|
NS_ASSERTION(aValue, "Null pointer!");
|
|
|
|
|
2012-10-23 16:31:19 +00:00
|
|
|
const nsACString* pattern =
|
|
|
|
static_cast<const nsACString*>(aUserArg);
|
|
|
|
|
|
|
|
if (!pattern || PatternMatchesOrigin(*pattern, aKey)) {
|
|
|
|
for (uint32_t i = 0; i < aValue->Length(); i++) {
|
|
|
|
nsRefPtr<FileManager>& fileManager = aValue->ElementAt(i);
|
|
|
|
fileManager->Invalidate();
|
|
|
|
}
|
|
|
|
return PL_DHASH_REMOVE;
|
2011-12-16 07:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
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!");
|
2013-03-26 11:13:17 +00:00
|
|
|
|
|
|
|
mFileManagers.Init();
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDatabaseManager::~IndexedDatabaseManager()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
bool IndexedDatabaseManager::sIsMainProcess = false;
|
2013-05-10 21:22:01 +00:00
|
|
|
int32_t IndexedDatabaseManager::sLowDiskSpaceMode = 0;
|
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-03-26 11:13:17 +00:00
|
|
|
if (!gInstance) {
|
2012-06-01 17:21:12 +00:00
|
|
|
sIsMainProcess = XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-08-26 08:21:35 +00:00
|
|
|
|
2013-05-10 21:22:01 +00: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 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
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
if (PR_ATOMIC_SET(&gInitialized, 1)) {
|
|
|
|
NS_ERROR("Initialized more than once?!");
|
2012-06-01 17:21:12 +00:00
|
|
|
}
|
2011-08-26 08:21:35 +00:00
|
|
|
|
2010-10-19 17:58:33 +00:00
|
|
|
gInstance = instance;
|
2013-03-26 11:13:17 +00:00
|
|
|
|
|
|
|
ClearOnShutdown(&gInstance);
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
return gInstance;
|
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.
|
2010-09-10 19:12:11 +00:00
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if (!!gInitialized && PR_ATOMIC_SET(&gClosed, 1)) {
|
|
|
|
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,
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptErrorEvent event(true, NS_LOAD_ERROR);
|
|
|
|
request->FillScriptErrorEvent(&event);
|
2012-09-09 23:29:12 +00:00
|
|
|
NS_ABORT_IF_FALSE(event.fileName,
|
|
|
|
"FillScriptErrorEvent should give us a non-null string "
|
|
|
|
"for our error's fileName");
|
|
|
|
|
2012-06-29 16:48:34 +00: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-09 23:29:12 +00:00
|
|
|
if (NS_FAILED(scriptError->InitWithWindowID(errorName,
|
|
|
|
nsDependentString(event.fileName),
|
|
|
|
EmptyString(), event.lineNr,
|
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-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
|
|
|
{
|
2013-03-26 11:13:17 +00:00
|
|
|
return !!gClosed;
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
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-10 21:22:01 +00:00
|
|
|
|
|
|
|
//static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::InLowDiskSpaceMode()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(gInstance,
|
|
|
|
"InLowDiskSpaceMode() called before indexedDB has been "
|
|
|
|
"initialized!");
|
|
|
|
return !!sLowDiskSpaceMode;
|
|
|
|
}
|
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>
|
|
|
|
IndexedDatabaseManager::GetFileManager(const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
nsTArray<nsRefPtr<FileManager> >* array;
|
|
|
|
if (!mFileManagers.Get(aOrigin, &array)) {
|
|
|
|
return nullptr;
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
2010-10-19 17:58:42 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
for (uint32_t i = 0; i < array->Length(); i++) {
|
|
|
|
nsRefPtr<FileManager>& fileManager = array->ElementAt(i);
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
if (fileManager->DatabaseName().Equals(aDatabaseName)) {
|
|
|
|
nsRefPtr<FileManager> result = fileManager;
|
|
|
|
return result.forget();
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-26 11:13:17 +00:00
|
|
|
|
|
|
|
return nullptr;
|
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-03-26 11:13:17 +00:00
|
|
|
nsTArray<nsRefPtr<FileManager> >* array;
|
|
|
|
if (!mFileManagers.Get(aFileManager->Origin(), &array)) {
|
|
|
|
array = new nsTArray<nsRefPtr<FileManager> >();
|
|
|
|
mFileManagers.Put(aFileManager->Origin(), array);
|
2010-10-19 17:58:33 +00:00
|
|
|
}
|
2013-03-26 11:13:17 +00:00
|
|
|
|
|
|
|
array->AppendElement(aFileManager);
|
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-03-26 11:13:17 +00:00
|
|
|
mFileManagers.Enumerate(InvalidateAndRemoveFileManagers, nullptr);
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 12:53:12 +00:00
|
|
|
void
|
2013-03-26 11:13:17 +00:00
|
|
|
IndexedDatabaseManager::InvalidateFileManagersForPattern(
|
|
|
|
const nsACString& aPattern)
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_ASSERTION(!aPattern.IsEmpty(), "Empty pattern!");
|
2013-06-05 08:11:23 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
mFileManagers.Enumerate(InvalidateAndRemoveFileManagers,
|
|
|
|
const_cast<nsACString*>(&aPattern));
|
|
|
|
}
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::InvalidateFileManager(const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
nsTArray<nsRefPtr<FileManager> >* array;
|
|
|
|
if (!mFileManagers.Get(aOrigin, &array)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
for (uint32_t i = 0; i < array->Length(); i++) {
|
|
|
|
nsRefPtr<FileManager> fileManager = array->ElementAt(i);
|
|
|
|
if (fileManager->DatabaseName().Equals(aDatabaseName)) {
|
|
|
|
fileManager->Invalidate();
|
|
|
|
array->RemoveElementAt(i);
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
if (array->IsEmpty()) {
|
|
|
|
mFileManagers.Remove(aOrigin);
|
2011-11-02 12:53:12 +00:00
|
|
|
}
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
break;
|
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(
|
|
|
|
const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName,
|
|
|
|
int64_t aFileId,
|
|
|
|
int32_t* aRefCnt,
|
|
|
|
int32_t* aDBRefCnt,
|
|
|
|
int32_t* aSliceRefCnt,
|
|
|
|
bool* aResult)
|
|
|
|
{
|
|
|
|
nsRefPtr<GetFileReferencesHelper> helper =
|
|
|
|
new GetFileReferencesHelper(aOrigin, aDatabaseName, aFileId);
|
|
|
|
|
|
|
|
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
|
|
|
|
IndexedDatabaseManager::InitWindowless(const jsval& aObj, JSContext* aCx)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_NOT_AVAILABLE);
|
2012-01-24 10:03:37 +00:00
|
|
|
NS_ENSURE_ARG(!JSVAL_IS_PRIMITIVE(aObj));
|
|
|
|
|
2013-05-02 09:12:46 +00:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JSVAL_TO_OBJECT(aObj));
|
2012-08-30 00:50:28 +00:00
|
|
|
|
|
|
|
JSBool hasIndexedDB;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:27:39 +00:00
|
|
|
// Instantiating this class will register exception providers so even
|
2012-01-24 10:03:37 +00:00
|
|
|
// in xpcshell we will get typed (dom) exceptions, instead of general
|
|
|
|
// exceptions.
|
2012-01-03 15:27:39 +00:00
|
|
|
nsCOMPtr<nsIDOMScriptObjectFactory> sof(do_GetService(kDOMSOF_CID));
|
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS_GetGlobalForObject(aCx, obj));
|
2012-08-30 00:50:28 +00:00
|
|
|
NS_ASSERTION(global, "What?! No global!");
|
2012-01-24 10:03:37 +00:00
|
|
|
|
2012-06-01 17:21:12 +00:00
|
|
|
nsRefPtr<IDBFactory> factory;
|
2012-08-02 06:02:29 +00:00
|
|
|
nsresult rv =
|
|
|
|
IDBFactory::Create(aCx, global, nullptr, getter_AddRefs(factory));
|
2012-06-01 17:21:12 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(factory, "This should never fail for chrome!");
|
2012-01-03 15:27:39 +00:00
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> indexedDBVal(aCx);
|
|
|
|
rv = nsContentUtils::WrapNative(aCx, obj, factory, indexedDBVal.address());
|
2012-01-03 15:27:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!JS_DefineProperty(aCx, obj, "indexedDB", indexedDBVal, nullptr,
|
|
|
|
nullptr, JSPROP_ENUMERATE)) {
|
2012-01-03 15:27:39 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JSObject*> keyrangeObj(aCx,
|
|
|
|
JS_NewObject(aCx, nullptr, nullptr, nullptr));
|
2012-01-03 15:27:39 +00:00
|
|
|
NS_ENSURE_TRUE(keyrangeObj, NS_ERROR_OUT_OF_MEMORY);
|
2012-01-24 10:03:37 +00:00
|
|
|
|
2012-01-03 15:27:39 +00:00
|
|
|
if (!IDBKeyRange::DefineConstructors(aCx, keyrangeObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineProperty(aCx, obj, "IDBKeyRange", OBJECT_TO_JSVAL(keyrangeObj),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, nullptr, JSPROP_ENUMERATE)) {
|
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,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
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)) {
|
|
|
|
PR_ATOMIC_SET(&sLowDiskSpaceMode, 1);
|
|
|
|
}
|
|
|
|
else if (data.EqualsLiteral(LOW_DISK_SPACE_DATA_FREE)) {
|
|
|
|
PR_ATOMIC_SET(&sLowDiskSpaceMode, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown data value!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Unknown topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
|
|
|
|
quotaManager->DecreaseUsageForOrigin(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 =
|
|
|
|
mgr->GetFileManager(mOrigin, mDatabaseName);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|