This commit is contained in:
Ben Turner 2012-04-30 16:52:27 -07:00
Родитель 4e5d9404d3
Коммит e8e0f22c6e
7 изменённых файлов: 87 добавлений и 84 удалений

Просмотреть файл

@ -46,12 +46,10 @@
#include "mozilla/storage.h"
#include "mozilla/dom/ContentChild.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsComponentManagerUtils.h"
#include "nsIScriptSecurityManager.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsContentUtils.h"
#include "nsDirectoryServiceUtils.h"
#include "nsDOMClassInfoID.h"
#include "nsIPrincipal.h"
#include "nsHashKeys.h"
@ -180,46 +178,6 @@ IDBFactory::NoteUsedByProcessType(GeckoProcessType aProcessType)
}
}
// static
nsresult
IDBFactory::GetDirectory(nsIFile** aDirectory)
{
nsresult rv;
if (XRE_GetProcessType() == GeckoProcessType_Default) {
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, aDirectory);
NS_ENSURE_SUCCESS(rv, rv);
rv = (*aDirectory)->Append(NS_LITERAL_STRING("indexedDB"));
NS_ENSURE_SUCCESS(rv, rv);
} else {
nsCOMPtr<nsILocalFile> localDirectory =
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
rv = localDirectory->InitWithPath(
ContentChild::GetSingleton()->GetIndexedDBPath());
NS_ENSURE_SUCCESS(rv, rv);
localDirectory.forget((nsILocalFile**)aDirectory);
}
return NS_OK;
}
// static
nsresult
IDBFactory::GetDirectoryForOrigin(const nsACString& aASCIIOrigin,
nsIFile** aDirectory)
{
nsCOMPtr<nsIFile> directory;
nsresult rv = GetDirectory(getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
NS_ConvertASCIItoUTF16 originSanitized(aASCIIOrigin);
originSanitized.ReplaceChar(":/", '+');
rv = directory->Append(originSanitized);
NS_ENSURE_SUCCESS(rv, rv);
directory.forget(aDirectory);
return NS_OK;
}
inline
bool
IgnoreWhitespace(PRUnichar c)

Просмотреть файл

@ -81,13 +81,6 @@ public:
static void
NoteUsedByProcessType(GeckoProcessType aProcessType);
static nsresult
GetDirectory(nsIFile** aDirectory);
static nsresult
GetDirectoryForOrigin(const nsACString& aASCIIOrigin,
nsIFile** aDirectory);
static nsresult
LoadDatabaseInformation(mozIStorageConnection* aConnection,
nsIAtom* aDatabaseId,

Просмотреть файл

@ -37,11 +37,14 @@
*
* ***** END LICENSE BLOCK ***** */
#include "base/basictypes.h"
#include "IndexedDatabaseManager.h"
#include "DatabaseInfo.h"
#include "nsIDOMScriptObjectFactory.h"
#include "nsIFile.h"
#include "nsILocalFile.h"
#include "nsIObserverService.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIScriptSecurityManager.h"
@ -53,7 +56,10 @@
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/storage.h"
#include "mozilla/dom/ContentChild.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsContentUtils.h"
#include "nsDirectoryServiceUtils.h"
#include "nsThreadUtils.h"
#include "nsXPCOM.h"
#include "nsXPCOMPrivate.h"
@ -239,6 +245,18 @@ IndexedDatabaseManager::GetOrCreate()
return nsnull;
}
nsCOMPtr<nsIFile> dbBaseDirectory;
nsresult rv =
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
getter_AddRefs(dbBaseDirectory));
NS_ENSURE_SUCCESS(rv, nsnull);
rv = dbBaseDirectory->Append(NS_LITERAL_STRING("indexedDB"));
NS_ENSURE_SUCCESS(rv, nsnull);
rv = dbBaseDirectory->GetPath(instance->mDatabaseBasePath);
NS_ENSURE_SUCCESS(rv, nsnull);
// 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);
@ -248,8 +266,7 @@ IndexedDatabaseManager::GetOrCreate()
NS_ENSURE_TRUE(obs, nsnull);
// We need this callback to know when to shut down all our threads.
nsresult rv = obs->AddObserver(instance, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
false);
rv = obs->AddObserver(instance, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
NS_ENSURE_SUCCESS(rv, nsnull);
// Make a lazy thread for any IO we need (like clearing or enumerating the
@ -284,6 +301,32 @@ IndexedDatabaseManager::FactoryCreate()
return GetOrCreate().get();
}
nsresult
IndexedDatabaseManager::GetDirectoryForOrigin(const nsACString& aASCIIOrigin,
nsIFile** aDirectory) const
{
nsresult rv;
nsCOMPtr<nsILocalFile> directory =
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
const nsString& path = XRE_GetProcessType() == GeckoProcessType_Default ?
GetBaseDirectory() :
ContentChild::GetSingleton()->GetIndexedDBPath();
rv = directory->InitWithPath(path);
NS_ENSURE_SUCCESS(rv, rv);
NS_ConvertASCIItoUTF16 originSanitized(aASCIIOrigin);
originSanitized.ReplaceChar(":/", '+');
rv = directory->Append(originSanitized);
NS_ENSURE_SUCCESS(rv, rv);
directory.forget(reinterpret_cast<nsILocalFile**>(aDirectory));
return NS_OK;
}
bool
IndexedDatabaseManager::RegisterDatabase(IDBDatabase* aDatabase)
{
@ -631,8 +674,7 @@ IndexedDatabaseManager::EnsureOriginIsInitialized(const nsACString& aOrigin,
#endif
nsCOMPtr<nsIFile> directory;
nsresult rv = IDBFactory::GetDirectoryForOrigin(aOrigin,
getter_AddRefs(directory));
nsresult rv = GetDirectoryForOrigin(aOrigin, getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
bool exists;
@ -1282,6 +1324,9 @@ NS_IMPL_THREADSAFE_ISUPPORTS1(IndexedDatabaseManager::OriginClearRunnable,
NS_IMETHODIMP
IndexedDatabaseManager::OriginClearRunnable::Run()
{
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
NS_ASSERTION(mgr, "This should never fail!");
if (NS_IsMainThread()) {
// On the first time on the main thread we dispatch to the IO thread.
if (mFirstCallback) {
@ -1303,9 +1348,6 @@ IndexedDatabaseManager::OriginClearRunnable::Run()
NS_ASSERTION(!mThread, "Should have been cleared already!");
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
NS_ASSERTION(mgr, "This should never fail!");
mgr->InvalidateFileManagersForOrigin(mOrigin);
// Tell the IndexedDatabaseManager that we're done.
@ -1318,8 +1360,7 @@ IndexedDatabaseManager::OriginClearRunnable::Run()
// Remove the directory that contains all our databases.
nsCOMPtr<nsIFile> directory;
nsresult rv = IDBFactory::GetDirectoryForOrigin(mOrigin,
getter_AddRefs(directory));
nsresult rv = mgr->GetDirectoryForOrigin(mOrigin, getter_AddRefs(directory));
if (NS_SUCCEEDED(rv)) {
bool exists;
rv = directory->Exists(&exists);
@ -1377,6 +1418,9 @@ IncrementUsage(PRUint64* aUsage, PRUint64 aDelta)
nsresult
IndexedDatabaseManager::AsyncUsageRunnable::RunInternal()
{
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
NS_ASSERTION(mgr, "This should never fail!");
if (NS_IsMainThread()) {
// Call the callback unless we were canceled.
if (!mCanceled) {
@ -1390,10 +1434,7 @@ IndexedDatabaseManager::AsyncUsageRunnable::RunInternal()
mCallback = nsnull;
// And tell the IndexedDatabaseManager that we're done.
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
if (mgr) {
mgr->OnUsageCheckComplete(this);
}
mgr->OnUsageCheckComplete(this);
return NS_OK;
}
@ -1404,8 +1445,7 @@ IndexedDatabaseManager::AsyncUsageRunnable::RunInternal()
// Get the directory that contains all the database files we care about.
nsCOMPtr<nsIFile> directory;
nsresult rv = IDBFactory::GetDirectoryForOrigin(mOrigin,
getter_AddRefs(directory));
nsresult rv = mgr->GetDirectoryForOrigin(mOrigin, getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
bool exists;

Просмотреть файл

@ -60,6 +60,7 @@
#define INDEXEDDB_MANAGER_CONTRACTID "@mozilla.org/dom/indexeddb/manager;1"
class mozIStorageQuotaCallback;
class nsIFile;
class nsITimer;
BEGIN_INDEXEDDB_NAMESPACE
@ -193,6 +194,16 @@ public:
nsresult AsyncDeleteFile(FileManager* aFileManager,
PRInt64 aFileId);
const nsString&
GetBaseDirectory() const
{
return mDatabaseBasePath;
}
nsresult
GetDirectoryForOrigin(const nsACString& aASCIIOrigin,
nsIFile** aDirectory) const;
static mozilla::Mutex& FileMutex()
{
IndexedDatabaseManager* mgr = Get();
@ -388,6 +399,8 @@ private:
// It's s also used to atomically update FileInfo.mRefCnt, FileInfo.mDBRefCnt
// and FileInfo.mSliceRefCnt
mozilla::Mutex mFileMutex;
nsString mDatabaseBasePath;
};
class AutoEnterWindow

Просмотреть файл

@ -2337,9 +2337,12 @@ DeleteDatabaseHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
{
NS_ASSERTION(!aConnection, "How did we get a connection here?");
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
NS_ASSERTION(mgr, "This should never fail!");
nsCOMPtr<nsIFile> directory;
nsresult rv = IDBFactory::GetDirectoryForOrigin(mASCIIOrigin,
getter_AddRefs(directory));
nsresult rv = mgr->GetDirectoryForOrigin(mASCIIOrigin,
getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
NS_ASSERTION(directory, "What?");

Просмотреть файл

@ -3,17 +3,7 @@
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const Ci = Components.interfaces;
const nsIIndexedDatabaseManager =
Ci.nsIIndexedDatabaseManager;
var idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"]
.getService(nsIIndexedDatabaseManager);
idbManager.initWindowless(this);
// in xpcshell profile are not default
do_get_profile();
// oddly, if ProfD is requested from some worker thread first instead of the main thread it is crashing... so:
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
var file = dirSvc.get("ProfD", Ci.nsIFile);
const { 'classes': Cc, 'interfaces': Ci } = Components;
const DOMException = Ci.nsIDOMDOMException;
const IDBCursor = Ci.nsIIDBCursor;
@ -26,7 +16,12 @@ const IDBIndex = Ci.nsIIDBIndex
const IDBObjectStore = Ci.nsIIDBObjectStore
const IDBRequest = Ci.nsIIDBRequest
// XPCShell does not get a profile by default.
do_get_profile();
var idbManager = Cc["@mozilla.org/dom/indexeddb/manager;1"].
getService(Ci.nsIIndexedDatabaseManager);
idbManager.initWindowless(this);
function is(a, b, msg) {
if(a != b)

Просмотреть файл

@ -69,6 +69,7 @@
#include "nsAppDirectoryServiceDefs.h"
#include "nsAppRunner.h"
#include "IDBFactory.h"
#include "IndexedDatabaseManager.h"
#if defined(MOZ_SYDNEYAUDIO)
#include "AudioParent.h"
#endif
@ -528,18 +529,18 @@ ContentParent::RecvReadPermissions(InfallibleTArray<IPC::Permission>* aPermissio
bool
ContentParent::RecvGetIndexedDBDirectory(nsString* aDirectory)
{
indexedDB::IDBFactory::NoteUsedByProcessType(GeckoProcessType_Content);
using namespace indexedDB;
nsCOMPtr<nsIFile> dbDirectory;
nsresult rv = indexedDB::IDBFactory::GetDirectory(getter_AddRefs(dbDirectory));
IDBFactory::NoteUsedByProcessType(GeckoProcessType_Content);
if (NS_FAILED(rv)) {
NS_ERROR("Failed to get IndexedDB directory");
nsRefPtr<IndexedDatabaseManager> mgr =
IndexedDatabaseManager::GetOrCreate();
if (!mgr) {
NS_ERROR("This should not fail!");
return true;
}
dbDirectory->GetPath(*aDirectory);
*aDirectory = mgr->GetBaseDirectory();
return true;
}