2010-09-10 12:12:11 -07: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 04:12:37 -07: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 12:12:11 -07:00
|
|
|
|
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
#include "mozIApplicationClearPrivateDataParams.h"
|
2012-06-19 18:50:39 -07:00
|
|
|
#include "nsIAtom.h"
|
2012-06-29 09:48:34 -07:00
|
|
|
#include "nsIConsoleService.h"
|
2012-01-03 07:27:39 -08:00
|
|
|
#include "nsIDOMScriptObjectFactory.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "nsIFile.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
#include "nsIFileStorage.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "nsIObserverService.h"
|
2012-10-23 09:31:19 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2012-06-29 09:48:34 -07:00
|
|
|
#include "nsIScriptError.h"
|
2011-12-03 09:10:21 -08:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-08-26 01:21:35 -07:00
|
|
|
#include "nsISHEntry.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2010-10-19 10:58:33 -07:00
|
|
|
#include "nsITimer.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
#include "mozilla/dom/file/FileService.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2012-11-10 10:32:37 -08:00
|
|
|
#include "mozilla/dom/TabContext.h"
|
2012-01-05 05:42:47 -08:00
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2011-08-26 01:21:35 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "mozilla/Services.h"
|
2011-08-26 01:21:35 -07:00
|
|
|
#include "mozilla/storage.h"
|
2012-04-30 16:52:27 -07:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-10-23 09:31:19 -07:00
|
|
|
#include "nsCRTGlue.h"
|
2012-04-30 16:52:27 -07:00
|
|
|
#include "nsDirectoryServiceUtils.h"
|
2012-06-29 09:48:34 -07:00
|
|
|
#include "nsEventDispatcher.h"
|
2012-10-23 09:31:19 -07:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
2010-10-19 10:58:33 -07:00
|
|
|
#include "nsXPCOMPrivate.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "IDBDatabase.h"
|
2010-10-19 10:58:52 -07:00
|
|
|
#include "IDBEvents.h"
|
2010-09-10 12:12:11 -07:00
|
|
|
#include "IDBFactory.h"
|
2012-01-03 07:27:39 -08:00
|
|
|
#include "IDBKeyRange.h"
|
2011-12-15 23:34:24 -08:00
|
|
|
#include "OpenDatabaseHelper.h"
|
2010-10-19 10:58:33 -07:00
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "IndexedDatabaseInlines.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2012-12-17 11:25:10 -08:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// The amount of time, in milliseconds, that our IO thread will stay alive
|
|
|
|
// after the last event it processes.
|
|
|
|
#define DEFAULT_THREAD_TIMEOUT_MS 30000
|
|
|
|
|
|
|
|
// The amount of time, in milliseconds, that we will wait for active database
|
|
|
|
// transactions on shutdown before aborting them.
|
|
|
|
#define DEFAULT_SHUTDOWN_TIMER_MS 30000
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2011-08-26 01:21:35 -07:00
|
|
|
// Amount of space that IndexedDB databases may use by default in megabytes.
|
|
|
|
#define DEFAULT_QUOTA_MB 50
|
|
|
|
|
|
|
|
// Preference that users can set to override DEFAULT_QUOTA_MB
|
|
|
|
#define PREF_INDEXEDDB_QUOTA "dom.indexedDB.warningQuota"
|
|
|
|
|
2012-06-07 10:44:17 -07:00
|
|
|
// profile-before-change, when we need to shut down IDB
|
|
|
|
#define PROFILE_BEFORE_CHANGE_OBSERVER_ID "profile-before-change"
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
using namespace mozilla::services;
|
2012-11-10 10:32:37 -08:00
|
|
|
using namespace mozilla::dom;
|
2011-08-26 01:21:35 -07:00
|
|
|
using mozilla::Preferences;
|
2012-06-03 09:33:52 -07:00
|
|
|
using mozilla::dom::file::FileService;
|
2012-12-17 11:25:10 -08:00
|
|
|
using mozilla::dom::quota::QuotaManager;
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-01-03 07:27:39 -08:00
|
|
|
static NS_DEFINE_CID(kDOMSOF_CID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
namespace {
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t gShutdown = 0;
|
|
|
|
int32_t gClosed = 0;
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// Does not hold a reference.
|
2012-07-30 07:20:58 -07:00
|
|
|
IndexedDatabaseManager* gInstance = nullptr;
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t gIndexedDBQuotaMB = DEFAULT_QUOTA_MB;
|
2011-08-26 01:21:35 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
bool
|
2012-08-24 11:51:33 -07:00
|
|
|
GetDatabaseBaseFilename(const nsAString& aFilename,
|
|
|
|
nsAString& aDatabaseBaseFilename)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aFilename.IsEmpty(), "Bad argument!");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(sqlite, ".sqlite");
|
|
|
|
nsAString::size_type filenameLen = aFilename.Length();
|
|
|
|
nsAString::size_type sqliteLen = sqlite.Length();
|
|
|
|
|
|
|
|
if (sqliteLen > filenameLen ||
|
|
|
|
Substring(aFilename, filenameLen - sqliteLen, sqliteLen) != sqlite) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
aDatabaseBaseFilename = Substring(aFilename, 0, filenameLen - sqliteLen);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
// Adds all databases in the hash to the given array.
|
2012-06-03 09:33:52 -07:00
|
|
|
template <class T>
|
2010-09-10 12:12:11 -07:00
|
|
|
PLDHashOperator
|
|
|
|
EnumerateToTArray(const nsACString& aKey,
|
|
|
|
nsTArray<IDBDatabase*>* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aKey.IsEmpty(), "Empty key!");
|
|
|
|
NS_ASSERTION(aValue, "Null pointer!");
|
|
|
|
NS_ASSERTION(aUserArg, "Null pointer!");
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
static_cast<nsTArray<T>*>(aUserArg)->AppendElements(*aValue);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PatternMatchesOrigin(const nsACString& aPatternString, const nsACString& aOrigin)
|
|
|
|
{
|
|
|
|
// Aren't we smart!
|
|
|
|
return StringBeginsWith(aOrigin, aPatternString);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum MozBrowserPatternFlag
|
|
|
|
{
|
|
|
|
MozBrowser = 0,
|
|
|
|
NotMozBrowser,
|
|
|
|
IgnoreMozBrowser
|
|
|
|
};
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
// Use one of the friendly overloads below.
|
|
|
|
void
|
|
|
|
GetOriginPatternString(uint32_t aAppId, MozBrowserPatternFlag aBrowserFlag,
|
|
|
|
const nsACString& aOrigin, nsAutoCString& _retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID,
|
|
|
|
"Bad appId!");
|
|
|
|
NS_ASSERTION(aOrigin.IsEmpty() || aBrowserFlag != IgnoreMozBrowser,
|
|
|
|
"Bad args!");
|
|
|
|
|
|
|
|
if (aOrigin.IsEmpty()) {
|
|
|
|
_retval.Truncate();
|
|
|
|
|
|
|
|
_retval.AppendInt(aAppId);
|
|
|
|
_retval.Append('+');
|
|
|
|
|
|
|
|
if (aBrowserFlag != IgnoreMozBrowser) {
|
|
|
|
if (aBrowserFlag == MozBrowser) {
|
|
|
|
_retval.Append('t');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_retval.Append('f');
|
|
|
|
}
|
|
|
|
_retval.Append('+');
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (aAppId != nsIScriptSecurityManager::NO_APP_ID ||
|
|
|
|
aBrowserFlag == MozBrowser) {
|
|
|
|
nsAutoCString pattern;
|
|
|
|
GetOriginPatternString(aAppId, aBrowserFlag, EmptyCString(), pattern);
|
|
|
|
NS_ASSERTION(PatternMatchesOrigin(pattern, aOrigin),
|
|
|
|
"Origin doesn't match parameters!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_retval = aOrigin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetOriginPatternString(uint32_t aAppId, bool aBrowserOnly,
|
|
|
|
const nsACString& aOrigin, nsAutoCString& _retval)
|
|
|
|
{
|
|
|
|
return GetOriginPatternString(aAppId,
|
|
|
|
aBrowserOnly ? MozBrowser : NotMozBrowser,
|
|
|
|
aOrigin, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetOriginPatternStringMaybeIgnoreBrowser(uint32_t aAppId, bool aBrowserOnly,
|
|
|
|
nsAutoCString& _retval)
|
|
|
|
{
|
|
|
|
return GetOriginPatternString(aAppId,
|
|
|
|
aBrowserOnly ? MozBrowser : IgnoreMozBrowser,
|
|
|
|
EmptyCString(), _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ValueType>
|
|
|
|
class PatternMatchArray : public nsAutoTArray<ValueType, 20>
|
|
|
|
{
|
|
|
|
typedef PatternMatchArray<ValueType> SelfType;
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
Closure(SelfType& aSelf, const nsACString& aPattern)
|
|
|
|
: mSelf(aSelf), mPattern(aPattern)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
SelfType& mSelf;
|
|
|
|
const nsACString& mPattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <class T>
|
|
|
|
void
|
|
|
|
Find(const T& aHashtable,
|
|
|
|
const nsACString& aPattern)
|
|
|
|
{
|
|
|
|
SelfType::Clear();
|
|
|
|
|
|
|
|
Closure closure(*this, aPattern);
|
|
|
|
aHashtable.EnumerateRead(SelfType::Enumerate, &closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static PLDHashOperator
|
|
|
|
Enumerate(const nsACString& aKey,
|
|
|
|
nsTArray<ValueType>* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aKey.IsEmpty(), "Empty key!");
|
|
|
|
NS_ASSERTION(aValue, "Null pointer!");
|
|
|
|
NS_ASSERTION(aUserArg, "Null pointer!");
|
|
|
|
|
|
|
|
Closure* closure = static_cast<Closure*>(aUserArg);
|
|
|
|
|
|
|
|
if (PatternMatchesOrigin(closure->mPattern, aKey)) {
|
|
|
|
closure->mSelf.AppendElements(*aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef PatternMatchArray<IDBDatabase*> DatabasePatternMatchArray;
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
PLDHashOperator
|
2012-10-23 09:31:19 -07:00
|
|
|
InvalidateAndRemoveFileManagers(
|
|
|
|
const nsACString& aKey,
|
|
|
|
nsAutoPtr<nsTArray<nsRefPtr<FileManager> > >& aValue,
|
|
|
|
void* aUserArg)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aKey.IsEmpty(), "Empty key!");
|
|
|
|
NS_ASSERTION(aValue, "Null pointer!");
|
|
|
|
|
2012-10-23 09:31:19 -07: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-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
void
|
|
|
|
SanitizeOriginString(nsCString& aOrigin)
|
|
|
|
{
|
|
|
|
// We want profiles to be platform-independent so we always need to replace
|
|
|
|
// the same characters on every platform. Windows has the most extensive set
|
|
|
|
// of illegal characters so we use its FILE_ILLEGAL_CHARACTERS and
|
|
|
|
// FILE_PATH_SEPARATOR.
|
|
|
|
static const char kReplaceChars[] = CONTROL_CHARACTERS "/:*?\"<>|\\";
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
NS_ASSERTION(!strcmp(kReplaceChars,
|
|
|
|
FILE_ILLEGAL_CHARACTERS FILE_PATH_SEPARATOR),
|
|
|
|
"Illegal file characters have changed!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
aOrigin.ReplaceChar(kReplaceChars, '+');
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetASCIIOriginFromURI(nsIURI* aURI,
|
|
|
|
uint32_t aAppId,
|
|
|
|
bool aInMozBrowser,
|
|
|
|
nsACString& aOrigin)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "Null uri!");
|
|
|
|
|
|
|
|
nsCString origin;
|
|
|
|
mozilla::GetExtendedOrigin(aURI, aAppId, aInMozBrowser, origin);
|
|
|
|
|
|
|
|
if (origin.IsEmpty()) {
|
|
|
|
NS_WARNING("GetExtendedOrigin returned empty string!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aOrigin.Assign(origin);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetASCIIOriginFromPrincipal(nsIPrincipal* aPrincipal,
|
|
|
|
nsACString& aOrigin)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aPrincipal, "Don't hand me a null principal!");
|
|
|
|
|
|
|
|
static const char kChromeOrigin[] = "chrome";
|
|
|
|
|
|
|
|
nsCString origin;
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(aPrincipal)) {
|
|
|
|
origin.AssignLiteral(kChromeOrigin);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bool isNullPrincipal;
|
|
|
|
nsresult rv = aPrincipal->GetIsNullPrincipal(&isNullPrincipal);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (isNullPrincipal) {
|
|
|
|
NS_WARNING("IndexedDB not supported from this principal!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aPrincipal->GetExtendedOrigin(origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (origin.EqualsLiteral(kChromeOrigin)) {
|
|
|
|
NS_WARNING("Non-chrome principal can't use chrome origin!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aOrigin.Assign(origin);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
IndexedDatabaseManager::IndexedDatabaseManager()
|
2012-12-19 09:45:57 -08:00
|
|
|
: mFileMutex("IndexedDatabaseManager.mFileMutex")
|
2010-09-10 12:12:11 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!gInstance, "More than one instance!");
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDatabaseManager::~IndexedDatabaseManager()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(!gInstance || gInstance == this, "Different instances!");
|
2012-07-30 07:20:58 -07:00
|
|
|
gInstance = nullptr;
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
bool IndexedDatabaseManager::sIsMainProcess = false;
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// static
|
|
|
|
already_AddRefed<IndexedDatabaseManager>
|
|
|
|
IndexedDatabaseManager::GetOrCreate()
|
2010-09-10 12:12:11 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 10:58:42 -07:00
|
|
|
if (IsShuttingDown()) {
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ERROR("Calling GetOrCreateInstance() after shutdown!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
nsRefPtr<IndexedDatabaseManager> instance(gInstance);
|
|
|
|
|
|
|
|
if (!instance) {
|
2012-06-01 10:21:12 -07:00
|
|
|
sIsMainProcess = XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-08-26 01:21:35 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
instance = new IndexedDatabaseManager();
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
instance->mLiveDatabases.Init();
|
|
|
|
instance->mFileManagers.Init();
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
nsresult rv;
|
2012-04-30 16:52:27 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (sIsMainProcess) {
|
|
|
|
nsCOMPtr<nsIFile> dbBaseDirectory;
|
2012-08-01 23:02:29 -07:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_INDEXEDDB_PARENT_DIR,
|
|
|
|
getter_AddRefs(dbBaseDirectory));
|
2012-06-21 09:39:12 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-01 23:02:29 -07:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(dbBaseDirectory));
|
2012-06-21 09:39:12 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2012-04-30 16:52:27 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
rv = dbBaseDirectory->Append(NS_LITERAL_STRING("indexedDB"));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
rv = dbBaseDirectory->GetPath(instance->mDatabaseBasePath);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2012-04-30 16:52:27 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// Make a lazy thread for any IO we need (like clearing or enumerating the
|
|
|
|
// contents of indexedDB database directories).
|
2012-08-01 23:02:29 -07:00
|
|
|
instance->mIOThread =
|
|
|
|
new LazyIdleThread(DEFAULT_THREAD_TIMEOUT_MS,
|
|
|
|
NS_LITERAL_CSTRING("IndexedDB I/O"),
|
|
|
|
LazyIdleThread::ManualShutdown);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
// Make a timer here to avoid potential failures later. We don't actually
|
|
|
|
// initialize the timer until shutdown.
|
|
|
|
instance->mShutdownTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(instance->mShutdownTimer, nullptr);
|
2012-06-01 10:21:12 -07:00
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-12-19 09:45:57 -08:00
|
|
|
// Make sure that the quota manager is up.
|
|
|
|
NS_ENSURE_TRUE(QuotaManager::GetOrCreate(), nullptr);
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(obs, nullptr);
|
2010-10-19 10:58:33 -07:00
|
|
|
|
|
|
|
// We need this callback to know when to shut down all our threads.
|
2012-06-07 10:44:17 -07:00
|
|
|
rv = obs->AddObserver(instance, PROFILE_BEFORE_CHANGE_OBSERVER_ID, false);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (NS_FAILED(Preferences::AddIntVarCache(&gIndexedDBQuotaMB,
|
|
|
|
PREF_INDEXEDDB_QUOTA,
|
|
|
|
DEFAULT_QUOTA_MB))) {
|
|
|
|
NS_WARNING("Unable to respond to quota pref changes!");
|
|
|
|
gIndexedDBQuotaMB = DEFAULT_QUOTA_MB;
|
|
|
|
}
|
2011-08-26 01:21:35 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// The observer service will hold our last reference, don't AddRef here.
|
|
|
|
gInstance = instance;
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
return instance.forget();
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// static
|
2010-09-10 12:12:11 -07:00
|
|
|
IndexedDatabaseManager*
|
2010-10-19 10:58:33 -07:00
|
|
|
IndexedDatabaseManager::Get()
|
2010-09-10 12:12:11 -07:00
|
|
|
{
|
2010-10-19 10:58:33 -07:00
|
|
|
// Does not return an owning reference.
|
2010-09-10 12:12:11 -07:00
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// static
|
|
|
|
IndexedDatabaseManager*
|
|
|
|
IndexedDatabaseManager::FactoryCreate()
|
|
|
|
{
|
|
|
|
// Returns a raw pointer that carries an owning reference! Lame, but the
|
|
|
|
// singleton factory macros force this.
|
|
|
|
return GetOrCreate().get();
|
|
|
|
}
|
|
|
|
|
2012-04-30 16:52:27 -07:00
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::GetDirectoryForOrigin(const nsACString& aASCIIOrigin,
|
|
|
|
nsIFile** aDirectory) const
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> directory =
|
2012-04-30 16:52:27 -07:00
|
|
|
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
rv = directory->InitWithPath(GetBaseDirectory());
|
2012-04-30 16:52:27 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
nsAutoCString originSanitized(aASCIIOrigin);
|
|
|
|
SanitizeOriginString(originSanitized);
|
2012-04-30 16:52:27 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
rv = directory->Append(NS_ConvertASCIItoUTF16(originSanitized));
|
2012-04-30 16:52:27 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
directory.forget(aDirectory);
|
2012-04-30 16:52:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// static
|
|
|
|
already_AddRefed<nsIAtom>
|
|
|
|
IndexedDatabaseManager::GetDatabaseId(const nsACString& aOrigin,
|
|
|
|
const nsAString& aName)
|
|
|
|
{
|
|
|
|
nsCString str(aOrigin);
|
|
|
|
str.Append("*");
|
|
|
|
str.Append(NS_ConvertUTF16toUTF8(aName));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(str);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(atom, nullptr);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
return atom.forget();
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:48:34 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> eventTarget;
|
|
|
|
rv = aVisitor.mDOMEvent->GetTarget(getter_AddRefs(eventTarget));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIDBRequest> strongRequest = do_QueryInterface(eventTarget);
|
|
|
|
IDBRequest* request = static_cast<IDBRequest*>(strongRequest.get());
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDOMError> error;
|
|
|
|
rv = request->GetError(getter_AddRefs(error));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsString errorName;
|
|
|
|
if (error) {
|
|
|
|
rv = error->GetName(errorName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptErrorEvent event(true, NS_LOAD_ERROR);
|
|
|
|
request->FillScriptErrorEvent(&event);
|
2012-09-09 16:29:12 -07:00
|
|
|
NS_ABORT_IF_FALSE(event.fileName,
|
|
|
|
"FillScriptErrorEvent should give us a non-null string "
|
|
|
|
"for our error's fileName");
|
|
|
|
|
2012-06-29 09:48:34 -07: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 16:29:12 -07:00
|
|
|
if (NS_FAILED(scriptError->InitWithWindowID(errorName,
|
|
|
|
nsDependentString(event.fileName),
|
|
|
|
EmptyString(), event.lineNr,
|
2012-07-20 04:16:17 -07:00
|
|
|
0, 0,
|
2012-06-29 09:48:34 -07: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 09:31:19 -07:00
|
|
|
// static
|
|
|
|
bool
|
2012-11-10 10:32:37 -08:00
|
|
|
IndexedDatabaseManager::TabContextMayAccessOrigin(const TabContext& aContext,
|
|
|
|
const nsACString& aOrigin)
|
2012-11-09 17:14:40 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aOrigin.IsEmpty(), "Empty origin!");
|
|
|
|
|
2012-11-10 10:32:37 -08: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-09 17:14:40 -08:00
|
|
|
nsAutoCString pattern;
|
2012-11-10 10:32:37 -08:00
|
|
|
GetOriginPatternStringMaybeIgnoreBrowser(aContext.OwnOrContainingAppId(),
|
|
|
|
aContext.IsBrowserElement(),
|
|
|
|
pattern);
|
2012-10-23 09:31:19 -07:00
|
|
|
|
2012-11-09 17:14:40 -08:00
|
|
|
return PatternMatchesOrigin(pattern, aOrigin);
|
2012-10-23 09:31:19 -07:00
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::RegisterDatabase(IDBDatabase* aDatabase)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabase, "Null pointer!");
|
|
|
|
|
|
|
|
// Don't allow any new databases to be created after shutdown.
|
2010-10-19 10:58:42 -07:00
|
|
|
if (IsShuttingDown()) {
|
2010-09-10 12:12:11 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this database to its origin array if it exists, create it otherwise.
|
|
|
|
nsTArray<IDBDatabase*>* array;
|
|
|
|
if (!mLiveDatabases.Get(aDatabase->Origin(), &array)) {
|
|
|
|
nsAutoPtr<nsTArray<IDBDatabase*> > newArray(new nsTArray<IDBDatabase*>());
|
2012-05-18 10:30:49 -07:00
|
|
|
mLiveDatabases.Put(aDatabase->Origin(), newArray);
|
2010-09-10 12:12:11 -07:00
|
|
|
array = newArray.forget();
|
|
|
|
}
|
|
|
|
if (!array->AppendElement(aDatabase)) {
|
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-19 10:58:42 -07:00
|
|
|
|
|
|
|
aDatabase->mRegistered = true;
|
2010-09-10 12:12:11 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::UnregisterDatabase(IDBDatabase* aDatabase)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabase, "Null pointer!");
|
|
|
|
|
|
|
|
// Remove this database from its origin array, maybe remove the array if it
|
|
|
|
// is then empty.
|
|
|
|
nsTArray<IDBDatabase*>* array;
|
|
|
|
if (mLiveDatabases.Get(aDatabase->Origin(), &array) &&
|
|
|
|
array->RemoveElement(aDatabase)) {
|
|
|
|
if (array->IsEmpty()) {
|
|
|
|
mLiveDatabases.Remove(aDatabase->Origin());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ERROR("Didn't know anything about this database!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::OnUsageCheckComplete(AsyncUsageRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
NS_ASSERTION(!aRunnable->mURI, "Should have been cleared!");
|
|
|
|
NS_ASSERTION(!aRunnable->mCallback, "Should have been cleared!");
|
|
|
|
|
|
|
|
if (!mUsageRunnables.RemoveElement(aRunnable)) {
|
|
|
|
NS_ERROR("Don't know anything about this runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
nsresult
|
2012-10-23 09:31:19 -07:00
|
|
|
IndexedDatabaseManager::WaitForOpenAllowed(
|
|
|
|
const OriginOrPatternString& aOriginOrPattern,
|
|
|
|
nsIAtom* aId,
|
|
|
|
nsIRunnable* aRunnable)
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-10-23 09:31:19 -07:00
|
|
|
NS_ASSERTION(!aOriginOrPattern.IsEmpty(), "Empty pattern!");
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
nsAutoPtr<SynchronizedOp> op(new SynchronizedOp(aOriginOrPattern, aId));
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
// See if this runnable needs to wait.
|
|
|
|
bool delayed = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = mSynchronizedOps.Length(); index > 0; index--) {
|
2011-11-02 05:53:12 -07:00
|
|
|
nsAutoPtr<SynchronizedOp>& existingOp = mSynchronizedOps[index - 1];
|
|
|
|
if (op->MustWaitFor(*existingOp)) {
|
|
|
|
existingOp->DelayRunnable(aRunnable);
|
|
|
|
delayed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, dispatch it immediately.
|
|
|
|
if (!delayed) {
|
|
|
|
nsresult rv = NS_DispatchToCurrentThread(aRunnable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
// Adding this to the synchronized ops list will block any additional
|
|
|
|
// ops from proceeding until this one is done.
|
|
|
|
mSynchronizedOps.AppendElement(op.forget());
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
void
|
2012-10-23 09:31:19 -07:00
|
|
|
IndexedDatabaseManager::AllowNextSynchronizedOp(
|
|
|
|
const OriginOrPatternString& aOriginOrPattern,
|
|
|
|
nsIAtom* aId)
|
2010-09-10 12:12:11 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-10-23 09:31:19 -07:00
|
|
|
NS_ASSERTION(!aOriginOrPattern.IsEmpty(), "Empty origin/pattern!");
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mSynchronizedOps.Length();
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2011-11-02 05:53:12 -07:00
|
|
|
nsAutoPtr<SynchronizedOp>& op = mSynchronizedOps[index];
|
2012-10-23 09:31:19 -07:00
|
|
|
if (op->mOriginOrPattern.IsOrigin() == aOriginOrPattern.IsOrigin() &&
|
|
|
|
op->mOriginOrPattern == aOriginOrPattern) {
|
2011-11-02 05:53:12 -07:00
|
|
|
if (op->mId == aId) {
|
|
|
|
NS_ASSERTION(op->mDatabases.IsEmpty(), "How did this happen?");
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
op->DispatchDelayedRunnables();
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
mSynchronizedOps.RemoveElementAt(index);
|
|
|
|
return;
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// If one or the other is for an origin clear, we should have matched
|
|
|
|
// solely on origin.
|
|
|
|
NS_ASSERTION(op->mId && aId, "Why didn't we match earlier?");
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
NS_NOTREACHED("Why didn't we find a SynchronizedOp?");
|
2010-10-19 10:58:42 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsresult
|
2012-06-19 18:50:39 -07:00
|
|
|
IndexedDatabaseManager::AcquireExclusiveAccess(
|
2012-10-23 09:31:19 -07:00
|
|
|
const nsACString& aPattern,
|
2012-06-19 18:50:39 -07:00
|
|
|
IDBDatabase* aDatabase,
|
|
|
|
AsyncConnectionHelper* aHelper,
|
|
|
|
nsIRunnable* aRunnable,
|
|
|
|
WaitingOnDatabasesCallback aCallback,
|
|
|
|
void* aClosure)
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_ASSERTION(!aDatabase || aHelper, "Need a helper with a database!");
|
|
|
|
NS_ASSERTION(aDatabase || aRunnable, "Need a runnable without a database!");
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Find the right SynchronizedOp.
|
2012-06-19 18:50:39 -07:00
|
|
|
SynchronizedOp* op =
|
2012-10-23 09:31:19 -07:00
|
|
|
FindSynchronizedOp(aPattern, aDatabase ? aDatabase->Id() : nullptr);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
NS_ASSERTION(op, "We didn't find a SynchronizedOp?");
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_ASSERTION(!op->mHelper, "SynchronizedOp already has a helper?!?");
|
|
|
|
NS_ASSERTION(!op->mRunnable, "SynchronizedOp already has a runnable?!?");
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
DatabasePatternMatchArray matches;
|
|
|
|
matches.Find(mLiveDatabases, aPattern);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// We need to wait for the databases to go away.
|
2010-10-19 10:58:52 -07:00
|
|
|
// Hold on to all database objects that represent the same database file
|
|
|
|
// (except the one that is requesting this version change).
|
|
|
|
nsTArray<nsRefPtr<IDBDatabase> > liveDatabases;
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
if (!matches.IsEmpty()) {
|
2012-06-19 18:50:39 -07:00
|
|
|
if (aDatabase) {
|
|
|
|
// Grab all databases that are not yet closed but whose database id match
|
|
|
|
// the one we're looking for.
|
2012-10-23 09:31:19 -07:00
|
|
|
for (uint32_t index = 0; index < matches.Length(); index++) {
|
|
|
|
IDBDatabase*& database = matches[index];
|
2012-06-19 18:50:39 -07:00
|
|
|
if (!database->IsClosed() &&
|
2011-11-02 05:53:12 -07:00
|
|
|
database != aDatabase &&
|
2012-06-19 18:50:39 -07:00
|
|
|
database->Id() == aDatabase->Id()) {
|
|
|
|
liveDatabases.AppendElement(database);
|
|
|
|
}
|
2011-11-02 05:53:12 -07:00
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
2012-06-19 18:50:39 -07:00
|
|
|
else {
|
|
|
|
// We want *all* databases, even those that are closed, if we're going to
|
|
|
|
// clear the origin.
|
2012-10-23 09:31:19 -07:00
|
|
|
liveDatabases.AppendElements(matches);
|
2012-06-19 18:50:39 -07:00
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
op->mHelper = aHelper;
|
2012-06-19 18:50:39 -07:00
|
|
|
op->mRunnable = aRunnable;
|
|
|
|
|
|
|
|
if (!liveDatabases.IsEmpty()) {
|
|
|
|
NS_ASSERTION(op->mDatabases.IsEmpty(),
|
|
|
|
"How do we already have databases here?");
|
|
|
|
op->mDatabases.AppendElements(liveDatabases);
|
|
|
|
|
|
|
|
// Give our callback the databases so it can decide what to do with them.
|
|
|
|
aCallback(liveDatabases, aClosure);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_ASSERTION(liveDatabases.IsEmpty(),
|
|
|
|
"Should have done something with the array!");
|
|
|
|
|
|
|
|
if (aDatabase) {
|
|
|
|
// Wait for those databases to close.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're trying to open a database and nothing blocks it, or if we're
|
|
|
|
// clearing an origin, then go ahead and schedule the op.
|
|
|
|
nsresult rv = RunSynchronizedOp(aDatabase, op);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::IsShuttingDown()
|
|
|
|
{
|
|
|
|
return !!gShutdown;
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::IsClosed()
|
|
|
|
{
|
|
|
|
return !!gClosed;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void
|
2011-01-27 13:47:36 -08:00
|
|
|
IndexedDatabaseManager::AbortCloseDatabasesForWindow(nsPIDOMWindow* aWindow)
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aWindow, "Null pointer!");
|
|
|
|
|
|
|
|
nsAutoTArray<IDBDatabase*, 50> liveDatabases;
|
2012-06-03 09:33:52 -07:00
|
|
|
mLiveDatabases.EnumerateRead(EnumerateToTArray<IDBDatabase*>,
|
|
|
|
&liveDatabases);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
FileService* service = FileService::Get();
|
2011-01-27 13:47:36 -08:00
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::Get();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < liveDatabases.Length(); index++) {
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBDatabase*& database = liveDatabases[index];
|
2012-01-24 02:03:37 -08:00
|
|
|
if (database->GetOwner() == aWindow) {
|
2011-01-27 13:47:36 -08:00
|
|
|
if (NS_FAILED(database->Close())) {
|
|
|
|
NS_WARNING("Failed to close database for dying window!");
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
if (service) {
|
|
|
|
service->AbortLockedFilesForStorage(database);
|
|
|
|
}
|
|
|
|
|
2011-01-27 13:47:36 -08:00
|
|
|
if (pool) {
|
|
|
|
pool->AbortTransactionsForDatabase(database);
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 13:47:36 -08:00
|
|
|
bool
|
|
|
|
IndexedDatabaseManager::HasOpenTransactions(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aWindow, "Null pointer!");
|
|
|
|
|
|
|
|
nsAutoTArray<IDBDatabase*, 50> liveDatabases;
|
2012-06-03 09:33:52 -07:00
|
|
|
mLiveDatabases.EnumerateRead(EnumerateToTArray<IDBDatabase*>,
|
|
|
|
&liveDatabases);
|
|
|
|
|
|
|
|
FileService* service = FileService::Get();
|
2011-01-27 13:47:36 -08:00
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::Get();
|
2012-06-03 09:33:52 -07:00
|
|
|
if (!service && !pool) {
|
2011-01-27 13:47:36 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < liveDatabases.Length(); index++) {
|
2011-01-27 13:47:36 -08:00
|
|
|
IDBDatabase*& database = liveDatabases[index];
|
2012-01-24 02:03:37 -08:00
|
|
|
if (database->GetOwner() == aWindow &&
|
2012-06-03 09:33:52 -07:00
|
|
|
((service && service->HasLockedFilesForStorage(database)) ||
|
|
|
|
(pool && pool->HasTransactionsForDatabase(database)))) {
|
2011-01-27 13:47:36 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::OnDatabaseClosed(IDBDatabase* aDatabase)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabase, "Null pointer!");
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Check through the list of SynchronizedOps to see if any are waiting for
|
|
|
|
// this database to close before proceeding.
|
2012-06-19 18:50:39 -07:00
|
|
|
SynchronizedOp* op = FindSynchronizedOp(aDatabase->Origin(), aDatabase->Id());
|
|
|
|
if (op) {
|
|
|
|
// This database is in the scope of this SynchronizedOp. Remove it
|
|
|
|
// from the list if necessary.
|
|
|
|
if (op->mDatabases.RemoveElement(aDatabase)) {
|
|
|
|
// Now set up the helper if there are no more live databases.
|
|
|
|
NS_ASSERTION(op->mHelper || op->mRunnable,
|
|
|
|
"How did we get rid of the helper/runnable before "
|
|
|
|
"removing the last database?");
|
|
|
|
if (op->mDatabases.IsEmpty()) {
|
|
|
|
// At this point, all databases are closed, so no new transactions
|
|
|
|
// can be started. There may, however, still be outstanding
|
|
|
|
// transactions that have not completed. We need to wait for those
|
|
|
|
// before we dispatch the helper.
|
|
|
|
if (NS_FAILED(RunSynchronizedOp(aDatabase, op))) {
|
|
|
|
NS_WARNING("Failed to run synchronized op!");
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 01:21:35 -07:00
|
|
|
// static
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2011-08-26 01:21:35 -07:00
|
|
|
IndexedDatabaseManager::GetIndexedDBQuotaMB()
|
|
|
|
{
|
2013-01-15 04:22:03 -08:00
|
|
|
return uint32_t(std::max(gIndexedDBQuotaMB, 0));
|
2011-08-26 01:21:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-12-15 23:34:24 -08:00
|
|
|
IndexedDatabaseManager::EnsureOriginIsInitialized(const nsACString& aOrigin,
|
2012-08-07 21:35:48 -07:00
|
|
|
FactoryPrivilege aPrivilege,
|
2011-12-15 23:34:24 -08:00
|
|
|
nsIFile** aDirectory)
|
2011-08-26 01:21:35 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool correctThread;
|
2011-08-26 01:21:35 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mIOThread->IsOnCurrentThread(&correctThread)) &&
|
|
|
|
correctThread,
|
|
|
|
"Running on the wrong thread!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsIFile> directory;
|
2012-04-30 16:52:27 -07:00
|
|
|
nsresult rv = GetDirectoryForOrigin(aOrigin, getter_AddRefs(directory));
|
2011-08-26 01:21:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
bool exists;
|
|
|
|
rv = directory->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
bool isDirectory;
|
|
|
|
rv = directory->IsDirectory(&isDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(isDirectory, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = directory->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
if (mInitializedOrigins.Contains(aOrigin)) {
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ADDREF(*aDirectory = directory);
|
|
|
|
return NS_OK;
|
2011-08-26 01:21:35 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
// We need to see if there are any files in the directory already. If they
|
2012-08-24 11:51:33 -07:00
|
|
|
// are database files then we need to cleanup stored files (if it's needed)
|
2012-12-17 11:25:10 -08:00
|
|
|
// and also initialize the quota.
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
nsAutoTArray<nsString, 20> subdirsToProcess;
|
2011-12-15 23:34:24 -08:00
|
|
|
nsAutoTArray<nsCOMPtr<nsIFile> , 20> unknownFiles;
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
uint64_t usage = 0;
|
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
nsTHashtable<nsStringHashKey> validSubdirs;
|
2012-05-18 10:30:49 -07:00
|
|
|
validSubdirs.Init(20);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> entries;
|
|
|
|
rv = directory->GetDirectoryEntries(getter_AddRefs(entries));
|
2011-08-26 01:21:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED((rv = entries->HasMoreElements(&hasMore))) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
rv = entries->GetNext(getter_AddRefs(entry));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(entry);
|
|
|
|
NS_ENSURE_TRUE(file, NS_NOINTERFACE);
|
|
|
|
|
|
|
|
nsString leafName;
|
|
|
|
rv = file->GetLeafName(leafName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (StringEndsWith(leafName, NS_LITERAL_STRING(".sqlite-journal"))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isDirectory;
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = file->IsDirectory(&isDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (isDirectory) {
|
2011-12-20 03:23:27 -08:00
|
|
|
if (!validSubdirs.GetEntry(leafName)) {
|
|
|
|
subdirsToProcess.AppendElement(leafName);
|
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString dbBaseFilename;
|
2012-08-24 11:51:33 -07:00
|
|
|
if (!GetDatabaseBaseFilename(leafName, dbBaseFilename)) {
|
2011-12-15 23:34:24 -08:00
|
|
|
unknownFiles.AppendElement(file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
nsCOMPtr<nsIFile> fmDirectory;
|
|
|
|
rv = directory->Clone(getter_AddRefs(fmDirectory));
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fmDirectory->Append(dbBaseFilename);
|
2011-08-26 01:21:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = FileManager::InitDirectory(fmDirectory, file, aOrigin);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-07 21:35:48 -07:00
|
|
|
if (aPrivilege != Chrome) {
|
2012-12-17 11:25:10 -08:00
|
|
|
uint64_t fileUsage;
|
|
|
|
rv = FileManager::GetUsage(fmDirectory, &fileUsage);
|
2012-08-01 14:09:23 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-17 11:25:10 -08:00
|
|
|
|
|
|
|
IncrementUsage(&usage, fileUsage);
|
|
|
|
|
|
|
|
int64_t fileSize;
|
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
IncrementUsage(&usage, uint64_t(fileSize));
|
2012-08-01 14:09:23 -07:00
|
|
|
}
|
2011-12-20 03:23:27 -08:00
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
validSubdirs.PutEntry(dbBaseFilename);
|
2011-08-26 01:21:35 -07:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-26 01:21:35 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < subdirsToProcess.Length(); i++) {
|
2011-12-20 03:23:27 -08:00
|
|
|
const nsString& subdir = subdirsToProcess[i];
|
|
|
|
if (!validSubdirs.GetEntry(subdir)) {
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_WARNING("Unknown subdirectory found!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 01:21:35 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < unknownFiles.Length(); i++) {
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsIFile>& unknownFile = unknownFiles[i];
|
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
// Some temporary SQLite files could disappear, so we have to check if the
|
|
|
|
// unknown file still exists.
|
2011-12-15 23:34:24 -08:00
|
|
|
bool exists;
|
|
|
|
rv = unknownFile->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
nsString leafName;
|
|
|
|
unknownFile->GetLeafName(leafName);
|
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
// The journal file may exists even after db has been correctly opened.
|
2011-12-15 23:34:24 -08:00
|
|
|
if (!StringEndsWith(leafName, NS_LITERAL_STRING(".sqlite-journal"))) {
|
|
|
|
NS_WARNING("Unknown file found!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
if (aPrivilege != Chrome) {
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
|
|
|
quotaManager->InitQuotaForOrigin(aOrigin, GetIndexedDBQuotaMB(), usage);
|
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
mInitializedOrigins.AppendElement(aOrigin);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
NS_ADDREF(*aDirectory = directory);
|
|
|
|
return NS_OK;
|
2011-08-26 01:21:35 -07:00
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
void
|
|
|
|
IndexedDatabaseManager::UninitializeOriginsByPattern(
|
|
|
|
const nsACString& aPattern)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
bool correctThread;
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mIOThread->IsOnCurrentThread(&correctThread)) &&
|
|
|
|
correctThread,
|
|
|
|
"Running on the wrong thread!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int32_t i = mInitializedOrigins.Length() - 1; i >= 0; i--) {
|
|
|
|
if (PatternMatchesOrigin(aPattern, mInitializedOrigins[i])) {
|
|
|
|
mInitializedOrigins.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-03 09:10:21 -08:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::GetASCIIOriginFromWindow(nsPIDOMWindow* aWindow,
|
|
|
|
nsCString& aASCIIOrigin)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"We're about to touch a window off the main thread!");
|
|
|
|
|
2012-01-03 07:27:39 -08:00
|
|
|
if (!aWindow) {
|
|
|
|
aASCIIOrigin.AssignLiteral("chrome");
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(nsContentUtils::IsCallerChrome(),
|
2012-01-03 07:27:39 -08:00
|
|
|
"Null window but not chrome!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-03 09:10:21 -08:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
|
|
|
|
NS_ENSURE_TRUE(sop, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
NS_ENSURE_TRUE(principal, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
nsresult rv = GetASCIIOriginFromPrincipal(principal, aASCIIOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-03 09:10:21 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
//static
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
already_AddRefed<FileManager>
|
|
|
|
IndexedDatabaseManager::GetFileManager(const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
|
|
|
nsTArray<nsRefPtr<FileManager> >* array;
|
|
|
|
if (!mFileManagers.Get(aOrigin, &array)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-12-20 03:23:27 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < array->Length(); i++) {
|
2011-12-20 03:23:27 -08:00
|
|
|
nsRefPtr<FileManager>& fileManager = array->ElementAt(i);
|
|
|
|
|
|
|
|
if (fileManager->DatabaseName().Equals(aDatabaseName)) {
|
|
|
|
nsRefPtr<FileManager> result = fileManager;
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-12-20 03:23:27 -08:00
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
void
|
2012-12-17 11:25:10 -08:00
|
|
|
IndexedDatabaseManager::AddFileManager(FileManager* aFileManager)
|
2012-08-24 11:51:33 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aFileManager, "Null file manager!");
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<FileManager> >* array;
|
2012-12-17 11:25:10 -08:00
|
|
|
if (!mFileManagers.Get(aFileManager->Origin(), &array)) {
|
2012-08-24 11:51:33 -07:00
|
|
|
array = new nsTArray<nsRefPtr<FileManager> >();
|
2012-12-17 11:25:10 -08:00
|
|
|
mFileManagers.Put(aFileManager->Origin(), array);
|
2012-08-24 11:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
array->AppendElement(aFileManager);
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
void
|
2012-10-23 09:31:19 -07:00
|
|
|
IndexedDatabaseManager::InvalidateFileManagersForPattern(
|
|
|
|
const nsACString& aPattern)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
2012-10-23 09:31:19 -07:00
|
|
|
NS_ASSERTION(!aPattern.IsEmpty(), "Empty pattern!");
|
|
|
|
mFileManagers.Enumerate(InvalidateAndRemoveFileManagers,
|
|
|
|
const_cast<nsACString*>(&aPattern));
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::InvalidateFileManager(const nsACString& aOrigin,
|
|
|
|
const nsAString& aDatabaseName)
|
|
|
|
{
|
|
|
|
nsTArray<nsRefPtr<FileManager> >* array;
|
|
|
|
if (!mFileManagers.Get(aOrigin, &array)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < array->Length(); i++) {
|
2011-12-15 23:34:24 -08:00
|
|
|
nsRefPtr<FileManager> fileManager = array->ElementAt(i);
|
|
|
|
if (fileManager->DatabaseName().Equals(aDatabaseName)) {
|
|
|
|
fileManager->Invalidate();
|
|
|
|
array->RemoveElementAt(i);
|
|
|
|
|
|
|
|
if (array->IsEmpty()) {
|
|
|
|
mFileManagers.Remove(aOrigin);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::AsyncDeleteFile(FileManager* aFileManager,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aFileId)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aFileManager);
|
|
|
|
|
|
|
|
// See if we're currently clearing the databases for this origin. If so then
|
|
|
|
// we pretend that we've already deleted everything.
|
|
|
|
if (IsClearOriginPending(aFileManager->Origin())) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<AsyncDeleteFileRunnable> runnable =
|
2012-08-24 11:51:33 -07:00
|
|
|
new AsyncDeleteFileRunnable(aFileManager, aFileId);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
nsresult rv = mIOThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// static
|
|
|
|
nsresult
|
2012-06-19 18:50:39 -07:00
|
|
|
IndexedDatabaseManager::RunSynchronizedOp(IDBDatabase* aDatabase,
|
|
|
|
SynchronizedOp* aOp)
|
2011-11-02 05:53:12 -07:00
|
|
|
{
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aOp, "Null pointer!");
|
|
|
|
NS_ASSERTION(!aDatabase || aOp->mHelper, "No helper on this op!");
|
|
|
|
NS_ASSERTION(aDatabase || aOp->mRunnable, "No runnable on this op!");
|
|
|
|
NS_ASSERTION(!aDatabase || aOp->mDatabases.IsEmpty(),
|
|
|
|
"This op isn't ready to run!");
|
|
|
|
|
|
|
|
FileService* service = FileService::Get();
|
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::Get();
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2012-08-10 09:15:02 -07:00
|
|
|
nsTArray<IDBDatabase*> databases;
|
2012-06-19 18:50:39 -07:00
|
|
|
if (aDatabase) {
|
|
|
|
if (service || pool) {
|
|
|
|
databases.AppendElement(aDatabase);
|
|
|
|
}
|
2011-11-02 05:53:12 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-06-19 18:50:39 -07:00
|
|
|
aOp->mDatabases.SwapElements(databases);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t waitCount = service && pool && !databases.IsEmpty() ? 2 : 1;
|
2012-06-19 18:50:39 -07:00
|
|
|
|
|
|
|
nsRefPtr<WaitForTransactionsToFinishRunnable> runnable =
|
|
|
|
new WaitForTransactionsToFinishRunnable(aOp, waitCount);
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
// There's no point in delaying if we don't yet have a transaction thread pool
|
|
|
|
// or a file service. Also, if we're not waiting on any databases then we can
|
|
|
|
// also run immediately.
|
|
|
|
if (!(service || pool) || databases.IsEmpty()) {
|
|
|
|
nsresult rv = runnable->Run();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-11-02 05:53:12 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
// Ask each service to call us back when they're done with this database.
|
|
|
|
if (service) {
|
|
|
|
// Have to copy here in case the pool needs a list too.
|
|
|
|
nsTArray<nsCOMPtr<nsIFileStorage> > array;
|
|
|
|
array.AppendElements(databases);
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
if (!service->WaitForAllStoragesToComplete(array, runnable)) {
|
|
|
|
NS_WARNING("Failed to wait for storages to complete!");
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-11-02 05:53:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
if (pool && !pool->WaitForAllDatabasesToComplete(databases, runnable)) {
|
|
|
|
NS_WARNING("Failed to wait for databases to complete!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-11-02 05:53:12 -07:00
|
|
|
}
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
IndexedDatabaseManager::SynchronizedOp*
|
|
|
|
IndexedDatabaseManager::FindSynchronizedOp(const nsACString& aPattern,
|
|
|
|
nsIAtom* aId)
|
|
|
|
{
|
|
|
|
for (uint32_t index = 0; index < mSynchronizedOps.Length(); index++) {
|
|
|
|
const nsAutoPtr<SynchronizedOp>& currentOp = mSynchronizedOps[index];
|
|
|
|
if (PatternMatchesOrigin(aPattern, currentOp->mOriginOrPattern) &&
|
|
|
|
(!currentOp->mId || currentOp->mId == aId)) {
|
|
|
|
return currentOp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::ClearDatabasesForApp(uint32_t aAppId, bool aBrowserOnly)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID,
|
|
|
|
"Bad appId!");
|
|
|
|
|
|
|
|
// This only works from the main process.
|
|
|
|
NS_ENSURE_TRUE(IsMainProcess(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsAutoCString pattern;
|
|
|
|
GetOriginPatternStringMaybeIgnoreBrowser(aAppId, aBrowserOnly, pattern);
|
|
|
|
|
|
|
|
// If there is a pending or running clear operation for this app, return
|
|
|
|
// immediately.
|
|
|
|
if (IsClearOriginPending(pattern)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
OriginOrPatternString oops = OriginOrPatternString::FromPattern(pattern);
|
|
|
|
|
|
|
|
// Queue up the origin clear runnable.
|
|
|
|
nsRefPtr<OriginClearRunnable> runnable = new OriginClearRunnable(oops);
|
|
|
|
|
|
|
|
nsresult rv = WaitForOpenAllowed(oops, nullptr, runnable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
runnable->AdvanceState();
|
|
|
|
|
|
|
|
// Give the runnable some help by invalidating any databases in the way.
|
|
|
|
DatabasePatternMatchArray matches;
|
|
|
|
matches.Find(mLiveDatabases, pattern);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < matches.Length(); index++) {
|
|
|
|
// We need to grab references here to prevent the database from dying while
|
|
|
|
// we invalidate it.
|
|
|
|
nsRefPtr<IDBDatabase> database = matches[index];
|
|
|
|
database->Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_IMPL_ISUPPORTS2(IndexedDatabaseManager, nsIIndexedDatabaseManager,
|
|
|
|
nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-19 10:58:33 -07:00
|
|
|
IndexedDatabaseManager::GetUsageForURI(
|
|
|
|
nsIURI* aURI,
|
2012-10-23 09:31:19 -07:00
|
|
|
nsIIndexedDatabaseUsageCallback* aCallback,
|
|
|
|
uint32_t aAppId,
|
|
|
|
bool aInMozBrowserOnly,
|
|
|
|
uint8_t aOptionalArgCount)
|
2010-09-10 12:12:11 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
// This only works from the main process.
|
|
|
|
NS_ENSURE_TRUE(IsMainProcess(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
if (!aOptionalArgCount) {
|
|
|
|
aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
// Figure out which origin we're dealing with.
|
|
|
|
nsCString origin;
|
2012-10-23 09:31:19 -07:00
|
|
|
nsresult rv = GetASCIIOriginFromURI(aURI, aAppId, aInMozBrowserOnly, origin);
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
OriginOrPatternString oops = OriginOrPatternString::FromOrigin(origin);
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
nsRefPtr<AsyncUsageRunnable> runnable =
|
2012-10-23 09:31:19 -07:00
|
|
|
new AsyncUsageRunnable(aAppId, aInMozBrowserOnly, oops, aURI, aCallback);
|
2010-10-19 10:58:33 -07:00
|
|
|
|
|
|
|
nsRefPtr<AsyncUsageRunnable>* newRunnable =
|
|
|
|
mUsageRunnables.AppendElement(runnable);
|
|
|
|
NS_ENSURE_TRUE(newRunnable, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
// Otherwise put the computation runnable in the queue.
|
2012-10-23 09:31:19 -07:00
|
|
|
rv = WaitForOpenAllowed(oops, nullptr, runnable);
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
runnable->AdvanceState();
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::CancelGetUsageForURI(
|
|
|
|
nsIURI* aURI,
|
2012-10-23 09:31:19 -07:00
|
|
|
nsIIndexedDatabaseUsageCallback* aCallback,
|
|
|
|
uint32_t aAppId,
|
|
|
|
bool aInMozBrowserOnly,
|
|
|
|
uint8_t aOptionalArgCount)
|
2010-10-19 10:58:33 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
// This only works from the main process.
|
|
|
|
NS_ENSURE_TRUE(IsMainProcess(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
if (!aOptionalArgCount) {
|
|
|
|
aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// See if one of our pending callbacks matches both the URI and the callback
|
|
|
|
// given. Cancel an remove it if so.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < mUsageRunnables.Length(); index++) {
|
2010-10-19 10:58:33 -07:00
|
|
|
nsRefPtr<AsyncUsageRunnable>& runnable = mUsageRunnables[index];
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
if (runnable->mAppId == aAppId &&
|
|
|
|
runnable->mInMozBrowserOnly == aInMozBrowserOnly) {
|
|
|
|
bool equals;
|
|
|
|
nsresult rv = runnable->mURI->Equals(aURI, &equals);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
if (equals && SameCOMIdentity(aCallback, runnable->mCallback)) {
|
|
|
|
runnable->Cancel();
|
|
|
|
break;
|
|
|
|
}
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-10-23 09:31:19 -07:00
|
|
|
IndexedDatabaseManager::ClearDatabasesForURI(nsIURI* aURI,
|
|
|
|
uint32_t aAppId,
|
|
|
|
bool aInMozBrowserOnly,
|
|
|
|
uint8_t aOptionalArgCount)
|
2010-09-10 12:12:11 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
// This only works from the main process.
|
|
|
|
NS_ENSURE_TRUE(IsMainProcess(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
if (!aOptionalArgCount) {
|
|
|
|
aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
// Figure out which origin we're dealing with.
|
|
|
|
nsCString origin;
|
2012-10-23 09:31:19 -07:00
|
|
|
nsresult rv = GetASCIIOriginFromURI(aURI, aAppId, aInMozBrowserOnly, origin);
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
nsAutoCString pattern;
|
|
|
|
GetOriginPatternString(aAppId, aInMozBrowserOnly, origin, pattern);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// If there is a pending or running clear operation for this origin, return
|
2010-09-10 12:12:11 -07:00
|
|
|
// immediately.
|
2012-10-23 09:31:19 -07:00
|
|
|
if (IsClearOriginPending(pattern)) {
|
2011-11-02 05:53:12 -07:00
|
|
|
return NS_OK;
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
OriginOrPatternString oops = OriginOrPatternString::FromPattern(pattern);
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Queue up the origin clear runnable.
|
2012-10-23 09:31:19 -07:00
|
|
|
nsRefPtr<OriginClearRunnable> runnable = new OriginClearRunnable(oops);
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
rv = WaitForOpenAllowed(oops, nullptr, runnable);
|
2011-11-02 05:53:12 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
runnable->AdvanceState();
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
// Give the runnable some help by invalidating any databases in the way.
|
|
|
|
DatabasePatternMatchArray matches;
|
|
|
|
matches.Find(mLiveDatabases, pattern);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
for (uint32_t index = 0; index < matches.Length(); index++) {
|
|
|
|
// We need to grab references to any live databases here to prevent them
|
|
|
|
// from dying while we invalidate them.
|
|
|
|
nsRefPtr<IDBDatabase> database = matches[index];
|
|
|
|
database->Invalidate();
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
2012-06-19 18:50:39 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// After everything has been invalidated the helper should be dispatched to
|
|
|
|
// the end of the event queue.
|
2010-09-10 12:12:11 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-06-07 10:44:17 -07:00
|
|
|
if (!strcmp(aTopic, PROFILE_BEFORE_CHANGE_OBSERVER_ID)) {
|
2010-10-19 10:58:42 -07:00
|
|
|
// Setting this flag prevents the service from being recreated and prevents
|
2010-09-10 12:12:11 -07:00
|
|
|
// further databases from being created.
|
2011-03-28 12:58:49 -07:00
|
|
|
if (PR_ATOMIC_SET(&gShutdown, 1)) {
|
2010-10-19 10:58:42 -07:00
|
|
|
NS_ERROR("Shutdown more than once?!");
|
|
|
|
}
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
if (sIsMainProcess) {
|
2012-06-03 09:33:52 -07:00
|
|
|
FileService* service = FileService::Get();
|
|
|
|
if (service) {
|
|
|
|
// This should only wait for IDB databases (file storages) to complete.
|
|
|
|
// Other file storages may still have running locked files.
|
|
|
|
// If the necko service (thread pool) gets the shutdown notification
|
|
|
|
// first then the sync loop won't be processed at all, otherwise it will
|
|
|
|
// lock the main thread until all IDB file storages are finished.
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIFileStorage> >
|
|
|
|
liveDatabases(mLiveDatabases.Count());
|
|
|
|
mLiveDatabases.EnumerateRead(
|
|
|
|
EnumerateToTArray<nsCOMPtr<nsIFileStorage> >,
|
|
|
|
&liveDatabases);
|
|
|
|
|
|
|
|
if (!liveDatabases.IsEmpty()) {
|
|
|
|
nsRefPtr<WaitForLockedFilesToFinishRunnable> runnable =
|
|
|
|
new WaitForLockedFilesToFinishRunnable();
|
|
|
|
|
|
|
|
if (!service->WaitForAllStoragesToComplete(liveDatabases,
|
|
|
|
runnable)) {
|
|
|
|
NS_WARNING("Failed to wait for databases to complete!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIThread* thread = NS_GetCurrentThread();
|
|
|
|
while (runnable->IsBusy()) {
|
|
|
|
if (!NS_ProcessNextEvent(thread)) {
|
|
|
|
NS_ERROR("Failed to process next event!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// Make sure to join with our IO thread.
|
|
|
|
if (NS_FAILED(mIOThread->Shutdown())) {
|
|
|
|
NS_WARNING("Failed to shutdown IO thread!");
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// Kick off the shutdown timer.
|
|
|
|
if (NS_FAILED(mShutdownTimer->Init(this, DEFAULT_SHUTDOWN_TIMER_MS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT))) {
|
|
|
|
NS_WARNING("Failed to initialize shutdown timer!");
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// This will spin the event loop while we wait on all the database threads
|
|
|
|
// to close. Our timer may fire during that loop.
|
|
|
|
TransactionThreadPool::Shutdown();
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// Cancel the timer regardless of whether it actually fired.
|
|
|
|
if (NS_FAILED(mShutdownTimer->Cancel())) {
|
|
|
|
NS_WARNING("Failed to cancel shutdown timer!");
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
mFileManagers.Enumerate(InvalidateAndRemoveFileManagers, nullptr);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
if (PR_ATOMIC_SET(&gClosed, 1)) {
|
|
|
|
NS_ERROR("Close more than once?!");
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC)) {
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(sIsMainProcess, "Should only happen in the main process!");
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_WARNING("Some database operations are taking longer than expected "
|
|
|
|
"during shutdown and will be aborted!");
|
|
|
|
|
|
|
|
// Grab all live databases, for all origins.
|
|
|
|
nsAutoTArray<IDBDatabase*, 50> liveDatabases;
|
2012-06-03 09:33:52 -07:00
|
|
|
mLiveDatabases.EnumerateRead(EnumerateToTArray<IDBDatabase*>,
|
|
|
|
&liveDatabases);
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
// Invalidate them all.
|
2010-09-10 12:12:11 -07:00
|
|
|
if (!liveDatabases.IsEmpty()) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = liveDatabases.Length();
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2010-10-19 10:58:33 -07:00
|
|
|
liveDatabases[index]->Invalidate();
|
2010-09-10 12:12:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-09-10 12:12:11 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
if (!strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA)) {
|
|
|
|
nsCOMPtr<mozIApplicationClearPrivateDataParams> params =
|
|
|
|
do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
uint32_t appId;
|
|
|
|
nsresult rv = params->GetAppId(&appId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool browserOnly;
|
|
|
|
rv = params->GetBrowserOnly(&browserOnly);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ClearDatabasesForApp(appId, browserOnly);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 12:12:11 -07:00
|
|
|
NS_NOTREACHED("Unknown topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(IndexedDatabaseManager::OriginClearRunnable,
|
|
|
|
nsIRunnable)
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::
|
|
|
|
OriginClearRunnable::InvalidateOpenedDatabases(
|
|
|
|
nsTArray<nsRefPtr<IDBDatabase> >& aDatabases,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<IDBDatabase> > databases;
|
|
|
|
databases.SwapElements(aDatabases);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < databases.Length(); index++) {
|
2012-06-19 18:50:39 -07:00
|
|
|
databases[index]->Invalidate();
|
|
|
|
}
|
2012-10-23 09:31:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::
|
|
|
|
OriginClearRunnable::DeleteFiles(IndexedDatabaseManager* aManager)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aManager, "Don't pass me null!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> directory =
|
|
|
|
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
rv = directory->InitWithPath(aManager->GetBaseDirectory());
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> entries;
|
2012-11-09 19:29:07 -08:00
|
|
|
if (NS_FAILED(directory->GetDirectoryEntries(getter_AddRefs(entries))) ||
|
|
|
|
!entries) {
|
2012-10-23 09:31:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString originSanitized(mOriginOrPattern);
|
|
|
|
SanitizeOriginString(originSanitized);
|
|
|
|
|
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED((rv = entries->HasMoreElements(&hasMore))) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
rv = entries->GetNext(getter_AddRefs(entry));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2012-06-19 18:50:39 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(entry);
|
|
|
|
NS_ASSERTION(file, "Don't know what this is!");
|
|
|
|
|
|
|
|
bool isDirectory;
|
|
|
|
rv = file->IsDirectory(&isDirectory);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
if (!isDirectory) {
|
|
|
|
NS_WARNING("Something in the IndexedDB directory that doesn't belong!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString leafName;
|
|
|
|
rv = file->GetLeafName(leafName);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
// Skip databases for other apps.
|
|
|
|
if (!PatternMatchesOrigin(originSanitized,
|
|
|
|
NS_ConvertUTF16toUTF8(leafName))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(file->Remove(true))) {
|
|
|
|
// This should never fail if we've closed all database connections
|
|
|
|
// correctly...
|
|
|
|
NS_ERROR("Failed to remove directory!");
|
|
|
|
}
|
2012-12-17 11:25:10 -08:00
|
|
|
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
|
|
|
quotaManager->RemoveQuotaForPattern(mOriginOrPattern);
|
|
|
|
|
|
|
|
aManager->UninitializeOriginsByPattern(mOriginOrPattern);
|
2012-10-23 09:31:19 -07:00
|
|
|
}
|
2012-06-19 18:50:39 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::OriginClearRunnable::Run()
|
|
|
|
{
|
2012-04-30 16:52:27 -07:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never fail!");
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
switch (mCallbackState) {
|
|
|
|
case Pending: {
|
|
|
|
NS_NOTREACHED("Should never get here without being dispatched!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
case OpenAllowed: {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
AdvanceState();
|
2011-01-10 16:09:56 -08:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
// Now we have to wait until the thread pool is done with all of the
|
|
|
|
// databases we care about.
|
2012-10-23 09:31:19 -07:00
|
|
|
nsresult rv = mgr->AcquireExclusiveAccess(mOriginOrPattern, this,
|
|
|
|
InvalidateOpenedDatabases,
|
|
|
|
nullptr);
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
case IO: {
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
AdvanceState();
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
DeleteFiles(mgr);
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
// Now dispatch back to the main thread.
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
return NS_OK;
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
case Complete: {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
mgr->InvalidateFileManagersForPattern(mOriginOrPattern);
|
2012-06-19 18:50:39 -07:00
|
|
|
|
|
|
|
// Tell the IndexedDatabaseManager that we're done.
|
2012-10-23 09:31:19 -07:00
|
|
|
mgr->AllowNextSynchronizedOp(mOriginOrPattern, nullptr);
|
2012-06-19 18:50:39 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown state value!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Should never get here!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
IndexedDatabaseManager::AsyncUsageRunnable::AsyncUsageRunnable(
|
2012-10-23 09:31:19 -07:00
|
|
|
uint32_t aAppId,
|
|
|
|
bool aInMozBrowserOnly,
|
|
|
|
const OriginOrPatternString& aOrigin,
|
2010-10-19 10:58:33 -07:00
|
|
|
nsIURI* aURI,
|
|
|
|
nsIIndexedDatabaseUsageCallback* aCallback)
|
|
|
|
: mURI(aURI),
|
|
|
|
mCallback(aCallback),
|
|
|
|
mUsage(0),
|
2011-12-15 23:34:24 -08:00
|
|
|
mFileUsage(0),
|
2012-10-23 09:31:19 -07:00
|
|
|
mAppId(aAppId),
|
2012-06-22 11:47:05 -07:00
|
|
|
mCanceled(0),
|
2012-10-23 09:31:19 -07:00
|
|
|
mOrigin(aOrigin),
|
|
|
|
mCallbackState(Pending),
|
|
|
|
mInMozBrowserOnly(aInMozBrowserOnly)
|
2010-10-19 10:58:33 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aURI, "Null pointer!");
|
2012-10-23 09:31:19 -07:00
|
|
|
NS_ASSERTION(aOrigin.IsOrigin(), "Expect origin only here!");
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_ASSERTION(!aOrigin.IsEmpty(), "Empty origin!");
|
|
|
|
NS_ASSERTION(aCallback, "Null pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::AsyncUsageRunnable::Cancel()
|
|
|
|
{
|
2011-03-28 12:58:49 -07:00
|
|
|
if (PR_ATOMIC_SET(&mCanceled, 1)) {
|
2010-10-19 10:58:33 -07:00
|
|
|
NS_ERROR("Canceled more than once?!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::AsyncUsageRunnable::TakeShortcut()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mCallbackState == Pending, "Huh?");
|
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToCurrentThread(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCallbackState = Shortcut;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::AsyncUsageRunnable::RunInternal()
|
|
|
|
{
|
2012-04-30 16:52:27 -07:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never fail!");
|
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
switch (mCallbackState) {
|
|
|
|
case Pending: {
|
|
|
|
NS_NOTREACHED("Should never get here without being dispatched!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
case OpenAllowed: {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
AdvanceState();
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
if (NS_FAILED(mgr->IOThread()->Dispatch(this, NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch to the IO thread!");
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
case IO: {
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
AdvanceState();
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2012-06-22 11:47:05 -07:00
|
|
|
// Get the directory that contains all the database files we care about.
|
|
|
|
nsCOMPtr<nsIFile> directory;
|
|
|
|
nsresult rv = mgr->GetDirectoryForOrigin(mOrigin, getter_AddRefs(directory));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool exists;
|
|
|
|
rv = directory->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If the directory exists then enumerate all the files inside, adding up the
|
|
|
|
// sizes to get the final usage statistic.
|
|
|
|
if (exists && !mCanceled) {
|
|
|
|
rv = GetUsageForDirectory(directory, &mUsage);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run dispatches us back to the main thread.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Complete: // Fall through
|
|
|
|
case Shortcut: {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
// Call the callback unless we were canceled.
|
|
|
|
if (!mCanceled) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t usage = mUsage;
|
2012-06-22 11:47:05 -07:00
|
|
|
IncrementUsage(&usage, mFileUsage);
|
2012-10-23 09:31:19 -07:00
|
|
|
mCallback->OnUsageResult(mURI, usage, mFileUsage, mAppId,
|
|
|
|
mInMozBrowserOnly);
|
2012-06-22 11:47:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up.
|
2012-07-30 07:20:58 -07:00
|
|
|
mURI = nullptr;
|
|
|
|
mCallback = nullptr;
|
2012-06-22 11:47:05 -07:00
|
|
|
|
|
|
|
// And tell the IndexedDatabaseManager that we're done.
|
|
|
|
mgr->OnUsageCheckComplete(this);
|
|
|
|
if (mCallbackState == Complete) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mgr->AllowNextSynchronizedOp(mOrigin, nullptr);
|
2012-06-22 11:47:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown state value!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
2012-06-22 11:47:05 -07:00
|
|
|
|
|
|
|
NS_NOTREACHED("Should never get here!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult
|
|
|
|
IndexedDatabaseManager::AsyncUsageRunnable::GetUsageForDirectory(
|
|
|
|
nsIFile* aDirectory,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t* aUsage)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aDirectory, "Null pointer!");
|
|
|
|
NS_ASSERTION(aUsage, "Null pointer!");
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> entries;
|
|
|
|
nsresult rv = aDirectory->GetDirectoryEntries(getter_AddRefs(entries));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
if (!entries) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED((rv = entries->HasMoreElements(&hasMore))) &&
|
|
|
|
hasMore && !mCanceled) {
|
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
rv = entries->GetNext(getter_AddRefs(entry));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:33 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsIFile> file(do_QueryInterface(entry));
|
|
|
|
NS_ASSERTION(file, "Don't know what this is!");
|
|
|
|
|
|
|
|
bool isDirectory;
|
|
|
|
rv = file->IsDirectory(&isDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (isDirectory) {
|
|
|
|
if (aUsage == &mFileUsage) {
|
|
|
|
NS_WARNING("Unknown directory found!");
|
2011-12-20 03:23:27 -08:00
|
|
|
}
|
|
|
|
else {
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = GetUsageForDirectory(file, &mFileUsage);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
continue;
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t fileSize;
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
NS_ASSERTION(fileSize >= 0, "Negative size?!");
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
IncrementUsage(aUsage, uint64_t(fileSize));
|
2010-10-19 10:58:33 -07:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-10-19 10:58:33 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(IndexedDatabaseManager::AsyncUsageRunnable,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::AsyncUsageRunnable::Run()
|
|
|
|
{
|
|
|
|
nsresult rv = RunInternal();
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mUsage = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(
|
|
|
|
IndexedDatabaseManager::WaitForTransactionsToFinishRunnable,
|
|
|
|
nsIRunnable)
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-02 05:53:12 -07:00
|
|
|
IndexedDatabaseManager::WaitForTransactionsToFinishRunnable::Run()
|
2010-10-19 10:58:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_ASSERTION(mOp, "Null op!");
|
|
|
|
NS_ASSERTION(mOp->mHelper || mOp->mRunnable, "Nothing to run!");
|
2012-06-03 09:33:52 -07:00
|
|
|
NS_ASSERTION(mCountdown, "Wrong countdown!");
|
|
|
|
|
|
|
|
if (--mCountdown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Don't hold the callback alive longer than necessary.
|
|
|
|
nsRefPtr<AsyncConnectionHelper> helper;
|
|
|
|
helper.swap(mOp->mHelper);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable;
|
|
|
|
runnable.swap(mOp->mRunnable);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mOp = nullptr;
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
nsresult rv;
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
if (helper && helper->HasTransaction()) {
|
|
|
|
// If the helper has a transaction, dispatch it to the transaction
|
|
|
|
// threadpool.
|
|
|
|
rv = helper->DispatchToTransactionPool();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise, dispatch it to the IO thread.
|
|
|
|
IndexedDatabaseManager* manager = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(manager, "We should definitely have a manager here");
|
|
|
|
|
|
|
|
nsIEventTarget* target = manager->IOThread();
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
rv = helper ?
|
|
|
|
helper->Dispatch(target) :
|
|
|
|
target->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The helper or runnable is responsible for calling
|
|
|
|
// IndexedDatabaseManager::AllowNextSynchronizedOp.
|
2010-10-19 10:58:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(
|
|
|
|
IndexedDatabaseManager::WaitForLockedFilesToFinishRunnable,
|
|
|
|
nsIRunnable)
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::WaitForLockedFilesToFinishRunnable::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
mBusy = false;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
IndexedDatabaseManager::
|
2012-10-23 09:31:19 -07:00
|
|
|
SynchronizedOp::SynchronizedOp(const OriginOrPatternString& aOriginOrPattern,
|
2012-06-19 18:50:39 -07:00
|
|
|
nsIAtom* aId)
|
2012-10-23 09:31:19 -07:00
|
|
|
: mOriginOrPattern(aOriginOrPattern), mId(aId)
|
2011-10-25 05:49:31 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-11-02 05:53:12 -07:00
|
|
|
MOZ_COUNT_CTOR(IndexedDatabaseManager::SynchronizedOp);
|
|
|
|
}
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
IndexedDatabaseManager::SynchronizedOp::~SynchronizedOp()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
MOZ_COUNT_DTOR(IndexedDatabaseManager::SynchronizedOp);
|
|
|
|
}
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
bool
|
2012-10-23 09:31:19 -07:00
|
|
|
IndexedDatabaseManager::
|
|
|
|
SynchronizedOp::MustWaitFor(const SynchronizedOp& aExistingOp)
|
2011-11-02 05:53:12 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
bool match;
|
|
|
|
|
|
|
|
if (aExistingOp.mOriginOrPattern.IsOrigin()) {
|
|
|
|
if (mOriginOrPattern.IsOrigin()) {
|
|
|
|
match = aExistingOp.mOriginOrPattern.Equals(mOriginOrPattern);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
match = PatternMatchesOrigin(mOriginOrPattern, aExistingOp.mOriginOrPattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mOriginOrPattern.IsOrigin()) {
|
|
|
|
match = PatternMatchesOrigin(aExistingOp.mOriginOrPattern, mOriginOrPattern);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
match = PatternMatchesOrigin(mOriginOrPattern, aExistingOp.mOriginOrPattern) ||
|
|
|
|
PatternMatchesOrigin(aExistingOp.mOriginOrPattern, mOriginOrPattern);
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// If the origins don't match, the second can proceed.
|
2012-10-23 09:31:19 -07:00
|
|
|
if (!match) {
|
2011-11-02 05:53:12 -07:00
|
|
|
return false;
|
2011-10-25 05:49:31 -07:00
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// If the origins and the ids match, the second must wait.
|
2012-10-23 09:31:19 -07:00
|
|
|
if (aExistingOp.mId == mId) {
|
2011-11-02 05:53:12 -07:00
|
|
|
return true;
|
2011-10-25 05:49:31 -07:00
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Waiting is required if either one corresponds to an origin clearing
|
|
|
|
// (a null Id).
|
2012-10-23 09:31:19 -07:00
|
|
|
if (!aExistingOp.mId || !mId) {
|
2011-11-02 05:53:12 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Otherwise, things for the same origin but different databases can proceed
|
|
|
|
// independently.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::SynchronizedOp::DelayRunnable(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(mDelayedRunnables.IsEmpty() || !mId,
|
|
|
|
"Only ClearOrigin operations can delay multiple runnables!");
|
|
|
|
|
|
|
|
mDelayedRunnables.AppendElement(aRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IndexedDatabaseManager::SynchronizedOp::DispatchDelayedRunnables()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mHelper, "Any helper should be gone by now!");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mDelayedRunnables.Length();
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2011-11-02 05:53:12 -07:00
|
|
|
NS_DispatchToCurrentThread(mDelayedRunnables[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
mDelayedRunnables.Clear();
|
2011-10-25 05:49:31 -07:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-01-03 07:27:39 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::InitWindowless(const jsval& aObj, JSContext* aCx)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_NOT_AVAILABLE);
|
2012-01-24 02:03:37 -08:00
|
|
|
NS_ENSURE_ARG(!JSVAL_IS_PRIMITIVE(aObj));
|
|
|
|
|
2012-08-29 17:50:28 -07:00
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(aObj);
|
|
|
|
|
|
|
|
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 07:27:39 -08:00
|
|
|
// Instantiating this class will register exception providers so even
|
2012-01-24 02:03:37 -08:00
|
|
|
// in xpcshell we will get typed (dom) exceptions, instead of general
|
|
|
|
// exceptions.
|
2012-01-03 07:27:39 -08:00
|
|
|
nsCOMPtr<nsIDOMScriptObjectFactory> sof(do_GetService(kDOMSOF_CID));
|
|
|
|
|
2012-01-24 02:03:37 -08:00
|
|
|
JSObject* global = JS_GetGlobalForObject(aCx, obj);
|
2012-08-29 17:50:28 -07:00
|
|
|
NS_ASSERTION(global, "What?! No global!");
|
2012-01-24 02:03:37 -08:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
nsRefPtr<IDBFactory> factory;
|
2012-08-01 23:02:29 -07:00
|
|
|
nsresult rv =
|
|
|
|
IDBFactory::Create(aCx, global, nullptr, getter_AddRefs(factory));
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(factory, "This should never fail for chrome!");
|
2012-01-03 07:27:39 -08:00
|
|
|
|
2012-06-29 09:48:35 -07:00
|
|
|
jsval indexedDBVal;
|
|
|
|
rv = nsContentUtils::WrapNative(aCx, obj, factory, &indexedDBVal);
|
2012-01-03 07:27:39 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!JS_DefineProperty(aCx, obj, "indexedDB", indexedDBVal, nullptr,
|
|
|
|
nullptr, JSPROP_ENUMERATE)) {
|
2012-01-03 07:27:39 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
JSObject* keyrangeObj = JS_NewObject(aCx, nullptr, nullptr, nullptr);
|
2012-01-03 07:27:39 -08:00
|
|
|
NS_ENSURE_TRUE(keyrangeObj, NS_ERROR_OUT_OF_MEMORY);
|
2012-01-24 02:03:37 -08:00
|
|
|
|
2012-01-03 07:27:39 -08:00
|
|
|
if (!IDBKeyRange::DefineConstructors(aCx, keyrangeObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineProperty(aCx, obj, "IDBKeyRange", OBJECT_TO_JSVAL(keyrangeObj),
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, JSPROP_ENUMERATE)) {
|
2012-01-03 07:27:39 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
IndexedDatabaseManager::
|
|
|
|
AsyncDeleteFileRunnable::AsyncDeleteFileRunnable(FileManager* aFileManager,
|
|
|
|
int64_t aFileId)
|
|
|
|
: mFileManager(aFileManager), mFileId(aFileId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(IndexedDatabaseManager::AsyncDeleteFileRunnable,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IndexedDatabaseManager::AsyncDeleteFileRunnable::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-24 11:51:33 -07: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 11:25:10 -08:00
|
|
|
nsresult rv;
|
|
|
|
int64_t fileSize;
|
2012-08-24 11:51:33 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
if (mFileManager->Privilege() != Chrome) {
|
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = file->Remove(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2012-06-11 14:59:27 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
if (mFileManager->Privilege() != Chrome) {
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
|
|
|
quotaManager->DecreaseUsageForOrigin(mFileManager->Origin(), fileSize);
|
2012-06-11 14:59:27 -07:00
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07: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-15 23:34:24 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|