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