2012-06-03 09:33:52 -07: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"
|
2014-02-24 12:56:13 -08:00
|
|
|
#include "nsNetUtil.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
#include "nsIDOMFile.h"
|
|
|
|
#include "nsIFileStorage.h"
|
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
#include "File.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
#include "FileRequest.h"
|
|
|
|
#include "FileService.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
#include "MetadataHelper.h"
|
2013-06-12 00:00:06 -07:00
|
|
|
#include "mozilla/dom/FileHandleBinding.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2012-12-02 00:58:57 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2012-06-03 09:33:52 -07: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)
|
|
|
|
{ }
|
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
virtual nsresult
|
2014-01-23 11:49:40 -08:00
|
|
|
GetSuccessResult(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
virtual void
|
|
|
|
ReleaseObjects() MOZ_OVERRIDE
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mFileHandle = nullptr;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
MetadataHelper::ReleaseObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<FileHandle> mFileHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(FileHandle, DOMEventTargetHelper,
|
2012-12-02 00:53:35 -08:00
|
|
|
mFileStorage)
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileHandle)
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(FileHandle, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileHandle, DOMEventTargetHelper)
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
// static
|
|
|
|
already_AddRefed<FileHandle>
|
|
|
|
FileHandle::Create(nsPIDOMWindow* aWindow,
|
|
|
|
nsIFileStorage* aFileStorage,
|
|
|
|
nsIFile* aFile)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-02-24 12:56:13 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
nsRefPtr<FileHandle> newFileHandle = new FileHandle(aWindow);
|
|
|
|
|
|
|
|
newFileHandle->mFileStorage = aFileStorage;
|
|
|
|
nsresult rv = aFile->GetLeafName(newFileHandle->mName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
newFileHandle->mFile = aFile;
|
|
|
|
newFileHandle->mFileName = newFileHandle->mName;
|
|
|
|
|
|
|
|
return newFileHandle.forget();
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
// virtual
|
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
FileHandle::CreateStream(nsIFile* aFile, bool aReadOnly)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-02-24 12:56:13 -08:00
|
|
|
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;
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
2014-02-24 12:56:13 -08:00
|
|
|
return stream.forget();
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
2012-12-02 00:58:57 -08:00
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
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);
|
2012-12-02 00:58:57 -08:00
|
|
|
}
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
already_AddRefed<LockedFile>
|
2012-12-02 00:58:57 -08:00
|
|
|
FileHandle::Open(FileMode aMode, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
if (FileService::IsShuttingDown() || mFileStorage->IsShuttingDown()) {
|
2012-12-02 00:58:57 -08:00
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2013-06-12 00:00:06 -07:00
|
|
|
nsRefPtr<LockedFile> lockedFile = LockedFile::Create(this, aMode);
|
2012-12-02 00:58:57 -08:00
|
|
|
if (!lockedFile) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lockedFile.forget();
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2013-03-17 01:51:36 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-12-02 00:58:57 -08:00
|
|
|
FileHandle::GetFile(ErrorResult& aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!GetOwner()) {
|
2012-12-02 00:58:57 -08:00
|
|
|
return nullptr;
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<LockedFile> lockedFile =
|
2013-06-12 00:00:06 -07:00
|
|
|
LockedFile::Create(this, FileMode::Readonly, LockedFile::PARALLEL);
|
2012-12-02 00:58:57 -08:00
|
|
|
if (!lockedFile) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-03-16 09:53:44 -07:00
|
|
|
nsRefPtr<FileRequest> request =
|
|
|
|
FileRequest::Create(GetOwner(), lockedFile, /* aWrapAsDOMRequest */ true);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-01-19 23:58:24 -08:00
|
|
|
nsRefPtr<MetadataParameters> params = new MetadataParameters(true, false);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsRefPtr<GetFileHelper> helper =
|
2012-07-09 22:07:01 -07:00
|
|
|
new GetFileHelper(lockedFile, request, params, this);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsresult rv = helper->Enqueue();
|
2012-12-02 00:58:57 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-09 22:07:01 -07:00
|
|
|
|
2013-03-17 01:51:36 -07:00
|
|
|
return request.forget();
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-23 11:49:40 -08:00
|
|
|
GetFileHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMFile> domFile =
|
|
|
|
mFileHandle->CreateFileObject(mLockedFile, mParams->Size());
|
|
|
|
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2012-06-03 09:33:52 -07:00
|
|
|
nsresult rv =
|
2013-05-02 02:12:46 -07:00
|
|
|
nsContentUtils::WrapNative(aCx, global, domFile,
|
2014-01-23 11:49:40 -08:00
|
|
|
&NS_GET_IID(nsIDOMFile), aVal);
|
2012-06-03 09:33:52 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return NS_OK;
|
|
|
|
}
|