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"
|
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-12-17 06:26:15 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2014-09-26 23:21:57 +00:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
2014-12-17 06:26:15 +00:00
|
|
|
#include "mozilla/dom/ErrorEvent.h"
|
2014-02-23 05:01:12 +00:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2014-09-26 23:21:57 +00:00
|
|
|
#include "mozilla/dom/PBlobChild.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-09-26 23:21:57 +00:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "nsContentUtils.h"
|
2014-12-17 06:26:15 +00:00
|
|
|
#include "nsGlobalWindow.h"
|
2010-09-10 19:12:11 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2014-10-16 04:56:52 +00:00
|
|
|
#include "prlog.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"
|
2014-10-16 04:56:52 +00:00
|
|
|
#include "ProfilerHelpers.h"
|
2014-12-17 06:26:15 +00:00
|
|
|
#include "WorkerScope.h"
|
|
|
|
#include "WorkerPrivate.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"
|
|
|
|
#include "mozilla/dom/IDBIndexBinding.h"
|
2014-06-12 03:35:29 +00:00
|
|
|
#include "mozilla/dom/IDBKeyRangeBinding.h"
|
|
|
|
#include "mozilla/dom/IDBMutableFileBinding.h"
|
2013-09-25 23:10:57 +00:00
|
|
|
#include "mozilla/dom/IDBObjectStoreBinding.h"
|
|
|
|
#include "mozilla/dom/IDBOpenDBRequestBinding.h"
|
|
|
|
#include "mozilla/dom/IDBRequestBinding.h"
|
|
|
|
#include "mozilla/dom/IDBTransactionBinding.h"
|
|
|
|
#include "mozilla/dom/IDBVersionChangeEventBinding.h"
|
|
|
|
|
2013-09-28 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"
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
using namespace mozilla::dom::quota;
|
2014-12-17 06:26:15 +00:00
|
|
|
using namespace mozilla::dom::workers;
|
2013-09-11 04:18:36 +00:00
|
|
|
|
|
|
|
class FileManagerInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
already_AddRefed<FileManager>
|
|
|
|
GetFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsAString& aName) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
AddFileManager(FileManager* aFileManager);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasFileManagers() const
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
return !mPersistentStorageFileManagers.IsEmpty() ||
|
2014-11-28 08:44:12 +00:00
|
|
|
!mTemporaryStorageFileManagers.IsEmpty() ||
|
|
|
|
!mDefaultStorageFileManagers.IsEmpty();
|
2013-09-11 04:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateAllFileManagers() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateAndRemoveFileManagers(PersistenceType aPersistenceType);
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateAndRemoveFileManager(PersistenceType aPersistenceType,
|
|
|
|
const nsAString& aName);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<nsRefPtr<FileManager> >&
|
|
|
|
GetArray(PersistenceType aPersistenceType);
|
|
|
|
|
|
|
|
const nsTArray<nsRefPtr<FileManager> >&
|
|
|
|
GetImmutableArray(PersistenceType aPersistenceType) const
|
|
|
|
{
|
|
|
|
return const_cast<FileManagerInfo*>(this)->GetArray(aPersistenceType);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager> > mPersistentStorageFileManagers;
|
|
|
|
nsTArray<nsRefPtr<FileManager> > mTemporaryStorageFileManagers;
|
2014-11-28 08:44:12 +00:00
|
|
|
nsTArray<nsRefPtr<FileManager> > mDefaultStorageFileManagers;
|
2013-09-11 04:18:36 +00:00
|
|
|
};
|
|
|
|
|
2014-09-17 23:36:01 +00:00
|
|
|
namespace {
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-10-16 04:56:52 +00:00
|
|
|
#define IDB_PREF_BRANCH_ROOT "dom.indexedDB."
|
|
|
|
|
|
|
|
const char kTestingPref[] = IDB_PREF_BRANCH_ROOT "testing";
|
2014-12-17 06:26:15 +00:00
|
|
|
const char kPrefExperimental[] = IDB_PREF_BRANCH_ROOT "experimental";
|
2014-10-16 04:56:52 +00:00
|
|
|
|
|
|
|
#define IDB_PREF_LOGGING_BRANCH_ROOT IDB_PREF_BRANCH_ROOT "logging."
|
|
|
|
|
|
|
|
const char kPrefLoggingEnabled[] = IDB_PREF_LOGGING_BRANCH_ROOT "enabled";
|
|
|
|
const char kPrefLoggingDetails[] = IDB_PREF_LOGGING_BRANCH_ROOT "details";
|
|
|
|
|
|
|
|
#if defined(DEBUG) || defined(MOZ_ENABLE_PROFILER_SPS)
|
|
|
|
const char kPrefLoggingProfiler[] =
|
|
|
|
IDB_PREF_LOGGING_BRANCH_ROOT "profiler-marks";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef IDB_PREF_LOGGING_BRANCH_ROOT
|
|
|
|
#undef IDB_PREF_BRANCH_ROOT
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
StaticRefPtr<IndexedDatabaseManager> gDBManager;
|
2012-10-23 16:31:19 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
Atomic<bool> gInitialized(false);
|
|
|
|
Atomic<bool> gClosed(false);
|
|
|
|
Atomic<bool> gTestingMode(false);
|
|
|
|
Atomic<bool> gExperimentalFeaturesEnabled(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:
|
2014-06-23 19:56:07 +00:00
|
|
|
~AsyncDeleteFileRunnable() {}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
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:
|
2014-06-23 19:56:07 +00:00
|
|
|
~GetFileReferencesHelper() {}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
void
|
2014-12-17 06:26:15 +00:00
|
|
|
AtomicBoolPrefChangedCallback(const char* aPrefName, void* aClosure)
|
2014-09-26 23:21:57 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-12-17 06:26:15 +00:00
|
|
|
MOZ_ASSERT(aClosure);
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
*static_cast<Atomic<bool>*>(aClosure) = Preferences::GetBool(aPrefName);
|
2014-09-26 23:21:57 +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-10-30 20:18:33 +00:00
|
|
|
bool IndexedDatabaseManager::sFullSynchronousMode = false;
|
2014-10-16 04:56:52 +00:00
|
|
|
|
|
|
|
PRLogModuleInfo* IndexedDatabaseManager::sLoggingModule;
|
|
|
|
|
|
|
|
Atomic<IndexedDatabaseManager::LoggingMode>
|
|
|
|
IndexedDatabaseManager::sLoggingMode(
|
|
|
|
IndexedDatabaseManager::Logging_Disabled);
|
|
|
|
|
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-10-16 04:56:52 +00:00
|
|
|
if (!sLoggingModule) {
|
|
|
|
sLoggingModule = PR_NewLogModule("IndexedDB");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
// During Init() we can't yet call IsMainProcess(), just check sIsMainProcess
|
|
|
|
// directly.
|
|
|
|
if (sIsMainProcess) {
|
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
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
Preferences::RegisterCallbackAndCall(AtomicBoolPrefChangedCallback,
|
|
|
|
kTestingPref,
|
|
|
|
&gTestingMode);
|
|
|
|
Preferences::RegisterCallbackAndCall(AtomicBoolPrefChangedCallback,
|
|
|
|
kPrefExperimental,
|
|
|
|
&gExperimentalFeaturesEnabled);
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2014-10-30 20:18:33 +00:00
|
|
|
// By default IndexedDB uses SQLite with PRAGMA synchronous = NORMAL. This
|
|
|
|
// guarantees (unlike synchronous = OFF) atomicity and consistency, but not
|
|
|
|
// necessarily durability in situations such as power loss. This preference
|
|
|
|
// allows enabling PRAGMA synchronous = FULL on SQLite, which does guarantee
|
|
|
|
// durability, but with an extra fsync() and the corresponding performance
|
|
|
|
// hit.
|
|
|
|
sFullSynchronousMode = Preferences::GetBool("dom.indexedDB.fullSynchronous");
|
|
|
|
|
2014-10-16 04:56:52 +00:00
|
|
|
Preferences::RegisterCallback(LoggingModePrefChangedCallback,
|
|
|
|
kPrefLoggingDetails);
|
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
|
|
|
Preferences::RegisterCallback(LoggingModePrefChangedCallback,
|
|
|
|
kPrefLoggingProfiler);
|
|
|
|
#endif
|
|
|
|
Preferences::RegisterCallbackAndCall(LoggingModePrefChangedCallback,
|
|
|
|
kPrefLoggingEnabled);
|
|
|
|
|
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
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
Preferences::UnregisterCallback(AtomicBoolPrefChangedCallback,
|
|
|
|
kTestingPref,
|
|
|
|
&gTestingMode);
|
|
|
|
Preferences::UnregisterCallback(AtomicBoolPrefChangedCallback,
|
|
|
|
kPrefExperimental,
|
|
|
|
&gExperimentalFeaturesEnabled);
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2014-10-16 04:56:52 +00:00
|
|
|
Preferences::UnregisterCallback(LoggingModePrefChangedCallback,
|
|
|
|
kPrefLoggingDetails);
|
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
|
|
|
Preferences::UnregisterCallback(LoggingModePrefChangedCallback,
|
|
|
|
kPrefLoggingProfiler);
|
|
|
|
#endif
|
|
|
|
Preferences::UnregisterCallback(LoggingModePrefChangedCallback,
|
|
|
|
kPrefLoggingEnabled);
|
|
|
|
|
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
|
2014-12-17 06:26:15 +00:00
|
|
|
IndexedDatabaseManager::CommonPostHandleEvent(
|
|
|
|
DOMEventTargetHelper* aEventTarget,
|
|
|
|
IDBFactory* aFactory,
|
|
|
|
EventChainPostVisitor& aVisitor)
|
2012-06-29 16:48:34 +00:00
|
|
|
{
|
2014-12-17 06:26:15 +00:00
|
|
|
MOZ_ASSERT(aEventTarget);
|
|
|
|
MOZ_ASSERT(aFactory);
|
|
|
|
MOZ_ASSERT(aVisitor.mDOMEvent);
|
2012-06-29 16:48:34 +00:00
|
|
|
|
|
|
|
if (aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString type;
|
|
|
|
nsresult rv = aVisitor.mDOMEvent->GetType(type);
|
2014-12-17 06:26:15 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-29 16:48:34 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(errorType, "error");
|
|
|
|
|
|
|
|
MOZ_ASSERT(nsDependentString(kErrorEventType) == errorType);
|
|
|
|
|
|
|
|
if (type != errorType) {
|
2012-06-29 16:48:34 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-19 22:18:32 +00:00
|
|
|
nsCOMPtr<EventTarget> eventTarget =
|
|
|
|
aVisitor.mDOMEvent->InternalDOMEvent()->GetTarget();
|
2014-12-17 06:26:15 +00:00
|
|
|
MOZ_ASSERT(eventTarget);
|
2012-06-29 16:48:34 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
auto* request = static_cast<IDBRequest*>(eventTarget.get());
|
2012-06-29 16:48:34 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
nsRefPtr<DOMError> error = request->GetErrorAfterResult();
|
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-11-14 02:20:38 +00:00
|
|
|
request->GetCallerLocation(init.mFilename, &init.mLineno);
|
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
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-12-17 06:26:15 +00:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (nsPIDOMWindow* window = aEventTarget->GetOwner()) {
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(window);
|
|
|
|
MOZ_ASSERT(sgo);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(sgo->HandleScriptError(init, &status)))) {
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't fire error events at any global for non-window JS on the main
|
|
|
|
// thread.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not on the main thread, must be in a worker.
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
nsRefPtr<WorkerGlobalScope> globalScope = workerPrivate->GlobalScope();
|
|
|
|
MOZ_ASSERT(globalScope);
|
|
|
|
|
|
|
|
nsRefPtr<ErrorEvent> errorEvent =
|
|
|
|
ErrorEvent::Constructor(globalScope, errorType, init);
|
|
|
|
MOZ_ASSERT(errorEvent);
|
|
|
|
|
|
|
|
errorEvent->SetTrusted(true);
|
|
|
|
|
|
|
|
auto* target = static_cast<EventTarget*>(globalScope.get());
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
EventDispatcher::DispatchDOMEvent(target,
|
|
|
|
/* aWidgetEvent */ nullptr,
|
|
|
|
errorEvent,
|
|
|
|
/* aPresContext */ nullptr,
|
|
|
|
&status)))) {
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
}
|
2012-06-29 16:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
if (status == nsEventStatus_eConsumeNoDefault) {
|
2012-06-29 16:48:34 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
nsAutoCString category;
|
|
|
|
if (aFactory->IsChrome()) {
|
|
|
|
category.AssignLiteral("chrome ");
|
|
|
|
} else {
|
|
|
|
category.AssignLiteral("content ");
|
2012-06-29 16:48:34 +00:00
|
|
|
}
|
2014-12-17 06:26:15 +00:00
|
|
|
category.AppendLiteral("javascript");
|
2012-06-29 16:48:34 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
// Log the error to the error console.
|
2012-06-29 16:48:34 +00:00
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
2014-12-17 06:26:15 +00:00
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(consoleService);
|
2012-06-29 16:48:34 +00:00
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
|
|
|
|
MOZ_ASSERT(consoleService);
|
|
|
|
|
|
|
|
if (uint64_t innerWindowID = aFactory->InnerWindowID()) {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
scriptError->InitWithWindowID(errorName,
|
|
|
|
init.mFilename,
|
|
|
|
/* aSourceLine */ EmptyString(),
|
|
|
|
init.mLineno,
|
|
|
|
/* aColumnNumber */ 0,
|
|
|
|
nsIScriptError::errorFlag,
|
|
|
|
category,
|
|
|
|
innerWindowID)));
|
|
|
|
} else {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
scriptError->Init(errorName,
|
|
|
|
init.mFilename,
|
|
|
|
/* aSourceLine */ EmptyString(),
|
|
|
|
init.mLineno,
|
|
|
|
/* aColumnNumber */ 0,
|
|
|
|
nsIScriptError::errorFlag,
|
|
|
|
category.get())));
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(consoleService->LogMessage(scriptError)));
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-06-29 16:48:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
// We need to ensure that the manager has been created already here so that we
|
|
|
|
// load preferences that may control which properties are exposed.
|
|
|
|
if (NS_WARN_IF(!GetOrCreate())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) ||
|
|
|
|
!IDBIndexBinding::GetConstructorObject(aCx, aGlobal) ||
|
|
|
|
!IDBKeyRangeBinding::GetConstructorObject(aCx, aGlobal) ||
|
2014-06-12 03:35:29 +00:00
|
|
|
!IDBMutableFileBinding::GetConstructorObject(aCx, aGlobal) ||
|
2013-11-19 07:36:12 +00:00
|
|
|
!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;
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_FAILED(IDBFactory::CreateForChromeJS(aCx,
|
|
|
|
aGlobal,
|
|
|
|
getter_AddRefs(factory)))) {
|
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
|
|
|
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);
|
2014-04-08 22:27:19 +00:00
|
|
|
js::AssertSameCompartment(aCx, aGlobal);
|
2014-11-26 19:25:20 +00:00
|
|
|
if (!GetOrCreateDOMReflector(aCx, factory, &indexedDB)) {
|
2013-09-28 11:25:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-22 02:11:17 +00:00
|
|
|
return JS_DefineProperty(aCx, aGlobal, IDB_STR, indexedDB, JSPROP_ENUMERATE);
|
2013-09-28 11:25:46 +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
|
|
|
{
|
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
|
|
|
}
|
2014-10-16 04:56:52 +00:00
|
|
|
|
|
|
|
// static
|
|
|
|
IndexedDatabaseManager::LoggingMode
|
|
|
|
IndexedDatabaseManager::GetLoggingMode()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gDBManager,
|
|
|
|
"GetLoggingMode called before IndexedDatabaseManager has been "
|
|
|
|
"initialized!");
|
|
|
|
|
|
|
|
return sLoggingMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
PRLogModuleInfo*
|
|
|
|
IndexedDatabaseManager::GetLoggingModule()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gDBManager,
|
|
|
|
"GetLoggingModule called before IndexedDatabaseManager has been "
|
|
|
|
"initialized!");
|
|
|
|
|
|
|
|
return sLoggingModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DEBUG
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::InTestingMode()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gDBManager,
|
|
|
|
"InTestingMode() called before indexedDB has been initialized!");
|
|
|
|
|
|
|
|
return gTestingMode;
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:18:33 +00:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::FullSynchronous()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gDBManager,
|
|
|
|
"FullSynchronous() called before indexedDB has been initialized!");
|
|
|
|
|
|
|
|
return sFullSynchronousMode;
|
|
|
|
}
|
|
|
|
|
2014-12-17 06:26:15 +00:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::ExperimentalFeaturesEnabled(JSContext* aCx,
|
|
|
|
JSObject* aGlobal)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (NS_WARN_IF(!GetOrCreate())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(Get(),
|
|
|
|
"ExperimentalFeaturesEnabled() called off the main thread "
|
|
|
|
"before indexedDB has been initialized!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return gExperimentalFeaturesEnabled;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2014-11-28 08:44:12 +00:00
|
|
|
class MOZ_STACK_CLASS Helper MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PLDHashOperator
|
|
|
|
Enumerate(const nsACString& aKey,
|
|
|
|
FileManagerInfo* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
MOZ_ASSERT(!aKey.IsEmpty());
|
|
|
|
MOZ_ASSERT(aValue);
|
|
|
|
|
|
|
|
aValue->InvalidateAllFileManagers();
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mFileManagerInfos.EnumerateRead(Helper::Enumerate, nullptr);
|
|
|
|
mFileManagerInfos.Clear();
|
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,
|
2014-11-28 08:44:12 +00:00
|
|
|
const nsACString& aOrigin)
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2013-06-05 08:11:23 +00:00
|
|
|
AssertIsOnIOThread();
|
2014-11-28 08:44:12 +00:00
|
|
|
MOZ_ASSERT(!aOrigin.IsEmpty());
|
2013-06-05 08:11:23 +00:00
|
|
|
|
2014-11-28 08:44:12 +00:00
|
|
|
FileManagerInfo* info;
|
|
|
|
if (!mFileManagerInfos.Get(aOrigin, &info)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-11 04:18:36 +00:00
|
|
|
|
2014-11-28 08:44:12 +00:00
|
|
|
info->InvalidateAndRemoveFileManagers(aPersistenceType);
|
2013-09-11 04:18:36 +00:00
|
|
|
|
2014-11-28 08:44:12 +00:00
|
|
|
if (!info->HasFileManagers()) {
|
|
|
|
mFileManagerInfos.Remove(aOrigin);
|
2013-09-11 04:18:36 +00:00
|
|
|
}
|
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.
|
2014-04-14 20:48:49 +00:00
|
|
|
if (quotaManager->IsClearOriginPending(
|
|
|
|
aFileManager->Origin(),
|
|
|
|
Nullable<PersistenceType>(aFileManager->Type()))) {
|
2013-03-26 11:13:17 +00:00
|
|
|
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
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(!InTestingMode())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2013-06-05 08:11:23 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (IsMainProcess()) {
|
|
|
|
nsRefPtr<GetFileReferencesHelper> helper =
|
|
|
|
new GetFileReferencesHelper(aPersistenceType, aOrigin, aDatabaseName,
|
|
|
|
aFileId);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
helper->DispatchAndReturnFileReferences(aRefCnt, aDBRefCnt,
|
|
|
|
aSliceRefCnt, aResult);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ContentChild* contentChild = ContentChild::GetSingleton();
|
|
|
|
if (NS_WARN_IF(!contentChild)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!contentChild->SendGetFileReferences(aPersistenceType,
|
|
|
|
nsCString(aOrigin),
|
|
|
|
nsString(aDatabaseName),
|
|
|
|
aFileId,
|
|
|
|
aRefCnt,
|
|
|
|
aDBRefCnt,
|
|
|
|
aSliceRefCnt,
|
|
|
|
aResult)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 08:11:23 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-16 04:56:52 +00:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::LoggingModePrefChangedCallback(
|
|
|
|
const char* /* aPrefName */,
|
|
|
|
void* /* aClosure */)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!Preferences::GetBool(kPrefLoggingEnabled)) {
|
|
|
|
sLoggingMode = Logging_Disabled;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useProfiler =
|
|
|
|
#if defined(DEBUG) || defined(MOZ_ENABLE_PROFILER_SPS)
|
|
|
|
Preferences::GetBool(kPrefLoggingProfiler);
|
|
|
|
#if !defined(MOZ_ENABLE_PROFILER_SPS)
|
|
|
|
if (useProfiler) {
|
|
|
|
NS_WARNING("IndexedDB cannot create profiler marks because this build does "
|
|
|
|
"not have profiler extensions enabled!");
|
|
|
|
useProfiler = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const bool logDetails = Preferences::GetBool(kPrefLoggingDetails);
|
|
|
|
|
|
|
|
if (useProfiler) {
|
|
|
|
sLoggingMode = logDetails ?
|
|
|
|
Logging_DetailedProfilerMarks :
|
|
|
|
Logging_ConciseProfilerMarks;
|
|
|
|
} else {
|
|
|
|
sLoggingMode = logDetails ? Logging_Detailed : Logging_Concise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
NS_IMPL_ADDREF(IndexedDatabaseManager)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(IndexedDatabaseManager, Destroy())
|
2014-06-17 19:01:47 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE(IndexedDatabaseManager, nsIObserver)
|
2012-01-03 15:27:39 +00:00
|
|
|
|
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();
|
|
|
|
}
|
2014-11-28 08:44:12 +00:00
|
|
|
|
|
|
|
for (i = 0; i < mDefaultStorageFileManagers.Length(); i++) {
|
|
|
|
mDefaultStorageFileManagers[i]->Invalidate();
|
|
|
|
}
|
2013-09-11 04:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-11-28 08:44:12 +00:00
|
|
|
case PERSISTENCE_TYPE_DEFAULT:
|
|
|
|
return mDefaultStorageFileManagers;
|
2013-09-11 04:18:36 +00:00
|
|
|
|
|
|
|
case PERSISTENCE_TYPE_INVALID:
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Bad storage type value!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 18:51:33 +00:00
|
|
|
AsyncDeleteFileRunnable::AsyncDeleteFileRunnable(FileManager* aFileManager,
|
|
|
|
int64_t aFileId)
|
|
|
|
: mFileManager(aFileManager), mFileId(aFileId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(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
|
|
|
|
2014-10-28 19:06:54 +00:00
|
|
|
if (mFileManager->EnforcingQuota()) {
|
2012-12-17 19:25:10 +00:00
|
|
|
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
|
|
|
|
2014-10-28 19:06:54 +00:00
|
|
|
if (mFileManager->EnforcingQuota()) {
|
2012-12-17 19:25:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(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;
|
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|