2012-06-03 16:33:52 +00:00
|
|
|
/* -*- 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 "FileHandle.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMClassInfoID.h"
|
|
|
|
|
|
|
|
#include "nsIDOMFile.h"
|
|
|
|
#include "nsIFileStorage.h"
|
|
|
|
|
|
|
|
#include "FileRequest.h"
|
|
|
|
#include "FileService.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
#include "MetadataHelper.h"
|
|
|
|
|
2012-12-02 08:58:57 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2012-06-03 16:33:52 +00:00
|
|
|
USING_FILE_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GetFileHelper : public MetadataHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetFileHelper(LockedFile* aLockedFile,
|
|
|
|
FileRequest* aFileRequest,
|
|
|
|
MetadataParameters* aParams,
|
|
|
|
FileHandle* aFileHandle)
|
|
|
|
: MetadataHelper(aLockedFile, aFileRequest, aParams),
|
|
|
|
mFileHandle(aFileHandle)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetSuccessResult(JSContext* aCx, jsval* aVal);
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseObjects()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
mFileHandle = nullptr;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
MetadataHelper::ReleaseObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<FileHandle> mFileHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-12-02 08:53:35 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(FileHandle, nsDOMEventTargetHelper,
|
|
|
|
mFileStorage)
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2012-06-04 14:57:25 +00:00
|
|
|
NS_IMPL_EVENT_HANDLER(FileHandle, abort)
|
|
|
|
NS_IMPL_EVENT_HANDLER(FileHandle, error)
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-02 08:58:57 +00:00
|
|
|
FileHandle::GetDOMName(nsAString& aName)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
aName = mName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-02 08:58:57 +00:00
|
|
|
FileHandle::GetDOMType(nsAString& aType)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileHandle::Open(const nsAString& aMode,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t aOptionalArgCount,
|
2012-06-03 16:33:52 +00:00
|
|
|
nsIDOMLockedFile** _retval)
|
|
|
|
{
|
2012-12-02 08:58:57 +00:00
|
|
|
FileMode mode;
|
2012-06-03 16:33:52 +00:00
|
|
|
if (aOptionalArgCount) {
|
|
|
|
if (aMode.EqualsLiteral("readwrite")) {
|
2012-12-02 08:58:57 +00:00
|
|
|
mode = FileModeValues::Readwrite;
|
|
|
|
} else if (aMode.EqualsLiteral("readonly")) {
|
|
|
|
mode = FileModeValues::Readonly;
|
|
|
|
} else {
|
2012-06-03 16:33:52 +00:00
|
|
|
return NS_ERROR_TYPE_ERR;
|
|
|
|
}
|
2012-12-02 08:58:57 +00:00
|
|
|
} else {
|
|
|
|
mode = FileModeValues::Readonly;
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
2012-12-02 08:58:57 +00:00
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsIDOMLockedFile> lockedFile = Open(mode, rv);
|
|
|
|
lockedFile.forget(_retval);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMLockedFile>
|
|
|
|
FileHandle::Open(FileMode aMode, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (FileService::IsShuttingDown() || mFileStorage->IsStorageShuttingDown()) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-02 08:58:57 +00:00
|
|
|
MOZ_STATIC_ASSERT(static_cast<uint32_t>(FileModeValues::Readonly) ==
|
|
|
|
static_cast<uint32_t>(LockedFile::READ_ONLY),
|
|
|
|
"Enum values should match.");
|
|
|
|
MOZ_STATIC_ASSERT(static_cast<uint32_t>(FileModeValues::Readwrite) ==
|
|
|
|
static_cast<uint32_t>(LockedFile::READ_WRITE),
|
|
|
|
"Enum values should match.");
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2012-12-02 08:58:57 +00:00
|
|
|
nsRefPtr<LockedFile> lockedFile =
|
|
|
|
LockedFile::Create(this, static_cast<LockedFile::Mode>(aMode));
|
|
|
|
if (!lockedFile) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lockedFile.forget();
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-10 05:07:01 +00:00
|
|
|
FileHandle::GetFile(nsIDOMDOMRequest** _retval)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
2012-12-02 08:58:57 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsIDOMDOMRequest> request = GetFile(rv);
|
|
|
|
request.forget(_retval);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMDOMRequest>
|
|
|
|
FileHandle::GetFile(ErrorResult& aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!GetOwner()) {
|
2012-12-02 08:58:57 +00:00
|
|
|
return nullptr;
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<LockedFile> lockedFile =
|
|
|
|
LockedFile::Create(this, LockedFile::READ_ONLY, LockedFile::PARALLEL);
|
2012-12-02 08:58:57 +00:00
|
|
|
if (!lockedFile) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2012-07-10 05:07:01 +00:00
|
|
|
nsRefPtr<FileRequest> request =
|
|
|
|
FileRequest::Create(GetOwner(), lockedFile, false);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
nsRefPtr<MetadataParameters> params = new MetadataParameters();
|
|
|
|
params->Init(true, false);
|
|
|
|
|
|
|
|
nsRefPtr<GetFileHelper> helper =
|
2012-07-10 05:07:01 +00:00
|
|
|
new GetFileHelper(lockedFile, request, params, this);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
nsresult rv = helper->Enqueue();
|
2012-12-02 08:58:57 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-10 05:07:01 +00:00
|
|
|
|
2012-12-02 08:58:57 +00:00
|
|
|
nsRefPtr<DOMRequest> domRequest = request.forget();
|
|
|
|
return domRequest.forget();
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHODIMP_(int64_t)
|
2012-06-03 16:33:52 +00:00
|
|
|
FileHandle::GetFileId()
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(mozilla::dom::indexedDB::FileInfo*)
|
|
|
|
FileHandle::GetFileInfo()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetFileHelper::GetSuccessResult(JSContext* aCx, jsval* aVal)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMFile> domFile =
|
|
|
|
mFileHandle->CreateFileObject(mLockedFile, mParams->Size());
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::WrapNative(aCx, JS_GetGlobalForScopeChain(aCx), domFile,
|
|
|
|
&NS_GET_IID(nsIDOMFile), aVal);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-02 08:58:57 +00:00
|
|
|
|
|
|
|
/* virtual */
|
|
|
|
JSObject*
|
2013-03-11 23:03:47 +00:00
|
|
|
FileHandle::WrapObject(JSContext* aCx, JSObject* aScope)
|
2012-12-02 08:58:57 +00:00
|
|
|
{
|
2013-03-11 23:03:47 +00:00
|
|
|
return FileHandleBinding::Wrap(aCx, aScope, this);
|
2012-12-02 08:58:57 +00:00
|
|
|
}
|