2014-07-17 20:40:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-07-17 20:40:54 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-02-17 00:46:08 +03:00
|
|
|
#ifndef mozilla_dom_idbfilehandle_h__
|
|
|
|
#define mozilla_dom_idbfilehandle_h__
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
#include "IDBFileRequest.h"
|
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2017-06-07 13:36:42 +03:00
|
|
|
#include "mozilla/dom/FileModeBinding.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2015-09-09 14:15:05 +03:00
|
|
|
#include "nsIRunnable.h"
|
2015-01-12 00:35:24 +03:00
|
|
|
#include "nsWeakReference.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
class nsPIDOMWindowInner;
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
class Blob;
|
|
|
|
class FileRequestData;
|
|
|
|
class FileRequestParams;
|
2014-07-17 20:40:54 +04:00
|
|
|
struct IDBFileMetadataParameters;
|
2015-09-09 14:15:05 +03:00
|
|
|
class IDBFileRequest;
|
2014-07-17 20:40:54 +04:00
|
|
|
class IDBMutableFile;
|
2017-06-07 13:36:42 +03:00
|
|
|
class StringOrArrayBufferOrArrayBufferViewOrBlob;
|
|
|
|
|
|
|
|
namespace indexedDB {
|
|
|
|
class BackgroundFileHandleChild;
|
|
|
|
}
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
class IDBFileHandle final
|
|
|
|
: public DOMEventTargetHelper
|
|
|
|
, public nsIRunnable
|
|
|
|
, public nsSupportsWeakReference
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2017-06-07 13:36:42 +03:00
|
|
|
public:
|
|
|
|
enum ReadyState
|
|
|
|
{
|
|
|
|
INITIAL = 0,
|
|
|
|
LOADING,
|
|
|
|
FINISHING,
|
|
|
|
DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDBMutableFile> mMutableFile;
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
indexedDB::BackgroundFileHandleChild* mBackgroundActor;
|
|
|
|
|
|
|
|
uint64_t mLocation;
|
|
|
|
|
|
|
|
uint32_t mPendingRequestCount;
|
|
|
|
|
|
|
|
ReadyState mReadyState;
|
|
|
|
FileMode mMode;
|
|
|
|
|
|
|
|
bool mAborted;
|
|
|
|
bool mCreating;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool mSentFinishOrAbort;
|
|
|
|
bool mFiredCompleteOrAbort;
|
|
|
|
#endif
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
public:
|
2014-07-17 20:40:54 +04:00
|
|
|
static already_AddRefed<IDBFileHandle>
|
2015-09-09 14:15:05 +03:00
|
|
|
Create(IDBMutableFile* aMutableFile,
|
|
|
|
FileMode aMode);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
static IDBFileHandle*
|
|
|
|
GetCurrent();
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnOwningThread() const
|
|
|
|
#ifdef DEBUG
|
|
|
|
;
|
|
|
|
#else
|
|
|
|
{ }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
SetBackgroundActor(indexedDB::BackgroundFileHandleChild* aActor);
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearBackgroundActor()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
mBackgroundActor = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StartRequest(IDBFileRequest* aFileRequest, const FileRequestParams& aParams);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnNewRequest();
|
|
|
|
|
|
|
|
void
|
|
|
|
OnRequestFinished(bool aActorDestroyedNormally);
|
|
|
|
|
|
|
|
void
|
|
|
|
FireCompleteOrAbortEvents(bool aAborted);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsOpen() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsFinishingOrDone() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
return mReadyState == FINISHING || mReadyState == DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsDone() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
return mReadyState == DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsAborted() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
return mAborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Abort();
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
// WebIDL
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner*
|
2014-07-17 20:40:54 +04:00
|
|
|
GetParentObject() const
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
IDBMutableFile*
|
|
|
|
GetMutableFile() const
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
return mMutableFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDBMutableFile*
|
|
|
|
GetFileHandle() const
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
return GetMutableFile();
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
FileMode
|
|
|
|
Mode() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
return mMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Active() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
return IsOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<uint64_t>
|
|
|
|
GetLocation() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (mLocation == UINT64_MAX) {
|
|
|
|
return Nullable<uint64_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Nullable<uint64_t>(mLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetLocation(const Nullable<uint64_t>& aLocation)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// Null means the end-of-file.
|
|
|
|
if (aLocation.IsNull()) {
|
|
|
|
mLocation = UINT64_MAX;
|
|
|
|
} else {
|
|
|
|
mLocation = aLocation.Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
GetMetadata(const IDBFileMetadataParameters& aParameters, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
ReadAsArrayBuffer(uint64_t aSize, ErrorResult& aRv)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2017-06-07 13:36:42 +03:00
|
|
|
return Read(aSize, false, NullString(), aRv);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
ReadAsText(uint64_t aSize, const nsAString& aEncoding, ErrorResult& aRv)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2017-06-07 13:36:42 +03:00
|
|
|
return Read(aSize, true, aEncoding, aRv);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
2015-09-09 14:15:05 +03:00
|
|
|
Write(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
|
|
|
|
ErrorResult& aRv)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2017-06-07 13:36:42 +03:00
|
|
|
return WriteOrAppend(aValue, false, aRv);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
2015-09-09 14:15:05 +03:00
|
|
|
Append(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
|
|
|
|
ErrorResult& aRv)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2017-06-07 13:36:42 +03:00
|
|
|
return WriteOrAppend(aValue, true, aRv);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
2017-06-07 13:36:42 +03:00
|
|
|
Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
2017-06-07 13:36:42 +03:00
|
|
|
Flush(ErrorResult& aRv);
|
|
|
|
|
|
|
|
void
|
|
|
|
Abort(ErrorResult& aRv);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(complete)
|
|
|
|
IMPL_EVENT_HANDLER(abort)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBFileHandle, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
// nsIDOMEventTarget
|
|
|
|
virtual nsresult
|
2016-10-21 05:11:07 +03:00
|
|
|
GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
|
2015-09-09 14:15:05 +03:00
|
|
|
|
|
|
|
// WrapperCache
|
|
|
|
virtual JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
private:
|
2017-06-07 13:36:42 +03:00
|
|
|
IDBFileHandle(IDBMutableFile* aMutableFile,
|
|
|
|
FileMode aMode);
|
2014-07-17 20:40:54 +04:00
|
|
|
~IDBFileHandle();
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
bool
|
|
|
|
CheckState(ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
CheckStateForWrite(ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
CheckStateForWriteOrAppend(bool aAppend, ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
CheckWindow();
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
Read(uint64_t aSize, bool aHasEncoding, const nsAString& aEncoding,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
WriteOrAppend(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
WriteOrAppend(const nsAString& aValue, bool aAppend, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
WriteOrAppend(const ArrayBuffer& aValue, bool aAppend, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
WriteOrAppend(const ArrayBufferView& aValue, bool aAppend, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
WriteOrAppend(Blob& aValue, bool aAppend, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
WriteInternal(const FileRequestData& aData, uint64_t aDataLength,
|
|
|
|
bool aAppend, ErrorResult& aRv);
|
|
|
|
|
|
|
|
void
|
|
|
|
SendFinish();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
void
|
|
|
|
SendAbort();
|
2014-07-17 20:40:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-02-17 00:46:08 +03:00
|
|
|
#endif // mozilla_dom_idbfilehandle_h__
|