dom/indexedDB/IDBFileHandle.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "IDBFileHandle.h"
michael@0 8
michael@0 9 #include "mozilla/dom/file/File.h"
michael@0 10 #include "mozilla/dom/IDBFileHandleBinding.h"
michael@0 11 #include "mozilla/dom/quota/FileStreams.h"
michael@0 12
michael@0 13 #include "IDBDatabase.h"
michael@0 14
michael@0 15 USING_INDEXEDDB_NAMESPACE
michael@0 16 USING_QUOTA_NAMESPACE
michael@0 17
michael@0 18 namespace {
michael@0 19
michael@0 20 inline
michael@0 21 already_AddRefed<nsIFile>
michael@0 22 GetFileFor(FileInfo* aFileInfo)
michael@0 23
michael@0 24 {
michael@0 25 FileManager* fileManager = aFileInfo->Manager();
michael@0 26 nsCOMPtr<nsIFile> directory = fileManager->GetDirectory();
michael@0 27 NS_ENSURE_TRUE(directory, nullptr);
michael@0 28
michael@0 29 nsCOMPtr<nsIFile> file = fileManager->GetFileForId(directory,
michael@0 30 aFileInfo->Id());
michael@0 31 NS_ENSURE_TRUE(file, nullptr);
michael@0 32
michael@0 33 return file.forget();
michael@0 34 }
michael@0 35
michael@0 36 } // anonymous namespace
michael@0 37
michael@0 38 IDBFileHandle::IDBFileHandle(IDBDatabase* aOwner)
michael@0 39 : FileHandle(aOwner)
michael@0 40 {
michael@0 41 }
michael@0 42
michael@0 43 // static
michael@0 44 already_AddRefed<IDBFileHandle>
michael@0 45 IDBFileHandle::Create(IDBDatabase* aDatabase,
michael@0 46 const nsAString& aName,
michael@0 47 const nsAString& aType,
michael@0 48 already_AddRefed<FileInfo> aFileInfo)
michael@0 49 {
michael@0 50 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 51
michael@0 52 nsRefPtr<FileInfo> fileInfo(aFileInfo);
michael@0 53 NS_ASSERTION(fileInfo, "Null pointer!");
michael@0 54
michael@0 55 nsRefPtr<IDBFileHandle> newFile = new IDBFileHandle(aDatabase);
michael@0 56
michael@0 57 newFile->mFileStorage = aDatabase;
michael@0 58 newFile->mName = aName;
michael@0 59 newFile->mType = aType;
michael@0 60
michael@0 61 newFile->mFile = GetFileFor(fileInfo);
michael@0 62 NS_ENSURE_TRUE(newFile->mFile, nullptr);
michael@0 63 newFile->mFileName.AppendInt(fileInfo->Id());
michael@0 64
michael@0 65 fileInfo.swap(newFile->mFileInfo);
michael@0 66
michael@0 67 return newFile.forget();
michael@0 68 }
michael@0 69
michael@0 70 already_AddRefed<nsISupports>
michael@0 71 IDBFileHandle::CreateStream(nsIFile* aFile, bool aReadOnly)
michael@0 72 {
michael@0 73 nsCOMPtr<nsIOfflineStorage> storage = do_QueryInterface(mFileStorage);
michael@0 74 NS_ASSERTION(storage, "This should always succeed!");
michael@0 75
michael@0 76 PersistenceType persistenceType = storage->Type();
michael@0 77 const nsACString& group = storage->Group();
michael@0 78 const nsACString& origin = storage->Origin();
michael@0 79
michael@0 80 nsCOMPtr<nsISupports> result;
michael@0 81
michael@0 82 if (aReadOnly) {
michael@0 83 nsRefPtr<FileInputStream> stream =
michael@0 84 FileInputStream::Create(persistenceType, group, origin, aFile, -1, -1,
michael@0 85 nsIFileInputStream::DEFER_OPEN);
michael@0 86 result = NS_ISUPPORTS_CAST(nsIFileInputStream*, stream);
michael@0 87 }
michael@0 88 else {
michael@0 89 nsRefPtr<FileStream> stream =
michael@0 90 FileStream::Create(persistenceType, group, origin, aFile, -1, -1,
michael@0 91 nsIFileStream::DEFER_OPEN);
michael@0 92 result = NS_ISUPPORTS_CAST(nsIFileStream*, stream);
michael@0 93 }
michael@0 94 NS_ENSURE_TRUE(result, nullptr);
michael@0 95
michael@0 96 return result.forget();
michael@0 97 }
michael@0 98
michael@0 99 already_AddRefed<nsIDOMFile>
michael@0 100 IDBFileHandle::CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile,
michael@0 101 uint32_t aFileSize)
michael@0 102 {
michael@0 103 nsCOMPtr<nsIDOMFile> file = new mozilla::dom::file::File(
michael@0 104 mName, mType, aFileSize, mFile, aLockedFile, mFileInfo);
michael@0 105
michael@0 106 return file.forget();
michael@0 107 }
michael@0 108
michael@0 109 // virtual
michael@0 110 JSObject*
michael@0 111 IDBFileHandle::WrapObject(JSContext* aCx)
michael@0 112 {
michael@0 113 return IDBFileHandleBinding::Wrap(aCx, this);
michael@0 114 }
michael@0 115
michael@0 116 IDBDatabase*
michael@0 117 IDBFileHandle::Database()
michael@0 118 {
michael@0 119 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 120
michael@0 121 IDBDatabase* database = static_cast<IDBDatabase*>(mFileStorage.get());
michael@0 122 MOZ_ASSERT(database);
michael@0 123
michael@0 124 return database;
michael@0 125 }

mercurial