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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_file_lockedfile_h__
|
|
|
|
#define mozilla_dom_file_lockedfile_h__
|
|
|
|
|
|
|
|
#include "FileCommon.h"
|
2014-03-24 08:10:27 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-03-31 23:13:50 -07:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2013-06-12 00:00:06 -07:00
|
|
|
#include "mozilla/dom/FileModeBinding.h"
|
2014-03-24 08:10:27 -07:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIRunnable.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class DOMFileMetadataParameters;
|
|
|
|
class DOMRequest;
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-03-17 21:48:19 -07:00
|
|
|
namespace mozilla {
|
|
|
|
class EventChainPreVisitor;
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
BEGIN_FILE_NAMESPACE
|
|
|
|
|
|
|
|
class FileHandle;
|
|
|
|
class FileRequest;
|
|
|
|
class MetadataHelper;
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
class LockedFile : public DOMEventTargetHelper,
|
2012-06-03 09:33:52 -07:00
|
|
|
public nsIRunnable
|
|
|
|
{
|
|
|
|
friend class FinishHelper;
|
|
|
|
friend class FileService;
|
|
|
|
friend class FileHelper;
|
|
|
|
friend class MetadataHelper;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LockedFile, DOMEventTargetHelper)
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
enum RequestMode
|
|
|
|
{
|
|
|
|
NORMAL = 0, // Sequential
|
|
|
|
PARALLEL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ReadyState
|
|
|
|
{
|
|
|
|
INITIAL = 0,
|
|
|
|
LOADING,
|
|
|
|
FINISHING,
|
|
|
|
DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
static already_AddRefed<LockedFile>
|
|
|
|
Create(FileHandle* aFileHandle,
|
2013-06-12 00:00:06 -07:00
|
|
|
FileMode aMode,
|
2012-06-03 09:33:52 -07:00
|
|
|
RequestMode aRequestMode = NORMAL);
|
|
|
|
|
|
|
|
// nsIDOMEventTarget
|
|
|
|
virtual nsresult
|
2014-03-17 21:48:19 -07:00
|
|
|
PreHandleEvent(EventChainPreVisitor& aVisitor) MOZ_OVERRIDE;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
CreateParallelStream(nsISupports** aStream);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetOrCreateStream(nsISupports** aStream);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsOpen() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsAborted() const
|
|
|
|
{
|
|
|
|
return mAborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileHandle*
|
|
|
|
Handle() const
|
|
|
|
{
|
|
|
|
return mFileHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
OpenInputStream(bool aWholeFile, uint64_t aStart, uint64_t aLength,
|
2012-06-03 09:33:52 -07:00
|
|
|
nsIInputStream** aResult);
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
// WrapperCache
|
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
FileHandle* GetFileHandle() const
|
|
|
|
{
|
|
|
|
return Handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileMode Mode() const
|
|
|
|
{
|
|
|
|
return mMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Active() const
|
|
|
|
{
|
|
|
|
return IsOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<uint64_t> GetLocation() const
|
|
|
|
{
|
|
|
|
if (mLocation == UINT64_MAX) {
|
|
|
|
return Nullable<uint64_t>();
|
|
|
|
}
|
|
|
|
return Nullable<uint64_t>(mLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLocation(const Nullable<uint64_t>& aLocation)
|
|
|
|
{
|
|
|
|
// Null means the end-of-file.
|
|
|
|
if (aLocation.IsNull()) {
|
|
|
|
mLocation = UINT64_MAX;
|
|
|
|
} else {
|
|
|
|
mLocation = aLocation.Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
GetMetadata(const DOMFileMetadataParameters& aParameters, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
ReadAsArrayBuffer(uint64_t aSize, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
ReadAsText(uint64_t aSize, const nsAString& aEncoding, ErrorResult& aRv);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Write(const T& aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint64_t length;
|
|
|
|
nsCOMPtr<nsIInputStream> stream = GetInputStream(aValue, &length, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return Write(stream, length, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Append(const T& aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint64_t length;
|
|
|
|
nsCOMPtr<nsIInputStream> stream = GetInputStream(aValue, &length, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return Append(stream, length, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Flush(ErrorResult& aRv);
|
|
|
|
|
|
|
|
void Abort(ErrorResult& aRv);
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(complete)
|
|
|
|
IMPL_EVENT_HANDLER(abort)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
private:
|
|
|
|
LockedFile();
|
|
|
|
~LockedFile();
|
|
|
|
|
|
|
|
void
|
|
|
|
OnNewRequest();
|
|
|
|
|
|
|
|
void
|
|
|
|
OnRequestFinished();
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
bool CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
|
|
|
|
bool CheckStateForWrite(ErrorResult& aRv);
|
|
|
|
|
2014-03-16 09:53:44 -07:00
|
|
|
already_AddRefed<FileRequest>
|
2012-06-03 09:33:52 -07:00
|
|
|
GenerateFileRequest();
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Write(nsIInputStream* aInputStream, uint64_t aInputLength, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return WriteOrAppend(aInputStream, aInputLength, false, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Append(nsIInputStream* aInputStream, uint64_t aInputLength, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return WriteOrAppend(aInputStream, aInputLength, true, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
WriteOrAppend(nsIInputStream* aInputStream, uint64_t aInputLength,
|
|
|
|
bool aAppend, ErrorResult& aRv);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
Finish();
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
static already_AddRefed<nsIInputStream>
|
|
|
|
GetInputStream(const ArrayBuffer& aValue, uint64_t* aInputLength,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
static already_AddRefed<nsIInputStream>
|
|
|
|
GetInputStream(nsIDOMBlob* aValue, uint64_t* aInputLength, ErrorResult& aRv);
|
|
|
|
static already_AddRefed<nsIInputStream>
|
|
|
|
GetInputStream(const nsAString& aValue, uint64_t* aInputLength,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
nsRefPtr<FileHandle> mFileHandle;
|
|
|
|
ReadyState mReadyState;
|
2013-06-12 00:00:06 -07:00
|
|
|
FileMode mMode;
|
2012-06-03 09:33:52 -07:00
|
|
|
RequestMode mRequestMode;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mLocation;
|
|
|
|
uint32_t mPendingRequests;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mParallelStreams;
|
|
|
|
nsCOMPtr<nsISupports> mStream;
|
|
|
|
|
|
|
|
bool mAborted;
|
|
|
|
bool mCreating;
|
|
|
|
};
|
|
|
|
|
2012-06-14 19:31:55 -07:00
|
|
|
class FinishHelper MOZ_FINAL : public nsIRunnable
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
friend class LockedFile;
|
|
|
|
|
|
|
|
public:
|
2013-07-18 19:21:20 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-06-03 09:33:52 -07:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
private:
|
|
|
|
FinishHelper(LockedFile* aLockedFile);
|
|
|
|
~FinishHelper()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsRefPtr<LockedFile> mLockedFile;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mParallelStreams;
|
|
|
|
nsCOMPtr<nsISupports> mStream;
|
|
|
|
|
|
|
|
bool mAborted;
|
|
|
|
};
|
|
|
|
|
|
|
|
END_FILE_NAMESPACE
|
|
|
|
|
|
|
|
#endif // mozilla_dom_file_lockedfile_h__
|