2010-06-23 23:46:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-04-30 03:46:20 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-06-28 20:44:30 +04:00
|
|
|
#include "IDBFactory.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2012-06-01 21:21:12 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2010-08-27 00:57:25 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-12-20 14:59:07 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsContentUtils.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsHashKeys.h"
|
2010-08-27 00:57:25 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOMCID.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-09-10 02:15:40 +04:00
|
|
|
#include "CheckPermissionsHelper.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "DatabaseInfo.h"
|
2010-06-28 20:46:49 +04:00
|
|
|
#include "IDBDatabase.h"
|
2011-10-20 20:10:56 +04:00
|
|
|
#include "IDBEvents.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "IDBKeyRange.h"
|
2010-09-10 23:12:11 +04:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2011-11-08 23:03:18 +04:00
|
|
|
#include "Key.h"
|
2011-05-29 03:42:57 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
#include "mozilla/dom/PBrowserChild.h"
|
|
|
|
#include "mozilla/dom/TabChild.h"
|
|
|
|
using mozilla::dom::TabChild;
|
|
|
|
|
|
|
|
#include "ipc/IndexedDBChild.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct ObjectStoreInfoMap
|
|
|
|
{
|
|
|
|
ObjectStoreInfoMap()
|
|
|
|
: id(LL_MININT), info(nsnull) { }
|
|
|
|
|
|
|
|
PRInt64 id;
|
|
|
|
ObjectStoreInfo* info;
|
|
|
|
};
|
|
|
|
|
2011-08-26 12:21:35 +04:00
|
|
|
} // anonymous namespace
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-04-30 03:46:20 +04:00
|
|
|
IDBFactory::IDBFactory()
|
2012-06-01 21:21:12 +04:00
|
|
|
: mOwningObject(nsnull), mActorChild(nsnull), mActorParent(nsnull)
|
2011-04-30 03:46:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
IDBFactory::~IDBFactory()
|
|
|
|
{
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ASSERTION(!mActorParent, "Actor parent owns us, how can we be dying?!");
|
|
|
|
if (mActorChild) {
|
|
|
|
NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
mActorChild->Send__delete__(mActorChild);
|
|
|
|
NS_ASSERTION(!mActorChild, "Should have cleared in Send__delete__!");
|
|
|
|
}
|
2012-01-24 14:03:37 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// static
|
2012-06-01 21:21:12 +04:00
|
|
|
nsresult
|
|
|
|
IDBFactory::Create(nsPIDOMWindow* aWindow,
|
|
|
|
const nsACString& aASCIIOrigin,
|
|
|
|
IDBFactory** aFactory)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ASSERTION(aASCIIOrigin.IsEmpty() || nsContentUtils::IsCallerChrome(),
|
|
|
|
"Non-chrome may not supply their own origin!");
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ENSURE_TRUE(aWindow, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-01-24 14:03:37 +04:00
|
|
|
|
|
|
|
if (aWindow->IsOuterWindow()) {
|
2011-01-27 04:53:02 +03:00
|
|
|
aWindow = aWindow->GetCurrentInnerWindow();
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ENSURE_TRUE(aWindow, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the manager is up before we do anything here since lots of
|
|
|
|
// decisions depend on which process we're running in.
|
|
|
|
nsRefPtr<indexedDB::IndexedDatabaseManager> mgr =
|
|
|
|
indexedDB::IndexedDatabaseManager::GetOrCreate();
|
|
|
|
NS_ENSURE_TRUE(mgr, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCString origin(aASCIIOrigin);
|
|
|
|
if (origin.IsEmpty()) {
|
|
|
|
rv = IndexedDatabaseManager::GetASCIIOriginFromWindow(aWindow, origin);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Not allowed.
|
|
|
|
*aFactory = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-01-27 04:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDBFactory> factory = new IDBFactory();
|
2012-06-01 21:21:12 +04:00
|
|
|
factory->mASCIIOrigin = origin;
|
2012-01-24 14:03:37 +04:00
|
|
|
factory->mWindow = aWindow;
|
2012-06-01 21:21:12 +04:00
|
|
|
|
|
|
|
if (!IndexedDatabaseManager::IsMainProcess()) {
|
|
|
|
TabChild* tabChild = GetTabChildFrom(aWindow);
|
|
|
|
NS_ENSURE_TRUE(tabChild, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
IndexedDBChild* actor = new IndexedDBChild(origin);
|
|
|
|
|
|
|
|
bool allowed;
|
|
|
|
tabChild->SendPIndexedDBConstructor(actor, origin, &allowed);
|
|
|
|
|
|
|
|
if (!allowed) {
|
|
|
|
actor->Send__delete__(actor);
|
|
|
|
*aFactory = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
actor->SetFactory(factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
factory.forget(aFactory);
|
|
|
|
return NS_OK;
|
2012-01-24 14:03:37 +04:00
|
|
|
}
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
// static
|
2012-06-01 21:21:12 +04:00
|
|
|
nsresult
|
2012-01-24 14:03:37 +04:00
|
|
|
IDBFactory::Create(JSContext* aCx,
|
2012-06-01 21:21:12 +04:00
|
|
|
JSObject* aOwningObject,
|
|
|
|
IDBFactory** aFactory)
|
2012-01-24 14:03:37 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aCx, "Null context!");
|
|
|
|
NS_ASSERTION(aOwningObject, "Null object!");
|
|
|
|
NS_ASSERTION(JS_GetGlobalForObject(aCx, aOwningObject) == aOwningObject,
|
|
|
|
"Not a global object!");
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ASSERTION(nsContentUtils::IsCallerChrome(), "Only for chrome!");
|
|
|
|
|
|
|
|
nsCString origin;
|
|
|
|
nsresult rv =
|
|
|
|
IndexedDatabaseManager::GetASCIIOriginFromWindow(nsnull, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
nsRefPtr<IDBFactory> factory = new IDBFactory();
|
2012-06-01 21:21:12 +04:00
|
|
|
factory->mASCIIOrigin = origin;
|
2012-01-24 14:03:37 +04:00
|
|
|
factory->mOwningObject = aOwningObject;
|
2012-06-01 21:21:12 +04:00
|
|
|
|
|
|
|
factory.forget(aFactory);
|
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<mozIStorageConnection>
|
2010-06-28 20:44:30 +04:00
|
|
|
IDBFactory::GetConnection(const nsAString& aDatabaseFilePath)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ASSERTION(StringEndsWith(aDatabaseFilePath, NS_LITERAL_STRING(".sqlite")),
|
|
|
|
"Bad file path!");
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> dbFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_TRUE(dbFile, nsnull);
|
|
|
|
|
|
|
|
nsresult rv = dbFile->InitWithPath(aDatabaseFilePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2010-06-23 23:46:08 +04:00
|
|
|
rv = dbFile->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
NS_ENSURE_TRUE(exists, nsnull);
|
|
|
|
|
2010-09-10 02:15:40 +04:00
|
|
|
nsCOMPtr<mozIStorageServiceQuotaManagement> ss =
|
2010-06-23 23:46:08 +04:00
|
|
|
do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(ss, nsnull);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> connection;
|
2010-09-10 02:15:40 +04:00
|
|
|
rv = ss->OpenDatabaseWithVFS(dbFile, NS_LITERAL_CSTRING("quota"),
|
|
|
|
getter_AddRefs(connection));
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2012-06-27 07:14:53 +04:00
|
|
|
// Turn on foreign key constraints and recursive triggers.
|
|
|
|
// The "INSERT OR REPLACE" statement doesn't fire the update trigger,
|
|
|
|
// instead it fires only the insert trigger. This confuses the update
|
|
|
|
// refcount function. This behavior changes with enabled recursive triggers,
|
|
|
|
// so the statement fires the delete trigger first and then the insert
|
|
|
|
// trigger.
|
2010-06-23 23:46:08 +04:00
|
|
|
rv = connection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2012-06-27 07:14:53 +04:00
|
|
|
"PRAGMA foreign_keys = ON; "
|
|
|
|
"PRAGMA recursive_triggers = ON;"
|
2010-06-23 23:46:08 +04:00
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
|
|
|
return connection.forget();
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:59:07 +04:00
|
|
|
inline
|
|
|
|
bool
|
|
|
|
IgnoreWhitespace(PRUnichar c)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IDBFactory::LoadDatabaseInformation(mozIStorageConnection* aConnection,
|
2011-11-02 16:53:12 +04:00
|
|
|
nsIAtom* aDatabaseId,
|
2011-10-20 20:10:56 +04:00
|
|
|
PRUint64* aVersion,
|
2010-10-19 21:58:52 +04:00
|
|
|
ObjectStoreInfoArray& aObjectStores)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aConnection, "Null pointer!");
|
|
|
|
|
|
|
|
aObjectStores.Clear();
|
|
|
|
|
|
|
|
// Load object store names and ids.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT name, id, key_path, auto_increment "
|
|
|
|
"FROM object_store"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoTArray<ObjectStoreInfoMap, 20> infoMap;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasResult;
|
2010-10-19 21:58:52 +04:00
|
|
|
while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
2011-12-17 04:40:47 +04:00
|
|
|
nsRefPtr<ObjectStoreInfo>* element =
|
2010-10-19 21:58:52 +04:00
|
|
|
aObjectStores.AppendElement(new ObjectStoreInfo());
|
|
|
|
|
|
|
|
ObjectStoreInfo* info = element->get();
|
|
|
|
|
|
|
|
rv = stmt->GetString(0, info->name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
info->id = stmt->AsInt64(1);
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
PRInt32 columnType;
|
|
|
|
nsresult rv = stmt->GetTypeOfIndex(2, &columnType);
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
// NB: We don't have to handle the NULL case, since that is the default
|
|
|
|
// for a new KeyPath.
|
|
|
|
if (columnType != mozIStorageStatement::VALUE_TYPE_NULL) {
|
2011-12-04 21:39:01 +04:00
|
|
|
NS_ASSERTION(columnType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Should be a string");
|
2012-06-22 02:27:13 +04:00
|
|
|
nsString keyPathSerialization;
|
|
|
|
rv = stmt->GetString(2, keyPathSerialization);
|
2011-12-04 21:39:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-20 14:59:07 +04:00
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
info->keyPath = KeyPath::DeserializeFromString(keyPathSerialization);
|
2011-12-04 21:39:01 +04:00
|
|
|
}
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
info->nextAutoIncrementId = stmt->AsInt64(3);
|
|
|
|
info->comittedAutoIncrementId = info->nextAutoIncrementId;
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
info->autoIncrement = !!info->nextAutoIncrementId;
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
ObjectStoreInfoMap* mapEntry = infoMap.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(mapEntry, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
mapEntry->id = info->id;
|
|
|
|
mapEntry->info = info;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Load index information
|
|
|
|
rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
2011-12-17 04:40:47 +04:00
|
|
|
"SELECT object_store_id, id, name, key_path, unique_index, multientry "
|
2010-10-19 21:58:52 +04:00
|
|
|
"FROM object_store_index"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
|
|
|
PRInt64 objectStoreId = stmt->AsInt64(0);
|
|
|
|
|
|
|
|
ObjectStoreInfo* objectStoreInfo = nsnull;
|
|
|
|
for (PRUint32 index = 0; index < infoMap.Length(); index++) {
|
|
|
|
if (infoMap[index].id == objectStoreId) {
|
|
|
|
objectStoreInfo = infoMap[index].info;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!objectStoreInfo) {
|
|
|
|
NS_ERROR("Index for nonexistant object store!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexInfo* indexInfo = objectStoreInfo->indexes.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(indexInfo, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
indexInfo->id = stmt->AsInt64(1);
|
|
|
|
|
|
|
|
rv = stmt->GetString(2, indexInfo->name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
nsString keyPathSerialization;
|
|
|
|
rv = stmt->GetString(3, keyPathSerialization);
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
// XXX bent wants to assert here
|
|
|
|
indexInfo->keyPath = KeyPath::DeserializeFromString(keyPathSerialization);
|
2010-10-19 21:58:52 +04:00
|
|
|
indexInfo->unique = !!stmt->AsInt32(4);
|
2011-12-04 21:39:01 +04:00
|
|
|
indexInfo->multiEntry = !!stmt->AsInt32(5);
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Load version information.
|
|
|
|
rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT version "
|
|
|
|
"FROM database"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!hasResult) {
|
|
|
|
NS_ERROR("Database has no version!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
PRInt64 version = 0;
|
|
|
|
rv = stmt->GetInt64(0, &version);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
*aVersion = NS_MAX<PRInt64>(version, 0);
|
|
|
|
|
|
|
|
return rv;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
2011-12-17 04:40:47 +04:00
|
|
|
IDBFactory::SetDatabaseMetadata(DatabaseInfo* aDatabaseInfo,
|
|
|
|
PRUint64 aVersion,
|
|
|
|
ObjectStoreInfoArray& aObjectStores)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabaseInfo, "Null pointer!");
|
|
|
|
|
|
|
|
ObjectStoreInfoArray objectStores;
|
2011-12-17 04:40:47 +04:00
|
|
|
objectStores.SwapElements(aObjectStores);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsTArray<nsString> existingNames;
|
|
|
|
aDatabaseInfo->GetObjectStoreNames(existingNames);
|
|
|
|
NS_ASSERTION(existingNames.IsEmpty(), "Should be an empty DatabaseInfo");
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
2011-12-17 04:40:47 +04:00
|
|
|
#endif
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
aDatabaseInfo->version = aVersion;
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < objectStores.Length(); index++) {
|
2011-12-17 04:40:47 +04:00
|
|
|
nsRefPtr<ObjectStoreInfo>& info = objectStores[index];
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2011-11-08 04:15:43 +04:00
|
|
|
if (!aDatabaseInfo->PutObjectStore(info)) {
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBFactory)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBFactory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBFactory)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFactory)
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2010-06-28 20:44:30 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBFactory)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBFactory)
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBFactory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBFactory)
|
|
|
|
if (tmp->mOwningObject) {
|
|
|
|
tmp->mOwningObject = nsnull;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBFactory)
|
2012-06-11 03:44:50 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mOwningObject)
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2010-06-28 20:44:30 +04:00
|
|
|
DOMCI_DATA(IDBFactory, IDBFactory)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-08 04:15:45 +04:00
|
|
|
nsresult
|
|
|
|
IDBFactory::OpenCommon(const nsAString& aName,
|
|
|
|
PRInt64 aVersion,
|
|
|
|
bool aDeleting,
|
2012-06-29 20:48:34 +04:00
|
|
|
JSContext* aCallingCx,
|
2012-06-01 21:21:12 +04:00
|
|
|
IDBOpenDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-01-24 14:03:37 +04:00
|
|
|
NS_ASSERTION(mWindow || mOwningObject, "Must have one of these!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-01-03 19:27:39 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window;
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo;
|
2012-01-24 14:03:37 +04:00
|
|
|
JSObject* scriptOwner = nsnull;
|
2012-01-03 19:27:39 +04:00
|
|
|
|
|
|
|
if (mWindow) {
|
2012-01-24 14:03:37 +04:00
|
|
|
window = mWindow;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scriptOwner = mOwningObject;
|
2012-01-03 19:27:39 +04:00
|
|
|
}
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-10-20 20:10:56 +04:00
|
|
|
nsRefPtr<IDBOpenDBRequest> request =
|
2012-06-29 20:48:34 +04:00
|
|
|
IDBOpenDBRequest::Create(window, scriptOwner, aCallingCx);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-27 00:57:25 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsresult rv;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
if (IndexedDatabaseManager::IsMainProcess()) {
|
|
|
|
nsRefPtr<OpenDatabaseHelper> openHelper =
|
|
|
|
new OpenDatabaseHelper(request, aName, mASCIIOrigin, aVersion, aDeleting);
|
2011-11-02 16:53:12 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
rv = openHelper->Init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-09-10 02:15:40 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsRefPtr<CheckPermissionsHelper> permissionHelper =
|
|
|
|
new CheckPermissionsHelper(openHelper, window, mASCIIOrigin, aDeleting);
|
2010-09-10 23:12:11 +04:00
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
|
|
|
rv =
|
|
|
|
mgr->WaitForOpenAllowed(mASCIIOrigin, openHelper->Id(), permissionHelper);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
else if (aDeleting) {
|
|
|
|
nsCOMPtr<nsIAtom> databaseId =
|
|
|
|
IndexedDatabaseManager::GetDatabaseId(mASCIIOrigin, aName);
|
|
|
|
NS_ENSURE_TRUE(databaseId, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
IndexedDBDeleteDatabaseRequestChild* actor =
|
|
|
|
new IndexedDBDeleteDatabaseRequestChild(this, request, databaseId);
|
|
|
|
|
|
|
|
mActorChild->SendPIndexedDBDeleteDatabaseRequestConstructor(
|
|
|
|
actor,
|
|
|
|
nsString(aName));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
IndexedDBDatabaseChild* dbActor =
|
|
|
|
static_cast<IndexedDBDatabaseChild*>(
|
|
|
|
mActorChild->SendPIndexedDBDatabaseConstructor(nsString(aName),
|
|
|
|
aVersion));
|
|
|
|
|
|
|
|
dbActor->SetRequest(request);
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-11-08 04:15:45 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBFactory::Open(const nsAString& aName,
|
|
|
|
PRInt64 aVersion,
|
2012-06-29 20:48:34 +04:00
|
|
|
JSContext* aCx,
|
2011-11-08 04:15:45 +04:00
|
|
|
PRUint8 aArgc,
|
|
|
|
nsIIDBOpenDBRequest** _retval)
|
|
|
|
{
|
|
|
|
if (aVersion < 1 && aArgc) {
|
2012-04-12 01:55:21 +04:00
|
|
|
return NS_ERROR_TYPE_ERR;
|
2011-11-08 04:15:45 +04:00
|
|
|
}
|
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
nsRefPtr<IDBOpenDBRequest> request;
|
2012-06-29 20:48:34 +04:00
|
|
|
nsresult rv = OpenCommon(aName, aVersion, false, aCx,
|
|
|
|
getter_AddRefs(request));
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
2011-11-08 04:15:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBFactory::DeleteDatabase(const nsAString& aName,
|
2012-06-29 20:48:34 +04:00
|
|
|
JSContext* aCx,
|
2011-11-08 04:15:45 +04:00
|
|
|
nsIIDBOpenDBRequest** _retval)
|
|
|
|
{
|
2012-06-01 21:21:12 +04:00
|
|
|
nsRefPtr<IDBOpenDBRequest> request;
|
2012-06-29 20:48:34 +04:00
|
|
|
nsresult rv = OpenCommon(aName, 0, true, aCx, getter_AddRefs(request));
|
2012-06-01 21:21:12 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
2011-11-08 04:15:45 +04:00
|
|
|
}
|
2011-11-08 23:03:18 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBFactory::Cmp(const jsval& aFirst,
|
|
|
|
const jsval& aSecond,
|
|
|
|
JSContext* aCx,
|
|
|
|
PRInt16* _retval)
|
|
|
|
{
|
|
|
|
Key first, second;
|
|
|
|
nsresult rv = first.SetFromJSVal(aCx, aFirst);
|
2012-06-01 21:21:12 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-11-08 23:03:18 +04:00
|
|
|
|
|
|
|
rv = second.SetFromJSVal(aCx, aSecond);
|
2012-06-01 21:21:12 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-11-08 23:03:18 +04:00
|
|
|
|
|
|
|
if (first.IsUnset() || second.IsUnset()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
*_retval = Key::CompareKeys(first, second);
|
2011-11-08 23:03:18 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|