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"
|
|
|
|
|
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"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-06-12 00:00:06 -07:00
|
|
|
#include "mozilla/dom/FileHandleBinding.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIDOMFile.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsNetUtil.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
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-05-07 07:32:12 -07:00
|
|
|
FileHandle::FileHandle(nsPIDOMWindow* aWindow)
|
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileHandle::FileHandle(DOMEventTargetHelper* aOwner)
|
|
|
|
: DOMEventTargetHelper(aOwner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileHandle::~FileHandle()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-07 07:33:02 -07:00
|
|
|
bool
|
|
|
|
FileHandle::IsShuttingDown()
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-05-07 07:33:02 -07:00
|
|
|
return FileService::IsShuttingDown();
|
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*
|
2014-04-08 15:27:18 -07:00
|
|
|
FileHandle::WrapObject(JSContext* aCx)
|
2014-02-24 12:56:13 -08:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return FileHandleBinding::Wrap(aCx, 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());
|
|
|
|
|
2014-05-07 07:33:02 -07:00
|
|
|
if (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());
|
|
|
|
|
|
|
|
nsresult rv =
|
2014-04-09 21:58:41 -07:00
|
|
|
nsContentUtils::WrapNative(aCx, domFile, &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;
|
|
|
|
}
|
2014-05-07 07:32:12 -07:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|