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.

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

mercurial