michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "IDBFileHandle.h" michael@0: michael@0: #include "mozilla/dom/file/File.h" michael@0: #include "mozilla/dom/IDBFileHandleBinding.h" michael@0: #include "mozilla/dom/quota/FileStreams.h" michael@0: michael@0: #include "IDBDatabase.h" michael@0: michael@0: USING_INDEXEDDB_NAMESPACE michael@0: USING_QUOTA_NAMESPACE michael@0: michael@0: namespace { michael@0: michael@0: inline michael@0: already_AddRefed michael@0: GetFileFor(FileInfo* aFileInfo) michael@0: michael@0: { michael@0: FileManager* fileManager = aFileInfo->Manager(); michael@0: nsCOMPtr directory = fileManager->GetDirectory(); michael@0: NS_ENSURE_TRUE(directory, nullptr); michael@0: michael@0: nsCOMPtr file = fileManager->GetFileForId(directory, michael@0: aFileInfo->Id()); michael@0: NS_ENSURE_TRUE(file, nullptr); michael@0: michael@0: return file.forget(); michael@0: } michael@0: michael@0: } // anonymous namespace michael@0: michael@0: IDBFileHandle::IDBFileHandle(IDBDatabase* aOwner) michael@0: : FileHandle(aOwner) michael@0: { michael@0: } michael@0: michael@0: // static michael@0: already_AddRefed michael@0: IDBFileHandle::Create(IDBDatabase* aDatabase, michael@0: const nsAString& aName, michael@0: const nsAString& aType, michael@0: already_AddRefed aFileInfo) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: michael@0: nsRefPtr fileInfo(aFileInfo); michael@0: NS_ASSERTION(fileInfo, "Null pointer!"); michael@0: michael@0: nsRefPtr newFile = new IDBFileHandle(aDatabase); michael@0: michael@0: newFile->mFileStorage = aDatabase; michael@0: newFile->mName = aName; michael@0: newFile->mType = aType; michael@0: michael@0: newFile->mFile = GetFileFor(fileInfo); michael@0: NS_ENSURE_TRUE(newFile->mFile, nullptr); michael@0: newFile->mFileName.AppendInt(fileInfo->Id()); michael@0: michael@0: fileInfo.swap(newFile->mFileInfo); michael@0: michael@0: return newFile.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: IDBFileHandle::CreateStream(nsIFile* aFile, bool aReadOnly) michael@0: { michael@0: nsCOMPtr storage = do_QueryInterface(mFileStorage); michael@0: NS_ASSERTION(storage, "This should always succeed!"); michael@0: michael@0: PersistenceType persistenceType = storage->Type(); michael@0: const nsACString& group = storage->Group(); michael@0: const nsACString& origin = storage->Origin(); michael@0: michael@0: nsCOMPtr result; michael@0: michael@0: if (aReadOnly) { michael@0: nsRefPtr stream = michael@0: FileInputStream::Create(persistenceType, group, origin, aFile, -1, -1, michael@0: nsIFileInputStream::DEFER_OPEN); michael@0: result = NS_ISUPPORTS_CAST(nsIFileInputStream*, stream); michael@0: } michael@0: else { michael@0: nsRefPtr stream = michael@0: FileStream::Create(persistenceType, group, origin, aFile, -1, -1, michael@0: nsIFileStream::DEFER_OPEN); michael@0: result = NS_ISUPPORTS_CAST(nsIFileStream*, stream); michael@0: } michael@0: NS_ENSURE_TRUE(result, nullptr); michael@0: michael@0: return result.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: IDBFileHandle::CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile, michael@0: uint32_t aFileSize) michael@0: { michael@0: nsCOMPtr file = new mozilla::dom::file::File( michael@0: mName, mType, aFileSize, mFile, aLockedFile, mFileInfo); michael@0: michael@0: return file.forget(); michael@0: } michael@0: michael@0: // virtual michael@0: JSObject* michael@0: IDBFileHandle::WrapObject(JSContext* aCx) michael@0: { michael@0: return IDBFileHandleBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: IDBDatabase* michael@0: IDBFileHandle::Database() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: michael@0: IDBDatabase* database = static_cast(mFileStorage.get()); michael@0: MOZ_ASSERT(database); michael@0: michael@0: return database; michael@0: }