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"
|
|
|
|
|
|
|
|
#include "IDBEvents.h"
|
|
|
|
#include "IDBMutableFile.h"
|
2015-09-09 14:15:05 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "mozilla/dom/IDBFileHandleBinding.h"
|
2015-09-09 14:15:05 +03:00
|
|
|
#include "mozilla/dom/filehandle/ActorsChild.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#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;
|
2014-09-27 03:21:57 +04:00
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
IDBFileHandle::IDBFileHandle(FileMode aMode,
|
|
|
|
IDBMutableFile* aMutableFile)
|
2015-09-09 14:15:05 +03:00
|
|
|
: FileHandleBase(DEBUGONLY(aMutableFile->OwningThread(),)
|
|
|
|
aMode)
|
2014-07-17 20:40:54 +04:00
|
|
|
, mMutableFile(aMutableFile)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IDBFileHandle::~IDBFileHandle()
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
mMutableFile->UnregisterFileHandle(this);
|
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 =
|
2015-09-09 14:15:05 +03:00
|
|
|
new IDBFileHandle(aMode, aMutableFile);
|
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);
|
|
|
|
nsContentUtils::RunInMetastableState(runnable.forget());
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
fileHandle->SetCreating();
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
aMutableFile->RegisterFileHandle(fileHandle);
|
|
|
|
|
|
|
|
return fileHandle.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();
|
2015-09-09 14:15:05 +03:00
|
|
|
|
|
|
|
StartRequest(fileRequest, params);
|
|
|
|
|
|
|
|
return fileRequest.forget().downcast<IDBFileRequest>();
|
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
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileHandle)
|
|
|
|
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();
|
|
|
|
|
|
|
|
OnReturnToEventLoop();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
IDBFileHandle::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
aVisitor.mCanHandle = true;
|
|
|
|
aVisitor.mParentTarget = mMutableFile;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
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
|
|
|
return IDBFileHandleBinding::Wrap(aCx, this, aGivenProto);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
mozilla::dom::MutableFileBase*
|
|
|
|
IDBFileHandle::MutableFile() const
|
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
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
return mMutableFile;
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
void
|
|
|
|
IDBFileHandle::HandleCompleteOrAbort(bool aAborted)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
FileHandleBase::HandleCompleteOrAbort(aAborted);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
if (aAborted) {
|
2014-09-27 03:21:57 +04:00
|
|
|
event = CreateGenericEvent(this, nsDependentString(kAbortEventType),
|
2014-07-17 20:40:54 +04:00
|
|
|
eDoesBubble, eNotCancelable);
|
|
|
|
} else {
|
2014-09-27 03:21:57 +04:00
|
|
|
event = CreateGenericEvent(this, nsDependentString(kCompleteEventType),
|
2014-07-17 20:40:54 +04:00
|
|
|
eDoesNotBubble, eNotCancelable);
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!event)) {
|
2015-09-09 14:15:05 +03:00
|
|
|
return;
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
if (NS_FAILED(DispatchEvent(event, &dummy))) {
|
2015-09-09 14:15:05 +03:00
|
|
|
NS_WARNING("DispatchEvent failed!");
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBFileHandle::CheckWindow()
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozilla::dom::FileRequestBase>
|
|
|
|
IDBFileHandle::GenerateFileRequest()
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
return IDBFileRequest::Create(GetOwner(), this,
|
|
|
|
/* aWrapAsDOMRequest */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|