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-06-11 20:35:29 -07:00
|
|
|
#ifndef mozilla_dom_FileHandle_h
|
|
|
|
#define mozilla_dom_FileHandle_h
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#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/ErrorResult.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.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;
|
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
|
|
|
namespace dom {
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
class FileHelper;
|
2014-07-17 09:40:54 -07:00
|
|
|
class FileRequestBase;
|
2014-05-07 07:32:12 -07:00
|
|
|
class FileService;
|
|
|
|
class FinishHelper;
|
2012-06-03 09:33:52 -07:00
|
|
|
class MetadataHelper;
|
2014-07-17 09:40:54 -07:00
|
|
|
class MutableFileBase;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
/**
|
|
|
|
* This class provides a base for FileHandle implementations.
|
|
|
|
*/
|
|
|
|
class FileHandleBase
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum RequestMode
|
|
|
|
{
|
|
|
|
NORMAL = 0, // Sequential
|
|
|
|
PARALLEL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ReadyState
|
|
|
|
{
|
|
|
|
INITIAL = 0,
|
|
|
|
LOADING,
|
|
|
|
FINISHING,
|
|
|
|
DONE
|
|
|
|
};
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
private:
|
|
|
|
friend class FileHelper;
|
|
|
|
friend class FileService;
|
|
|
|
friend class FinishHelper;
|
|
|
|
friend class MetadataHelper;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
ReadyState mReadyState;
|
|
|
|
FileMode mMode;
|
|
|
|
RequestMode mRequestMode;
|
|
|
|
uint64_t mLocation;
|
|
|
|
uint32_t mPendingRequests;
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>> mParallelStreams;
|
|
|
|
nsCOMPtr<nsISupports> mStream;
|
|
|
|
|
|
|
|
bool mAborted;
|
|
|
|
bool mCreating;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
|
|
|
AddRef() = 0;
|
|
|
|
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
|
|
|
Release() = 0;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
CreateParallelStream(nsISupports** aStream);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetOrCreateStream(nsISupports** aStream);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsOpen() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsAborted() const
|
|
|
|
{
|
|
|
|
return mAborted;
|
|
|
|
}
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
void
|
|
|
|
SetCreating()
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-07-17 09:40:54 -07:00
|
|
|
mCreating = true;
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
virtual MutableFileBase*
|
|
|
|
MutableFile() const = 0;
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
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-07-17 09:40:54 -07:00
|
|
|
// Shared WebIDL (IndexedDB FileHandle and FileSystem FileHandle)
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
already_AddRefed<FileRequestBase>
|
|
|
|
Read(uint64_t aSize, bool aHasEncoding, const nsAString& aEncoding,
|
|
|
|
ErrorResult& aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
already_AddRefed<FileRequestBase>
|
2014-03-24 08:10:27 -07:00
|
|
|
Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
already_AddRefed<FileRequestBase>
|
2014-03-24 08:10:27 -07:00
|
|
|
Flush(ErrorResult& aRv);
|
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
void
|
|
|
|
Abort(ErrorResult& aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
protected:
|
|
|
|
FileHandleBase(FileMode aMode,
|
|
|
|
RequestMode aRequestMode);
|
|
|
|
~FileHandleBase();
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
OnNewRequest();
|
|
|
|
|
|
|
|
void
|
|
|
|
OnRequestFinished();
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
void
|
|
|
|
OnReturnToEventLoop();
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
OnCompleteOrAbort(bool aAborted) = 0;
|
|
|
|
|
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-07-17 09:40:54 -07:00
|
|
|
virtual bool
|
|
|
|
CheckWindow() = 0;
|
|
|
|
|
|
|
|
virtual already_AddRefed<FileRequestBase>
|
|
|
|
GenerateFileRequest() = 0;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-04-07 06:03:35 -07:00
|
|
|
template<class T>
|
2014-07-17 09:40:54 -07:00
|
|
|
already_AddRefed<FileRequestBase>
|
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
|
2014-07-17 09:40:54 -07:00
|
|
|
if (!CheckWindow()) {
|
2014-04-07 06:03:35 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteInternal(stream, length, aAppend, aRv);
|
2014-03-24 08:10:27 -07:00
|
|
|
}
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
already_AddRefed<FileRequestBase>
|
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
|
|
|
};
|
|
|
|
|
2012-06-14 19:31:55 -07:00
|
|
|
class FinishHelper MOZ_FINAL : public nsIRunnable
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-07-17 09:40:54 -07:00
|
|
|
friend class FileHandleBase;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
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:
|
2014-07-17 09:40:54 -07:00
|
|
|
FinishHelper(FileHandleBase* aFileHandle);
|
2012-06-03 09:33:52 -07:00
|
|
|
~FinishHelper()
|
|
|
|
{ }
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
nsRefPtr<FileHandleBase> mFileHandle;
|
2014-06-11 20:35:29 -07:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>> mParallelStreams;
|
2012-06-03 09:33:52 -07:00
|
|
|
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-06-11 20:35:29 -07:00
|
|
|
#endif // mozilla_dom_FileHandle_h
|