Bug 963064 - FileHandle cleanup. r=bent

This commit is contained in:
Jan Varga 2014-02-24 21:56:13 +01:00
parent 6f4a557e27
commit 914a48b461
13 changed files with 169 additions and 310 deletions

View File

@ -65,12 +65,13 @@
#include "mozilla/layers/ShadowLayers.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/file/FileHandle.h"
#include "mozilla/dom/FileHandleBinding.h"
#include "mozilla/dom/IDBFactoryBinding.h"
#include "mozilla/dom/indexedDB/IndexedDatabaseManager.h"
#include "mozilla/dom/quota/PersistenceType.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "nsDOMBlobBuilder.h"
#include "nsIDOMFileHandle.h"
#include "nsPrintfCString.h"
#include "nsViewportInfo.h"
#include "nsIFormControl.h"
@ -3023,6 +3024,12 @@ nsDOMWindowUtils::GetFileId(JS::Handle<JS::Value> aFile, JSContext* aCx,
if (!JSVAL_IS_PRIMITIVE(aFile)) {
JSObject* obj = JSVAL_TO_OBJECT(aFile);
file::FileHandle* fileHandle;
if (NS_SUCCEEDED(UNWRAP_OBJECT(FileHandle, obj, fileHandle))) {
*aResult = fileHandle->GetFileId();
return NS_OK;
}
nsISupports* nativeObj =
nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, obj);
@ -3031,12 +3038,6 @@ nsDOMWindowUtils::GetFileId(JS::Handle<JS::Value> aFile, JSContext* aCx,
*aResult = blob->GetFileId();
return NS_OK;
}
nsCOMPtr<nsIDOMFileHandle> fileHandle = do_QueryInterface(nativeObj);
if (fileHandle) {
*aResult = fileHandle->GetFileId();
return NS_OK;
}
}
*aResult = -1;

View File

@ -1,66 +0,0 @@
/* -*- 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 "DOMFileHandle.h"
#include "nsIFileStreams.h"
#include "nsDOMClassInfoID.h"
#include "nsNetUtil.h"
#include "File.h"
#include "LockedFile.h"
USING_FILE_NAMESPACE
// static
already_AddRefed<DOMFileHandle>
DOMFileHandle::Create(nsPIDOMWindow* aWindow,
nsIFileStorage* aFileStorage,
nsIFile* aFile)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
nsRefPtr<DOMFileHandle> newFile(new DOMFileHandle(aWindow));
newFile->mFileStorage = aFileStorage;
nsresult rv = aFile->GetLeafName(newFile->mName);
NS_ENSURE_SUCCESS(rv, nullptr);
newFile->mFile = aFile;
newFile->mFileName = newFile->mName;
return newFile.forget();
}
already_AddRefed<nsISupports>
DOMFileHandle::CreateStream(nsIFile* aFile, bool aReadOnly)
{
nsresult rv;
if (aReadOnly) {
nsCOMPtr<nsIInputStream> stream;
rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), aFile, -1, -1,
nsIFileInputStream::DEFER_OPEN);
NS_ENSURE_SUCCESS(rv, nullptr);
return stream.forget();
}
nsCOMPtr<nsIFileStream> stream;
rv = NS_NewLocalFileStream(getter_AddRefs(stream), aFile, -1, -1,
nsIFileStream::DEFER_OPEN);
NS_ENSURE_SUCCESS(rv, nullptr);
return stream.forget();
}
already_AddRefed<nsIDOMFile>
DOMFileHandle::CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize)
{
nsCOMPtr<nsIDOMFile> file =
new File(mName, mType, aFileSize, mFile, aLockedFile);
return file.forget();
}

View File

@ -1,42 +0,0 @@
/* -*- 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/. */
#ifndef mozilla_dom_file_domfilehandle_h__
#define mozilla_dom_file_domfilehandle_h__
#include "mozilla/Attributes.h"
#include "FileCommon.h"
#include "FileHandle.h"
BEGIN_FILE_NAMESPACE
class DOMFileHandle : public FileHandle
{
public:
static already_AddRefed<DOMFileHandle>
Create(nsPIDOMWindow* aWindow,
nsIFileStorage* aFileStorage,
nsIFile* aFile);
virtual already_AddRefed<nsISupports>
CreateStream(nsIFile* aFile, bool aReadOnly) MOZ_OVERRIDE;
virtual already_AddRefed<nsIDOMFile>
CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize) MOZ_OVERRIDE;
protected:
DOMFileHandle(nsPIDOMWindow* aWindow)
: FileHandle(aWindow)
{ }
~DOMFileHandle()
{ }
};
END_FILE_NAMESPACE
#endif // mozilla_dom_file_domfilehandle_h__

View File

@ -8,10 +8,12 @@
#include "nsContentUtils.h"
#include "nsDOMClassInfoID.h"
#include "nsNetUtil.h"
#include "nsIDOMFile.h"
#include "nsIFileStorage.h"
#include "File.h"
#include "FileRequest.h"
#include "FileService.h"
#include "LockedFile.h"
@ -35,12 +37,12 @@ public:
mFileHandle(aFileHandle)
{ }
nsresult
virtual nsresult
GetSuccessResult(JSContext* aCx,
JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
void
ReleaseObjects()
virtual void
ReleaseObjects() MOZ_OVERRIDE
{
mFileHandle = nullptr;
@ -57,51 +59,73 @@ NS_IMPL_CYCLE_COLLECTION_INHERITED_1(FileHandle, nsDOMEventTargetHelper,
mFileStorage)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileHandle)
NS_INTERFACE_MAP_ENTRY(nsIDOMFileHandle)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(FileHandle, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(FileHandle, nsDOMEventTargetHelper)
NS_IMPL_EVENT_HANDLER(FileHandle, abort)
NS_IMPL_EVENT_HANDLER(FileHandle, error)
NS_IMETHODIMP
FileHandle::GetDOMName(nsAString& aName)
// static
already_AddRefed<FileHandle>
FileHandle::Create(nsPIDOMWindow* aWindow,
nsIFileStorage* aFileStorage,
nsIFile* aFile)
{
aName = mName;
return NS_OK;
}
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
NS_IMETHODIMP
FileHandle::GetDOMType(nsAString& aType)
{
aType = mType;
return NS_OK;
}
nsRefPtr<FileHandle> newFileHandle = new FileHandle(aWindow);
NS_IMETHODIMP
FileHandle::Open(const nsAString& aMode,
uint8_t aOptionalArgCount,
nsIDOMLockedFile** _retval)
{
FileMode mode;
if (aOptionalArgCount) {
if (aMode.EqualsLiteral("readwrite")) {
mode = FileMode::Readwrite;
} else if (aMode.EqualsLiteral("readonly")) {
mode = FileMode::Readonly;
} else {
return NS_ERROR_TYPE_ERR;
}
} else {
mode = FileMode::Readonly;
newFileHandle->mFileStorage = aFileStorage;
nsresult rv = aFile->GetLeafName(newFileHandle->mName);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
ErrorResult rv;
nsCOMPtr<nsIDOMLockedFile> lockedFile = Open(mode, rv);
lockedFile.forget(_retval);
return rv.ErrorCode();
newFileHandle->mFile = aFile;
newFileHandle->mFileName = newFileHandle->mName;
return newFileHandle.forget();
}
// virtual
already_AddRefed<nsISupports>
FileHandle::CreateStream(nsIFile* aFile, bool aReadOnly)
{
nsresult rv;
if (aReadOnly) {
nsCOMPtr<nsIInputStream> stream;
rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), aFile, -1, -1,
nsIFileInputStream::DEFER_OPEN);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
return stream.forget();
}
nsCOMPtr<nsIFileStream> stream;
rv = NS_NewLocalFileStream(getter_AddRefs(stream), aFile, -1, -1,
nsIFileStream::DEFER_OPEN);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
return stream.forget();
}
// virtual
already_AddRefed<nsIDOMFile>
FileHandle::CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize)
{
nsCOMPtr<nsIDOMFile> file =
new File(mName, mType, aFileSize, mFile, aLockedFile);
return file.forget();
}
// virtual
JSObject*
FileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return FileHandleBinding::Wrap(aCx, aScope, this);
}
already_AddRefed<nsIDOMLockedFile>
@ -123,15 +147,6 @@ FileHandle::Open(FileMode aMode, ErrorResult& aError)
return lockedFile.forget();
}
NS_IMETHODIMP
FileHandle::GetFile(nsIDOMDOMRequest** _retval)
{
ErrorResult rv;
nsRefPtr<DOMRequest> request = GetFile(rv);
request.forget(_retval);
return rv.ErrorCode();
}
already_AddRefed<DOMRequest>
FileHandle::GetFile(ErrorResult& aError)
{
@ -165,18 +180,6 @@ FileHandle::GetFile(ErrorResult& aError)
return request.forget();
}
NS_IMETHODIMP_(int64_t)
FileHandle::GetFileId()
{
return -1;
}
NS_IMETHODIMP_(mozilla::dom::indexedDB::FileInfo*)
FileHandle::GetFileInfo()
{
return nullptr;
}
nsresult
GetFileHelper::GetSuccessResult(JSContext* aCx,
JS::MutableHandle<JS::Value> aVal)
@ -191,10 +194,3 @@ GetFileHelper::GetSuccessResult(JSContext* aCx,
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
return NS_OK;
}
/* virtual */
JSObject*
FileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return FileHandleBinding::Wrap(aCx, aScope, this);
}

View File

@ -9,7 +9,6 @@
#include "FileCommon.h"
#include "nsIDOMFileHandle.h"
#include "nsIFile.h"
#include "nsIFileStorage.h"
@ -19,12 +18,16 @@
#include "mozilla/dom/FileModeBinding.h"
class nsIDOMFile;
class nsIDOMLockedFile;
class nsIFileStorage;
class nsPIDOMWindow;
namespace mozilla {
namespace dom {
class DOMRequest;
namespace indexedDB {
class FileInfo;
} // namespace indexedDB
} // namespace dom
} // namespace mozilla
@ -36,13 +39,12 @@ class FinishHelper;
class FileHelper;
/**
* Must be subclassed. The subclass must implement CreateStream and
* CreateFileObject. Basically, every file storage implementation provides its
* own FileHandle implementation (for example IDBFileHandle provides IndexedDB
* specific implementation).
* This class provides a default FileHandle implementation, but it can be also
* subclassed. The subclass can override implementation of GetFileId,
* GetFileInfo, CreateStream and CreateFileObject.
* (for example IDBFileHandle provides IndexedDB specific implementation).
*/
class FileHandle : public nsDOMEventTargetHelper,
public nsIDOMFileHandle
class FileHandle : public nsDOMEventTargetHelper
{
friend class FileService;
friend class LockedFile;
@ -51,30 +53,13 @@ class FileHandle : public nsDOMEventTargetHelper,
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIDOMFILEHANDLE
nsPIDOMWindow* GetParentObject() const
{
return GetOwner();
}
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
void GetName(nsString& aName) const
{
aName = mName;
}
void GetType(nsString& aType) const
{
aType = mType;
}
already_AddRefed<nsIDOMLockedFile> Open(FileMode aMode, ErrorResult& aError);
already_AddRefed<DOMRequest> GetFile(ErrorResult& aError);
IMPL_EVENT_HANDLER(abort)
IMPL_EVENT_HANDLER(error)
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileHandle, nsDOMEventTargetHelper)
static already_AddRefed<FileHandle>
Create(nsPIDOMWindow* aWindow,
nsIFileStorage* aFileStorage,
nsIFile* aFile);
const nsAString&
Name() const
{
@ -87,11 +72,55 @@ public:
return mType;
}
virtual int64_t
GetFileId()
{
return -1;
}
virtual mozilla::dom::indexedDB::FileInfo*
GetFileInfo()
{
return nullptr;
}
virtual already_AddRefed<nsISupports>
CreateStream(nsIFile* aFile, bool aReadOnly) = 0;
CreateStream(nsIFile* aFile, bool aReadOnly);
virtual already_AddRefed<nsIDOMFile>
CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize) = 0;
CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize);
// nsWrapperCache
virtual JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
// WebIDL
nsPIDOMWindow*
GetParentObject() const
{
return GetOwner();
}
void
GetName(nsString& aName) const
{
aName = mName;
}
void
GetType(nsString& aType) const
{
aType = mType;
}
already_AddRefed<nsIDOMLockedFile>
Open(FileMode aMode, ErrorResult& aError);
already_AddRefed<DOMRequest>
GetFile(ErrorResult& aError);
IMPL_EVENT_HANDLER(abort)
IMPL_EVENT_HANDLER(error)
protected:
FileHandle(nsPIDOMWindow* aWindow)
@ -105,7 +134,8 @@ protected:
}
~FileHandle()
{ }
{
}
nsCOMPtr<nsIFileStorage> mFileStorage;

View File

@ -458,9 +458,9 @@ LockedFile::IsOpen() const
}
NS_IMETHODIMP
LockedFile::GetFileHandle(nsIDOMFileHandle** aFileHandle)
LockedFile::GetFileHandle(nsISupports** aFileHandle)
{
nsCOMPtr<nsIDOMFileHandle> result(mFileHandle);
nsCOMPtr<nsISupports> result(mFileHandle);
result.forget(aFileHandle);
return NS_OK;
}

View File

@ -7,7 +7,6 @@
TEST_DIRS += ['test']
XPIDL_SOURCES += [
'nsIDOMFileHandle.idl',
'nsIDOMLockedFile.idl',
]
@ -23,7 +22,6 @@ EXPORTS.mozilla.dom.file += [
'ArchiveRequest.h',
'ArchiveZipEvent.h',
'ArchiveZipFile.h',
'DOMFileHandle.h',
'DOMFileRequest.h',
'File.h',
'FileCommon.h',
@ -40,7 +38,6 @@ UNIFIED_SOURCES += [
'ArchiveZipEvent.cpp',
'ArchiveZipFile.cpp',
'AsyncHelper.cpp',
'DOMFileHandle.cpp',
'DOMFileRequest.cpp',
'File.cpp',
'FileHandle.cpp',

View File

@ -1,53 +0,0 @@
/* -*- 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 "nsISupports.idl"
%{C++
namespace mozilla {
namespace dom {
namespace indexedDB {
class FileInfo;
}
}
}
%}
[ptr] native FileInfo(mozilla::dom::indexedDB::FileInfo);
interface nsIDOMDOMRequest;
interface nsIDOMEventListener;
interface nsIDOMLockedFile;
[scriptable, builtinclass, uuid(47438729-7f3f-4a66-b6d4-d1297c593d46)]
interface nsIDOMFileHandle : nsISupports
{
[binaryname(DOMName)]
readonly attribute DOMString name;
[binaryname(DOMType)]
readonly attribute DOMString type;
// mode can be either "readonly" or "readwrite"
[optional_argc]
nsIDOMLockedFile
open([optional /* "readonly" */] in DOMString mode);
nsIDOMDOMRequest
getFile();
[notxpcom]
long long
getFileId();
[notxpcom]
FileInfo
getFileInfo();
[implicit_jscontext] attribute jsval onabort;
[implicit_jscontext] attribute jsval onerror;
};

View File

@ -7,12 +7,11 @@
#include "nsISupports.idl"
interface nsIDOMEventListener;
interface nsIDOMFileHandle;
[scriptable, builtinclass, uuid(a09cdc35-6b1e-42ce-95bb-f8f10a354202)]
interface nsIDOMLockedFile : nsISupports
{
readonly attribute nsIDOMFileHandle fileHandle;
readonly attribute nsISupports /* FileHandle */ fileHandle;
// "readonly" or "readwrite"
readonly attribute DOMString mode;

View File

@ -953,6 +953,5 @@ CreateFileHelper::GetSuccessResult(JSContext* aCx,
IDBFileHandle::Create(mDatabase, mName, mType, mFileInfo.forget());
IDB_ENSURE_TRUE(fileHandle, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
return WrapNative(aCx, NS_ISUPPORTS_CAST(nsIDOMFileHandle*, fileHandle),
aVal);
return WrapNative(aCx, NS_ISUPPORTS_CAST(EventTarget*, fileHandle), aVal);
}

View File

@ -40,13 +40,6 @@ IDBFileHandle::IDBFileHandle(IDBDatabase* aOwner)
{
}
// virtual
JSObject*
IDBFileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return IDBFileHandleBinding::Wrap(aCx, aScope, this);
}
// static
already_AddRefed<IDBFileHandle>
IDBFileHandle::Create(IDBDatabase* aDatabase,
@ -113,6 +106,13 @@ IDBFileHandle::CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile,
return file.forget();
}
// virtual
JSObject*
IDBFileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return IDBFileHandleBinding::Wrap(aCx, aScope, this);
}
IDBDatabase*
IDBFileHandle::Database()
{

View File

@ -18,32 +18,37 @@ class IDBDatabase;
class IDBFileHandle : public file::FileHandle
{
typedef mozilla::dom::file::LockedFile LockedFile;
public:
virtual JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
NS_IMETHOD_(int64_t)
GetFileId()
{
return mFileInfo->Id();
}
NS_IMETHOD_(FileInfo*)
GetFileInfo()
{
return mFileInfo;
}
static already_AddRefed<IDBFileHandle>
Create(IDBDatabase* aDatabase, const nsAString& aName,
const nsAString& aType, already_AddRefed<FileInfo> aFileInfo);
virtual int64_t
GetFileId() MOZ_OVERRIDE
{
return mFileInfo->Id();
}
virtual FileInfo*
GetFileInfo() MOZ_OVERRIDE
{
return mFileInfo;
}
virtual already_AddRefed<nsISupports>
CreateStream(nsIFile* aFile, bool aReadOnly);
CreateStream(nsIFile* aFile, bool aReadOnly) MOZ_OVERRIDE;
virtual already_AddRefed<nsIDOMFile>
CreateFileObject(file::LockedFile* aLockedFile, uint32_t aFileSize);
CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize) MOZ_OVERRIDE;
// nsWrapperCache
virtual JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
// WebIDL
IDBDatabase*
Database();
@ -51,7 +56,8 @@ private:
IDBFileHandle(IDBDatabase* aOwner);
~IDBFileHandle()
{ }
{
}
nsRefPtr<FileInfo> mFileInfo;
};

View File

@ -780,19 +780,11 @@ public:
nsRefPtr<IDBFileHandle> fileHandle = IDBFileHandle::Create(aDatabase,
aData.name, aData.type, fileInfo.forget());
JS::Rooted<JS::Value> wrappedFileHandle(aCx);
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
nsresult rv =
nsContentUtils::WrapNative(aCx, global,
static_cast<nsIDOMFileHandle*>(fileHandle),
&NS_GET_IID(nsIDOMFileHandle),
&wrappedFileHandle);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to wrap native!");
if (!global) {
return nullptr;
}
return JSVAL_TO_OBJECT(wrappedFileHandle);
return fileHandle->WrapObject(aCx, global);
}
static JSObject* CreateAndWrapBlobOrFile(JSContext* aCx,