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/. */
|
|
|
|
|
|
|
|
#include "IDBFileHandle.h"
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
#include "ActorsChild.h"
|
|
|
|
#include "BackgroundChildImpl.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "IDBEvents.h"
|
|
|
|
#include "IDBMutableFile.h"
|
2015-09-09 14:15:05 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2017-06-07 13:36:42 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "mozilla/dom/IDBFileHandleBinding.h"
|
2017-06-07 13:36:42 +03:00
|
|
|
#include "mozilla/dom/IPCBlobUtils.h"
|
|
|
|
#include "mozilla/dom/PBackgroundFileHandle.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2017-06-07 13:36:42 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2017-06-07 13:36:20 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsQueryObject.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2016-02-17 00:46:08 +03:00
|
|
|
|
|
|
|
using namespace mozilla::dom::indexedDB;
|
2017-06-07 13:36:42 +03:00
|
|
|
using namespace mozilla::ipc;
|
2014-09-27 03:21:57 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
GenerateFileRequest(IDBFileHandle* aFileHandle)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2017-06-07 13:36:42 +03:00
|
|
|
MOZ_ASSERT(aFileHandle);
|
|
|
|
aFileHandle->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
RefPtr<IDBFileRequest> fileRequest =
|
|
|
|
IDBFileRequest::Create(aFileHandle, /* aWrapAsDOMRequest */ false);
|
|
|
|
MOZ_ASSERT(fileRequest);
|
|
|
|
|
|
|
|
return fileRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
IDBFileHandle::IDBFileHandle(IDBMutableFile* aMutableFile,
|
|
|
|
FileMode aMode)
|
|
|
|
: mMutableFile(aMutableFile)
|
|
|
|
, mBackgroundActor(nullptr)
|
|
|
|
, mLocation(0)
|
|
|
|
, mPendingRequestCount(0)
|
|
|
|
, mReadyState(INITIAL)
|
|
|
|
, mMode(aMode)
|
|
|
|
, mAborted(false)
|
|
|
|
, mCreating(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
, mSentFinishOrAbort(false)
|
|
|
|
, mFiredCompleteOrAbort(false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aMutableFile);
|
|
|
|
aMutableFile->AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IDBFileHandle::~IDBFileHandle()
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2017-06-07 13:36:42 +03:00
|
|
|
MOZ_ASSERT(!mPendingRequestCount);
|
|
|
|
MOZ_ASSERT(!mCreating);
|
|
|
|
MOZ_ASSERT(mSentFinishOrAbort);
|
|
|
|
MOZ_ASSERT_IF(mBackgroundActor, mFiredCompleteOrAbort);
|
2015-09-09 14:15:05 +03:00
|
|
|
|
|
|
|
mMutableFile->UnregisterFileHandle(this);
|
2017-06-07 13:36:42 +03:00
|
|
|
|
|
|
|
if (mBackgroundActor) {
|
|
|
|
mBackgroundActor->SendDeleteMeInternal();
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mBackgroundActor, "SendDeleteMeInternal should have cleared!");
|
|
|
|
}
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<IDBFileHandle>
|
2015-09-09 14:15:05 +03:00
|
|
|
IDBFileHandle::Create(IDBMutableFile* aMutableFile,
|
|
|
|
FileMode aMode)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
MOZ_ASSERT(aMutableFile);
|
|
|
|
aMutableFile->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aMode == FileMode::Readonly || aMode == FileMode::Readwrite);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDBFileHandle> fileHandle =
|
2017-06-07 13:36:42 +03:00
|
|
|
new IDBFileHandle(aMutableFile, aMode);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
fileHandle->BindToOwner(aMutableFile);
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
// XXX Fix!
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "This won't work on non-main threads!");
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = do_QueryObject(fileHandle);
|
2017-11-17 06:01:27 +03:00
|
|
|
nsContentUtils::AddPendingIDBTransaction(runnable.forget());
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
fileHandle->mCreating = true;
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
aMutableFile->RegisterFileHandle(fileHandle);
|
|
|
|
|
|
|
|
return fileHandle.forget();
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
// static
|
|
|
|
IDBFileHandle*
|
|
|
|
IDBFileHandle::GetCurrent()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(BackgroundChild::GetForCurrentThread());
|
|
|
|
|
|
|
|
BackgroundChildImpl::ThreadLocal* threadLocal =
|
|
|
|
BackgroundChildImpl::GetThreadLocalForCurrentThread();
|
|
|
|
MOZ_ASSERT(threadLocal);
|
|
|
|
|
|
|
|
return threadLocal->mCurrentFileHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::AssertIsOnOwningThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMutableFile);
|
|
|
|
mMutableFile->AssertIsOnOwningThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::SetBackgroundActor(BackgroundFileHandleChild* aActor)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aActor);
|
|
|
|
MOZ_ASSERT(!mBackgroundActor);
|
|
|
|
|
|
|
|
mBackgroundActor = aActor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::StartRequest(IDBFileRequest* aFileRequest,
|
|
|
|
const FileRequestParams& aParams)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aFileRequest);
|
|
|
|
MOZ_ASSERT(aParams.type() != FileRequestParams::T__None);
|
|
|
|
|
|
|
|
BackgroundFileRequestChild* actor =
|
|
|
|
new BackgroundFileRequestChild(aFileRequest);
|
|
|
|
|
|
|
|
mBackgroundActor->SendPBackgroundFileRequestConstructor(actor, aParams);
|
|
|
|
|
|
|
|
// Balanced in BackgroundFileRequestChild::Recv__delete__().
|
|
|
|
OnNewRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::OnNewRequest()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (!mPendingRequestCount) {
|
|
|
|
MOZ_ASSERT(mReadyState == INITIAL);
|
|
|
|
mReadyState = LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
++mPendingRequestCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::OnRequestFinished(bool aActorDestroyedNormally)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mPendingRequestCount);
|
|
|
|
|
|
|
|
--mPendingRequestCount;
|
|
|
|
|
|
|
|
if (!mPendingRequestCount && !mMutableFile->IsInvalidated()) {
|
|
|
|
mReadyState = FINISHING;
|
|
|
|
|
|
|
|
if (aActorDestroyedNormally) {
|
|
|
|
if (!mAborted) {
|
|
|
|
SendFinish();
|
|
|
|
} else {
|
|
|
|
SendAbort();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Don't try to send any more messages to the parent if the request actor
|
|
|
|
// was killed.
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(!mSentFinishOrAbort);
|
|
|
|
mSentFinishOrAbort = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::FireCompleteOrAbortEvents(bool aAborted)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mFiredCompleteOrAbort);
|
|
|
|
|
|
|
|
mReadyState = DONE;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mFiredCompleteOrAbort = true;
|
|
|
|
#endif
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
RefPtr<Event> event;
|
2017-06-07 13:36:42 +03:00
|
|
|
if (aAborted) {
|
|
|
|
event = CreateGenericEvent(this, nsDependentString(kAbortEventType),
|
|
|
|
eDoesBubble, eNotCancelable);
|
|
|
|
} else {
|
|
|
|
event = CreateGenericEvent(this, nsDependentString(kCompleteEventType),
|
|
|
|
eDoesNotBubble, eNotCancelable);
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
IgnoredErrorResult rv;
|
|
|
|
DispatchEvent(*event, rv);
|
|
|
|
if (rv.Failed()) {
|
2017-06-07 13:36:42 +03:00
|
|
|
NS_WARNING("DispatchEvent failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::IsOpen() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// If we haven't started anything then we're open.
|
|
|
|
if (mReadyState == INITIAL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already started then we need to check to see if we still have the
|
|
|
|
// mCreating flag set. If we do (i.e. we haven't returned to the event loop
|
|
|
|
// from the time we were created) then we are open. Otherwise check the
|
|
|
|
// currently running file handles to see if it's the same. We only allow other
|
|
|
|
// requests to be made if this file handle is currently running.
|
|
|
|
if (mReadyState == LOADING && (mCreating || GetCurrent() == this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::Abort()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (IsFinishingOrDone()) {
|
|
|
|
// Already started (and maybe finished) the finish or abort so there is
|
|
|
|
// nothing to do here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool isInvalidated = mMutableFile->IsInvalidated();
|
|
|
|
bool needToSendAbort = mReadyState == INITIAL && !isInvalidated;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (isInvalidated) {
|
|
|
|
mSentFinishOrAbort = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mAborted = true;
|
|
|
|
mReadyState = DONE;
|
|
|
|
|
|
|
|
// Fire the abort event if there are no outstanding requests. Otherwise the
|
|
|
|
// abort event will be fired when all outstanding requests finish.
|
|
|
|
if (needToSendAbort) {
|
|
|
|
SendAbort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::GetMetadata(const IDBFileMetadataParameters& aParameters,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// Common state checking
|
|
|
|
if (!CheckState(aRv)) {
|
2014-07-17 20:40:54 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
// Argument checking for get metadata.
|
|
|
|
if (!aParameters.mSize && !aParameters.mLastModified) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_METADATA_NOT_CONFIGURED>();
|
2014-07-17 20:40:54 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-14 11:57:41 +03:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
FileRequestGetMetadataParams params;
|
|
|
|
params.size() = aParameters.mSize;
|
|
|
|
params.lastModified() = aParameters.mLastModified;
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
RefPtr<IDBFileRequest> fileRequest = GenerateFileRequest(this);
|
|
|
|
|
|
|
|
StartRequest(fileRequest, params);
|
|
|
|
|
|
|
|
return fileRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State checking for write
|
|
|
|
if (!CheckStateForWrite(aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting location and additional state checking for truncate
|
|
|
|
uint64_t location;
|
|
|
|
if (aSize.WasPassed()) {
|
|
|
|
// Just in case someone calls us from C++
|
|
|
|
MOZ_ASSERT(aSize.Value() != UINT64_MAX, "Passed wrong size!");
|
|
|
|
location = aSize.Value();
|
|
|
|
} else {
|
|
|
|
if (mLocation == UINT64_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
location = mLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequestTruncateParams params;
|
|
|
|
params.offset() = location;
|
|
|
|
|
|
|
|
RefPtr<IDBFileRequest> fileRequest = GenerateFileRequest(this);
|
|
|
|
|
|
|
|
StartRequest(fileRequest, params);
|
|
|
|
|
|
|
|
if (aSize.WasPassed()) {
|
|
|
|
mLocation = aSize.Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::Flush(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State checking for write
|
|
|
|
if (!CheckStateForWrite(aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequestFlushParams params;
|
|
|
|
|
|
|
|
RefPtr<IDBFileRequest> fileRequest = GenerateFileRequest(this);
|
2015-09-09 14:15:05 +03:00
|
|
|
|
|
|
|
StartRequest(fileRequest, params);
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
return fileRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::Abort(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// This method is special enough for not using generic state checking methods.
|
|
|
|
|
|
|
|
if (IsFinishingOrDone()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::CheckState(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!IsOpen()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_INACTIVE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Common state checking
|
|
|
|
if (!CheckState(aRv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additional state checking for read
|
|
|
|
if (mLocation == UINT64_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Argument checking for read
|
|
|
|
if (!aSize) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_READ_SIZE>();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::CheckStateForWrite(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Common state checking
|
|
|
|
if (!CheckState(aRv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additional state checking for write
|
|
|
|
if (mMode != FileMode::Readwrite) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_READ_ONLY_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::CheckStateForWriteOrAppend(bool aAppend, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// State checking for write
|
|
|
|
if (!CheckStateForWrite(aRv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additional state checking for write
|
|
|
|
if (!aAppend && mLocation == UINT64_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::CheckWindow()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::Read(uint64_t aSize, bool aHasEncoding,
|
|
|
|
const nsAString& aEncoding, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State and argument checking for read
|
|
|
|
if (!CheckStateAndArgumentsForRead(aSize, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequestReadParams params;
|
|
|
|
params.offset() = mLocation;
|
|
|
|
params.size() = aSize;
|
|
|
|
|
|
|
|
RefPtr<IDBFileRequest> fileRequest = GenerateFileRequest(this);
|
|
|
|
if (aHasEncoding) {
|
|
|
|
fileRequest->SetEncoding(aEncoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
StartRequest(fileRequest, params);
|
|
|
|
|
|
|
|
mLocation += aSize;
|
|
|
|
|
|
|
|
return fileRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::WriteOrAppend(
|
|
|
|
const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (aValue.IsString()) {
|
|
|
|
return WriteOrAppend(aValue.GetAsString(), aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aValue.IsArrayBuffer()) {
|
|
|
|
return WriteOrAppend(aValue.GetAsArrayBuffer(), aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aValue.IsArrayBufferView()) {
|
|
|
|
return WriteOrAppend(aValue.GetAsArrayBufferView(), aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aValue.IsBlob());
|
|
|
|
return WriteOrAppend(aValue.GetAsBlob(), aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::WriteOrAppend(const nsAString& aValue,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State checking for write or append
|
|
|
|
if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 cstr(aValue);
|
|
|
|
|
|
|
|
uint64_t dataLength = cstr.Length();;
|
|
|
|
if (!dataLength) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequestStringData stringData(cstr);
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteInternal(stringData, dataLength, aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::WriteOrAppend(const ArrayBuffer& aValue,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State checking for write or append
|
|
|
|
if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue.ComputeLengthAndData();
|
|
|
|
|
|
|
|
uint64_t dataLength = aValue.Length();;
|
|
|
|
if (!dataLength) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* data = reinterpret_cast<const char*>(aValue.Data());
|
|
|
|
|
|
|
|
FileRequestStringData stringData;
|
|
|
|
if (NS_WARN_IF(!stringData.string().Assign(data, aValue.Length(),
|
|
|
|
fallible_t()))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteInternal(stringData, dataLength, aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::WriteOrAppend(const ArrayBufferView& aValue,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State checking for write or append
|
|
|
|
if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue.ComputeLengthAndData();
|
|
|
|
|
|
|
|
uint64_t dataLength = aValue.Length();;
|
|
|
|
if (!dataLength) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* data = reinterpret_cast<const char*>(aValue.Data());
|
|
|
|
|
|
|
|
FileRequestStringData stringData;
|
|
|
|
if (NS_WARN_IF(!stringData.string().Assign(data, aValue.Length(),
|
|
|
|
fallible_t()))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteInternal(stringData, dataLength, aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::WriteOrAppend(Blob& aValue,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// State checking for write or append
|
|
|
|
if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
uint64_t dataLength = aValue.GetSize(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dataLength) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBackgroundChild* backgroundActor = BackgroundChild::GetForCurrentThread();
|
|
|
|
MOZ_ASSERT(backgroundActor);
|
|
|
|
|
|
|
|
IPCBlob ipcBlob;
|
|
|
|
nsresult rv =
|
|
|
|
IPCBlobUtils::Serialize(aValue.Impl(), backgroundActor, ipcBlob);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequestBlobData blobData;
|
|
|
|
blobData.blob() = ipcBlob;
|
|
|
|
|
|
|
|
// Do nothing if the window is closed
|
|
|
|
if (!CheckWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteInternal(blobData, dataLength, aAppend, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileHandle::WriteInternal(const FileRequestData& aData,
|
|
|
|
uint64_t aDataLength,
|
|
|
|
bool aAppend,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
DebugOnly<ErrorResult> error;
|
|
|
|
MOZ_ASSERT(CheckStateForWrite(error));
|
|
|
|
MOZ_ASSERT_IF(!aAppend, mLocation != UINT64_MAX);
|
|
|
|
MOZ_ASSERT(aDataLength);
|
|
|
|
MOZ_ASSERT(CheckWindow());
|
|
|
|
|
|
|
|
FileRequestWriteParams params;
|
|
|
|
params.offset() = aAppend ? UINT64_MAX : mLocation;
|
|
|
|
params.data() = aData;
|
|
|
|
params.dataLength() = aDataLength;
|
|
|
|
|
|
|
|
RefPtr<IDBFileRequest> fileRequest = GenerateFileRequest(this);
|
|
|
|
MOZ_ASSERT(fileRequest);
|
|
|
|
|
|
|
|
StartRequest(fileRequest, params);
|
|
|
|
|
|
|
|
if (aAppend) {
|
|
|
|
mLocation = UINT64_MAX;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLocation += aDataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::SendFinish()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mAborted);
|
|
|
|
MOZ_ASSERT(IsFinishingOrDone());
|
|
|
|
MOZ_ASSERT(!mSentFinishOrAbort);
|
|
|
|
MOZ_ASSERT(!mPendingRequestCount);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mBackgroundActor);
|
|
|
|
mBackgroundActor->SendFinish();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mSentFinishOrAbort = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileHandle::SendAbort()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mAborted);
|
|
|
|
MOZ_ASSERT(IsFinishingOrDone());
|
|
|
|
MOZ_ASSERT(!mSentFinishOrAbort);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mBackgroundActor);
|
|
|
|
mBackgroundActor->SendAbort();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mSentFinishOrAbort = true;
|
|
|
|
#endif
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBFileHandle, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBFileHandle, DOMEventTargetHelper)
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFileHandle)
|
2014-07-17 20:40:54 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
|
2015-01-12 00:35:24 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2014-07-17 20:40:54 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBFileHandle)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBFileHandle,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMutableFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBFileHandle,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
// Don't unlink mMutableFile!
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBFileHandle::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
// We're back at the event loop, no longer newborn.
|
|
|
|
mCreating = false;
|
|
|
|
|
|
|
|
// Maybe finish if there were no requests generated.
|
|
|
|
if (mReadyState == INITIAL) {
|
|
|
|
mReadyState = DONE;
|
|
|
|
|
|
|
|
SendFinish();
|
|
|
|
}
|
2015-09-09 14:15:05 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
void
|
2016-10-21 05:11:07 +03:00
|
|
|
IDBFileHandle::GetEventTargetParent(EventChainPreVisitor& aVisitor)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
aVisitor.mCanHandle = true;
|
2017-12-18 19:07:36 +03:00
|
|
|
aVisitor.SetParentTarget(mMutableFile, false);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// virtual
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
IDBFileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2018-06-26 00:20:54 +03:00
|
|
|
return IDBFileHandle_Binding::Wrap(aCx, this, aGivenProto);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|