Files
palemoon27/dom/events/JSEventHandler.cpp
T
roytam1 59e31c9007 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1182987 - Part 1: Remove unreferenced files immediately; r=baku (3ced56ca25)
- Bug 1182987 - Part 2: Add getQuotaObjects() to mozIStorageConnection; r=mak (64b4dc418c)
- Bug 1182987 - Part 3: Add "cleanup" transaction with disabled quota checks and vacuuming/checkpointing after commit; r=baku (7c19f28ae2)
- Bug 1182987 - Part 4: Add a test for QuotaExceededError recovery and the new "cleanup" transaction type; r=baku (f91935e737)
- Bug 1182987 - Part 5: Change mode of "readwrite" transaction to "cleanup" after QuotaExceeded is fired; r=baku (79d709970d)
- Bug 1257725 part 3. Get rid of ThreadsafeAutoJSContext usage in Promise code. r=bholley (405d3c03d4)
- Bug 1257725 part 1. Get rid of ThreadsafeAutoJSContext usage in JSEventHandler::HandleEvent. r=smaug (3222a73565)
- Bug 1257725 part 4. Get rid of ThreadsafeAutoJSContext usage in IndexedDB code, except for IDBRequest::CaptureCaller. r=khuey (8ad88560f0)
- Bug 1257725 part 5. Get rid of ThreadsafeAutoJSContext usage in IDBRequest::CaptureCaller. r=khuey (50a1f05ec9)
- Bug 1257725 part 6. Get rid of ThreadsafeAutoJSContext. r=bholley (8968c69fcc)
- Bug 1247420 - part1: removeContentState. r=smaug (6c7a54b58e)
- Bug 1247420 - part2: IPC hover state management for select. r=Felipc (c7809aec7c)
- Bug 1223533 - Don't hide the select popup on irrelevant pagehide events. r=mconley (0cf218515a)
- Bug 1253486, [e10s only] hide select popups when the select element is removed, r=mconley (8a7049b6f1)
- Bug 1180827 - Fix reuse of previous search results. r=MattN (6b4e65d318)
- Bug 1242208 - Fix cached form history results with a datalist present. r=MattN (10078ada31)
- Bug 1252074 - test_form_autocomplete.html and test_form_autocomplete_with_list.html should pass on e10s. r=paolo (8a9cf4a5f1)
- Bug 1260441 - Never pass a null js context to OpenCursor() r=bz (8d818b0257)
- Bug 1170045 - part 2 - use SegmentedVector in the DeferredFinalize implementation; r=mccr8 (3954a5e390)
- Bug 1265770. Don't try to get a prototype for the interface object for an interface that's [NoInterfaceObject], since it's just unnecessary work that can't even be done at all in some cases (e.g. when the parent interface is also [NoInterfaceObject]). r=peterv (53d3077e31)
- Bug 1264187 - check for a ProtoAndIfaceCache before blindly destroying it; r=bz (97536e815b)
- Bug 1104955 part 3. Pass our unscopable names to CreateInterfaceObjects and have it define the right thing on the prototype. r=khuey (48386ab6b5)
- Bug 934889: Use JS_InitStandardClasses everywhere now that it works. r=bz (01d545259a)
- Bug 1258585. Remove some remaining vestiges of WebIDL quickstubs. r=peterv (3fa02388f1)
2024-05-03 10:52:27 +08:00

252 lines
8.0 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 "nsJSUtils.h"
#include "nsString.h"
#include "nsIServiceManager.h"
#include "nsIScriptSecurityManager.h"
#include "nsIScriptContext.h"
#include "nsIScriptGlobalObject.h"
#include "nsIXPConnect.h"
#include "nsIMutableArray.h"
#include "nsVariant.h"
#include "nsIDOMBeforeUnloadEvent.h"
#include "nsGkAtoms.h"
#include "xpcpublic.h"
#include "nsJSEnvironment.h"
#include "nsDOMJSUtils.h"
#include "WorkerPrivate.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/CycleCollectedJSRuntime.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/JSEventHandler.h"
#include "mozilla/Likely.h"
#include "mozilla/dom/ErrorEvent.h"
namespace mozilla {
using namespace dom;
JSEventHandler::JSEventHandler(nsISupports* aTarget,
nsIAtom* aType,
const TypedEventHandler& aTypedHandler)
: mEventName(aType)
, mTypedHandler(aTypedHandler)
{
nsCOMPtr<nsISupports> base = do_QueryInterface(aTarget);
mTarget = base.get();
// Note, we call HoldJSObjects to get CanSkip called before CC.
HoldJSObjects(this);
}
JSEventHandler::~JSEventHandler()
{
NS_ASSERTION(!mTarget, "Should have called Disconnect()!");
DropJSObjects(this);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(JSEventHandler)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(JSEventHandler)
tmp->mTypedHandler.ForgetHandler();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(JSEventHandler)
if (MOZ_UNLIKELY(cb.WantDebugInfo()) && tmp->mEventName) {
nsAutoCString name;
name.AppendLiteral("JSEventHandler handlerName=");
name.Append(
NS_ConvertUTF16toUTF8(nsDependentAtomString(tmp->mEventName)).get());
cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name.get());
} else {
NS_IMPL_CYCLE_COLLECTION_DESCRIBE(JSEventHandler, tmp->mRefCnt.get())
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mTypedHandler.Ptr())
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(JSEventHandler)
if (tmp->IsBlackForCC()) {
return true;
}
// If we have a target, it is the one which has tmp as onfoo handler.
if (tmp->mTarget) {
nsXPCOMCycleCollectionParticipant* cp = nullptr;
CallQueryInterface(tmp->mTarget, &cp);
nsISupports* canonical = nullptr;
tmp->mTarget->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
reinterpret_cast<void**>(&canonical));
// Usually CanSkip ends up unmarking the event listeners of mTarget,
// so tmp may become black.
if (cp && canonical && cp->CanSkip(canonical, true)) {
return tmp->IsBlackForCC();
}
}
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(JSEventHandler)
return tmp->IsBlackForCC();
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(JSEventHandler)
return tmp->IsBlackForCC();
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSEventHandler)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(JSEventHandler)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(JSEventHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(JSEventHandler)
bool
JSEventHandler::IsBlackForCC()
{
// We can claim to be black if all the things we reference are
// effectively black already.
return !mTypedHandler.HasEventHandler() ||
!mTypedHandler.Ptr()->HasGrayCallable();
}
nsresult
JSEventHandler::HandleEvent(nsIDOMEvent* aEvent)
{
nsCOMPtr<EventTarget> target = do_QueryInterface(mTarget);
if (!target || !mTypedHandler.HasEventHandler() ||
!GetTypedEventHandler().Ptr()->CallbackPreserveColor()) {
return NS_ERROR_FAILURE;
}
Event* event = aEvent->InternalDOMEvent();
bool isMainThread = event->IsMainThreadEvent();
bool isChromeHandler =
isMainThread ?
nsContentUtils::ObjectPrincipal(
GetTypedEventHandler().Ptr()->CallbackPreserveColor()) ==
nsContentUtils::GetSystemPrincipal() :
mozilla::dom::workers::IsCurrentThreadRunningChromeWorker();
if (mTypedHandler.Type() == TypedEventHandler::eOnError) {
MOZ_ASSERT_IF(mEventName, mEventName == nsGkAtoms::onerror);
nsString errorMsg, file;
EventOrString msgOrEvent;
Optional<nsAString> fileName;
Optional<uint32_t> lineNumber;
Optional<uint32_t> columnNumber;
Optional<JS::Handle<JS::Value>> error;
NS_ENSURE_TRUE(aEvent, NS_ERROR_UNEXPECTED);
ErrorEvent* scriptEvent = aEvent->InternalDOMEvent()->AsErrorEvent();
if (scriptEvent) {
scriptEvent->GetMessage(errorMsg);
msgOrEvent.SetAsString().Rebind(errorMsg.Data(), errorMsg.Length());
scriptEvent->GetFilename(file);
fileName = &file;
lineNumber.Construct();
lineNumber.Value() = scriptEvent->Lineno();
columnNumber.Construct();
columnNumber.Value() = scriptEvent->Colno();
error.Construct(nsContentUtils::RootingCxForThread());
scriptEvent->GetError(&error.Value());
} else {
msgOrEvent.SetAsEvent() = aEvent->InternalDOMEvent();
}
RefPtr<OnErrorEventHandlerNonNull> handler =
mTypedHandler.OnErrorEventHandler();
ErrorResult rv;
bool handled = handler->Call(mTarget, msgOrEvent, fileName, lineNumber,
columnNumber, error, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
if (handled) {
event->PreventDefaultInternal(isChromeHandler);
}
return NS_OK;
}
if (mTypedHandler.Type() == TypedEventHandler::eOnBeforeUnload) {
MOZ_ASSERT(mEventName == nsGkAtoms::onbeforeunload);
RefPtr<OnBeforeUnloadEventHandlerNonNull> handler =
mTypedHandler.OnBeforeUnloadEventHandler();
ErrorResult rv;
nsString retval;
handler->Call(mTarget, *(aEvent->InternalDOMEvent()), retval, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
nsCOMPtr<nsIDOMBeforeUnloadEvent> beforeUnload = do_QueryInterface(aEvent);
NS_ENSURE_STATE(beforeUnload);
if (!DOMStringIsNull(retval)) {
event->PreventDefaultInternal(isChromeHandler);
nsAutoString text;
beforeUnload->GetReturnValue(text);
// Set the text in the beforeUnload event as long as it wasn't
// already set (through event.returnValue, which takes
// precedence over a value returned from a JS function in IE)
if (text.IsEmpty()) {
beforeUnload->SetReturnValue(retval);
}
}
return NS_OK;
}
MOZ_ASSERT(mTypedHandler.Type() == TypedEventHandler::eNormal);
ErrorResult rv;
RefPtr<EventHandlerNonNull> handler = mTypedHandler.NormalEventHandler();
JS::Rooted<JS::Value> retval(CycleCollectedJSRuntime::Get()->Runtime());
handler->Call(mTarget, *(aEvent->InternalDOMEvent()), &retval, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
// If the handler returned false and its sense is not reversed,
// or the handler returned true and its sense is reversed from
// the usual (false means cancel), then prevent default.
if (retval.isBoolean() &&
retval.toBoolean() == (mEventName == nsGkAtoms::onerror ||
mEventName == nsGkAtoms::onmouseover)) {
event->PreventDefaultInternal(isChromeHandler);
}
return NS_OK;
}
} // namespace mozilla
using namespace mozilla;
/*
* Factory functions
*/
nsresult
NS_NewJSEventHandler(nsISupports* aTarget,
nsIAtom* aEventType,
const TypedEventHandler& aTypedHandler,
JSEventHandler** aReturn)
{
NS_ENSURE_ARG(aEventType || !NS_IsMainThread());
JSEventHandler* it =
new JSEventHandler(aTarget, aEventType, aTypedHandler);
NS_ADDREF(*aReturn = it);
return NS_OK;
}