1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/IDBFileHandle.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "IDBFileHandle.h" 1.11 + 1.12 +#include "mozilla/dom/file/File.h" 1.13 +#include "mozilla/dom/IDBFileHandleBinding.h" 1.14 +#include "mozilla/dom/quota/FileStreams.h" 1.15 + 1.16 +#include "IDBDatabase.h" 1.17 + 1.18 +USING_INDEXEDDB_NAMESPACE 1.19 +USING_QUOTA_NAMESPACE 1.20 + 1.21 +namespace { 1.22 + 1.23 +inline 1.24 +already_AddRefed<nsIFile> 1.25 +GetFileFor(FileInfo* aFileInfo) 1.26 + 1.27 +{ 1.28 + FileManager* fileManager = aFileInfo->Manager(); 1.29 + nsCOMPtr<nsIFile> directory = fileManager->GetDirectory(); 1.30 + NS_ENSURE_TRUE(directory, nullptr); 1.31 + 1.32 + nsCOMPtr<nsIFile> file = fileManager->GetFileForId(directory, 1.33 + aFileInfo->Id()); 1.34 + NS_ENSURE_TRUE(file, nullptr); 1.35 + 1.36 + return file.forget(); 1.37 +} 1.38 + 1.39 +} // anonymous namespace 1.40 + 1.41 +IDBFileHandle::IDBFileHandle(IDBDatabase* aOwner) 1.42 + : FileHandle(aOwner) 1.43 +{ 1.44 +} 1.45 + 1.46 +// static 1.47 +already_AddRefed<IDBFileHandle> 1.48 +IDBFileHandle::Create(IDBDatabase* aDatabase, 1.49 + const nsAString& aName, 1.50 + const nsAString& aType, 1.51 + already_AddRefed<FileInfo> aFileInfo) 1.52 +{ 1.53 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.54 + 1.55 + nsRefPtr<FileInfo> fileInfo(aFileInfo); 1.56 + NS_ASSERTION(fileInfo, "Null pointer!"); 1.57 + 1.58 + nsRefPtr<IDBFileHandle> newFile = new IDBFileHandle(aDatabase); 1.59 + 1.60 + newFile->mFileStorage = aDatabase; 1.61 + newFile->mName = aName; 1.62 + newFile->mType = aType; 1.63 + 1.64 + newFile->mFile = GetFileFor(fileInfo); 1.65 + NS_ENSURE_TRUE(newFile->mFile, nullptr); 1.66 + newFile->mFileName.AppendInt(fileInfo->Id()); 1.67 + 1.68 + fileInfo.swap(newFile->mFileInfo); 1.69 + 1.70 + return newFile.forget(); 1.71 +} 1.72 + 1.73 +already_AddRefed<nsISupports> 1.74 +IDBFileHandle::CreateStream(nsIFile* aFile, bool aReadOnly) 1.75 +{ 1.76 + nsCOMPtr<nsIOfflineStorage> storage = do_QueryInterface(mFileStorage); 1.77 + NS_ASSERTION(storage, "This should always succeed!"); 1.78 + 1.79 + PersistenceType persistenceType = storage->Type(); 1.80 + const nsACString& group = storage->Group(); 1.81 + const nsACString& origin = storage->Origin(); 1.82 + 1.83 + nsCOMPtr<nsISupports> result; 1.84 + 1.85 + if (aReadOnly) { 1.86 + nsRefPtr<FileInputStream> stream = 1.87 + FileInputStream::Create(persistenceType, group, origin, aFile, -1, -1, 1.88 + nsIFileInputStream::DEFER_OPEN); 1.89 + result = NS_ISUPPORTS_CAST(nsIFileInputStream*, stream); 1.90 + } 1.91 + else { 1.92 + nsRefPtr<FileStream> stream = 1.93 + FileStream::Create(persistenceType, group, origin, aFile, -1, -1, 1.94 + nsIFileStream::DEFER_OPEN); 1.95 + result = NS_ISUPPORTS_CAST(nsIFileStream*, stream); 1.96 + } 1.97 + NS_ENSURE_TRUE(result, nullptr); 1.98 + 1.99 + return result.forget(); 1.100 +} 1.101 + 1.102 +already_AddRefed<nsIDOMFile> 1.103 +IDBFileHandle::CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile, 1.104 + uint32_t aFileSize) 1.105 +{ 1.106 + nsCOMPtr<nsIDOMFile> file = new mozilla::dom::file::File( 1.107 + mName, mType, aFileSize, mFile, aLockedFile, mFileInfo); 1.108 + 1.109 + return file.forget(); 1.110 +} 1.111 + 1.112 +// virtual 1.113 +JSObject* 1.114 +IDBFileHandle::WrapObject(JSContext* aCx) 1.115 +{ 1.116 + return IDBFileHandleBinding::Wrap(aCx, this); 1.117 +} 1.118 + 1.119 +IDBDatabase* 1.120 +IDBFileHandle::Database() 1.121 +{ 1.122 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.123 + 1.124 + IDBDatabase* database = static_cast<IDBDatabase*>(mFileStorage.get()); 1.125 + MOZ_ASSERT(database); 1.126 + 1.127 + return database; 1.128 +}