2012-06-03 09:33:52 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "IDBFileHandle.h"
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
#include "mozilla/dom/file/File.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "mozilla/dom/quota/FileStreams.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
#include "IDBDatabase.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-12-17 11:25:10 -08:00
|
|
|
USING_QUOTA_NAMESPACE
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
|
|
|
already_AddRefed<nsIFile>
|
|
|
|
GetFileFor(FileInfo* aFileInfo)
|
|
|
|
|
|
|
|
{
|
|
|
|
FileManager* fileManager = aFileInfo->Manager();
|
|
|
|
nsCOMPtr<nsIFile> directory = fileManager->GetDirectory();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(directory, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = fileManager->GetFileForId(directory,
|
|
|
|
aFileInfo->Id());
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(file, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<IDBFileHandle>
|
|
|
|
IDBFileHandle::Create(IDBDatabase* aDatabase,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
already_AddRefed<FileInfo> aFileInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<FileInfo> fileInfo(aFileInfo);
|
|
|
|
NS_ASSERTION(fileInfo, "Null pointer!");
|
|
|
|
|
|
|
|
nsRefPtr<IDBFileHandle> newFile = new IDBFileHandle();
|
|
|
|
|
|
|
|
newFile->BindToOwner(aDatabase);
|
|
|
|
|
|
|
|
newFile->mFileStorage = aDatabase;
|
|
|
|
newFile->mName = aName;
|
|
|
|
newFile->mType = aType;
|
|
|
|
|
|
|
|
newFile->mFile = GetFileFor(fileInfo);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(newFile->mFile, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
newFile->mFileName.AppendInt(fileInfo->Id());
|
|
|
|
|
|
|
|
fileInfo.swap(newFile->mFileInfo);
|
|
|
|
|
|
|
|
return newFile.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
IDBFileHandle::CreateStream(nsIFile* aFile, bool aReadOnly)
|
|
|
|
{
|
2012-12-17 11:25:10 -08:00
|
|
|
const nsACString& origin = mFileStorage->StorageOrigin();
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> result;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
if (aReadOnly) {
|
2012-12-17 11:25:10 -08:00
|
|
|
nsRefPtr<FileInputStream> stream = FileInputStream::Create(
|
|
|
|
origin, aFile, -1, -1, nsIFileInputStream::DEFER_OPEN);
|
|
|
|
result = NS_ISUPPORTS_CAST(nsIFileInputStream*, stream);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-12-17 11:25:10 -08:00
|
|
|
nsRefPtr<FileStream> stream = FileStream::Create(
|
|
|
|
origin, aFile, -1, -1, nsIFileStream::DEFER_OPEN);
|
|
|
|
result = NS_ISUPPORTS_CAST(nsIFileStream*, stream);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
2012-12-17 11:25:10 -08:00
|
|
|
NS_ENSURE_TRUE(result, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMFile>
|
|
|
|
IDBFileHandle::CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFileSize)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMFile> file = new mozilla::dom::file::File(
|
|
|
|
mName, mType, aFileSize, mFile, aLockedFile, mFileInfo);
|
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileHandle)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBFileHandle)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBFileHandle)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(FileHandle)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBFileHandle, FileHandle)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBFileHandle, FileHandle)
|
|
|
|
|
|
|
|
DOMCI_DATA(IDBFileHandle, IDBFileHandle)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBFileHandle::GetDatabase(nsIIDBDatabase** aDatabase)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIDBDatabase> database = do_QueryInterface(mFileStorage);
|
|
|
|
NS_ASSERTION(database, "This should always succeed!");
|
|
|
|
|
|
|
|
database.forget(aDatabase);
|
|
|
|
return NS_OK;
|
|
|
|
}
|