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/. */
|
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
#ifndef mozilla_dom_LockedFile_h
|
|
|
|
#define mozilla_dom_LockedFile_h
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-03-24 08:10:27 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2013-06-12 00:00:06 -07:00
|
|
|
#include "mozilla/dom/FileModeBinding.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/dom/Nullable.h"
|
2014-03-24 08:10:27 -07:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-03-24 08:10:27 -07:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIRunnable.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "nsTArray.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
class nsAString;
|
|
|
|
class nsIDOMBlob;
|
|
|
|
class nsPIDOMWindow;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-03-17 21:48:19 -07:00
|
|
|
namespace mozilla {
|
2014-05-07 07:32:12 -07:00
|
|
|
|
2014-03-17 21:48:19 -07:00
|
|
|
class EventChainPreVisitor;
|
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
namespace dom {
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
class DOMFileMetadataParameters;
|
2012-06-03 09:33:52 -07:00
|
|
|
class FileHandle;
|
2014-05-07 07:32:12 -07:00
|
|
|
class FileHelper;
|
2012-06-03 09:33:52 -07:00
|
|
|
class FileRequest;
|
2014-05-07 07:32:12 -07:00
|
|
|
class FileService;
|
|
|
|
class FinishHelper;
|
2012-06-03 09:33:52 -07:00
|
|
|
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 FileHelper;
|
2014-05-07 07:32:12 -07:00
|
|
|
friend class FileService;
|
|
|
|
friend class FinishHelper;
|
2012-06-03 09:33:52 -07:00
|
|
|
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
|
|
|
|
virtual JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2014-03-24 08:10:27 -07:00
|
|
|
|
|
|
|
// WebIDL
|
2014-04-07 06:03:35 -07:00
|
|
|
nsPIDOMWindow*
|
|
|
|
GetParentObject() const
|
2014-03-24 08:10:27 -07:00
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileHandle*
|
|
|
|
GetFileHandle() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
return Handle();
|
|
|
|
}
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
FileMode
|
|
|
|
Mode() const
|
2014-03-24 08:10:27 -07:00
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
return mMode;
|
|
|
|
}
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
bool
|
|
|
|
Active() const
|
2014-03-24 08:10:27 -07:00
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
return IsOpen();
|
|
|
|
}
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
Nullable<uint64_t>
|
|
|
|
GetLocation() const
|
2014-03-24 08:10:27 -07:00
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
if (mLocation == UINT64_MAX) {
|
|
|
|
return Nullable<uint64_t>();
|
|
|
|
}
|
2014-04-07 06:03:35 -07:00
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
return Nullable<uint64_t>(mLocation);
|
|
|
|
}
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
void
|
|
|
|
SetLocation(const Nullable<uint64_t>& aLocation)
|
2014-03-24 08:10:27 -07:00
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
// 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)
|
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return WriteOrAppend(aValue, false, aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Append(const T& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return WriteOrAppend(aValue, true, aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
Flush(ErrorResult& aRv);
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
void
|
|
|
|
Abort(ErrorResult& aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
|
|
|
|
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-04-07 06:03:35 -07:00
|
|
|
bool
|
|
|
|
CheckState(ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
CheckStateForWrite(ErrorResult& aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
|
2014-03-16 09:53:44 -07:00
|
|
|
already_AddRefed<FileRequest>
|
2012-06-03 09:33:52 -07:00
|
|
|
GenerateFileRequest();
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
template<class T>
|
2014-03-24 08:10:27 -07:00
|
|
|
already_AddRefed<FileRequest>
|
2014-04-07 06:03:35 -07:00
|
|
|
WriteOrAppend(const T& aValue, bool aAppend, ErrorResult& aRv)
|
2014-03-24 08:10:27 -07:00
|
|
|
{
|
2014-04-07 06:03:35 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2014-03-24 08:10:27 -07:00
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
// State checking for write
|
|
|
|
if (!CheckStateForWrite(aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additional state checking for write
|
|
|
|
if (!aAppend && mLocation == UINT64_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t length;
|
|
|
|
nsCOMPtr<nsIInputStream> stream = GetInputStream(aValue, &length, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!length) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!GetOwner()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteInternal(stream, length, aAppend, aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<FileRequest>
|
2014-04-07 06:03:35 -07:00
|
|
|
WriteInternal(nsIInputStream* aInputStream, uint64_t aInputLength,
|
2014-03-24 08:10:27 -07:00
|
|
|
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);
|
2014-04-07 06:03:35 -07:00
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
static already_AddRefed<nsIInputStream>
|
|
|
|
GetInputStream(nsIDOMBlob* aValue, uint64_t* aInputLength, ErrorResult& aRv);
|
2014-04-07 06:03:35 -07:00
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
#endif // mozilla_dom_LockedFile_h
|