Files
palemoon27/dom/base/DOMRequest.cpp
T
roytam1 7127a02a35 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1256424. Get rid of ThreadsafeAutoSafeJSContext. r=bholley (445aa7dd4b)
- Bug 1256424 followup to actually address the review comments (d085cf1900)
- Bug 1003432: Expose CustomEvent in Worker. r=smaug (3b143dc6e4)
- Bug 1256414 - Hide MozSettingsEvent from the Web; r=khuey (52fe18e823)
- Bug 1257038: Remove the worker descriptor for WorkerLocation. r=bz (2b9721e4fe)
- Bug 1257039: Remove the worker descriptor for FileReaderSync. r=bz (cc33ce76e6)
- Bug 1257355: Remove the worker descriptor for WorkerNavigator. r=bz (067f1fc9ea)
- Bug 1257480 - null check for GetOrCreateGlobalScope() in WorkerDebuggerGlobalScope, r=khuey (0d3d640fc0)
- Make split-profile run jprof in the directory with the jprof-log. No bug. (58f5060e39)
- Bug 1250333 - do not create accessibles for trailing BRs, r=davidb, roc (d8e1193adc)
- Bug 1251712 - propagate a context flag for alerts, r=davdib (d5ef6167ea)
- Bug 1251752 - logging: add tree specific methods, r=yzen (f974c207ca)
- Bug 1251218 - add special TreeWalker constructor for children creation, r=marcoz (74f939b93e)
- Bug 1251337 - TreeWalker doesn't have to check ARIA owned children for each DOM state, r=yzen (19d83af7e1)
- Bug 1249730 - make TreeWalker bi-directional, r=yzen (a229a591e1)
- Bug 1249730 - make TreeWalker bi-directional, r=yzen (5acc1155b1)
- Bug 1251743 - ARIA owns reallocation may insert a child at wrong index, r=yzen (d95065109b)
- Bug 1249253 - content removal processing can wrongly remove ARIA owned children, r=yzen (45df52f4c9)
- Bug 1254989 - extend TreeWalker::Next to accept a stopper node, r=yzen (c5dfbbdc96)
- Bug 1249730 - make TreeWalker bi-directional, follow up fix, r=yzen (747504b5ec)
- Bug 1196652 - OriginSuffix is shown in about:serviceworker on b2g. r=ferjm (f21d534755)
- Bug 1224570 - [Service Workers Panel] Service Workers panel fails to show registered service workers after restart. r=fabrice (7d7e95db4c)
- Bug 1169674 - Use originNoSuffix for permission event. r=fabrice, f=bholley (8f389cb0c7)
- Bug 1207499 - Part 1: Remove use of expression closure from b2g/. r=sicking (42b96cbfdf)
- Bug 1207499 - Part 2: Remove use of expression closure from chrome/. r=bsmedberg (7abb390349)
- Bug 1207499 - Part 3: Remove use of expression closure from docshell/. r=bz (aee60733a9)
- Bug 1207499 - Part 5: Remove use of expression closure from modules/. r=mwu (6c29c775fe)
- Bug 1147562 - Update remaining callsites of newChannel before landing the shim in netwerk/ (r=jduell) (2d37fab088)
- Bug 1207499 - Part 6: Remove use of expression closure from netwerk/. r=jduell (2881b3450f)
- Bug 1207499 - Part 7: Remove use of expression closure from parser/.  r=jst (2519fb1fff)
- Bug 1207499 - Part 8: Remove use of expression closure from security/. r=keeler (de33d27e8f)
- Bug 1207499 - Part 12: Remove use of expression closure from widget/. r=roc (0e7fd889d9)
- Bug 1207499 - Part 13: Remove use of expression closure from xpcom/. r=froydnj (f95a4eefde)
- Bug 1249219 - Part 1: Define NonOwningAnimationTarget. r=birtles (c13d77b5d9)
- Bug 1249219 - Part 2: Remove struct PseudoElementHashKey. r=birtles (f8ff47d484)
- Bug 1249219 - Part 3: Replace Pair<Element*, CSSPseudoElementType> with NonOwningAnimationTarget. r=birtles (68a1a5e149)
- Bug 1218620 - Allow opacity animation running on compositor even if the frame has any restricted transforms. r=birtles (0f26c81fb0)
- Bug 1254419 - Move GetPropertyState alongside GetFrames; r=hiro (046dbce30e)
- Bug 1246320 part 0 - Whitespace fixes; r=whitespace-only (eda3e8b8ae)
- Bug 1247531 - Annotate intentional switch fallthrough to suppress -Wimplicit-fallthrough warning in dom/animations/. r=dholbert (fbdff98c25)
- Bug 1246320 part 1 - Add AnimationUtils::GetCurrentRealmDocument; r=bz (53c52acbe7)
- Bug 1246320 part 2 - Pass document to ParseEasing; r=hiro (269325c142)
- Bug 1246320 part 3 - Rework KeyframeEffect(ReadOnly) constructor helpers; r=hiro (e0c58fbe49)
- Bug 1246320 part 4 - Pass a document to TimingParams; r=hiro (f9ef7bc956)
- Bug 1246320 part 5 - Simplify KeyframeEffect(ReadOnly) Constructor overloads further; r=hiro (ec932de9a7)
- Bug 1254419 - Rename getPropertyState() to getProperties(); r=heycam, r=bz (4ab86c889d)
- Bug 1254419 - Fix zero-length segment handling; r=heycam (a638e5bbd7)
- Bug 1254419 - Fill in values sequence in getProperties(); r=heycam (c7c233f5ce)
- Bug 1254419 - Return animation property information from getProperties() even if the property is overridden; r=hiro (55537b4445)
- Bug 1254419 - Add values member to AnimationPropertyDetails; r=heycam, r=bz (3ea6c1fb7f)
- Bug 1254419 - Add tests for getProperties(); r=heycam (ff9999286d)
- Bug 1254419 - Make always-set members of AnimationProperty(Value)Details required; r=bz (07ef47d79a)
- Bug 1254419 - Throw if we fail to allocate memory for a values array in getProperties(); r=bz (51ae5d9f99)
2024-03-29 09:22:21 +08:00

383 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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/. */
#include "DOMRequest.h"
#include "DOMError.h"
#include "nsThreadUtils.h"
#include "DOMCursor.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/ScriptSettings.h"
#include "jsfriendapi.h"
#include "nsContentUtils.h"
using mozilla::dom::AnyCallback;
using mozilla::dom::DOMError;
using mozilla::dom::DOMRequest;
using mozilla::dom::DOMRequestService;
using mozilla::dom::DOMCursor;
using mozilla::dom::Promise;
using mozilla::dom::AutoJSAPI;
DOMRequest::DOMRequest(nsPIDOMWindow* aWindow)
: DOMEventTargetHelper(aWindow->IsInnerWindow() ?
aWindow : aWindow->GetCurrentInnerWindow())
, mResult(JS::UndefinedValue())
, mDone(false)
{
}
DOMRequest::DOMRequest(nsIGlobalObject* aGlobal)
: DOMEventTargetHelper(aGlobal)
, mResult(JS::UndefinedValue())
, mDone(false)
{
}
DOMRequest::~DOMRequest()
{
mResult.setUndefined();
mozilla::DropJSObjects(this);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMRequest)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DOMRequest,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mError)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DOMRequest,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mError)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
tmp->mResult.setUndefined();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(DOMRequest,
DOMEventTargetHelper)
// Don't need NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER because
// DOMEventTargetHelper does it for us.
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mResult)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMRequest)
NS_INTERFACE_MAP_ENTRY(nsIDOMDOMRequest)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(DOMRequest, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(DOMRequest, DOMEventTargetHelper)
/* virtual */ JSObject*
DOMRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return DOMRequestBinding::Wrap(aCx, this, aGivenProto);
}
NS_IMPL_EVENT_HANDLER(DOMRequest, success)
NS_IMPL_EVENT_HANDLER(DOMRequest, error)
NS_IMETHODIMP
DOMRequest::GetReadyState(nsAString& aReadyState)
{
DOMRequestReadyState readyState = ReadyState();
switch (readyState) {
case DOMRequestReadyState::Pending:
aReadyState.AssignLiteral("pending");
break;
case DOMRequestReadyState::Done:
aReadyState.AssignLiteral("done");
break;
default:
MOZ_CRASH("Unrecognized readyState.");
}
return NS_OK;
}
NS_IMETHODIMP
DOMRequest::GetResult(JS::MutableHandle<JS::Value> aResult)
{
GetResult(nullptr, aResult);
return NS_OK;
}
NS_IMETHODIMP
DOMRequest::GetError(nsISupports** aError)
{
NS_IF_ADDREF(*aError = GetError());
return NS_OK;
}
void
DOMRequest::FireSuccess(JS::Handle<JS::Value> aResult)
{
NS_ASSERTION(!mDone, "mDone shouldn't have been set to true already!");
NS_ASSERTION(!mError, "mError shouldn't have been set!");
NS_ASSERTION(mResult.isUndefined(), "mResult shouldn't have been set!");
mDone = true;
if (aResult.isGCThing()) {
RootResultVal();
}
mResult = aResult;
FireEvent(NS_LITERAL_STRING("success"), false, false);
if (mPromise) {
mPromise->MaybeResolve(mResult);
}
}
void
DOMRequest::FireError(const nsAString& aError)
{
NS_ASSERTION(!mDone, "mDone shouldn't have been set to true already!");
NS_ASSERTION(!mError, "mError shouldn't have been set!");
NS_ASSERTION(mResult.isUndefined(), "mResult shouldn't have been set!");
mDone = true;
mError = new DOMError(GetOwner(), aError);
FireEvent(NS_LITERAL_STRING("error"), true, true);
if (mPromise) {
mPromise->MaybeRejectBrokenly(mError);
}
}
void
DOMRequest::FireError(nsresult aError)
{
NS_ASSERTION(!mDone, "mDone shouldn't have been set to true already!");
NS_ASSERTION(!mError, "mError shouldn't have been set!");
NS_ASSERTION(mResult.isUndefined(), "mResult shouldn't have been set!");
mDone = true;
mError = new DOMError(GetOwner(), aError);
FireEvent(NS_LITERAL_STRING("error"), true, true);
if (mPromise) {
mPromise->MaybeRejectBrokenly(mError);
}
}
void
DOMRequest::FireDetailedError(DOMError* aError)
{
NS_ASSERTION(!mDone, "mDone shouldn't have been set to true already!");
NS_ASSERTION(!mError, "mError shouldn't have been set!");
NS_ASSERTION(mResult.isUndefined(), "mResult shouldn't have been set!");
NS_ASSERTION(aError, "No detailed error provided");
mDone = true;
mError = aError;
FireEvent(NS_LITERAL_STRING("error"), true, true);
if (mPromise) {
mPromise->MaybeRejectBrokenly(mError);
}
}
void
DOMRequest::FireEvent(const nsAString& aType, bool aBubble, bool aCancelable)
{
if (NS_FAILED(CheckInnerWindowCorrectness())) {
return;
}
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
event->InitEvent(aType, aBubble, aCancelable);
event->SetTrusted(true);
bool dummy;
DispatchEvent(event, &dummy);
}
void
DOMRequest::RootResultVal()
{
mozilla::HoldJSObjects(this);
}
void
DOMRequest::Then(JSContext* aCx, AnyCallback* aResolveCallback,
AnyCallback* aRejectCallback,
JS::MutableHandle<JS::Value> aRetval,
mozilla::ErrorResult& aRv)
{
if (!mPromise) {
mPromise = Promise::Create(DOMEventTargetHelper::GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
if (mDone) {
// Since we create mPromise lazily, it's possible that the DOMRequest object
// has already fired its success/error event. In that case we should
// manually resolve/reject mPromise here. mPromise will take care of
// calling the callbacks on |promise| as needed.
if (mError) {
mPromise->MaybeRejectBrokenly(mError);
} else {
mPromise->MaybeResolve(mResult);
}
}
}
// Just use the global of the Promise itself as the callee global.
JS::Rooted<JSObject*> global(aCx, mPromise->PromiseObj());
global = js::GetGlobalForObjectCrossCompartment(global);
mPromise->Then(aCx, global, aResolveCallback, aRejectCallback, aRetval, aRv);
}
NS_IMPL_ISUPPORTS(DOMRequestService, nsIDOMRequestService)
NS_IMETHODIMP
DOMRequestService::CreateRequest(nsIDOMWindow* aWindow,
nsIDOMDOMRequest** aRequest)
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(aWindow));
NS_ENSURE_STATE(win);
NS_ADDREF(*aRequest = new DOMRequest(win));
return NS_OK;
}
NS_IMETHODIMP
DOMRequestService::CreateCursor(nsIDOMWindow* aWindow,
nsICursorContinueCallback* aCallback,
nsIDOMDOMCursor** aCursor)
{
nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(aWindow));
NS_ENSURE_STATE(win);
NS_ADDREF(*aCursor = new DOMCursor(win, aCallback));
return NS_OK;
}
NS_IMETHODIMP
DOMRequestService::FireSuccess(nsIDOMDOMRequest* aRequest,
JS::Handle<JS::Value> aResult)
{
NS_ENSURE_STATE(aRequest);
static_cast<DOMRequest*>(aRequest)->FireSuccess(aResult);
return NS_OK;
}
NS_IMETHODIMP
DOMRequestService::FireError(nsIDOMDOMRequest* aRequest,
const nsAString& aError)
{
NS_ENSURE_STATE(aRequest);
static_cast<DOMRequest*>(aRequest)->FireError(aError);
return NS_OK;
}
NS_IMETHODIMP
DOMRequestService::FireDetailedError(nsIDOMDOMRequest* aRequest,
nsISupports* aError)
{
NS_ENSURE_STATE(aRequest);
nsCOMPtr<DOMError> err = do_QueryInterface(aError);
NS_ENSURE_STATE(err);
static_cast<DOMRequest*>(aRequest)->FireDetailedError(err);
return NS_OK;
}
class FireSuccessAsyncTask : public nsRunnable
{
FireSuccessAsyncTask(DOMRequest* aRequest,
const JS::Value& aResult) :
mReq(aRequest),
mResult(nsContentUtils::RootingCxForThread(), aResult)
{
}
public:
// Due to the fact that initialization can fail during shutdown (since we
// can't fetch a js context), set up an initiatization function to make sure
// we can return the failure appropriately
static nsresult
Dispatch(DOMRequest* aRequest,
const JS::Value& aResult)
{
RefPtr<FireSuccessAsyncTask> asyncTask =
new FireSuccessAsyncTask(aRequest, aResult);
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(asyncTask)));
return NS_OK;
}
NS_IMETHODIMP
Run()
{
mReq->FireSuccess(JS::Handle<JS::Value>::fromMarkedLocation(mResult.address()));
return NS_OK;
}
private:
RefPtr<DOMRequest> mReq;
JS::PersistentRooted<JS::Value> mResult;
};
class FireErrorAsyncTask : public nsRunnable
{
public:
FireErrorAsyncTask(DOMRequest* aRequest,
const nsAString& aError) :
mReq(aRequest),
mError(aError)
{
}
NS_IMETHODIMP
Run()
{
mReq->FireError(mError);
return NS_OK;
}
private:
RefPtr<DOMRequest> mReq;
nsString mError;
};
NS_IMETHODIMP
DOMRequestService::FireSuccessAsync(nsIDOMDOMRequest* aRequest,
JS::Handle<JS::Value> aResult)
{
NS_ENSURE_STATE(aRequest);
return FireSuccessAsyncTask::Dispatch(static_cast<DOMRequest*>(aRequest), aResult);
}
NS_IMETHODIMP
DOMRequestService::FireErrorAsync(nsIDOMDOMRequest* aRequest,
const nsAString& aError)
{
NS_ENSURE_STATE(aRequest);
nsCOMPtr<nsIRunnable> asyncTask =
new FireErrorAsyncTask(static_cast<DOMRequest*>(aRequest), aError);
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(asyncTask)));
return NS_OK;
}
NS_IMETHODIMP
DOMRequestService::FireDone(nsIDOMDOMCursor* aCursor) {
NS_ENSURE_STATE(aCursor);
static_cast<DOMCursor*>(aCursor)->FireDone();
return NS_OK;
}